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