2 * ========================LICENSE_START=================================
5 * Copyright (C) 2021 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package org.oran.dmaapadapter;
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;
30 import java.lang.invoke.MethodHandles;
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.List;
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;
45 @RestController("ConsumerSimulatorController")
46 @Tag(name = "Test Consumer Simulator (exists only in test)")
47 public class ConsumerController {
49 private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
51 public static final String CONSUMER_TARGET_URL = "/consumer";
53 public static class TestResults {
55 public List<String> receivedBodies = Collections.synchronizedList(new ArrayList<String>());
57 public TestResults() {}
59 public boolean hasReceived(String str) {
60 for (String received : receivedBodies) {
61 if (received.equals(str)) {
69 receivedBodies.clear();
73 final TestResults testResults = new TestResults();
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))) //
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);