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
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Modifications Copyright (C) 2019 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.flow.controller.node;
27
28 import static org.junit.Assert.assertEquals;
29 import static org.mockito.Matchers.anyObject;
30 import static org.mockito.Matchers.eq;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.when;
33
34 import java.util.HashMap;
35 import java.util.Map;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.Mockito;
39 import org.onap.appc.flow.controller.data.ResponseAction;
40 import org.onap.appc.flow.controller.dbervices.FlowControlDBService;
41 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
42 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
43
44 public class FlowControlNodeTest {
45
46     private FlowControlDBService dbService;
47     private SvcLogicContext ctx;
48     private FlowControlNode flowControlNode;
49     private FlowSequenceGenerator flowSequenceGenerator;
50     private Map<String, String> inParams;
51
52     @Before
53     public void setUp() throws Exception {
54         ctx = new SvcLogicContext();
55         ctx.setAttribute("response.status", "success");
56         dbService = mock(FlowControlDBService.class);
57         flowSequenceGenerator = mock(FlowSequenceGenerator.class);
58         flowControlNode = new FlowControlNode(dbService, flowSequenceGenerator);
59         inParams = new HashMap<>();
60         inParams.put("responsePrefix", "response");
61     }
62
63     @Test
64     public void testProcessFlow() throws Exception {
65         String transactionJson = "{\"transaction-id\": \"1\","
66                 + "  \"action\": \"HealthCheck\", \"action-level\": \"vnf\","
67                 + "  \"executionModule\": \"APPC\", \"executionRPC\": \"healthcheck\", \"executionType\": \"node\","
68                 + "\"precheck\":{\"precheck-operator\":\"any\",\"precheck-options\": ["
69                 + "{\"pre-transaction-id\":\"1\",\"param-name\":\"executionType\",\"param-value\":\"node\"}]} }";
70         when(flowSequenceGenerator.getFlowSequence(eq(inParams), eq(ctx), anyObject()))
71         .thenReturn("{\"transactions\" :[" + transactionJson + "] }");
72         flowControlNode.processFlow(inParams, ctx);
73         assertEquals("response.", ctx.getAttribute("response-prefix"));
74     }
75
76     @Test
77     public void testProcessFlowWithoutPrecheck() throws Exception {
78         String transactionJson = "{\"transaction-id\": \"1\","
79                 + "  \"action\": \"HealthCheck\", \"action-level\": \"vnf\","
80                 + "  \"executionModule\": \"APPC\", \"executionRPC\": \"healthcheck\", \"executionType\": \"node\","
81                 + "\"precheck\":{\"precheck-operator\":\"any\",\"precheck-options\": ["
82                 + "{\"pre-transaction-id\":\"1\",\"param-name\":\"executionType\",\"param-value\":\"node1\"}]} }";
83         when(flowSequenceGenerator.getFlowSequence(eq(inParams), eq(ctx), anyObject()))
84         .thenReturn("{\"transactions\" :[" + transactionJson + "] }");
85         flowControlNode.processFlow(inParams, ctx);
86         assertEquals("response.", ctx.getAttribute("response-prefix"));
87     }
88
89     @Test(expected = SvcLogicException.class)
90     public void testProcessFlowWithFailure() throws Exception {
91         when(flowSequenceGenerator.getFlowSequence(eq(inParams), eq(ctx), anyObject()))
92         .thenReturn("{\"transactions\" :[] }");
93         ctx.setAttribute("response.status", "fail");
94         flowControlNode.processFlow(inParams, ctx);
95     }
96
97     @Test(expected = SvcLogicException.class)
98     public void testProcessFlowWithNoExecutionType() throws Exception {
99         String transactionJson = "{\"transaction-id\": \"1\","
100                 + "  \"action\": \"HealthCheck\", \"action-level\": \"vnf\","
101                 + "  \"executionModule\": \"APPC\", \"executionRPC\": \"healthcheck\", \"executionType\": \"other\","
102                 + "\"precheck\":{\"precheck-operator\":\"any\",\"precheck-options\": ["
103                 + "{\"pre-transaction-id\":\"1\",\"param-name\":\"executionType\",\"param-value\":\"other\"}]} }";
104         when(flowSequenceGenerator.getFlowSequence(eq(inParams), eq(ctx), anyObject()))
105         .thenReturn("{\"transactions\" :[" + transactionJson + "] }");
106         ctx.setAttribute("response.status", "fail");
107         flowControlNode.processFlow(inParams, ctx);
108     }
109
110     @Test
111     public void testProcessFlowIntermediateMessage() throws Exception {
112         FlowControlNode mockNode = Mockito.spy(flowControlNode);
113         String transactionJson = "{\"transaction-id\": \"1\","
114                 + "  \"action\": \"HealthCheck\", \"action-level\": \"vnf\","
115                 + "  \"executionModule\": \"APPC\", \"executionRPC\": \"healthcheck\", \"executionType\": \"node\","
116                 + "\"precheck\":{\"precheck-operator\":\"any\",\"precheck-options\": ["
117                 + "{\"pre-transaction-id\":\"1\",\"param-name\":\"executionType\",\"param-value\":\"node\"}]} }";
118         when(flowSequenceGenerator.getFlowSequence(eq(inParams), eq(ctx), anyObject()))
119             .thenReturn("{\"transactions\" :[" + transactionJson + "] }");
120         ResponseAction responseAction = Mockito.mock(ResponseAction.class);
121         when(responseAction.isIntermediateMessage()).thenReturn(true);
122         when(responseAction.getRetry()).thenReturn("1");
123         when(responseAction.getJump()).thenReturn("4");
124         Mockito.doReturn(responseAction).when(mockNode).handleResponse(anyObject(), anyObject());
125         mockNode.processFlow(inParams, ctx);
126         Mockito.verify(mockNode).sendIntermediateMessage();
127     }
128
129     @Test
130     public void testProcessFlowIntermediateMessageIsIgnore() throws Exception {
131         FlowControlNode mockNode = Mockito.spy(flowControlNode);
132         String transactionJson = "{\"transaction-id\": \"1\","
133                 + "  \"action\": \"HealthCheck\", \"action-level\": \"vnf\","
134                 + "  \"executionModule\": \"APPC\", \"executionRPC\": \"healthcheck\", \"executionType\": \"node\","
135                 + "\"precheck\":{\"precheck-operator\":\"any\",\"precheck-options\": ["
136                 + "{\"pre-transaction-id\":\"1\",\"param-name\":\"executionType\",\"param-value\":\"node\"}]} }";
137         when(flowSequenceGenerator.getFlowSequence(eq(inParams), eq(ctx), anyObject()))
138             .thenReturn("{\"transactions\" :[" + transactionJson + "] }");
139         ResponseAction responseAction = Mockito.mock(ResponseAction.class);
140         when(responseAction.isIgnore()).thenReturn(true);
141         Mockito.doReturn(responseAction).when(mockNode).handleResponse(anyObject(), anyObject());
142         mockNode.processFlow(inParams, ctx);
143         assertEquals("response.", ctx.getAttribute("response-prefix"));
144     }
145
146     @Test
147     public void testProcessFlowIntermediateMessageIsStop() throws Exception {
148         FlowControlNode mockNode = Mockito.spy(flowControlNode);
149         String transactionJson = "{\"transaction-id\": \"1\","
150                 + "  \"action\": \"HealthCheck\", \"action-level\": \"vnf\","
151                 + "  \"executionModule\": \"APPC\", \"executionRPC\": \"healthcheck\", \"executionType\": \"node\","
152                 + "\"precheck\":{\"precheck-operator\":\"any\",\"precheck-options\": ["
153                 + "{\"pre-transaction-id\":\"1\",\"param-name\":\"executionType\",\"param-value\":\"node\"}]} }";
154         when(flowSequenceGenerator.getFlowSequence(eq(inParams), eq(ctx), anyObject()))
155             .thenReturn("{\"transactions\" :[" + transactionJson + "] }");
156         ResponseAction responseAction = Mockito.mock(ResponseAction.class);
157         when(responseAction.isStop()).thenReturn(true);
158         Mockito.doReturn(responseAction).when(mockNode).handleResponse(anyObject(), anyObject());
159         mockNode.processFlow(inParams, ctx);
160         assertEquals("response.", ctx.getAttribute("response-prefix"));
161     }
162
163     @Test
164     public void testPreProcessorNullTransactionPrecheckOptions() throws Exception {
165         FlowControlNode mockNode = Mockito.spy(flowControlNode);
166         String transactionJson = "{\"transaction-id\": \"1\","
167                 + "  \"action\": \"HealthCheck\", \"action-level\": \"vnf\","
168                 + "  \"executionModule\": \"APPC\", \"executionRPC\": \"healthcheck\", \"executionType\": \"node\","
169                 + "\"precheck\":{\"precheck-operator\":\"any\",\"precheck-options\": []} }";
170         when(flowSequenceGenerator.getFlowSequence(eq(inParams), eq(ctx), anyObject()))
171             .thenReturn("{\"transactions\" :[" + transactionJson + "] }");
172         mockNode.processFlow(inParams, ctx);
173         assertEquals("response.", ctx.getAttribute("response-prefix"));
174     }
175 }