189ecb96c716bb680cb41dc64e4df33835db948c
[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 msoCompleteProcessTest() throws Exception{
118                 execution.setVariable("mso-request-id", "00f704ca-c5e5-4f95-a72c-6889db7b0688");
119                 execution.setVariable("requestAction", "createInstance");
120                 execution.setVariable("resourceId", "123");
121                 execution.setVariable("source","MSO");
122                 execution.setVariable("resourceName", "Service");
123                 execution.setVariable("aLaCarte", true);
124                 workflowActionBBTasks.setupCompleteMsoProcess(execution);
125                 String response = (String) execution.getVariable("CompleteMsoProcessRequest");
126                 assertEquals(response,"<aetgt:MsoCompletionRequest xmlns:aetgt=\"http://org.onap/so/workflow/schema/v1\" xmlns:ns=\"http://org.onap/so/request/types/v1\"><request-info xmlns=\"http://org.onap/so/infra/vnf-request/v1\"><request-id>00f704ca-c5e5-4f95-a72c-6889db7b0688</request-id><action>createInstance</action><source>MSO</source></request-info><status-message>ALaCarte-Service-createInstance request was executed correctly.</status-message><serviceInstanceId>123</serviceInstanceId><mso-bpel-name>WorkflowActionBB</mso-bpel-name></aetgt:MsoCompletionRequest>");
127         }
128         
129         @Test
130         public void rollbackExecutionPathTest(){
131                 execution.setVariable("handlingCode", "Rollback");
132                 execution.setVariable("isRollback", false);
133                 List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
134                 ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock();
135                 BuildingBlock bb1 = new BuildingBlock();
136                 bb1.setBpmnFlowName("AssignVfModuleBB");
137                 ebb1.setBuildingBlock(bb1);
138                 flowsToExecute.add(ebb1);
139                 ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock();
140                 BuildingBlock bb2 = new BuildingBlock();
141                 bb2.setBpmnFlowName("CreateVfModuleBB");
142                 ebb2.setBuildingBlock(bb2);
143                 flowsToExecute.add(ebb2);
144                 ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock();
145                 BuildingBlock bb3 = new BuildingBlock();
146                 bb3.setBpmnFlowName("ActivateVfModuleBB");
147                 ebb3.setBuildingBlock(bb3);
148                 flowsToExecute.add(ebb3);
149                 
150                 execution.setVariable("flowsToExecute", flowsToExecute);
151                 execution.setVariable("gCurrentSequence", 3);
152                 doNothing().when(workflowActionBBFailure).updateRequestErrorStatusMessage(isA(DelegateExecution.class));
153                 
154                 workflowActionBBTasks.rollbackExecutionPath(execution);
155                 List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
156                 assertEquals(ebbs.get(0).getBuildingBlock().getBpmnFlowName(),"DeactivateVfModuleBB");
157                 assertEquals(ebbs.get(1).getBuildingBlock().getBpmnFlowName(),"DeleteVfModuleBB");
158                 assertEquals(ebbs.get(2).getBuildingBlock().getBpmnFlowName(),"UnassignVfModuleBB");
159                 assertEquals(0,execution.getVariable("gCurrentSequence"));
160         }
161         
162         @Test
163         public void rollbackExecutionPathUnfinishedFlowTest(){
164                 execution.setVariable("handlingCode", "Rollback");
165                 execution.setVariable("isRollback", false);
166                 List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
167                 ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock();
168                 BuildingBlock bb1 = new BuildingBlock();
169                 bb1.setBpmnFlowName("AssignVfModuleBB");
170                 ebb1.setBuildingBlock(bb1);
171                 flowsToExecute.add(ebb1);
172                 ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock();
173                 BuildingBlock bb2 = new BuildingBlock();
174                 bb2.setBpmnFlowName("CreateVfModuleBB");
175                 ebb2.setBuildingBlock(bb2);
176                 flowsToExecute.add(ebb2);
177                 ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock();
178                 BuildingBlock bb3 = new BuildingBlock();
179                 bb3.setBpmnFlowName("ActivateVfModuleBB");
180                 ebb3.setBuildingBlock(bb3);
181                 flowsToExecute.add(ebb3);
182                 
183                 execution.setVariable("flowsToExecute", flowsToExecute);
184                 execution.setVariable("gCurrentSequence", 2);
185                 doNothing().when(workflowActionBBFailure).updateRequestErrorStatusMessage(isA(DelegateExecution.class));
186                 
187                 workflowActionBBTasks.rollbackExecutionPath(execution);
188                 List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
189                 assertEquals(ebbs.get(0).getBuildingBlock().getBpmnFlowName(),"DeleteVfModuleBB");
190                 assertEquals(ebbs.get(1).getBuildingBlock().getBpmnFlowName(),"UnassignVfModuleBB");    
191                 assertEquals(0,execution.getVariable("gCurrentSequence"));
192                 assertEquals(0,execution.getVariable("retryCount"));
193         }
194         
195         @Test
196         public void rollbackExecutionTest(){
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("AssignServiceInstanceBB");
203                 ebb1.setBuildingBlock(bb1);
204                 flowsToExecute.add(ebb1);
205                 ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock();
206                 BuildingBlock bb2 = new BuildingBlock();
207                 bb2.setBpmnFlowName("CreateNetworkCollectionBB");
208                 ebb2.setBuildingBlock(bb2);
209                 flowsToExecute.add(ebb2);
210                 ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock();
211                 BuildingBlock bb3 = new BuildingBlock();
212                 bb3.setBpmnFlowName("AssignNetworkBB");
213                 ebb3.setBuildingBlock(bb3);
214                 flowsToExecute.add(ebb3);
215                 ExecuteBuildingBlock ebb4 = new ExecuteBuildingBlock();
216                 BuildingBlock bb4 = new BuildingBlock();
217                 bb4.setBpmnFlowName("CreateNetworkBB");
218                 ebb4.setBuildingBlock(bb4);
219                 flowsToExecute.add(ebb4);
220                 
221                 execution.setVariable("flowsToExecute", flowsToExecute);
222                 execution.setVariable("gCurrentSequence", 3);
223                 doNothing().when(workflowActionBBFailure).updateRequestErrorStatusMessage(isA(DelegateExecution.class));
224                 
225                 workflowActionBBTasks.rollbackExecutionPath(execution);
226                 List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
227                 assertEquals(ebbs.get(0).getBuildingBlock().getBpmnFlowName(),"UnassignNetworkBB");
228                 assertEquals(ebbs.get(1).getBuildingBlock().getBpmnFlowName(),"DeleteNetworkCollectionBB");     
229                 assertEquals(ebbs.get(2).getBuildingBlock().getBpmnFlowName(),"UnassignServiceInstanceBB");
230                 assertEquals(0,execution.getVariable("gCurrentSequence"));
231         }
232         
233         @Test
234         public void rollbackExecutionRollbackToAssignedTest(){
235                 execution.setVariable("isRollback", false);
236                 execution.setVariable("handlingCode", "RollbackToAssigned");
237                 List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
238                 ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock();
239                 BuildingBlock bb1 = new BuildingBlock();
240                 bb1.setBpmnFlowName("AssignServiceInstanceBB");
241                 ebb1.setBuildingBlock(bb1);
242                 flowsToExecute.add(ebb1);
243                 ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock();
244                 BuildingBlock bb2 = new BuildingBlock();
245                 bb2.setBpmnFlowName("CreateNetworkCollectionBB");
246                 ebb2.setBuildingBlock(bb2);
247                 flowsToExecute.add(ebb2);
248                 ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock();
249                 BuildingBlock bb3 = new BuildingBlock();
250                 bb3.setBpmnFlowName("AssignNetworkBB");
251                 ebb3.setBuildingBlock(bb3);
252                 flowsToExecute.add(ebb3);
253                 ExecuteBuildingBlock ebb4 = new ExecuteBuildingBlock();
254                 BuildingBlock bb4 = new BuildingBlock();
255                 bb4.setBpmnFlowName("CreateNetworkBB");
256                 ebb4.setBuildingBlock(bb4);
257                 flowsToExecute.add(ebb4);
258                 
259                 execution.setVariable("flowsToExecute", flowsToExecute);
260                 execution.setVariable("gCurrentSequence", 3);
261                 doNothing().when(workflowActionBBFailure).updateRequestErrorStatusMessage(isA(DelegateExecution.class));
262
263                 workflowActionBBTasks.rollbackExecutionPath(execution);
264                 List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
265                 assertEquals(ebbs.get(0).getBuildingBlock().getBpmnFlowName(),"DeleteNetworkCollectionBB");
266                 assertEquals(0,execution.getVariable("gCurrentSequence"));
267         }
268         
269         @Test
270         public void checkRetryStatusTest(){
271                 String reqId = "reqId123";
272                 execution.setVariable("mso-request-id", reqId);
273                 doNothing().when(workflowActionBBFailure).updateRequestErrorStatusMessage(isA(DelegateExecution.class));
274                 execution.setVariable("handlingCode","Retry");
275                 execution.setVariable("retryCount", 1);
276                 execution.setVariable("gCurrentSequence",1);
277                 InfraActiveRequests req = new InfraActiveRequests();
278                 doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
279                 workflowActionBBTasks.checkRetryStatus(execution);
280                 assertEquals(0,execution.getVariable("gCurrentSequence"));
281         }
282         
283         @Test
284         public void checkRetryStatusNoRetryTest(){
285                 String reqId = "reqId123";
286                 execution.setVariable("mso-request-id", reqId);
287                 execution.setVariable("retryCount", 3);
288                 execution.setVariable("handlingCode","Success");
289                 execution.setVariable("gCurrentSequence",1);
290                 InfraActiveRequests req = new InfraActiveRequests();
291                 doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
292                 workflowActionBBTasks.checkRetryStatus(execution);
293                 assertEquals(0,execution.getVariable("retryCount"));
294         }
295 }