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