2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.bpmn.infrastructure.workflow.tasks;
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;
31 import java.util.ArrayList;
32 import java.util.List;
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;
50 public class WorkflowActionBBTasksTest extends BaseTaskTest {
53 protected WorkflowAction workflowAction;
57 protected WorkflowActionBBTasks workflowActionBBTasks;
60 InfraActiveRequests reqMock;
62 private DelegateExecution execution;
65 public ExpectedException thrown = ExpectedException.none();
68 public void before() throws Exception {
69 execution = new DelegateExecutionFake();
70 org.onap.aai.domain.yang.ServiceInstance servInstance = new org.onap.aai.domain.yang.ServiceInstance();
71 servInstance.setServiceInstanceId("TEST");
72 when(bbSetupUtils.getAAIServiceInstanceByName(anyString(), anyObject())).thenReturn(servInstance);
73 workflowAction.setBbInputSetupUtils(bbSetupUtils);
74 workflowAction.setBbInputSetup(bbInputSetup);
78 public void selectBBTest() throws Exception{
79 String gAction = "Delete-Network-Collection";
80 execution.setVariable("mso-request-id", "00f704ca-c5e5-4f95-a72c-6889db7b0688");
81 execution.setVariable("requestAction", gAction);
82 execution.setVariable("gCurrentSequence", 0);
83 execution.setVariable("homing", false);
84 execution.setVariable("calledHoming", false);
85 List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
86 ExecuteBuildingBlock ebb = new ExecuteBuildingBlock();
87 flowsToExecute.add(ebb);
88 execution.setVariable("flowsToExecute", flowsToExecute);
89 workflowActionBBTasks.selectBB(execution);
90 boolean success = (boolean) execution.getVariable("completed");
91 assertEquals(true,success);
95 public void select2BBTest() throws Exception{
96 String gAction = "Delete-Network-Collection";
97 execution.setVariable("mso-request-id", "00f704ca-c5e5-4f95-a72c-6889db7b0688");
98 execution.setVariable("requestAction", gAction);
99 execution.setVariable("gCurrentSequence", 0);
100 execution.setVariable("homing", false);
101 execution.setVariable("calledHoming", false);
102 List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
103 ExecuteBuildingBlock ebb = new ExecuteBuildingBlock();
104 ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock();
105 flowsToExecute.add(ebb);
106 flowsToExecute.add(ebb2);
107 execution.setVariable("flowsToExecute", flowsToExecute);
108 workflowActionBBTasks.selectBB(execution);
109 boolean success = (boolean) execution.getVariable("completed");
110 assertEquals(false,success);
114 public void msoCompleteProcessTest() throws Exception{
115 execution.setVariable("mso-request-id", "00f704ca-c5e5-4f95-a72c-6889db7b0688");
116 execution.setVariable("requestAction", "createInstance");
117 execution.setVariable("resourceId", "123");
118 execution.setVariable("source","MSO");
119 execution.setVariable("resourceName", "Service");
120 execution.setVariable("aLaCarte", true);
121 workflowActionBBTasks.setupCompleteMsoProcess(execution);
122 String response = (String) execution.getVariable("CompleteMsoProcessRequest");
123 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 public void rollbackExecutionPathTest(){
128 execution.setVariable("handlingCode", "Rollback");
129 execution.setVariable("isRollback", false);
130 List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
131 ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock();
132 BuildingBlock bb1 = new BuildingBlock();
133 bb1.setBpmnFlowName("AssignVfModuleBB");
134 ebb1.setBuildingBlock(bb1);
135 flowsToExecute.add(ebb1);
136 ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock();
137 BuildingBlock bb2 = new BuildingBlock();
138 bb2.setBpmnFlowName("CreateVfModuleBB");
139 ebb2.setBuildingBlock(bb2);
140 flowsToExecute.add(ebb2);
141 ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock();
142 BuildingBlock bb3 = new BuildingBlock();
143 bb3.setBpmnFlowName("ActivateVfModuleBB");
144 ebb3.setBuildingBlock(bb3);
145 flowsToExecute.add(ebb3);
147 execution.setVariable("flowsToExecute", flowsToExecute);
148 execution.setVariable("gCurrentSequence", 3);
149 doNothing().when(workflowActionBBTasks).updateRequestErrorStatusMessage(isA(DelegateExecution.class));
151 workflowActionBBTasks.rollbackExecutionPath(execution);
152 List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
153 assertEquals(ebbs.get(0).getBuildingBlock().getBpmnFlowName(),"DeactivateVfModuleBB");
154 assertEquals(ebbs.get(1).getBuildingBlock().getBpmnFlowName(),"DeleteVfModuleBB");
155 assertEquals(ebbs.get(2).getBuildingBlock().getBpmnFlowName(),"UnassignVfModuleBB");
156 assertEquals(0,execution.getVariable("gCurrentSequence"));
160 public void rollbackExecutionPathUnfinishedFlowTest(){
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);
180 execution.setVariable("flowsToExecute", flowsToExecute);
181 execution.setVariable("gCurrentSequence", 2);
182 doNothing().when(workflowActionBBTasks).updateRequestErrorStatusMessage(isA(DelegateExecution.class));
184 workflowActionBBTasks.rollbackExecutionPath(execution);
185 List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
186 assertEquals(ebbs.get(0).getBuildingBlock().getBpmnFlowName(),"DeleteVfModuleBB");
187 assertEquals(ebbs.get(1).getBuildingBlock().getBpmnFlowName(),"UnassignVfModuleBB");
188 assertEquals(0,execution.getVariable("gCurrentSequence"));
189 assertEquals(0,execution.getVariable("retryCount"));
193 public void rollbackExecutionTest(){
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("AssignServiceInstanceBB");
200 ebb1.setBuildingBlock(bb1);
201 flowsToExecute.add(ebb1);
202 ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock();
203 BuildingBlock bb2 = new BuildingBlock();
204 bb2.setBpmnFlowName("CreateNetworkCollectionBB");
205 ebb2.setBuildingBlock(bb2);
206 flowsToExecute.add(ebb2);
207 ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock();
208 BuildingBlock bb3 = new BuildingBlock();
209 bb3.setBpmnFlowName("AssignNetworkBB");
210 ebb3.setBuildingBlock(bb3);
211 flowsToExecute.add(ebb3);
212 ExecuteBuildingBlock ebb4 = new ExecuteBuildingBlock();
213 BuildingBlock bb4 = new BuildingBlock();
214 bb4.setBpmnFlowName("CreateNetworkBB");
215 ebb4.setBuildingBlock(bb4);
216 flowsToExecute.add(ebb4);
218 execution.setVariable("flowsToExecute", flowsToExecute);
219 execution.setVariable("gCurrentSequence", 3);
220 doNothing().when(workflowActionBBTasks).updateRequestErrorStatusMessage(isA(DelegateExecution.class));
222 workflowActionBBTasks.rollbackExecutionPath(execution);
223 List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
224 assertEquals(ebbs.get(0).getBuildingBlock().getBpmnFlowName(),"UnassignNetworkBB");
225 assertEquals(ebbs.get(1).getBuildingBlock().getBpmnFlowName(),"DeleteNetworkCollectionBB");
226 assertEquals(ebbs.get(2).getBuildingBlock().getBpmnFlowName(),"UnassignServiceInstanceBB");
227 assertEquals(0,execution.getVariable("gCurrentSequence"));
231 public void rollbackExecutionRollbackToAssignedTest(){
232 execution.setVariable("isRollback", false);
233 execution.setVariable("handlingCode", "RollbackToAssigned");
234 List<ExecuteBuildingBlock> flowsToExecute = new ArrayList();
235 ExecuteBuildingBlock ebb1 = new ExecuteBuildingBlock();
236 BuildingBlock bb1 = new BuildingBlock();
237 bb1.setBpmnFlowName("AssignServiceInstanceBB");
238 ebb1.setBuildingBlock(bb1);
239 flowsToExecute.add(ebb1);
240 ExecuteBuildingBlock ebb2 = new ExecuteBuildingBlock();
241 BuildingBlock bb2 = new BuildingBlock();
242 bb2.setBpmnFlowName("CreateNetworkCollectionBB");
243 ebb2.setBuildingBlock(bb2);
244 flowsToExecute.add(ebb2);
245 ExecuteBuildingBlock ebb3 = new ExecuteBuildingBlock();
246 BuildingBlock bb3 = new BuildingBlock();
247 bb3.setBpmnFlowName("AssignNetworkBB");
248 ebb3.setBuildingBlock(bb3);
249 flowsToExecute.add(ebb3);
250 ExecuteBuildingBlock ebb4 = new ExecuteBuildingBlock();
251 BuildingBlock bb4 = new BuildingBlock();
252 bb4.setBpmnFlowName("CreateNetworkBB");
253 ebb4.setBuildingBlock(bb4);
254 flowsToExecute.add(ebb4);
256 execution.setVariable("flowsToExecute", flowsToExecute);
257 execution.setVariable("gCurrentSequence", 3);
258 doNothing().when(workflowActionBBTasks).updateRequestErrorStatusMessage(isA(DelegateExecution.class));
260 workflowActionBBTasks.rollbackExecutionPath(execution);
261 List<ExecuteBuildingBlock> ebbs = (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
262 assertEquals(ebbs.get(0).getBuildingBlock().getBpmnFlowName(),"DeleteNetworkCollectionBB");
263 assertEquals(0,execution.getVariable("gCurrentSequence"));
267 public void checkRetryStatusTest(){
268 String reqId = "reqId123";
269 execution.setVariable("mso-request-id", reqId);
270 doNothing().when(workflowActionBBTasks).updateRequestErrorStatusMessage(isA(DelegateExecution.class));
271 execution.setVariable("handlingCode","Retry");
272 execution.setVariable("retryCount", 1);
273 execution.setVariable("gCurrentSequence",1);
274 InfraActiveRequests req = new InfraActiveRequests();
275 doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
276 workflowActionBBTasks.checkRetryStatus(execution);
277 assertEquals(0,execution.getVariable("gCurrentSequence"));
281 public void checkRetryStatusNoRetryTest(){
282 String reqId = "reqId123";
283 execution.setVariable("mso-request-id", reqId);
284 execution.setVariable("retryCount", 3);
285 execution.setVariable("handlingCode","Success");
286 execution.setVariable("gCurrentSequence",1);
287 InfraActiveRequests req = new InfraActiveRequests();
288 doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
289 workflowActionBBTasks.checkRetryStatus(execution);
290 assertEquals(0,execution.getVariable("retryCount"));
295 public void updateRequestStatusToFailed_Null_Rollback(){
296 String reqId = "reqId123";
297 execution.setVariable("mso-request-id", reqId);
298 execution.setVariable("retryCount", 3);
299 execution.setVariable("handlingCode","Success");
300 execution.setVariable("gCurrentSequence",1);
301 WorkflowException we = new WorkflowException("WorkflowAction",1231,"Error Case");
302 execution.setVariable("WorkflowException",we);
304 doReturn(reqMock).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
305 workflowActionBBTasks.updateRequestStatusToFailed(execution);
306 Mockito.verify( reqMock, Mockito.times(1)).setStatusMessage("Error Case");
307 Mockito.verify( reqMock, Mockito.times(1)).setRequestStatus("FAILED");
308 Mockito.verify( reqMock, Mockito.times(1)).setProgress(Long.valueOf(100));
309 Mockito.verify( reqMock, Mockito.times(1)).setLastModifiedBy("CamundaBPMN");