Replaced all tabs with spaces in java and pom.xml
[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.anyString;
25 import static org.mockito.ArgumentMatchers.isA;
26 import static org.mockito.Mockito.doNothing;
27 import static org.mockito.Mockito.doReturn;
28 import static org.mockito.Mockito.when;
29 import org.camunda.bpm.engine.delegate.DelegateExecution;
30 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
31 import org.junit.Before;
32 import org.junit.Rule;
33 import org.junit.Test;
34 import org.junit.rules.ExpectedException;
35 import org.mockito.InjectMocks;
36 import org.mockito.Mock;
37 import org.mockito.Mockito;
38 import org.mockito.Spy;
39 import org.onap.so.bpmn.BaseTaskTest;
40 import org.onap.so.bpmn.core.WorkflowException;
41 import org.onap.so.bpmn.servicedecomposition.bbobjects.Customer;
42 import org.onap.so.db.request.beans.InfraActiveRequests;
43
44 public class WorkflowActionBBFailureTest extends BaseTaskTest {
45
46     @Mock
47     protected WorkflowAction workflowAction;
48
49     @InjectMocks
50     @Spy
51     protected WorkflowActionBBFailure workflowActionBBFailure;
52
53     @Mock
54     InfraActiveRequests reqMock;
55
56     private DelegateExecution execution;
57
58     @Rule
59     public ExpectedException thrown = ExpectedException.none();
60
61     @Before
62     public void before() throws Exception {
63         execution = new DelegateExecutionFake();
64         org.onap.aai.domain.yang.ServiceInstance servInstance = new org.onap.aai.domain.yang.ServiceInstance();
65         servInstance.setServiceInstanceId("TEST");
66         when(bbSetupUtils.getAAIServiceInstanceByName(anyString(), isA(Customer.class))).thenReturn(servInstance);
67         workflowAction.setBbInputSetupUtils(bbSetupUtils);
68         workflowAction.setBbInputSetup(bbInputSetup);
69     }
70
71     @Test
72     public void updateRequestStatusToFailed_Null_Rollback() {
73         String reqId = "reqId123";
74         execution.setVariable("mso-request-id", reqId);
75         execution.setVariable("retryCount", 3);
76         execution.setVariable("handlingCode", "Success");
77         execution.setVariable("gCurrentSequence", 1);
78         WorkflowException we = new WorkflowException("WorkflowAction", 1231, "Error Case");
79         execution.setVariable("WorkflowException", we);
80
81         doReturn(reqMock).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
82         workflowActionBBFailure.updateRequestStatusToFailed(execution);
83         Mockito.verify(reqMock, Mockito.times(1)).setStatusMessage("Error Case");
84         Mockito.verify(reqMock, Mockito.times(1)).setRequestStatus("FAILED");
85         Mockito.verify(reqMock, Mockito.times(1)).setProgress(Long.valueOf(100));
86         Mockito.verify(reqMock, Mockito.times(1)).setLastModifiedBy("CamundaBPMN");
87     }
88
89     @Test
90     public void updateRequestStatusToFailed() {
91         execution.setVariable("mso-request-id", "123");
92         execution.setVariable("isRollbackComplete", false);
93         execution.setVariable("isRollback", false);
94         InfraActiveRequests req = new InfraActiveRequests();
95         WorkflowException wfe = new WorkflowException("processKey123", 1, "error in test case");
96         execution.setVariable("WorkflowException", wfe);
97         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
98         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
99         workflowActionBBFailure.updateRequestStatusToFailed(execution);
100         String errorMsg = (String) execution.getVariable("ErrorMessage");
101         assertEquals("error in test case", errorMsg);
102     }
103
104     @Test
105     public void updateRequestStatusToFailedRollback() {
106         execution.setVariable("mso-request-id", "123");
107         execution.setVariable("isRollbackComplete", false);
108         execution.setVariable("isRollback", true);
109         InfraActiveRequests req = new InfraActiveRequests();
110         WorkflowException wfe = new WorkflowException("processKey123", 1, "error in rollback");
111         execution.setVariable("WorkflowException", wfe);
112         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
113         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
114         workflowActionBBFailure.updateRequestStatusToFailed(execution);
115         String errorMsg = (String) execution.getVariable("RollbackErrorMessage");
116         assertEquals("error in rollback", errorMsg);
117     }
118
119     @Test
120     public void updateRequestStatusToFailedRollbackCompleted() {
121         execution.setVariable("mso-request-id", "123");
122         execution.setVariable("isRollbackComplete", true);
123         execution.setVariable("isRollback", true);
124         InfraActiveRequests req = new InfraActiveRequests();
125         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
126         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
127         workflowActionBBFailure.updateRequestStatusToFailed(execution);
128         String errorMsg = (String) execution.getVariable("RollbackErrorMessage");
129         assertEquals("Rollback has been completed successfully.", errorMsg);
130     }
131
132     @Test
133     public void updateRequestStatusToFailedNoWorkflowException() {
134         execution.setVariable("mso-request-id", "123");
135         execution.setVariable("isRollbackComplete", false);
136         execution.setVariable("isRollback", false);
137         execution.setVariable("WorkflowExceptionErrorMessage", "error in test case");
138         InfraActiveRequests req = new InfraActiveRequests();
139         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
140         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
141         workflowActionBBFailure.updateRequestStatusToFailed(execution);
142         String errorMsg = (String) execution.getVariable("ErrorMessage");
143         assertEquals("error in test case", errorMsg);
144     }
145 }