1dbe83f7f2d417f2b4646570da99917abbf7748d
[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 = "Consts.PRODUCER_API_CALLBACKS_NAME")
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 void reset() {
60             receivedBodies.clear();
61         }
62     }
63
64     final TestResults testResults = new TestResults();
65
66     @PostMapping(path = CONSUMER_TARGET_URL, produces = MediaType.APPLICATION_JSON_VALUE)
67     @Operation(summary = "GET from topic", description = "The call is invoked to push data to consumer")
68     @ApiResponses(value = { //
69             @ApiResponse(responseCode = "200", description = "OK", //
70                     content = @Content(schema = @Schema(implementation = VoidResponse.class))) //
71     })
72     public ResponseEntity<Object> postData(@RequestBody String body) {
73         logger.info("Received by consumer: {}", body);
74         testResults.receivedBodies.add(body);
75         return new ResponseEntity<>(HttpStatus.OK);
76     }
77
78 }