diff --git a/integration-tests/src/test/java/com/redhat/parodos/flows/SimpleRestartWorkFlowTest.java b/integration-tests/src/test/java/com/redhat/parodos/flows/SimpleRestartWorkFlowTest.java new file mode 100644 index 000000000..c09b4917c --- /dev/null +++ b/integration-tests/src/test/java/com/redhat/parodos/flows/SimpleRestartWorkFlowTest.java @@ -0,0 +1,180 @@ +package com.redhat.parodos.flows; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +import com.redhat.parodos.flows.common.WorkFlowTestBuilder; +import com.redhat.parodos.flows.common.WorkFlowTestBuilder.TestComponents; +import com.redhat.parodos.sdk.api.WorkflowApi; +import com.redhat.parodos.sdk.api.WorkflowDefinitionApi; +import com.redhat.parodos.sdk.invoker.ApiException; +import com.redhat.parodos.sdk.model.ArgumentRequestDTO; +import com.redhat.parodos.sdk.model.WorkFlowContextResponseDTO; +import com.redhat.parodos.sdk.model.WorkFlowDefinitionResponseDTO; +import com.redhat.parodos.sdk.model.WorkFlowExecutionResponseDTO; +import com.redhat.parodos.sdk.model.WorkFlowExecutionResponseDTO.WorkStatusEnum; +import com.redhat.parodos.sdk.model.WorkFlowRequestDTO; +import com.redhat.parodos.sdk.model.WorkFlowStatusResponseDTO; +import com.redhat.parodos.sdk.model.WorkRequestDTO; +import com.redhat.parodos.sdkutils.WorkFlowServiceUtils; +import com.redhat.parodos.workflow.consts.WorkFlowConstants; +import lombok.extern.slf4j.Slf4j; +import static org.assertj.core.api.Assertions.assertThat; +import org.jetbrains.annotations.NotNull; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import org.junit.Test; + +@Slf4j +public class SimpleRestartWorkFlowTest { + + private static final String WORKFLOW_NAME = "onboardingComplexAssessment" + WorkFlowConstants.ASSESSMENT_WORKFLOW; + + @NotNull + private static WorkFlowContextResponseDTO assertAssessmentWorkflowExecutionSuccess(WorkflowApi workflowApi, + WorkFlowExecutionResponseDTO workFlowResponseDTO, + WorkFlowStatusResponseDTO.StatusEnum expectedStatus) throws ApiException { + // wait till assessment workflow is completed + WorkFlowStatusResponseDTO workFlowStatusResponseDTO = WorkFlowServiceUtils.waitWorkflowStatusAsync(workflowApi, + workFlowResponseDTO.getWorkFlowExecutionId()); + assertNotNull(workFlowStatusResponseDTO); + assertThat(workFlowStatusResponseDTO.getStatus()).isEqualTo(expectedStatus); + + WorkFlowContextResponseDTO workflowOptions = workflowApi + .getWorkflowParameters(workFlowResponseDTO.getWorkFlowExecutionId(), List.of("WORKFLOW_OPTIONS")); + assertNotNull(workflowOptions); + assertNotNull(workflowOptions.getWorkFlowOptions()); + assertNotNull(workflowOptions.getWorkFlowOptions().getNewOptions()); + + return workflowOptions; + } + + @Test + public void runRestartWorkFlow() throws ApiException, InterruptedException { + log.info("******** Running the assessment workFlow ********"); + TestComponents components = new WorkFlowTestBuilder().withDefaultProject().withWorkFlowDefinition(WORKFLOW_NAME) + .build(); + + // Define WorkFlowRequest + WorkFlowRequestDTO workFlowRequestDTO = new WorkFlowRequestDTO(); + workFlowRequestDTO.setProjectId(components.project().getId()); + workFlowRequestDTO.setWorkFlowName(WORKFLOW_NAME); + workFlowRequestDTO.setWorks(List.of(new WorkRequestDTO() + .arguments(List.of(new ArgumentRequestDTO().key("GIT_REPO_URL").value("git_repo_url"))))); + + log.info("Running the Assessment to see what WorkFlows are eligible for this situation:"); + WorkflowApi workflowApi = new WorkflowApi(components.apiClient()); + WorkFlowExecutionResponseDTO workFlowResponseDTO = workflowApi.execute(workFlowRequestDTO); + assertEquals(WorkStatusEnum.IN_PROGRESS, workFlowResponseDTO.getWorkStatus()); + log.info("workflow submitted successfully with response: {}", workFlowResponseDTO); + + WorkFlowContextResponseDTO workflowOptions = assertAssessmentWorkflowExecutionSuccess(workflowApi, workFlowResponseDTO, + WorkFlowStatusResponseDTO.StatusEnum.COMPLETED); + String infrastructureOption = workflowOptions.getWorkFlowOptions().getNewOptions().get(0).getWorkFlowName(); + log.info("The Following Option Is Available: {}", infrastructureOption); + + log.info("Restarting the assessment WorkFlow"); + workFlowResponseDTO = workflowApi.restartWorkflow(workFlowResponseDTO.getWorkFlowExecutionId()); + assertEquals(WorkStatusEnum.IN_PROGRESS, workFlowResponseDTO.getWorkStatus()); + log.info("restarted workflow submitted successfully with response: {}", workFlowResponseDTO); + workflowOptions = assertAssessmentWorkflowExecutionSuccess(workflowApi, workFlowResponseDTO, + WorkFlowStatusResponseDTO.StatusEnum.COMPLETED); + String infrastructureOptionRestarted = workflowOptions.getWorkFlowOptions().getNewOptions().get(0).getWorkFlowName(); + log.info("The Following Option Is Available after restart: {}", infrastructureOptionRestarted); + + assertEquals(infrastructureOption, infrastructureOptionRestarted); + + } + + @Test + public void runRestartWorkFlowComplex() throws ApiException, InterruptedException { + log.info("******** Running The Complex workFlow ********"); + TestComponents components = new WorkFlowTestBuilder().withDefaultProject().withWorkFlowDefinition(WORKFLOW_NAME) + .build(); + + // Define WorkFlowRequest + WorkFlowRequestDTO workFlowRequestDTO = new WorkFlowRequestDTO(); + workFlowRequestDTO.setProjectId(components.project().getId()); + workFlowRequestDTO.setWorkFlowName(WORKFLOW_NAME); + workFlowRequestDTO.setWorks(List.of(new WorkRequestDTO() + .arguments(List.of(new ArgumentRequestDTO().key("GIT_REPO_URL").value("git_repo_url"))))); + + log.info("Running the Assessment to see what WorkFlows are eligible for this situation:"); + WorkflowApi workflowApi = new WorkflowApi(components.apiClient()); + WorkFlowExecutionResponseDTO workFlowResponseDTO = workflowApi.execute(workFlowRequestDTO); + assertEquals(WorkStatusEnum.IN_PROGRESS, workFlowResponseDTO.getWorkStatus()); + log.info("workflow submitted successfully with response: {}", workFlowResponseDTO); + + // wait till assessment workflow is completed + WorkFlowContextResponseDTO workflowOptions = assertAssessmentWorkflowExecutionSuccess(workflowApi, workFlowResponseDTO, + WorkFlowStatusResponseDTO.StatusEnum.COMPLETED); + WorkFlowStatusResponseDTO workFlowStatusResponseDTO; + String infrastructureOption = workflowOptions.getWorkFlowOptions().getNewOptions().get(0).getWorkFlowName(); + log.info("The Following Option Is Available: {}", infrastructureOption); + + log.info("Running the onboarding WorkFlow"); + log.info("executes 3 tasks in Parallel with a WorkFlowChecker"); + WorkflowDefinitionApi workflowDefinitionApi = new WorkflowDefinitionApi(); + List workFlowDefinitions = workflowDefinitionApi + .getWorkFlowDefinitions(infrastructureOption); + + assertNotNull(workFlowDefinitions); + assertTrue(workFlowDefinitions.size() > 0); + assertNotNull("There is no valid Onboarding workflow id", workFlowDefinitions.get(0).getId()); + assertEquals("There is no valid Onboarding workflow name", workFlowDefinitions.get(0).getName(), + infrastructureOption); + log.info("Onboarding workflow id {}", workFlowDefinitions.get(0).getId()); + log.info("Onboarding workflow name {}", workFlowDefinitions.get(0).getName()); + + WorkRequestDTO work1 = new WorkRequestDTO(); + work1.setWorkName("certWorkFlowTask"); + work1.setArguments(Arrays.asList(new ArgumentRequestDTO().key("user-id").value("test-user-id"), + new ArgumentRequestDTO().key("api-server").value("api.com"))); + + WorkRequestDTO work2 = new WorkRequestDTO(); + work2.setWorkName("adGroupWorkFlowTask"); + work2.setArguments(Arrays.asList(new ArgumentRequestDTO().key("user-id").value("test-user-id"), + new ArgumentRequestDTO().key("api-server").value("api.com"))); + + WorkRequestDTO work3 = new WorkRequestDTO(); + work3.setWorkName("dynatraceWorkFlowTask"); + work3.setArguments(Arrays.asList(new ArgumentRequestDTO().key("user-id").value("test-user-id"), + new ArgumentRequestDTO().key("api-server").value("api.com"))); + + workFlowRequestDTO.setProjectId(components.project().getId()); + workFlowRequestDTO.setWorkFlowName(workFlowDefinitions.get(0).getName()); + workFlowRequestDTO.setWorks(Arrays.asList(work1, work2, work3)); + workFlowResponseDTO = workflowApi.execute(workFlowRequestDTO); + + assertNotNull("There is no valid WorkFlowExecutionId", workFlowResponseDTO.getWorkFlowExecutionId()); + assertEquals(WorkStatusEnum.IN_PROGRESS, workFlowResponseDTO.getWorkStatus()); + log.info("Onboarding workflow execution id: {}", workFlowResponseDTO.getWorkFlowExecutionId()); + + workFlowStatusResponseDTO = WorkFlowServiceUtils.waitWorkflowStatusAsync(workflowApi, + workFlowResponseDTO.getWorkFlowExecutionId()); + + assertNotNull(workFlowStatusResponseDTO); + assertNotNull(workFlowStatusResponseDTO.getWorkFlowExecutionId()); + assertEquals(WorkFlowStatusResponseDTO.StatusEnum.COMPLETED, workFlowStatusResponseDTO.getStatus()); + log.info("Onboarding workflow execution completed with status {}", workFlowStatusResponseDTO.getStatus()); + + log.info("Restarting the onboarding WorkFlow"); + workFlowResponseDTO = workflowApi.restartWorkflow(workFlowResponseDTO.getWorkFlowExecutionId()); + assertEquals(WorkStatusEnum.IN_PROGRESS, workFlowResponseDTO.getWorkStatus()); + log.info("restarted workflow submitted successfully with response: {}", workFlowResponseDTO); + WorkFlowStatusResponseDTO restartedWorkFlowStatusResponseDTO = WorkFlowServiceUtils.waitWorkflowStatusAsync(workflowApi, + workFlowResponseDTO.getWorkFlowExecutionId()); + + assertNotNull(restartedWorkFlowStatusResponseDTO); + List works = workFlowStatusResponseDTO.getWorks().stream().map(workStatusResponseDTO -> workStatusResponseDTO.getName()).collect(Collectors.toList()); + List restartedWorks = restartedWorkFlowStatusResponseDTO.getWorks().stream().map(workStatusResponseDTO -> workStatusResponseDTO.getName()).collect(Collectors.toList()); + assertEquals(works, restartedWorks); + assertNotNull(restartedWorkFlowStatusResponseDTO.getWorkFlowExecutionId()); + assertEquals(WorkFlowStatusResponseDTO.StatusEnum.COMPLETED, restartedWorkFlowStatusResponseDTO.getStatus()); + log.info("Restarted onboarding workflow execution completed with status {}", restartedWorkFlowStatusResponseDTO.getStatus()); + + } + +}