23ffbac64593551f969ff42156406425918606cd
[so.git] /
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();
148         ebb.setBuildingBlock(bb);
149         execution.setVariable("buildingBlock", ebb);
150         execution.setVariable("mso-request-id", "123");
151         execution.setVariable("isRollbackComplete", false);
152         execution.setVariable("isRollback", true);
153         InfraActiveRequests req = new InfraActiveRequests();
154         req.setStatusMessage("PINC failure.");
155         WorkflowException wfe = new WorkflowException("processKey123", 1, "error in rollback");
156         execution.setVariable("WorkflowException", wfe);
157         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
158         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
159         workflowActionBBFailure.updateRequestStatusToFailed(execution);
160         String errorMsg = (String) execution.getVariable("RollbackErrorMessage");
161         assertEquals("error in rollback", errorMsg);
162         assertEquals(
163                 "PINC failure. Warning: The vf-module is active but configuration was not removed completely for one or more VMs.",
164                 req.getStatusMessage());
165         assertEquals(Status.FAILED.toString(), req.getRequestStatus());
166     }
167
168     @Test
169     public void updateRequestStatusToRolledback() {
170         execution.setVariable("mso-request-id", "123");
171         execution.setVariable("isRollbackComplete", true);
172         execution.setVariable("isRollback", true);
173         execution.setVariable("rollbackTargetState", "ROLLED_BACK");
174         InfraActiveRequests req = new InfraActiveRequests();
175         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
176         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
177         workflowActionBBFailure.updateRequestStatusToFailed(execution);
178         String errorMsg = (String) execution.getVariable("RollbackErrorMessage");
179         assertEquals("Rollback has been completed successfully.", errorMsg);
180         assertEquals(Status.ROLLED_BACK.toString(), req.getRequestStatus());
181     }
182
183     @Test
184     public void updateRequestStatusToRolledbackToAssigned() {
185         execution.setVariable("mso-request-id", "123");
186         execution.setVariable("isRollbackComplete", true);
187         execution.setVariable("isRollback", true);
188         execution.setVariable("rollbackTargetState", "ROLLED_BACK_TO_ASSIGNED");
189         InfraActiveRequests req = new InfraActiveRequests();
190         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
191         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
192         workflowActionBBFailure.updateRequestStatusToFailed(execution);
193         String errorMsg = (String) execution.getVariable("RollbackErrorMessage");
194         assertEquals("Rollback has been completed successfully.", errorMsg);
195         assertEquals(Status.ROLLED_BACK_TO_ASSIGNED.toString(), req.getRequestStatus());
196     }
197
198     @Test
199     public void updateRequestStatusToRolledbackToCreated() {
200         execution.setVariable("mso-request-id", "123");
201         execution.setVariable("isRollbackComplete", true);
202         execution.setVariable("isRollback", true);
203         execution.setVariable("rollbackTargetState", "ROLLED_BACK_TO_CREATED");
204         InfraActiveRequests req = new InfraActiveRequests();
205         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
206         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
207         workflowActionBBFailure.updateRequestStatusToFailed(execution);
208         String errorMsg = (String) execution.getVariable("RollbackErrorMessage");
209         assertEquals("Rollback has been completed successfully.", errorMsg);
210         assertEquals(Status.ROLLED_BACK_TO_CREATED.toString(), req.getRequestStatus());
211     }
212
213     @Test
214     public void updateRequestStatusToFailedNoWorkflowException() {
215         execution.setVariable("mso-request-id", "123");
216         execution.setVariable("isRollbackComplete", false);
217         execution.setVariable("isRollback", false);
218         execution.setVariable("WorkflowExceptionErrorMessage", "error in test case");
219         InfraActiveRequests req = new InfraActiveRequests();
220         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
221         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
222         workflowActionBBFailure.updateRequestStatusToFailed(execution);
223         String errorMsg = (String) execution.getVariable("ErrorMessage");
224         assertEquals("error in test case", errorMsg);
225     }
226
227     @Test
228     public void updateRequestErrorStatusMessageTest() {
229         String reqId = "reqId123";
230         execution.setVariable("mso-request-id", reqId);
231         WorkflowException we = new WorkflowException("WorkflowAction", 1231, "Error Case");
232         execution.setVariable("WorkflowException", we);
233
234         doReturn(reqMock).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
235         workflowActionBBFailure.updateRequestErrorStatusMessage(execution);
236         Mockito.verify(reqMock, Mockito.times(1)).setStatusMessage("Error Case");
237         Mockito.verify(reqMock, Mockito.times(1)).setProgress(Long.valueOf(100));
238         Mockito.verify(reqMock, Mockito.times(1)).setLastModifiedBy("CamundaBPMN");
239         Mockito.verify(reqMock, Mockito.times(1)).setEndTime(any(Timestamp.class));
240     }
241
242     @Test
243     public void updateRequestRollbackErrorStatusMessageTest() {
244         String reqId = "reqId123";
245         execution.setVariable("mso-request-id", reqId);
246         WorkflowException we = new WorkflowException("WorkflowAction", 1231, "Error Case");
247         execution.setVariable("WorkflowException", we);
248         execution.setVariable("isRollback", true);
249
250         doReturn(reqMock).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
251         workflowActionBBFailure.updateRequestErrorStatusMessage(execution);
252         Mockito.verify(reqMock, Mockito.times(0)).setStatusMessage("Error Case");
253         Mockito.verify(reqMock, Mockito.times(1)).setRollbackStatusMessage("Error Case");
254         Mockito.verify(reqMock, Mockito.times(1)).setProgress(Long.valueOf(100));
255         Mockito.verify(reqMock, Mockito.times(1)).setLastModifiedBy("CamundaBPMN");
256         Mockito.verify(reqMock, Mockito.times(1)).setEndTime(any(Timestamp.class));
257     }
258
259     @Test
260     public void updateRequestNotRollbackErrorStatusMessageTest() {
261         String reqId = "reqId123";
262         execution.setVariable("mso-request-id", reqId);
263         WorkflowException we = new WorkflowException("WorkflowAction", 1231, "Error Case");
264         execution.setVariable("WorkflowException", we);
265         execution.setVariable("isRollback", false);
266
267         doReturn(reqMock).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
268         workflowActionBBFailure.updateRequestErrorStatusMessage(execution);
269         Mockito.verify(reqMock, Mockito.times(1)).setStatusMessage("Error Case");
270         Mockito.verify(reqMock, Mockito.times(0)).setRollbackStatusMessage("Error Case");
271         Mockito.verify(reqMock, Mockito.times(1)).setProgress(Long.valueOf(100));
272         Mockito.verify(reqMock, Mockito.times(1)).setLastModifiedBy("CamundaBPMN");
273         Mockito.verify(reqMock, Mockito.times(1)).setEndTime(any(Timestamp.class));
274     }
275 }