Fix configuration in JSon 55/2355/1
authorelinuxhenrik <henrik.b.andersson@est.tech>
Tue, 28 Jan 2020 13:30:18 +0000 (14:30 +0100)
committerelinuxhenrik <henrik.b.andersson@est.tech>
Tue, 28 Jan 2020 13:39:33 +0000 (14:39 +0100)
Change-Id: Ibf4fea77a2a651d4ca4be1ffbab40a9156686217
Signed-off-by: elinuxhenrik <henrik.b.andersson@est.tech>
13 files changed:
policy-agent/pom.xml
policy-agent/src/main/java/org/oransc/policyagent/configuration/ApplicationConfigParser.java
policy-agent/src/main/java/org/oransc/policyagent/controllers/PolicyController.java
policy-agent/src/main/java/org/oransc/policyagent/tasks/RefreshConfigTask.java
policy-agent/src/main/java/org/oransc/policyagent/tasks/RicRecoveryTask.java
policy-agent/src/test/java/org/oransc/policyagent/ApplicationTest.java
policy-agent/src/test/java/org/oransc/policyagent/MockPolicyAgent.java
policy-agent/src/test/java/org/oransc/policyagent/clients/StdA1ClientTest.java
policy-agent/src/test/java/org/oransc/policyagent/configuration/ApplicationConfigParserTest.java [new file with mode: 0644]
policy-agent/src/test/java/org/oransc/policyagent/tasks/RefreshConfigTaskTest.java
policy-agent/src/test/java/org/oransc/policyagent/tasks/RepositorySupervisionTest.java
policy-agent/src/test/java/org/oransc/policyagent/tasks/StartupServiceTest.java
policy-agent/src/test/resources/test_application_configuration_with_dmaap_config.json

index 06f77c5..311229f 100644 (file)
@@ -1,13 +1,23 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<!-- * ========================LICENSE_START================================= 
-       * O-RAN-SC * %% * Copyright (C) 2019 Nordix Foundation * %% * 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=================================== -->
+<!--
+* ========================LICENSE_START=================================
+* O-RAN-SC
+* %%
+* Copyright (C) 2019 Nordix Foundation
+* %%
+* 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===================================
+-->
 <project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
@@ -15,7 +25,7 @@
        <parent>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-parent</artifactId>
-               <version>2.1.6.RELEASE</version>
+               <version>2.2.4.RELEASE</version>
                <relativePath />
        </parent>
        <groupId>org.o-ran-sc.nonrtric</groupId>
index 807b12f..1352cb5 100644 (file)
@@ -34,7 +34,7 @@ import java.util.Set;
 import java.util.Vector;
 
 import javax.validation.constraints.NotNull;
-
+import lombok.Getter;
 import org.onap.dmaap.mr.test.clients.ProtocolTypeConstants;
 import org.oransc.policyagent.exceptions.ServiceException;
 import org.springframework.http.MediaType;
@@ -47,23 +47,23 @@ public class ApplicationConfigParser {
         .serializeNulls() //
         .create(); //
 
-    private Vector<RicConfig> ricConfig;
+    @Getter
+    private Vector<RicConfig> ricConfigs;
+    @Getter
     private Properties dmaapPublisherConfig;
+    @Getter
     private Properties dmaapConsumerConfig;
 
-    public ApplicationConfigParser() {
-    }
-
     public void parse(JsonObject root) throws ServiceException {
-        JsonObject ricConfigJson = root.getAsJsonObject(CONFIG);
-        ricConfig = parseRics(ricConfigJson);
-        JsonObject dmaapPublisherConfigJson = root.getAsJsonObject("streams_publishes");
+        JsonObject agentConfigJson = root.getAsJsonObject(CONFIG);
+        ricConfigs = parseRics(agentConfigJson);
+        JsonObject dmaapPublisherConfigJson = agentConfigJson.getAsJsonObject("streams_publishes");
         if (dmaapPublisherConfigJson == null) {
             dmaapPublisherConfig = new Properties();
         } else {
             dmaapPublisherConfig = parseDmaapConfig(dmaapPublisherConfigJson);
         }
-        JsonObject dmaapConsumerConfigJson = root.getAsJsonObject("streams_subscribes");
+        JsonObject dmaapConsumerConfigJson = agentConfigJson.getAsJsonObject("streams_subscribes");
         if (dmaapConsumerConfigJson == null) {
             dmaapConsumerConfig = new Properties();
         } else {
@@ -71,18 +71,6 @@ public class ApplicationConfigParser {
         }
     }
 
-    public Vector<RicConfig> getRicConfigs() {
-        return this.ricConfig;
-    }
-
-    public Properties getDmaapPublisherConfig() {
-        return dmaapPublisherConfig;
-    }
-
-    public Properties getDmaapConsumerConfig() {
-        return dmaapConsumerConfig;
-    }
-
     private Vector<RicConfig> parseRics(JsonObject config) throws ServiceException {
         Vector<RicConfig> result = new Vector<RicConfig>();
         for (JsonElement ricElem : getAsJsonArray(config, "ric")) {
index 4376f04..97fe2a5 100644 (file)
@@ -145,7 +145,7 @@ public class PolicyController {
     public Mono<ResponseEntity<Void>> deletePolicy( //
         @RequestParam(name = "instance", required = true) String id) {
         Policy policy = policies.get(id);
-        if (policy != null && policy.ric().getState() == (Ric.RicState.IDLE)) {
+        if (policy != null && policy.ric().getState() == Ric.RicState.IDLE) {
             policies.remove(policy);
             return a1ClientFactory.createA1Client(policy.ric()) //
                 .flatMap(client -> client.deletePolicy(policy)) //
@@ -169,7 +169,7 @@ public class PolicyController {
 
         Ric ric = rics.get(ricName);
         PolicyType type = policyTypes.get(typeName);
-        if (ric != null && type != null && ric.getState() == (Ric.RicState.IDLE)) {
+        if (ric != null && type != null && ric.getState() == Ric.RicState.IDLE) {
             Policy policy = ImmutablePolicy.builder() //
                 .id(instanceId) //
                 .json(jsonBody) //
index 1ab5fc9..267fc1f 100644 (file)
@@ -146,8 +146,7 @@ public class RefreshConfigTask {
         ServiceLoader.load(TypeAdapterFactory.class).forEach(gsonBuilder::registerTypeAdapterFactory);
 
         try (InputStream inputStream = createInputStream(filepath)) {
-            JsonParser parser = new JsonParser();
-            JsonObject rootObject = getJsonElement(parser, inputStream).getAsJsonObject();
+            JsonObject rootObject = getJsonElement(inputStream).getAsJsonObject();
             if (rootObject == null) {
                 throw new JsonSyntaxException("Root is not a json object");
             }
@@ -161,8 +160,8 @@ public class RefreshConfigTask {
         }
     }
 
-    JsonElement getJsonElement(JsonParser parser, InputStream inputStream) {
-        return parser.parse(new InputStreamReader(inputStream));
+    JsonElement getJsonElement(InputStream inputStream) {
+        return JsonParser.parseReader(new InputStreamReader(inputStream));
     }
 
     InputStream createInputStream(@NotNull String filepath) throws IOException {
index b67dbe0..ab25eab 100644 (file)
@@ -41,7 +41,8 @@ import reactor.core.publisher.Flux;
 import reactor.core.publisher.Mono;
 
 /**
- * Recovery handling of RIC, which means:
+ * Recovery handling of RIC.
+ * This means:
  * - load all policy types
  * - send all policy instances to the RIC
  * --- if that fails remove all policy instances
@@ -68,7 +69,7 @@ public class RicRecoveryTask {
         logger.debug("Handling ric: {}", ric.getConfig().name());
 
         synchronized (ric) {
-            if (ric.getState() == (Ric.RicState.RECOVERING)) {
+            if (ric.getState() == Ric.RicState.RECOVERING) {
                 return; // Already running
             }
             ric.setState(Ric.RicState.RECOVERING);
index 5b1f3ca..d9a4b5c 100644 (file)
@@ -165,12 +165,12 @@ public class ApplicationTest {
     @Test
     public void testRecovery() throws Exception {
         reset();
-        Policy policy = addPolicy("policyId", "typeName", "service", "ric"); // This should be created in the RIC
         Policy policy2 = addPolicy("policyId2", "typeName", "service", "ric");
 
         getA1Client("ric").putPolicy(policy2); // put it in the RIC
         policies.remove(policy2); // Remove it from the repo -> should be deleted in the RIC
 
+        Policy policy = addPolicy("policyId", "typeName", "service", "ric"); // This should be created in the RIC
         supervision.checkAllRics(); // The created policy should be put in the RIC
         Policies ricPolicies = getA1Client("ric").getPolicies();
         assertThat(ricPolicies.size()).isEqualTo(1);
@@ -419,7 +419,7 @@ public class ApplicationTest {
 
     private static <T> List<T> parseList(String jsonString, Class<T> clazz) {
         List<T> result = new ArrayList<>();
-        JsonArray jsonArr = new JsonParser().parse(jsonString).getAsJsonArray();
+        JsonArray jsonArr = JsonParser.parseString(jsonString).getAsJsonArray();
         for (JsonElement jsonElement : jsonArr) {
             T o = gson.fromJson(jsonElement.toString(), clazz);
             result.add(o);
@@ -428,7 +428,7 @@ public class ApplicationTest {
     }
 
     private static List<String> parseSchemas(String jsonString) {
-        JsonArray arrayOfSchema = new JsonParser().parse(jsonString).getAsJsonArray();
+        JsonArray arrayOfSchema = JsonParser.parseString(jsonString).getAsJsonArray();
         List<String> result = new ArrayList<>();
         for (JsonElement schemaObject : arrayOfSchema) {
             result.add(schemaObject.toString());
index b4c4129..1ea677c 100644 (file)
@@ -61,7 +61,7 @@ public class MockPolicyAgent {
     }
 
     /**
-     * overrides the BeanFactory
+     * Overrides the BeanFactory.
      */
     @TestConfiguration
     static class TestBeanFactory {
@@ -138,7 +138,7 @@ public class MockPolicyAgent {
     }
 
     private static String title(String jsonSchema) {
-        JsonObject parsedSchema = (JsonObject) new JsonParser().parse(jsonSchema);
+        JsonObject parsedSchema = (JsonObject) JsonParser.parseString(jsonSchema);
         String title = parsedSchema.get("title").getAsString();
         return title;
     }
index 2c83e5a..05e5463 100644 (file)
@@ -34,8 +34,6 @@ import org.json.JSONException;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
-import org.junit.runner.RunWith;
-import org.mockito.junit.MockitoJUnitRunner;
 import org.mockito.junit.jupiter.MockitoExtension;
 import org.oransc.policyagent.configuration.ImmutableRicConfig;
 import org.oransc.policyagent.configuration.RicConfig;
@@ -49,7 +47,6 @@ import reactor.core.publisher.Mono;
 import reactor.test.StepVerifier;
 
 @ExtendWith(MockitoExtension.class)
-@RunWith(MockitoJUnitRunner.class)
 public class StdA1ClientTest {
     private static final String RIC_URL = "RicUrl";
     private static final String POLICYTYPES_IDENTITIES_URL = "/policytypes";
diff --git a/policy-agent/src/test/java/org/oransc/policyagent/configuration/ApplicationConfigParserTest.java b/policy-agent/src/test/java/org/oransc/policyagent/configuration/ApplicationConfigParserTest.java
new file mode 100644 (file)
index 0000000..9b7dc81
--- /dev/null
@@ -0,0 +1,92 @@
+/*-
+ * ========================LICENSE_START=================================
+ * O-RAN-SC
+ * %%
+ * Copyright (C) 2019 Nordix Foundation
+ * %%
+ * 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.oransc.policyagent.configuration;
+
+import static org.junit.jupiter.api.Assertions.assertAll;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import com.google.common.base.Charsets;
+import com.google.common.io.Resources;
+import com.google.gson.JsonIOException;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+import com.google.gson.JsonSyntaxException;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
+import java.util.Properties;
+import org.junit.jupiter.api.Test;
+import org.onap.dmaap.mr.test.clients.ProtocolTypeConstants;
+import org.springframework.http.MediaType;
+
+public class ApplicationConfigParserTest {
+
+    @Test
+    public void whenCorrectDmaapConfig() throws Exception {
+        JsonObject jsonRootObject = getJsonRootObject();
+
+        ApplicationConfigParser parserUnderTest = new ApplicationConfigParser();
+
+        parserUnderTest.parse(jsonRootObject);
+
+        Properties actualPublisherConfig = parserUnderTest.getDmaapPublisherConfig();
+        assertAll("publisherConfig",
+            () -> assertEquals("localhost:6845/events", actualPublisherConfig.get("ServiceName")),
+            () -> assertEquals("A1-POLICY-AGENT-WRITE", actualPublisherConfig.get("topic")),
+            () -> assertEquals("localhost:6845", actualPublisherConfig.get("host")),
+            () -> assertEquals(MediaType.APPLICATION_JSON.toString(), actualPublisherConfig.get("contenttype")),
+            () -> assertEquals("admin", actualPublisherConfig.get("userName")),
+            () -> assertEquals("admin", actualPublisherConfig.get("password")),
+            () -> assertEquals(ProtocolTypeConstants.HTTPNOAUTH.toString(), actualPublisherConfig.get("TransportType")),
+            () -> assertEquals(15000, actualPublisherConfig.get("timeout")),
+            () -> assertEquals(1000, actualPublisherConfig.get("limit")));
+
+        Properties actualConsumerConfig = parserUnderTest.getDmaapConsumerConfig();
+        assertAll("consumerConfig",
+            () -> assertEquals("localhost:6845/events", actualConsumerConfig.get("ServiceName")),
+            () -> assertEquals("A1-POLICY-AGENT-READ", actualConsumerConfig.get("topic")),
+            () -> assertEquals("localhost:6845", actualConsumerConfig.get("host")),
+            () -> assertEquals(MediaType.APPLICATION_JSON.toString(), actualConsumerConfig.get("contenttype")),
+            () -> assertEquals("admin", actualConsumerConfig.get("userName")),
+            () -> assertEquals("admin", actualConsumerConfig.get("password")),
+            () -> assertEquals("users", actualConsumerConfig.get("group")),
+            () -> assertEquals("policy-agent", actualConsumerConfig.get("id")),
+            () -> assertEquals(ProtocolTypeConstants.HTTPNOAUTH.toString(), actualConsumerConfig.get("TransportType")),
+            () -> assertEquals(15000, actualConsumerConfig.get("timeout")),
+            () -> assertEquals(1000, actualConsumerConfig.get("limit")));
+    }
+
+    private JsonObject getJsonRootObject() throws JsonIOException, JsonSyntaxException, IOException {
+        JsonObject rootObject = JsonParser.parseReader(new InputStreamReader(getCorrectJson())).getAsJsonObject();
+        return rootObject;
+    }
+
+    private static InputStream getCorrectJson() throws IOException {
+        URL url = ApplicationConfigParser.class.getClassLoader()
+            .getResource("test_application_configuration_with_dmaap_config.json");
+        String string = Resources.toString(url, Charsets.UTF_8);
+        return new ByteArrayInputStream((string.getBytes(StandardCharsets.UTF_8)));
+    }
+
+}
index 788ac41..90b3847 100644 (file)
@@ -51,10 +51,8 @@ import java.util.Vector;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
-import org.junit.runner.RunWith;
 import org.mockito.Mock;
 import org.mockito.Spy;
-import org.mockito.junit.MockitoJUnitRunner;
 import org.mockito.junit.jupiter.MockitoExtension;
 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient;
 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties;
@@ -71,7 +69,6 @@ import reactor.core.publisher.Mono;
 import reactor.test.StepVerifier;
 
 @ExtendWith(MockitoExtension.class)
-@RunWith(MockitoJUnitRunner.class)
 public class RefreshConfigTaskTest {
 
     private RefreshConfigTask refreshTaskUnderTest;
@@ -191,7 +188,7 @@ public class RefreshConfigTaskTest {
     }
 
     private JsonObject getJsonRootObject() throws JsonIOException, JsonSyntaxException, IOException {
-        JsonObject rootObject = (new JsonParser()).parse(new InputStreamReader(getCorrectJson())).getAsJsonObject();
+        JsonObject rootObject = JsonParser.parseReader(new InputStreamReader(getCorrectJson())).getAsJsonObject();
         return rootObject;
     }
 
index 92f48de..fb8d46f 100644 (file)
@@ -35,9 +35,7 @@ import java.util.Vector;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
-import org.junit.runner.RunWith;
 import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
 import org.mockito.junit.jupiter.MockitoExtension;
 import org.oransc.policyagent.clients.A1Client;
 import org.oransc.policyagent.clients.A1ClientFactory;
@@ -57,7 +55,6 @@ import reactor.core.publisher.Flux;
 import reactor.core.publisher.Mono;
 
 @ExtendWith(MockitoExtension.class)
-@RunWith(MockitoJUnitRunner.class)
 public class RepositorySupervisionTest {
     @Mock
     A1Client a1ClientMock;
@@ -111,9 +108,6 @@ public class RepositorySupervisionTest {
         PolicyTypes types = new PolicyTypes();
         Services services = new Services();
 
-        RepositorySupervision supervisorUnderTest =
-            new RepositorySupervision(rics, policies, a1ClientFactory, types, services);
-
         Mono<List<String>> policyIds = Mono.just(Arrays.asList("policyId1", "policyId2"));
 
         doReturn(policyIds).when(a1ClientMock).getPolicyTypeIdentities();
@@ -122,6 +116,9 @@ public class RepositorySupervisionTest {
         doReturn(Mono.just("OK")).when(a1ClientMock).putPolicy(any());
         doReturn(Flux.empty()).when(a1ClientMock).deleteAllPolicies();
 
+        RepositorySupervision supervisorUnderTest =
+            new RepositorySupervision(rics, policies, a1ClientFactory, types, services);
+
         supervisorUnderTest.checkAllRics();
 
         await().untilAsserted(() -> RicState.IDLE.equals(ric1.getState()));
index 164aca8..8bf705c 100644 (file)
@@ -41,8 +41,6 @@ import java.util.Vector;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
-import org.junit.runner.RunWith;
-import org.mockito.junit.MockitoJUnitRunner;
 import org.mockito.junit.jupiter.MockitoExtension;
 import org.oransc.policyagent.clients.A1Client;
 import org.oransc.policyagent.clients.A1ClientFactory;
@@ -60,7 +58,6 @@ import reactor.core.publisher.Flux;
 import reactor.core.publisher.Mono;
 
 @ExtendWith(MockitoExtension.class)
-@RunWith(MockitoJUnitRunner.class)
 public class StartupServiceTest {
     private static final String FIRST_RIC_NAME = "first";
     private static final String FIRST_RIC_URL = "firstUrl";
@@ -72,8 +69,6 @@ public class StartupServiceTest {
 
     private static final String POLICY_TYPE_1_NAME = "type1";
     private static final String POLICY_TYPE_2_NAME = "type2";
-    private static final String POLICY_ID_1 = "policy1";
-    private static final String POLICY_ID_2 = "policy2";
 
     ApplicationConfig appConfigMock;
     RefreshConfigTask refreshTaskMock;
index c945360..ec292bf 100644 (file)
@@ -1,39 +1,38 @@
 {
-   "config": {
-      "//description": "Application configuration",
-      "ric": [
+   "config":{
+      "ric":[
          {
-            "name": "ric1",
-            "baseUrl": "http://localhost:8080/",
-            "managedElementIds": [
+            "name":"ric1",
+            "baseUrl":"http://localhost:8083/",
+            "managedElementIds":[
                "kista_1",
                "kista_2"
             ]
          },
          {
-            "name": "ric2",
-            "baseUrl": "http://localhost:8081/",
-            "managedElementIds": [
+            "name":"ric2",
+            "baseUrl":"http://localhost:8085/",
+            "managedElementIds":[
                "kista_3",
                "kista_4"
             ]
          }
-      ]
-   },
-   "streams_publishes": {
-      "dmaap_publisher": {
-         "type": "message_router",
-         "dmaap_info": {
-            "topic_url": "http://admin:admin@localhost:6845/events/A1-POLICY-AGENT-WRITE"
+      ],
+      "streams_publishes":{
+         "dmaap_publisher":{
+            "type":"message_router",
+            "dmaap_info":{
+               "topic_url":"http://admin:admin@localhost:6845/events/A1-POLICY-AGENT-WRITE"
+            }
+         }
+      },
+      "streams_subscribes":{
+         "dmaap_subscriber":{
+            "type":"message_router",
+            "dmaap_info":{
+               "topic_url":"http://admin:admin@localhost:6845/events/A1-POLICY-AGENT-READ/users/policy-agent"
+            }
          }
-      }
-   },
-   "streams_subscribes": {
-      "dmaap_subscriber": {
-         "dmaap_info": {
-            "topic_url": "http://admin:admin@localhost:6845/events/A1-POLICY-AGENT-READ/users/policy-agent"
-         },
-         "type": "message_router"
       }
    }
 }
\ No newline at end of file