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

Commit

Permalink
refactor tasks and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
anludke authored and RichardW98 committed Jul 31, 2023
1 parent 53fa2da commit ffb4ffd
Show file tree
Hide file tree
Showing 6 changed files with 4 additions and 262 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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());
}

}
Original file line number Diff line number Diff line change
@@ -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());
}

}
Original file line number Diff line number Diff line change
@@ -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());
}

}

0 comments on commit ffb4ffd

Please sign in to comment.