2 * ============LICENSE_START=======================================================
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
15 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.flow.controller.node;
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;
34 import java.util.HashMap;
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;
45 public class FlowControlNodeTest {
47 private FlowControlDBService dbService;
48 private SvcLogicContext ctx;
49 private FlowControlNode flowControlNode;
50 private FlowSequenceGenerator flowSequenceGenerator;
51 private Map<String, String> inParams;
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");
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"));
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"));
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);
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);
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();
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"));
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"));
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"));