From e120e190fd13b9301f41d5b1f174c20a9acc3093 Mon Sep 17 00:00:00 2001 From: Gloria Ciavarrini Date: Mon, 7 Aug 2023 13:08:08 +0200 Subject: [PATCH 1/8] applicationID must be a string `GetApplicationTask` add `applicationID` paramenter as a String not as an Integer. Signed-off-by: Gloria Ciavarrini --- .../CreateApplicationTask.java | 2 +- .../migrationtoolkit/MTAApplicationClient.java | 6 +++--- .../MTAApplicationClientImpl.java | 2 +- .../migrationtoolkit/SubmitAnalysisTask.java | 4 ++-- .../CreateApplicationTaskTest.java | 2 +- .../migrationtoolkit/GetAnalysisTaskTest.java | 3 ++- .../SubmitAnalysisTaskTest.java | 18 +++++++++--------- .../tasks/migrationtoolkit/TestConsts.java | 2 +- 8 files changed, 20 insertions(+), 19 deletions(-) diff --git a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/CreateApplicationTask.java b/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/CreateApplicationTask.java index 3615f73e4..fe05e6a88 100644 --- a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/CreateApplicationTask.java +++ b/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/CreateApplicationTask.java @@ -69,7 +69,7 @@ public WorkReport execute(WorkContext workContext) { } Result result = mtaClient - .create(new App(0, appName, new Repository("git", repo, branch), new Identity[] { identity })); + .create(new App("0", appName, new Repository("git", repo, branch), new Identity[] { identity })); if (result == null) { taskLogger.logErrorWithSlf4j("MTA client returned empty result with no error"); diff --git a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/MTAApplicationClient.java b/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/MTAApplicationClient.java index e9c8c8848..f33cf92e6 100644 --- a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/MTAApplicationClient.java +++ b/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/MTAApplicationClient.java @@ -3,7 +3,7 @@ record Repository(String kind, String url, String branch) { } -record App(int id, String name, Repository repository, Identity[] identities) { +record App(String id, String name, Repository repository, Identity[] identities) { } record Identity(int id, String name) { @@ -28,7 +28,7 @@ record Task(App application, String state, String name, String addon, Data data, } record TaskGroup(int id, String name, String state, String addon, Data data, Object bucket, Task[] tasks) { - static TaskGroup ofCloudReadiness(int appID) { + static TaskGroup ofCloudReadiness(String appID) { return new TaskGroup(0, "taskgroups.windup", null, "windup", new Data(new Mode(false, false, false, ""), "/windup/report", new Rules("", null), new Scope(false, new Packages(new String[] {}, new String[] {})), new String[] {}, @@ -60,7 +60,7 @@ interface MTAApplicationClient { interface MTATaskGroupClient { - Result create(int appId); + Result create(String appId); Result get(int id); diff --git a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/MTAApplicationClientImpl.java b/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/MTAApplicationClientImpl.java index 63a647c3c..7740e48d7 100644 --- a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/MTAApplicationClientImpl.java +++ b/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/MTAApplicationClientImpl.java @@ -132,7 +132,7 @@ public Result create(App app) { } @Override - public Result create(int appId) { + public Result create(String appId) { try { var tgnew = TaskGroup.ofCloudReadiness(appId); diff --git a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/SubmitAnalysisTask.java b/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/SubmitAnalysisTask.java index 067c8773e..90b48d8eb 100644 --- a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/SubmitAnalysisTask.java +++ b/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/SubmitAnalysisTask.java @@ -43,9 +43,9 @@ public SubmitAnalysisTask(URI serverURL, String bearerToken) { */ @Override public WorkReport execute(WorkContext workContext) { - int applicationID; + String applicationID; try { - applicationID = Integer.parseInt(getRequiredParameterValue("applicationID")); + applicationID = getRequiredParameterValue("applicationID"); if (mtaClient == null) { var serverUrl = getOptionalParameterValue("serverURL", null); var bearerToken = getOptionalParameterValue("bearerToken", null); diff --git a/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/migrationtoolkit/CreateApplicationTaskTest.java b/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/migrationtoolkit/CreateApplicationTaskTest.java index 7ed4b96f7..63ffe893b 100644 --- a/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/migrationtoolkit/CreateApplicationTaskTest.java +++ b/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/migrationtoolkit/CreateApplicationTaskTest.java @@ -97,7 +97,7 @@ public void createCompletes() { // 0 is wanted explicitly because it is an empty ID for the server request. (IDs // are generated by the server) - verify(mockClient, times(1)).create(new App(0, APP_NAME, new Repository("git", REPO_URL, REPO_BRANCH), null)); + verify(mockClient, times(1)).create(new App("0", APP_NAME, new Repository("git", REPO_URL, REPO_BRANCH), null)); } } diff --git a/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/migrationtoolkit/GetAnalysisTaskTest.java b/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/migrationtoolkit/GetAnalysisTaskTest.java index 96f3074f2..1dd747d3f 100644 --- a/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/migrationtoolkit/GetAnalysisTaskTest.java +++ b/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/migrationtoolkit/GetAnalysisTaskTest.java @@ -21,6 +21,7 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.nullValue; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.anyInt; import static org.mockito.Mockito.eq; import static org.mockito.Mockito.mock; @@ -98,7 +99,7 @@ public void getByID() { assertThat(execute.getStatus(), equalTo(WorkStatus.COMPLETED)); assertThat(execute.getWorkContext().getEntrySet(), hasItem(entry("taskGroupID", taskGroupID))); verify(mockClient, times(1)).get(eq(Integer.parseInt(taskGroupID))); - verify(mockClient, times(0)).create(anyInt()); + verify(mockClient, times(0)).create(anyString()); } static TaskGroup successfulGet() { diff --git a/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/migrationtoolkit/SubmitAnalysisTaskTest.java b/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/migrationtoolkit/SubmitAnalysisTaskTest.java index 575ece33b..2d56ed63b 100644 --- a/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/migrationtoolkit/SubmitAnalysisTaskTest.java +++ b/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/migrationtoolkit/SubmitAnalysisTaskTest.java @@ -14,6 +14,7 @@ import org.junit.jupiter.api.Test; import org.mockito.Mock; +import static com.redhat.parodos.tasks.migrationtoolkit.TestConsts.APP_ID; import static com.redhat.parodos.workflows.workflow.WorkContextAssert.assertThat; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; @@ -21,7 +22,7 @@ import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.nullValue; -import static org.mockito.Mockito.anyInt; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -55,13 +56,13 @@ public void missingMandatoryParams() { assertThat(execute.getError(), is(instanceOf(MissingParameterException.class))); assertThat(execute.getStatus(), equalTo(WorkStatus.FAILED)); assertThat(execute.getWorkContext().get("taskGroup"), is(nullValue())); - verify(mockClient, times(0)).create(anyInt()); + verify(mockClient, times(0)).create(anyString()); } @Test @SneakyThrows public void failsCreatingTaskGroup() { - when(mockClient.create(anyInt())).thenReturn(new Result.Failure<>(new Exception("not found"))); + when(mockClient.create(anyString())).thenReturn(new Result.Failure<>(new Exception("not found"))); ctx.put("applicationID", "123"); WorkContextDelegate.write(ctx, WorkContextDelegate.ProcessType.WORKFLOW_EXECUTION, WorkContextDelegate.Resource.ID, UUID.randomUUID()); @@ -72,16 +73,15 @@ public void failsCreatingTaskGroup() { assertThat(execute.getError(), is(instanceOf(Exception.class))); assertThat(execute.getStatus(), equalTo(WorkStatus.FAILED)); assertThat(execute.getWorkContext().get("taskGroup"), is(nullValue())); - verify(mockClient, times(1)).create(anyInt()); + verify(mockClient, times(1)).create(anyString()); } @Test @SneakyThrows public void createCompletes() { int taskGroupID = 1; - int appID = 123; - ctx.put("applicationID", Integer.toString(appID)); - when(mockClient.create(appID)).thenReturn(new Result.Success<>(of(taskGroupID, appID))); + ctx.put("applicationID", APP_ID); + when(mockClient.create(APP_ID)).thenReturn(new Result.Success<>(of(taskGroupID, APP_ID))); WorkContextDelegate.write(ctx, WorkContextDelegate.ProcessType.WORKFLOW_EXECUTION, WorkContextDelegate.Resource.ID, UUID.randomUUID()); underTest.preExecute(ctx); @@ -91,11 +91,11 @@ public void createCompletes() { assertThat(execute.getStatus(), equalTo(WorkStatus.COMPLETED)); assertThat(execute.getWorkContext()).hasEntryKey("analysisTaskGroup"); assertThat(((TaskGroup) execute.getWorkContext().get("analysisTaskGroup")).id(), equalTo(taskGroupID)); - verify(mockClient, times(1)).create(appID); + verify(mockClient, times(1)).create(APP_ID); } @NotNull - private static TaskGroup of(int id, int appID) { + private static TaskGroup of(int id, String appID) { return new TaskGroup(id, "", "", "", null, null, new Task[] { new Task(new App(appID, "", null, null), "", "", "", null, "") }); } diff --git a/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/migrationtoolkit/TestConsts.java b/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/migrationtoolkit/TestConsts.java index ef71e069d..c98e8a3d7 100644 --- a/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/migrationtoolkit/TestConsts.java +++ b/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/migrationtoolkit/TestConsts.java @@ -2,7 +2,7 @@ public interface TestConsts { - int APP_ID = 123; + String APP_ID = "123"; String APP_NAME = "some_name"; From f2d03329f91f064fd3ca6b598a5218753a4f1adf Mon Sep 17 00:00:00 2001 From: Gloria Ciavarrini Date: Mon, 7 Aug 2023 09:47:55 +0200 Subject: [PATCH 2/8] Remove @Disable annotations Signed-off-by: Gloria Ciavarrini --- .../tasks/migrationtoolkit/GetApplicationTaskTest.java | 4 ---- 1 file changed, 4 deletions(-) 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 4b6396191..a9109a36f 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 @@ -31,7 +31,6 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -@Disabled public class GetApplicationTaskTest { GetApplicationTask underTest; @@ -64,7 +63,6 @@ public void missingMandatoryParams() { @Test @SneakyThrows - @Disabled // FIXME public void failsGettingAppNotFound() { when(mockClient.get(anyString())).thenReturn(new Result.Failure<>(new Exception("not found"))); ctx.put("applicationName", APP_NAME); @@ -81,8 +79,6 @@ public void failsGettingAppNotFound() { @Test @SneakyThrows - @Disabled - // FIXME 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))); From 5cb7ea4450b8daef456fac89751c0acd6959f519 Mon Sep 17 00:00:00 2001 From: Gloria Ciavarrini Date: Tue, 8 Aug 2023 10:13:53 +0200 Subject: [PATCH 3/8] 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()); From 4c3e772cc072ae86f81f2e7524ec94f25503f5bc Mon Sep 17 00:00:00 2001 From: Gloria Ciavarrini Date: Tue, 8 Aug 2023 12:12:59 +0200 Subject: [PATCH 4/8] TaskGroupID is a string Signed-off-by: Gloria Ciavarrini --- .../tasks/migrationtoolkit/GetAnalysisTask.java | 8 ++++---- .../tasks/migrationtoolkit/GetApplicationTask.java | 2 +- .../tasks/migrationtoolkit/MTAApplicationClient.java | 4 ++-- .../migrationtoolkit/MTAApplicationClientImpl.java | 4 ++-- .../tasks/migrationtoolkit/GetApplicationTaskTest.java | 10 +++++----- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/GetAnalysisTask.java b/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/GetAnalysisTask.java index dcb4a5d3c..3f26011d8 100644 --- a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/GetAnalysisTask.java +++ b/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/GetAnalysisTask.java @@ -76,15 +76,15 @@ public WorkReport execute(WorkContext workContext) { mtaClient = new MTAClient(serverUrl, bearerToken); } - int taskGroupID; + String taskGroupID; try { - taskGroupID = Integer.parseInt(getRequiredParameterValue("taskGroupID")); + taskGroupID = getRequiredParameterValue("taskGroupID"); } catch (MissingParameterException | NumberFormatException e) { return new DefaultWorkReport(WorkStatus.FAILED, workContext, e); } - Result result = mtaClient.get(taskGroupID); + Result result = mtaClient.getTaskGroup(taskGroupID); if (result == null) { taskLogger.logErrorWithSlf4j("MTA client returned empty result with no error."); @@ -99,7 +99,7 @@ else if (result instanceof Result.Failure failure) { else if (result instanceof Result.Success success) { if ("Ready".equals(success.value().state()) && success.value().tasks() != null && "Succeeded".equals(success.value().tasks()[0].state())) { - String reportURL = "%s/hub/applications/%d/bucket%s".formatted(serverUrl, + String reportURL = "%s/hub/applications/%s/bucket%s".formatted(serverUrl, success.value().tasks()[0].application().id(), success.value().data().output()); taskLogger.logInfoWithSlf4j("MTA client returned success result with report url: {}", reportURL); addParameter("reportURL", reportURL); 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 063129b5a..7ef556edc 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 @@ -56,7 +56,7 @@ public WorkReport execute(WorkContext workContext) { return new DefaultWorkReport(WorkStatus.FAILED, workContext, e); } - Result result = mtaClient.get(applicationName); + Result result = mtaClient.getApp(applicationName); if (result == null) { taskLogger.logErrorWithSlf4j("MTA client returned empty result with no error"); diff --git a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/MTAApplicationClient.java b/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/MTAApplicationClient.java index f33cf92e6..7e2cbafa6 100644 --- a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/MTAApplicationClient.java +++ b/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/MTAApplicationClient.java @@ -50,7 +50,7 @@ record Failure (Throwable t) implements Result { interface MTAApplicationClient { - Result get(String name); + Result getApp(String name); Result create(App app); @@ -62,6 +62,6 @@ interface MTATaskGroupClient { Result create(String appId); - Result get(int id); + Result getTaskGroup(String id); } diff --git a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/MTAApplicationClientImpl.java b/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/MTAApplicationClientImpl.java index 7740e48d7..0d55ec1dd 100644 --- a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/MTAApplicationClientImpl.java +++ b/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/MTAApplicationClientImpl.java @@ -59,7 +59,7 @@ class MTAClient implements MTAApplicationClient, MTATaskGroupClient { } @Override - public Result get(String name) { + public Result getApp(String name) { // apps in MTA have unique constraints on name. try { HttpResponse getAll = client.send( @@ -167,7 +167,7 @@ public Result create(String appId) { } @Override - public Result get(int id) { + public Result getTaskGroup(String id) { try { var getTG = client.send( HttpRequest.newBuilder().GET().uri(serverURI.resolve("/hub/taskgroups/" + id)).build(), 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 bf37fe594..ec63d985a 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 @@ -70,13 +70,13 @@ public void missingMandatoryParams() { assertThat(execute.getError(), is(instanceOf(MissingParameterException.class))); assertThat(execute.getStatus(), equalTo(WorkStatus.FAILED)); - verify(this.mockClient, times(0)).get(anyString()); + verify(this.mockClient, times(0)).getApp(anyString()); } @Test @SneakyThrows public void failsGettingAppNotFound() { - when(mockClient.get(anyString())).thenReturn(new Result.Failure<>(new Exception("not found"))); + when(mockClient.getApp(anyString())).thenReturn(new Result.Failure<>(new Exception("not found"))); doReturn(APP_NAME).when(this.underTest).getRequiredParameterValue(eq("applicationName")); doNothing().when(taskLoggerMock).logErrorWithSlf4j(any()); @@ -89,13 +89,13 @@ public void failsGettingAppNotFound() { assertThat(execute.getError(), is(instanceOf(Exception.class))); assertThat(execute.getError(), is(not(instanceOf(MissingParameterException.class)))); assertThat(execute.getStatus(), equalTo(WorkStatus.FAILED)); - verify(mockClient, times(1)).get(anyString()); + verify(mockClient, times(1)).getApp(anyString()); } @Test @SneakyThrows public void getByName() { - when(mockClient.get(anyString())).thenReturn( + when(mockClient.getApp(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()); @@ -119,7 +119,7 @@ public void getByName() { assertThat(workflowExecutionArguments, hasKey("applicationID")); assertThat(workflowExecutionArguments.get("applicationID"), equalTo((APP_ID))); - verify(mockClient, times(1)).get(anyString()); + verify(mockClient, times(1)).getApp(anyString()); verify(mockClient, times(0)).create(any()); } From 11ae5f54a2cb34575e2055ad4a76ef87cdad5538 Mon Sep 17 00:00:00 2001 From: Gloria Ciavarrini Date: Tue, 8 Aug 2023 12:14:07 +0200 Subject: [PATCH 5/8] Fix unit test for GetAnalysisTask Signed-off-by: Gloria Ciavarrini --- .../migrationtoolkit/GetAnalysisTask.java | 5 +++ .../migrationtoolkit/SubmitAnalysisTask.java | 5 +++ .../migrationtoolkit/GetAnalysisTaskTest.java | 38 +++++++++++-------- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/GetAnalysisTask.java b/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/GetAnalysisTask.java index 3f26011d8..5ebd2c091 100644 --- a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/GetAnalysisTask.java +++ b/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/GetAnalysisTask.java @@ -12,6 +12,7 @@ import com.redhat.parodos.workflow.parameter.WorkParameter; import com.redhat.parodos.workflow.parameter.WorkParameterType; 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; @@ -41,7 +42,11 @@ public class GetAnalysisTask extends BaseInfrastructureWorkFlowTask { @Inject private Notifier notificationSender; + // For testing purposes + protected WorkFlowTaskLogger taskLogger; + public GetAnalysisTask(URI serverURL, String bearerToken, Notifier notifier) { + super(); this.serverUrl = serverURL; this.mtaClient = new MTAClient(serverURL, bearerToken); this.notificationSender = notifier; diff --git a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/SubmitAnalysisTask.java b/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/SubmitAnalysisTask.java index 90b48d8eb..cf3e7437a 100644 --- a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/SubmitAnalysisTask.java +++ b/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/SubmitAnalysisTask.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; @@ -30,7 +31,11 @@ public class SubmitAnalysisTask extends BaseInfrastructureWorkFlowTask { // This method is useful for testing as well. protected MTATaskGroupClient mtaClient; + // For testing purposes + protected WorkFlowTaskLogger taskLogger; + public SubmitAnalysisTask() { + super(); } public SubmitAnalysisTask(URI serverURL, String bearerToken) { diff --git a/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/migrationtoolkit/GetAnalysisTaskTest.java b/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/migrationtoolkit/GetAnalysisTaskTest.java index 1dd747d3f..215ff07c0 100644 --- a/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/migrationtoolkit/GetAnalysisTaskTest.java +++ b/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/migrationtoolkit/GetAnalysisTaskTest.java @@ -2,14 +2,15 @@ import java.util.UUID; +import com.redhat.parodos.infrastructure.Notifier; 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; @@ -21,36 +22,41 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.nullValue; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.anyInt; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.eq; 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; -@Disabled public class GetAnalysisTaskTest { GetAnalysisTask underTest; MTATaskGroupClient mockClient; + WorkFlowTaskLogger taskLoggerMock; + WorkContext ctx; @BeforeEach public void setUp() { this.mockClient = mock(MTATaskGroupClient.class); - underTest = new GetAnalysisTask(null, null, null); + taskLoggerMock = mock(WorkFlowTaskLogger.class); + Notifier notifier = mock(Notifier.class); + underTest = spy(new GetAnalysisTask(null, null, notifier)); underTest.mtaClient = mockClient; + underTest.taskLogger = taskLoggerMock; underTest.setBeanName("MTATaskGroupClient"); ctx = new WorkContext(); } @Test @SneakyThrows - @Disabled - // FIXME public void missingMandatoryParams() { WorkContextDelegate.write(ctx, WorkContextDelegate.ProcessType.WORKFLOW_EXECUTION, WorkContextDelegate.Resource.ID, UUID.randomUUID()); @@ -60,15 +66,14 @@ public void missingMandatoryParams() { assertThat(execute.getError(), is(instanceOf(MissingParameterException.class))); assertThat(execute.getStatus(), equalTo(WorkStatus.FAILED)); assertThat(execute.getWorkContext().get("taskGroupID"), is(nullValue())); - verify(mockClient, times(0)).get(anyInt()); + verify(mockClient, times(0)).getTaskGroup(anyString()); } @Test @SneakyThrows - @Disabled - // FIXME public void failsGetTaskGroup() { - when(mockClient.get(anyInt())).thenReturn(new Result.Failure<>(new Exception("not found"))); + when(mockClient.getTaskGroup(anyString())).thenReturn(new Result.Failure<>(new Exception("not found"))); + doReturn("123").when(this.underTest).getRequiredParameterValue(eq("taskGroupID")); ctx.put("taskGroupID", "123"); WorkContextDelegate.write(ctx, WorkContextDelegate.ProcessType.WORKFLOW_EXECUTION, @@ -79,17 +84,18 @@ public void failsGetTaskGroup() { assertThat(execute.getError(), is(not(instanceOf(MissingParameterException.class)))); assertThat(execute.getStatus(), equalTo(WorkStatus.FAILED)); assertThat(execute.getWorkContext().get("taskGroupID"), equalTo("123")); - verify(mockClient, times(1)).get(anyInt()); + verify(mockClient, times(1)).getTaskGroup(anyString()); } @Test @SneakyThrows - @Disabled - // FIXME public void getByID() { var taskGroupID = "1"; ctx.put("taskGroupID", taskGroupID); - when(mockClient.get(Integer.parseInt(taskGroupID))).thenReturn(new Result.Success<>(successfulGet())); + doReturn(taskGroupID).when(this.underTest).getRequiredParameterValue(eq("taskGroupID")); + when(mockClient.getTaskGroup(taskGroupID)).thenReturn(new Result.Success<>(successfulGet())); + doNothing().when(taskLoggerMock).logErrorWithSlf4j(any()); + WorkContextDelegate.write(ctx, WorkContextDelegate.ProcessType.WORKFLOW_EXECUTION, WorkContextDelegate.Resource.ID, UUID.randomUUID()); underTest.preExecute(ctx); @@ -98,12 +104,12 @@ public void getByID() { assertThat(execute.getError(), is(nullValue())); assertThat(execute.getStatus(), equalTo(WorkStatus.COMPLETED)); assertThat(execute.getWorkContext().getEntrySet(), hasItem(entry("taskGroupID", taskGroupID))); - verify(mockClient, times(1)).get(eq(Integer.parseInt(taskGroupID))); + verify(mockClient, times(1)).getTaskGroup(eq(taskGroupID)); verify(mockClient, times(0)).create(anyString()); } static TaskGroup successfulGet() { - return new TaskGroup(0, "taskgroups.windup", "Ready", "windup", + return new TaskGroup("0", "taskgroups.windup", "Ready", "windup", new Data(new Mode(false, false, false, ""), "/windup/report", new Rules("", null), new Scope(false, new Packages(new String[] {}, new String[] {})), new String[] {}, new String[] { "cloud-readiness" }), From 274ca2a5cee84fa4f81eaf50f63f40f542d2cdac Mon Sep 17 00:00:00 2001 From: Gloria Ciavarrini Date: Tue, 8 Aug 2023 12:34:06 +0200 Subject: [PATCH 6/8] TaskGroupID is a string --- .../parodos/tasks/migrationtoolkit/MTAApplicationClient.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/MTAApplicationClient.java b/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/MTAApplicationClient.java index 7e2cbafa6..b11e5b8e9 100644 --- a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/MTAApplicationClient.java +++ b/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/MTAApplicationClient.java @@ -27,9 +27,9 @@ record Data(Mode mode, String output, Rules rules, Scope scope, String[] sources record Task(App application, String state, String name, String addon, Data data, Object bucket) { } -record TaskGroup(int id, String name, String state, String addon, Data data, Object bucket, Task[] tasks) { +record TaskGroup(String id, String name, String state, String addon, Data data, Object bucket, Task[] tasks) { static TaskGroup ofCloudReadiness(String appID) { - return new TaskGroup(0, "taskgroups.windup", null, "windup", + return new TaskGroup("0", "taskgroups.windup", null, "windup", new Data(new Mode(false, false, false, ""), "/windup/report", new Rules("", null), new Scope(false, new Packages(new String[] {}, new String[] {})), new String[] {}, new String[] { "cloud-readiness" }), From 0a67163bba50591c2cd4b67478e84b2ee866b46b Mon Sep 17 00:00:00 2001 From: Gloria Ciavarrini Date: Tue, 8 Aug 2023 12:36:59 +0200 Subject: [PATCH 7/8] Fix submit analysis test --- .../SubmitAnalysisTaskTest.java | 42 +++++++++++++++---- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/migrationtoolkit/SubmitAnalysisTaskTest.java b/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/migrationtoolkit/SubmitAnalysisTaskTest.java index 2d56ed63b..be99e912c 100644 --- a/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/migrationtoolkit/SubmitAnalysisTaskTest.java +++ b/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/migrationtoolkit/SubmitAnalysisTaskTest.java @@ -1,16 +1,17 @@ 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.jetbrains.annotations.NotNull; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.mockito.Mock; @@ -18,16 +19,25 @@ import static com.redhat.parodos.workflows.workflow.WorkContextAssert.assertThat; 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.any; import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.openMocks; -@Disabled public class SubmitAnalysisTaskTest { SubmitAnalysisTask underTest; @@ -35,12 +45,17 @@ public class SubmitAnalysisTaskTest { @Mock MTATaskGroupClient mockClient; + @Mock + WorkFlowTaskLogger taskLoggerMock; + WorkContext ctx; @BeforeEach public void setUp() { - underTest = new SubmitAnalysisTask(); + openMocks(this); + underTest = spy(new SubmitAnalysisTask()); underTest.mtaClient = mockClient; + underTest.taskLogger = taskLoggerMock; underTest.setBeanName("SubmitAnalysisTask"); ctx = new WorkContext(); } @@ -63,6 +78,10 @@ public void missingMandatoryParams() { @SneakyThrows public void failsCreatingTaskGroup() { when(mockClient.create(anyString())).thenReturn(new Result.Failure<>(new Exception("not found"))); + doNothing().when(taskLoggerMock).logErrorWithSlf4j(any()); + + doReturn("123").when(this.underTest).getRequiredParameterValue(eq("applicationID")); + ctx.put("applicationID", "123"); WorkContextDelegate.write(ctx, WorkContextDelegate.ProcessType.WORKFLOW_EXECUTION, WorkContextDelegate.Resource.ID, UUID.randomUUID()); @@ -79,7 +98,8 @@ public void failsCreatingTaskGroup() { @Test @SneakyThrows public void createCompletes() { - int taskGroupID = 1; + String taskGroupID = "1"; + doReturn(APP_ID).when(this.underTest).getRequiredParameterValue(eq("applicationID")); ctx.put("applicationID", APP_ID); when(mockClient.create(APP_ID)).thenReturn(new Result.Success<>(of(taskGroupID, APP_ID))); WorkContextDelegate.write(ctx, WorkContextDelegate.ProcessType.WORKFLOW_EXECUTION, @@ -89,13 +109,21 @@ public void createCompletes() { assertThat(execute.getError(), is(nullValue())); assertThat(execute.getStatus(), equalTo(WorkStatus.COMPLETED)); - assertThat(execute.getWorkContext()).hasEntryKey("analysisTaskGroup"); - assertThat(((TaskGroup) execute.getWorkContext().get("analysisTaskGroup")).id(), equalTo(taskGroupID)); + 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("taskGroupID")); + assertThat(workflowExecutionArguments.get("taskGroupID"), equalTo(taskGroupID)); verify(mockClient, times(1)).create(APP_ID); } @NotNull - private static TaskGroup of(int id, String appID) { + private static TaskGroup of(String id, String appID) { return new TaskGroup(id, "", "", "", null, null, new Task[] { new Task(new App(appID, "", null, null), "", "", "", null, "") }); } From a81b16aac56441540c65b3854e9647f4fa7fdc8d Mon Sep 17 00:00:00 2001 From: Gloria Ciavarrini Date: Tue, 8 Aug 2023 17:24:28 +0200 Subject: [PATCH 8/8] Fix unit tests for CreateApplicationTask Signed-off-by: Gloria Ciavarrini --- .../CreateApplicationTask.java | 5 +++ .../CreateApplicationTaskTest.java | 31 ++++++++++++++++--- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/CreateApplicationTask.java b/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/CreateApplicationTask.java index fe05e6a88..09a8bfb0d 100644 --- a/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/CreateApplicationTask.java +++ b/prebuilt-tasks/src/main/java/com/redhat/parodos/tasks/migrationtoolkit/CreateApplicationTask.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; @@ -26,7 +27,11 @@ public class CreateApplicationTask extends BaseInfrastructureWorkFlowTask { // This method is useful for testing as well. protected MTAApplicationClient mtaClient; + // For testing purposes + protected WorkFlowTaskLogger taskLogger; + public CreateApplicationTask() { + super(); } public CreateApplicationTask(URI serverURL, String bearerToken) { diff --git a/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/migrationtoolkit/CreateApplicationTaskTest.java b/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/migrationtoolkit/CreateApplicationTaskTest.java index 63ffe893b..2b2dd1675 100644 --- a/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/migrationtoolkit/CreateApplicationTaskTest.java +++ b/prebuilt-tasks/src/test/java/com/redhat/parodos/tasks/migrationtoolkit/CreateApplicationTaskTest.java @@ -4,12 +4,12 @@ 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 org.mockito.Mock; @@ -21,13 +21,18 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.instanceOf; +import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.Mockito.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.openMocks; -@Disabled public class CreateApplicationTaskTest { CreateApplicationTask underTest; @@ -35,12 +40,18 @@ public class CreateApplicationTaskTest { @Mock MTAApplicationClient mockClient; + @Mock + WorkFlowTaskLogger taskLoggerMock; + WorkContext ctx; @BeforeEach public void setUp() { - underTest = new CreateApplicationTask(); + openMocks(this); + underTest = spy(new CreateApplicationTask()); underTest.mtaClient = mockClient; + underTest.setBeanName("CreateApplicationTask"); + underTest.taskLogger = taskLoggerMock; ctx = new WorkContext(); } @@ -62,6 +73,9 @@ public void missingMandatoryParameters() { @SneakyThrows public void createFails() { when(mockClient.create(any(App.class))).thenReturn(new Result.Failure<>(new Exception("some error from MTA"))); + doReturn(APP_NAME).when(this.underTest).getRequiredParameterValue(eq("applicationName")); + doReturn(REPO_URL).when(this.underTest).getRequiredParameterValue(eq("repositoryURL")); + ctx.put("applicationName", APP_NAME); ctx.put("repositoryURL", REPO_URL); WorkContextDelegate.write(ctx, WorkContextDelegate.ProcessType.WORKFLOW_EXECUTION, @@ -83,6 +97,10 @@ public void createCompletes() { ctx.put("repositoryURL", REPO_URL); ctx.put("branch", REPO_BRANCH); + doReturn(APP_NAME).when(this.underTest).getOptionalParameterValue(eq("applicationName"), eq("")); + doReturn(REPO_URL).when(this.underTest).getRequiredParameterValue(eq("repositoryURL")); + doReturn(REPO_BRANCH).when(this.underTest).getOptionalParameterValue(eq("branch"), isNull()); + when(mockClient.create(any())).thenReturn( new Result.Success<>(new App(APP_ID, APP_NAME, new Repository("git", REPO_URL, REPO_BRANCH), null))); WorkContextDelegate.write(ctx, WorkContextDelegate.ProcessType.WORKFLOW_EXECUTION, @@ -92,12 +110,15 @@ public void createCompletes() { assertThat(execute.getError(), is(nullValue())); assertThat(execute.getStatus(), equalTo(WorkStatus.COMPLETED)); - assertThat(execute.getWorkContext().get("application"), + assertThat(((App) execute.getWorkContext().get("application")), notNullValue()); + assertThat(((App) execute.getWorkContext().get("application")).identities(), nullValue()); + assertThat((App) execute.getWorkContext().get("application"), equalTo(new App(APP_ID, APP_NAME, new Repository("git", REPO_URL, REPO_BRANCH), null))); // 0 is wanted explicitly because it is an empty ID for the server request. (IDs // are generated by the server) - verify(mockClient, times(1)).create(new App("0", APP_NAME, new Repository("git", REPO_URL, REPO_BRANCH), null)); + verify(mockClient, times(1)) + .create(new App("0", APP_NAME, new Repository("git", REPO_URL, REPO_BRANCH), any())); } }