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