2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.bpmn.infrastructure.workflow.tasks;
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.db.request.beans.InfraActiveRequests;
46 public class WorkflowActionBBFailureTest extends BaseTaskTest {
49 protected WorkflowAction workflowAction;
53 protected WorkflowActionBBFailure workflowActionBBFailure;
56 InfraActiveRequests reqMock;
58 private DelegateExecution execution;
61 public ExpectedException thrown = ExpectedException.none();
64 public void before() throws Exception {
65 execution = new DelegateExecutionFake();
66 org.onap.aai.domain.yang.ServiceInstance servInstance = new org.onap.aai.domain.yang.ServiceInstance();
67 servInstance.setServiceInstanceId("TEST");
68 when(bbSetupUtils.getAAIServiceInstanceByName(anyString(), isA(Customer.class))).thenReturn(servInstance);
69 workflowAction.setBbInputSetupUtils(bbSetupUtils);
70 workflowAction.setBbInputSetup(bbInputSetup);
74 public void updateRequestStatusToFailed_Null_Rollback() {
75 String reqId = "reqId123";
76 execution.setVariable("mso-request-id", reqId);
77 execution.setVariable("retryCount", 3);
78 execution.setVariable("handlingCode", "Success");
79 execution.setVariable("gCurrentSequence", 1);
80 WorkflowException we = new WorkflowException("WorkflowAction", 1231, "Error Case");
81 execution.setVariable("WorkflowException", we);
83 doReturn(reqMock).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
84 workflowActionBBFailure.updateRequestStatusToFailed(execution);
85 Mockito.verify(reqMock, Mockito.times(1)).setStatusMessage("Error Case");
86 Mockito.verify(reqMock, Mockito.times(1)).setRequestStatus("FAILED");
87 Mockito.verify(reqMock, Mockito.times(1)).setProgress(Long.valueOf(100));
88 Mockito.verify(reqMock, Mockito.times(1)).setLastModifiedBy("CamundaBPMN");
89 Mockito.verify(reqMock, Mockito.times(1)).setEndTime(any(Timestamp.class));
93 public void updateRequestStatusToFailed() {
94 execution.setVariable("mso-request-id", "123");
95 execution.setVariable("isRollbackComplete", false);
96 execution.setVariable("isRollback", false);
97 InfraActiveRequests req = new InfraActiveRequests();
98 WorkflowException wfe = new WorkflowException("processKey123", 1, "error in test case");
99 execution.setVariable("WorkflowException", wfe);
100 doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
101 doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
102 workflowActionBBFailure.updateRequestStatusToFailed(execution);
103 String errorMsg = (String) execution.getVariable("ErrorMessage");
104 assertEquals("error in test case", errorMsg);
108 public void updateRequestStatusToFailedRollback() {
109 execution.setVariable("mso-request-id", "123");
110 execution.setVariable("isRollbackComplete", false);
111 execution.setVariable("isRollback", true);
112 InfraActiveRequests req = new InfraActiveRequests();
113 WorkflowException wfe = new WorkflowException("processKey123", 1, "error in rollback");
114 execution.setVariable("WorkflowException", wfe);
115 doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
116 doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
117 workflowActionBBFailure.updateRequestStatusToFailed(execution);
118 String errorMsg = (String) execution.getVariable("RollbackErrorMessage");
119 assertEquals("error in rollback", errorMsg);
123 public void updateRequestStatusToFailedRollbackCompleted() {
124 execution.setVariable("mso-request-id", "123");
125 execution.setVariable("isRollbackComplete", true);
126 execution.setVariable("isRollback", true);
127 InfraActiveRequests req = new InfraActiveRequests();
128 doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
129 doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
130 workflowActionBBFailure.updateRequestStatusToFailed(execution);
131 String errorMsg = (String) execution.getVariable("RollbackErrorMessage");
132 assertEquals("Rollback has been completed successfully.", errorMsg);
136 public void updateRequestStatusToFailedNoWorkflowException() {
137 execution.setVariable("mso-request-id", "123");
138 execution.setVariable("isRollbackComplete", false);
139 execution.setVariable("isRollback", false);
140 execution.setVariable("WorkflowExceptionErrorMessage", "error in test case");
141 InfraActiveRequests req = new InfraActiveRequests();
142 doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
143 doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
144 workflowActionBBFailure.updateRequestStatusToFailed(execution);
145 String errorMsg = (String) execution.getVariable("ErrorMessage");
146 assertEquals("error in test case", errorMsg);
150 public void updateRequestErrorStatusMessageTest() {
151 String reqId = "reqId123";
152 execution.setVariable("mso-request-id", reqId);
153 WorkflowException we = new WorkflowException("WorkflowAction", 1231, "Error Case");
154 execution.setVariable("WorkflowException", we);
156 doReturn(reqMock).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
157 workflowActionBBFailure.updateRequestErrorStatusMessage(execution);
158 Mockito.verify(reqMock, Mockito.times(1)).setStatusMessage("Error Case");
159 Mockito.verify(reqMock, Mockito.times(1)).setProgress(Long.valueOf(100));
160 Mockito.verify(reqMock, Mockito.times(1)).setLastModifiedBy("CamundaBPMN");
161 Mockito.verify(reqMock, Mockito.times(1)).setEndTime(any(Timestamp.class));
165 public void updateRequestRollbackErrorStatusMessageTest() {
166 String reqId = "reqId123";
167 execution.setVariable("mso-request-id", reqId);
168 WorkflowException we = new WorkflowException("WorkflowAction", 1231, "Error Case");
169 execution.setVariable("WorkflowException", we);
170 execution.setVariable("isRollback", true);
172 doReturn(reqMock).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
173 workflowActionBBFailure.updateRequestErrorStatusMessage(execution);
174 Mockito.verify(reqMock, Mockito.times(0)).setStatusMessage("Error Case");
175 Mockito.verify(reqMock, Mockito.times(1)).setRollbackStatusMessage("Error Case");
176 Mockito.verify(reqMock, Mockito.times(1)).setProgress(Long.valueOf(100));
177 Mockito.verify(reqMock, Mockito.times(1)).setLastModifiedBy("CamundaBPMN");
178 Mockito.verify(reqMock, Mockito.times(1)).setEndTime(any(Timestamp.class));
182 public void updateRequestNotRollbackErrorStatusMessageTest() {
183 String reqId = "reqId123";
184 execution.setVariable("mso-request-id", reqId);
185 WorkflowException we = new WorkflowException("WorkflowAction", 1231, "Error Case");
186 execution.setVariable("WorkflowException", we);
187 execution.setVariable("isRollback", false);
189 doReturn(reqMock).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
190 workflowActionBBFailure.updateRequestErrorStatusMessage(execution);
191 Mockito.verify(reqMock, Mockito.times(1)).setStatusMessage("Error Case");
192 Mockito.verify(reqMock, Mockito.times(0)).setRollbackStatusMessage("Error Case");
193 Mockito.verify(reqMock, Mockito.times(1)).setProgress(Long.valueOf(100));
194 Mockito.verify(reqMock, Mockito.times(1)).setLastModifiedBy("CamundaBPMN");
195 Mockito.verify(reqMock, Mockito.times(1)).setEndTime(any(Timestamp.class));