From f31013068f5f794533dfb48eabaa14fdf613c61c Mon Sep 17 00:00:00 2001 From: Gloria Ciavarrini Date: Tue, 8 Aug 2023 10:13:53 +0200 Subject: [PATCH] Fix unit tests for GetApplicationTask Signed-off-by: Gloria Ciavarrini --- .../migrationtoolkit/GetApplicationTask.java | 5 +++ .../GetApplicationTaskTest.java | 34 +++++++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/GetApplicationTask.java b/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/GetApplicationTask.java index 7b19f435e..063129b5a 100644 --- a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/GetApplicationTask.java +++ b/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/GetApplicationTask.java @@ -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; @@ -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) { diff --git a/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/migrationtoolkit/GetApplicationTaskTest.java b/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/migrationtoolkit/GetApplicationTaskTest.java index a9109a36f..bf37fe594 100644 --- a/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/migrationtoolkit/GetApplicationTaskTest.java +++ b/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/migrationtoolkit/GetApplicationTaskTest.java @@ -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; @@ -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; @@ -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(); } @@ -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()); @@ -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 workflowExecutionArguments = (Map) 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());