52a2cf722383e014556de3f1d8ef693b0de0be87
[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.anyString;
25 import static org.mockito.ArgumentMatchers.isA;
26 import static org.mockito.ArgumentMatchers.any;
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.After;
34 import org.junit.Before;
35 import org.junit.Rule;
36 import org.junit.Test;
37 import org.junit.rules.ExpectedException;
38 import org.mockito.InjectMocks;
39 import org.mockito.Mock;
40 import org.mockito.Mockito;
41 import org.mockito.Spy;
42 import org.onap.so.bpmn.BaseTaskTest;
43 import org.onap.so.bpmn.core.WorkflowException;
44 import org.onap.so.bpmn.servicedecomposition.bbobjects.Customer;
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     }
107
108     @Test
109     public void updateRequestStatusToFailedRollback() {
110         execution.setVariable("mso-request-id", "123");
111         execution.setVariable("isRollbackComplete", false);
112         execution.setVariable("isRollback", true);
113         InfraActiveRequests req = new InfraActiveRequests();
114         WorkflowException wfe = new WorkflowException("processKey123", 1, "error in rollback");
115         execution.setVariable("WorkflowException", wfe);
116         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
117         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
118         workflowActionBBFailure.updateRequestStatusToFailed(execution);
119         String errorMsg = (String) execution.getVariable("RollbackErrorMessage");
120         assertEquals("error in rollback", errorMsg);
121     }
122
123     @Test
124     public void updateRequestStatusToFailedRollbackCompleted() {
125         execution.setVariable("mso-request-id", "123");
126         execution.setVariable("isRollbackComplete", true);
127         execution.setVariable("isRollback", true);
128         InfraActiveRequests req = new InfraActiveRequests();
129         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
130         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
131         workflowActionBBFailure.updateRequestStatusToFailed(execution);
132         String errorMsg = (String) execution.getVariable("RollbackErrorMessage");
133         assertEquals("Rollback has been completed successfully.", errorMsg);
134     }
135
136     @Test
137     public void updateRequestStatusToFailedNoWorkflowException() {
138         execution.setVariable("mso-request-id", "123");
139         execution.setVariable("isRollbackComplete", false);
140         execution.setVariable("isRollback", false);
141         execution.setVariable("WorkflowExceptionErrorMessage", "error in test case");
142         InfraActiveRequests req = new InfraActiveRequests();
143         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
144         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
145         workflowActionBBFailure.updateRequestStatusToFailed(execution);
146         String errorMsg = (String) execution.getVariable("ErrorMessage");
147         assertEquals("error in test case", errorMsg);
148     }
149
150     @Test
151     public void updateRequestErrorStatusMessageTest() {
152         String reqId = "reqId123";
153         execution.setVariable("mso-request-id", reqId);
154         WorkflowException we = new WorkflowException("WorkflowAction", 1231, "Error Case");
155         execution.setVariable("WorkflowException", we);
156
157         doReturn(reqMock).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
158         workflowActionBBFailure.updateRequestErrorStatusMessage(execution);
159         Mockito.verify(reqMock, Mockito.times(1)).setStatusMessage("Error Case");
160         Mockito.verify(reqMock, Mockito.times(1)).setRequestStatus("FAILED");
161         Mockito.verify(reqMock, Mockito.times(1)).setProgress(Long.valueOf(100));
162         Mockito.verify(reqMock, Mockito.times(1)).setLastModifiedBy("CamundaBPMN");
163         Mockito.verify(reqMock, Mockito.times(1)).setEndTime(any(Timestamp.class));
164     }
165 }