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

Fix unit tests for GetApplicationTask #497

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public WorkReport execute(WorkContext workContext) {
}

Result<App> 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");
Expand Down
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
Expand Up @@ -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) {
Expand All @@ -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[] {},
Expand Down Expand Up @@ -60,7 +60,7 @@ interface MTAApplicationClient {

interface MTATaskGroupClient {

Result<TaskGroup> create(int appId);
Result<TaskGroup> create(String appId);

Result<TaskGroup> get(int id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public Result<App> create(App app) {
}

@Override
public Result<TaskGroup> create(int appId) {
public Result<TaskGroup> create(String appId) {

try {
var tgnew = TaskGroup.ofCloudReadiness(appId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
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,32 +20,42 @@
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;

@Disabled
public class GetApplicationTaskTest {

GetApplicationTask underTest;

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 @@ -64,9 +75,11 @@ public void missingMandatoryParams() {

@Test
@SneakyThrows
@Disabled // FIXME
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 @@ -81,20 +94,31 @@ 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)));
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
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;
import static org.hamcrest.Matchers.equalTo;
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;
Expand Down Expand Up @@ -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());
Expand All @@ -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);
Expand All @@ -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, "") });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public interface TestConsts {

int APP_ID = 123;
String APP_ID = "123";

String APP_NAME = "some_name";

Expand Down
Loading