Enable CreateChildServiceBB for Service-Macro-Create
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / infrastructure / workflow / tasks / WorkflowActionBBFailureTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 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.any;
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.mock;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
32 import static org.onap.so.bpmn.infrastructure.service.composition.ServiceCompositionConstants.CHILD_SVC_REQ_CORRELATION_ID;
33 import static org.onap.so.bpmn.infrastructure.service.composition.ServiceCompositionConstants.CHILD_SVC_REQ_ERROR;
34 import static org.onap.so.bpmn.infrastructure.service.composition.ServiceCompositionConstants.CHILD_SVC_REQ_STATUS;
35 import static org.onap.so.bpmn.infrastructure.service.composition.ServiceCompositionConstants.IS_CHILD_PROCESS;
36 import static org.onap.so.bpmn.infrastructure.service.composition.ServiceCompositionConstants.PARENT_CORRELATION_ID;
37
38 import java.sql.Timestamp;
39 import org.camunda.bpm.engine.ProcessEngineServices;
40 import org.camunda.bpm.engine.RuntimeService;
41 import org.camunda.bpm.engine.delegate.DelegateExecution;
42 import org.camunda.bpm.engine.runtime.MessageCorrelationBuilder;
43 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
44 import org.junit.Before;
45 import org.junit.Rule;
46 import org.junit.Test;
47 import org.junit.rules.ExpectedException;
48 import org.mockito.InjectMocks;
49 import org.mockito.Mock;
50 import org.mockito.Mockito;
51 import org.mockito.Spy;
52 import org.onap.so.bpmn.BaseTaskTest;
53 import org.onap.so.bpmn.core.WorkflowException;
54 import org.onap.so.bpmn.servicedecomposition.bbobjects.Customer;
55 import org.onap.so.bpmn.servicedecomposition.entities.BuildingBlock;
56 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
57 import org.onap.so.constants.Status;
58 import org.onap.so.db.request.beans.InfraActiveRequests;
59
60 public class WorkflowActionBBFailureTest extends BaseTaskTest {
61
62     @Mock
63     protected WorkflowAction workflowAction;
64
65     @InjectMocks
66     @Spy
67     protected WorkflowActionBBFailure workflowActionBBFailure;
68
69     @Mock
70     InfraActiveRequests reqMock;
71
72     private DelegateExecution execution;
73
74     @Rule
75     public ExpectedException thrown = ExpectedException.none();
76
77     @Before
78     public void before() throws Exception {
79         execution = new DelegateExecutionFake();
80         org.onap.aai.domain.yang.ServiceInstance servInstance = new org.onap.aai.domain.yang.ServiceInstance();
81         servInstance.setServiceInstanceId("TEST");
82         when(bbSetupUtils.getAAIServiceInstanceByName(anyString(), isA(Customer.class))).thenReturn(servInstance);
83         workflowAction.setBbInputSetupUtils(bbSetupUtils);
84         workflowAction.setBbInputSetup(bbInputSetup);
85     }
86
87     @Test
88     public void updateRequestStatusToFailed_Null_Rollback() {
89         String reqId = "reqId123";
90         execution.setVariable("mso-request-id", reqId);
91         execution.setVariable("retryCount", 3);
92         execution.setVariable("handlingCode", "Success");
93         execution.setVariable("gCurrentSequence", 1);
94         WorkflowException we = new WorkflowException("WorkflowAction", 1231, "Error Case");
95         execution.setVariable("WorkflowException", we);
96
97         doReturn(reqMock).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
98         workflowActionBBFailure.updateRequestStatusToFailed(execution);
99         Mockito.verify(reqMock, Mockito.times(1)).setStatusMessage("Error Case");
100         Mockito.verify(reqMock, Mockito.times(1)).setRequestStatus("FAILED");
101         Mockito.verify(reqMock, Mockito.times(1)).setProgress(Long.valueOf(100));
102         Mockito.verify(reqMock, Mockito.times(1)).setLastModifiedBy("CamundaBPMN");
103         Mockito.verify(reqMock, Mockito.times(1)).setEndTime(any(Timestamp.class));
104     }
105
106     @Test
107     public void updateRequestStatusToFailed() {
108         execution.setVariable("mso-request-id", "123");
109         execution.setVariable("isRollbackComplete", false);
110         execution.setVariable("isRollback", false);
111         InfraActiveRequests req = new InfraActiveRequests();
112         WorkflowException wfe = new WorkflowException("processKey123", 1, "error in test case");
113         execution.setVariable("WorkflowException", wfe);
114         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
115         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
116         workflowActionBBFailure.updateRequestStatusToFailed(execution);
117         String errorMsg = (String) execution.getVariable("ErrorMessage");
118         assertEquals("error in test case", errorMsg);
119         assertEquals(Status.FAILED.toString(), req.getRequestStatus());
120     }
121
122     @Test
123     public void updateRequestStatusToAborted() {
124         execution.setVariable("mso-request-id", "123");
125         execution.setVariable("isRollbackComplete", false);
126         execution.setVariable("isRollback", false);
127         execution.setVariable("handlingCode", "Abort");
128         InfraActiveRequests req = new InfraActiveRequests();
129         WorkflowException wfe = new WorkflowException("processKey123", 1, "error in test case");
130         execution.setVariable("WorkflowException", wfe);
131         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
132         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
133         workflowActionBBFailure.updateRequestStatusToFailed(execution);
134         String errorMsg = (String) execution.getVariable("ErrorMessage");
135         assertEquals("error in test case", errorMsg);
136         assertEquals(Status.ABORTED.toString(), req.getRequestStatus());
137     }
138
139     @Test
140     public void updateRequestStatusToFailedRollback() {
141         execution.setVariable("mso-request-id", "123");
142         execution.setVariable("isRollbackComplete", false);
143         execution.setVariable("isRollback", true);
144         InfraActiveRequests req = new InfraActiveRequests();
145         WorkflowException wfe = new WorkflowException("processKey123", 1, "error in rollback");
146         execution.setVariable("WorkflowException", wfe);
147         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
148         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
149         workflowActionBBFailure.updateRequestStatusToFailed(execution);
150         String errorMsg = (String) execution.getVariable("RollbackErrorMessage");
151         assertEquals("error in rollback", errorMsg);
152         assertEquals(Status.FAILED.toString(), req.getRequestStatus());
153     }
154
155     @Test
156     public void updateRequestStatusToFailedRollbackFabric() {
157         BuildingBlock bb = new BuildingBlock().setBpmnFlowName("UnassignFabricConfigurationBB");
158         ExecuteBuildingBlock ebb = new ExecuteBuildingBlock().setBuildingBlock(bb);
159         execution.setVariable("buildingBlock", ebb);
160         execution.setVariable("mso-request-id", "123");
161         execution.setVariable("isRollbackComplete", false);
162         execution.setVariable("isRollback", true);
163         InfraActiveRequests req = new InfraActiveRequests();
164         req.setStatusMessage("PINC failure.");
165         WorkflowException wfe = new WorkflowException("processKey123", 1, "error in rollback");
166         execution.setVariable("WorkflowException", wfe);
167         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
168         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
169         workflowActionBBFailure.updateRequestStatusToFailed(execution);
170         String errorMsg = (String) execution.getVariable("RollbackErrorMessage");
171         assertEquals("error in rollback", errorMsg);
172         assertEquals(
173                 "PINC failure. Warning: The vf-module is active but configuration was not removed completely for one or more VMs.",
174                 req.getStatusMessage());
175         assertEquals(Status.FAILED.toString(), req.getRequestStatus());
176     }
177
178     @Test
179     public void updateRequestStatusToRolledback() {
180         execution.setVariable("mso-request-id", "123");
181         execution.setVariable("isRollbackComplete", true);
182         execution.setVariable("isRollback", true);
183         execution.setVariable("rollbackTargetState", "ROLLED_BACK");
184         InfraActiveRequests req = new InfraActiveRequests();
185         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
186         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
187         workflowActionBBFailure.updateRequestStatusToFailed(execution);
188         String errorMsg = (String) execution.getVariable("RollbackErrorMessage");
189         assertEquals("Rollback has been completed successfully.", errorMsg);
190         assertEquals(Status.ROLLED_BACK.toString(), req.getRequestStatus());
191     }
192
193     @Test
194     public void updateRequestStatusToRolledbackToAssigned() {
195         execution.setVariable("mso-request-id", "123");
196         execution.setVariable("isRollbackComplete", true);
197         execution.setVariable("isRollback", true);
198         execution.setVariable("rollbackTargetState", "ROLLED_BACK_TO_ASSIGNED");
199         InfraActiveRequests req = new InfraActiveRequests();
200         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
201         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
202         workflowActionBBFailure.updateRequestStatusToFailed(execution);
203         String errorMsg = (String) execution.getVariable("RollbackErrorMessage");
204         assertEquals("Rollback has been completed successfully.", errorMsg);
205         assertEquals(Status.ROLLED_BACK_TO_ASSIGNED.toString(), req.getRequestStatus());
206     }
207
208     @Test
209     public void updateRequestStatusToRolledbackToCreated() {
210         execution.setVariable("mso-request-id", "123");
211         execution.setVariable("isRollbackComplete", true);
212         execution.setVariable("isRollback", true);
213         execution.setVariable("rollbackTargetState", "ROLLED_BACK_TO_CREATED");
214         InfraActiveRequests req = new InfraActiveRequests();
215         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
216         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
217         workflowActionBBFailure.updateRequestStatusToFailed(execution);
218         String errorMsg = (String) execution.getVariable("RollbackErrorMessage");
219         assertEquals("Rollback has been completed successfully.", errorMsg);
220         assertEquals(Status.ROLLED_BACK_TO_CREATED.toString(), req.getRequestStatus());
221     }
222
223     @Test
224     public void updateRequestStatusToFailedNoWorkflowException() {
225         execution.setVariable("mso-request-id", "123");
226         execution.setVariable("isRollbackComplete", false);
227         execution.setVariable("isRollback", false);
228         execution.setVariable("WorkflowExceptionErrorMessage", "error in test case");
229         InfraActiveRequests req = new InfraActiveRequests();
230         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
231         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
232         workflowActionBBFailure.updateRequestStatusToFailed(execution);
233         String errorMsg = (String) execution.getVariable("ErrorMessage");
234         assertEquals("error in test case", errorMsg);
235     }
236
237     @Test
238     public void updateRequestErrorStatusMessageTest() {
239         String reqId = "reqId123";
240         execution.setVariable("mso-request-id", reqId);
241         WorkflowException we = new WorkflowException("WorkflowAction", 1231, "Error Case");
242         execution.setVariable("WorkflowException", we);
243
244         doReturn(reqMock).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
245         workflowActionBBFailure.updateRequestErrorStatusMessage(execution);
246         Mockito.verify(reqMock, Mockito.times(1)).setStatusMessage("Error Case");
247         Mockito.verify(reqMock, Mockito.times(1)).setProgress(Long.valueOf(100));
248         Mockito.verify(reqMock, Mockito.times(1)).setLastModifiedBy("CamundaBPMN");
249         Mockito.verify(reqMock, Mockito.times(1)).setEndTime(any(Timestamp.class));
250     }
251
252     @Test
253     public void updateRequestRollbackErrorStatusMessageTest() {
254         String reqId = "reqId123";
255         execution.setVariable("mso-request-id", reqId);
256         WorkflowException we = new WorkflowException("WorkflowAction", 1231, "Error Case");
257         execution.setVariable("WorkflowException", we);
258         execution.setVariable("isRollback", true);
259
260         doReturn(reqMock).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
261         workflowActionBBFailure.updateRequestErrorStatusMessage(execution);
262         Mockito.verify(reqMock, Mockito.times(0)).setStatusMessage("Error Case");
263         Mockito.verify(reqMock, Mockito.times(1)).setRollbackStatusMessage("Error Case");
264         Mockito.verify(reqMock, Mockito.times(1)).setProgress(Long.valueOf(100));
265         Mockito.verify(reqMock, Mockito.times(1)).setLastModifiedBy("CamundaBPMN");
266         Mockito.verify(reqMock, Mockito.times(1)).setEndTime(any(Timestamp.class));
267     }
268
269     @Test
270     public void updateRequestNotRollbackErrorStatusMessageTest() {
271         String reqId = "reqId123";
272         execution.setVariable("mso-request-id", reqId);
273         WorkflowException we = new WorkflowException("WorkflowAction", 1231, "Error Case");
274         execution.setVariable("WorkflowException", we);
275         execution.setVariable("isRollback", false);
276
277         doReturn(reqMock).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
278         workflowActionBBFailure.updateRequestErrorStatusMessage(execution);
279         Mockito.verify(reqMock, Mockito.times(1)).setStatusMessage("Error Case");
280         Mockito.verify(reqMock, Mockito.times(0)).setRollbackStatusMessage("Error Case");
281         Mockito.verify(reqMock, Mockito.times(1)).setProgress(Long.valueOf(100));
282         Mockito.verify(reqMock, Mockito.times(1)).setLastModifiedBy("CamundaBPMN");
283         Mockito.verify(reqMock, Mockito.times(1)).setEndTime(any(Timestamp.class));
284     }
285
286     @Test
287     public void invokeSendMessageForChildServiceRollBackCompletedSuccessfully() {
288         String parentCorrelationId = "parentCorrelationId";
289         DelegateExecution mockExecution = Mockito.mock(DelegateExecution.class);
290         ProcessEngineServices processEngineServices = mock(ProcessEngineServices.class);
291         RuntimeService runtimeService = mock(RuntimeService.class);
292         MessageCorrelationBuilder messageCorrelationBuilder = mock(MessageCorrelationBuilder.class);
293         when(processEngineServices.getRuntimeService()).thenReturn(runtimeService);
294         when(runtimeService.createMessageCorrelation(anyString())).thenReturn(messageCorrelationBuilder);
295         when(messageCorrelationBuilder.setVariable(CHILD_SVC_REQ_STATUS, "FAILED"))
296                 .thenReturn(messageCorrelationBuilder);
297         when(messageCorrelationBuilder.setVariable(CHILD_SVC_REQ_ERROR, "Rollback has been completed successfully."))
298                 .thenReturn(messageCorrelationBuilder);
299         when(messageCorrelationBuilder.processInstanceVariableEquals(CHILD_SVC_REQ_CORRELATION_ID, parentCorrelationId))
300                 .thenReturn(messageCorrelationBuilder);
301
302         when(mockExecution.getVariable(PARENT_CORRELATION_ID)).thenReturn(parentCorrelationId);
303         when(mockExecution.getVariable("mso-request-id")).thenReturn("123");
304         when(mockExecution.getVariable("isRollbackComplete")).thenReturn(true);
305         when(mockExecution.getVariable("isRollback")).thenReturn(true);
306         when(mockExecution.getVariable(IS_CHILD_PROCESS)).thenReturn(true);
307
308         when(mockExecution.getProcessEngineServices()).thenReturn(processEngineServices);
309
310         InfraActiveRequests req = new InfraActiveRequests();
311         WorkflowException wfe = new WorkflowException("processKey123", 1, "error in rollback");
312         when(mockExecution.getVariable("WorkflowException")).thenReturn(wfe);
313         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
314         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
315         workflowActionBBFailure.updateRequestStatusToFailed(mockExecution);
316
317         verify(messageCorrelationBuilder).setVariable(CHILD_SVC_REQ_STATUS, "FAILED");
318         verify(messageCorrelationBuilder).setVariable(CHILD_SVC_REQ_ERROR,
319                 "Rollback has been completed successfully.");
320         verify(messageCorrelationBuilder).processInstanceVariableEquals(CHILD_SVC_REQ_CORRELATION_ID,
321                 parentCorrelationId);
322     }
323
324     @Test
325     public void invokeSendMessageForChildServiceRollBackFailure() {
326         String parentCorrelationId = "parentCorrelationId";
327         DelegateExecution mockExecution = Mockito.mock(DelegateExecution.class);
328         ProcessEngineServices processEngineServices = mock(ProcessEngineServices.class);
329         RuntimeService runtimeService = mock(RuntimeService.class);
330         MessageCorrelationBuilder messageCorrelationBuilder = mock(MessageCorrelationBuilder.class);
331         when(processEngineServices.getRuntimeService()).thenReturn(runtimeService);
332         when(runtimeService.createMessageCorrelation(anyString())).thenReturn(messageCorrelationBuilder);
333         when(messageCorrelationBuilder.setVariable(CHILD_SVC_REQ_STATUS, "FAILED"))
334                 .thenReturn(messageCorrelationBuilder);
335         when(messageCorrelationBuilder.setVariable(CHILD_SVC_REQ_ERROR, "error in rollback"))
336                 .thenReturn(messageCorrelationBuilder);
337         when(messageCorrelationBuilder.processInstanceVariableEquals(PARENT_CORRELATION_ID, parentCorrelationId))
338                 .thenReturn(messageCorrelationBuilder);
339
340         when(mockExecution.getVariable(PARENT_CORRELATION_ID)).thenReturn(parentCorrelationId);
341         when(mockExecution.getVariable("mso-request-id")).thenReturn("123");
342         when(mockExecution.getVariable("isRollbackComplete")).thenReturn(false);
343         when(mockExecution.getVariable("isRollback")).thenReturn(true);
344         when(mockExecution.getVariable(IS_CHILD_PROCESS)).thenReturn(true);
345
346         when(mockExecution.getProcessEngineServices()).thenReturn(processEngineServices);
347
348         InfraActiveRequests req = new InfraActiveRequests();
349         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
350         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
351         workflowActionBBFailure.updateRequestStatusToFailed(mockExecution);
352
353         verify(messageCorrelationBuilder).setVariable(CHILD_SVC_REQ_STATUS, "FAILED");
354         verify(messageCorrelationBuilder).setVariable(CHILD_SVC_REQ_ERROR,
355                 "Failed to determine rollback error message.");
356
357         WorkflowException wfe = new WorkflowException("processKey123", 1, "error in rollback");
358         when(mockExecution.getVariable("WorkflowException")).thenReturn(wfe);
359         workflowActionBBFailure.updateRequestStatusToFailed(mockExecution);
360         verify(messageCorrelationBuilder).setVariable(CHILD_SVC_REQ_ERROR, "error in rollback");
361         verify(messageCorrelationBuilder).processInstanceVariableEquals(CHILD_SVC_REQ_CORRELATION_ID,
362                 parentCorrelationId);
363     }
364
365     @Test
366     public void invokeSendMessageForChildServiceNoRollBack() {
367         String parentCorrelationId = "parentCorrelationId";
368         DelegateExecution mockExecution = Mockito.mock(DelegateExecution.class);
369         ProcessEngineServices processEngineServices = mock(ProcessEngineServices.class);
370         RuntimeService runtimeService = mock(RuntimeService.class);
371         MessageCorrelationBuilder messageCorrelationBuilder = mock(MessageCorrelationBuilder.class);
372         when(processEngineServices.getRuntimeService()).thenReturn(runtimeService);
373         when(runtimeService.createMessageCorrelation(anyString())).thenReturn(messageCorrelationBuilder);
374         when(messageCorrelationBuilder.setVariable(CHILD_SVC_REQ_STATUS, "FAILED"))
375                 .thenReturn(messageCorrelationBuilder);
376         when(messageCorrelationBuilder.setVariable(CHILD_SVC_REQ_ERROR, "error in rollback"))
377                 .thenReturn(messageCorrelationBuilder);
378         when(messageCorrelationBuilder.processInstanceVariableEquals(PARENT_CORRELATION_ID, parentCorrelationId))
379                 .thenReturn(messageCorrelationBuilder);
380
381         when(mockExecution.getVariable(PARENT_CORRELATION_ID)).thenReturn(parentCorrelationId);
382         when(mockExecution.getVariable("mso-request-id")).thenReturn("123");
383         when(mockExecution.getVariable("isRollbackComplete")).thenReturn(false);
384         when(mockExecution.getVariable("isRollback")).thenReturn(false);
385         when(mockExecution.getVariable(IS_CHILD_PROCESS)).thenReturn(true);
386
387         when(mockExecution.getProcessEngineServices()).thenReturn(processEngineServices);
388
389         InfraActiveRequests req = new InfraActiveRequests();
390         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
391         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
392         workflowActionBBFailure.updateRequestStatusToFailed(mockExecution);
393
394         verify(messageCorrelationBuilder).setVariable(CHILD_SVC_REQ_STATUS, "FAILED");
395         verify(messageCorrelationBuilder).setVariable(CHILD_SVC_REQ_ERROR, "Failed to determine error message");
396
397         WorkflowException wfe = new WorkflowException("processKey123", 1, "error in rollback");
398         when(mockExecution.getVariable("WorkflowException")).thenReturn(wfe);
399         workflowActionBBFailure.updateRequestStatusToFailed(mockExecution);
400         verify(messageCorrelationBuilder).setVariable(CHILD_SVC_REQ_ERROR, "error in rollback");
401         verify(messageCorrelationBuilder).processInstanceVariableEquals(CHILD_SVC_REQ_CORRELATION_ID,
402                 parentCorrelationId);
403     }
404
405
406
407 }