70e89d6bfa582f57ce19f54d514106382faac091
[nonrtric.git] / dmaap-adaptor-java / src / test / java / org / oran / dmaapadapter / ConsumerController.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2021 Nordix Foundation
6  * %%
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20
21 package org.oran.dmaapadapter;
22
23 import io.swagger.v3.oas.annotations.Operation;
24 import io.swagger.v3.oas.annotations.media.Content;
25 import io.swagger.v3.oas.annotations.media.Schema;
26 import io.swagger.v3.oas.annotations.responses.ApiResponse;
27 import io.swagger.v3.oas.annotations.responses.ApiResponses;
28 import io.swagger.v3.oas.annotations.tags.Tag;
29
30 import java.lang.invoke.MethodHandles;
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.List;
34
35 import org.oran.dmaapadapter.controllers.VoidResponse;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.http.HttpStatus;
39 import org.springframework.http.MediaType;
40 import org.springframework.http.ResponseEntity;
41 import org.springframework.web.bind.annotation.PostMapping;
42 import org.springframework.web.bind.annotation.RequestBody;
43 import org.springframework.web.bind.annotation.RestController;
44
45 @RestController("ConsumerSimulatorController")
46 @Tag(name = "Test Consumer Simulator (exists only in test)")
47 public class ConsumerController {
48
49     private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
50
51     public static final String CONSUMER_TARGET_URL = "/consumer";
52
53     public static class TestResults {
54
55         public List<String> receivedBodies = Collections.synchronizedList(new ArrayList<String>());
56
57         public TestResults() {}
58
59         public boolean hasReceived(String str) {
60             for (String received : receivedBodies) {
61                 if (received.equals(str)) {
62                     return true;
63                 }
64             }
65             return false;
66         }
67
68         public void reset() {
69             receivedBodies.clear();
70         }
71     }
72
73     final TestResults testResults = new TestResults();
74
75     @PostMapping(path = CONSUMER_TARGET_URL, produces = MediaType.APPLICATION_JSON_VALUE)
76     @Operation(summary = "Consume data", description = "The call is invoked to push data to consumer")
77     @ApiResponses(value = { //
78             @ApiResponse(responseCode = "200", description = "OK", //
79                     content = @Content(schema = @Schema(implementation = VoidResponse.class))) //
80     })
81     public ResponseEntity<Object> postData(@RequestBody String body) {
82         logger.info("Received by consumer: {}", body);
83         testResults.receivedBodies.add(body);
84         return new ResponseEntity<>(HttpStatus.OK);
85     }
86
87 }