Fix unit-tests of mass-pnf-simulator 28/84628/2
authorRehanRaza <muhammad.rehan.raza@est.tech>
Tue, 9 Apr 2019 08:18:07 +0000 (08:18 +0000)
committerRehanRaza <muhammad.rehan.raza@est.tech>
Tue, 9 Apr 2019 08:18:07 +0000 (08:18 +0000)
Change-Id: Ie11e97862c8cbbdb269b8b5e6d4361340b98f1b2
Issue-ID: DCAEGEN2-1225
Signed-off-by: RehanRaza <muhammad.rehan.raza@est.tech>
14 files changed:
test/mocks/mass-pnf-sim/pnf-sim-lightweight/simulator.sh
test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/main/java/org/onap/pnfsimulator/FileProvider.java
test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/main/java/org/onap/pnfsimulator/simulator/Simulator.java
test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/main/java/org/onap/pnfsimulator/simulator/SimulatorFactory.java
test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/main/java/org/onap/pnfsimulator/simulator/validation/NoRopFilesException.java [new file with mode: 0644]
test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/message/JSONObjectFactoryTest.java
test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/message/MessageProviderTest.java
test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/rest/SimulatorControllerTest.java
test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/simulator/SimulatorFactoryTest.java
test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/simulator/SimulatorTest.java
test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/java/org/onap/pnfsimulator/simulator/TestMessages.java
test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/resources/org/onap/pnfsimulator/simulator/validCommonEventHeaderParams.json
test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/resources/org/onap/pnfsimulator/simulator/validNotificationParams.json
test/mocks/mass-pnf-sim/pnf-sim-lightweight/src/test/resources/org/onap/pnfsimulator/simulator/validSimulatorParams.json

index 6ba7079..6507e05 100755 (executable)
@@ -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
index 9eb7332..beb564d 100644 (file)
@@ -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<String> getFiles() {
+    public List<String> getFiles() throws NoRopFilesException {
 
         List<String> files = queryFiles();
 
@@ -22,17 +21,15 @@ public class FileProvider {
         return fileListSorted;
     }
 
-    private static List<String> queryFiles() {
+    private static List<String> queryFiles() throws NoRopFilesException {
 
         File folder = new File("./files/onap/");
         File[] listOfFiles = folder.listFiles();
-        List<String> 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<String> results = new ArrayList<>();
         for (int i = 0; i < listOfFiles.length; i++) {
             if (listOfFiles[i].isFile()) {
                 results.add(listOfFiles[i].getName());
index 5c64057..ba11476 100644 (file)
@@ -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<JSONObject> 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<String> fileList = FileProvider.getFiles();
+
+                List<String> 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<JSONObject> 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;
index a01c2e0..21717d8 100644 (file)
@@ -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 (file)
index 0000000..d3765a8
--- /dev/null
@@ -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);
+    }
+}
index 4331195..da41afd 100644 (file)
@@ -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
index aadb54c..0fa8a12 100644 (file)
@@ -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"));
-
     }
 
 }
index 3603480..d1db8d5 100644 (file)
 
 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<String> 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());
     }
index e69de29..d8e60c1 100644 (file)
@@ -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));
+    }
+}
+
+
index e69de29..7ed9f04 100644 (file)
@@ -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<String> 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));
+    }
+}
+
index 7511084..d92b3c2 100644 (file)
@@ -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<JSONObject> VALID_PNF_REGISTRATION_PARAMS = Optional
         .of(new JSONObject(getContent("validPnfRegistrationParams.json")));
-    static final Optional<JSONObject> VALID_NOTIFICATION_PARAMS = Optional
+    public static final Optional<JSONObject> VALID_NOTIFICATION_PARAMS = Optional
         .of(new JSONObject(getContent("validNotificationParams.json")));
 
     static final JSONObject INVALID_SIMULATOR_PARAMS = new JSONObject(
index e0f4550..b988da0 100644 (file)
@@ -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
index f7f463d..af0cdf4 100644 (file)
@@ -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"
 }