From 33bc735010ef94896b30c539ad71da46741820bb Mon Sep 17 00:00:00 2001 From: Annel Ketcha Date: Fri, 28 Jul 2023 16:59:08 -0400 Subject: [PATCH] refactor tasks and tests --- ...jectAccessRequestApprovalWorkFlowTask.java | 4 +- ...ctAccessRequestEscalationWorkFlowTask.java | 4 +- ...AccessRequestApprovalWorkFlowTaskTest.java | 70 ------------ .../ProjectAccessRequestWorkFlowTaskTest.java | 11 -- ...equestApprovalWorkFlowCheckerTaskTest.java | 107 ------------------ ...cessRequestEscalationWorkFlowTaskTest.java | 70 ------------ .../client/ProjectRequesterClient.java | 6 +- 7 files changed, 7 insertions(+), 265 deletions(-) diff --git a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/project/ProjectAccessRequestApprovalWorkFlowTask.java b/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/project/ProjectAccessRequestApprovalWorkFlowTask.java index 01e382fed..1642e9cd9 100644 --- a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/project/ProjectAccessRequestApprovalWorkFlowTask.java +++ b/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/project/ProjectAccessRequestApprovalWorkFlowTask.java @@ -69,13 +69,13 @@ public WorkReport execute(WorkContext workContext) { NotificationMessageCreateRequestDTO notificationMessageCreateRequestDTO = new NotificationMessageCreateRequestDTO(); notificationMessageCreateRequestDTO.setSubject(NOTIFICATION_SUBJECT_ACCESS_REQUEST_APPROVAL); notificationMessageCreateRequestDTO.setUsernames(Arrays.stream(approvalUsernames.split(",")).toList()); - notificationMessageCreateRequestDTO.setBody(buildMessage( + notificationMessageCreateRequestDTO.setBody(getMessage( String.format("%s/api/v1/projects/access/%s", projectRequester.getBasePath(), accessRequestId))); notifier.send(notificationMessageCreateRequestDTO); return new DefaultWorkReport(WorkStatus.COMPLETED, workContext); } - private String buildMessage(String url) { + private String getMessage(String url) { return "Hi there," + "\n" + "A project request awaits your approval. Use the url below to approve or reject the request." + "\n" + "Url: " + url + "\n" + "Thank you," + "\n" + "The Parodos Team"; diff --git a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/project/escalation/ProjectAccessRequestEscalationWorkFlowTask.java b/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/project/escalation/ProjectAccessRequestEscalationWorkFlowTask.java index 2b1973c63..7b698fddc 100644 --- a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/project/escalation/ProjectAccessRequestEscalationWorkFlowTask.java +++ b/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/project/escalation/ProjectAccessRequestEscalationWorkFlowTask.java @@ -67,13 +67,13 @@ public WorkReport execute(WorkContext workContext) { NotificationMessageCreateRequestDTO notificationMessageCreateRequestDTO = new NotificationMessageCreateRequestDTO(); notificationMessageCreateRequestDTO.setSubject(NOTIFICATION_SUBJECT_ACCESS_REQUEST_ESCALATION); notificationMessageCreateRequestDTO.addUsernamesItem(escalationUsername); - notificationMessageCreateRequestDTO.setBody(buildMessage( + notificationMessageCreateRequestDTO.setBody(getMessage( String.format("%s/api/v1/projects/access/%s/status", projectRequester.getBasePath(), accessRequestId))); notifier.send(notificationMessageCreateRequestDTO); return new DefaultWorkReport(WorkStatus.COMPLETED, workContext); } - private String buildMessage(String url) { + private String getMessage(String url) { return "Hi there," + "\n" + "A project request below has been escalated as being pending for a while." + "\n" + "Url: " + url + "\n" + "Thank you," + "\n" + "The Parodos Team"; } diff --git a/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/project/ProjectAccessRequestApprovalWorkFlowTaskTest.java b/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/project/ProjectAccessRequestApprovalWorkFlowTaskTest.java index b57bff72a..1b6f5f922 100644 --- a/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/project/ProjectAccessRequestApprovalWorkFlowTaskTest.java +++ b/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/project/ProjectAccessRequestApprovalWorkFlowTaskTest.java @@ -1,75 +1,5 @@ package com.redhat.parodos.tasks.project; -import java.util.UUID; - -import com.redhat.parodos.infrastructure.Notifier; -import com.redhat.parodos.infrastructure.ProjectRequester; -import com.redhat.parodos.workflow.exception.MissingParameterException; -import com.redhat.parodos.workflows.work.WorkContext; -import com.redhat.parodos.workflows.work.WorkReport; -import com.redhat.parodos.workflows.work.WorkStatus; -import lombok.SneakyThrows; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; - -import static org.junit.Assert.assertEquals; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.spy; - -@RunWith(MockitoJUnitRunner.class) public class ProjectAccessRequestApprovalWorkFlowTaskTest { - private static final String ACCESS_REQUEST_ID_PARAMETER_NAME = "ACCESS_REQUEST_ID"; - - private static final String ACCESS_REQUEST_APPROVAL_USERNAMES_PARAMETER_NAME = "ACCESS_REQUEST_APPROVAL_USERNAMES"; - - private static final String ACCESS_REQUEST_APPROVAL_USERNAMES_VALUE_TEST = "approval-username-test"; - - @Mock - private ProjectRequester projectRequester; - - @Mock - private Notifier notifier; - - private WorkContext workContext; - - private ProjectAccessRequestApprovalWorkFlowTask projectAccessRequestApprovalWorkFlowTask; - - @Before - public void setUp() { - this.projectAccessRequestApprovalWorkFlowTask = spy( - new ProjectAccessRequestApprovalWorkFlowTask(projectRequester, notifier)); - this.projectAccessRequestApprovalWorkFlowTask.setBeanName("projectAccessRequestApprovalWorkFlowTask"); - workContext = new WorkContext(); - } - - @Test - @SneakyThrows - public void executeSuccess() { - doReturn(String.valueOf(UUID.randomUUID())).when(this.projectAccessRequestApprovalWorkFlowTask) - .getRequiredParameterValue(eq(ACCESS_REQUEST_ID_PARAMETER_NAME)); - doReturn(ACCESS_REQUEST_APPROVAL_USERNAMES_VALUE_TEST).when(this.projectAccessRequestApprovalWorkFlowTask) - .getRequiredParameterValue(eq(ACCESS_REQUEST_APPROVAL_USERNAMES_PARAMETER_NAME)); - doNothing().when(notifier).send(any()); - - WorkReport workReport = projectAccessRequestApprovalWorkFlowTask.execute(workContext); - assertEquals(WorkStatus.COMPLETED, workReport.getStatus()); - } - - @Test - @SneakyThrows - public void executeFail() { - doThrow(MissingParameterException.class).when(this.projectAccessRequestApprovalWorkFlowTask) - .getRequiredParameterValue(eq(ACCESS_REQUEST_ID_PARAMETER_NAME)); - WorkReport workReport = projectAccessRequestApprovalWorkFlowTask.execute(workContext); - assertEquals(WorkStatus.FAILED, workReport.getStatus()); - } - } diff --git a/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/project/ProjectAccessRequestWorkFlowTaskTest.java b/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/project/ProjectAccessRequestWorkFlowTaskTest.java index 6792b9b32..c6684ec4f 100644 --- a/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/project/ProjectAccessRequestWorkFlowTaskTest.java +++ b/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/project/ProjectAccessRequestWorkFlowTaskTest.java @@ -4,7 +4,6 @@ import com.redhat.parodos.infrastructure.ProjectRequester; import com.redhat.parodos.sdk.model.AccessResponseDTO; -import com.redhat.parodos.workflow.exception.MissingParameterException; import com.redhat.parodos.workflow.utils.WorkContextUtils; import com.redhat.parodos.workflows.work.WorkContext; import com.redhat.parodos.workflows.work.WorkReport; @@ -21,7 +20,6 @@ import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; @@ -91,13 +89,4 @@ public void executeFailForInvalidRole() { assertEquals(WorkStatus.FAILED, workReport.getStatus()); } - @Test - @SneakyThrows - public void executeFailForMissingRequiredParameter() { - doThrow(MissingParameterException.class).when(this.projectAccessRequestWorkFlowTask) - .getRequiredParameterValue(eq(USERNAME_PARAMETER_NAME)); - WorkReport workReport = projectAccessRequestWorkFlowTask.execute(workContext); - assertEquals(WorkStatus.FAILED, workReport.getStatus()); - } - } diff --git a/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/project/checker/ProjectAccessRequestApprovalWorkFlowCheckerTaskTest.java b/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/project/checker/ProjectAccessRequestApprovalWorkFlowCheckerTaskTest.java index b9457afda..73f0d29b7 100644 --- a/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/project/checker/ProjectAccessRequestApprovalWorkFlowCheckerTaskTest.java +++ b/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/project/checker/ProjectAccessRequestApprovalWorkFlowCheckerTaskTest.java @@ -1,112 +1,5 @@ package com.redhat.parodos.tasks.project.checker; -import java.util.UUID; - -import com.redhat.parodos.infrastructure.ProjectRequester; -import com.redhat.parodos.sdk.model.AccessStatusResponseDTO; -import com.redhat.parodos.workflow.exception.MissingParameterException; -import com.redhat.parodos.workflow.utils.WorkContextUtils; -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; -import lombok.SneakyThrows; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; - -import static org.junit.Assert.assertEquals; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.when; - -@RunWith(MockitoJUnitRunner.class) public class ProjectAccessRequestApprovalWorkFlowCheckerTaskTest { - private static final String ACCESS_REQUEST_ID_PARAMETER_NAME = "ACCESS_REQUEST_ID"; - - private static final long SLA_TEST = 100L; - - @Mock - private ProjectRequester projectRequester; - - @Mock - private WorkFlow workFlow; - - private WorkContext workContext; - - private ProjectAccessRequestApprovalWorkFlowCheckerTask projectAccessRequestApprovalWorkFlowCheckerTask; - - @Before - public void setUp() { - this.projectAccessRequestApprovalWorkFlowCheckerTask = spy( - new ProjectAccessRequestApprovalWorkFlowCheckerTask(workFlow, SLA_TEST, projectRequester)); - this.projectAccessRequestApprovalWorkFlowCheckerTask - .setBeanName("projectAccessRequestApprovalWorkFlowCheckerTask"); - workContext = new WorkContext(); - WorkContextUtils.setProjectId(workContext, UUID.randomUUID()); - } - - @Test - @SneakyThrows - public void checkWorkFlowStatusCompleted() { - UUID accessRequestId = UUID.randomUUID(); - doReturn(String.valueOf(accessRequestId)).when(this.projectAccessRequestApprovalWorkFlowCheckerTask) - .getRequiredParameterValue(eq(ACCESS_REQUEST_ID_PARAMETER_NAME)); - - AccessStatusResponseDTO accessStatusResponseDTO = new AccessStatusResponseDTO(); - accessStatusResponseDTO.setAccessRequestId(accessRequestId); - accessStatusResponseDTO.setStatus(AccessStatusResponseDTO.StatusEnum.APPROVED); - when(projectRequester.getAccessStatus(any())).thenReturn(accessStatusResponseDTO); - - WorkReport workReport = projectAccessRequestApprovalWorkFlowCheckerTask.execute(workContext); - assertEquals(WorkStatus.COMPLETED, workReport.getStatus()); - } - - @Test - @SneakyThrows - public void checkWorkFlowStatusRejected() { - UUID accessRequestId = UUID.randomUUID(); - doReturn(String.valueOf(accessRequestId)).when(this.projectAccessRequestApprovalWorkFlowCheckerTask) - .getRequiredParameterValue(eq(ACCESS_REQUEST_ID_PARAMETER_NAME)); - - AccessStatusResponseDTO accessStatusResponseDTO = new AccessStatusResponseDTO(); - accessStatusResponseDTO.setAccessRequestId(accessRequestId); - accessStatusResponseDTO.setStatus(AccessStatusResponseDTO.StatusEnum.REJECTED); - when(projectRequester.getAccessStatus(any())).thenReturn(accessStatusResponseDTO); - - WorkReport workReport = projectAccessRequestApprovalWorkFlowCheckerTask.execute(workContext); - assertEquals(WorkStatus.REJECTED, workReport.getStatus()); - } - - @Test - @SneakyThrows - public void checkWorkFlowStatusPending() { - UUID accessRequestId = UUID.randomUUID(); - doReturn(String.valueOf(accessRequestId)).when(this.projectAccessRequestApprovalWorkFlowCheckerTask) - .getRequiredParameterValue(eq(ACCESS_REQUEST_ID_PARAMETER_NAME)); - - AccessStatusResponseDTO accessStatusResponseDTO = new AccessStatusResponseDTO(); - accessStatusResponseDTO.setAccessRequestId(accessRequestId); - accessStatusResponseDTO.setStatus(AccessStatusResponseDTO.StatusEnum.PENDING); - when(projectRequester.getAccessStatus(any())).thenReturn(accessStatusResponseDTO); - - WorkReport workReport = projectAccessRequestApprovalWorkFlowCheckerTask.execute(workContext); - assertEquals(WorkStatus.FAILED, workReport.getStatus()); - } - - @Test - @SneakyThrows - public void executeFailForMissingRequiredParameter() { - doThrow(MissingParameterException.class).when(this.projectAccessRequestApprovalWorkFlowCheckerTask) - .getRequiredParameterValue(eq(ACCESS_REQUEST_ID_PARAMETER_NAME)); - WorkReport workReport = projectAccessRequestApprovalWorkFlowCheckerTask.execute(workContext); - assertEquals(WorkStatus.FAILED, workReport.getStatus()); - } - } diff --git a/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/project/escalation/ProjectAccessRequestEscalationWorkFlowTaskTest.java b/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/project/escalation/ProjectAccessRequestEscalationWorkFlowTaskTest.java index 97f98b345..b3609bc28 100644 --- a/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/project/escalation/ProjectAccessRequestEscalationWorkFlowTaskTest.java +++ b/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/project/escalation/ProjectAccessRequestEscalationWorkFlowTaskTest.java @@ -1,75 +1,5 @@ package com.redhat.parodos.tasks.project.escalation; -import java.util.UUID; - -import com.redhat.parodos.infrastructure.Notifier; -import com.redhat.parodos.infrastructure.ProjectRequester; -import com.redhat.parodos.workflow.exception.MissingParameterException; -import com.redhat.parodos.workflows.work.WorkContext; -import com.redhat.parodos.workflows.work.WorkReport; -import com.redhat.parodos.workflows.work.WorkStatus; -import lombok.SneakyThrows; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; - -import static org.junit.Assert.assertEquals; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.spy; - -@RunWith(MockitoJUnitRunner.class) public class ProjectAccessRequestEscalationWorkFlowTaskTest { - private static final String ACCESS_REQUEST_ID_PARAMETER_NAME = "ACCESS_REQUEST_ID"; - - private static final String ACCESS_REQUEST_ESCALATION_USERNAME_PARAMETER_NAME = "ACCESS_REQUEST_ESCALATION_USERNAME"; - - private static final String ACCESS_REQUEST_ESCALATION_USERNAME_VALUE_TEST = "escalation-username-test"; - - @Mock - private ProjectRequester projectRequester; - - @Mock - private Notifier notifier; - - private WorkContext workContext; - - private ProjectAccessRequestEscalationWorkFlowTask projectAccessRequestEscalationWorkFlowTask; - - @Before - public void setUp() { - this.projectAccessRequestEscalationWorkFlowTask = spy( - new ProjectAccessRequestEscalationWorkFlowTask(projectRequester, notifier)); - this.projectAccessRequestEscalationWorkFlowTask.setBeanName("projectAccessRequestEscalationWorkFlowTask"); - workContext = new WorkContext(); - } - - @Test - @SneakyThrows - public void executeSuccess() { - doReturn(String.valueOf(UUID.randomUUID())).when(this.projectAccessRequestEscalationWorkFlowTask) - .getRequiredParameterValue(eq(ACCESS_REQUEST_ID_PARAMETER_NAME)); - doReturn(ACCESS_REQUEST_ESCALATION_USERNAME_VALUE_TEST).when(this.projectAccessRequestEscalationWorkFlowTask) - .getRequiredParameterValue(eq(ACCESS_REQUEST_ESCALATION_USERNAME_PARAMETER_NAME)); - doNothing().when(notifier).send(any()); - - WorkReport workReport = projectAccessRequestEscalationWorkFlowTask.execute(workContext); - assertEquals(WorkStatus.COMPLETED, workReport.getStatus()); - } - - @Test - @SneakyThrows - public void executeFail() { - doThrow(MissingParameterException.class).when(this.projectAccessRequestEscalationWorkFlowTask) - .getRequiredParameterValue(eq(ACCESS_REQUEST_ID_PARAMETER_NAME)); - WorkReport workReport = projectAccessRequestEscalationWorkFlowTask.execute(workContext); - assertEquals(WorkStatus.FAILED, workReport.getStatus()); - } - } diff --git a/workflow-service/src/main/java/com/redhat/parodos/client/ProjectRequesterClient.java b/workflow-service/src/main/java/com/redhat/parodos/client/ProjectRequesterClient.java index 24dc32614..1e28ae311 100644 --- a/workflow-service/src/main/java/com/redhat/parodos/client/ProjectRequesterClient.java +++ b/workflow-service/src/main/java/com/redhat/parodos/client/ProjectRequesterClient.java @@ -25,9 +25,9 @@ public class ProjectRequesterClient implements ProjectRequester { private final ProjectAccessApi projectAccessApi; - public ProjectRequesterClient(@Value("${workflow.url}") String url, - @Value("${workflow.auth.basic.user}") String user, - @Value("${workflow.auth.basic.password}") String password) { + public ProjectRequesterClient(@Value("${workflow.url:test}") String url, + @Value("${workflow.auth.basic.user:test}") String user, + @Value("${workflow.auth.basic.password:test}") String password) { ApiClient apiClient = new ApiClient().setBasePath(url).addDefaultHeader(HttpHeaders.AUTHORIZATION, "Basic " + Base64.getEncoder().encodeToString((user + ":" + password).getBytes())); projectApi = new ProjectApi(apiClient);