Merge "fix major sonar bug"
[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                 assertEquals(true,success);
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                 assertEquals(false,success);
119         }
120         
121         @Test
122         public void updateRequestStatusToCompleteTest() throws Exception{
123                 String reqId = "reqId123";
124                 execution.setVariable("mso-request-id", reqId);
125                 execution.setVariable("requestAction", "createInstance");
126                 execution.setVariable("resourceName", "Service");
127                 execution.setVariable("aLaCarte", true);
128                 InfraActiveRequests req = new InfraActiveRequests();
129                 doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
130                 doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
131                 workflowActionBBTasks.updateRequestStatusToComplete(execution);
132                 assertEquals("ALaCarte-Service-createInstance request was executed correctly.",execution.getVariable("finalStatusMessage"));
133         }
134         
135         @Test
136         public void updateRequestStatusToFailedFlowStatusTest() {
137                 String reqId = "reqId123";
138                 execution.setVariable("mso-request-id", reqId);
139                 execution.setVariable("isRollbackComplete", false);
140                 execution.setVariable("isRollback", false);
141                 ExecuteBuildingBlock ebb = new ExecuteBuildingBlock();
142                 BuildingBlock buildingBlock = new BuildingBlock();
143                 buildingBlock.setBpmnFlowName("CreateNetworkBB");
144                 ebb.setBuildingBlock(buildingBlock);
145                 execution.setVariable("buildingBlock", ebb);
146                 WorkflowException wfe = new WorkflowException("failure", 1, "failure");
147                 execution.setVariable("WorkflowException", wfe);
148                 InfraActiveRequests req = new InfraActiveRequests();
149                 doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
150                 doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
151                 workflowActionBBTasks.updateRequestStatusToFailed(execution);
152                 assertEquals("CreateNetworkBB has failed.",execution.getVariable("flowStatus"));
153         }
154         
155         @Test
156         public void rollbackExecutionPathTest(){
157                 execution.setVariable("handlingCode", "Rollback");
158                 execution.setVariable("isRollback", false);
159                 List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
160                 ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock();
161                 BuildingBlock bb1 = new BuildingBlock();
162                 bb1.setBpmnFlowName("AssignVfModuleBB");
163                 ebb1.setBuildingBlock(bb1);
164                 flowsToExecute.add(ebb1);
165                 ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock();
166                 BuildingBlock bb2 = new BuildingBlock();
167                 bb2.setBpmnFlowName("CreateVfModuleBB");
168                 ebb2.setBuildingBlock(bb2);
169                 flowsToExecute.add(ebb2);
170                 ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock();
171                 BuildingBlock bb3 = new BuildingBlock();
172                 bb3.setBpmnFlowName("ActivateVfModuleBB");
173                 ebb3.setBuildingBlock(bb3);
174                 flowsToExecute.add(ebb3);
175                 
176                 execution.setVariable("flowsToExecute", flowsToExecute);
177                 execution.setVariable("gCurrentSequence", 3);
178                 doNothing().when(workflowActionBBFailure).updateRequestErrorStatusMessage(isA(DelegateExecution.class));
179                 
180                 workflowActionBBTasks.rollbackExecutionPath(execution);
181                 List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
182                 assertEquals(ebbs.get(0).getBuildingBlock().getBpmnFlowName(),"DeactivateVfModuleBB");
183                 assertEquals(ebbs.get(1).getBuildingBlock().getBpmnFlowName(),"DeleteVfModuleBB");
184                 assertEquals(ebbs.get(2).getBuildingBlock().getBpmnFlowName(),"UnassignVfModuleBB");
185                 assertEquals(0,execution.getVariable("gCurrentSequence"));
186         }
187         
188         @Test
189         public void rollbackExecutionPathUnfinishedFlowTest(){
190                 execution.setVariable("handlingCode", "Rollback");
191                 execution.setVariable("isRollback", false);
192                 List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
193                 ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock();
194                 BuildingBlock bb1 = new BuildingBlock();
195                 bb1.setBpmnFlowName("AssignVfModuleBB");
196                 ebb1.setBuildingBlock(bb1);
197                 flowsToExecute.add(ebb1);
198                 ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock();
199                 BuildingBlock bb2 = new BuildingBlock();
200                 bb2.setBpmnFlowName("CreateVfModuleBB");
201                 ebb2.setBuildingBlock(bb2);
202                 flowsToExecute.add(ebb2);
203                 ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock();
204                 BuildingBlock bb3 = new BuildingBlock();
205                 bb3.setBpmnFlowName("ActivateVfModuleBB");
206                 ebb3.setBuildingBlock(bb3);
207                 flowsToExecute.add(ebb3);
208                 
209                 execution.setVariable("flowsToExecute", flowsToExecute);
210                 execution.setVariable("gCurrentSequence", 2);
211                 doNothing().when(workflowActionBBFailure).updateRequestErrorStatusMessage(isA(DelegateExecution.class));
212                 
213                 workflowActionBBTasks.rollbackExecutionPath(execution);
214                 List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
215                 assertEquals(ebbs.get(0).getBuildingBlock().getBpmnFlowName(),"DeleteVfModuleBB");
216                 assertEquals(ebbs.get(1).getBuildingBlock().getBpmnFlowName(),"UnassignVfModuleBB");    
217                 assertEquals(0,execution.getVariable("gCurrentSequence"));
218                 assertEquals(0,execution.getVariable("retryCount"));
219         }
220         
221         @Test
222         public void rollbackExecutionTest(){
223                 execution.setVariable("handlingCode", "Rollback");
224                 execution.setVariable("isRollback", false);
225                 List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
226                 ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock();
227                 BuildingBlock bb1 = new BuildingBlock();
228                 bb1.setBpmnFlowName("AssignServiceInstanceBB");
229                 ebb1.setBuildingBlock(bb1);
230                 flowsToExecute.add(ebb1);
231                 ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock();
232                 BuildingBlock bb2 = new BuildingBlock();
233                 bb2.setBpmnFlowName("CreateNetworkCollectionBB");
234                 ebb2.setBuildingBlock(bb2);
235                 flowsToExecute.add(ebb2);
236                 ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock();
237                 BuildingBlock bb3 = new BuildingBlock();
238                 bb3.setBpmnFlowName("AssignNetworkBB");
239                 ebb3.setBuildingBlock(bb3);
240                 flowsToExecute.add(ebb3);
241                 ExecuteBuildingBlock ebb4 = new ExecuteBuildingBlock();
242                 BuildingBlock bb4 = new BuildingBlock();
243                 bb4.setBpmnFlowName("CreateNetworkBB");
244                 ebb4.setBuildingBlock(bb4);
245                 flowsToExecute.add(ebb4);
246                 
247                 execution.setVariable("flowsToExecute", flowsToExecute);
248                 execution.setVariable("gCurrentSequence", 3);
249                 doNothing().when(workflowActionBBFailure).updateRequestErrorStatusMessage(isA(DelegateExecution.class));
250                 
251                 workflowActionBBTasks.rollbackExecutionPath(execution);
252                 List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
253                 assertEquals(ebbs.get(0).getBuildingBlock().getBpmnFlowName(),"UnassignNetworkBB");
254                 assertEquals(ebbs.get(1).getBuildingBlock().getBpmnFlowName(),"DeleteNetworkCollectionBB");     
255                 assertEquals(ebbs.get(2).getBuildingBlock().getBpmnFlowName(),"UnassignServiceInstanceBB");
256                 assertEquals(0,execution.getVariable("gCurrentSequence"));
257         }
258         
259         @Test
260         public void rollbackExecutionRollbackToAssignedTest(){
261                 execution.setVariable("isRollback", false);
262                 execution.setVariable("handlingCode", "RollbackToAssigned");
263                 List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
264                 ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock();
265                 BuildingBlock bb1 = new BuildingBlock();
266                 bb1.setBpmnFlowName("AssignVfModuleBB");
267                 ebb1.setBuildingBlock(bb1);
268                 flowsToExecute.add(ebb1);
269                 ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock();
270                 BuildingBlock bb2 = new BuildingBlock();
271                 bb2.setBpmnFlowName("CreateVfModuleBB");
272                 ebb2.setBuildingBlock(bb2);
273                 flowsToExecute.add(ebb2);
274                 ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock();
275                 BuildingBlock bb3 = new BuildingBlock();
276                 bb3.setBpmnFlowName("ActivateVfModuleBB");
277                 ebb3.setBuildingBlock(bb3);
278                 flowsToExecute.add(ebb3);
279                 
280                 execution.setVariable("flowsToExecute", flowsToExecute);
281                 execution.setVariable("gCurrentSequence", 2);
282                 
283                 workflowActionBBTasks.rollbackExecutionPath(execution);
284                 List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
285                 assertEquals("DeleteVfModuleBB",ebbs.get(0).getBuildingBlock().getBpmnFlowName());
286                 assertEquals(0,execution.getVariable("gCurrentSequence"));
287                 assertEquals(1,ebbs.size());
288         }
289         
290         @Test
291         public void checkRetryStatusTest(){
292                 String reqId = "reqId123";
293                 execution.setVariable("mso-request-id", reqId);
294                 doNothing().when(workflowActionBBFailure).updateRequestErrorStatusMessage(isA(DelegateExecution.class));
295                 doReturn("6").when(environment).getProperty("mso.rainyDay.maxRetries");
296                 execution.setVariable("handlingCode","Retry");
297                 execution.setVariable("retryCount", 1);
298                 execution.setVariable("gCurrentSequence",1);
299                 InfraActiveRequests req = new InfraActiveRequests();
300                 doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
301                 workflowActionBBTasks.checkRetryStatus(execution);
302                 assertEquals(0,execution.getVariable("gCurrentSequence"));
303         }
304         
305         @Test
306         public void checkRetryStatusTestExceededMaxRetries(){
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", 6);
313                 execution.setVariable("gCurrentSequence",1);
314                 InfraActiveRequests req = new InfraActiveRequests();
315                 doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
316                 try{
317                         workflowActionBBTasks.checkRetryStatus(execution);
318                 } catch (BpmnError e) {
319                         WorkflowException exception = (WorkflowException) execution.getVariable("WorkflowException");
320                         assertEquals("Exceeded maximum retries. Ending flow with status Abort",exception.getErrorMessage());
321                 }
322         }
323         
324         @Test
325         public void checkRetryStatusNoRetryTest(){
326                 String reqId = "reqId123";
327                 execution.setVariable("mso-request-id", reqId);
328                 execution.setVariable("retryCount", 3);
329                 execution.setVariable("handlingCode","Success");
330                 execution.setVariable("gCurrentSequence",1);
331                 InfraActiveRequests req = new InfraActiveRequests();
332                 doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
333                 workflowActionBBTasks.checkRetryStatus(execution);
334                 assertEquals(0,execution.getVariable("retryCount"));
335         }
336 }