diff options
Diffstat (limited to 'pnfsimulator/integration')
2 files changed, 80 insertions, 13 deletions
diff --git a/pnfsimulator/integration/src/main/java/org/onap/pnfsimulator/integration/VesSimulatorController.java b/pnfsimulator/integration/src/main/java/org/onap/pnfsimulator/integration/VesSimulatorController.java index 70e0c60..304df60 100644 --- a/pnfsimulator/integration/src/main/java/org/onap/pnfsimulator/integration/VesSimulatorController.java +++ b/pnfsimulator/integration/src/main/java/org/onap/pnfsimulator/integration/VesSimulatorController.java @@ -22,18 +22,27 @@ package org.onap.pnfsimulator.integration; import com.google.gson.Gson; import com.google.gson.JsonObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; + @RequestMapping("ves-simulator") @RestController public class VesSimulatorController { + private static final Logger LOGGER = LoggerFactory.getLogger(VesSimulatorController.class); private final VesSimulatorService vesSimulatorService; private final Gson gson; + private final ResponseEntity<String> response = ResponseEntity + .status(HttpStatus.ACCEPTED) + .body("Accepted"); @Autowired public VesSimulatorController(VesSimulatorService vesSimulatorService, Gson gson) { @@ -42,18 +51,21 @@ public class VesSimulatorController { } @PostMapping("eventListener/v5") - String sendEventToDmaapV5(@RequestBody String body) { - System.out.println("Received event" + body); - JsonObject jsonObject = gson.fromJson(body, JsonObject.class); + public ResponseEntity<String> sendEventToDmaapV5(@RequestBody String body) { + JsonObject jsonObject = getJsonObjectFromBody(body); vesSimulatorService.sendEventToDmaapV5(jsonObject); - return "MessageAccepted"; + return response; } @PostMapping("eventListener/v7") - String sendEventToDmaapV7(@RequestBody String body) { - System.out.println("Received event" + body); - JsonObject jsonObject = gson.fromJson(body, JsonObject.class); + public ResponseEntity<String> sendEventToDmaapV7(@RequestBody String body) { + JsonObject jsonObject = getJsonObjectFromBody(body); vesSimulatorService.sendEventToDmaapV7(jsonObject); - return "MessageAccepted"; + return response; + } + + private JsonObject getJsonObjectFromBody(@RequestBody String body) { + LOGGER.info(String.format("Received event: %s", body)); + return gson.fromJson(body, JsonObject.class); } } diff --git a/pnfsimulator/integration/src/test/java/org/onap/pnfsimulator/integration/OptionalTemplatesTest.java b/pnfsimulator/integration/src/test/java/org/onap/pnfsimulator/integration/OptionalTemplatesTest.java index a5ffe4d..8c8767d 100644 --- a/pnfsimulator/integration/src/test/java/org/onap/pnfsimulator/integration/OptionalTemplatesTest.java +++ b/pnfsimulator/integration/src/test/java/org/onap/pnfsimulator/integration/OptionalTemplatesTest.java @@ -23,6 +23,7 @@ package org.onap.pnfsimulator.integration; import static io.restassured.RestAssured.given; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.stringContainsInOrder; import com.google.gson.JsonObject; import com.mongodb.MongoClient; @@ -39,6 +40,8 @@ import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; import java.util.Collections; +import java.util.List; + import org.assertj.core.api.Assertions; import org.bson.Document; import org.junit.After; @@ -50,6 +53,7 @@ import org.mockito.Mockito; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.http.HttpStatus; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @@ -109,8 +113,8 @@ public class OptionalTemplatesTest { .when() .post(SINGLE_EVENT_URL) .then() - .statusCode(202) - .body("message", equalTo("One-time direct event sent successfully")); + .statusCode(HttpStatus.ACCEPTED.value()) + .body("message", equalTo("Accepted")); //then long afterExecution = Instant.now().getEpochSecond(); @@ -124,7 +128,7 @@ public class OptionalTemplatesTest { .get("sourceName").getAsString()).isEqualTo("Single_sourceName"); assertThat(value .getAsJsonObject(COMMON_EVENT_HEADER) - .get("eventId1").getAsString().length()).isEqualTo(20); + .get("eventId1").getAsString()).hasSize(20); assertThat(value .getAsJsonObject(COMMON_EVENT_HEADER) .get("eventId2").getAsString()).isEqualTo("10"); @@ -160,8 +164,8 @@ public class OptionalTemplatesTest { .when() .post(SINGLE_EVENT_URL) .then() - .statusCode(202) - .body("message", equalTo("One-time direct event sent successfully")); + .statusCode(HttpStatus.ACCEPTED.value()) + .body("message", equalTo("Accepted")); //then Mockito.verify(vesSimulatorService, @@ -173,6 +177,57 @@ public class OptionalTemplatesTest { .isEqualTo("{\"commonEventHeader\":{\"sourceName\":\"HistoricalEvent\",\"version\":3}}"); } + @Test + public void whenTriggeredSimulatorWithWrongVesIpInformationShouldBeReturned() { + //given + String body = "{\n" + + "\"vesServerUrl\": \"https://" + currentVesSimulatorIp + ":8080/ves-simulator/eventListener/v5\",\n" + + "\"event\": { \n" + + "\"commonEventHeader\": {\n" + + "\"sourceName\": \"HistoricalEvent\",\n" + + "\"version\": 3" + + "}\n" + + "}\n" + + "}"; + + //when + given() + .contentType("application/json") + .body(body) + .when() + .post(SINGLE_EVENT_URL) + .then() + .statusCode(421) + .body("message", + equalTo( + "Fail to connect with ves: Connect to "+currentVesSimulatorIp+":8080 " + + "[/"+currentVesSimulatorIp+"] " + + "failed: Connection refused (Connection refused)")); + } + + @Test + public void whenTriggeredSimulatorWithWrongEventShouldReturnedError() { + //given + String body = "{\n" + + "\"vesServerUrl\": \"https://" + currentVesSimulatorIp + ":9443/ves-simulator/eventListener/v5\",\n" + + "\"event\": { \n" + + "this is not JSON {}" + + "}\n" + + "}"; + + //when + given() + .contentType("application/json") + .body(body) + .when() + .post(SINGLE_EVENT_URL) + .then() + .statusCode(HttpStatus.BAD_REQUEST.value()) + .body("message", + stringContainsInOrder(List.of("JSON parse error:","Unexpected character ('t' (code 116)):")) + ); + } + private Document findSourceNameInMongoDB() throws UnknownHostException { MongoCredential credential = MongoCredential .createCredential(PNF_SIMULATOR_DB_USER, PNF_SIMULATOR_DB, PNF_SIMULATOR_DB_PSWD.toCharArray()); |