Skip to content

Commit

Permalink
refactors package structure and adds more java doc
Browse files Browse the repository at this point in the history
  • Loading branch information
eduwercamacaro committed Dec 20, 2024
1 parent 9d267b6 commit 0d7d30d
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 32 deletions.
2 changes: 1 addition & 1 deletion server/src/test/java/e2e/JsonArrSearchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
import io.littlehorse.sdk.wfsdk.Workflow;
import io.littlehorse.test.LHTest;
import io.littlehorse.test.LHWorkflow;
import io.littlehorse.test.SearchResultCaptor;
import io.littlehorse.test.WorkflowVerifier;
import io.littlehorse.test.internal.TestExecutionContext;
import io.littlehorse.test.internal.step.SearchResultCaptor;
import java.util.List;
import java.util.function.Function;
import org.junit.jupiter.api.Test;
Expand Down
2 changes: 1 addition & 1 deletion server/src/test/java/e2e/UserTaskTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
import io.littlehorse.test.LHTest;
import io.littlehorse.test.LHUserTaskForm;
import io.littlehorse.test.LHWorkflow;
import io.littlehorse.test.SearchResultCaptor;
import io.littlehorse.test.WorkflowVerifier;
import io.littlehorse.test.internal.TestExecutionContext;
import io.littlehorse.test.internal.step.SearchResultCaptor;
import java.time.Duration;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
Expand Down
2 changes: 1 addition & 1 deletion server/src/test/java/e2e/WfRunSearchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
import io.littlehorse.sdk.worker.LHTaskMethod;
import io.littlehorse.test.LHTest;
import io.littlehorse.test.LHWorkflow;
import io.littlehorse.test.SearchResultCaptor;
import io.littlehorse.test.WorkflowVerifier;
import io.littlehorse.test.internal.TestExecutionContext;
import io.littlehorse.test.internal.step.SearchResultCaptor;
import java.util.List;
import java.util.function.Function;
import org.assertj.core.api.Assertions;
Expand Down
11 changes: 9 additions & 2 deletions test-utils/src/main/java/io/littlehorse/test/CapturedResult.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package io.littlehorse.test;

import io.littlehorse.test.internal.step.VerifyTaskRunOutputsStep;

/**
* {@code CapturedResult} is a wrapper for a response. see {@link VerifyTaskRunOutputsStep.SearchResultCaptor}
* @param <T> Search response type
*/
public interface CapturedResult<T> {

/**
* Returns the search response value
*/
T get();

void set(T result);

Class<T> type();
}
3 changes: 3 additions & 0 deletions test-utils/src/main/java/io/littlehorse/test/LHTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@
@ExtendWith(LHExtension.class)
public @interface LHTest {

/**
* An array of external events that will be registered during bootstrapping.
*/
String[] externalEventNames() default {};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.littlehorse.test.internal.step;

import io.littlehorse.test.CapturedResult;

class CapturedResultImpl<T> implements CapturedResult<T> {

private T value;
private final Class<T> target;

public CapturedResultImpl(Class<T> target) {
this.target = target;
}

@Override
public T get() {
return value;
}

public void set(T result) {
if (value != null) {
throw new IllegalStateException("Already has value");
}
value = result;
}

@Override
public Class<T> type() {
return target;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
package io.littlehorse.test;
package io.littlehorse.test.internal.step;

import com.google.protobuf.Message;
import io.littlehorse.test.CapturedResult;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
* {@code SearchResultCaptor} intercepts search result at any point of the workflow execution.
* <blockquote><pre>
* workflowVerifier.prepareRun(myWorkflow)
* .doSearch(SearchWfSpecRequest.class, searchResultCaptor.capture(), searchRequest)
* .start();
* CapturedResult<WfSpecIdList> capturedResult = searchResultCaptor.getValue();
* </pre></blockquote>
* @param <T> Search response type.
*/
public final class SearchResultCaptor<T extends Message> {
private final Class<T> target;
private final AtomicInteger currentIndex = new AtomicInteger();
Expand All @@ -19,18 +30,30 @@ public static <T extends Message> SearchResultCaptor<T> of(final Class<T> target
return new SearchResultCaptor<>(target);
}

/**
* Captures the result within a {@code Step}
* @return search response
*/
public CapturedResult<T> capture() {
CapturedResult<T> captured = new CapturedResultImpl();
CapturedResult<T> captured = new CapturedResultImpl(target);
results.add(captured);
return captured;
}

/**
* Ignores the result
*/
public CapturedResult<T> skip() {
SkipResult skipResult = new SkipResult();
results.add(skipResult);
return skipResult;
}

/**
* Returns current value.
* Note: this method guarantees the order of the values during the execution.
* @return search response
*/
public CapturedResult<T> getValue() {
CapturedResult<T> result;
do {
Expand All @@ -44,36 +67,13 @@ public CapturedResult<T> getValue() {
return result;
}

private final class CapturedResultImpl implements CapturedResult<T> {
private T value;

@Override
public T get() {
return value;
}

@Override
public void set(T result) {
if (value != null) {
throw new IllegalStateException("Already has value");
}
value = result;
}

@Override
public Class<T> type() {
return target;
}
}

private final class SkipResult implements CapturedResult<T> {

@Override
public T get() {
throw new UnsupportedOperationException();
}

@Override
public void set(T result) {
// skip
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@

public class SearchStep<I, O> implements Step {

private final CapturedResult<O> resultHolder;
private final CapturedResultImpl<O> resultHolder;
private final Function<TestExecutionContext, I> requestBuilder;

private final Method targetRpc;

public SearchStep(
Class<I> requestType, Function<TestExecutionContext, I> requestBuilder, CapturedResult<O> resultHolder) {
this.resultHolder = resultHolder;
if (!(resultHolder instanceof CapturedResultImpl)) {
throw new IllegalArgumentException("resultHolder must be CapturedResultImpl");
}
this.resultHolder = (CapturedResultImpl) resultHolder;
this.requestBuilder = requestBuilder;
this.targetRpc = findTargetRpc(requestType, resultHolder.type());
}
Expand Down

0 comments on commit 0d7d30d

Please sign in to comment.