Commit 2d4b6f1d authored by Aalekh Jain's avatar Aalekh Jain
Browse files

Updated `WorkflowRunServiceTest.java`

parent dfe24513
......@@ -27,6 +27,7 @@ import org.opengroup.osdu.workflow.model.WorkflowRunResponse;
import org.opengroup.osdu.workflow.model.WorkflowRunsPage;
import org.opengroup.osdu.workflow.model.WorkflowStatusType;
import org.opengroup.osdu.workflow.provider.interfaces.IWorkflowEngineService;
import org.opengroup.osdu.workflow.provider.interfaces.IWorkflowManagerService;
import org.opengroup.osdu.workflow.provider.interfaces.IWorkflowMetadataRepository;
import org.opengroup.osdu.workflow.provider.interfaces.IWorkflowRunRepository;
import org.opengroup.osdu.workflow.service.WorkflowRunServiceImpl;
......@@ -81,6 +82,20 @@ public class WorkflowRunServiceTest {
" \"createdBy\": \"user@email.com\",\n" +
" \"version\": 1\n" +
"}";
private static final String SYSTEM_WORKFLOW_METADATA = "{\n" +
" \"workflowId\": \"some-dag-name\",\n" +
" \"workflowName\": \"some-dag-name\",\n" +
" \"isSystemWorkflow\": true,\n" +
" \"registrationInstructions\": {\n" +
" \"dagName\": \"dag-name\"\n" +
"},\n" +
" \"description\": \"This is a test workflow\",\n" +
" \"creationTimestamp\": 1600144876028,\n" +
" \"createdBy\": \"user@email.com\",\n" +
" \"version\": 1\n" +
"}";
private static final String WORKFLOW_TRIGGER_REQUEST_DATA = "{\n" +
" \"runId\": \"d13f7fd0-d27e-4176-8d60-6e9aad86e347\",\n" +
" \"executionContext\": {\n" +
......@@ -133,6 +148,9 @@ public class WorkflowRunServiceTest {
@Mock
private IWorkflowRunRepository workflowRunRepository;
@Mock
private IWorkflowManagerService workflowManagerService;
@Mock
private DpsHeaders dpsHeaders;
......@@ -157,7 +175,7 @@ public class WorkflowRunServiceTest {
.readValue(WORKFLOW_TRIGGER_RESPONSE, TriggerWorkflowResponse.class);
final TriggerWorkflowRequest request =
OBJECT_MAPPER.readValue(WORKFLOW_TRIGGER_REQUEST_DATA, TriggerWorkflowRequest.class);
when(workflowMetadataRepository.getWorkflow(eq(WORKFLOW_NAME))).thenReturn(workflowMetadata);
when(workflowManagerService.getWorkflowByName(eq(WORKFLOW_NAME))).thenReturn(workflowMetadata);
final ArgumentCaptor<WorkflowEngineRequest> workflowEngineRequestArgumentCaptor =
ArgumentCaptor.forClass(WorkflowEngineRequest.class);
when(workflowEngineService.triggerWorkflow(workflowEngineRequestArgumentCaptor.capture(),
......@@ -176,7 +194,7 @@ public class WorkflowRunServiceTest {
.triggerWorkflow(WORKFLOW_NAME, request);
//then
verify(workflowMetadataRepository).getWorkflow(eq(WORKFLOW_NAME));
verify(workflowManagerService).getWorkflowByName(eq(WORKFLOW_NAME));
verify(workflowEngineService)
.triggerWorkflow(any(WorkflowEngineRequest.class), eq(createWorkflowPayload(RUN_ID, request)));
WorkflowEngineRequest capturedWorkflowEngineRequest =
......@@ -184,12 +202,13 @@ public class WorkflowRunServiceTest {
assertThat(capturedWorkflowEngineRequest.getWorkflowName(), equalTo(WORKFLOW_NAME));
assertThat(capturedWorkflowEngineRequest.getRunId(), equalTo(RUN_ID));
assertThat(capturedWorkflowEngineRequest.getWorkflowId(), equalTo(WORKFLOW_NAME));
assertThat(capturedWorkflowEngineRequest.isSystemWorkflow(), equalTo(false));
verify(workflowRunRepository).saveWorkflowRun(any(WorkflowRun.class));
verify(dpsHeaders).getAuthorization();
verify(dpsHeaders).getUserEmail();
verify(dpsHeaders).getCorrelationId();
verify(statusPublisher).publishStatusWithNoErrors(any(), any(DpsHeaders.class), any(String.class), any(Status.class));
assertThat(returnedWorkflowRun, equalTo(buildWorkflowRunResponse(responseWorkflowRun)));
assertThat(returnedWorkflowRun, equalTo(buildWorkflowRunResponse(responseWorkflowRun)));
assertThat(workflowRunArgumentCaptor.getValue().getRunId(), equalTo(RUN_ID));
assertThat(workflowRunArgumentCaptor.getValue().getWorkflowName(), equalTo(WORKFLOW_NAME));
assertThat(workflowRunArgumentCaptor.getValue().getSubmittedBy(), equalTo(USER_EMAIL));
......@@ -198,10 +217,61 @@ public class WorkflowRunServiceTest {
equalTo(WorkflowStatusType.SUBMITTED));
}
@Test
void testTriggerWorkflowWithExistingSystemWorkflowId() throws Exception {
final WorkflowMetadata workflowMetadata = OBJECT_MAPPER
.readValue(SYSTEM_WORKFLOW_METADATA, WorkflowMetadata.class);
final TriggerWorkflowResponse triggerWorkflowResponse = OBJECT_MAPPER
.readValue(WORKFLOW_TRIGGER_RESPONSE, TriggerWorkflowResponse.class);
final ArgumentCaptor<Long> startTimeStampArgumentCaptor = ArgumentCaptor.forClass(Long.class);
final TriggerWorkflowRequest request =
OBJECT_MAPPER.readValue(WORKFLOW_TRIGGER_REQUEST_DATA, TriggerWorkflowRequest.class);
when(workflowManagerService.getWorkflowByName(eq(WORKFLOW_NAME))).thenReturn(workflowMetadata);
final ArgumentCaptor<WorkflowEngineRequest> workflowEngineRequestArgumentCaptor =
ArgumentCaptor.forClass(WorkflowEngineRequest.class);
when(workflowEngineService.triggerWorkflow(workflowEngineRequestArgumentCaptor.capture(),
eq(createWorkflowPayload(RUN_ID, request)))).thenReturn(triggerWorkflowResponse);
when(dpsHeaders.getAuthorization()).thenReturn(AUTH_TOKEN);
when(dpsHeaders.getUserEmail()).thenReturn(USER_EMAIL);
when(dpsHeaders.getCorrelationId()).thenReturn(CORRELATION_ID);
final ArgumentCaptor<WorkflowRun> workflowRunArgumentCaptor = ArgumentCaptor
.forClass(WorkflowRun.class);
final WorkflowRun responseWorkflowRun = mock(WorkflowRun.class);
when(workflowRunRepository.saveWorkflowRun(workflowRunArgumentCaptor.capture()))
.thenReturn(responseWorkflowRun);
//when
final WorkflowRunResponse returnedWorkflowRun = workflowRunService
.triggerWorkflow(WORKFLOW_NAME, request);
//then
verify(workflowManagerService).getWorkflowByName(eq(WORKFLOW_NAME));
verify(workflowEngineService)
.triggerWorkflow(any(WorkflowEngineRequest.class), eq(createWorkflowPayload(RUN_ID, request)));
WorkflowEngineRequest capturedWorkflowEngineRequest =
workflowEngineRequestArgumentCaptor.getValue();
assertThat(capturedWorkflowEngineRequest.getWorkflowName(), equalTo(WORKFLOW_NAME));
assertThat(capturedWorkflowEngineRequest.getRunId(), equalTo(RUN_ID));
assertThat(capturedWorkflowEngineRequest.getWorkflowId(), equalTo(WORKFLOW_NAME));
assertThat(capturedWorkflowEngineRequest.isSystemWorkflow(), equalTo(true));
verify(workflowRunRepository).saveWorkflowRun(any(WorkflowRun.class));
verify(dpsHeaders).getAuthorization();
verify(dpsHeaders).getUserEmail();
verify(dpsHeaders).getCorrelationId();
verify(statusPublisher).publishStatusWithNoErrors(any(), any(DpsHeaders.class), any(String.class), any(Status.class));
assertThat(returnedWorkflowRun, equalTo(buildWorkflowRunResponse(responseWorkflowRun)));
assertThat(workflowRunArgumentCaptor.getValue().getRunId(), equalTo(RUN_ID));
assertThat(workflowRunArgumentCaptor.getValue().getWorkflowName(), equalTo(WORKFLOW_NAME));
assertThat(workflowRunArgumentCaptor.getValue().getSubmittedBy(), equalTo(USER_EMAIL));
assertThat(workflowRunArgumentCaptor.getValue().getWorkflowEngineExecutionDate(), equalTo(EXECUTION_DATE));
assertThat(workflowRunArgumentCaptor.getValue().getStatus(),
equalTo(WorkflowStatusType.SUBMITTED));
}
@Test
void testTriggerWorkflowWithNonExistingWorkflowId() throws Exception {
//given
when(workflowMetadataRepository.getWorkflow(eq(WORKFLOW_NAME)))
when(workflowManagerService.getWorkflowByName(eq(WORKFLOW_NAME)))
.thenThrow(WorkflowNotFoundException.class);
final TriggerWorkflowRequest request =
OBJECT_MAPPER.readValue(WORKFLOW_TRIGGER_REQUEST_DATA, TriggerWorkflowRequest.class);
......@@ -210,7 +280,7 @@ public class WorkflowRunServiceTest {
Assertions.assertThrows(WorkflowNotFoundException.class, () -> {
workflowRunService.triggerWorkflow(WORKFLOW_NAME, request);
});
verify(workflowMetadataRepository).getWorkflow(eq(WORKFLOW_NAME));
verify(workflowManagerService).getWorkflowByName(eq(WORKFLOW_NAME));
}
@Test
......@@ -219,7 +289,7 @@ public class WorkflowRunServiceTest {
final WorkflowMetadata workflowMetadata = OBJECT_MAPPER.readValue(WORKFLOW_METADATA, WorkflowMetadata.class);
final TriggerWorkflowRequest request =
OBJECT_MAPPER.readValue(WORKFLOW_TRIGGER_REQUEST_DATA, TriggerWorkflowRequest.class);
when(workflowMetadataRepository.getWorkflow(eq(WORKFLOW_NAME))).thenReturn(workflowMetadata);
when(workflowManagerService.getWorkflowByName(eq(WORKFLOW_NAME))).thenReturn(workflowMetadata);
final ArgumentCaptor<WorkflowEngineRequest> workflowEngineRequestArgumentCaptor =
ArgumentCaptor.forClass(WorkflowEngineRequest.class);
doThrow(new CoreException("Failed to trigger workflow"))
......@@ -233,7 +303,7 @@ public class WorkflowRunServiceTest {
workflowRunService.triggerWorkflow(WORKFLOW_NAME, request);
});
verify(workflowMetadataRepository).getWorkflow(eq(WORKFLOW_NAME));
verify(workflowManagerService).getWorkflowByName(eq(WORKFLOW_NAME));
verify(workflowEngineService)
.triggerWorkflow(any(WorkflowEngineRequest.class), eq(createWorkflowPayload(RUN_ID, request)));
WorkflowEngineRequest capturedWorkflowEngineRequest =
......@@ -260,7 +330,7 @@ public class WorkflowRunServiceTest {
when(workflowRunRepository.getWorkflowRun(eq(WORKFLOW_NAME), eq(RUN_ID)))
.thenReturn(submittedWorkflowRun);
when(workflowMetadataRepository.getWorkflow(eq(WORKFLOW_NAME))).thenReturn(workflowMetadata);
when(workflowManagerService.getWorkflowByName(eq(WORKFLOW_NAME))).thenReturn(workflowMetadata);
final ArgumentCaptor<WorkflowEngineRequest> workflowEngineRequestArgumentCaptor =
ArgumentCaptor.forClass(WorkflowEngineRequest.class);
when(workflowEngineService.getWorkflowRunStatus(workflowEngineRequestArgumentCaptor.capture())).
......@@ -273,7 +343,7 @@ public class WorkflowRunServiceTest {
getWorkflowRunByName(WORKFLOW_NAME, RUN_ID);
//then
verify(workflowMetadataRepository).getWorkflow(eq(WORKFLOW_NAME));
verify(workflowManagerService).getWorkflowByName(eq(WORKFLOW_NAME));
verify(workflowRunRepository).getWorkflowRun(eq(WORKFLOW_NAME), eq(RUN_ID));
verify(workflowEngineService).getWorkflowRunStatus(any(WorkflowEngineRequest.class));
WorkflowEngineRequest capturedWorkflowEngineRequest =
......@@ -307,7 +377,7 @@ public class WorkflowRunServiceTest {
final ArgumentCaptor<WorkflowRun> workflowRunArgumentCaptor = ArgumentCaptor
.forClass(WorkflowRun.class);
when(workflowMetadataRepository.getWorkflow(eq(WORKFLOW_NAME))).thenReturn(workflowMetadata);
when(workflowManagerService.getWorkflowByName(eq(WORKFLOW_NAME))).thenReturn(workflowMetadata);
when(workflowRunRepository.getWorkflowRun(eq(WORKFLOW_NAME), eq(RUN_ID))).
thenReturn(submittedWorkflowRun);
final ArgumentCaptor<WorkflowEngineRequest> workflowEngineRequestArgumentCaptor =
......@@ -323,7 +393,7 @@ public class WorkflowRunServiceTest {
workflowRunService.getWorkflowRunByName(WORKFLOW_NAME, RUN_ID);
//then
verify(workflowMetadataRepository).getWorkflow(eq(WORKFLOW_NAME));
verify(workflowManagerService).getWorkflowByName(eq(WORKFLOW_NAME));
verify(workflowRunRepository).getWorkflowRun(eq(WORKFLOW_NAME), eq(RUN_ID));
verify(workflowEngineService).getWorkflowRunStatus(any(WorkflowEngineRequest.class));
WorkflowEngineRequest capturedWorkflowEngineRequest =
......@@ -354,7 +424,7 @@ public class WorkflowRunServiceTest {
final WorkflowMetadata workflowMetadata = OBJECT_MAPPER.readValue(WORKFLOW_METADATA, WorkflowMetadata.class);
final WorkflowRun runningWorkflowRun = OBJECT_MAPPER.readValue(RUNNING_WORKFLOW_RUN, WorkflowRun.class);
when(workflowMetadataRepository.getWorkflow(eq(WORKFLOW_NAME))).thenReturn(workflowMetadata);
when(workflowManagerService.getWorkflowByName(eq(WORKFLOW_NAME))).thenReturn(workflowMetadata);
when(workflowRunRepository.getWorkflowRun(eq(WORKFLOW_NAME), eq(RUN_ID))).
thenReturn(runningWorkflowRun);
final ArgumentCaptor<WorkflowEngineRequest> workflowEngineRequestArgumentCaptor =
......@@ -367,7 +437,7 @@ public class WorkflowRunServiceTest {
workflowRunService.getWorkflowRunByName(WORKFLOW_NAME, RUN_ID);
//then
verify(workflowMetadataRepository).getWorkflow(eq(WORKFLOW_NAME));
verify(workflowManagerService).getWorkflowByName(eq(WORKFLOW_NAME));
verify(workflowRunRepository).getWorkflowRun(eq(WORKFLOW_NAME), eq(RUN_ID));
verify(workflowEngineService).getWorkflowRunStatus(any(WorkflowEngineRequest.class));
WorkflowEngineRequest capturedWorkflowEngineRequest =
......@@ -524,7 +594,7 @@ public class WorkflowRunServiceTest {
when(workflowRunRepository.getWorkflowRunsByWorkflowName(eq(WORKFLOW_NAME), anyInt(),
eq(TEST_CURSOR))).thenReturn(new WorkflowRunsPage(Arrays.asList(submittedWorkflowRun),
null));
when(workflowMetadataRepository.getWorkflow(eq(WORKFLOW_NAME))).thenReturn(workflowMetadata);
when(workflowManagerService.getWorkflowByName(eq(WORKFLOW_NAME))).thenReturn(workflowMetadata);
when(workflowEngineService.getWorkflowRunStatus(requestArgumentCaptor.capture()))
.thenReturn(WorkflowStatusType.RUNNING);
when(workflowRunRepository.updateWorkflowRun(workflowRunArgumentCaptor.capture()))
......@@ -545,7 +615,7 @@ public class WorkflowRunServiceTest {
verify(workflowRunRepository).getWorkflowRunsByWorkflowName(eq(WORKFLOW_NAME), anyInt(),
eq(TEST_CURSOR));
verify(workflowRunRepository, times(0)).deleteWorkflowRuns(eq(WORKFLOW_NAME), any(List.class));
verify(workflowMetadataRepository).getWorkflow(eq(WORKFLOW_NAME));
verify(workflowManagerService).getWorkflowByName(eq(WORKFLOW_NAME));
verify(workflowEngineService).getWorkflowRunStatus(any(WorkflowEngineRequest.class));
verify(workflowRunRepository).updateWorkflowRun(any(WorkflowRun.class));
......@@ -584,7 +654,7 @@ public class WorkflowRunServiceTest {
when(workflowRunRepository.getWorkflowRunsByWorkflowName(eq(WORKFLOW_NAME), anyInt(),
eq(TEST_CURSOR))).thenReturn(new WorkflowRunsPage(Arrays.asList(runningWorkflowRun),
null));
when(workflowMetadataRepository.getWorkflow(eq(WORKFLOW_NAME))).thenReturn(workflowMetadata);
when(workflowManagerService.getWorkflowByName(eq(WORKFLOW_NAME))).thenReturn(workflowMetadata);
when(workflowEngineService.getWorkflowRunStatus(requestArgumentCaptor.capture()))
.thenReturn(WorkflowStatusType.FINISHED);
when(workflowRunRepository.updateWorkflowRun(workflowRunArgumentCaptor.capture()))
......@@ -601,7 +671,7 @@ public class WorkflowRunServiceTest {
verify(workflowRunRepository).getWorkflowRunsByWorkflowName(eq(WORKFLOW_NAME), anyInt(),
eq(TEST_CURSOR));
verify(workflowRunRepository).deleteWorkflowRuns(eq(WORKFLOW_NAME), any(List.class));
verify(workflowMetadataRepository, times(2)).getWorkflow(eq(WORKFLOW_NAME));
verify(workflowManagerService, times(2)).getWorkflowByName(eq(WORKFLOW_NAME));
verify(workflowEngineService, times(2))
.getWorkflowRunStatus(any(WorkflowEngineRequest.class));
verify(workflowRunRepository, times(2))
......@@ -654,25 +724,25 @@ public class WorkflowRunServiceTest {
Arrays.asList(finishedWorkflowRun, submittedWorkflowRun, runningWorkflowRun);
final Map<String, Object> config = new HashMap<>();
final WorkflowMetadata workflowMetadata = mock(WorkflowMetadata.class);
when(workflowMetadataRepository.getWorkflow(eq(WORKFLOW_NAME))).thenReturn(workflowMetadata);
when(workflowManagerService.getWorkflowByName(eq(WORKFLOW_NAME))).thenReturn(workflowMetadata);
when(workflowRunRepository.getAllRunInstancesOfWorkflow(eq(WORKFLOW_NAME), eq(config))).thenReturn(workflowRuns);
//when and then
Assertions.assertEquals(workflowRuns, workflowRunService.getAllRunInstancesOfWorkflow(WORKFLOW_NAME, config));
verify(workflowRunRepository).getAllRunInstancesOfWorkflow(eq(WORKFLOW_NAME), eq(config));
verify(workflowMetadataRepository).getWorkflow(eq(WORKFLOW_NAME));
verify(workflowManagerService).getWorkflowByName(eq(WORKFLOW_NAME));
}
@Test
void testGetAllRunInstancesOfWorkflowForNonExistentWorkflowName() {
//given
when(workflowMetadataRepository.getWorkflow(WORKFLOW_NAME)).thenThrow(WorkflowNotFoundException.class);
when(workflowManagerService.getWorkflowByName(WORKFLOW_NAME)).thenThrow(WorkflowNotFoundException.class);
//when and then
Assertions.assertThrows(WorkflowNotFoundException.class, () -> {
workflowRunService.getAllRunInstancesOfWorkflow(WORKFLOW_NAME, new HashMap<>());
});
verify(workflowMetadataRepository).getWorkflow(eq(WORKFLOW_NAME));
verify(workflowManagerService).getWorkflowByName(eq(WORKFLOW_NAME));
}
private Map<String, Object> createWorkflowPayload(final String runId,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment