Apply defect and Fortify fixes to config bundle code
[appc.git] / appc-config / appc-flow-controller / provider / src / test / java / org / onap / appc / flow / controller / node / FlowControlNodeTest.java
index d171f5f..64914bb 100644 (file)
@@ -2,10 +2,12 @@
  * ============LICENSE_START=======================================================
  * ONAP : APPC
  * ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Copyright (C) 2017 Amdocs
  * =============================================================================
+ * Modifications Copyright (C) 2019 Ericsson
+ * =============================================================================
  * 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
  * 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.junit.Assert.assertEquals;
+import static org.mockito.Matchers.anyObject;
+import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
-import com.fasterxml.jackson.databind.ObjectMapper;
-import java.util.ArrayList;
 import java.util.HashMap;
-import java.util.List;
 import java.util.Map;
-import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
+import org.mockito.Mockito;
+import org.onap.appc.flow.controller.data.ResponseAction;
 import org.onap.appc.flow.controller.dbervices.FlowControlDBService;
-import org.onap.appc.flow.controller.interfaceData.Capabilities;
-import org.onap.appc.flow.controller.interfaceData.DependencyInfo;
-import org.onap.appc.flow.controller.interfaceData.Vnfcs;
 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
+import org.onap.ccsdk.sli.core.sli.SvcLogicException;
 
 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());
-  }
-
-  @Test
-  public void should_handle_dependency_config() throws Exception {
-
-    Vnfcs vnfcs = new Vnfcs();
-    vnfcs.setVnfcType("some-type");
-    vnfcs.setResilience("some-resilence");
-    vnfcs.setMandatory("some-mandatory");
-    Map<String, List<Vnfcs>> input = new HashMap<>();
-    List<Vnfcs> list = new ArrayList<>();
-    list.add(vnfcs);
-    list.add(vnfcs);
-    input.put("vnfcs", list);
-
-    String jsonPayload = new ObjectMapper().writeValueAsString(input);
-
-    when(dbService.getDependencyInfo(ctx)).thenReturn(jsonPayload);
-
-    FlowControlNode flowControlNode = new FlowControlNode(null, dbService);
-    DependencyInfo dependencyInfo = flowControlNode.getDependencyInfo(ctx);
-
-    Assert.assertEquals("DependencyInfo [vnfcs=[Vnfcs [vnfcType=some-type, mandatory=some-mandatory, resilience=some-resilence, parents=[]], Vnfcs [vnfcType=some-type, mandatory=some-mandatory, resilience=some-resilence, parents=[]]]]", dependencyInfo.toString());
-  }
+    private FlowControlDBService dbService;
+    private SvcLogicContext ctx;
+    private FlowControlNode flowControlNode;
+    private FlowSequenceGenerator flowSequenceGenerator;
+    private Map<String, String> inParams;
+
+    @Before
+    public void setUp() throws Exception {
+        ctx = new SvcLogicContext();
+        ctx.setAttribute("response.status", "success");
+        dbService = mock(FlowControlDBService.class);
+        flowSequenceGenerator = mock(FlowSequenceGenerator.class);
+        flowControlNode = new FlowControlNode(dbService, flowSequenceGenerator);
+        inParams = new HashMap<>();
+        inParams.put("responsePrefix", "response");
+    }
+
+    @Test
+    public void testProcessFlow() throws Exception {
+        String transactionJson = "{\"transaction-id\": \"1\","
+                + "  \"action\": \"HealthCheck\", \"action-level\": \"vnf\","
+                + "  \"executionModule\": \"APPC\", \"executionRPC\": \"healthcheck\", \"executionType\": \"node\","
+                + "\"precheck\":{\"precheck-operator\":\"any\",\"precheck-options\": ["
+                + "{\"pre-transaction-id\":\"1\",\"param-name\":\"executionType\",\"param-value\":\"node\"}]} }";
+        when(flowSequenceGenerator.getFlowSequence(eq(inParams), eq(ctx), anyObject()))
+        .thenReturn("{\"transactions\" :[" + transactionJson + "] }");
+        flowControlNode.processFlow(inParams, ctx);
+        assertEquals("response.", ctx.getAttribute("response-prefix"));
+    }
+
+    @Test
+    public void testProcessFlowWithoutPrecheck() throws Exception {
+        String transactionJson = "{\"transaction-id\": \"1\","
+                + "  \"action\": \"HealthCheck\", \"action-level\": \"vnf\","
+                + "  \"executionModule\": \"APPC\", \"executionRPC\": \"healthcheck\", \"executionType\": \"node\","
+                + "\"precheck\":{\"precheck-operator\":\"any\",\"precheck-options\": ["
+                + "{\"pre-transaction-id\":\"1\",\"param-name\":\"executionType\",\"param-value\":\"node1\"}]} }";
+        when(flowSequenceGenerator.getFlowSequence(eq(inParams), eq(ctx), anyObject()))
+        .thenReturn("{\"transactions\" :[" + transactionJson + "] }");
+        flowControlNode.processFlow(inParams, ctx);
+        assertEquals("response.", ctx.getAttribute("response-prefix"));
+    }
+
+    @Test(expected = SvcLogicException.class)
+    public void testProcessFlowWithFailure() throws Exception {
+        when(flowSequenceGenerator.getFlowSequence(eq(inParams), eq(ctx), anyObject()))
+        .thenReturn("{\"transactions\" :[] }");
+        ctx.setAttribute("response.status", "fail");
+        flowControlNode.processFlow(inParams, ctx);
+    }
+
+    @Test(expected = SvcLogicException.class)
+    public void testProcessFlowWithNoExecutionType() throws Exception {
+        String transactionJson = "{\"transaction-id\": \"1\","
+                + "  \"action\": \"HealthCheck\", \"action-level\": \"vnf\","
+                + "  \"executionModule\": \"APPC\", \"executionRPC\": \"healthcheck\", \"executionType\": \"other\","
+                + "\"precheck\":{\"precheck-operator\":\"any\",\"precheck-options\": ["
+                + "{\"pre-transaction-id\":\"1\",\"param-name\":\"executionType\",\"param-value\":\"other\"}]} }";
+        when(flowSequenceGenerator.getFlowSequence(eq(inParams), eq(ctx), anyObject()))
+        .thenReturn("{\"transactions\" :[" + transactionJson + "] }");
+        ctx.setAttribute("response.status", "fail");
+        flowControlNode.processFlow(inParams, ctx);
+    }
+
+    @Test
+    public void testProcessFlowIntermediateMessage() throws Exception {
+        FlowControlNode mockNode = Mockito.spy(flowControlNode);
+        String transactionJson = "{\"transaction-id\": \"1\","
+                + "  \"action\": \"HealthCheck\", \"action-level\": \"vnf\","
+                + "  \"executionModule\": \"APPC\", \"executionRPC\": \"healthcheck\", \"executionType\": \"node\","
+                + "\"precheck\":{\"precheck-operator\":\"any\",\"precheck-options\": ["
+                + "{\"pre-transaction-id\":\"1\",\"param-name\":\"executionType\",\"param-value\":\"node\"}]} }";
+        when(flowSequenceGenerator.getFlowSequence(eq(inParams), eq(ctx), anyObject()))
+            .thenReturn("{\"transactions\" :[" + transactionJson + "] }");
+        ResponseAction responseAction = Mockito.mock(ResponseAction.class);
+        when(responseAction.isIntermediateMessage()).thenReturn(true);
+        when(responseAction.getRetry()).thenReturn("1");
+        when(responseAction.getJump()).thenReturn("4");
+        Mockito.doReturn(responseAction).when(mockNode).handleResponse(anyObject(), anyObject());
+        mockNode.processFlow(inParams, ctx);
+        Mockito.verify(mockNode).sendIntermediateMessage();
+    }
+
+    @Test
+    public void testProcessFlowIntermediateMessageIsIgnore() throws Exception {
+        FlowControlNode mockNode = Mockito.spy(flowControlNode);
+        String transactionJson = "{\"transaction-id\": \"1\","
+                + "  \"action\": \"HealthCheck\", \"action-level\": \"vnf\","
+                + "  \"executionModule\": \"APPC\", \"executionRPC\": \"healthcheck\", \"executionType\": \"node\","
+                + "\"precheck\":{\"precheck-operator\":\"any\",\"precheck-options\": ["
+                + "{\"pre-transaction-id\":\"1\",\"param-name\":\"executionType\",\"param-value\":\"node\"}]} }";
+        when(flowSequenceGenerator.getFlowSequence(eq(inParams), eq(ctx), anyObject()))
+            .thenReturn("{\"transactions\" :[" + transactionJson + "] }");
+        ResponseAction responseAction = Mockito.mock(ResponseAction.class);
+        when(responseAction.isIgnore()).thenReturn(true);
+        Mockito.doReturn(responseAction).when(mockNode).handleResponse(anyObject(), anyObject());
+        mockNode.processFlow(inParams, ctx);
+        assertEquals("response.", ctx.getAttribute("response-prefix"));
+    }
+
+    @Test
+    public void testProcessFlowIntermediateMessageIsStop() throws Exception {
+        FlowControlNode mockNode = Mockito.spy(flowControlNode);
+        String transactionJson = "{\"transaction-id\": \"1\","
+                + "  \"action\": \"HealthCheck\", \"action-level\": \"vnf\","
+                + "  \"executionModule\": \"APPC\", \"executionRPC\": \"healthcheck\", \"executionType\": \"node\","
+                + "\"precheck\":{\"precheck-operator\":\"any\",\"precheck-options\": ["
+                + "{\"pre-transaction-id\":\"1\",\"param-name\":\"executionType\",\"param-value\":\"node\"}]} }";
+        when(flowSequenceGenerator.getFlowSequence(eq(inParams), eq(ctx), anyObject()))
+            .thenReturn("{\"transactions\" :[" + transactionJson + "] }");
+        ResponseAction responseAction = Mockito.mock(ResponseAction.class);
+        when(responseAction.isStop()).thenReturn(true);
+        Mockito.doReturn(responseAction).when(mockNode).handleResponse(anyObject(), anyObject());
+        mockNode.processFlow(inParams, ctx);
+        assertEquals("response.", ctx.getAttribute("response-prefix"));
+    }
+
+    @Test
+    public void testPreProcessorNullTransactionPrecheckOptions() throws Exception {
+        FlowControlNode mockNode = Mockito.spy(flowControlNode);
+        String transactionJson = "{\"transaction-id\": \"1\","
+                + "  \"action\": \"HealthCheck\", \"action-level\": \"vnf\","
+                + "  \"executionModule\": \"APPC\", \"executionRPC\": \"healthcheck\", \"executionType\": \"node\","
+                + "\"precheck\":{\"precheck-operator\":\"any\",\"precheck-options\": []} }";
+        when(flowSequenceGenerator.getFlowSequence(eq(inParams), eq(ctx), anyObject()))
+            .thenReturn("{\"transactions\" :[" + transactionJson + "] }");
+        mockNode.processFlow(inParams, ctx);
+        assertEquals("response.", ctx.getAttribute("response-prefix"));
+    }
 }