From 20176a7a1ca1da1734c03d7edb6431d4048e5729 Mon Sep 17 00:00:00 2001 From: Moti Asayag Date: Thu, 25 May 2023 11:51:45 +0300 Subject: [PATCH] Refactor async execution methods Signed-off-by: Moti Asayag --- .../CheckerWorkFlowPostInterceptor.java | 12 ++++--- .../WorkFlowContinuationService.java | 7 ++-- .../WorkFlowContinuationServiceImpl.java | 19 +++++------ .../execution/service/WorkFlowExecutor.java | 12 ++++--- .../service/WorkFlowExecutorImpl.java | 29 ++++++++--------- .../service/WorkFlowServiceImpl.java | 6 ++-- .../aspect/WorkFlowExecutionAspectTest.java | 5 +-- .../WorkFlowExecutionInterceptorTest.java | 17 ++++------ .../WorkFlowContinuationServiceImplTest.java | 32 ++++++++++++------- .../service/WorkFlowExecutorImplTest.java | 15 +++++++-- .../service/WorkFlowServiceImplTest.java | 7 ++-- 11 files changed, 91 insertions(+), 70 deletions(-) diff --git a/workflow-service/src/main/java/com/redhat/parodos/workflow/execution/aspect/CheckerWorkFlowPostInterceptor.java b/workflow-service/src/main/java/com/redhat/parodos/workflow/execution/aspect/CheckerWorkFlowPostInterceptor.java index 988a70e61..1a366bc2f 100644 --- a/workflow-service/src/main/java/com/redhat/parodos/workflow/execution/aspect/CheckerWorkFlowPostInterceptor.java +++ b/workflow-service/src/main/java/com/redhat/parodos/workflow/execution/aspect/CheckerWorkFlowPostInterceptor.java @@ -9,6 +9,7 @@ import com.redhat.parodos.workflow.execution.continuation.WorkFlowContinuationServiceImpl; import com.redhat.parodos.workflow.execution.entity.WorkFlowExecution; import com.redhat.parodos.workflow.execution.scheduler.WorkFlowSchedulerServiceImpl; +import com.redhat.parodos.workflow.execution.service.WorkFlowExecutor.ExecutionContext; import com.redhat.parodos.workflow.execution.service.WorkFlowServiceImpl; import com.redhat.parodos.workflows.work.WorkContext; import com.redhat.parodos.workflows.work.WorkReport; @@ -105,10 +106,13 @@ private void startOrStopWorkFlowCheckerOnSchedule(WorkFlow workFlow, * active checkers */ if (workFlowService.findRunningChecker(mainWorkFlowExecution).isEmpty()) { - workFlowContinuationServiceImpl.continueWorkFlow(projectId, userId, mainWorkFlowName, workContext, - mainWorkFlowExecution.getId(), - Optional.ofNullable(mainWorkFlowExecution.getWorkFlowDefinition().getRollbackWorkFlowDefinition()) - .map(WorkFlowDefinition::getName).orElse(null)); + workFlowContinuationServiceImpl.continueWorkFlow(ExecutionContext.builder().projectId(projectId) + .userId(userId).workFlowName(mainWorkFlowName).workContext(workContext) + .executionId(mainWorkFlowExecution.getId()) + .rollbackWorkFlowName(Optional + .ofNullable(mainWorkFlowExecution.getWorkFlowDefinition().getRollbackWorkFlowDefinition()) + .map(WorkFlowDefinition::getName).orElse(null)) + .build()); } } diff --git a/workflow-service/src/main/java/com/redhat/parodos/workflow/execution/continuation/WorkFlowContinuationService.java b/workflow-service/src/main/java/com/redhat/parodos/workflow/execution/continuation/WorkFlowContinuationService.java index 61d48bc89..60fd6c163 100644 --- a/workflow-service/src/main/java/com/redhat/parodos/workflow/execution/continuation/WorkFlowContinuationService.java +++ b/workflow-service/src/main/java/com/redhat/parodos/workflow/execution/continuation/WorkFlowContinuationService.java @@ -15,9 +15,7 @@ */ package com.redhat.parodos.workflow.execution.continuation; -import java.util.UUID; - -import com.redhat.parodos.workflows.work.WorkContext; +import com.redhat.parodos.workflow.execution.service.WorkFlowExecutor.ExecutionContext; /** * When the application starts up it will run any workflows in Progress @see @@ -30,7 +28,6 @@ public interface WorkFlowContinuationService { void workFlowRunAfterStartup(); - void continueWorkFlow(UUID projectId, UUID userId, String workflowName, WorkContext workContext, UUID executionId, - String rollbackWorkflowName); + void continueWorkFlow(ExecutionContext executionContext); } diff --git a/workflow-service/src/main/java/com/redhat/parodos/workflow/execution/continuation/WorkFlowContinuationServiceImpl.java b/workflow-service/src/main/java/com/redhat/parodos/workflow/execution/continuation/WorkFlowContinuationServiceImpl.java index 650d361b9..751a87346 100644 --- a/workflow-service/src/main/java/com/redhat/parodos/workflow/execution/continuation/WorkFlowContinuationServiceImpl.java +++ b/workflow-service/src/main/java/com/redhat/parodos/workflow/execution/continuation/WorkFlowContinuationServiceImpl.java @@ -17,13 +17,12 @@ import java.util.List; import java.util.Optional; -import java.util.UUID; import com.redhat.parodos.workflow.definition.entity.WorkFlowDefinition; import com.redhat.parodos.workflow.execution.entity.WorkFlowExecution; import com.redhat.parodos.workflow.execution.repository.WorkFlowRepository; import com.redhat.parodos.workflow.execution.service.WorkFlowExecutor; -import com.redhat.parodos.workflows.work.WorkContext; +import com.redhat.parodos.workflow.execution.service.WorkFlowExecutor.ExecutionContext; import com.redhat.parodos.workflows.work.WorkStatus; import lombok.extern.slf4j.Slf4j; @@ -65,18 +64,20 @@ public void workFlowRunAfterStartup() { WorkFlowDefinition workFlowDefinition = workFlowExecution.getWorkFlowDefinition(); // continue with the same execution id - continueWorkFlow(workFlowExecution.getProjectId(), workFlowExecution.getUser().getId(), - workFlowDefinition.getName(), workFlowExecution.getWorkFlowExecutionContext().getWorkContext(), - workFlowExecution.getId(), Optional.ofNullable(workFlowDefinition.getRollbackWorkFlowDefinition()) - .map(WorkFlowDefinition::getName).orElse(null)); + continueWorkFlow(ExecutionContext.builder().projectId(workFlowExecution.getProjectId()) + .userId(workFlowExecution.getUser().getId()).workFlowName(workFlowDefinition.getName()) + .workContext(workFlowExecution.getWorkFlowExecutionContext().getWorkContext()) + .executionId(workFlowExecution.getId()) + .rollbackWorkFlowName(Optional.ofNullable(workFlowDefinition.getRollbackWorkFlowDefinition()) + .map(WorkFlowDefinition::getName).orElse(null)) + .build()); // TODO: continue 'FAILED' Checkers in this main workflow execution }); } @Override - public void continueWorkFlow(UUID projectId, UUID userId, String workflowName, WorkContext workContext, - UUID executionId, String rollbackWorkflowName) { - workFlowExecutor.executeAsync(projectId, userId, workflowName, workContext, executionId, rollbackWorkflowName); + public void continueWorkFlow(ExecutionContext executionContext) { + workFlowExecutor.executeAsync(executionContext); } } diff --git a/workflow-service/src/main/java/com/redhat/parodos/workflow/execution/service/WorkFlowExecutor.java b/workflow-service/src/main/java/com/redhat/parodos/workflow/execution/service/WorkFlowExecutor.java index 5bc20d6ac..8f37bebde 100644 --- a/workflow-service/src/main/java/com/redhat/parodos/workflow/execution/service/WorkFlowExecutor.java +++ b/workflow-service/src/main/java/com/redhat/parodos/workflow/execution/service/WorkFlowExecutor.java @@ -4,16 +4,20 @@ import com.redhat.parodos.workflows.work.WorkContext; import com.redhat.parodos.workflows.work.WorkReport; +import lombok.Builder; import org.springframework.scheduling.annotation.Async; public interface WorkFlowExecutor { @Async - void executeAsync(UUID projectId, UUID userId, String workflowName, WorkContext workContext, UUID executionId, - String rollbackWorkflowName); + void executeAsync(ExecutionContext context); - WorkReport execute(UUID projectId, UUID userId, String workflowName, WorkContext workContext, UUID executionId, - String rollbackWorkflowName); + WorkReport execute(ExecutionContext context); + + @Builder + record ExecutionContext(UUID projectId, UUID userId, String workFlowName, WorkContext workContext, UUID executionId, + String rollbackWorkFlowName) { + } } diff --git a/workflow-service/src/main/java/com/redhat/parodos/workflow/execution/service/WorkFlowExecutorImpl.java b/workflow-service/src/main/java/com/redhat/parodos/workflow/execution/service/WorkFlowExecutorImpl.java index cf7d05c80..e23bd6eff 100644 --- a/workflow-service/src/main/java/com/redhat/parodos/workflow/execution/service/WorkFlowExecutorImpl.java +++ b/workflow-service/src/main/java/com/redhat/parodos/workflow/execution/service/WorkFlowExecutorImpl.java @@ -1,13 +1,11 @@ package com.redhat.parodos.workflow.execution.service; import java.util.Optional; -import java.util.UUID; import com.redhat.parodos.workflow.WorkFlowDelegate; import com.redhat.parodos.workflow.execution.repository.WorkFlowRepository; import com.redhat.parodos.workflow.utils.WorkContextUtils; import com.redhat.parodos.workflows.engine.WorkFlowEngineBuilder; -import com.redhat.parodos.workflows.work.WorkContext; import com.redhat.parodos.workflows.work.WorkReport; import com.redhat.parodos.workflows.work.WorkStatus; import com.redhat.parodos.workflows.workflow.WorkFlow; @@ -29,29 +27,28 @@ public WorkFlowExecutorImpl(WorkFlowDelegate workFlowDelegate, WorkFlowRepositor } @Override - public void executeAsync(UUID projectId, UUID userId, String workflowName, WorkContext workContext, - UUID executionId, String rollbackWorkflowName) { - execute(projectId, userId, workflowName, workContext, executionId, rollbackWorkflowName); + public void executeAsync(ExecutionContext executionContext) { + execute(executionContext); } @Override - public WorkReport execute(UUID projectId, UUID userId, String workflowName, WorkContext workContext, - UUID executionId, String rollbackWorkflowName) { - WorkFlow workFlow = workFlowDelegate.getWorkFlowByName(workflowName); - log.info("execute workFlow {}", workflowName); - WorkContextUtils.updateWorkContextPartially(workContext, projectId, userId, workflowName, executionId); - WorkReport report = WorkFlowEngineBuilder.aNewWorkFlowEngine().build().run(workFlow, workContext); + public WorkReport execute(ExecutionContext context) { + WorkFlow workFlow = workFlowDelegate.getWorkFlowByName(context.workFlowName()); + log.info("execute workFlow {}", context.workFlowName()); + WorkContextUtils.updateWorkContextPartially(context.workContext(), context.projectId(), context.userId(), + context.workFlowName(), context.executionId()); + WorkReport report = WorkFlowEngineBuilder.aNewWorkFlowEngine().build().run(workFlow, context.workContext()); // need to use the status from db to avoid of repetitive execution on rollback - if (workFlowRepository.findById(executionId).map(execution -> execution.getStatus() == WorkStatus.FAILED) - .orElse(false)) { - Optional.ofNullable(workFlowDelegate.getWorkFlowByName(rollbackWorkflowName)) + if (workFlowRepository.findById(context.executionId()) + .map(execution -> execution.getStatus() == WorkStatus.FAILED).orElse(false)) { + Optional.ofNullable(workFlowDelegate.getWorkFlowByName(context.rollbackWorkFlowName())) .ifPresentOrElse(rollbackWorkFlow -> { log.error( "The Infrastructure workflow failed. Check the logs for errors coming for the Tasks in this workflow. Checking if there is a Rollback"); - WorkFlowEngineBuilder.aNewWorkFlowEngine().build().run(rollbackWorkFlow, workContext); + WorkFlowEngineBuilder.aNewWorkFlowEngine().build().run(rollbackWorkFlow, context.workContext()); }, () -> log.error( "A rollback workflow could not be found for failed workflow: {} in execution: {}", - workflowName, executionId)); + context.workFlowName(), context.executionId())); } return report; } diff --git a/workflow-service/src/main/java/com/redhat/parodos/workflow/execution/service/WorkFlowServiceImpl.java b/workflow-service/src/main/java/com/redhat/parodos/workflow/execution/service/WorkFlowServiceImpl.java index 54f7ff045..bc02ac8f5 100644 --- a/workflow-service/src/main/java/com/redhat/parodos/workflow/execution/service/WorkFlowServiceImpl.java +++ b/workflow-service/src/main/java/com/redhat/parodos/workflow/execution/service/WorkFlowServiceImpl.java @@ -157,8 +157,10 @@ public WorkReport execute(WorkFlowRequestDTO workFlowRequestDTO) { WorkFlowExecution workFlowExecution = saveWorkFlow(projectId, user.getId(), workFlowDefinitionRepository.findFirstByName(workflowName), WorkStatus.IN_PROGRESS, null, arguments); WorkContextUtils.setMainExecutionId(workContext, workFlowExecution.getId()); - workFlowExecutor.executeAsync(projectId, user.getId(), workflowName, workContext, workFlowExecution.getId(), - workFlowDefinitionResponseDTO.getRollbackWorkflow()); + workFlowExecutor + .executeAsync(WorkFlowExecutor.ExecutionContext.builder().projectId(projectId).userId(user.getId()) + .workFlowName(workflowName).workContext(workContext).executionId(workFlowExecution.getId()) + .rollbackWorkFlowName(workFlowDefinitionResponseDTO.getRollbackWorkflow()).build()); return new DefaultWorkReport(WorkStatus.IN_PROGRESS, workContext, null); } diff --git a/workflow-service/src/test/java/com/redhat/parodos/workflow/execution/aspect/WorkFlowExecutionAspectTest.java b/workflow-service/src/test/java/com/redhat/parodos/workflow/execution/aspect/WorkFlowExecutionAspectTest.java index e8ec3f895..f318cc5ba 100644 --- a/workflow-service/src/test/java/com/redhat/parodos/workflow/execution/aspect/WorkFlowExecutionAspectTest.java +++ b/workflow-service/src/test/java/com/redhat/parodos/workflow/execution/aspect/WorkFlowExecutionAspectTest.java @@ -17,6 +17,7 @@ import com.redhat.parodos.workflow.execution.entity.WorkFlowExecution; import com.redhat.parodos.workflow.execution.repository.WorkFlowRepository; import com.redhat.parodos.workflow.execution.scheduler.WorkFlowSchedulerServiceImpl; +import com.redhat.parodos.workflow.execution.service.WorkFlowExecutor.ExecutionContext; import com.redhat.parodos.workflow.execution.service.WorkFlowServiceImpl; import com.redhat.parodos.workflows.work.DefaultWorkReport; import com.redhat.parodos.workflows.work.WorkContext; @@ -125,7 +126,7 @@ public void ExecuteAroundAdviceWithValidDataTest() { when(workFlowRepository.findFirstByWorkFlowDefinitionIdAndMainWorkFlowExecution(any(), any())) .thenReturn(workFlowExecution); when(workFlowRepository.findById(any())).thenReturn(Optional.of(workFlowExecution)); - doNothing().when(workFlowContinuationService).continueWorkFlow(any(), any(), any(), any(), any(), any()); + doNothing().when(workFlowContinuationService).continueWorkFlow(any(ExecutionContext.class)); // when WorkReport workReport = this.workFlowExecutionAspect.executeAroundAdvice(proceedingJoinPoint, workContext); @@ -230,4 +231,4 @@ static WorkFlowDefinition getSampleWorkFlowDefinition(String name) { return workFlowDefinition; } -} \ No newline at end of file +} diff --git a/workflow-service/src/test/java/com/redhat/parodos/workflow/execution/aspect/WorkFlowExecutionInterceptorTest.java b/workflow-service/src/test/java/com/redhat/parodos/workflow/execution/aspect/WorkFlowExecutionInterceptorTest.java index cd6243175..f8e18e187 100644 --- a/workflow-service/src/test/java/com/redhat/parodos/workflow/execution/aspect/WorkFlowExecutionInterceptorTest.java +++ b/workflow-service/src/test/java/com/redhat/parodos/workflow/execution/aspect/WorkFlowExecutionInterceptorTest.java @@ -11,6 +11,7 @@ import com.redhat.parodos.workflow.execution.entity.WorkFlowExecution; import com.redhat.parodos.workflow.execution.repository.WorkFlowRepository; import com.redhat.parodos.workflow.execution.scheduler.WorkFlowSchedulerServiceImpl; +import com.redhat.parodos.workflow.execution.service.WorkFlowExecutor.ExecutionContext; import com.redhat.parodos.workflow.execution.service.WorkFlowServiceImpl; import com.redhat.parodos.workflows.work.WorkContext; import com.redhat.parodos.workflows.work.WorkReport; @@ -25,7 +26,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.mockito.ArgumentMatchers.nullable; import static org.mockito.Mockito.any; import static org.mockito.Mockito.anyString; import static org.mockito.Mockito.eq; @@ -37,10 +37,6 @@ @ExtendWith(SpringExtension.class) public class WorkFlowExecutionInterceptorTest { - private static final UUID TEST_USER_ID = UUID.randomUUID(); - - private static final String TEST_USERNAME = "test-username"; - private static final String TEST_WORKFLOW_NAME = "test-workflow-name"; private WorkFlowExecutionInterceptor interceptor; @@ -70,7 +66,7 @@ public class WorkFlowExecutionInterceptorTest { @BeforeEach public void setUp() { - User user = createUser(TEST_USER_ID, TEST_USERNAME); + User user = createUser(); expectedWorkFlowExecution = new WorkFlowExecution(); expectedWorkFlowExecution.setProjectId(UUID.randomUUID()); expectedWorkFlowExecution.setUser(user); @@ -145,14 +141,13 @@ public void testHandleCompletePostWorkFlowExecution() { verify(workFlowService, times(0)).saveWorkFlow(any(UUID.class), any(UUID.class), any(), eq(WorkStatus.IN_PROGRESS), any(), anyString()); verify(workFlowSchedulerService, times(1)).stop(any(), any(), any(WorkFlow.class)); - verify(workFlowContinuationServiceImpl, times(1)).continueWorkFlow(any(UUID.class), any(UUID.class), - anyString(), any(WorkContext.class), any(UUID.class), nullable(String.class)); + verify(workFlowContinuationServiceImpl, times(1)).continueWorkFlow(any(ExecutionContext.class)); assertEquals(result.getStatus(), report.getStatus()); } - private User createUser(UUID userId, String username) { - User user = User.builder().username(username).build(); - user.setId(userId); + private User createUser() { + User user = User.builder().username(UUID.randomUUID().toString()).build(); + user.setId(UUID.randomUUID()); return user; } diff --git a/workflow-service/src/test/java/com/redhat/parodos/workflow/execution/continuation/WorkFlowContinuationServiceImplTest.java b/workflow-service/src/test/java/com/redhat/parodos/workflow/execution/continuation/WorkFlowContinuationServiceImplTest.java index 9f59fd737..55cf43fc0 100644 --- a/workflow-service/src/test/java/com/redhat/parodos/workflow/execution/continuation/WorkFlowContinuationServiceImplTest.java +++ b/workflow-service/src/test/java/com/redhat/parodos/workflow/execution/continuation/WorkFlowContinuationServiceImplTest.java @@ -15,18 +15,19 @@ import com.redhat.parodos.workflow.execution.repository.WorkFlowRepository; import com.redhat.parodos.workflow.execution.repository.WorkFlowTaskRepository; import com.redhat.parodos.workflow.execution.service.WorkFlowExecutor; +import com.redhat.parodos.workflow.execution.service.WorkFlowExecutor.ExecutionContext; import com.redhat.parodos.workflows.work.WorkContext; import com.redhat.parodos.workflows.work.WorkStatus; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.ArgumentMatchers.nullable; import static org.mockito.Mockito.any; import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -72,7 +73,7 @@ void workFlowSkipCompletedJobs() { // then verify(this.workFlowRepository, times(1)).findByStatusInAndIsMain(workFlowStatuses); - verify(this.workFlowExecutor, times(0)).executeAsync(any(), any(), any(), any(), any(), any()); + verify(this.workFlowExecutor, times(0)).executeAsync(any(ExecutionContext.class)); } @Test @@ -86,8 +87,8 @@ void workFlowCompleteInProgress() { // then verify(this.workFlowRepository, times(1)).findByStatusInAndIsMain(workFlowStatuses); - verify(this.workFlowExecutor, times(1)).executeAsync(eq(workFlowExecution.getProjectId()), - eq(workFlowExecution.getUser().getId()), eq(TEST_WORKFLOW), any(), any(), nullable(String.class)); + + verifyAsyncExecution(workFlowExecution); } @Test @@ -102,8 +103,8 @@ void workFlowCompletePending() { // then verify(this.workFlowRepository, times(1)).findByStatusInAndIsMain(workFlowStatuses); - verify(this.workFlowExecutor, times(1)).executeAsync(eq(workFlowExecution.getProjectId()), - eq(workFlowExecution.getUser().getId()), eq(TEST_WORKFLOW), any(), any(), nullable(String.class)); + + verifyAsyncExecution(workFlowExecution); } @Test @@ -127,8 +128,7 @@ void workFlowCompleteWithTaskExecutions() { // then verify(this.workFlowRepository, times(1)).findByStatusInAndIsMain(workFlowStatuses); - verify(this.workFlowExecutor, times(1)).executeAsync(eq(workFlowExecution.getProjectId()), - eq(workFlowExecution.getUser().getId()), eq(TEST_WORKFLOW), any(), any(), nullable(String.class)); + verifyAsyncExecution(workFlowExecution); } @Test @@ -146,8 +146,8 @@ void workFlowCompleteWithInvalidJson() { when(this.workFlowTaskRepository.findByWorkFlowExecutionId(wfExecution.getId())) .thenReturn(List.of(workFlowTaskExecution)); - doThrow(new RuntimeException("JsonParseException")).when(workFlowExecutor).executeAsync(any(), any(), any(), - any(), any(), any()); + doThrow(new RuntimeException("JsonParseException")).when(workFlowExecutor) + .executeAsync(any(ExecutionContext.class)); // when Exception exception = assertThrows(RuntimeException.class, () -> this.service.workFlowRunAfterStartup()); @@ -157,7 +157,7 @@ void workFlowCompleteWithInvalidJson() { assertTrue(exception.getMessage().contains("JsonParseException")); verify(this.workFlowRepository, times(1)).findByStatusInAndIsMain(workFlowStatuses); - verify(this.workFlowExecutor, times(1)).executeAsync(any(), any(), any(), any(), any(), any()); + verify(this.workFlowExecutor, times(1)).executeAsync(any(ExecutionContext.class)); } @@ -187,4 +187,12 @@ private WorkFlowDefinition sampleWorkFlowDefinition() { return workFlowDefinition; } + private void verifyAsyncExecution(WorkFlowExecution workFlowExecution) { + var argument = ArgumentCaptor.forClass(ExecutionContext.class); + verify(this.workFlowExecutor, times(1)).executeAsync(argument.capture()); + assertThat(argument.getValue().projectId()).isEqualTo(workFlowExecution.getProjectId()); + assertThat(argument.getValue().userId()).isEqualTo(workFlowExecution.getUser().getId()); + assertThat(argument.getValue().workFlowName()).isEqualTo(TEST_WORKFLOW); + } + } diff --git a/workflow-service/src/test/java/com/redhat/parodos/workflow/execution/service/WorkFlowExecutorImplTest.java b/workflow-service/src/test/java/com/redhat/parodos/workflow/execution/service/WorkFlowExecutorImplTest.java index b52832164..2a76d412a 100644 --- a/workflow-service/src/test/java/com/redhat/parodos/workflow/execution/service/WorkFlowExecutorImplTest.java +++ b/workflow-service/src/test/java/com/redhat/parodos/workflow/execution/service/WorkFlowExecutorImplTest.java @@ -6,6 +6,7 @@ import com.redhat.parodos.workflow.WorkFlowDelegate; import com.redhat.parodos.workflow.execution.entity.WorkFlowExecution; import com.redhat.parodos.workflow.execution.repository.WorkFlowRepository; +import com.redhat.parodos.workflow.execution.service.WorkFlowExecutor.ExecutionContext; import com.redhat.parodos.workflows.work.DefaultWorkReport; import com.redhat.parodos.workflows.work.WorkContext; import com.redhat.parodos.workflows.work.WorkStatus; @@ -66,7 +67,10 @@ public void init() { @Test void execute_when_mainWorkflowIsFailed_and_rollbackIsFound_then_executeRollback() { workFlowExecution.setStatus(WorkStatus.FAILED); - workFlowExecutor.execute(projectId, userId, workflowName, workContext, executionId, rollbackWorkflowName); + ExecutionContext executionContext = ExecutionContext.builder().projectId(projectId).userId(userId) + .workFlowName(workflowName).workContext(workContext).executionId(executionId) + .rollbackWorkFlowName(rollbackWorkflowName).build(); + workFlowExecutor.execute(executionContext); verify(mainWorkFlow, Mockito.times(1)).execute(any(WorkContext.class)); verify(rollbackWorkFlow, Mockito.times(1)).execute(any(WorkContext.class)); @@ -76,7 +80,9 @@ void execute_when_mainWorkflowIsFailed_and_rollbackIsFound_then_executeRollback( @Test void execute_when_mainWorkflowIsFailed_and_rollbackIsNotFound_then_notExecuteRollback() { workFlowExecution.setStatus(WorkStatus.FAILED); - workFlowExecutor.execute(projectId, userId, workflowName, workContext, executionId, null); + ExecutionContext executionContext = ExecutionContext.builder().projectId(projectId).userId(userId) + .workFlowName(workflowName).workContext(workContext).executionId(executionId).build(); + workFlowExecutor.execute(executionContext); verify(mainWorkFlow, Mockito.times(1)).execute(any(WorkContext.class)); verify(rollbackWorkFlow, Mockito.never()).execute(any(WorkContext.class)); @@ -85,7 +91,10 @@ void execute_when_mainWorkflowIsFailed_and_rollbackIsNotFound_then_notExecuteRol @Test void execute_when_mainWorkflowIsCompleted_then_notExecuteRollback() { workFlowExecution.setStatus(WorkStatus.COMPLETED); - workFlowExecutor.execute(projectId, userId, workflowName, workContext, executionId, rollbackWorkflowName); + ExecutionContext executionContext = ExecutionContext.builder().projectId(projectId).userId(userId) + .workFlowName(workflowName).workContext(workContext).executionId(executionId) + .rollbackWorkFlowName(rollbackWorkflowName).build(); + workFlowExecutor.execute(executionContext); verify(mainWorkFlow, Mockito.times(1)).execute(any(WorkContext.class)); verify(rollbackWorkFlow, Mockito.never()).execute(any(WorkContext.class)); diff --git a/workflow-service/src/test/java/com/redhat/parodos/workflow/execution/service/WorkFlowServiceImplTest.java b/workflow-service/src/test/java/com/redhat/parodos/workflow/execution/service/WorkFlowServiceImplTest.java index b3abcc63b..61bda6d55 100644 --- a/workflow-service/src/test/java/com/redhat/parodos/workflow/execution/service/WorkFlowServiceImplTest.java +++ b/workflow-service/src/test/java/com/redhat/parodos/workflow/execution/service/WorkFlowServiceImplTest.java @@ -32,6 +32,7 @@ import com.redhat.parodos.workflow.execution.entity.WorkFlowTaskExecution; import com.redhat.parodos.workflow.execution.repository.WorkFlowRepository; import com.redhat.parodos.workflow.execution.repository.WorkFlowTaskRepository; +import com.redhat.parodos.workflow.execution.service.WorkFlowExecutor.ExecutionContext; import com.redhat.parodos.workflow.option.WorkFlowOption; import com.redhat.parodos.workflows.work.DefaultWorkReport; import com.redhat.parodos.workflows.work.Work; @@ -131,8 +132,10 @@ void executeTestWithValidData() { .thenReturn(this.sampleWorkflowDefinition("test")); // when - WorkReport report = this.workFlowExecutor.execute(UUID.randomUUID(), UUID.randomUUID(), TEST_WORKFLOW_NAME, - new WorkContext(), UUID.randomUUID(), null); + ExecutionContext executionContext = ExecutionContext.builder().projectId(UUID.randomUUID()) + .userId(UUID.randomUUID()).workFlowName(TEST_WORKFLOW_NAME).workContext(new WorkContext()) + .executionId(UUID.randomUUID()).build(); + WorkReport report = this.workFlowExecutor.execute(executionContext); // then assertNotNull(report);