Bugfixes for December 2018
[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.Matchers.anyObject;
25 import static org.mockito.Matchers.anyString;
26 import static org.mockito.Matchers.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.DelegateExecution;
35 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
36 import org.junit.Before;
37 import org.junit.Rule;
38 import org.junit.Test;
39 import org.junit.rules.ExpectedException;
40 import org.mockito.InjectMocks;
41 import org.mockito.Mock;
42 import org.mockito.Mockito;
43 import org.mockito.Spy;
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.db.request.beans.InfraActiveRequests;
49
50 public class WorkflowActionBBTasksTest extends BaseTaskTest {
51
52         @Mock
53         protected WorkflowAction workflowAction;
54         
55         @Mock
56         protected WorkflowActionBBFailure workflowActionBBFailure;
57         
58         @InjectMocks
59         @Spy
60         protected WorkflowActionBBTasks workflowActionBBTasks;
61         
62         @Mock
63         InfraActiveRequests reqMock;
64         
65         private DelegateExecution execution;
66         
67         @Rule
68         public ExpectedException thrown = ExpectedException.none();
69         
70         @Before
71         public void before() throws Exception {
72                 execution = new DelegateExecutionFake();
73                 org.onap.aai.domain.yang.ServiceInstance servInstance = new org.onap.aai.domain.yang.ServiceInstance();
74                 servInstance.setServiceInstanceId("TEST");
75                 when(bbSetupUtils.getAAIServiceInstanceByName(anyString(), anyObject())).thenReturn(servInstance);
76                 workflowAction.setBbInputSetupUtils(bbSetupUtils);
77                 workflowAction.setBbInputSetup(bbInputSetup);
78         }
79         
80         @Test
81         public void selectBBTest() throws Exception{
82                 String gAction = "Delete-Network-Collection";
83                 execution.setVariable("mso-request-id", "00f704ca-c5e5-4f95-a72c-6889db7b0688");
84                 execution.setVariable("requestAction", gAction);
85                 execution.setVariable("gCurrentSequence", 0);
86                 execution.setVariable("homing", false);
87                 execution.setVariable("calledHoming", false);
88                 List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
89                 ExecuteBuildingBlock ebb = new ExecuteBuildingBlock();
90                 flowsToExecute.add(ebb);
91                 execution.setVariable("flowsToExecute", flowsToExecute);
92                 workflowActionBBTasks.selectBB(execution);
93                 boolean success = (boolean) execution.getVariable("completed");
94                 assertEquals(true,success);
95         }
96         
97         @Test
98         public void select2BBTest() throws Exception{
99                 String gAction = "Delete-Network-Collection";
100                 execution.setVariable("mso-request-id", "00f704ca-c5e5-4f95-a72c-6889db7b0688");
101                 execution.setVariable("requestAction", gAction);
102                 execution.setVariable("gCurrentSequence", 0);
103                 execution.setVariable("homing", false);
104                 execution.setVariable("calledHoming", false);
105                 List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
106                 ExecuteBuildingBlock ebb = new ExecuteBuildingBlock();
107                 ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock();
108                 flowsToExecute.add(ebb);
109                 flowsToExecute.add(ebb2);
110                 execution.setVariable("flowsToExecute", flowsToExecute);
111                 workflowActionBBTasks.selectBB(execution);
112                 boolean success = (boolean) execution.getVariable("completed");
113                 assertEquals(false,success);
114         }
115         
116         @Test
117         public void updateRequestStatusToCompleteTest() throws Exception{
118                 String reqId = "reqId123";
119                 execution.setVariable("mso-request-id", reqId);
120                 execution.setVariable("requestAction", "createInstance");
121                 execution.setVariable("resourceName", "Service");
122                 execution.setVariable("aLaCarte", true);
123                 InfraActiveRequests req = new InfraActiveRequests();
124                 doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
125                 doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
126                 workflowActionBBTasks.updateRequestStatusToComplete(execution);
127                 assertEquals("ALaCarte-Service-createInstance request was executed correctly.",execution.getVariable("finalStatusMessage"));
128         }
129         
130         @Test
131         public void updateRequestStatusToFailedFlowStatusTest() {
132                 String reqId = "reqId123";
133                 execution.setVariable("mso-request-id", reqId);
134                 execution.setVariable("isRollbackComplete", false);
135                 execution.setVariable("isRollback", false);
136                 ExecuteBuildingBlock ebb = new ExecuteBuildingBlock();
137                 BuildingBlock buildingBlock = new BuildingBlock();
138                 buildingBlock.setBpmnFlowName("CreateNetworkBB");
139                 ebb.setBuildingBlock(buildingBlock);
140                 execution.setVariable("buildingBlock", ebb);
141                 WorkflowException wfe = new WorkflowException("failure", 1, "failure");
142                 execution.setVariable("WorkflowException", wfe);
143                 InfraActiveRequests req = new InfraActiveRequests();
144                 doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
145                 doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
146                 workflowActionBBTasks.updateRequestStatusToFailed(execution);
147                 assertEquals("CreateNetworkBB has failed.",execution.getVariable("flowStatus"));
148         }
149         
150         @Test
151         public void rollbackExecutionPathTest(){
152                 execution.setVariable("handlingCode", "Rollback");
153                 execution.setVariable("isRollback", false);
154                 List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
155                 ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock();
156                 BuildingBlock bb1 = new BuildingBlock();
157                 bb1.setBpmnFlowName("AssignVfModuleBB");
158                 ebb1.setBuildingBlock(bb1);
159                 flowsToExecute.add(ebb1);
160                 ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock();
161                 BuildingBlock bb2 = new BuildingBlock();
162                 bb2.setBpmnFlowName("CreateVfModuleBB");
163                 ebb2.setBuildingBlock(bb2);
164                 flowsToExecute.add(ebb2);
165                 ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock();
166                 BuildingBlock bb3 = new BuildingBlock();
167                 bb3.setBpmnFlowName("ActivateVfModuleBB");
168                 ebb3.setBuildingBlock(bb3);
169                 flowsToExecute.add(ebb3);
170                 
171                 execution.setVariable("flowsToExecute", flowsToExecute);
172                 execution.setVariable("gCurrentSequence", 3);
173                 doNothing().when(workflowActionBBFailure).updateRequestErrorStatusMessage(isA(DelegateExecution.class));
174                 
175                 workflowActionBBTasks.rollbackExecutionPath(execution);
176                 List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
177                 assertEquals(ebbs.get(0).getBuildingBlock().getBpmnFlowName(),"DeactivateVfModuleBB");
178                 assertEquals(ebbs.get(1).getBuildingBlock().getBpmnFlowName(),"DeleteVfModuleBB");
179                 assertEquals(ebbs.get(2).getBuildingBlock().getBpmnFlowName(),"UnassignVfModuleBB");
180                 assertEquals(0,execution.getVariable("gCurrentSequence"));
181         }
182         
183         @Test
184         public void rollbackExecutionPathUnfinishedFlowTest(){
185                 execution.setVariable("handlingCode", "Rollback");
186                 execution.setVariable("isRollback", false);
187                 List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
188                 ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock();
189                 BuildingBlock bb1 = new BuildingBlock();
190                 bb1.setBpmnFlowName("AssignVfModuleBB");
191                 ebb1.setBuildingBlock(bb1);
192                 flowsToExecute.add(ebb1);
193                 ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock();
194                 BuildingBlock bb2 = new BuildingBlock();
195                 bb2.setBpmnFlowName("CreateVfModuleBB");
196                 ebb2.setBuildingBlock(bb2);
197                 flowsToExecute.add(ebb2);
198                 ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock();
199                 BuildingBlock bb3 = new BuildingBlock();
200                 bb3.setBpmnFlowName("ActivateVfModuleBB");
201                 ebb3.setBuildingBlock(bb3);
202                 flowsToExecute.add(ebb3);
203                 
204                 execution.setVariable("flowsToExecute", flowsToExecute);
205                 execution.setVariable("gCurrentSequence", 2);
206                 doNothing().when(workflowActionBBFailure).updateRequestErrorStatusMessage(isA(DelegateExecution.class));
207                 
208                 workflowActionBBTasks.rollbackExecutionPath(execution);
209                 List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
210                 assertEquals(ebbs.get(0).getBuildingBlock().getBpmnFlowName(),"DeleteVfModuleBB");
211                 assertEquals(ebbs.get(1).getBuildingBlock().getBpmnFlowName(),"UnassignVfModuleBB");    
212                 assertEquals(0,execution.getVariable("gCurrentSequence"));
213                 assertEquals(0,execution.getVariable("retryCount"));
214         }
215         
216         @Test
217         public void rollbackExecutionTest(){
218                 execution.setVariable("handlingCode", "Rollback");
219                 execution.setVariable("isRollback", false);
220                 List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
221                 ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock();
222                 BuildingBlock bb1 = new BuildingBlock();
223                 bb1.setBpmnFlowName("AssignServiceInstanceBB");
224                 ebb1.setBuildingBlock(bb1);
225                 flowsToExecute.add(ebb1);
226                 ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock();
227                 BuildingBlock bb2 = new BuildingBlock();
228                 bb2.setBpmnFlowName("CreateNetworkCollectionBB");
229                 ebb2.setBuildingBlock(bb2);
230                 flowsToExecute.add(ebb2);
231                 ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock();
232                 BuildingBlock bb3 = new BuildingBlock();
233                 bb3.setBpmnFlowName("AssignNetworkBB");
234                 ebb3.setBuildingBlock(bb3);
235                 flowsToExecute.add(ebb3);
236                 ExecuteBuildingBlock ebb4 = new ExecuteBuildingBlock();
237                 BuildingBlock bb4 = new BuildingBlock();
238                 bb4.setBpmnFlowName("CreateNetworkBB");
239                 ebb4.setBuildingBlock(bb4);
240                 flowsToExecute.add(ebb4);
241                 
242                 execution.setVariable("flowsToExecute", flowsToExecute);
243                 execution.setVariable("gCurrentSequence", 3);
244                 doNothing().when(workflowActionBBFailure).updateRequestErrorStatusMessage(isA(DelegateExecution.class));
245                 
246                 workflowActionBBTasks.rollbackExecutionPath(execution);
247                 List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
248                 assertEquals(ebbs.get(0).getBuildingBlock().getBpmnFlowName(),"UnassignNetworkBB");
249                 assertEquals(ebbs.get(1).getBuildingBlock().getBpmnFlowName(),"DeleteNetworkCollectionBB");     
250                 assertEquals(ebbs.get(2).getBuildingBlock().getBpmnFlowName(),"UnassignServiceInstanceBB");
251                 assertEquals(0,execution.getVariable("gCurrentSequence"));
252         }
253         
254         @Test
255         public void rollbackExecutionRollbackToAssignedTest(){
256                 execution.setVariable("isRollback", false);
257                 execution.setVariable("handlingCode", "RollbackToAssigned");
258                 List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
259                 ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock();
260                 BuildingBlock bb1 = new BuildingBlock();
261                 bb1.setBpmnFlowName("AssignServiceInstanceBB");
262                 ebb1.setBuildingBlock(bb1);
263                 flowsToExecute.add(ebb1);
264                 ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock();
265                 BuildingBlock bb2 = new BuildingBlock();
266                 bb2.setBpmnFlowName("CreateNetworkCollectionBB");
267                 ebb2.setBuildingBlock(bb2);
268                 flowsToExecute.add(ebb2);
269                 ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock();
270                 BuildingBlock bb3 = new BuildingBlock();
271                 bb3.setBpmnFlowName("AssignNetworkBB");
272                 ebb3.setBuildingBlock(bb3);
273                 flowsToExecute.add(ebb3);
274                 ExecuteBuildingBlock ebb4 = new ExecuteBuildingBlock();
275                 BuildingBlock bb4 = new BuildingBlock();
276                 bb4.setBpmnFlowName("CreateNetworkBB");
277                 ebb4.setBuildingBlock(bb4);
278                 flowsToExecute.add(ebb4);
279                 
280                 execution.setVariable("flowsToExecute", flowsToExecute);
281                 execution.setVariable("gCurrentSequence", 3);
282                 doNothing().when(workflowActionBBFailure).updateRequestErrorStatusMessage(isA(DelegateExecution.class));
283
284                 workflowActionBBTasks.rollbackExecutionPath(execution);
285                 List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
286                 assertEquals(ebbs.get(0).getBuildingBlock().getBpmnFlowName(),"DeleteNetworkCollectionBB");
287                 assertEquals(0,execution.getVariable("gCurrentSequence"));
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                 execution.setVariable("handlingCode","Retry");
296                 execution.setVariable("retryCount", 1);
297                 execution.setVariable("gCurrentSequence",1);
298                 InfraActiveRequests req = new InfraActiveRequests();
299                 doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
300                 workflowActionBBTasks.checkRetryStatus(execution);
301                 assertEquals(0,execution.getVariable("gCurrentSequence"));
302         }
303         
304         @Test
305         public void checkRetryStatusNoRetryTest(){
306                 String reqId = "reqId123";
307                 execution.setVariable("mso-request-id", reqId);
308                 execution.setVariable("retryCount", 3);
309                 execution.setVariable("handlingCode","Success");
310                 execution.setVariable("gCurrentSequence",1);
311                 InfraActiveRequests req = new InfraActiveRequests();
312                 doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
313                 workflowActionBBTasks.checkRetryStatus(execution);
314                 assertEquals(0,execution.getVariable("retryCount"));
315         }
316 }