Conditional Macro Action Selection
[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.aai.domain.yang.GenericVnf;
44 import org.onap.so.bpmn.BaseTaskTest;
45 import org.onap.so.bpmn.core.WorkflowException;
46 import org.onap.so.bpmn.servicedecomposition.entities.BuildingBlock;
47 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
48 import org.onap.so.bpmn.servicedecomposition.entities.WorkflowResourceIds;
49 import org.onap.so.db.catalog.beans.VnfResourceCustomization;
50 import org.onap.so.db.request.beans.InfraActiveRequests;
51 import org.springframework.core.env.Environment;
52
53 public class WorkflowActionBBTasksTest extends BaseTaskTest {
54
55     @Mock
56     protected WorkflowAction workflowAction;
57
58     @Mock
59     protected WorkflowActionBBFailure workflowActionBBFailure;
60
61     @InjectMocks
62     @Spy
63     protected WorkflowActionBBTasks workflowActionBBTasks;
64
65     @Mock
66     InfraActiveRequests reqMock;
67
68     private DelegateExecution execution;
69
70     @Mock
71     protected Environment environment;
72
73     @Rule
74     public ExpectedException thrown = ExpectedException.none();
75
76     @Before
77     public void before() throws Exception {
78         execution = new DelegateExecutionFake();
79         org.onap.aai.domain.yang.ServiceInstance servInstance = new org.onap.aai.domain.yang.ServiceInstance();
80         servInstance.setServiceInstanceId("TEST");
81         when(bbSetupUtils.getAAIServiceInstanceByName(anyString(), anyObject())).thenReturn(servInstance);
82         workflowAction.setBbInputSetupUtils(bbSetupUtils);
83         workflowAction.setBbInputSetup(bbInputSetup);
84     }
85
86     @Test
87     public void selectBBTest() throws Exception {
88         String gAction = "Delete-Network-Collection";
89         execution.setVariable("mso-request-id", "00f704ca-c5e5-4f95-a72c-6889db7b0688");
90         execution.setVariable("requestAction", gAction);
91         execution.setVariable("gCurrentSequence", 0);
92         execution.setVariable("homing", false);
93         execution.setVariable("calledHoming", false);
94         List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
95         ExecuteBuildingBlock ebb = new ExecuteBuildingBlock();
96
97         String vnfCustomizationUUID = "1234567";
98         String serviceInstanceId = "1234567";
99         BuildingBlock buildingBlock = new BuildingBlock();
100         buildingBlock.setBpmnFlowName("ConfigAssignVnfBB");
101         buildingBlock.setKey(vnfCustomizationUUID);
102         ebb.setBuildingBlock(buildingBlock);
103         WorkflowResourceIds workflowResourceIds = new WorkflowResourceIds();
104         workflowResourceIds.setServiceInstanceId(serviceInstanceId);
105         ebb.setWorkflowResourceIds(workflowResourceIds);
106         flowsToExecute.add(ebb);
107
108         List<VnfResourceCustomization> vnfResourceCustomizations = new ArrayList();
109         VnfResourceCustomization vrc = new VnfResourceCustomization();
110         vrc.setSkipPostInstConf(false);
111         vrc.setModelCustomizationUUID(vnfCustomizationUUID);
112         vnfResourceCustomizations.add(vrc);
113         GenericVnf genericVnf = new GenericVnf();
114         genericVnf.setModelCustomizationId(vnfCustomizationUUID);
115         doReturn(vnfResourceCustomizations).when(catalogDbClient)
116                 .getVnfResourceCustomizationByModelUuid(serviceInstanceId);
117         doReturn(vrc).when(catalogDbClient).findVnfResourceCustomizationInList(vnfCustomizationUUID,
118                 vnfResourceCustomizations);
119
120         execution.setVariable("flowsToExecute", flowsToExecute);
121         workflowActionBBTasks.selectBB(execution);
122         boolean success = (boolean) execution.getVariable("completed");
123         int currentSequence = (int) execution.getVariable("gCurrentSequence");
124         assertEquals(true, success);
125         assertEquals(1, currentSequence);
126     }
127
128     @Test
129     public void select2BBTest() throws Exception {
130         String gAction = "Delete-Network-Collection";
131         execution.setVariable("mso-request-id", "00f704ca-c5e5-4f95-a72c-6889db7b0688");
132         execution.setVariable("requestAction", gAction);
133         execution.setVariable("gCurrentSequence", 0);
134         execution.setVariable("homing", false);
135         execution.setVariable("calledHoming", false);
136         List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
137         ExecuteBuildingBlock ebb = new ExecuteBuildingBlock();
138         ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock();
139
140         String vnfCustomizationUUID = "1234567";
141         String serviceInstanceId = "1234567";
142         BuildingBlock buildingBlock = new BuildingBlock();
143         buildingBlock.setBpmnFlowName("ConfigDeployVnfBB");
144         buildingBlock.setKey(vnfCustomizationUUID);
145         ebb.setBuildingBlock(buildingBlock);
146         WorkflowResourceIds workflowResourceIds = new WorkflowResourceIds();
147         workflowResourceIds.setServiceInstanceId(serviceInstanceId);
148         ebb.setWorkflowResourceIds(workflowResourceIds);
149         flowsToExecute.add(ebb);
150
151         List<VnfResourceCustomization> vnfResourceCustomizations = new ArrayList();
152         VnfResourceCustomization vrc = new VnfResourceCustomization();
153         vrc.setSkipPostInstConf(false);
154         vrc.setModelCustomizationUUID(vnfCustomizationUUID);
155         vnfResourceCustomizations.add(vrc);
156         GenericVnf genericVnf = new GenericVnf();
157         genericVnf.setModelCustomizationId(vnfCustomizationUUID);
158         doReturn(vnfResourceCustomizations).when(catalogDbClient)
159                 .getVnfResourceCustomizationByModelUuid(serviceInstanceId);
160         doReturn(vrc).when(catalogDbClient).findVnfResourceCustomizationInList(vnfCustomizationUUID,
161                 vnfResourceCustomizations);
162
163         flowsToExecute.add(ebb2);
164         execution.setVariable("flowsToExecute", flowsToExecute);
165         workflowActionBBTasks.selectBB(execution);
166         boolean success = (boolean) execution.getVariable("completed");
167         int currentSequence = (int) execution.getVariable("gCurrentSequence");
168         assertEquals(false, success);
169         assertEquals(1, currentSequence);
170     }
171
172     @Test
173     public void updateRequestStatusToCompleteTest() throws Exception {
174         String reqId = "reqId123";
175         execution.setVariable("mso-request-id", reqId);
176         execution.setVariable("requestAction", "createInstance");
177         execution.setVariable("resourceName", "Service");
178         execution.setVariable("aLaCarte", true);
179         InfraActiveRequests req = new InfraActiveRequests();
180         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
181         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
182         workflowActionBBTasks.updateRequestStatusToComplete(execution);
183         assertEquals("ALaCarte-Service-createInstance request was executed correctly.",
184                 execution.getVariable("finalStatusMessage"));
185     }
186
187     @Test
188     public void rollbackExecutionPathTest() {
189         execution.setVariable("handlingCode", "Rollback");
190         execution.setVariable("isRollback", false);
191         List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
192         ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock();
193         BuildingBlock bb1 = new BuildingBlock();
194         bb1.setBpmnFlowName("AssignVfModuleBB");
195         ebb1.setBuildingBlock(bb1);
196         flowsToExecute.add(ebb1);
197         ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock();
198         BuildingBlock bb2 = new BuildingBlock();
199         bb2.setBpmnFlowName("CreateVfModuleBB");
200         ebb2.setBuildingBlock(bb2);
201         flowsToExecute.add(ebb2);
202         ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock();
203         BuildingBlock bb3 = new BuildingBlock();
204         bb3.setBpmnFlowName("ActivateVfModuleBB");
205         ebb3.setBuildingBlock(bb3);
206         flowsToExecute.add(ebb3);
207
208         execution.setVariable("flowsToExecute", flowsToExecute);
209         execution.setVariable("gCurrentSequence", 3);
210         doNothing().when(workflowActionBBFailure).updateRequestErrorStatusMessage(isA(DelegateExecution.class));
211
212         workflowActionBBTasks.rollbackExecutionPath(execution);
213         List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
214         assertEquals(ebbs.get(0).getBuildingBlock().getBpmnFlowName(), "DeactivateVfModuleBB");
215         assertEquals(ebbs.get(1).getBuildingBlock().getBpmnFlowName(), "DeleteVfModuleBB");
216         assertEquals(ebbs.get(2).getBuildingBlock().getBpmnFlowName(), "UnassignVfModuleBB");
217         assertEquals(0, execution.getVariable("gCurrentSequence"));
218     }
219
220     @Test
221     public void rollbackExecutionPathUnfinishedFlowTest() {
222         execution.setVariable("handlingCode", "Rollback");
223         execution.setVariable("isRollback", false);
224         List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
225         ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock();
226         BuildingBlock bb1 = new BuildingBlock();
227         bb1.setBpmnFlowName("AssignVfModuleBB");
228         ebb1.setBuildingBlock(bb1);
229         flowsToExecute.add(ebb1);
230         ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock();
231         BuildingBlock bb2 = new BuildingBlock();
232         bb2.setBpmnFlowName("CreateVfModuleBB");
233         ebb2.setBuildingBlock(bb2);
234         flowsToExecute.add(ebb2);
235         ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock();
236         BuildingBlock bb3 = new BuildingBlock();
237         bb3.setBpmnFlowName("ActivateVfModuleBB");
238         ebb3.setBuildingBlock(bb3);
239         flowsToExecute.add(ebb3);
240
241         execution.setVariable("flowsToExecute", flowsToExecute);
242         execution.setVariable("gCurrentSequence", 2);
243         doNothing().when(workflowActionBBFailure).updateRequestErrorStatusMessage(isA(DelegateExecution.class));
244
245         workflowActionBBTasks.rollbackExecutionPath(execution);
246         List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
247         assertEquals(ebbs.get(0).getBuildingBlock().getBpmnFlowName(), "DeleteVfModuleBB");
248         assertEquals(ebbs.get(1).getBuildingBlock().getBpmnFlowName(), "UnassignVfModuleBB");
249         assertEquals(0, execution.getVariable("gCurrentSequence"));
250         assertEquals(0, execution.getVariable("retryCount"));
251     }
252
253     @Test
254     public void rollbackExecutionTest() {
255         execution.setVariable("handlingCode", "Rollback");
256         execution.setVariable("isRollback", false);
257         List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
258         ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock();
259         BuildingBlock bb1 = new BuildingBlock();
260         bb1.setBpmnFlowName("AssignServiceInstanceBB");
261         ebb1.setBuildingBlock(bb1);
262         flowsToExecute.add(ebb1);
263         ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock();
264         BuildingBlock bb2 = new BuildingBlock();
265         bb2.setBpmnFlowName("CreateNetworkCollectionBB");
266         ebb2.setBuildingBlock(bb2);
267         flowsToExecute.add(ebb2);
268         ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock();
269         BuildingBlock bb3 = new BuildingBlock();
270         bb3.setBpmnFlowName("AssignNetworkBB");
271         ebb3.setBuildingBlock(bb3);
272         flowsToExecute.add(ebb3);
273         ExecuteBuildingBlock ebb4 = new ExecuteBuildingBlock();
274         BuildingBlock bb4 = new BuildingBlock();
275         bb4.setBpmnFlowName("CreateNetworkBB");
276         ebb4.setBuildingBlock(bb4);
277         flowsToExecute.add(ebb4);
278
279         execution.setVariable("flowsToExecute", flowsToExecute);
280         execution.setVariable("gCurrentSequence", 3);
281         doNothing().when(workflowActionBBFailure).updateRequestErrorStatusMessage(isA(DelegateExecution.class));
282
283         workflowActionBBTasks.rollbackExecutionPath(execution);
284         List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
285         assertEquals(ebbs.get(0).getBuildingBlock().getBpmnFlowName(), "UnassignNetworkBB");
286         assertEquals(ebbs.get(1).getBuildingBlock().getBpmnFlowName(), "DeleteNetworkCollectionBB");
287         assertEquals(ebbs.get(2).getBuildingBlock().getBpmnFlowName(), "UnassignServiceInstanceBB");
288         assertEquals(0, execution.getVariable("gCurrentSequence"));
289     }
290
291     @Test
292     public void rollbackExecutionRollbackToAssignedTest() {
293         execution.setVariable("isRollback", false);
294         execution.setVariable("handlingCode", "RollbackToAssigned");
295         List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
296         ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock();
297         BuildingBlock bb1 = new BuildingBlock();
298         bb1.setBpmnFlowName("AssignVfModuleBB");
299         ebb1.setBuildingBlock(bb1);
300         flowsToExecute.add(ebb1);
301         ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock();
302         BuildingBlock bb2 = new BuildingBlock();
303         bb2.setBpmnFlowName("CreateVfModuleBB");
304         ebb2.setBuildingBlock(bb2);
305         flowsToExecute.add(ebb2);
306         ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock();
307         BuildingBlock bb3 = new BuildingBlock();
308         bb3.setBpmnFlowName("ActivateVfModuleBB");
309         ebb3.setBuildingBlock(bb3);
310         flowsToExecute.add(ebb3);
311
312         execution.setVariable("flowsToExecute", flowsToExecute);
313         execution.setVariable("gCurrentSequence", 2);
314
315         workflowActionBBTasks.rollbackExecutionPath(execution);
316         List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
317         assertEquals("DeleteVfModuleBB", ebbs.get(0).getBuildingBlock().getBpmnFlowName());
318         assertEquals(0, execution.getVariable("gCurrentSequence"));
319         assertEquals(1, ebbs.size());
320     }
321
322     @Test
323     public void rollbackExecutionRollbackToCreatedTest() {
324         execution.setVariable("isRollback", false);
325         execution.setVariable("handlingCode", "RollbackToCreated");
326         List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
327         ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock();
328         BuildingBlock bb1 = new BuildingBlock();
329         bb1.setBpmnFlowName("AssignVfModuleBB");
330         ebb1.setBuildingBlock(bb1);
331         flowsToExecute.add(ebb1);
332         ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock();
333         BuildingBlock bb2 = new BuildingBlock();
334         bb2.setBpmnFlowName("CreateVfModuleBB");
335         ebb2.setBuildingBlock(bb2);
336         flowsToExecute.add(ebb2);
337         ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock();
338         BuildingBlock bb3 = new BuildingBlock();
339         bb3.setBpmnFlowName("ActivateVfModuleBB");
340         ebb3.setBuildingBlock(bb3);
341         flowsToExecute.add(ebb3);
342
343         execution.setVariable("flowsToExecute", flowsToExecute);
344         execution.setVariable("gCurrentSequence", 3);
345
346         workflowActionBBTasks.rollbackExecutionPath(execution);
347         List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
348         assertEquals("DeactivateVfModuleBB", ebbs.get(0).getBuildingBlock().getBpmnFlowName());
349         assertEquals(0, execution.getVariable("gCurrentSequence"));
350         assertEquals(1, ebbs.size());
351     }
352
353     @Test
354     public void checkRetryStatusTest() {
355         String reqId = "reqId123";
356         execution.setVariable("mso-request-id", reqId);
357         doNothing().when(workflowActionBBFailure).updateRequestErrorStatusMessage(isA(DelegateExecution.class));
358         doReturn("6").when(environment).getProperty("mso.rainyDay.maxRetries");
359         execution.setVariable("handlingCode", "Retry");
360         execution.setVariable("retryCount", 1);
361         execution.setVariable("gCurrentSequence", 1);
362         InfraActiveRequests req = new InfraActiveRequests();
363         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
364         workflowActionBBTasks.checkRetryStatus(execution);
365         assertEquals(0, execution.getVariable("gCurrentSequence"));
366     }
367
368     @Test
369     public void checkRetryStatusTestExceededMaxRetries() {
370         String reqId = "reqId123";
371         execution.setVariable("mso-request-id", reqId);
372         doNothing().when(workflowActionBBFailure).updateRequestErrorStatusMessage(isA(DelegateExecution.class));
373         doReturn("6").when(environment).getProperty("mso.rainyDay.maxRetries");
374         execution.setVariable("handlingCode", "Retry");
375         execution.setVariable("retryCount", 6);
376         execution.setVariable("gCurrentSequence", 1);
377         InfraActiveRequests req = new InfraActiveRequests();
378         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
379         try {
380             workflowActionBBTasks.checkRetryStatus(execution);
381         } catch (BpmnError e) {
382             WorkflowException exception = (WorkflowException) execution.getVariable("WorkflowException");
383             assertEquals("Exceeded maximum retries. Ending flow with status Abort", exception.getErrorMessage());
384         }
385     }
386
387     @Test
388     public void checkRetryStatusNoRetryTest() {
389         String reqId = "reqId123";
390         execution.setVariable("mso-request-id", reqId);
391         execution.setVariable("retryCount", 3);
392         execution.setVariable("handlingCode", "Success");
393         execution.setVariable("gCurrentSequence", 1);
394         InfraActiveRequests req = new InfraActiveRequests();
395         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
396         workflowActionBBTasks.checkRetryStatus(execution);
397         assertEquals(0, execution.getVariable("retryCount"));
398     }
399
400     @Test
401     public void updateInstanceId() {
402         String reqId = "req123";
403         String instanceId = "123123123";
404         execution.setVariable("mso-request-id", reqId);
405         execution.setVariable("resourceId", instanceId);
406         execution.setVariable("resourceType", WorkflowType.SERVICE);
407         doReturn(reqMock).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
408         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
409         workflowActionBBTasks.updateInstanceId(execution);
410         Mockito.verify(reqMock, Mockito.times(1)).setServiceInstanceId(instanceId);
411     }
412 }