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.constants.Status;
 
  45 import org.onap.so.db.request.beans.InfraActiveRequests;
 
  47 public class WorkflowActionBBFailureTest extends BaseTaskTest {
 
  50     protected WorkflowAction workflowAction;
 
  54     protected WorkflowActionBBFailure workflowActionBBFailure;
 
  57     InfraActiveRequests reqMock;
 
  59     private DelegateExecution execution;
 
  62     public ExpectedException thrown = ExpectedException.none();
 
  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);
 
  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);
 
  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));
 
  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());
 
 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());
 
 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());
 
 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());
 
 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());
 
 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());
 
 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);
 
 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);
 
 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));
 
 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);
 
 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));
 
 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);
 
 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));