PNF Simulator SFTP support 29/66629/4
authorMarcin Migdal <marcin.migdal@nokia.com>
Fri, 14 Sep 2018 12:14:03 +0000 (14:14 +0200)
committerMarcin Migdal <marcin.migdal@nokia.com>
Fri, 14 Sep 2018 13:07:41 +0000 (15:07 +0200)
Change-Id: If435a6540f7c8124fc8bbea7ad46a495ab087fda
Issue-ID: INT-607
Signed-off-by: Marcin Migdal <marcin.migdal@nokia.com>
test/mocks/pnfsimulator/pom.xml
test/mocks/pnfsimulator/src/main/java/org/onap/pnfsimulator/message/MessageProvider.java
test/mocks/pnfsimulator/src/test/java/org/onap/pnfsimulator/message/MessageProviderTest.java
test/mocks/pnfsimulator/src/test/java/org/onap/pnfsimulator/simulator/SimulatorFactoryTest.java
test/mocks/pnfsimulator/src/test/java/org/onap/pnfsimulator/simulator/TestMessages.java

index 03b3883..cddbbba 100644 (file)
@@ -4,7 +4,7 @@
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
 
-  
+
   <parent>
     <groupId>org.onap.oparent</groupId>
     <artifactId>oparent</artifactId>
index 4931c3b..13114ee 100644 (file)
@@ -31,13 +31,12 @@ import static org.onap.pnfsimulator.message.MessageConstants.PNF_REGISTRATION_FI
 
 import java.util.Map;
 import java.util.Optional;
-import javax.annotation.Nonnull;
 import org.json.JSONObject;
 
 public class MessageProvider {
 
-    public JSONObject createMessage(@Nonnull  JSONObject commonEventHeaderParams,@Nonnull Optional<JSONObject> pnfRegistrationParams,
-        @Nonnull Optional<JSONObject> notificationParams) {
+    public JSONObject createMessage(JSONObject commonEventHeaderParams, Optional<JSONObject> pnfRegistrationParams,
+        Optional<JSONObject> notificationParams) {
 
         if (!pnfRegistrationParams.isPresent() && !notificationParams.isPresent()) {
             throw new IllegalArgumentException(
index d40e29c..aadb54c 100644 (file)
@@ -35,8 +35,11 @@ import org.junit.jupiter.api.Test;
 
 public class MessageProviderTest {
 
-    private static final String testParamsJson =
-        "{\"key1\": \"val1\",\"key2\": \"val2\",\"pnf_key3\": \"pnfVal3\",\"key4\": \"val4\"}";
+    private static final String testParamsPnfRegistration =
+        "{\"pnfKey1\": \"pnfVal1\",\"pnfKey2\": \"pnfVal2\",\"pnfKey3\": \"pnfVal3\",\"pnfKey4\": \"pnfVal4\"}";
+
+    private static final String testParamsNotification =
+        "{\"notKey1\": \"notVal1\",\"notKey2\": \"notVal2\",\"notKey3\": \"notVal3\",\"notKey4\": \"notVal4\"}";
 
     private static MessageProvider messageProvider;
 
@@ -48,13 +51,14 @@ public class MessageProviderTest {
     @Test
     public void createMessage_should_throw_when_given_empty_arguments() {
         assertThrows(IllegalArgumentException.class,
-            () -> messageProvider.createMessage(new JSONObject(),Optional.empty(),Optional.empty()),
+            () -> messageProvider.createMessage(new JSONObject(), Optional.empty(), Optional.empty()),
             "Params object cannot be null");
     }
 
     @Test
     public void createMessage_should_create_constant_message_when_no_params_specified() {
-        JSONObject message = messageProvider.createMessage(new JSONObject(),Optional.ofNullable(new JSONObject()),Optional.ofNullable(new JSONObject()));
+        JSONObject message = messageProvider.createMessage(new JSONObject(), Optional.ofNullable(new JSONObject()),
+            Optional.ofNullable(new JSONObject()));
         JSONObject event = message.getJSONObject(EVENT);
 
         JSONObject commonEventHeader = event.getJSONObject(COMMON_EVENT_HEADER);
@@ -83,21 +87,29 @@ public class MessageProviderTest {
 
     @Test
     public void createMessage_should_throw_exception_when_params_specified_as_empty() {
-        assertThrows(IllegalArgumentException.class, () ->messageProvider.createMessage(new JSONObject(), Optional.empty(),
-            Optional.empty()));
+        assertThrows(IllegalArgumentException.class,
+            () -> messageProvider.createMessage(new JSONObject(), Optional.empty(),
+                Optional.empty()));
     }
 
     @Test
     public void createMessage_should_add_specified_params_to_valid_subobjects() {
-        JSONObject params = new JSONObject(testParamsJson);
-        JSONObject message = messageProvider.createMessage(new JSONObject(),Optional.of(params),Optional.empty());
+        JSONObject message = messageProvider
+            .createMessage(new JSONObject(), Optional.of(new JSONObject(testParamsPnfRegistration)),
+                Optional.of(new JSONObject(testParamsNotification)));
         JSONObject event = message.getJSONObject(EVENT);
 
         JSONObject commonEventHeader = event.getJSONObject(COMMON_EVENT_HEADER);
+        assertEquals(10, commonEventHeader.keySet().size());
+
         JSONObject pnfRegistrationFields = event.getJSONObject(PNF_REGISTRATION_FIELDS);
+        assertEquals("pnfVal1", pnfRegistrationFields.getString("pnfKey1"));
+        assertEquals("pnfVal2", pnfRegistrationFields.getString("pnfKey2"));
+
+        JSONObject notificationFields = event.getJSONObject(NOTIFICATION_FIELDS);
+        assertEquals("notVal1", notificationFields.getString("notKey1"));
+        assertEquals("notVal2", notificationFields.getString("notKey2"));
 
-        assertEquals("val1", pnfRegistrationFields.getString("key1"));
-        assertEquals("val2", pnfRegistrationFields.getString("key2"));
     }
 
 }
index 26f66b7..ea7a097 100644 (file)
@@ -22,13 +22,15 @@ package org.onap.pnfsimulator.simulator;
 
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
-import static org.onap.pnfsimulator.simulator.TestMessages.INVALID_MESSAGE_PARAMS_1;
-import static org.onap.pnfsimulator.simulator.TestMessages.INVALID_MESSAGE_PARAMS_2;
-import static org.onap.pnfsimulator.simulator.TestMessages.INVALID_MESSAGE_PARAMS_3;
+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.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 static org.onap.pnfsimulator.simulator.TestMessages.VALID_COMMON_EVENT_HEADER_PARAMS;
 
 import com.github.fge.jsonschema.core.exceptions.ProcessingException;
 import java.io.IOException;
@@ -51,12 +53,19 @@ class SimulatorFactoryTest {
     }
 
     @Test
-    void should_successfully_create_simulator_given_valid_params_and_valid_output_message()
+    void should_successfully_create_simulator_given_valid_pnf_registration_params_and_valid_output_message()
         throws ValidationException, IOException, ProcessingException {
         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()
+        throws ValidationException, IOException, ProcessingException {
+        assertNotNull(simulatorFactory.create(VALID_SIMULATOR_PARAMS, VALID_COMMON_EVENT_HEADER_PARAMS,
+            Optional.empty(), VALID_NOTIFICATION_PARAMS));
+    }
+
     @Test
     void should_throw_given_invalid_params() {
         assertThrows(
@@ -70,14 +79,22 @@ class SimulatorFactoryTest {
 
         assertThrows(
             ValidationException.class,
-            () -> simulatorFactory.create(VALID_SIMULATOR_PARAMS, VALID_COMMON_EVENT_HEADER_PARAMS ,INVALID_MESSAGE_PARAMS_1,Optional.empty()));
+            () -> simulatorFactory.create(VALID_SIMULATOR_PARAMS, VALID_COMMON_EVENT_HEADER_PARAMS,
+                INVALID_PNF_REGISTRATION_PARAMS_1, Optional.empty()));
+
+        assertThrows(
+            ValidationException.class,
+            () -> simulatorFactory.create(VALID_SIMULATOR_PARAMS, VALID_COMMON_EVENT_HEADER_PARAMS,
+                INVALID_PNF_REGISTRATION_PARAMS_2, Optional.empty()));
 
         assertThrows(
             ValidationException.class,
-            () -> simulatorFactory.create(VALID_SIMULATOR_PARAMS, VALID_COMMON_EVENT_HEADER_PARAMS, INVALID_MESSAGE_PARAMS_2,Optional.empty()));
+            () -> simulatorFactory.create(VALID_SIMULATOR_PARAMS, VALID_COMMON_EVENT_HEADER_PARAMS,
+                INVALID_PNF_REGISTRATION_PARAMS_3, Optional.empty()));
 
         assertThrows(
             ValidationException.class,
-            () -> simulatorFactory.create(VALID_SIMULATOR_PARAMS, VALID_COMMON_EVENT_HEADER_PARAMS, INVALID_MESSAGE_PARAMS_3,Optional.empty()));
+            () -> simulatorFactory.create(VALID_SIMULATOR_PARAMS, VALID_COMMON_EVENT_HEADER_PARAMS,
+                VALID_PNF_REGISTRATION_PARAMS, INVALID_NOTIFICATION_PARAMS));
     }
 }
\ No newline at end of file
index 7f33e17..7511084 100644 (file)
@@ -22,7 +22,6 @@ package org.onap.pnfsimulator.simulator;
 
 import java.io.IOException;
 import java.nio.file.Files;
-import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.util.Optional;
 import org.json.JSONObject;
@@ -31,23 +30,10 @@ 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"));
-    static final Optional<JSONObject> VALID_PNF_REGISTRATION_PARAMS = Optional.ofNullable(new JSONObject(getContent("validPnfRegistrationParams.json")));
-
-    private static String getContent(String fileName){
-
-        try {
-            String pathAsString = TestMessages.class.getResource(fileName).getPath();
-            Path path  = Paths.get(pathAsString);
-            StringBuilder stringBuilder = new StringBuilder();
-            Files.readAllLines(path).forEach(line -> {
-                stringBuilder.append(line);
-            });
-            return stringBuilder.toString();
-        } catch (IOException e) {
-            throw new RuntimeException(String.format("Cannot read JSON file %s",fileName));
-        }
-
-    }
+    static final Optional<JSONObject> VALID_PNF_REGISTRATION_PARAMS = Optional
+        .of(new JSONObject(getContent("validPnfRegistrationParams.json")));
+    static final Optional<JSONObject> VALID_NOTIFICATION_PARAMS = Optional
+        .of(new JSONObject(getContent("validNotificationParams.json")));
 
     static final JSONObject INVALID_SIMULATOR_PARAMS = new JSONObject(
         "{\n" +
@@ -56,7 +42,7 @@ final class TestMessages {
             "}");
 
 
-    static final Optional<JSONObject> INVALID_MESSAGE_PARAMS_1 = Optional.ofNullable(new JSONObject(
+    static final Optional<JSONObject> INVALID_PNF_REGISTRATION_PARAMS_1 = Optional.of(new JSONObject(
         "{\n" +
             "    \"pnfSerialNumber\": \"val1\",\n" +
             "    \"pnfVendorName\": \"val2\",\n" +
@@ -72,7 +58,7 @@ final class TestMessages {
             "    \"reportingEntityName\": \"val14\"\n" +
             "}"));
 
-    static final Optional<JSONObject> INVALID_MESSAGE_PARAMS_2 = Optional.ofNullable(new JSONObject(
+    static final Optional<JSONObject> INVALID_PNF_REGISTRATION_PARAMS_2 = Optional.of(new JSONObject(
         "{\n" +
             "    \"pnfVendorName\": \"val2\",\n" +
             "    \"pnfOamIpv4Address\": \"val3\",\n" +
@@ -89,7 +75,7 @@ final class TestMessages {
             "    \"reportingEntityName\": \"val14\"\n" +
             "}"));
 
-    static final Optional<JSONObject> INVALID_MESSAGE_PARAMS_3 = Optional.ofNullable(new JSONObject(
+    static final Optional<JSONObject> INVALID_PNF_REGISTRATION_PARAMS_3 = Optional.of(new JSONObject(
         "{\n" +
             "    \"pnfSerialNumber\": \"val1\",\n" +
             "    \"pnfOamIpv4Address\": \"val3\",\n" +
@@ -105,7 +91,26 @@ final class TestMessages {
             "    \"reportingEntityName\": \"val14\"\n" +
             "}"));
 
+    static final Optional<JSONObject> INVALID_NOTIFICATION_PARAMS = Optional.of(new JSONObject(
+        "{\n" +
+            "    \"mother\": \"val1\",\n" +
+            "    \"father\": \"val3\",\n" +
+            "}"));
+
 
     private TestMessages() {
     }
+
+    private static String getContent(String fileName) {
+        try {
+            String pathAsString = TestMessages.class.getResource(fileName).getPath();
+            StringBuilder stringBuilder = new StringBuilder();
+            Files.readAllLines(Paths.get(pathAsString)).forEach(line -> {
+                stringBuilder.append(line);
+            });
+            return stringBuilder.toString();
+        } catch (IOException e) {
+            throw new RuntimeException(String.format("Cannot read JSON file %s", fileName));
+        }
+    }
 }