From 14a324a7607335b381552e27c345970d5c2a2070 Mon Sep 17 00:00:00 2001 From: "Kalkere Ramesh, Sharan (sk720x)" Date: Mon, 10 Dec 2018 15:33:39 -0500 Subject: refactor updaterequeststatustofailed method fixed compliation error in workflowActionbbtest mocks moved request db failure logic to a separate class refactored and added test cases for updateReqToFailed Change-Id: Ie928aa541e86afb8e867ca445258b118eed8b357 Issue-ID: SO-1310 Signed-off-by: Kalkere Ramesh, Sharan (sk720x) --- .../workflow/tasks/WorkflowActionBBFailure.java | 116 +++++++++++++++++++ .../workflow/tasks/WorkflowActionBBTasks.java | 102 +---------------- .../tasks/WorkflowActionBBFailureTest.java | 126 +++++++++++++++++++++ .../workflow/tasks/WorkflowActionBBTasksTest.java | 38 ++----- 4 files changed, 258 insertions(+), 124 deletions(-) create mode 100644 bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/workflow/tasks/WorkflowActionBBFailure.java create mode 100644 bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/workflow/tasks/WorkflowActionBBFailureTest.java (limited to 'bpmn/so-bpmn-tasks') diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/workflow/tasks/WorkflowActionBBFailure.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/workflow/tasks/WorkflowActionBBFailure.java new file mode 100644 index 0000000000..7e6eb2a23d --- /dev/null +++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/workflow/tasks/WorkflowActionBBFailure.java @@ -0,0 +1,116 @@ +package org.onap.so.bpmn.infrastructure.workflow.tasks; + +import java.util.Optional; + +import org.camunda.bpm.engine.delegate.BpmnError; +import org.camunda.bpm.engine.delegate.DelegateExecution; +import org.onap.so.bpmn.core.WorkflowException; +import org.onap.so.db.request.beans.InfraActiveRequests; +import org.onap.so.db.request.client.RequestsDbClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class WorkflowActionBBFailure { + + private static final Logger logger = LoggerFactory.getLogger(WorkflowActionBBFailure.class); + @Autowired + private RequestsDbClient requestDbclient; + @Autowired + private WorkflowAction workflowAction; + + protected void updateRequestErrorStatusMessage(DelegateExecution execution) { + try { + String requestId = (String) execution.getVariable("mso-request-id"); + InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId); + String errorMsg = ""; + Optional errorMsgOp = retrieveErrorMessage(execution); + if(errorMsgOp.isPresent()){ + errorMsg = errorMsgOp.get(); + }else{ + errorMsg = "Failed to determine error message"; + } + request.setStatusMessage(errorMsg); + requestDbclient.updateInfraActiveRequests(request); + } catch (Exception e) { + logger.error("Failed to update Request db with the status message after retry or rollback has been initialized.",e); + } + } + + public void updateRequestStatusToFailed(DelegateExecution execution) { + try { + String requestId = (String) execution.getVariable("mso-request-id"); + InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId); + String rollbackErrorMsg = ""; + String errorMsg = ""; + Boolean rollbackCompletedSuccessfully = (Boolean) execution.getVariable("isRollbackComplete"); + Boolean isRollbackFailure = (Boolean) execution.getVariable("isRollback"); + + if(rollbackCompletedSuccessfully==null) + rollbackCompletedSuccessfully = false; + + if(isRollbackFailure==null) + isRollbackFailure = false; + + if(rollbackCompletedSuccessfully){ + rollbackErrorMsg = "Rollback has been completed successfully."; + request.setRollbackStatusMessage(rollbackErrorMsg); + execution.setVariable("RollbackErrorMessage", rollbackErrorMsg); + }else if(isRollbackFailure){ + Optional rollbackErrorMsgOp = retrieveErrorMessage(execution); + if(rollbackErrorMsgOp.isPresent()){ + rollbackErrorMsg = rollbackErrorMsgOp.get(); + }else{ + rollbackErrorMsg = "Failed to determine rollback error message."; + } + request.setRollbackStatusMessage(rollbackErrorMsg); + execution.setVariable("RollbackErrorMessage", rollbackErrorMsg); + }else{ + Optional errorMsgOp = retrieveErrorMessage(execution); + if(errorMsgOp.isPresent()){ + errorMsg = errorMsgOp.get(); + }else{ + errorMsg = "Failed to determine error message"; + } + request.setStatusMessage(errorMsg); + execution.setVariable("ErrorMessage", errorMsg); + } + request.setProgress(Long.valueOf(100)); + request.setRequestStatus("FAILED"); + request.setLastModifiedBy("CamundaBPMN"); + requestDbclient.updateInfraActiveRequests(request); + } catch (Exception e) { + workflowAction.buildAndThrowException(execution, "Error Updating Request Database", e); + } + } + + private Optional retrieveErrorMessage (DelegateExecution execution){ + String errorMsg = ""; + try { + WorkflowException exception = (WorkflowException) execution.getVariable("WorkflowException"); + if(exception != null && (exception.getErrorMessage()!=null || !exception.getErrorMessage().equals(""))){ + errorMsg = exception.getErrorMessage(); + } + if(errorMsg == null || errorMsg.equals("")){ + errorMsg = (String) execution.getVariable("WorkflowExceptionErrorMessage"); + } + return Optional.of(errorMsg); + } catch (Exception ex) { + logger.error("Failed to extract workflow exception from execution.",ex); + } + return Optional.of(errorMsg); + } + + public void updateRequestStatusToFailedWithRollback(DelegateExecution execution) { + execution.setVariable("isRollbackComplete", true); + updateRequestStatusToFailed(execution); + } + + public void abortCallErrorHandling(DelegateExecution execution) { + String msg = "Flow has failed. Rainy day handler has decided to abort the process."; + logger.error(msg); + throw new BpmnError(msg); + } +} diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/workflow/tasks/WorkflowActionBBTasks.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/workflow/tasks/WorkflowActionBBTasks.java index 7670e0245d..d9125e4104 100644 --- a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/workflow/tasks/WorkflowActionBBTasks.java +++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/workflow/tasks/WorkflowActionBBTasks.java @@ -22,6 +22,7 @@ package org.onap.so.bpmn.infrastructure.workflow.tasks; import java.util.ArrayList; import java.util.List; +import java.util.Optional; import org.camunda.bpm.engine.delegate.BpmnError; import org.camunda.bpm.engine.delegate.DelegateExecution; @@ -57,6 +58,8 @@ public class WorkflowActionBBTasks { private RequestsDbClient requestDbclient; @Autowired private WorkflowAction workflowAction; + @Autowired + private WorkflowActionBBFailure workflowActionBBFailure; public void selectBB(DelegateExecution execution) { List flowsToExecute = (List) execution @@ -207,7 +210,7 @@ public class WorkflowActionBBTasks { String retryDuration = (String) execution.getVariable("RetryDuration"); int retryCount = (int) execution.getVariable(RETRY_COUNT); if (handlingCode.equals("Retry")){ - updateRequestErrorStatusMessage(execution); + workflowActionBBFailure.updateRequestErrorStatusMessage(execution); try{ InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId); request.setRetryStatusMessage("Retry " + retryCount+1 + "/5 will be started in " + retryDuration); @@ -267,7 +270,7 @@ public class WorkflowActionBBTasks { } } - updateRequestErrorStatusMessage(execution); + workflowActionBBFailure.updateRequestErrorStatusMessage(execution); if (rollbackFlows.isEmpty()) execution.setVariable("isRollbackNeeded", false); @@ -282,99 +285,4 @@ public class WorkflowActionBBTasks { workflowAction.buildAndThrowException(execution, "Rollback has already been called. Cannot rollback a request that is currently in the rollback state."); } } - - protected void updateRequestErrorStatusMessage(DelegateExecution execution) { - try { - String requestId = (String) execution.getVariable(G_REQUEST_ID); - InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId); - String errorMsg = retrieveErrorMessage(execution); - if(errorMsg == null || errorMsg.equals("")){ - errorMsg = "Failed to determine error message"; - } - request.setStatusMessage(errorMsg); - logger.debug("Updating RequestDB to failed: errorMsg = " + errorMsg); - requestDbclient.updateInfraActiveRequests(request); - } catch (Exception e) { - logger.error("Failed to update Request db with the status message after retry or rollback has been initialized.",e); - } - } - - public void abortCallErrorHandling(DelegateExecution execution) { - String msg = "Flow has failed. Rainy day handler has decided to abort the process."; - logger.error(msg); - throw new BpmnError(msg); - } - - public void updateRequestStatusToFailed(DelegateExecution execution) { - try { - String requestId = (String) execution.getVariable(G_REQUEST_ID); - InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId); - String errorMsg = null; - String rollbackErrorMsg = null; - Boolean rollbackCompleted = (Boolean) execution.getVariable("isRollbackComplete"); - Boolean isRollbackFailure = (Boolean) execution.getVariable("isRollback"); - - if(rollbackCompleted==null) - rollbackCompleted = false; - - if(isRollbackFailure==null) - isRollbackFailure = false; - - if(rollbackCompleted){ - rollbackErrorMsg = "Rollback has been completed successfully."; - request.setRollbackStatusMessage(rollbackErrorMsg); - logger.debug("Updating RequestDB to failed: Rollback has been completed successfully"); - }else{ - if(isRollbackFailure){ - rollbackErrorMsg = retrieveErrorMessage(execution); - if(rollbackErrorMsg == null || rollbackErrorMsg.equals("")){ - rollbackErrorMsg = "Failed to determine rollback error message."; - } - request.setRollbackStatusMessage(rollbackErrorMsg); - logger.debug("Updating RequestDB to failed: rollbackErrorMsg = " + rollbackErrorMsg); - }else{ - errorMsg = retrieveErrorMessage(execution); - if(errorMsg == null || errorMsg.equals("")){ - errorMsg = "Failed to determine error message"; - } - request.setStatusMessage(errorMsg); - logger.debug("Updating RequestDB to failed: errorMsg = " + errorMsg); - } - } - request.setProgress(Long.valueOf(100)); - request.setRequestStatus("FAILED"); - request.setLastModifiedBy("CamundaBPMN"); - requestDbclient.updateInfraActiveRequests(request); - } catch (Exception e) { - workflowAction.buildAndThrowException(execution, "Error Updating Request Database", e); - } - } - - private String retrieveErrorMessage (DelegateExecution execution){ - String errorMsg = ""; - try { - WorkflowException exception = (WorkflowException) execution.getVariable("WorkflowException"); - if(exception != null && (exception.getErrorMessage()!=null || !exception.getErrorMessage().equals(""))){ - errorMsg = exception.getErrorMessage(); - } - } catch (Exception ex) { - //log error and attempt to extact WorkflowExceptionMessage - logger.error("Failed to extract workflow exception from execution.",ex); - } - - if (errorMsg == null){ - try { - errorMsg = (String) execution.getVariable("WorkflowExceptionErrorMessage"); - } catch (Exception ex) { - logger.error("Failed to extract workflow exception message from WorkflowException",ex); - errorMsg = "Unexpected Error in BPMN."; - } - } - return errorMsg; - } - - public void updateRequestStatusToFailedWithRollback(DelegateExecution execution) { - execution.setVariable("isRollbackComplete", true); - updateRequestStatusToFailed(execution); - } } diff --git a/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/workflow/tasks/WorkflowActionBBFailureTest.java b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/workflow/tasks/WorkflowActionBBFailureTest.java new file mode 100644 index 0000000000..d9a7eeb8f7 --- /dev/null +++ b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/workflow/tasks/WorkflowActionBBFailureTest.java @@ -0,0 +1,126 @@ +package org.onap.so.bpmn.infrastructure.workflow.tasks; + +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.isA; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.when; + +import org.camunda.bpm.engine.delegate.DelegateExecution; +import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.Spy; +import org.onap.so.bpmn.BaseTaskTest; +import org.onap.so.bpmn.core.WorkflowException; +import org.onap.so.bpmn.servicedecomposition.bbobjects.Customer; +import org.onap.so.db.request.beans.InfraActiveRequests; + +public class WorkflowActionBBFailureTest extends BaseTaskTest { + + @Mock + protected WorkflowAction workflowAction; + + @InjectMocks + @Spy + protected WorkflowActionBBFailure workflowActionBBFailure; + + @Mock + InfraActiveRequests reqMock; + + private DelegateExecution execution; + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Before + public void before() throws Exception { + execution = new DelegateExecutionFake(); + org.onap.aai.domain.yang.ServiceInstance servInstance = new org.onap.aai.domain.yang.ServiceInstance(); + servInstance.setServiceInstanceId("TEST"); + when(bbSetupUtils.getAAIServiceInstanceByName(anyString(), isA(Customer.class))).thenReturn(servInstance); + workflowAction.setBbInputSetupUtils(bbSetupUtils); + workflowAction.setBbInputSetup(bbInputSetup); + } + + @Test + public void updateRequestStatusToFailed_Null_Rollback(){ + String reqId = "reqId123"; + execution.setVariable("mso-request-id", reqId); + execution.setVariable("retryCount", 3); + execution.setVariable("handlingCode","Success"); + execution.setVariable("gCurrentSequence",1); + WorkflowException we = new WorkflowException("WorkflowAction",1231,"Error Case"); + execution.setVariable("WorkflowException",we); + + doReturn(reqMock).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId); + workflowActionBBFailure.updateRequestStatusToFailed(execution); + Mockito.verify( reqMock, Mockito.times(1)).setStatusMessage("Error Case"); + Mockito.verify( reqMock, Mockito.times(1)).setRequestStatus("FAILED"); + Mockito.verify( reqMock, Mockito.times(1)).setProgress(Long.valueOf(100)); + Mockito.verify( reqMock, Mockito.times(1)).setLastModifiedBy("CamundaBPMN"); + } + + @Test + public void updateRequestStatusToFailed(){ + execution.setVariable("mso-request-id", "123"); + execution.setVariable("isRollbackComplete", false); + execution.setVariable("isRollback", false); + InfraActiveRequests req = new InfraActiveRequests(); + WorkflowException wfe = new WorkflowException("processKey123", 1, "error in test case"); + execution.setVariable("WorkflowException", wfe); + doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123"); + doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class)); + workflowActionBBFailure.updateRequestStatusToFailed(execution); + String errorMsg = (String) execution.getVariable("ErrorMessage"); + assertEquals("error in test case", errorMsg); + } + + @Test + public void updateRequestStatusToFailedRollback(){ + execution.setVariable("mso-request-id", "123"); + execution.setVariable("isRollbackComplete", false); + execution.setVariable("isRollback", true); + InfraActiveRequests req = new InfraActiveRequests(); + WorkflowException wfe = new WorkflowException("processKey123", 1, "error in rollback"); + execution.setVariable("WorkflowException", wfe); + doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123"); + doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class)); + workflowActionBBFailure.updateRequestStatusToFailed(execution); + String errorMsg = (String) execution.getVariable("RollbackErrorMessage"); + assertEquals("error in rollback", errorMsg); + } + + @Test + public void updateRequestStatusToFailedRollbackCompleted(){ + execution.setVariable("mso-request-id", "123"); + execution.setVariable("isRollbackComplete", true); + execution.setVariable("isRollback", true); + InfraActiveRequests req = new InfraActiveRequests(); + doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123"); + doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class)); + workflowActionBBFailure.updateRequestStatusToFailed(execution); + String errorMsg = (String) execution.getVariable("RollbackErrorMessage"); + assertEquals("Rollback has been completed successfully.", errorMsg); + } + + @Test + public void updateRequestStatusToFailedNoWorkflowException(){ + execution.setVariable("mso-request-id", "123"); + execution.setVariable("isRollbackComplete", false); + execution.setVariable("isRollback", false); + execution.setVariable("WorkflowExceptionErrorMessage", "error in test case"); + InfraActiveRequests req = new InfraActiveRequests(); + doReturn(req).when(requestsDbClient).getInfraActiveRequestbyRequestId("123"); + doNothing().when(requestsDbClient).updateInfraActiveRequests(isA(InfraActiveRequests.class)); + workflowActionBBFailure.updateRequestStatusToFailed(execution); + String errorMsg = (String) execution.getVariable("ErrorMessage"); + assertEquals("error in test case", errorMsg); + } +} diff --git a/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/workflow/tasks/WorkflowActionBBTasksTest.java b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/workflow/tasks/WorkflowActionBBTasksTest.java index 0ef764ac99..189ecb96c7 100644 --- a/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/workflow/tasks/WorkflowActionBBTasksTest.java +++ b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/workflow/tasks/WorkflowActionBBTasksTest.java @@ -52,6 +52,9 @@ public class WorkflowActionBBTasksTest extends BaseTaskTest { @Mock protected WorkflowAction workflowAction; + @Mock + protected WorkflowActionBBFailure workflowActionBBFailure; + @InjectMocks @Spy protected WorkflowActionBBTasks workflowActionBBTasks; @@ -146,8 +149,8 @@ public class WorkflowActionBBTasksTest extends BaseTaskTest { execution.setVariable("flowsToExecute", flowsToExecute); execution.setVariable("gCurrentSequence", 3); - doNothing().when(workflowActionBBTasks).updateRequestErrorStatusMessage(isA(DelegateExecution.class)); - + doNothing().when(workflowActionBBFailure).updateRequestErrorStatusMessage(isA(DelegateExecution.class)); + workflowActionBBTasks.rollbackExecutionPath(execution); List ebbs = (List) execution.getVariable("flowsToExecute"); assertEquals(ebbs.get(0).getBuildingBlock().getBpmnFlowName(),"DeactivateVfModuleBB"); @@ -179,8 +182,8 @@ public class WorkflowActionBBTasksTest extends BaseTaskTest { execution.setVariable("flowsToExecute", flowsToExecute); execution.setVariable("gCurrentSequence", 2); - doNothing().when(workflowActionBBTasks).updateRequestErrorStatusMessage(isA(DelegateExecution.class)); - + doNothing().when(workflowActionBBFailure).updateRequestErrorStatusMessage(isA(DelegateExecution.class)); + workflowActionBBTasks.rollbackExecutionPath(execution); List ebbs = (List) execution.getVariable("flowsToExecute"); assertEquals(ebbs.get(0).getBuildingBlock().getBpmnFlowName(),"DeleteVfModuleBB"); @@ -217,8 +220,8 @@ public class WorkflowActionBBTasksTest extends BaseTaskTest { execution.setVariable("flowsToExecute", flowsToExecute); execution.setVariable("gCurrentSequence", 3); - doNothing().when(workflowActionBBTasks).updateRequestErrorStatusMessage(isA(DelegateExecution.class)); - + doNothing().when(workflowActionBBFailure).updateRequestErrorStatusMessage(isA(DelegateExecution.class)); + workflowActionBBTasks.rollbackExecutionPath(execution); List ebbs = (List) execution.getVariable("flowsToExecute"); assertEquals(ebbs.get(0).getBuildingBlock().getBpmnFlowName(),"UnassignNetworkBB"); @@ -255,7 +258,7 @@ public class WorkflowActionBBTasksTest extends BaseTaskTest { execution.setVariable("flowsToExecute", flowsToExecute); execution.setVariable("gCurrentSequence", 3); - doNothing().when(workflowActionBBTasks).updateRequestErrorStatusMessage(isA(DelegateExecution.class)); + doNothing().when(workflowActionBBFailure).updateRequestErrorStatusMessage(isA(DelegateExecution.class)); workflowActionBBTasks.rollbackExecutionPath(execution); List ebbs = (List) execution.getVariable("flowsToExecute"); @@ -267,7 +270,7 @@ public class WorkflowActionBBTasksTest extends BaseTaskTest { public void checkRetryStatusTest(){ String reqId = "reqId123"; execution.setVariable("mso-request-id", reqId); - doNothing().when(workflowActionBBTasks).updateRequestErrorStatusMessage(isA(DelegateExecution.class)); + doNothing().when(workflowActionBBFailure).updateRequestErrorStatusMessage(isA(DelegateExecution.class)); execution.setVariable("handlingCode","Retry"); execution.setVariable("retryCount", 1); execution.setVariable("gCurrentSequence",1); @@ -289,23 +292,4 @@ public class WorkflowActionBBTasksTest extends BaseTaskTest { workflowActionBBTasks.checkRetryStatus(execution); assertEquals(0,execution.getVariable("retryCount")); } - - - @Test - public void updateRequestStatusToFailed_Null_Rollback(){ - String reqId = "reqId123"; - execution.setVariable("mso-request-id", reqId); - execution.setVariable("retryCount", 3); - execution.setVariable("handlingCode","Success"); - execution.setVariable("gCurrentSequence",1); - WorkflowException we = new WorkflowException("WorkflowAction",1231,"Error Case"); - execution.setVariable("WorkflowException",we); - - doReturn(reqMock).when(requestsDbClient).getInfraActiveRequestbyRequestId(reqId); - workflowActionBBTasks.updateRequestStatusToFailed(execution); - Mockito.verify( reqMock, Mockito.times(1)).setStatusMessage("Error Case"); - Mockito.verify( reqMock, Mockito.times(1)).setRequestStatus("FAILED"); - Mockito.verify( reqMock, Mockito.times(1)).setProgress(Long.valueOf(100)); - Mockito.verify( reqMock, Mockito.times(1)).setLastModifiedBy("CamundaBPMN"); - } } -- cgit 1.2.3-korg