Merge "Added STD sim 2.0.0 tests"
[nonrtric.git] / policy-agent / src / test / java / org / oransc / policyagent / configuration / ApplicationConfigParserTest.java
index 79eb07a..5a6b023 100644 (file)
@@ -20,7 +20,6 @@
 
 package org.oransc.policyagent.configuration;
 
-import static org.junit.jupiter.api.Assertions.assertAll;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 
@@ -36,56 +35,30 @@ import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
-import java.net.MalformedURLException;
 import java.net.URL;
 import java.nio.charset.StandardCharsets;
 import java.util.Map;
-import java.util.Properties;
 
 import org.junit.jupiter.api.Test;
-import org.onap.dmaap.mr.test.clients.ProtocolTypeConstants;
 import org.oransc.policyagent.exceptions.ServiceException;
-import org.springframework.http.MediaType;
 
-public class ApplicationConfigParserTest {
+class ApplicationConfigParserTest {
 
     ApplicationConfigParser parserUnderTest = new ApplicationConfigParser();
 
     @Test
-    public void whenCorrectConfig() throws Exception {
+    void whenCorrectConfig() throws Exception {
         JsonObject jsonRootObject = getJsonRootObject();
 
         ApplicationConfigParser.ConfigParserResult result = parserUnderTest.parse(jsonRootObject);
 
-        Properties actualPublisherConfig = result.dmaapPublisherConfig();
-        assertAll("publisherConfig",
-            () -> assertEquals("localhost:6845/events", actualPublisherConfig.get("ServiceName"), "Wrong ServiceName"),
-            () -> assertEquals("A1-POLICY-AGENT-WRITE", actualPublisherConfig.get("topic"), "Wrong topic"),
-            () -> assertEquals("localhost:6845", actualPublisherConfig.get("host"), "Wrong host"),
-            () -> assertEquals(MediaType.APPLICATION_JSON.toString(), actualPublisherConfig.get("contenttype"),
-                "Wrong contenttype"),
-            () -> assertEquals("admin", actualPublisherConfig.get("userName"), "Wrong userName"),
-            () -> assertEquals("admin", actualPublisherConfig.get("password"), "Wrong password"),
-            () -> assertEquals(ProtocolTypeConstants.HTTPNOAUTH.toString(), actualPublisherConfig.get("TransportType"),
-                "Wrong TransportType"),
-            () -> assertEquals("15000", actualPublisherConfig.get("timeout"), "Wrong timeout"),
-            () -> assertEquals("100", actualPublisherConfig.get("limit"), "Wrong limit"));
-
-        Properties actualConsumerConfig = result.dmaapConsumerConfig();
-        assertAll("consumerConfig",
-            () -> assertEquals("localhost:6845/events", actualConsumerConfig.get("ServiceName"), "Wrong ServiceName"),
-            () -> assertEquals("A1-POLICY-AGENT-READ", actualConsumerConfig.get("topic"), "Wrong topic"),
-            () -> assertEquals("localhost:6845", actualConsumerConfig.get("host"), "Wrong host"),
-            () -> assertEquals(MediaType.APPLICATION_JSON.toString(), actualConsumerConfig.get("contenttype"),
-                "Wrong contenttype"),
-            () -> assertEquals("admin", actualConsumerConfig.get("userName"), "Wrong userName"),
-            () -> assertEquals("admin", actualConsumerConfig.get("password"), "Wrong password"),
-            () -> assertEquals("users", actualConsumerConfig.get("group"), "Wrong group"),
-            () -> assertEquals("policy-agent", actualConsumerConfig.get("id"), "Wrong id"),
-            () -> assertEquals(ProtocolTypeConstants.HTTPNOAUTH.toString(), actualConsumerConfig.get("TransportType"),
-                "Wrong TransportType"),
-            () -> assertEquals("15000", actualConsumerConfig.get("timeout"), "Wrong timeout"),
-            () -> assertEquals("100", actualConsumerConfig.get("limit"), "Wrong limit"));
+        String topicUrl = result.dmaapProducerTopicUrl();
+        assertEquals("http://admin:admin@localhost:6845/events/A1-POLICY-AGENT-WRITE", topicUrl, "controller contents");
+
+        topicUrl = result.dmaapConsumerTopicUrl();
+        assertEquals(
+            "http://admin:admin@localhost:6845/events/A1-POLICY-AGENT-READ/users/policy-agent?timeout=15000&limit=100",
+            topicUrl, "controller contents");
 
         Map<String, ControllerConfig> controllers = result.controllerConfigs();
         assertEquals(1, controllers.size(), "size");
@@ -111,7 +84,7 @@ public class ApplicationConfigParserTest {
     }
 
     @Test
-    public void whenDmaapConfigHasSeveralStreamsPublishing() throws Exception {
+    void whenDmaapConfigHasSeveralStreamsPublishing() throws Exception {
         JsonObject jsonRootObject = getJsonRootObject();
         JsonObject json = jsonRootObject.getAsJsonObject("config").getAsJsonObject("streams_publishes");
         JsonObject fake_info_object = new JsonObject();
@@ -131,6 +104,7 @@ public class ApplicationConfigParserTest {
         private JsonObject dmaap_publisher;
         private JsonObject fake_info_object;
 
+        @Override
         public String toString() {
             return String.format("[dmaap_publisher=%s, fake_info_object=%s]", dmaap_publisher.toString(),
                 fake_info_object.toString());
@@ -138,7 +112,7 @@ public class ApplicationConfigParserTest {
     }
 
     @Test
-    public void whenDmaapConfigHasSeveralStreamsSubscribing() throws Exception {
+    void whenDmaapConfigHasSeveralStreamsSubscribing() throws Exception {
         JsonObject jsonRootObject = getJsonRootObject();
         JsonObject json = jsonRootObject.getAsJsonObject("config").getAsJsonObject("streams_subscribes");
         JsonObject fake_info_object = new JsonObject();
@@ -158,6 +132,7 @@ public class ApplicationConfigParserTest {
         private JsonObject dmaap_subscriber;
         private JsonObject fake_info_object;
 
+        @Override
         public String toString() {
             return String.format("[dmaap_subscriber=%s, fake_info_object=%s]", dmaap_subscriber.toString(),
                 fake_info_object.toString());
@@ -165,39 +140,7 @@ public class ApplicationConfigParserTest {
     }
 
     @Test
-    public void whenMalformedUrlStreamsSubscribing() throws Exception {
-        JsonObject jsonRootObject = getJsonRootObject();
-        final String wrongTopicUrl = "WrongTopicUrl";
-        JsonObject json = getDmaapInfo(jsonRootObject, "streams_subscribes", "dmaap_subscriber");
-        json.addProperty("topic_url", wrongTopicUrl);
-        final String expectedMessage = "Could not parse the URL";
-
-        Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
-
-        assertEquals(expectedMessage, actualException.getMessage().replace("\"", ""),
-            "Wrong error message when the streams subscribes' URL is malformed");
-        assertEquals(MalformedURLException.class, actualException.getCause().getClass(),
-            "The exception is not a MalformedURLException");
-    }
-
-    @Test
-    public void whenMalformedUrlStreamsPublishing() throws Exception {
-        JsonObject jsonRootObject = getJsonRootObject();
-        final String wrongTopicUrl = "WrongTopicUrl";
-        JsonObject json = getDmaapInfo(jsonRootObject, "streams_publishes", "dmaap_publisher");
-        json.addProperty("topic_url", wrongTopicUrl);
-        final String expectedMessage = "Could not parse the URL";
-
-        Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
-
-        assertEquals(expectedMessage, actualException.getMessage().replace("\"", ""),
-            "Wrong error message when the streams publishes' URL is malformed");
-        assertEquals(MalformedURLException.class, actualException.getCause().getClass(),
-            "The exception is not a MalformedURLException");
-    }
-
-    @Test
-    public void whenWrongMemberNameInObject() throws Exception {
+    void whenWrongMemberNameInObject() throws Exception {
         JsonObject jsonRootObject = getJsonRootObject();
         JsonObject json = jsonRootObject.getAsJsonObject("config");
         json.remove("ric");
@@ -208,39 +151,7 @@ public class ApplicationConfigParserTest {
         assertEquals(message, actualException.getMessage(), "Wrong error message when wrong member name in object");
     }
 
-    @Test
-    public void whenWrongUrlPathStreamsSubscribing() throws Exception {
-        JsonObject jsonRootObject = getJsonRootObject();
-        final String wrongTopicUrlString =
-            "http://admin:admin@localhost:6845/events/A1-POLICY-AGENT-READ/users/policy-agent/wrong-topic-url";
-        final URL wrongTopicUrl = new URL(wrongTopicUrlString);
-        JsonObject json = getDmaapInfo(jsonRootObject, "streams_subscribes", "dmaap_subscriber");
-        json.addProperty("topic_url", wrongTopicUrlString);
-        final String expectedMessage = "The path has incorrect syntax: " + wrongTopicUrl.getPath();
-
-        Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
-
-        assertEquals(expectedMessage, actualException.getMessage(),
-            "Wrong error message when the streams subscribes' URL has incorrect syntax");
-    }
-
-    @Test
-    public void whenWrongUrlPathStreamsPublishing() throws Exception {
-        JsonObject jsonRootObject = getJsonRootObject();
-        final String wrongTopicUrlString =
-            "http://admin:admin@localhost:6845/events/A1-POLICY-AGENT-WRITE/wrong-topic-url";
-        final URL wrongTopicUrl = new URL(wrongTopicUrlString);
-        JsonObject json = getDmaapInfo(jsonRootObject, "streams_publishes", "dmaap_publisher");
-        json.addProperty("topic_url", wrongTopicUrlString);
-        final String expectedMessage = "The path has incorrect syntax: " + wrongTopicUrl.getPath();
-
-        Exception actualException = assertThrows(ServiceException.class, () -> parserUnderTest.parse(jsonRootObject));
-
-        assertEquals(expectedMessage, actualException.getMessage(),
-            "Wrong error message when the streams publishes' URL has incorrect syntax");
-    }
-
-    public JsonObject getDmaapInfo(JsonObject jsonRootObject, String streamsPublishesOrSubscribes,
+    JsonObject getDmaapInfo(JsonObject jsonRootObject, String streamsPublishesOrSubscribes,
         String dmaapPublisherOrSubscriber) throws Exception {
         return jsonRootObject.getAsJsonObject("config").getAsJsonObject(streamsPublishesOrSubscribes)
             .getAsJsonObject(dmaapPublisherOrSubscriber).getAsJsonObject("dmaap_info");