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.bpmn.servicedecomposition.entities.BuildingBlock;
 
  45 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
 
  46 import org.onap.so.constants.Status;
 
  47 import org.onap.so.db.request.beans.InfraActiveRequests;
 
  49 public class WorkflowActionBBFailureTest extends BaseTaskTest {
 
  52     protected WorkflowAction workflowAction;
 
  56     protected WorkflowActionBBFailure workflowActionBBFailure;
 
  59     InfraActiveRequests reqMock;
 
  61     private DelegateExecution execution;
 
  64     public ExpectedException thrown = ExpectedException.none();
 
  67     public void before() throws Exception {
 
  68         execution = new DelegateExecutionFake();
 
  69         org.onap.aai.domain.yang.ServiceInstance servInstance = new org.onap.aai.domain.yang.ServiceInstance();
 
  70         servInstance.setServiceInstanceId("TEST");
 
  71         when(bbSetupUtils.getAAIServiceInstanceByName(anyString(), isA(Customer.class))).thenReturn(servInstance);
 
  72         workflowAction.setBbInputSetupUtils(bbSetupUtils);
 
  73         workflowAction.setBbInputSetup(bbInputSetup);
 
  77     public void updateRequestStatusToFailed_Null_Rollback() {
 
  78         String reqId = "reqId123";
 
  79         execution.setVariable("mso-request-id", reqId);
 
  80         execution.setVariable("retryCount", 3);
 
  81         execution.setVariable("handlingCode", "Success");
 
  82         execution.setVariable("gCurrentSequence", 1);
 
  83         WorkflowException we = new WorkflowException("WorkflowAction", 1231, "Error Case");
 
  84         execution.setVariable("WorkflowException", we);
 
  86         doReturn(reqMock).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
 
  87         workflowActionBBFailure.updateRequestStatusToFailed(execution);
 
  88         Mockito.verify(reqMock, Mockito.times(1)).setStatusMessage("Error Case");
 
  89         Mockito.verify(reqMock, Mockito.times(1)).setRequestStatus("FAILED");
 
  90         Mockito.verify(reqMock, Mockito.times(1)).setProgress(Long.valueOf(100));
 
  91         Mockito.verify(reqMock, Mockito.times(1)).setLastModifiedBy("CamundaBPMN");
 
  92         Mockito.verify(reqMock, Mockito.times(1)).setEndTime(any(Timestamp.class));
 
  96     public void updateRequestStatusToFailed() {
 
  97         execution.setVariable("mso-request-id", "123");
 
  98         execution.setVariable("isRollbackComplete", false);
 
  99         execution.setVariable("isRollback", false);
 
 100         InfraActiveRequests req = new InfraActiveRequests();
 
 101         WorkflowException wfe = new WorkflowException("processKey123", 1, "error in test case");
 
 102         execution.setVariable("WorkflowException", wfe);
 
 103         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
 
 104         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
 
 105         workflowActionBBFailure.updateRequestStatusToFailed(execution);
 
 106         String errorMsg = (String) execution.getVariable("ErrorMessage");
 
 107         assertEquals("error in test case", errorMsg);
 
 108         assertEquals(Status.FAILED.toString(), req.getRequestStatus());
 
 112     public void updateRequestStatusToAborted() {
 
 113         execution.setVariable("mso-request-id", "123");
 
 114         execution.setVariable("isRollbackComplete", false);
 
 115         execution.setVariable("isRollback", false);
 
 116         execution.setVariable("handlingCode", "Abort");
 
 117         InfraActiveRequests req = new InfraActiveRequests();
 
 118         WorkflowException wfe = new WorkflowException("processKey123", 1, "error in test case");
 
 119         execution.setVariable("WorkflowException", wfe);
 
 120         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
 
 121         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
 
 122         workflowActionBBFailure.updateRequestStatusToFailed(execution);
 
 123         String errorMsg = (String) execution.getVariable("ErrorMessage");
 
 124         assertEquals("error in test case", errorMsg);
 
 125         assertEquals(Status.ABORTED.toString(), req.getRequestStatus());
 
 129     public void updateRequestStatusToFailedRollback() {
 
 130         execution.setVariable("mso-request-id", "123");
 
 131         execution.setVariable("isRollbackComplete", false);
 
 132         execution.setVariable("isRollback", true);
 
 133         InfraActiveRequests req = new InfraActiveRequests();
 
 134         WorkflowException wfe = new WorkflowException("processKey123", 1, "error in rollback");
 
 135         execution.setVariable("WorkflowException", wfe);
 
 136         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
 
 137         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
 
 138         workflowActionBBFailure.updateRequestStatusToFailed(execution);
 
 139         String errorMsg = (String) execution.getVariable("RollbackErrorMessage");
 
 140         assertEquals("error in rollback", errorMsg);
 
 141         assertEquals(Status.FAILED.toString(), req.getRequestStatus());
 
 145     public void updateRequestStatusToFailedRollbackFabric() {
 
 146         ExecuteBuildingBlock ebb = new ExecuteBuildingBlock();
 
 147         BuildingBlock bb = new BuildingBlock();
 
 148         bb.setBpmnFlowName("UnassignFabricConfigurationBB");
 
 149         ebb.setBuildingBlock(bb);
 
 150         execution.setVariable("buildingBlock", ebb);
 
 151         execution.setVariable("mso-request-id", "123");
 
 152         execution.setVariable("isRollbackComplete", false);
 
 153         execution.setVariable("isRollback", true);
 
 154         InfraActiveRequests req = new InfraActiveRequests();
 
 155         req.setStatusMessage("PINC failure.");
 
 156         WorkflowException wfe = new WorkflowException("processKey123", 1, "error in rollback");
 
 157         execution.setVariable("WorkflowException", wfe);
 
 158         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
 
 159         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
 
 160         workflowActionBBFailure.updateRequestStatusToFailed(execution);
 
 161         String errorMsg = (String) execution.getVariable("RollbackErrorMessage");
 
 162         assertEquals("error in rollback", errorMsg);
 
 164                 "PINC failure. Warning: The vf-module is active but configuration was not removed completely for one or more VMs.",
 
 165                 req.getStatusMessage());
 
 166         assertEquals(Status.FAILED.toString(), req.getRequestStatus());
 
 170     public void updateRequestStatusToRolledback() {
 
 171         execution.setVariable("mso-request-id", "123");
 
 172         execution.setVariable("isRollbackComplete", true);
 
 173         execution.setVariable("isRollback", true);
 
 174         execution.setVariable("rollbackTargetState", "ROLLED_BACK");
 
 175         InfraActiveRequests req = new InfraActiveRequests();
 
 176         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
 
 177         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
 
 178         workflowActionBBFailure.updateRequestStatusToFailed(execution);
 
 179         String errorMsg = (String) execution.getVariable("RollbackErrorMessage");
 
 180         assertEquals("Rollback has been completed successfully.", errorMsg);
 
 181         assertEquals(Status.ROLLED_BACK.toString(), req.getRequestStatus());
 
 185     public void updateRequestStatusToRolledbackToAssigned() {
 
 186         execution.setVariable("mso-request-id", "123");
 
 187         execution.setVariable("isRollbackComplete", true);
 
 188         execution.setVariable("isRollback", true);
 
 189         execution.setVariable("rollbackTargetState", "ROLLED_BACK_TO_ASSIGNED");
 
 190         InfraActiveRequests req = new InfraActiveRequests();
 
 191         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
 
 192         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
 
 193         workflowActionBBFailure.updateRequestStatusToFailed(execution);
 
 194         String errorMsg = (String) execution.getVariable("RollbackErrorMessage");
 
 195         assertEquals("Rollback has been completed successfully.", errorMsg);
 
 196         assertEquals(Status.ROLLED_BACK_TO_ASSIGNED.toString(), req.getRequestStatus());
 
 200     public void updateRequestStatusToRolledbackToCreated() {
 
 201         execution.setVariable("mso-request-id", "123");
 
 202         execution.setVariable("isRollbackComplete", true);
 
 203         execution.setVariable("isRollback", true);
 
 204         execution.setVariable("rollbackTargetState", "ROLLED_BACK_TO_CREATED");
 
 205         InfraActiveRequests req = new InfraActiveRequests();
 
 206         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
 
 207         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
 
 208         workflowActionBBFailure.updateRequestStatusToFailed(execution);
 
 209         String errorMsg = (String) execution.getVariable("RollbackErrorMessage");
 
 210         assertEquals("Rollback has been completed successfully.", errorMsg);
 
 211         assertEquals(Status.ROLLED_BACK_TO_CREATED.toString(), req.getRequestStatus());
 
 215     public void updateRequestStatusToFailedNoWorkflowException() {
 
 216         execution.setVariable("mso-request-id", "123");
 
 217         execution.setVariable("isRollbackComplete", false);
 
 218         execution.setVariable("isRollback", false);
 
 219         execution.setVariable("WorkflowExceptionErrorMessage", "error in test case");
 
 220         InfraActiveRequests req = new InfraActiveRequests();
 
 221         doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123");
 
 222         doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class));
 
 223         workflowActionBBFailure.updateRequestStatusToFailed(execution);
 
 224         String errorMsg = (String) execution.getVariable("ErrorMessage");
 
 225         assertEquals("error in test case", errorMsg);
 
 229     public void updateRequestErrorStatusMessageTest() {
 
 230         String reqId = "reqId123";
 
 231         execution.setVariable("mso-request-id", reqId);
 
 232         WorkflowException we = new WorkflowException("WorkflowAction", 1231, "Error Case");
 
 233         execution.setVariable("WorkflowException", we);
 
 235         doReturn(reqMock).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
 
 236         workflowActionBBFailure.updateRequestErrorStatusMessage(execution);
 
 237         Mockito.verify(reqMock, Mockito.times(1)).setStatusMessage("Error Case");
 
 238         Mockito.verify(reqMock, Mockito.times(1)).setProgress(Long.valueOf(100));
 
 239         Mockito.verify(reqMock, Mockito.times(1)).setLastModifiedBy("CamundaBPMN");
 
 240         Mockito.verify(reqMock, Mockito.times(1)).setEndTime(any(Timestamp.class));
 
 244     public void updateRequestRollbackErrorStatusMessageTest() {
 
 245         String reqId = "reqId123";
 
 246         execution.setVariable("mso-request-id", reqId);
 
 247         WorkflowException we = new WorkflowException("WorkflowAction", 1231, "Error Case");
 
 248         execution.setVariable("WorkflowException", we);
 
 249         execution.setVariable("isRollback", true);
 
 251         doReturn(reqMock).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
 
 252         workflowActionBBFailure.updateRequestErrorStatusMessage(execution);
 
 253         Mockito.verify(reqMock, Mockito.times(0)).setStatusMessage("Error Case");
 
 254         Mockito.verify(reqMock, Mockito.times(1)).setRollbackStatusMessage("Error Case");
 
 255         Mockito.verify(reqMock, Mockito.times(1)).setProgress(Long.valueOf(100));
 
 256         Mockito.verify(reqMock, Mockito.times(1)).setLastModifiedBy("CamundaBPMN");
 
 257         Mockito.verify(reqMock, Mockito.times(1)).setEndTime(any(Timestamp.class));
 
 261     public void updateRequestNotRollbackErrorStatusMessageTest() {
 
 262         String reqId = "reqId123";
 
 263         execution.setVariable("mso-request-id", reqId);
 
 264         WorkflowException we = new WorkflowException("WorkflowAction", 1231, "Error Case");
 
 265         execution.setVariable("WorkflowException", we);
 
 266         execution.setVariable("isRollback", false);
 
 268         doReturn(reqMock).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId);
 
 269         workflowActionBBFailure.updateRequestErrorStatusMessage(execution);
 
 270         Mockito.verify(reqMock, Mockito.times(1)).setStatusMessage("Error Case");
 
 271         Mockito.verify(reqMock, Mockito.times(0)).setRollbackStatusMessage("Error Case");
 
 272         Mockito.verify(reqMock, Mockito.times(1)).setProgress(Long.valueOf(100));
 
 273         Mockito.verify(reqMock, Mockito.times(1)).setLastModifiedBy("CamundaBPMN");
 
 274         Mockito.verify(reqMock, Mockito.times(1)).setEndTime(any(Timestamp.class));