Bump the version for the SO core to Kohn
[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.when;
30 import java.sql.Timestamp;
31 import org.camunda.bpm.engine.delegate.DelegateExecution;
32 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
33 import org.junit.Before;
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.junit.rules.ExpectedException;
37 import org.mockito.InjectMocks;
38 import org.mockito.Mock;
39 import org.mockito.Mockito;
40 import org.mockito.Spy;
41 import org.onap.so.bpmn.BaseTaskTest;
42 import org.onap.so.bpmn.core.WorkflowException;
43 import org.onap.so.bpmn.servicedecomposition.bbobjects.Customer;
44 import org.onap.so.bpmn.servicedecomposition.entities.BuildingBlock;
45 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
46 import org.onap.so.constants.Status;
47 import org.onap.so.db.request.beans.InfraActiveRequests;
48
49 public class WorkflowActionBBFailureTest extends BaseTaskTest {
50
51     @Mock
52     protected WorkflowAction workflowAction;
53
54     @InjectMocks
55     @Spy
56     protected WorkflowActionBBFailure workflowActionBBFailure;
57
58     @Mock
59     InfraActiveRequests reqMock;
60
61     private DelegateExecution execution;
62
63     @Rule
64     public ExpectedException thrown = ExpectedException.none();
65
66     @Before
67     public void before() throws Exception {
68         execution = new DelegateExecutionFake();
69         org.onap.aai.domain.yang.ServiceInstance servInstance = new org.onap.aai.domain.yang.ServiceInstance();
70         servInstance.setServiceInstanceId("TEST");
71         when(bbSetupUtils.getAAIServiceInstanceByName(anyString(), isA(Customer.class))).thenReturn(servInstance);
72         workflowAction.setBbInputSetupUtils(bbSetupUtils);
73         workflowAction.setBbInputSetup(bbInputSetup);
74     }
75
76     @Test
77     public void updateRequestStatusToFailed_Null_Rollback() {
78         String reqId = "reqId123";
79         execution.setVariable("mso-request-id", reqId);
80         execution.setVariable("retryCount", 3);
81         execution.setVariable("handlingCode", "Success");
82         execution.setVariable("gCurrentSequence", 1);
83         WorkflowException we = new WorkflowException("WorkflowAction", 1231, "Error Case");
84         execution.setVariable("WorkflowException", we);
85
86         doReturn(reqMock).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
87         workflowActionBBFailure.updateRequestStatusToFailed(execution);
88         Mockito.verify(reqMock, Mockito.times(1)).setStatusMessage("Error Case");
89         Mockito.verify(reqMock, Mockito.times(1)).setRequestStatus("FAILED");
90         Mockito.verify(reqMock, Mockito.times(1)).setProgress(Long.valueOf(100));
91         Mockito.verify(reqMock, Mockito.times(1)).setLastModifiedBy("CamundaBPMN");
92         Mockito.verify(reqMock, Mockito.times(1)).setEndTime(any(Timestamp.class));
93     }
94
95     @Test
96     public void updateRequestStatusToFailed() {
97         execution.setVariable("mso-request-id", "123");
98         execution.setVariable("isRollbackComplete", false);
99         execution.setVariable("isRollback", false);
100         InfraActiveRequests req = new InfraActiveRequests();
101         WorkflowException wfe = new WorkflowException("processKey123", 1, "error in test case");
102         execution.setVariable("WorkflowException", wfe);
103         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
104         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
105         workflowActionBBFailure.updateRequestStatusToFailed(execution);
106         String errorMsg = (String) execution.getVariable("ErrorMessage");
107         assertEquals("error in test case", errorMsg);
108         assertEquals(Status.FAILED.toString(), req.getRequestStatus());
109     }
110
111     @Test
112     public void updateRequestStatusToAborted() {
113         execution.setVariable("mso-request-id", "123");
114         execution.setVariable("isRollbackComplete", false);
115         execution.setVariable("isRollback", false);
116         execution.setVariable("handlingCode", "Abort");
117         InfraActiveRequests req = new InfraActiveRequests();
118         WorkflowException wfe = new WorkflowException("processKey123", 1, "error in test case");
119         execution.setVariable("WorkflowException", wfe);
120         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
121         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
122         workflowActionBBFailure.updateRequestStatusToFailed(execution);
123         String errorMsg = (String) execution.getVariable("ErrorMessage");
124         assertEquals("error in test case", errorMsg);
125         assertEquals(Status.ABORTED.toString(), req.getRequestStatus());
126     }
127
128     @Test
129     public void updateRequestStatusToFailedRollback() {
130         execution.setVariable("mso-request-id", "123");
131         execution.setVariable("isRollbackComplete", false);
132         execution.setVariable("isRollback", true);
133         InfraActiveRequests req = new InfraActiveRequests();
134         WorkflowException wfe = new WorkflowException("processKey123", 1, "error in rollback");
135         execution.setVariable("WorkflowException", wfe);
136         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
137         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
138         workflowActionBBFailure.updateRequestStatusToFailed(execution);
139         String errorMsg = (String) execution.getVariable("RollbackErrorMessage");
140         assertEquals("error in rollback", errorMsg);
141         assertEquals(Status.FAILED.toString(), req.getRequestStatus());
142     }
143
144     @Test
145     public void updateRequestStatusToFailedRollbackFabric() {
146         BuildingBlock bb = new BuildingBlock().setBpmnFlowName("UnassignFabricConfigurationBB");
147         ExecuteBuildingBlock ebb = new ExecuteBuildingBlock().setBuildingBlock(bb);
148         execution.setVariable("buildingBlock", ebb);
149         execution.setVariable("mso-request-id", "123");
150         execution.setVariable("isRollbackComplete", false);
151         execution.setVariable("isRollback", true);
152         InfraActiveRequests req = new InfraActiveRequests();
153         req.setStatusMessage("PINC failure.");
154         WorkflowException wfe = new WorkflowException("processKey123", 1, "error in rollback");
155         execution.setVariable("WorkflowException", wfe);
156         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
157         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
158         workflowActionBBFailure.updateRequestStatusToFailed(execution);
159         String errorMsg = (String) execution.getVariable("RollbackErrorMessage");
160         assertEquals("error in rollback", errorMsg);
161         assertEquals(
162                 "PINC failure. Warning: The vf-module is active but configuration was not removed completely for one or more VMs.",
163                 req.getStatusMessage());
164         assertEquals(Status.FAILED.toString(), req.getRequestStatus());
165     }
166
167     @Test
168     public void updateRequestStatusToRolledback() {
169         execution.setVariable("mso-request-id", "123");
170         execution.setVariable("isRollbackComplete", true);
171         execution.setVariable("isRollback", true);
172         execution.setVariable("rollbackTargetState", "ROLLED_BACK");
173         InfraActiveRequests req = new InfraActiveRequests();
174         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
175         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
176         workflowActionBBFailure.updateRequestStatusToFailed(execution);
177         String errorMsg = (String) execution.getVariable("RollbackErrorMessage");
178         assertEquals("Rollback has been completed successfully.", errorMsg);
179         assertEquals(Status.ROLLED_BACK.toString(), req.getRequestStatus());
180     }
181
182     @Test
183     public void updateRequestStatusToRolledbackToAssigned() {
184         execution.setVariable("mso-request-id", "123");
185         execution.setVariable("isRollbackComplete", true);
186         execution.setVariable("isRollback", true);
187         execution.setVariable("rollbackTargetState", "ROLLED_BACK_TO_ASSIGNED");
188         InfraActiveRequests req = new InfraActiveRequests();
189         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
190         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
191         workflowActionBBFailure.updateRequestStatusToFailed(execution);
192         String errorMsg = (String) execution.getVariable("RollbackErrorMessage");
193         assertEquals("Rollback has been completed successfully.", errorMsg);
194         assertEquals(Status.ROLLED_BACK_TO_ASSIGNED.toString(), req.getRequestStatus());
195     }
196
197     @Test
198     public void updateRequestStatusToRolledbackToCreated() {
199         execution.setVariable("mso-request-id", "123");
200         execution.setVariable("isRollbackComplete", true);
201         execution.setVariable("isRollback", true);
202         execution.setVariable("rollbackTargetState", "ROLLED_BACK_TO_CREATED");
203         InfraActiveRequests req = new InfraActiveRequests();
204         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
205         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
206         workflowActionBBFailure.updateRequestStatusToFailed(execution);
207         String errorMsg = (String) execution.getVariable("RollbackErrorMessage");
208         assertEquals("Rollback has been completed successfully.", errorMsg);
209         assertEquals(Status.ROLLED_BACK_TO_CREATED.toString(), req.getRequestStatus());
210     }
211
212     @Test
213     public void updateRequestStatusToFailedNoWorkflowException() {
214         execution.setVariable("mso-request-id", "123");
215         execution.setVariable("isRollbackComplete", false);
216         execution.setVariable("isRollback", false);
217         execution.setVariable("WorkflowExceptionErrorMessage", "error in test case");
218         InfraActiveRequests req = new InfraActiveRequests();
219         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
220         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
221         workflowActionBBFailure.updateRequestStatusToFailed(execution);
222         String errorMsg = (String) execution.getVariable("ErrorMessage");
223         assertEquals("error in test case", errorMsg);
224     }
225
226     @Test
227     public void updateRequestErrorStatusMessageTest() {
228         String reqId = "reqId123";
229         execution.setVariable("mso-request-id", reqId);
230         WorkflowException we = new WorkflowException("WorkflowAction", 1231, "Error Case");
231         execution.setVariable("WorkflowException", we);
232
233         doReturn(reqMock).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
234         workflowActionBBFailure.updateRequestErrorStatusMessage(execution);
235         Mockito.verify(reqMock, Mockito.times(1)).setStatusMessage("Error Case");
236         Mockito.verify(reqMock, Mockito.times(1)).setProgress(Long.valueOf(100));
237         Mockito.verify(reqMock, Mockito.times(1)).setLastModifiedBy("CamundaBPMN");
238         Mockito.verify(reqMock, Mockito.times(1)).setEndTime(any(Timestamp.class));
239     }
240
241     @Test
242     public void updateRequestRollbackErrorStatusMessageTest() {
243         String reqId = "reqId123";
244         execution.setVariable("mso-request-id", reqId);
245         WorkflowException we = new WorkflowException("WorkflowAction", 1231, "Error Case");
246         execution.setVariable("WorkflowException", we);
247         execution.setVariable("isRollback", true);
248
249         doReturn(reqMock).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
250         workflowActionBBFailure.updateRequestErrorStatusMessage(execution);
251         Mockito.verify(reqMock, Mockito.times(0)).setStatusMessage("Error Case");
252         Mockito.verify(reqMock, Mockito.times(1)).setRollbackStatusMessage("Error Case");
253         Mockito.verify(reqMock, Mockito.times(1)).setProgress(Long.valueOf(100));
254         Mockito.verify(reqMock, Mockito.times(1)).setLastModifiedBy("CamundaBPMN");
255         Mockito.verify(reqMock, Mockito.times(1)).setEndTime(any(Timestamp.class));
256     }
257
258     @Test
259     public void updateRequestNotRollbackErrorStatusMessageTest() {
260         String reqId = "reqId123";
261         execution.setVariable("mso-request-id", reqId);
262         WorkflowException we = new WorkflowException("WorkflowAction", 1231, "Error Case");
263         execution.setVariable("WorkflowException", we);
264         execution.setVariable("isRollback", false);
265
266         doReturn(reqMock).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
267         workflowActionBBFailure.updateRequestErrorStatusMessage(execution);
268         Mockito.verify(reqMock, Mockito.times(1)).setStatusMessage("Error Case");
269         Mockito.verify(reqMock, Mockito.times(0)).setRollbackStatusMessage("Error Case");
270         Mockito.verify(reqMock, Mockito.times(1)).setProgress(Long.valueOf(100));
271         Mockito.verify(reqMock, Mockito.times(1)).setLastModifiedBy("CamundaBPMN");
272         Mockito.verify(reqMock, Mockito.times(1)).setEndTime(any(Timestamp.class));
273     }
274 }