From f2a0cfdc146805275c6f6c869fd75e5c3c342cbb Mon Sep 17 00:00:00 2001 From: RehanRaza Date: Tue, 9 Apr 2019 08:18:07 +0000 Subject: Fix unit-tests of mass-pnf-simulator Change-Id: Ie11e97862c8cbbdb269b8b5e6d4361340b98f1b2 Issue-ID: DCAEGEN2-1225 Signed-off-by: RehanRaza --- .../mass-pnf-sim/pnf-sim-lightweight/simulator.sh | 2 +- .../java/org/onap/pnfsimulator/FileProvider.java | 15 +- .../org/onap/pnfsimulator/simulator/Simulator.java | 23 ++- .../pnfsimulator/simulator/SimulatorFactory.java | 6 +- .../simulator/validation/NoRopFilesException.java | 28 +++ .../message/JSONObjectFactoryTest.java | 30 ++- .../pnfsimulator/message/MessageProviderTest.java | 19 +- .../pnfsimulator/rest/SimulatorControllerTest.java | 35 +++- .../simulator/SimulatorFactoryTest.java | 66 +++++++ .../onap/pnfsimulator/simulator/SimulatorTest.java | 204 +++++++++++++++++++++ .../onap/pnfsimulator/simulator/TestMessages.java | 6 +- .../simulator/validCommonEventHeaderParams.json | 7 +- .../simulator/validNotificationParams.json | 18 +- .../simulator/validSimulatorParams.json | 1 - 14 files changed, 405 insertions(+), 55 deletions(-) create mode 100644 test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/main/java/org/onap/pnfsimulator/simulator/validation/NoRopFilesException.java (limited to 'test') diff --git a/test/mocks/mass-pnf-sim/pnf-sim-lightweight/simulator.sh b/test/mocks/mass-pnf-sim/pnf-sim-lightweight/simulator.sh index 6ba707973..6507e056a 100755 --- a/test/mocks/mass-pnf-sim/pnf-sim-lightweight/simulator.sh +++ b/test/mocks/mass-pnf-sim/pnf-sim-lightweight/simulator.sh @@ -94,7 +94,7 @@ function compose(){ function build_image(){ if [ -f pom.xml ]; then - mvn clean package docker:build -Dcheckstyle.skip -DskipTests + mvn clean package docker:build -Dcheckstyle.skip else echo "pom.xml file not found" exit 1 diff --git a/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/main/java/org/onap/pnfsimulator/FileProvider.java b/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/main/java/org/onap/pnfsimulator/FileProvider.java index 9eb733227..beb564da8 100644 --- a/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/main/java/org/onap/pnfsimulator/FileProvider.java +++ b/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/main/java/org/onap/pnfsimulator/FileProvider.java @@ -4,12 +4,11 @@ import java.io.File; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import org.onap.pnfsimulator.simulator.validation.NoRopFilesException; public class FileProvider { - private FileProvider() {} - - public static List getFiles() { + public List getFiles() throws NoRopFilesException { List files = queryFiles(); @@ -22,17 +21,15 @@ public class FileProvider { return fileListSorted; } - private static List queryFiles() { + private static List queryFiles() throws NoRopFilesException { File folder = new File("./files/onap/"); File[] listOfFiles = folder.listFiles(); - List results = new ArrayList<>(); - - if (listOfFiles.length == 0) { - return results; - // TODO: this should be a thrown exception catched in the Simulator class + if (listOfFiles == null || listOfFiles.length == 0) { + throw new NoRopFilesException("No ROP files found in specified directory"); } + List results = new ArrayList<>(); for (int i = 0; i < listOfFiles.length; i++) { if (listOfFiles[i].isFile()) { results.add(listOfFiles[i].getName()); diff --git a/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/main/java/org/onap/pnfsimulator/simulator/Simulator.java b/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/main/java/org/onap/pnfsimulator/simulator/Simulator.java index 5c6405742..ba114760f 100644 --- a/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/main/java/org/onap/pnfsimulator/simulator/Simulator.java +++ b/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/main/java/org/onap/pnfsimulator/simulator/Simulator.java @@ -30,6 +30,7 @@ import org.onap.pnfsimulator.message.MessageProvider; import org.onap.pnfsimulator.simulator.client.HttpClientAdapter; import org.onap.pnfsimulator.simulator.client.HttpClientAdapterImpl; import org.onap.pnfsimulator.simulator.validation.JSONValidator; +import org.onap.pnfsimulator.simulator.validation.NoRopFilesException; import org.onap.pnfsimulator.simulator.validation.ValidationException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -54,6 +55,8 @@ public class Simulator extends Thread { private Optional notificationParams; private String xnfUrl; private static final String DEFAULT_OUTPUT_SCHEMA_PATH = "json_schema/output_validator_ves_schema_30.0.1.json"; + private FileProvider fileProvider; + private Exception thrownException = null; private Simulator() {} @@ -68,10 +71,10 @@ public class Simulator extends Thread { endTime = Instant.now().plus(duration); while (isEndless || runningTimeNotExceeded()) { try { - List fileList = FileProvider.getFiles(); + + List fileList = fileProvider.getFiles(); MessageProvider messageProvider = new MessageProvider(); JSONValidator validator = new JSONValidator(); - messageBody = messageProvider.createMessage(this.commonEventHeaderParams, this.pnfRegistrationParams, this.notificationParams, fileList, this.xnfUrl); validator.validate(messageBody.toString(), DEFAULT_OUTPUT_SCHEMA_PATH); @@ -79,8 +82,9 @@ public class Simulator extends Thread { LOGGER.info("Message to be sent:\n" + getMessage()); httpClient.send(messageBody.toString(), vesUrl); Thread.sleep(interval.toMillis()); - } catch (InterruptedException | ValidationException | ProcessingException | IOException e) { - LOGGER.info("Simulation stopped due to an exception"); + } catch (InterruptedException | ValidationException | ProcessingException | IOException | NoRopFilesException e) { + LOGGER.info("Simulation stopped due to an exception: " + e); + thrownException = e; return; } } @@ -109,6 +113,10 @@ public class Simulator extends Thread { return isEndless; } + public Exception getThrownException() { + return thrownException; + } + public long getRemainingTime() { return Duration.between(Instant.now(), endTime).getSeconds(); } @@ -124,6 +132,7 @@ public class Simulator extends Thread { private Optional pnfRegistrationParams; private JSONObject commonEventHeaderParams; private String xnfUrl; + private FileProvider fileProvider; private Builder() { this.vesUrl = ""; @@ -180,6 +189,11 @@ public class Simulator extends Thread { return this; } + public Builder withFileProvider(FileProvider fileProvider) { + this.fileProvider = fileProvider; + return this; + } + public Simulator build() { Simulator simulator = new Simulator(); simulator.vesUrl = this.vesUrl; @@ -188,6 +202,7 @@ public class Simulator extends Thread { simulator.duration = this.duration; simulator.interval = this.interval; simulator.xnfUrl = this.xnfUrl; + simulator.fileProvider = this.fileProvider; simulator.commonEventHeaderParams = this.commonEventHeaderParams; simulator.pnfRegistrationParams = this.pnfRegistrationParams; simulator.notificationParams = this.notificationParams; diff --git a/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/main/java/org/onap/pnfsimulator/simulator/SimulatorFactory.java b/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/main/java/org/onap/pnfsimulator/simulator/SimulatorFactory.java index a01c2e0c6..21717d863 100644 --- a/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/main/java/org/onap/pnfsimulator/simulator/SimulatorFactory.java +++ b/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/main/java/org/onap/pnfsimulator/simulator/SimulatorFactory.java @@ -24,6 +24,7 @@ import java.time.Duration; import java.util.Optional; import org.json.JSONObject; import org.onap.pnfsimulator.ConfigurationProvider; +import org.onap.pnfsimulator.FileProvider; import org.onap.pnfsimulator.PnfSimConfig; import org.springframework.stereotype.Service; @@ -46,7 +47,8 @@ public class SimulatorFactory { Duration interval = Duration.ofSeconds(parseInt(simulatorParams.getString(MESSAGE_INTERVAL))); return Simulator.builder().withVesUrl(urlVes).withXnfUrl(xnfUrl).withDuration(duration) - .withCommonEventHeaderParams(commonEventHeaderParams).withNotificationParams(notificationParams) - .withPnfRegistrationParams(pnfRegistrationParams).withInterval(interval).build(); + .withFileProvider(new FileProvider()).withCommonEventHeaderParams(commonEventHeaderParams) + .withNotificationParams(notificationParams).withPnfRegistrationParams(pnfRegistrationParams) + .withInterval(interval).build(); } } diff --git a/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/main/java/org/onap/pnfsimulator/simulator/validation/NoRopFilesException.java b/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/main/java/org/onap/pnfsimulator/simulator/validation/NoRopFilesException.java new file mode 100644 index 000000000..d3765a8c1 --- /dev/null +++ b/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/main/java/org/onap/pnfsimulator/simulator/validation/NoRopFilesException.java @@ -0,0 +1,28 @@ +/* + * ============LICENSE_START======================================================= + * PNF-REGISTRATION-HANDLER + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. 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.simulator.validation; + +public class NoRopFilesException extends Exception { + + public NoRopFilesException(String message) { + super(message); + } +} diff --git a/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/message/JSONObjectFactoryTest.java b/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/message/JSONObjectFactoryTest.java index 4331195c9..da41afd0c 100644 --- a/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/message/JSONObjectFactoryTest.java +++ b/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/message/JSONObjectFactoryTest.java @@ -22,8 +22,27 @@ package org.onap.pnfsimulator.message; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import static org.onap.pnfsimulator.message.MessageConstants.*; - +import static org.onap.pnfsimulator.message.MessageConstants.EVENT_ID; +import static org.onap.pnfsimulator.message.MessageConstants.INTERNAL_HEADER_FIELDS; +import static org.onap.pnfsimulator.message.MessageConstants.LAST_EPOCH_MICROSEC; +import static org.onap.pnfsimulator.message.MessageConstants.NOTIFICATION_FIELDS_VERSION; +import static org.onap.pnfsimulator.message.MessageConstants.NOTIFICATION_FIELDS_VERSION_VALUE; +import static org.onap.pnfsimulator.message.MessageConstants.PNF_LAST_SERVICE_DATE; +import static org.onap.pnfsimulator.message.MessageConstants.PNF_MANUFACTURE_DATE; +import static org.onap.pnfsimulator.message.MessageConstants.PNF_REGISTRATION_FIELDS_VERSION; +import static org.onap.pnfsimulator.message.MessageConstants.PNF_REGISTRATION_FIELDS_VERSION_VALUE; +import static org.onap.pnfsimulator.message.MessageConstants.PRIORITY; +import static org.onap.pnfsimulator.message.MessageConstants.PRIORITY_NORMAL; +import static org.onap.pnfsimulator.message.MessageConstants.REPORTING_ENTITY_NAME; +import static org.onap.pnfsimulator.message.MessageConstants.SEQUENCE; +import static org.onap.pnfsimulator.message.MessageConstants.SEQUENCE_NUMBER; +import static org.onap.pnfsimulator.message.MessageConstants.SOURCE_NAME; +import static org.onap.pnfsimulator.message.MessageConstants.START_EPOCH_MICROSEC; +import static org.onap.pnfsimulator.message.MessageConstants.TIME_ZONE_OFFSET; +import static org.onap.pnfsimulator.message.MessageConstants.VERSION; +import static org.onap.pnfsimulator.message.MessageConstants.VERSION_NUMBER; +import static org.onap.pnfsimulator.message.MessageConstants.VES_EVENT_LISTENER_VERSION; +import static org.onap.pnfsimulator.message.MessageConstants.VES_EVENT_LISTENER_VERSION_NUMBER; import org.json.JSONObject; import org.junit.jupiter.api.Test; @@ -32,14 +51,17 @@ public class JSONObjectFactoryTest { @Test public void generateConstantCommonEventHeader_shouldCreateProperly(){ JSONObject commonEventHeader = JSONObjectFactory.generateConstantCommonEventHeader(); - assertEquals(8,commonEventHeader.toMap().size()); + assertEquals(11,commonEventHeader.toMap().size()); assertTrue(commonEventHeader.has(EVENT_ID)); + assertTrue(commonEventHeader.has(TIME_ZONE_OFFSET)); assertTrue(commonEventHeader.has(LAST_EPOCH_MICROSEC)); assertTrue(commonEventHeader.has(PRIORITY)); assertTrue(commonEventHeader.has(SEQUENCE)); assertTrue(commonEventHeader.has(START_EPOCH_MICROSEC)); assertTrue(commonEventHeader.has(INTERNAL_HEADER_FIELDS)); assertTrue(commonEventHeader.has(VERSION)); + assertTrue(commonEventHeader.has(SOURCE_NAME)); + assertTrue(commonEventHeader.has(REPORTING_ENTITY_NAME)); assertEquals(commonEventHeader.get(PRIORITY),PRIORITY_NORMAL); assertEquals(commonEventHeader.get(SEQUENCE),SEQUENCE_NUMBER); assertEquals(commonEventHeader.get(VERSION),VERSION_NUMBER); @@ -59,7 +81,7 @@ public class JSONObjectFactoryTest { @Test public void generateEventId_shouldCreateProperly(){ String eventId = JSONObjectFactory.generateEventId(); - assertTrue(eventId.startsWith("registration_")); + assertTrue(eventId.startsWith("FileReady_")); } @Test diff --git a/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/message/MessageProviderTest.java b/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/message/MessageProviderTest.java index aadb54cdc..0fa8a12ee 100644 --- a/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/message/MessageProviderTest.java +++ b/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/message/MessageProviderTest.java @@ -27,7 +27,6 @@ import static org.onap.pnfsimulator.message.MessageConstants.COMMON_EVENT_HEADER import static org.onap.pnfsimulator.message.MessageConstants.EVENT; import static org.onap.pnfsimulator.message.MessageConstants.NOTIFICATION_FIELDS; import static org.onap.pnfsimulator.message.MessageConstants.PNF_REGISTRATION_FIELDS; - import java.util.Optional; import org.json.JSONObject; import org.junit.jupiter.api.BeforeAll; @@ -93,23 +92,31 @@ public class MessageProviderTest { } @Test - public void createMessage_should_add_specified_params_to_valid_subobjects() { + public void createMessage_should_add_specified_params_to_valid_subobjects_with_event_pnf_registration() { JSONObject message = messageProvider - .createMessage(new JSONObject(), Optional.of(new JSONObject(testParamsPnfRegistration)), - Optional.of(new JSONObject(testParamsNotification))); + .createMessage(new JSONObject(), Optional.of(new JSONObject(testParamsPnfRegistration)), Optional.empty()); JSONObject event = message.getJSONObject(EVENT); JSONObject commonEventHeader = event.getJSONObject(COMMON_EVENT_HEADER); - assertEquals(10, commonEventHeader.keySet().size()); + assertEquals(13, commonEventHeader.keySet().size()); JSONObject pnfRegistrationFields = event.getJSONObject(PNF_REGISTRATION_FIELDS); assertEquals("pnfVal1", pnfRegistrationFields.getString("pnfKey1")); assertEquals("pnfVal2", pnfRegistrationFields.getString("pnfKey2")); + } + + @Test + public void createMessage_should_add_specified_params_to_valid_subobjects_with_event_notification() { + JSONObject message = messageProvider + .createMessage(new JSONObject(), Optional.empty(), Optional.of(new JSONObject(testParamsNotification))); + JSONObject event = message.getJSONObject(EVENT); + + JSONObject commonEventHeader = event.getJSONObject(COMMON_EVENT_HEADER); + assertEquals(12, commonEventHeader.keySet().size()); JSONObject notificationFields = event.getJSONObject(NOTIFICATION_FIELDS); assertEquals("notVal1", notificationFields.getString("notKey1")); assertEquals("notVal2", notificationFields.getString("notKey2")); - } } diff --git a/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/rest/SimulatorControllerTest.java b/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/rest/SimulatorControllerTest.java index 3603480bf..d1db8d55c 100644 --- a/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/rest/SimulatorControllerTest.java +++ b/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/rest/SimulatorControllerTest.java @@ -20,28 +20,35 @@ package org.onap.pnfsimulator.rest; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.any; +import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import static org.onap.pnfsimulator.simulator.TestMessages.VALID_COMMON_EVENT_HEADER_PARAMS; +import static org.onap.pnfsimulator.simulator.TestMessages.VALID_NOTIFICATION_PARAMS; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - import java.time.Duration; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import org.onap.pnfsimulator.FileProvider; import org.onap.pnfsimulator.simulator.Simulator; import org.onap.pnfsimulator.simulator.SimulatorFactory; import org.onap.pnfsimulator.simulator.client.HttpClientAdapter; import org.onap.pnfsimulator.simulator.validation.JSONValidator; +import org.onap.pnfsimulator.simulator.validation.NoRopFilesException; import org.onap.pnfsimulator.simulator.validation.ValidationException; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; @@ -53,9 +60,10 @@ class SimulatorControllerTest { private static final String STATUS_URL = "/simulator/status"; private static final String JSON_MSG_EXPRESSION = "$.message"; private static final String JSON_STATUS_EXPRESSION = "$.simulatorStatus"; + private static final String TEST_VES_URL = "http://localhost:10000/eventListener/v7"; + private static final String TEST_XNF_URL = "sftp://onap:pano@10.11.0.68" + "/"; private static final String PROPER_JSON = "{\n" + " \"simulatorParams\": {\n" + - " \"vesServerUrl\": \"http://10.154.187.70:8080/eventListener/v7\",\n" + " \"testDuration\": \"10\",\n" + " \"messageInterval\": \"1\"\n" + " },\n" + @@ -103,9 +111,24 @@ class SimulatorControllerTest { private Simulator simulator; + private FileProvider fileProvider = mock(FileProvider.class); + + private void createSampleFileList() { + List fileList = new ArrayList<>(); + fileList.add("A20190401.1608+0000-1622+0000_excl-eeiwbue-perf-large-pnf-sim-lw-1.xml.gz"); + fileList.add("A20190401.1623+0000-1637+0000_excl-eeiwbue-perf-large-pnf-sim-lw-1.xml.gz"); + + try { + doReturn(fileList).when(fileProvider).getFiles(); + } catch (NoRopFilesException e) { + e.printStackTrace(); + } + } + @BeforeEach void setup() { MockitoAnnotations.initMocks(this); + createSampleFileList(); simulator = createEndlessSimulator(); mockMvc = MockMvcBuilders .standaloneSetup(controller) @@ -115,6 +138,12 @@ class SimulatorControllerTest { private Simulator createEndlessSimulator() { return spy(Simulator.builder() .withCustomHttpClientAdapter(mock(HttpClientAdapter.class)) + .withCommonEventHeaderParams(VALID_COMMON_EVENT_HEADER_PARAMS) + .withPnfRegistrationParams(Optional.empty()) + .withNotificationParams(VALID_NOTIFICATION_PARAMS) + .withVesUrl(TEST_VES_URL) + .withXnfUrl(TEST_XNF_URL) + .withFileProvider(fileProvider) .withInterval(Duration.ofMinutes(1)) .build()); } diff --git a/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/simulator/SimulatorFactoryTest.java b/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/simulator/SimulatorFactoryTest.java index e69de29bb..d8e60c18d 100644 --- a/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/simulator/SimulatorFactoryTest.java +++ b/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/simulator/SimulatorFactoryTest.java @@ -0,0 +1,66 @@ +/* + * ============LICENSE_START======================================================= + * PNF-REGISTRATION-HANDLER + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. 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.simulator; + +import static org.junit.Assert.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.onap.pnfsimulator.simulator.TestMessages.INVALID_SIMULATOR_PARAMS; +import static org.onap.pnfsimulator.simulator.TestMessages.VALID_COMMON_EVENT_HEADER_PARAMS; +import static org.onap.pnfsimulator.simulator.TestMessages.VALID_NOTIFICATION_PARAMS; +import static org.onap.pnfsimulator.simulator.TestMessages.VALID_PNF_REGISTRATION_PARAMS; +import static org.onap.pnfsimulator.simulator.TestMessages.VALID_SIMULATOR_PARAMS; +import java.util.Optional; +import org.json.JSONException; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class SimulatorFactoryTest { + + + private SimulatorFactory simulatorFactory; + + @BeforeEach + void setUp() { + simulatorFactory = new SimulatorFactory(); + } + + @Test + void should_successfully_create_simulator_given_valid_pnf_registration_params() { + assertNotNull(simulatorFactory.create(VALID_SIMULATOR_PARAMS, VALID_COMMON_EVENT_HEADER_PARAMS, + VALID_PNF_REGISTRATION_PARAMS, Optional.empty())); + } + + @Test + void should_successfully_create_simulator_given_valid_notification_params_and_valid_output_message() { + assertNotNull(simulatorFactory.create(VALID_SIMULATOR_PARAMS, VALID_COMMON_EVENT_HEADER_PARAMS, + Optional.empty(), VALID_NOTIFICATION_PARAMS)); + } + + @Test + void should_throw_given_invalid_simulator_params() { + assertThrows( + JSONException.class, + () -> simulatorFactory.create(INVALID_SIMULATOR_PARAMS, VALID_COMMON_EVENT_HEADER_PARAMS, + VALID_PNF_REGISTRATION_PARAMS, VALID_NOTIFICATION_PARAMS)); + } +} + + diff --git a/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/simulator/SimulatorTest.java b/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/simulator/SimulatorTest.java index e69de29bb..7ed9f04c2 100644 --- a/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/simulator/SimulatorTest.java +++ b/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/simulator/SimulatorTest.java @@ -0,0 +1,204 @@ +/* + * ============LICENSE_START======================================================= + * PNF-REGISTRATION-HANDLER + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. 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.simulator; + +import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTimeout; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.onap.pnfsimulator.simulator.TestMessages.INVALID_NOTIFICATION_PARAMS; +import static org.onap.pnfsimulator.simulator.TestMessages.INVALID_PNF_REGISTRATION_PARAMS_1; +import static org.onap.pnfsimulator.simulator.TestMessages.INVALID_PNF_REGISTRATION_PARAMS_2; +import static org.onap.pnfsimulator.simulator.TestMessages.INVALID_PNF_REGISTRATION_PARAMS_3; +import static org.onap.pnfsimulator.simulator.TestMessages.VALID_COMMON_EVENT_HEADER_PARAMS; +import static org.onap.pnfsimulator.simulator.TestMessages.VALID_NOTIFICATION_PARAMS; +import static org.onap.pnfsimulator.simulator.TestMessages.VALID_PNF_REGISTRATION_PARAMS; +import java.time.Duration; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; +import org.mockito.Mockito; +import org.onap.pnfsimulator.FileProvider; +import org.onap.pnfsimulator.simulator.client.HttpClientAdapter; +import org.onap.pnfsimulator.simulator.validation.NoRopFilesException; +import org.onap.pnfsimulator.simulator.validation.ValidationException; + +public class SimulatorTest { + + private static final String TEST_VES_URL = "http://localhost:10000/eventListener/v7"; + private static final String TEST_XNF_URL = "sftp://onap:pano@10.11.0.68" + "/"; + private FileProvider fileProvider = mock(FileProvider.class); + + private void createSampleFileList() { + List fileList = new ArrayList<>(); + fileList.add("A20190401.1608+0000-1622+0000_excl-eeiwbue-perf-large-pnf-sim-lw-1.xml.gz"); + fileList.add("A20190401.1623+0000-1637+0000_excl-eeiwbue-perf-large-pnf-sim-lw-1.xml.gz"); + + try { + doReturn(fileList).when(fileProvider).getFiles(); + } catch (NoRopFilesException e) { + e.printStackTrace(); + } + } + + @Test + void builder_should_create_endless_simulator_when_duration_not_specified() { + Simulator simulator = Simulator + .builder() + .withDuration(Duration.ofSeconds(1)) + .withVesUrl(TEST_VES_URL).build(); + + assertFalse(simulator.isEndless()); + + simulator = Simulator + .builder() + .withVesUrl(TEST_VES_URL).build(); + + assertTrue(simulator.isEndless()); + } + + @Test + void simulator_should_stop_when_interrupted() { + createSampleFileList(); + + HttpClientAdapter httpClientMock = Mockito.mock(HttpClientAdapter.class); + Simulator simulator = Simulator.builder() + .withInterval(Duration.ofSeconds(1)) + .withCustomHttpClientAdapter(httpClientMock) + .withCommonEventHeaderParams(VALID_COMMON_EVENT_HEADER_PARAMS) + .withPnfRegistrationParams(Optional.empty()) + .withNotificationParams(VALID_NOTIFICATION_PARAMS) + .withVesUrl(TEST_VES_URL) + .withXnfUrl(TEST_XNF_URL) + .withCustomHttpClientAdapter(httpClientMock) + .withFileProvider(fileProvider).build(); + + simulator.start(); + simulator.interrupt(); + + assertTimeout(Duration.ofSeconds(1), (Executable) simulator::join); + } + + @Test + void should_throw_noropfiles_exception_given_empty_filelist() { + Simulator simulator = Simulator.builder() + .withDuration(Duration.ofMillis(100)) + .withInterval(Duration.ofMillis(10)) + .withCommonEventHeaderParams(VALID_COMMON_EVENT_HEADER_PARAMS) + .withPnfRegistrationParams(VALID_PNF_REGISTRATION_PARAMS) + .withNotificationParams(Optional.empty()) + .withVesUrl(TEST_VES_URL) + .withXnfUrl(TEST_XNF_URL) + .withFileProvider(new FileProvider()).build(); + simulator.run(); + Exception e = simulator.getThrownException(); + assertTrue(e instanceof NoRopFilesException); + } + + @Test + void should_throw_validation_exception_given_invalid_params() { + createSampleFileList(); + + Simulator simulator = Simulator.builder() + .withDuration(Duration.ofMillis(100)) + .withInterval(Duration.ofMillis(10)) + .withCommonEventHeaderParams(VALID_COMMON_EVENT_HEADER_PARAMS) + .withPnfRegistrationParams(INVALID_PNF_REGISTRATION_PARAMS_1) + .withNotificationParams(Optional.empty()) + .withVesUrl(TEST_VES_URL) + .withXnfUrl(TEST_XNF_URL) + .withFileProvider(fileProvider).build(); + simulator.run(); + Exception e = simulator.getThrownException(); + assertTrue(e instanceof ValidationException); + + simulator = Simulator.builder() + .withDuration(Duration.ofMillis(100)) + .withInterval(Duration.ofMillis(10)) + .withCommonEventHeaderParams(VALID_COMMON_EVENT_HEADER_PARAMS) + .withPnfRegistrationParams(INVALID_PNF_REGISTRATION_PARAMS_2) + .withNotificationParams(Optional.empty()) + .withVesUrl(TEST_VES_URL) + .withXnfUrl(TEST_XNF_URL) + .withFileProvider(fileProvider).build(); + simulator.run(); + e = simulator.getThrownException(); + assertTrue(e instanceof ValidationException); + + simulator = Simulator.builder() + .withDuration(Duration.ofMillis(100)) + .withInterval(Duration.ofMillis(10)) + .withCommonEventHeaderParams(VALID_COMMON_EVENT_HEADER_PARAMS) + .withPnfRegistrationParams(INVALID_PNF_REGISTRATION_PARAMS_3) + .withNotificationParams(Optional.empty()) + .withVesUrl(TEST_VES_URL) + .withXnfUrl(TEST_XNF_URL) + .withFileProvider(fileProvider).build(); + simulator.run(); + e = simulator.getThrownException(); + assertTrue(e instanceof ValidationException); + + simulator = Simulator.builder() + .withDuration(Duration.ofMillis(100)) + .withInterval(Duration.ofMillis(10)) + .withCommonEventHeaderParams(VALID_COMMON_EVENT_HEADER_PARAMS) + .withPnfRegistrationParams(VALID_PNF_REGISTRATION_PARAMS) + .withNotificationParams(INVALID_NOTIFICATION_PARAMS) + .withVesUrl(TEST_VES_URL) + .withXnfUrl(TEST_XNF_URL) + .withFileProvider(fileProvider).build(); + simulator.run(); + e = simulator.getThrownException(); + assertTrue(e instanceof ValidationException); + } + + @Test + void simulator_should_send_fileready_message() { + createSampleFileList(); + + HttpClientAdapter httpClientMock = Mockito.mock(HttpClientAdapter.class); + Simulator simulator = Simulator.builder() + .withDuration(Duration.ofMillis(100)) + .withInterval(Duration.ofMillis(10)) + .withCommonEventHeaderParams(VALID_COMMON_EVENT_HEADER_PARAMS) + .withPnfRegistrationParams(Optional.empty()) + .withNotificationParams(VALID_NOTIFICATION_PARAMS) + .withVesUrl(TEST_VES_URL) + .withXnfUrl(TEST_XNF_URL) + .withCustomHttpClientAdapter(httpClientMock) + .withFileProvider(fileProvider).build(); + simulator.run(); + Exception e = simulator.getThrownException(); + assertNull(e); + + assertTimeout(Duration.ofMillis(150), (Executable) simulator::join); + verify(httpClientMock, times(1)).send(anyString(), eq(TEST_VES_URL)); + } +} + diff --git a/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/simulator/TestMessages.java b/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/simulator/TestMessages.java index 7511084c4..d92b3c2c5 100644 --- a/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/simulator/TestMessages.java +++ b/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/simulator/TestMessages.java @@ -26,13 +26,13 @@ import java.nio.file.Paths; import java.util.Optional; import org.json.JSONObject; -final class TestMessages { +public final class TestMessages { static final JSONObject VALID_SIMULATOR_PARAMS = new JSONObject(getContent("validSimulatorParams.json")); - static final JSONObject VALID_COMMON_EVENT_HEADER_PARAMS = new JSONObject(getContent("validCommonEventHeaderParams.json")); + public static final JSONObject VALID_COMMON_EVENT_HEADER_PARAMS = new JSONObject(getContent("validCommonEventHeaderParams.json")); static final Optional VALID_PNF_REGISTRATION_PARAMS = Optional .of(new JSONObject(getContent("validPnfRegistrationParams.json"))); - static final Optional VALID_NOTIFICATION_PARAMS = Optional + public static final Optional VALID_NOTIFICATION_PARAMS = Optional .of(new JSONObject(getContent("validNotificationParams.json"))); static final JSONObject INVALID_SIMULATOR_PARAMS = new JSONObject( diff --git a/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/resources/org/onap/pnfsimulator/simulator/validCommonEventHeaderParams.json b/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/resources/org/onap/pnfsimulator/simulator/validCommonEventHeaderParams.json index e0f455045..b988da0f7 100644 --- a/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/resources/org/onap/pnfsimulator/simulator/validCommonEventHeaderParams.json +++ b/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/resources/org/onap/pnfsimulator/simulator/validCommonEventHeaderParams.json @@ -1,8 +1,5 @@ { - "eventName": "pnfRegistration_Nokia_5gDu", + "eventName": "Noti_RnNode-Ericsson_FileReady", "nfNamingCode": "gNB", - "nfcNamingCode": "oam", - "sourceName": "NOK6061ZW3", - "sourceId": "val13", - "reportingEntityName": "NOK6061ZW3" + "nfcNamingCode": "oam" } \ No newline at end of file diff --git a/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/resources/org/onap/pnfsimulator/simulator/validNotificationParams.json b/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/resources/org/onap/pnfsimulator/simulator/validNotificationParams.json index f7f463d3d..af0cdf409 100644 --- a/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/resources/org/onap/pnfsimulator/simulator/validNotificationParams.json +++ b/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/resources/org/onap/pnfsimulator/simulator/validNotificationParams.json @@ -1,20 +1,4 @@ { "changeIdentifier": "PM_MEAS_FILES", - "changeType": "FileReady", - "arrayOfNamedHashMap": [ - {"name": "A20161221.1031-1041.bin.gz", "hashMap": { - "location": "ftpes://192.169.0.1:22/ftp/rop/A20161224.1030-1045.bin.gz", - "compression": "gzip", - "fileformatType": "org.3GPP.32.435#measCollec", - "fileFormatVersion": "V10" - } - }, - {"name": "A20161222.1042-1102.bin.gz", "hashMap": { - "location": "ftpes://192.168.0.102:22/ftp/rop/A20161224.1045-1100.bin.gz", - "compression": "gzip", - "fileFormatType": "org.3GPP.32.435#measCollec", - "fileFormatVersion": "V10" - } - } - ] + "changeType": "FileReady" } diff --git a/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/resources/org/onap/pnfsimulator/simulator/validSimulatorParams.json b/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/resources/org/onap/pnfsimulator/simulator/validSimulatorParams.json index 6485ee4a4..018f185c4 100644 --- a/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/resources/org/onap/pnfsimulator/simulator/validSimulatorParams.json +++ b/test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/resources/org/onap/pnfsimulator/simulator/validSimulatorParams.json @@ -1,5 +1,4 @@ { - "vesServerUrl": "http://VES-HOST:VES-PORT/eventListener/v7", "testDuration": "10", "messageInterval": "1" } \ No newline at end of file -- cgit 1.2.3-korg