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

Commit

Permalink
Fix unit tests for GetApplicationTask
Browse files Browse the repository at this point in the history
Signed-off-by: Gloria Ciavarrini <[email protected]>
  • Loading branch information
gciavarrini committed Aug 9, 2023
1 parent f2d0332 commit a49781a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.redhat.parodos.workflow.exception.MissingParameterException;
import com.redhat.parodos.workflow.task.infrastructure.BaseInfrastructureWorkFlowTask;
import com.redhat.parodos.workflow.task.log.WorkFlowTaskLogger;
import com.redhat.parodos.workflows.work.DefaultWorkReport;
import com.redhat.parodos.workflows.work.WorkContext;
import com.redhat.parodos.workflows.work.WorkReport;
Expand All @@ -25,7 +26,11 @@ public class GetApplicationTask extends BaseInfrastructureWorkFlowTask {
// This method is useful for testing as well.
protected MTAApplicationClient mtaClient;

// For testing purposes
protected WorkFlowTaskLogger taskLogger;

public GetApplicationTask() {
super();
}

public GetApplicationTask(URI serverURL, String bearerToken) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package com.redhat.parodos.tasks.migrationtoolkit;

import java.util.Map;
import java.util.UUID;

import com.redhat.parodos.workflow.context.WorkContextDelegate;
import com.redhat.parodos.workflow.exception.MissingParameterException;
import com.redhat.parodos.workflow.task.log.WorkFlowTaskLogger;
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.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import static com.redhat.parodos.tasks.migrationtoolkit.TestConsts.APP_ID;
Expand All @@ -19,14 +20,21 @@
import static java.util.Map.entry;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand All @@ -37,13 +45,17 @@ public class GetApplicationTaskTest {

MTAApplicationClient mockClient;

WorkFlowTaskLogger taskLoggerMock;

WorkContext ctx;

@BeforeEach
public void setUp() {
mockClient = mock(MTAApplicationClient.class);
underTest = new GetApplicationTask();
taskLoggerMock = mock(WorkFlowTaskLogger.class);
underTest = spy(new GetApplicationTask());
underTest.mtaClient = mockClient;
underTest.taskLogger = taskLoggerMock;
underTest.setBeanName("GetApplicationTask");
ctx = new WorkContext();
}
Expand All @@ -65,6 +77,9 @@ public void missingMandatoryParams() {
@SneakyThrows
public void failsGettingAppNotFound() {
when(mockClient.get(anyString())).thenReturn(new Result.Failure<>(new Exception("not found")));
doReturn(APP_NAME).when(this.underTest).getRequiredParameterValue(eq("applicationName"));
doNothing().when(taskLoggerMock).logErrorWithSlf4j(any());

ctx.put("applicationName", APP_NAME);
WorkContextDelegate.write(ctx, WorkContextDelegate.ProcessType.WORKFLOW_EXECUTION,
WorkContextDelegate.Resource.ID, UUID.randomUUID());
Expand All @@ -82,15 +97,28 @@ public void failsGettingAppNotFound() {
public void getByName() {
when(mockClient.get(anyString())).thenReturn(
new Result.Success<>(new App(APP_ID, APP_NAME, new Repository("git", REPO_URL, REPO_BRANCH), null)));
doReturn(APP_NAME).when(this.underTest).getRequiredParameterValue(eq("applicationName"));
doNothing().when(taskLoggerMock).logErrorWithSlf4j(anyString(), anyString());

ctx.put("applicationName", APP_NAME);
WorkContextDelegate.write(ctx, WorkContextDelegate.ProcessType.WORKFLOW_EXECUTION,
WorkContextDelegate.Resource.ID, UUID.randomUUID());

underTest.preExecute(ctx);
WorkReport execute = underTest.execute(ctx);

assertThat(execute.getError(), is(nullValue()));
assertThat(execute.getStatus(), equalTo(WorkStatus.COMPLETED));
assertThat(execute.getWorkContext().getEntrySet(), hasItem(entry("applicationID", APP_ID)));
assertThat(execute.getWorkContext().getEntrySet(),
hasItem(allOf(hasProperty("key", equalTo("WORKFLOW_EXECUTION_ARGUMENTS")),
hasProperty("value", instanceOf(Map.class)))));

Map<String, Object> workflowExecutionArguments = (Map<String, Object>) execute.getWorkContext().getEntrySet()
.stream().filter(entry -> entry.getKey().equals("WORKFLOW_EXECUTION_ARGUMENTS")).findFirst()
.map(Map.Entry::getValue).orElse(null);

assertThat(workflowExecutionArguments, hasKey("applicationID"));
assertThat(workflowExecutionArguments.get("applicationID"), equalTo((APP_ID)));
verify(mockClient, times(1)).get(anyString());
verify(mockClient, times(0)).create(any());

Expand Down

0 comments on commit a49781a

Please sign in to comment.