029562a6e9d7259ae303ccd3a1771b5556624894
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / infrastructure / workflow / tasks / WorkflowActionBBTasksTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.bpmn.infrastructure.workflow.tasks;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.mockito.ArgumentMatchers.anyObject;
25 import static org.mockito.ArgumentMatchers.anyString;
26 import static org.mockito.ArgumentMatchers.isA;
27 import static org.mockito.Mockito.doNothing;
28 import static org.mockito.Mockito.doReturn;
29 import static org.mockito.Mockito.when;
30 import java.util.ArrayList;
31 import java.util.List;
32 import org.camunda.bpm.engine.delegate.BpmnError;
33 import org.camunda.bpm.engine.delegate.DelegateExecution;
34 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
35 import org.junit.Before;
36 import org.junit.Rule;
37 import org.junit.Test;
38 import org.junit.rules.ExpectedException;
39 import org.mockito.InjectMocks;
40 import org.mockito.Mock;
41 import org.mockito.Mockito;
42 import org.mockito.Spy;
43 import org.onap.so.bpmn.BaseTaskTest;
44 import org.onap.so.bpmn.core.WorkflowException;
45 import org.onap.so.bpmn.servicedecomposition.entities.BuildingBlock;
46 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
47 import org.onap.so.db.request.beans.InfraActiveRequests;
48 import org.springframework.core.env.Environment;
49
50 public class WorkflowActionBBTasksTest extends BaseTaskTest {
51
52     @Mock
53     protected WorkflowAction workflowAction;
54
55     @Mock
56     protected WorkflowActionBBFailure workflowActionBBFailure;
57
58     @InjectMocks
59     @Spy
60     protected WorkflowActionBBTasks workflowActionBBTasks;
61
62     @Mock
63     InfraActiveRequests reqMock;
64
65     private DelegateExecution execution;
66
67     @Mock
68     protected Environment environment;
69
70     @Rule
71     public ExpectedException thrown = ExpectedException.none();
72
73     @Before
74     public void before() throws Exception {
75         execution = new DelegateExecutionFake();
76         org.onap.aai.domain.yang.ServiceInstance servInstance = new org.onap.aai.domain.yang.ServiceInstance();
77         servInstance.setServiceInstanceId("TEST");
78         when(bbSetupUtils.getAAIServiceInstanceByName(anyString(), anyObject())).thenReturn(servInstance);
79         workflowAction.setBbInputSetupUtils(bbSetupUtils);
80         workflowAction.setBbInputSetup(bbInputSetup);
81     }
82
83     @Test
84     public void selectBBTest() throws Exception {
85         String gAction = "Delete-Network-Collection";
86         execution.setVariable("mso-request-id", "00f704ca-c5e5-4f95-a72c-6889db7b0688");
87         execution.setVariable("requestAction", gAction);
88         execution.setVariable("gCurrentSequence", 0);
89         execution.setVariable("homing", false);
90         execution.setVariable("calledHoming", false);
91         List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
92         ExecuteBuildingBlock ebb = new ExecuteBuildingBlock();
93         flowsToExecute.add(ebb);
94         execution.setVariable("flowsToExecute", flowsToExecute);
95         workflowActionBBTasks.selectBB(execution);
96         boolean success = (boolean) execution.getVariable("completed");
97         int currentSequence = (int) execution.getVariable("gCurrentSequence");
98         assertEquals(true, success);
99         assertEquals(1, currentSequence);
100     }
101
102     @Test
103     public void select2BBTest() throws Exception {
104         String gAction = "Delete-Network-Collection";
105         execution.setVariable("mso-request-id", "00f704ca-c5e5-4f95-a72c-6889db7b0688");
106         execution.setVariable("requestAction", gAction);
107         execution.setVariable("gCurrentSequence", 0);
108         execution.setVariable("homing", false);
109         execution.setVariable("calledHoming", false);
110         List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
111         ExecuteBuildingBlock ebb = new ExecuteBuildingBlock();
112         ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock();
113         flowsToExecute.add(ebb);
114         flowsToExecute.add(ebb2);
115         execution.setVariable("flowsToExecute", flowsToExecute);
116         workflowActionBBTasks.selectBB(execution);
117         boolean success = (boolean) execution.getVariable("completed");
118         int currentSequence = (int) execution.getVariable("gCurrentSequence");
119         assertEquals(false, success);
120         assertEquals(1, currentSequence);
121     }
122
123     @Test
124     public void updateRequestStatusToCompleteTest() throws Exception {
125         String reqId = "reqId123";
126         execution.setVariable("mso-request-id", reqId);
127         execution.setVariable("requestAction", "createInstance");
128         execution.setVariable("resourceName", "Service");
129         execution.setVariable("aLaCarte", true);
130         InfraActiveRequests req = new InfraActiveRequests();
131         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
132         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
133         workflowActionBBTasks.updateRequestStatusToComplete(execution);
134         assertEquals("ALaCarte-Service-createInstance request was executed correctly.",
135                 execution.getVariable("finalStatusMessage"));
136     }
137
138     @Test
139     public void rollbackExecutionPathTest() {
140         execution.setVariable("handlingCode", "Rollback");
141         execution.setVariable("isRollback", false);
142         List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
143         ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock();
144         BuildingBlock bb1 = new BuildingBlock();
145         bb1.setBpmnFlowName("AssignVfModuleBB");
146         ebb1.setBuildingBlock(bb1);
147         flowsToExecute.add(ebb1);
148         ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock();
149         BuildingBlock bb2 = new BuildingBlock();
150         bb2.setBpmnFlowName("CreateVfModuleBB");
151         ebb2.setBuildingBlock(bb2);
152         flowsToExecute.add(ebb2);
153         ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock();
154         BuildingBlock bb3 = new BuildingBlock();
155         bb3.setBpmnFlowName("ActivateVfModuleBB");
156         ebb3.setBuildingBlock(bb3);
157         flowsToExecute.add(ebb3);
158
159         execution.setVariable("flowsToExecute", flowsToExecute);
160         execution.setVariable("gCurrentSequence", 3);
161         doNothing().when(workflowActionBBFailure).updateRequestErrorStatusMessage(isA(DelegateExecution.class));
162
163         workflowActionBBTasks.rollbackExecutionPath(execution);
164         List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
165         assertEquals(ebbs.get(0).getBuildingBlock().getBpmnFlowName(), "DeactivateVfModuleBB");
166         assertEquals(ebbs.get(1).getBuildingBlock().getBpmnFlowName(), "DeleteVfModuleBB");
167         assertEquals(ebbs.get(2).getBuildingBlock().getBpmnFlowName(), "UnassignVfModuleBB");
168         assertEquals(0, execution.getVariable("gCurrentSequence"));
169     }
170
171     @Test
172     public void rollbackExecutionPathUnfinishedFlowTest() {
173         execution.setVariable("handlingCode", "Rollback");
174         execution.setVariable("isRollback", false);
175         List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
176         ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock();
177         BuildingBlock bb1 = new BuildingBlock();
178         bb1.setBpmnFlowName("AssignVfModuleBB");
179         ebb1.setBuildingBlock(bb1);
180         flowsToExecute.add(ebb1);
181         ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock();
182         BuildingBlock bb2 = new BuildingBlock();
183         bb2.setBpmnFlowName("CreateVfModuleBB");
184         ebb2.setBuildingBlock(bb2);
185         flowsToExecute.add(ebb2);
186         ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock();
187         BuildingBlock bb3 = new BuildingBlock();
188         bb3.setBpmnFlowName("ActivateVfModuleBB");
189         ebb3.setBuildingBlock(bb3);
190         flowsToExecute.add(ebb3);
191
192         execution.setVariable("flowsToExecute", flowsToExecute);
193         execution.setVariable("gCurrentSequence", 2);
194         doNothing().when(workflowActionBBFailure).updateRequestErrorStatusMessage(isA(DelegateExecution.class));
195
196         workflowActionBBTasks.rollbackExecutionPath(execution);
197         List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
198         assertEquals(ebbs.get(0).getBuildingBlock().getBpmnFlowName(), "DeleteVfModuleBB");
199         assertEquals(ebbs.get(1).getBuildingBlock().getBpmnFlowName(), "UnassignVfModuleBB");
200         assertEquals(0, execution.getVariable("gCurrentSequence"));
201         assertEquals(0, execution.getVariable("retryCount"));
202     }
203
204     @Test
205     public void rollbackExecutionTest() {
206         execution.setVariable("handlingCode", "Rollback");
207         execution.setVariable("isRollback", false);
208         List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
209         ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock();
210         BuildingBlock bb1 = new BuildingBlock();
211         bb1.setBpmnFlowName("AssignServiceInstanceBB");
212         ebb1.setBuildingBlock(bb1);
213         flowsToExecute.add(ebb1);
214         ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock();
215         BuildingBlock bb2 = new BuildingBlock();
216         bb2.setBpmnFlowName("CreateNetworkCollectionBB");
217         ebb2.setBuildingBlock(bb2);
218         flowsToExecute.add(ebb2);
219         ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock();
220         BuildingBlock bb3 = new BuildingBlock();
221         bb3.setBpmnFlowName("AssignNetworkBB");
222         ebb3.setBuildingBlock(bb3);
223         flowsToExecute.add(ebb3);
224         ExecuteBuildingBlock ebb4 = new ExecuteBuildingBlock();
225         BuildingBlock bb4 = new BuildingBlock();
226         bb4.setBpmnFlowName("CreateNetworkBB");
227         ebb4.setBuildingBlock(bb4);
228         flowsToExecute.add(ebb4);
229
230         execution.setVariable("flowsToExecute", flowsToExecute);
231         execution.setVariable("gCurrentSequence", 3);
232         doNothing().when(workflowActionBBFailure).updateRequestErrorStatusMessage(isA(DelegateExecution.class));
233
234         workflowActionBBTasks.rollbackExecutionPath(execution);
235         List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
236         assertEquals(ebbs.get(0).getBuildingBlock().getBpmnFlowName(), "UnassignNetworkBB");
237         assertEquals(ebbs.get(1).getBuildingBlock().getBpmnFlowName(), "DeleteNetworkCollectionBB");
238         assertEquals(ebbs.get(2).getBuildingBlock().getBpmnFlowName(), "UnassignServiceInstanceBB");
239         assertEquals(0, execution.getVariable("gCurrentSequence"));
240     }
241
242     @Test
243     public void rollbackExecutionRollbackToAssignedTest() {
244         execution.setVariable("isRollback", false);
245         execution.setVariable("handlingCode", "RollbackToAssigned");
246         List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
247         ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock();
248         BuildingBlock bb1 = new BuildingBlock();
249         bb1.setBpmnFlowName("AssignVfModuleBB");
250         ebb1.setBuildingBlock(bb1);
251         flowsToExecute.add(ebb1);
252         ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock();
253         BuildingBlock bb2 = new BuildingBlock();
254         bb2.setBpmnFlowName("CreateVfModuleBB");
255         ebb2.setBuildingBlock(bb2);
256         flowsToExecute.add(ebb2);
257         ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock();
258         BuildingBlock bb3 = new BuildingBlock();
259         bb3.setBpmnFlowName("ActivateVfModuleBB");
260         ebb3.setBuildingBlock(bb3);
261         flowsToExecute.add(ebb3);
262
263         execution.setVariable("flowsToExecute", flowsToExecute);
264         execution.setVariable("gCurrentSequence", 2);
265
266         workflowActionBBTasks.rollbackExecutionPath(execution);
267         List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
268         assertEquals("DeleteVfModuleBB", ebbs.get(0).getBuildingBlock().getBpmnFlowName());
269         assertEquals(0, execution.getVariable("gCurrentSequence"));
270         assertEquals(1, ebbs.size());
271     }
272
273     @Test
274     public void rollbackExecutionRollbackToCreatedTest() {
275         execution.setVariable("isRollback", false);
276         execution.setVariable("handlingCode", "RollbackToCreated");
277         List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
278         ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock();
279         BuildingBlock bb1 = new BuildingBlock();
280         bb1.setBpmnFlowName("AssignVfModuleBB");
281         ebb1.setBuildingBlock(bb1);
282         flowsToExecute.add(ebb1);
283         ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock();
284         BuildingBlock bb2 = new BuildingBlock();
285         bb2.setBpmnFlowName("CreateVfModuleBB");
286         ebb2.setBuildingBlock(bb2);
287         flowsToExecute.add(ebb2);
288         ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock();
289         BuildingBlock bb3 = new BuildingBlock();
290         bb3.setBpmnFlowName("ActivateVfModuleBB");
291         ebb3.setBuildingBlock(bb3);
292         flowsToExecute.add(ebb3);
293
294         execution.setVariable("flowsToExecute", flowsToExecute);
295         execution.setVariable("gCurrentSequence", 3);
296
297         workflowActionBBTasks.rollbackExecutionPath(execution);
298         List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
299         assertEquals("DeactivateVfModuleBB", ebbs.get(0).getBuildingBlock().getBpmnFlowName());
300         assertEquals(0, execution.getVariable("gCurrentSequence"));
301         assertEquals(1, ebbs.size());
302     }
303
304     @Test
305     public void checkRetryStatusTest() {
306         String reqId = "reqId123";
307         execution.setVariable("mso-request-id", reqId);
308         doNothing().when(workflowActionBBFailure).updateRequestErrorStatusMessage(isA(DelegateExecution.class));
309         doReturn("6").when(environment).getProperty("mso.rainyDay.maxRetries");
310         execution.setVariable("handlingCode", "Retry");
311         execution.setVariable("retryCount", 1);
312         execution.setVariable("gCurrentSequence", 1);
313         InfraActiveRequests req = new InfraActiveRequests();
314         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
315         workflowActionBBTasks.checkRetryStatus(execution);
316         assertEquals(0, execution.getVariable("gCurrentSequence"));
317     }
318
319     @Test
320     public void checkRetryStatusTestExceededMaxRetries() {
321         String reqId = "reqId123";
322         execution.setVariable("mso-request-id", reqId);
323         doNothing().when(workflowActionBBFailure).updateRequestErrorStatusMessage(isA(DelegateExecution.class));
324         doReturn("6").when(environment).getProperty("mso.rainyDay.maxRetries");
325         execution.setVariable("handlingCode", "Retry");
326         execution.setVariable("retryCount", 6);
327         execution.setVariable("gCurrentSequence", 1);
328         InfraActiveRequests req = new InfraActiveRequests();
329         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
330         try {
331             workflowActionBBTasks.checkRetryStatus(execution);
332         } catch (BpmnError e) {
333             WorkflowException exception = (WorkflowException) execution.getVariable("WorkflowException");
334             assertEquals("Exceeded maximum retries. Ending flow with status Abort", exception.getErrorMessage());
335         }
336     }
337
338     @Test
339     public void checkRetryStatusNoRetryTest() {
340         String reqId = "reqId123";
341         execution.setVariable("mso-request-id", reqId);
342         execution.setVariable("retryCount", 3);
343         execution.setVariable("handlingCode", "Success");
344         execution.setVariable("gCurrentSequence", 1);
345         InfraActiveRequests req = new InfraActiveRequests();
346         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
347         workflowActionBBTasks.checkRetryStatus(execution);
348         assertEquals(0, execution.getVariable("retryCount"));
349     }
350
351     @Test
352     public void updateInstanceId() {
353         String reqId = "req123";
354         String instanceId = "123123123";
355         execution.setVariable("mso-request-id", reqId);
356         execution.setVariable("resourceId", instanceId);
357         execution.setVariable("resourceType", WorkflowType.SERVICE);
358         doReturn(reqMock).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
359         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
360         workflowActionBBTasks.updateInstanceId(execution);
361         Mockito.verify(reqMock, Mockito.times(1)).setServiceInstanceId(instanceId);
362     }
363 }