diff options
author | Bartosz Gardziejewski <bartosz.gardziejewski@nokia.com> | 2021-01-29 10:43:17 +0100 |
---|---|---|
committer | Bartosz Gardziejewski <bartosz.gardziejewski@nokia.com> | 2021-02-02 09:32:13 +0100 |
commit | a8cafe1c784ae68b2a7cca313b362d14b13fd631 (patch) | |
tree | 9ed2ff5b311cd65fb079bccc8ebef7744c0afc05 /pnfsimulator/integration/src/test/java | |
parent | 6650865ef8836239d17fcbfcfb08871298787e80 (diff) |
add basic auth using username and password
Signed-off-by: Bartosz Gardziejewski <bartosz.gardziejewski@nokia.com>
Change-Id: I67bc2c25149c8e2d4943f23cfa8d726cdb95995f
Issue-ID: INT-1805
Diffstat (limited to 'pnfsimulator/integration/src/test/java')
6 files changed, 217 insertions, 120 deletions
diff --git a/pnfsimulator/integration/src/test/java/org/onap/pnfsimulator/integration/BasicAvailabilityTest.java b/pnfsimulator/integration/src/test/java/org/onap/pnfsimulator/integration/BasicAvailabilityTest.java index fffc447..323243c 100644 --- a/pnfsimulator/integration/src/test/java/org/onap/pnfsimulator/integration/BasicAvailabilityTest.java +++ b/pnfsimulator/integration/src/test/java/org/onap/pnfsimulator/integration/BasicAvailabilityTest.java @@ -24,6 +24,7 @@ import static io.restassured.RestAssured.given; import static io.restassured.RestAssured.when; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.hamcrest.Matchers.equalTo; +import static org.onap.pnfsimulator.integration.TestUtils.getCurrentIpAddress; import com.google.gson.JsonObject; @@ -230,13 +231,4 @@ public class BasicAvailabilityTest { return "http://0.0.0.0:5000/simulator/" + action; } - private String getCurrentIpAddress() throws SocketException { - return Collections.list(NetworkInterface.getNetworkInterfaces()).stream() - .flatMap(i -> Collections.list(i.getInetAddresses()).stream()) - .filter(ip -> ip instanceof Inet4Address) - .map(e -> (Inet4Address) e) - .findFirst() - .orElseThrow(RuntimeException::new) - .getHostAddress(); - } } 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 8c8767d..50ad1cd 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,24 +23,15 @@ 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 static org.onap.pnfsimulator.integration.TestUtils.COMMON_EVENT_HEADER; +import static org.onap.pnfsimulator.integration.TestUtils.PATCHED; +import static org.onap.pnfsimulator.integration.TestUtils.SINGLE_EVENT_URL; +import static org.onap.pnfsimulator.integration.TestUtils.findSourceNameInMongoDB; +import static org.onap.pnfsimulator.integration.TestUtils.getCurrentIpAddress; import com.google.gson.JsonObject; -import com.mongodb.MongoClient; -import com.mongodb.MongoClientOptions; -import com.mongodb.MongoCredential; -import com.mongodb.ServerAddress; -import com.mongodb.client.FindIterable; -import com.mongodb.client.MongoCollection; -import com.mongodb.client.MongoCursor; -import com.mongodb.client.MongoDatabase; import java.time.Instant; -import java.net.Inet4Address; -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; @@ -60,16 +51,6 @@ import org.springframework.test.context.junit4.SpringRunner; @SpringBootTest(classes = {Main.class, TestConfiguration.class}, webEnvironment = WebEnvironment.DEFINED_PORT) public class OptionalTemplatesTest { - private static final String PNF_SIMULATOR_DB = "pnf_simulator"; - private static final String COMMON_EVENT_HEADER = "commonEventHeader"; - private static final String PNF_SIMULATOR_DB_PSWD = "zXcVbN123!"; - private static final String PNF_SIMULATOR_DB_USER = "pnf_simulator_user"; - private static final String PATCHED = "patched"; - private static final String SINGLE_EVENT_URL = "http://0.0.0.0:5000/simulator/event"; - - @Autowired - VesSimulatorController vesSimulatorController; - @Autowired private VesSimulatorService vesSimulatorService; @@ -177,83 +158,4 @@ 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()); - MongoClient mongoClient = new MongoClient(new ServerAddress(Inet4Address.getLocalHost(), 27017), - credential, MongoClientOptions.builder().build()); - MongoDatabase pnfSimulatorDb = mongoClient.getDatabase(PNF_SIMULATOR_DB); - MongoCollection<Document> table = pnfSimulatorDb.getCollection("eventData"); - Document searchQuery = new Document(); - searchQuery.put(PATCHED, new Document("$regex", ".*" + "HistoricalEvent" + ".*")); - FindIterable<Document> findOfPatched = table.find(searchQuery); - Document dbObject = null; - MongoCursor<Document> cursor = findOfPatched.iterator(); - if (cursor.hasNext()) { - dbObject = cursor.next(); - } - return dbObject; - } - - private String getCurrentIpAddress() throws SocketException { - return Collections.list(NetworkInterface.getNetworkInterfaces()).stream() - .flatMap(i -> Collections.list(i.getInetAddresses()).stream()) - .filter(ip -> ip instanceof Inet4Address) - .map(e -> (Inet4Address) e) - .findFirst() - .orElseThrow(RuntimeException::new) - .getHostAddress(); - } - } diff --git a/pnfsimulator/integration/src/test/java/org/onap/pnfsimulator/integration/SingleEventTest.java b/pnfsimulator/integration/src/test/java/org/onap/pnfsimulator/integration/SingleEventTest.java new file mode 100644 index 0000000..3ad1385 --- /dev/null +++ b/pnfsimulator/integration/src/test/java/org/onap/pnfsimulator/integration/SingleEventTest.java @@ -0,0 +1,151 @@ +/*- + * ============LICENSE_START======================================================= + * Simulator + * ================================================================================ + * Copyright (C) 2020 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.pnfsimulator.integration; + +import com.google.gson.JsonObject; +import org.assertj.core.api.Assertions; +import org.bson.Document; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Mockito; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.HttpStatus; +import org.springframework.test.context.junit4.SpringRunner; + +import java.net.UnknownHostException; +import java.util.List; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.stringContainsInOrder; +import static org.onap.pnfsimulator.integration.TestUtils.PATCHED; +import static org.onap.pnfsimulator.integration.TestUtils.SINGLE_EVENT_URL; +import static org.onap.pnfsimulator.integration.TestUtils.findSourceNameInMongoDB; +import static org.onap.pnfsimulator.integration.TestUtils.getCurrentIpAddress; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = {Main.class, TestConfiguration.class}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) +public class SingleEventTest { + + @Autowired + private VesSimulatorService vesSimulatorService; + + private String currentVesSimulatorIp; + + @Before + public void setUp() throws Exception { + currentVesSimulatorIp = getCurrentIpAddress(); + } + + @After + public void tearDown() { + Mockito.reset(vesSimulatorService); + } + + @Test + public void whenTriggeredSimulatorWithWrongVesAddressInformationShouldBeReturned() { + //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)):")) + ); + } + + @Test + public void whenTriggeredSimulatorWithUsernameAndPasswordInUrlVesShouldAcceptRequest() throws UnknownHostException { + //given + String body = "{\n" + + "\"vesServerUrl\": \"https://user1:pass1@" + currentVesSimulatorIp + ":9443/ves-simulator/eventListener/v5\",\n" + + "\"event\": { \n" + + "\"commonEventHeader\": {\n" + + "\"sourceName\": \"HistoricalEvent\",\n" + + "\"version\": 3" + + "}\n" + + "}\n" + + "}"; + ArgumentCaptor<JsonObject> parameterCaptor = ArgumentCaptor.forClass(JsonObject.class); + + //when + given() + .contentType("application/json") + .body(body) + .when() + .post(SINGLE_EVENT_URL) + .then() + .statusCode(HttpStatus.ACCEPTED.value()) + .body("message", equalTo("Accepted")); + + //then + Mockito.verify(vesSimulatorService, + Mockito.timeout(3000)) + .sendEventToDmaapV5(parameterCaptor.capture()); + + Document sourceNameInMongoDB = findSourceNameInMongoDB(); + Assertions.assertThat(sourceNameInMongoDB.get(PATCHED)) + .isEqualTo("{\"commonEventHeader\":{\"sourceName\":\"HistoricalEvent\",\"version\":3}}"); + } +} diff --git a/pnfsimulator/integration/src/test/java/org/onap/pnfsimulator/integration/TestUtils.java b/pnfsimulator/integration/src/test/java/org/onap/pnfsimulator/integration/TestUtils.java new file mode 100644 index 0000000..f7df5e9 --- /dev/null +++ b/pnfsimulator/integration/src/test/java/org/onap/pnfsimulator/integration/TestUtils.java @@ -0,0 +1,57 @@ +package org.onap.pnfsimulator.integration; + +import com.mongodb.MongoClient; +import com.mongodb.MongoClientOptions; +import com.mongodb.MongoCredential; +import com.mongodb.ServerAddress; +import com.mongodb.client.FindIterable; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoCursor; +import com.mongodb.client.MongoDatabase; +import org.bson.Document; + +import java.net.Inet4Address; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.net.UnknownHostException; +import java.util.Collections; + +public class TestUtils { + + private TestUtils() {} + + public static final String PNF_SIMULATOR_DB = "pnf_simulator"; + public static final String COMMON_EVENT_HEADER = "commonEventHeader"; + public static final String PNF_SIMULATOR_DB_PSWD = "zXcVbN123!"; + public static final String PNF_SIMULATOR_DB_USER = "pnf_simulator_user"; + public static final String PATCHED = "patched"; + public static final String SINGLE_EVENT_URL = "http://0.0.0.0:5000/simulator/event"; + + public static Document findSourceNameInMongoDB() throws UnknownHostException { + MongoCredential credential = MongoCredential + .createCredential(PNF_SIMULATOR_DB_USER, PNF_SIMULATOR_DB, PNF_SIMULATOR_DB_PSWD.toCharArray()); + MongoClient mongoClient = new MongoClient(new ServerAddress(Inet4Address.getLocalHost(), 27017), + credential, MongoClientOptions.builder().build()); + MongoDatabase pnfSimulatorDb = mongoClient.getDatabase(PNF_SIMULATOR_DB); + MongoCollection<Document> table = pnfSimulatorDb.getCollection("eventData"); + Document searchQuery = new Document(); + searchQuery.put(PATCHED, new Document("$regex", ".*" + "HistoricalEvent" + ".*")); + FindIterable<Document> findOfPatched = table.find(searchQuery); + Document dbObject = null; + MongoCursor<Document> cursor = findOfPatched.iterator(); + if (cursor.hasNext()) { + dbObject = cursor.next(); + } + return dbObject; + } + + public static String getCurrentIpAddress() throws SocketException { + return Collections.list(NetworkInterface.getNetworkInterfaces()).stream() + .flatMap(i -> Collections.list(i.getInetAddresses()).stream()) + .filter(ip -> ip instanceof Inet4Address) + .map(e -> (Inet4Address) e) + .findFirst() + .orElseThrow(RuntimeException::new) + .getHostAddress(); + } +} diff --git a/pnfsimulator/integration/src/test/java/org/onap/pnfsimulator/integration/VariablesReplacement.java b/pnfsimulator/integration/src/test/java/org/onap/pnfsimulator/integration/VariablesReplacement.java index c0bd989..ae7970c 100644 --- a/pnfsimulator/integration/src/test/java/org/onap/pnfsimulator/integration/VariablesReplacement.java +++ b/pnfsimulator/integration/src/test/java/org/onap/pnfsimulator/integration/VariablesReplacement.java @@ -3,6 +3,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.onap.pnfsimulator.integration.TestUtils.getCurrentIpAddress; import com.google.gson.JsonObject; import java.net.Inet4Address; @@ -76,11 +77,4 @@ public class VariablesReplacement { return "http://0.0.0.0:5000/simulator/start"; } - private String getCurrentIpAddress() throws SocketException { - return Collections.list(NetworkInterface.getNetworkInterfaces()).stream() - .flatMap(i -> Collections.list(i.getInetAddresses()).stream()) - .filter(ip -> ip instanceof Inet4Address).map(e -> (Inet4Address) e).findFirst() - .orElseThrow(RuntimeException::new).getHostAddress(); - } - } diff --git a/pnfsimulator/integration/src/test/java/org/onap/pnfsimulator/integration/suites/DockerBasedTestsSuite.java b/pnfsimulator/integration/src/test/java/org/onap/pnfsimulator/integration/suites/DockerBasedTestsSuite.java index 3de06c3..f6a4c24 100644 --- a/pnfsimulator/integration/src/test/java/org/onap/pnfsimulator/integration/suites/DockerBasedTestsSuite.java +++ b/pnfsimulator/integration/src/test/java/org/onap/pnfsimulator/integration/suites/DockerBasedTestsSuite.java @@ -30,6 +30,7 @@ import org.junit.runners.Suite.SuiteClasses; import org.onap.pnfsimulator.integration.BasicAvailabilityTest; import org.onap.pnfsimulator.integration.OptionalTemplatesTest; import org.onap.pnfsimulator.integration.SearchInTemplatesTest; +import org.onap.pnfsimulator.integration.SingleEventTest; import org.onap.pnfsimulator.integration.TemplatesManagementTest; import org.onap.pnfsimulator.integration.VariablesReplacement; import org.slf4j.Logger; @@ -40,7 +41,7 @@ import static io.restassured.RestAssured.given; @RunWith(Suite.class) @SuiteClasses({BasicAvailabilityTest.class, TemplatesManagementTest.class, OptionalTemplatesTest.class, - SearchInTemplatesTest.class, VariablesReplacement.class}) + SearchInTemplatesTest.class, VariablesReplacement.class, SingleEventTest.class}) public class DockerBasedTestsSuite { private static final Logger LOGGER = LoggerFactory.getLogger(DockerBasedTestsSuite.class); |