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

Commit

Permalink
Refactor waitAsyncProject and witAsyncStatusResponse
Browse files Browse the repository at this point in the history
Merge these two method to avoid code duplication.
Add two utility methods to ease the code readability.

Signed-off-by: Gloria Ciavarrini <gciavarrini@redhat.com>
  • Loading branch information
gciavarrini committed Apr 17, 2023
1 parent efef93d commit 4f56f05
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.redhat.parodos.sdk.api.ProjectApi;
import com.redhat.parodos.sdk.api.WorkflowApi;
import com.redhat.parodos.sdk.api.WorkflowDefinitionApi;
import com.redhat.parodos.sdk.invoker.ApiCallback;
import com.redhat.parodos.sdk.invoker.ApiClient;
import com.redhat.parodos.sdk.invoker.ApiException;
import com.redhat.parodos.sdk.invoker.Configuration;
Expand All @@ -17,20 +18,19 @@
import com.redhat.parodos.sdk.model.WorkFlowStatusResponseDTO;
import com.redhat.parodos.sdk.model.WorkRequestDTO;
import com.redhat.parodos.workflow.utils.CredUtils;
import com.redhat.parodos.workflows.work.WorkStatus;
import lombok.extern.slf4j.Slf4j;
import org.junit.Before;
import org.junit.Test;
import org.springframework.http.HttpHeaders;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

import static com.redhat.parodos.examples.integration.utils.ExamplesUtils.getProjectByNameAndDescription;
import static com.redhat.parodos.examples.integration.utils.ExamplesUtils.waitAsyncStatusResponse;
import static com.redhat.parodos.examples.integration.utils.ExamplesUtils.waitAsyncResponse;
import static com.redhat.parodos.examples.integration.utils.ExamplesUtils.waitProjectStart;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -59,7 +59,7 @@ public void runComplexWorkFlow() throws ApiException, InterruptedException {
log.info("Running complex flow");
ProjectApi projectApi = new ProjectApi(apiClient);

ExamplesUtils.waitProjectStart(projectApi);
waitProjectStart(projectApi);
log.info("Project is ✔️ on {}", apiClient.getBasePath());

ProjectResponseDTO testProject;
Expand Down Expand Up @@ -158,8 +158,21 @@ public void runComplexWorkFlow() throws ApiException, InterruptedException {
assertEquals(workFlowResponseDTO.getWorkStatus(), WorkStatusEnum.IN_PROGRESS);
log.info("Onboarding workflow execution id: {}", workFlowResponseDTO.getWorkFlowExecutionId());

WorkFlowStatusResponseDTO workFlowStatusResponseDTO = waitAsyncStatusResponse(workflowApi,
workFlowResponseDTO.getWorkFlowExecutionId());
WorkFlowResponseDTO finalWorkFlowResponseDTO = workFlowResponseDTO;

WorkFlowStatusResponseDTO workFlowStatusResponseDTO = waitAsyncResponse(
new ExamplesUtils.FuncExecutor<WorkFlowStatusResponseDTO>() {
@Override
public boolean check(WorkFlowStatusResponseDTO result) {
return !result.getStatus().equals(WorkStatus.COMPLETED.toString());
}

@Override
public void execute(ApiCallback<WorkFlowStatusResponseDTO> callback) throws ApiException {
workflowApi.getStatusAsync(finalWorkFlowResponseDTO.getWorkFlowExecutionId(), callback);
}
});

assertNotNull(workFlowStatusResponseDTO);
assertNotNull(workFlowStatusResponseDTO.getWorkFlowExecutionId());
assertEquals(WorkStatusEnum.COMPLETED.toString(), workFlowStatusResponseDTO.getStatus());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.redhat.parodos.examples.integration;

import com.redhat.parodos.examples.integration.utils.ExamplesUtils;
import com.redhat.parodos.sdk.api.ProjectApi;
import com.redhat.parodos.sdk.api.WorkflowApi;
import com.redhat.parodos.sdk.api.WorkflowDefinitionApi;
Expand Down Expand Up @@ -30,6 +29,7 @@
import java.util.List;

import static com.redhat.parodos.examples.integration.utils.ExamplesUtils.getProjectByNameAndDescription;
import static com.redhat.parodos.examples.integration.utils.ExamplesUtils.waitProjectStart;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
Expand Down Expand Up @@ -59,8 +59,8 @@ public void runSimpleWorkFlow() throws ApiException, InterruptedException {
log.info("Running simple flow");

ProjectApi projectApi = new ProjectApi(apiClient);
waitProjectStart(projectApi);

ExamplesUtils.waitProjectStart(projectApi);
log.info("Project is ✔️ on {}", apiClient.getBasePath());

ProjectResponseDTO testProject;
Expand Down Expand Up @@ -143,6 +143,7 @@ public void runSimpleWorkFlow() throws ApiException, InterruptedException {

WorkflowApi workflowApi = new WorkflowApi();
log.info("******** Running The Simple Sequence Flow ********");

WorkFlowResponseDTO workFlowResponseDTO = workflowApi.execute(workFlowRequestDTO);

assertNotNull(workFlowResponseDTO.getWorkFlowExecutionId());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,103 +1,46 @@

package com.redhat.parodos.examples.integration.utils;

import com.redhat.parodos.sdk.api.ProjectApi;
import com.redhat.parodos.sdk.invoker.ApiCallback;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import com.redhat.parodos.sdk.invoker.ApiClient;
import com.redhat.parodos.sdk.invoker.ApiException;
import com.redhat.parodos.sdk.model.ProjectRequestDTO;
import com.redhat.parodos.sdk.model.ProjectResponseDTO;
import com.redhat.parodos.sdk.model.WorkFlowStatusResponseDTO;
import com.redhat.parodos.workflows.work.WorkStatus;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Strings;

import javax.annotation.Nullable;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import javax.annotation.Nullable;

import static org.junit.Assert.fail;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import com.redhat.parodos.sdk.api.ProjectApi;
import com.redhat.parodos.sdk.invoker.ApiCallback;
import com.redhat.parodos.sdk.invoker.ApiException;
import org.assertj.core.util.Strings;
import com.redhat.parodos.sdk.model.ProjectResponseDTO;
import lombok.Data;

/**
* @author Gloria Ciavarrini (Github: gciavarrini)
*/
@Slf4j
public final class ExamplesUtils {

public static void waitProjectStart(ProjectApi projectApi) throws ApiException, InterruptedException {
AsyncWaitProjectResult asyncResult = new AsyncWaitProjectResult();
Lock lock = new ReentrantLock();
Condition response = lock.newCondition();
ApiCallback<List<ProjectResponseDTO>> apiCallback = new ApiCallback<>() {
@Override
public void onFailure(ApiException e, int statusCode, Map<String, List<String>> responseHeaders) {
try {
projectApi.getProjectsAsync(this);
}
catch (ApiException apie) {
asyncResult.setError(apie.getMessage());
signal();
}
}

@Override
public void onSuccess(List<ProjectResponseDTO> result, int statusCode,
Map<String, List<String>> responseHeaders) {
signal();
}

@Override
public void onUploadProgress(long bytesWritten, long contentLength, boolean done) {
}

@Override
public void onDownloadProgress(long bytesRead, long contentLength, boolean done) {
}

private void signal() {
lock.lock();
try {
response.signal();
}
finally {
lock.unlock();
}
}
};
projectApi.getProjectsAsync(apiCallback);
lock.lock();
try {
// should be more than enough
response.await(60, TimeUnit.SECONDS);
if (asyncResult.getError() != null) {
fail("An error occurred while executing getProjectAsync: " + asyncResult.getError());
}
}
finally {
lock.unlock();
}
}

public static WorkFlowStatusResponseDTO waitAsyncStatusResponse(WorkflowApi workflowApi, String workFlowExecutionId)
throws ApiException, InterruptedException {
AsyncStatusResult asyncResult = new AsyncStatusResult();
public static <T> T waitAsyncResponse(FuncExecutor<T> f) throws ApiException, InterruptedException {
AsyncResult<T> asyncResult = new AsyncResult<>();
Lock lock = new ReentrantLock();
Condition response = lock.newCondition();
ApiCallback<WorkFlowStatusResponseDTO> apiCallback = new ApiCallback<>() {
ApiCallback<T> apiCallback = new ApiCallback<T>() {

@Override
public void onFailure(ApiException e, int statusCode, Map<String, List<String>> responseHeaders) {
System.out.println("onFAILURE");

try {
workflowApi.getStatusAsync(workFlowExecutionId, this);
f.execute(this);
}
catch (ApiException apie) {
asyncResult.setError(apie.getMessage());
Expand All @@ -106,11 +49,10 @@ public void onFailure(ApiException e, int statusCode, Map<String, List<String>>
}

@Override
public void onSuccess(WorkFlowStatusResponseDTO result, int statusCode,
Map<String, List<String>> responseHeaders) {
if (!result.getStatus().equals(WorkStatus.COMPLETED.toString())) {
public void onSuccess(T result, int statusCode, Map<String, List<String>> responseHeaders) {
if (f.check(result)) {
try {
workflowApi.getStatusAsync(workFlowExecutionId, this);
f.execute(this);
}
catch (ApiException apie) {
asyncResult.setError(apie.getMessage());
Expand All @@ -123,7 +65,6 @@ public void onSuccess(WorkFlowStatusResponseDTO result, int statusCode,
asyncResult.setError(null);
signal();
}

}

@Override
Expand All @@ -144,14 +85,13 @@ private void signal() {
}
}
};
workflowApi.getStatusAsync(workFlowExecutionId, apiCallback);
f.execute(apiCallback);
lock.lock();
try {
// should be more than enough
response.await(60, TimeUnit.SECONDS);

if (asyncResult.getError() != null) {
fail("An error occurred while executing waitAsyncStatusResponse: " + asyncResult.getError());
fail("An error occurred while executing waitAsyncResponse: " + asyncResult.getError());
}
}
finally {
Expand All @@ -160,6 +100,10 @@ private void signal() {
return asyncResult.getResult();
}

public static void waitProjectStart(ProjectApi projectApi) throws InterruptedException, ApiException {
waitAsyncResponse((FuncExecutor<List<ProjectResponseDTO>>) callback -> projectApi.getProjectsAsync(callback));
}

@Nullable
public static ProjectResponseDTO getProjectByNameAndDescription(List<ProjectResponseDTO> projects,
String projectName, String projectDescription) {
Expand All @@ -170,20 +114,23 @@ public static ProjectResponseDTO getProjectByNameAndDescription(List<ProjectResp
}

@Data
private static class AsyncWaitProjectResult {
private static class AsyncResult<T> {

private String error;

}
T result;

@Data
private static class AsyncStatusResult {
int statusCode;

WorkFlowStatusResponseDTO result;
}

int statusCode;
public interface FuncExecutor<T> {

private String error;
void execute(ApiCallback<T> callback) throws ApiException;

default boolean check(T result) {
return true;
}

}

Expand Down

0 comments on commit 4f56f05

Please sign in to comment.