Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
Test: workflow-service checks
Browse files Browse the repository at this point in the history
When introduced the master workflow, the service has a few changes out
of the coverage.

Signed-off-by: Eloy Coto <eloy.coto@acalustra.com>
  • Loading branch information
eloycoto committed Mar 14, 2023
1 parent ec33bb3 commit d8c5cc8
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ private String validateWorkflow(String workflowName, WorkFlow workFlow) {

// validate if workflow is master
WorkFlowDefinition workFlowDefinition = workFlowDefinitionRepository.findFirstByName(workflowName);
if (workFlowDefinition == null) {
return String.format("workflow '%s' is not registered!", workflowName);
}

if (!workFlowWorkRepository.findByWorkDefinitionId(workFlowDefinition.getId()).isEmpty()) {
log.error("workflow '{}' is not master workflow!", workflowName);
return String.format("workflow '%s' is not master workflow!", workflowName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.redhat.parodos.workflow.WorkFlowDelegate;
import com.redhat.parodos.workflow.definition.entity.WorkFlowDefinition;
import com.redhat.parodos.workflow.definition.entity.WorkFlowWorkDefinition;
import com.redhat.parodos.workflow.definition.repository.WorkFlowDefinitionRepository;
import com.redhat.parodos.workflow.definition.repository.WorkFlowWorkRepository;
import com.redhat.parodos.workflow.enums.WorkFlowStatus;
Expand All @@ -21,6 +22,7 @@
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.mockito.stubbing.OngoingStubbing;

import java.util.LinkedList;
import java.util.List;
Expand All @@ -46,14 +48,15 @@ class WorkFlowServiceImplTest {
private WorkFlowServiceImpl workFlowService;

@BeforeEach
void init_earch() {
void init_each() {
this.workFlowRepository = Mockito.mock(WorkFlowRepository.class);
this.workFlowDefinitionRepository = Mockito.mock(WorkFlowDefinitionRepository.class);
this.workFlowTaskRepository = Mockito.mock(WorkFlowTaskRepository.class);
this.workFlowDelegate = Mockito.mock(WorkFlowDelegate.class);
this.workFlowWorkRepository = Mockito.mock(WorkFlowWorkRepository.class);

this.workFlowService = new WorkFlowServiceImpl(this.workFlowDelegate, this.workFlowDefinitionRepository,
this.workFlowRepository, this.workFlowTaskRepository, workFlowWorkRepository);
this.workFlowRepository, this.workFlowTaskRepository, this.workFlowWorkRepository);
}

@Test
Expand Down Expand Up @@ -109,6 +112,89 @@ void executeTestWithNoValidWorkflow() {
Mockito.verify(this.workFlowDefinitionRepository, Mockito.times(0)).findFirstByName(Mockito.any());
}

@Test
void executeWithDTOWithValidData() {
// given
Work work = Mockito.mock(Work.class);
SequentialFlow workFlow = SequentialFlow.Builder.aNewSequentialFlow().named("test").execute(work).build();
Mockito.when(work.execute(Mockito.any()))
.thenReturn(new DefaultWorkReport(WorkStatus.COMPLETED, new WorkContext() {
{
put("foo", "bar");
}
}));
Mockito.when(this.workFlowDefinitionRepository.findFirstByName(Mockito.any()))
.thenReturn(this.sampleWorkflowDefinition("test"));
Mockito.when(this.workFlowWorkRepository.findByWorkDefinitionId(Mockito.any())).thenReturn(List.of());
Mockito.when(this.workFlowDelegate.initWorkFlowContext(Mockito.any())).thenReturn(new WorkContext());
Mockito.when(this.workFlowDelegate.getWorkFlowExecutionByName("test-workflow")).thenReturn(workFlow);

// when
WorkReport report = this.workFlowService.execute(WorkFlowRequestDTO.builder().projectId("test-project")
.works(List.of()).workFlowName("test-workflow").build());
// then
assertNotNull(report);
assertEquals(report.getStatus().toString(), "COMPLETED");
assertNull(report.getError());

assertNotNull(report.getWorkContext());

Mockito.verify(this.workFlowDelegate, Mockito.times(2)).getWorkFlowExecutionByName(Mockito.any());
Mockito.verify(this.workFlowDelegate, Mockito.times(1)).initWorkFlowContext(Mockito.any());
Mockito.verify(this.workFlowDefinitionRepository, Mockito.times(1)).findFirstByName(Mockito.any());
}

@Test
void executeWithDTOWithNoMasterWorkFlow() {
// given
Work work = Mockito.mock(Work.class);
SequentialFlow workFlow = SequentialFlow.Builder.aNewSequentialFlow().named("test").execute(work).build();
Mockito.when(this.workFlowDefinitionRepository.findFirstByName(Mockito.any()))
.thenReturn(this.sampleWorkflowDefinition("test"));
Mockito.when(this.workFlowWorkRepository.findByWorkDefinitionId(Mockito.any()))
.thenReturn(List.of(WorkFlowWorkDefinition.builder().build()));

Mockito.when(this.workFlowDelegate.getWorkFlowExecutionByName("test-workflow")).thenReturn(workFlow);

// when
WorkReport report = this.workFlowService.execute(WorkFlowRequestDTO.builder().projectId("test-project")
.works(List.of()).workFlowName("test-workflow").build());
// then
assertNotNull(report);
assertEquals(report.getStatus().toString(), "FAILED");
assertNotNull(report.getError());

assertNotNull(report.getWorkContext());

Mockito.verify(this.workFlowDelegate, Mockito.times(1)).getWorkFlowExecutionByName(Mockito.any());
Mockito.verify(this.workFlowDelegate, Mockito.times(0)).initWorkFlowContext(Mockito.any());
Mockito.verify(this.workFlowDefinitionRepository, Mockito.times(1)).findFirstByName(Mockito.any());
}

@Test
void executeWithDTOWithNoWorkFlowDefinition() {
// given
Work work = Mockito.mock(Work.class);
SequentialFlow workFlow = SequentialFlow.Builder.aNewSequentialFlow().named("test").execute(work).build();
Mockito.when(this.workFlowDefinitionRepository.findFirstByName(Mockito.any())).thenReturn(null);
Mockito.when(this.workFlowDelegate.getWorkFlowExecutionByName("test-workflow")).thenReturn(workFlow);

// when
WorkReport report = this.workFlowService.execute(WorkFlowRequestDTO.builder().projectId("test-project")
.works(List.of()).workFlowName("test-workflow").build());
// then
assertNotNull(report);
assertEquals(report.getStatus().toString(), "FAILED");
assertNotNull(report.getError());

assertNotNull(report.getWorkContext());

Mockito.verify(this.workFlowDelegate, Mockito.times(1)).getWorkFlowExecutionByName(Mockito.any());
Mockito.verify(this.workFlowDelegate, Mockito.never()).initWorkFlowContext(Mockito.any());
Mockito.verify(this.workFlowDefinitionRepository, Mockito.times(1)).findFirstByName(Mockito.any());
Mockito.verify(this.workFlowWorkRepository, Mockito.never()).findByWorkDefinitionId(Mockito.any());
}

@Test
void getWorkFlowByIDTestWithValidData() {
// given
Expand Down

0 comments on commit d8c5cc8

Please sign in to comment.