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