Improve coverage FlowControlNode #7 33/35133/2
authorkurczews <krzysztof.kurczewski@nokia.com>
Fri, 2 Mar 2018 13:10:26 +0000 (14:10 +0100)
committerTakamune Cho <tc012c@att.com>
Mon, 12 Mar 2018 15:12:42 +0000 (15:12 +0000)
* refactor & test capabilities data method

Change-Id: I523d07e283a347a4ad16f3babc8a25e0dd5a6acc
Issue-ID: APPC-440
Signed-off-by: kurczews <krzysztof.kurczewski@nokia.com>
appc-config/appc-flow-controller/provider/src/main/java/org/onap/appc/flow/controller/node/FlowControlNode.java
appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/node/FlowControlNodeTest.java [new file with mode: 0644]

index 35a99a9..16e8690 100644 (file)
@@ -59,6 +59,7 @@ import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VSERVE
 import com.att.eelf.configuration.EELFLogger;
 import com.att.eelf.configuration.EELFManager;
 import com.fasterxml.jackson.annotation.JsonInclude.Include;
+import com.fasterxml.jackson.core.type.TypeReference;
 import com.fasterxml.jackson.databind.DeserializationFeature;
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
@@ -468,66 +469,42 @@ public class FlowControlNode implements SvcLogicJavaPlugin {
     return dependencyInfo;
   }
 
-  private Capabilities getCapabilitiesData(SvcLogicContext ctx) throws Exception {
+  Capabilities getCapabilitiesData(SvcLogicContext ctx) throws Exception {
 
     String fn = "FlowExecutorNode.getCapabilitiesData";
-    Capabilities capabilities = new Capabilities();
     String capabilitiesData = dbService.getCapabilitiesData(ctx);
     log.info(fn + "capabilitiesDataInput:" + capabilitiesData);
 
-    if (capabilitiesData != null) {
-      ObjectMapper mapper = new ObjectMapper();
-      mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
-      mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
-      JsonNode capabilitiesNode = mapper.readValue(capabilitiesData, JsonNode.class);
-      log.info("capabilitiesNode:" + capabilitiesNode.toString());
-
-      JsonNode vnfs = capabilitiesNode.findValue(VNF);
-      List<String> vnfsList = new ArrayList<>();
-      if (vnfs != null) {
-        for (int i = 0; i < vnfs.size(); i++) {
-          String vnf = vnfs.get(i).asText();
-          vnfsList.add(vnf);
-        }
-      }
+    Capabilities capabilities = new Capabilities();
+    if (capabilitiesData == null) {
+      return capabilities;
+    }
 
-      JsonNode vfModules = capabilitiesNode.findValue(VF_MODULE);
-      List<String> vfModulesList = new ArrayList<>();
-      if (vfModules != null) {
-        for (int i = 0; i < vfModules.size(); i++) {
-          String vfModule = vfModules.get(i).asText();
-          vfModulesList.add(vfModule);
-        }
-      }
+    ObjectMapper mapper = new ObjectMapper();
+    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
+    mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
 
-      JsonNode vnfcs = capabilitiesNode.findValue(VNFC);
-      List<String> vnfcsList = new ArrayList<>();
-      if (vnfcs != null) {
-        for (int i = 0; i < vnfcs.size(); i++) {
-          String vnfc1 = vnfcs.get(i).asText();
-          vnfcsList.add(vnfc1);
-        }
-      }
+    JsonNode capabilitiesNode = mapper.readTree(capabilitiesData);
+    log.info("capabilitiesNode:" + capabilitiesNode.toString());
 
-      JsonNode vms = capabilitiesNode.findValue(VM);
-      List<String> vmList = new ArrayList<>();
-      if (vms != null) {
-        for (int i = 0; i < vms.size(); i++) {
-          String vm1 = vms.get(i).asText();
-          vmList.add(vm1);
-        }
-      }
+    capabilities.getVfModule().addAll(extractParameterList(mapper, capabilitiesNode, VF_MODULE));
+    capabilities.getVnfc().addAll(extractParameterList(mapper, capabilitiesNode, VNFC));
+    capabilities.getVnf().addAll(extractParameterList(mapper, capabilitiesNode, VNF));
+    capabilities.getVm().addAll(extractParameterList(mapper, capabilitiesNode, VM));
 
-      capabilities.getVnfc().addAll(vnfcsList);
-      capabilities.getVnf().addAll(vnfsList);
-      capabilities.getVfModule().addAll(vfModulesList);
-      capabilities.getVm().addAll(vmList);
+    log.info("Capabilities Output:" + capabilities.toString());
 
-      log.info("Capabilities Output:" + capabilities.toString());
-    }
     return capabilities;
   }
 
+  private <T> List<T> extractParameterList(ObjectMapper mapper, JsonNode root, String parameter) throws IOException {
+    JsonNode parameterNode = root.get(parameter);
+    if (parameterNode == null) {
+      return new ArrayList<>();
+    }
+    return mapper.readValue(parameterNode.toString(), new TypeReference<List<T>>() {});
+  }
+
   private Properties loadProperties() throws Exception {
     String directory = envVariables.getenv(SDNC_CONFIG_DIR_VAR);
     if (directory == null) {
diff --git a/appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/node/FlowControlNodeTest.java b/appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/node/FlowControlNodeTest.java
new file mode 100644 (file)
index 0000000..200a026
--- /dev/null
@@ -0,0 +1,73 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP : APPC
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Copyright (C) 2017 Amdocs
+ * =============================================================================
+ * 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.
+ *
+ * ECOMP is a trademark and service mark of AT&T Intellectual Property.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.appc.flow.controller.node;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.onap.appc.flow.controller.dbervices.FlowControlDBService;
+import org.onap.appc.flow.controller.interfaceData.Capabilities;
+import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
+
+public class FlowControlNodeTest {
+
+  private SvcLogicContext ctx;
+  private FlowControlDBService dbService;
+
+  @Before
+  public void setUp() {
+    ctx = mock(SvcLogicContext.class);
+    dbService = mock(FlowControlDBService.class);
+  }
+
+  @Test
+  public void should_handle_capabilities_full_config() throws Exception {
+
+    String jsonPayload = "{'vnf':['vnf-1', 'vnf-2'],'vf-module':['vf-module-1', 'vf-module-2'],'vnfc':['vnfc-1', 'vnfc-2'],'vm':['vm-1', 'vm-2']}";
+    when(dbService.getCapabilitiesData(ctx)).thenReturn(jsonPayload.replaceAll("'","\""));
+
+    FlowControlNode flowControlNode = new FlowControlNode(null, dbService);
+    Capabilities capabilitiesData = flowControlNode.getCapabilitiesData(ctx);
+
+    Assert.assertEquals("Capabilities [vnf=[vnf-1, vnf-2], vfModule=[vf-module-1, vf-module-2], vm=[vm-1, vm-2], vnfc=[vnfc-1, vnfc-2]]", capabilitiesData.toString());
+  }
+
+  @Test
+  public void should_handle_capabilities_config_with_missing_params() throws Exception {
+
+    // vm is empty, vnfc is absent
+    String jsonPayload = "{'vnf':['vnf-1', 'vnf-2'],'vf-module':['vf-module-1'],'vm':[]}";
+    when(dbService.getCapabilitiesData(ctx)).thenReturn(jsonPayload.replaceAll("'","\""));
+
+    FlowControlNode flowControlNode = new FlowControlNode(null, dbService);
+    Capabilities capabilitiesData = flowControlNode.getCapabilitiesData(ctx);
+
+    Assert.assertEquals("Capabilities [vnf=[vnf-1, vnf-2], vfModule=[vf-module-1], vm=[], vnfc=[]]", capabilitiesData.toString());
+  }
+
+}