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