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

Fix test for CreateApplicationTask #500

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
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 @@ -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 @@ -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) {
Expand Down Expand Up @@ -69,7 +74,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 @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -76,15 +81,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<TaskGroup> result = mtaClient.get(taskGroupID);
Result<TaskGroup> result = mtaClient.getTaskGroup(taskGroupID);

if (result == null) {
taskLogger.logErrorWithSlf4j("MTA client returned empty result with no error.");
Expand All @@ -99,7 +104,7 @@ else if (result instanceof Result.Failure<TaskGroup> failure) {
else if (result instanceof Result.Success<TaskGroup> 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);
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 All @@ -51,7 +56,7 @@ public WorkReport execute(WorkContext workContext) {
return new DefaultWorkReport(WorkStatus.FAILED, workContext, e);
}

Result<App> result = mtaClient.get(applicationName);
Result<App> result = mtaClient.getApp(applicationName);

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 @@ -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 @@ -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) {
static TaskGroup ofCloudReadiness(int appID) {
return new TaskGroup(0, "taskgroups.windup", null, "windup",
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",
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" }),
Expand All @@ -50,7 +50,7 @@ record Failure<V> (Throwable t) implements Result<V> {

interface MTAApplicationClient {

Result<App> get(String name);
Result<App> getApp(String name);

Result<App> create(App app);

Expand All @@ -60,8 +60,8 @@ interface MTAApplicationClient {

interface MTATaskGroupClient {

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

Result<TaskGroup> get(int id);
Result<TaskGroup> getTaskGroup(String id);

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class MTAClient implements MTAApplicationClient, MTATaskGroupClient {
}

@Override
public Result<App> get(String name) {
public Result<App> getApp(String name) {
// apps in MTA have unique constraints on name.
try {
HttpResponse<String> getAll = client.send(
Expand Down 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 Expand Up @@ -167,7 +167,7 @@ public Result<TaskGroup> create(int appId) {
}

@Override
public Result<TaskGroup> get(int id) {
public Result<TaskGroup> getTaskGroup(String id) {
try {
var getTG = client.send(
HttpRequest.newBuilder().GET().uri(serverURI.resolve("/hub/taskgroups/" + id)).build(),
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 @@ -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) {
Expand All @@ -43,9 +48,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 @@ -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;

Expand All @@ -21,26 +21,37 @@
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;

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

Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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()));
}

}
Loading
Loading