d89059ddf05bd23e9455da3159459746441f58d9
[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.data.Transaction;
41 import org.onap.appc.flow.controller.dbervices.FlowControlDBService;
42 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
43 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
44
45 public class FlowControlNodeTest {
46
47     private FlowControlDBService dbService;
48     private SvcLogicContext ctx;
49     private FlowControlNode flowControlNode;
50     private FlowSequenceGenerator flowSequenceGenerator;
51     private Map<String, String> inParams;
52
53     @Before
54     public void setUp() throws Exception {
55         ctx = new SvcLogicContext();
56         ctx.setAttribute("response.status", "success");
57         dbService = mock(FlowControlDBService.class);
58         flowSequenceGenerator = mock(FlowSequenceGenerator.class);
59         flowControlNode = new FlowControlNode(dbService, flowSequenceGenerator);
60         inParams = new HashMap<>();
61         inParams.put("responsePrefix", "response");
62     }
63
64     @Test
65     public void testProcessFlow() throws Exception {
66         String transactionJson = "{\"transaction-id\": \"1\","
67                 + "  \"action\": \"HealthCheck\", \"action-level\": \"vnf\","
68                 + "  \"executionModule\": \"APPC\", \"executionRPC\": \"healthcheck\", \"executionType\": \"node\","
69                 + "\"precheck\":{\"precheck-operator\":\"any\",\"precheck-options\": ["
70                 + "{\"pre-transaction-id\":\"1\",\"param-name\":\"executionType\",\"param-value\":\"node\"}]} }";
71         when(flowSequenceGenerator.getFlowSequence(eq(inParams), eq(ctx), anyObject()))
72         .thenReturn("{\"transactions\" :[" + transactionJson + "] }");
73         flowControlNode.processFlow(inParams, ctx);
74         assertEquals("response.", ctx.getAttribute("response-prefix"));
75     }
76
77     @Test
78     public void testProcessFlowWithoutPrecheck() throws Exception {
79         String transactionJson = "{\"transaction-id\": \"1\","
80                 + "  \"action\": \"HealthCheck\", \"action-level\": \"vnf\","
81                 + "  \"executionModule\": \"APPC\", \"executionRPC\": \"healthcheck\", \"executionType\": \"node\","
82                 + "\"precheck\":{\"precheck-operator\":\"any\",\"precheck-options\": ["
83                 + "{\"pre-transaction-id\":\"1\",\"param-name\":\"executionType\",\"param-value\":\"node1\"}]} }";
84         when(flowSequenceGenerator.getFlowSequence(eq(inParams), eq(ctx), anyObject()))
85         .thenReturn("{\"transactions\" :[" + transactionJson + "] }");
86         flowControlNode.processFlow(inParams, ctx);
87         assertEquals("response.", ctx.getAttribute("response-prefix"));
88     }
89
90     @Test(expected = SvcLogicException.class)
91     public void testProcessFlowWithFailure() throws Exception {
92         when(flowSequenceGenerator.getFlowSequence(eq(inParams), eq(ctx), anyObject()))
93         .thenReturn("{\"transactions\" :[] }");
94         ctx.setAttribute("response.status", "fail");
95         flowControlNode.processFlow(inParams, ctx);
96     }
97
98     @Test(expected = SvcLogicException.class)
99     public void testProcessFlowWithNoExecutionType() throws Exception {
100         String transactionJson = "{\"transaction-id\": \"1\","
101                 + "  \"action\": \"HealthCheck\", \"action-level\": \"vnf\","
102                 + "  \"executionModule\": \"APPC\", \"executionRPC\": \"healthcheck\", \"executionType\": \"other\","
103                 + "\"precheck\":{\"precheck-operator\":\"any\",\"precheck-options\": ["
104                 + "{\"pre-transaction-id\":\"1\",\"param-name\":\"executionType\",\"param-value\":\"other\"}]} }";
105         when(flowSequenceGenerator.getFlowSequence(eq(inParams), eq(ctx), anyObject()))
106         .thenReturn("{\"transactions\" :[" + transactionJson + "] }");
107         ctx.setAttribute("response.status", "fail");
108         flowControlNode.processFlow(inParams, ctx);
109     }
110
111     @Test
112     public void testProcessFlowIntermediateMessage() throws Exception {
113         FlowControlNode mockNode = Mockito.spy(flowControlNode);
114         String transactionJson = "{\"transaction-id\": \"1\","
115                 + "  \"action\": \"HealthCheck\", \"action-level\": \"vnf\","
116                 + "  \"executionModule\": \"APPC\", \"executionRPC\": \"healthcheck\", \"executionType\": \"node\","
117                 + "\"precheck\":{\"precheck-operator\":\"any\",\"precheck-options\": ["
118                 + "{\"pre-transaction-id\":\"1\",\"param-name\":\"executionType\",\"param-value\":\"node\"}]} }";
119         when(flowSequenceGenerator.getFlowSequence(eq(inParams), eq(ctx), anyObject()))
120             .thenReturn("{\"transactions\" :[" + transactionJson + "] }");
121         ResponseAction responseAction = Mockito.mock(ResponseAction.class);
122         when(responseAction.isIntermediateMessage()).thenReturn(true);
123         when(responseAction.getRetry()).thenReturn("1");
124         when(responseAction.getJump()).thenReturn("4");
125         Mockito.doReturn(responseAction).when(mockNode).handleResponse(anyObject(), anyObject());
126         mockNode.processFlow(inParams, ctx);
127         Mockito.verify(mockNode).sendIntermediateMessage();
128     }
129
130     @Test
131     public void testProcessFlowIntermediateMessageIsIgnore() throws Exception {
132         FlowControlNode mockNode = Mockito.spy(flowControlNode);
133         String transactionJson = "{\"transaction-id\": \"1\","
134                 + "  \"action\": \"HealthCheck\", \"action-level\": \"vnf\","
135                 + "  \"executionModule\": \"APPC\", \"executionRPC\": \"healthcheck\", \"executionType\": \"node\","
136                 + "\"precheck\":{\"precheck-operator\":\"any\",\"precheck-options\": ["
137                 + "{\"pre-transaction-id\":\"1\",\"param-name\":\"executionType\",\"param-value\":\"node\"}]} }";
138         when(flowSequenceGenerator.getFlowSequence(eq(inParams), eq(ctx), anyObject()))
139             .thenReturn("{\"transactions\" :[" + transactionJson + "] }");
140         ResponseAction responseAction = Mockito.mock(ResponseAction.class);
141         when(responseAction.isIgnore()).thenReturn(true);
142         Mockito.doReturn(responseAction).when(mockNode).handleResponse(anyObject(), anyObject());
143         mockNode.processFlow(inParams, ctx);
144         assertEquals("response.", ctx.getAttribute("response-prefix"));
145     }
146
147     @Test
148     public void testProcessFlowIntermediateMessageIsStop() throws Exception {
149         FlowControlNode mockNode = Mockito.spy(flowControlNode);
150         String transactionJson = "{\"transaction-id\": \"1\","
151                 + "  \"action\": \"HealthCheck\", \"action-level\": \"vnf\","
152                 + "  \"executionModule\": \"APPC\", \"executionRPC\": \"healthcheck\", \"executionType\": \"node\","
153                 + "\"precheck\":{\"precheck-operator\":\"any\",\"precheck-options\": ["
154                 + "{\"pre-transaction-id\":\"1\",\"param-name\":\"executionType\",\"param-value\":\"node\"}]} }";
155         when(flowSequenceGenerator.getFlowSequence(eq(inParams), eq(ctx), anyObject()))
156             .thenReturn("{\"transactions\" :[" + transactionJson + "] }");
157         ResponseAction responseAction = Mockito.mock(ResponseAction.class);
158         when(responseAction.isStop()).thenReturn(true);
159         Mockito.doReturn(responseAction).when(mockNode).handleResponse(anyObject(), anyObject());
160         mockNode.processFlow(inParams, ctx);
161         assertEquals("response.", ctx.getAttribute("response-prefix"));
162     }
163
164     @Test
165     public void testPreProcessorNullTransactionPrecheckOptions() throws Exception {
166         FlowControlNode mockNode = Mockito.spy(flowControlNode);
167         String transactionJson = "{\"transaction-id\": \"1\","
168                 + "  \"action\": \"HealthCheck\", \"action-level\": \"vnf\","
169                 + "  \"executionModule\": \"APPC\", \"executionRPC\": \"healthcheck\", \"executionType\": \"node\","
170                 + "\"precheck\":{\"precheck-operator\":\"any\",\"precheck-options\": []} }";
171         when(flowSequenceGenerator.getFlowSequence(eq(inParams), eq(ctx), anyObject()))
172             .thenReturn("{\"transactions\" :[" + transactionJson + "] }");
173         mockNode.processFlow(inParams, ctx);
174         assertEquals("response.", ctx.getAttribute("response-prefix"));
175     }
176 }