2d48d02774b017e016f14c0326b8351138290824
[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.constants.Status;
45 import org.onap.so.db.request.beans.InfraActiveRequests;
46
47 public class WorkflowActionBBFailureTest extends BaseTaskTest {
48
49     @Mock
50     protected WorkflowAction workflowAction;
51
52     @InjectMocks
53     @Spy
54     protected WorkflowActionBBFailure workflowActionBBFailure;
55
56     @Mock
57     InfraActiveRequests reqMock;
58
59     private DelegateExecution execution;
60
61     @Rule
62     public ExpectedException thrown = ExpectedException.none();
63
64     @Before
65     public void before() throws Exception {
66         execution = new DelegateExecutionFake();
67         org.onap.aai.domain.yang.ServiceInstance servInstance = new org.onap.aai.domain.yang.ServiceInstance();
68         servInstance.setServiceInstanceId("TEST");
69         when(bbSetupUtils.getAAIServiceInstanceByName(anyString(), isA(Customer.class))).thenReturn(servInstance);
70         workflowAction.setBbInputSetupUtils(bbSetupUtils);
71         workflowAction.setBbInputSetup(bbInputSetup);
72     }
73
74     @Test
75     public void updateRequestStatusToFailed_Null_Rollback() {
76         String reqId = "reqId123";
77         execution.setVariable("mso-request-id", reqId);
78         execution.setVariable("retryCount", 3);
79         execution.setVariable("handlingCode", "Success");
80         execution.setVariable("gCurrentSequence", 1);
81         WorkflowException we = new WorkflowException("WorkflowAction", 1231, "Error Case");
82         execution.setVariable("WorkflowException", we);
83
84         doReturn(reqMock).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
85         workflowActionBBFailure.updateRequestStatusToFailed(execution);
86         Mockito.verify(reqMock, Mockito.times(1)).setStatusMessage("Error Case");
87         Mockito.verify(reqMock, Mockito.times(1)).setRequestStatus("FAILED");
88         Mockito.verify(reqMock, Mockito.times(1)).setProgress(Long.valueOf(100));
89         Mockito.verify(reqMock, Mockito.times(1)).setLastModifiedBy("CamundaBPMN");
90         Mockito.verify(reqMock, Mockito.times(1)).setEndTime(any(Timestamp.class));
91     }
92
93     @Test
94     public void updateRequestStatusToFailed() {
95         execution.setVariable("mso-request-id", "123");
96         execution.setVariable("isRollbackComplete", false);
97         execution.setVariable("isRollback", false);
98         InfraActiveRequests req = new InfraActiveRequests();
99         WorkflowException wfe = new WorkflowException("processKey123", 1, "error in test case");
100         execution.setVariable("WorkflowException", wfe);
101         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
102         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
103         workflowActionBBFailure.updateRequestStatusToFailed(execution);
104         String errorMsg = (String) execution.getVariable("ErrorMessage");
105         assertEquals("error in test case", errorMsg);
106         assertEquals(Status.FAILED.toString(), req.getRequestStatus());
107     }
108
109     @Test
110     public void updateRequestStatusToAborted() {
111         execution.setVariable("mso-request-id", "123");
112         execution.setVariable("isRollbackComplete", false);
113         execution.setVariable("isRollback", false);
114         execution.setVariable("handlingCode", "Abort");
115         InfraActiveRequests req = new InfraActiveRequests();
116         WorkflowException wfe = new WorkflowException("processKey123", 1, "error in test case");
117         execution.setVariable("WorkflowException", wfe);
118         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
119         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
120         workflowActionBBFailure.updateRequestStatusToFailed(execution);
121         String errorMsg = (String) execution.getVariable("ErrorMessage");
122         assertEquals("error in test case", errorMsg);
123         assertEquals(Status.ABORTED.toString(), req.getRequestStatus());
124     }
125
126     @Test
127     public void updateRequestStatusToFailedRollback() {
128         execution.setVariable("mso-request-id", "123");
129         execution.setVariable("isRollbackComplete", false);
130         execution.setVariable("isRollback", true);
131         InfraActiveRequests req = new InfraActiveRequests();
132         WorkflowException wfe = new WorkflowException("processKey123", 1, "error in rollback");
133         execution.setVariable("WorkflowException", wfe);
134         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
135         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
136         workflowActionBBFailure.updateRequestStatusToFailed(execution);
137         String errorMsg = (String) execution.getVariable("RollbackErrorMessage");
138         assertEquals("error in rollback", errorMsg);
139         assertEquals(Status.FAILED.toString(), req.getRequestStatus());
140     }
141
142     @Test
143     public void updateRequestStatusToRolledback() {
144         execution.setVariable("mso-request-id", "123");
145         execution.setVariable("isRollbackComplete", true);
146         execution.setVariable("isRollback", true);
147         execution.setVariable("rollbackTargetState", "ROLLED_BACK");
148         InfraActiveRequests req = new InfraActiveRequests();
149         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
150         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
151         workflowActionBBFailure.updateRequestStatusToFailed(execution);
152         String errorMsg = (String) execution.getVariable("RollbackErrorMessage");
153         assertEquals("Rollback has been completed successfully.", errorMsg);
154         assertEquals(Status.ROLLED_BACK.toString(), req.getRequestStatus());
155     }
156
157     @Test
158     public void updateRequestStatusToRolledbackToAssigned() {
159         execution.setVariable("mso-request-id", "123");
160         execution.setVariable("isRollbackComplete", true);
161         execution.setVariable("isRollback", true);
162         execution.setVariable("rollbackTargetState", "ROLLED_BACK_TO_ASSIGNED");
163         InfraActiveRequests req = new InfraActiveRequests();
164         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
165         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
166         workflowActionBBFailure.updateRequestStatusToFailed(execution);
167         String errorMsg = (String) execution.getVariable("RollbackErrorMessage");
168         assertEquals("Rollback has been completed successfully.", errorMsg);
169         assertEquals(Status.ROLLED_BACK_TO_ASSIGNED.toString(), req.getRequestStatus());
170     }
171
172     @Test
173     public void updateRequestStatusToRolledbackToCreated() {
174         execution.setVariable("mso-request-id", "123");
175         execution.setVariable("isRollbackComplete", true);
176         execution.setVariable("isRollback", true);
177         execution.setVariable("rollbackTargetState", "ROLLED_BACK_TO_CREATED");
178         InfraActiveRequests req = new InfraActiveRequests();
179         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
180         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
181         workflowActionBBFailure.updateRequestStatusToFailed(execution);
182         String errorMsg = (String) execution.getVariable("RollbackErrorMessage");
183         assertEquals("Rollback has been completed successfully.", errorMsg);
184         assertEquals(Status.ROLLED_BACK_TO_CREATED.toString(), req.getRequestStatus());
185     }
186
187     @Test
188     public void updateRequestStatusToFailedNoWorkflowException() {
189         execution.setVariable("mso-request-id", "123");
190         execution.setVariable("isRollbackComplete", false);
191         execution.setVariable("isRollback", false);
192         execution.setVariable("WorkflowExceptionErrorMessage", "error in test case");
193         InfraActiveRequests req = new InfraActiveRequests();
194         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
195         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
196         workflowActionBBFailure.updateRequestStatusToFailed(execution);
197         String errorMsg = (String) execution.getVariable("ErrorMessage");
198         assertEquals("error in test case", errorMsg);
199     }
200
201     @Test
202     public void updateRequestErrorStatusMessageTest() {
203         String reqId = "reqId123";
204         execution.setVariable("mso-request-id", reqId);
205         WorkflowException we = new WorkflowException("WorkflowAction", 1231, "Error Case");
206         execution.setVariable("WorkflowException", we);
207
208         doReturn(reqMock).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
209         workflowActionBBFailure.updateRequestErrorStatusMessage(execution);
210         Mockito.verify(reqMock, Mockito.times(1)).setStatusMessage("Error Case");
211         Mockito.verify(reqMock, Mockito.times(1)).setProgress(Long.valueOf(100));
212         Mockito.verify(reqMock, Mockito.times(1)).setLastModifiedBy("CamundaBPMN");
213         Mockito.verify(reqMock, Mockito.times(1)).setEndTime(any(Timestamp.class));
214     }
215
216     @Test
217     public void updateRequestRollbackErrorStatusMessageTest() {
218         String reqId = "reqId123";
219         execution.setVariable("mso-request-id", reqId);
220         WorkflowException we = new WorkflowException("WorkflowAction", 1231, "Error Case");
221         execution.setVariable("WorkflowException", we);
222         execution.setVariable("isRollback", true);
223
224         doReturn(reqMock).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
225         workflowActionBBFailure.updateRequestErrorStatusMessage(execution);
226         Mockito.verify(reqMock, Mockito.times(0)).setStatusMessage("Error Case");
227         Mockito.verify(reqMock, Mockito.times(1)).setRollbackStatusMessage("Error Case");
228         Mockito.verify(reqMock, Mockito.times(1)).setProgress(Long.valueOf(100));
229         Mockito.verify(reqMock, Mockito.times(1)).setLastModifiedBy("CamundaBPMN");
230         Mockito.verify(reqMock, Mockito.times(1)).setEndTime(any(Timestamp.class));
231     }
232
233     @Test
234     public void updateRequestNotRollbackErrorStatusMessageTest() {
235         String reqId = "reqId123";
236         execution.setVariable("mso-request-id", reqId);
237         WorkflowException we = new WorkflowException("WorkflowAction", 1231, "Error Case");
238         execution.setVariable("WorkflowException", we);
239         execution.setVariable("isRollback", false);
240
241         doReturn(reqMock).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
242         workflowActionBBFailure.updateRequestErrorStatusMessage(execution);
243         Mockito.verify(reqMock, Mockito.times(1)).setStatusMessage("Error Case");
244         Mockito.verify(reqMock, Mockito.times(0)).setRollbackStatusMessage("Error Case");
245         Mockito.verify(reqMock, Mockito.times(1)).setProgress(Long.valueOf(100));
246         Mockito.verify(reqMock, Mockito.times(1)).setLastModifiedBy("CamundaBPMN");
247         Mockito.verify(reqMock, Mockito.times(1)).setEndTime(any(Timestamp.class));
248     }
249 }