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