Skip to content

Adding listen to all test #522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 27, 2025
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 @@ -80,7 +80,7 @@ private EventRegistrationBuilderCollection anyEvents(AnyEventConsumptionStrategy
}

private EventRegistrationBuilderCollection oneEvent(OneEventConsumptionStrategy oneStrategy) {
return new EventRegistrationBuilderCollection(List.of(from(oneStrategy.getOne())), false);
return new EventRegistrationBuilderCollection(List.of(from(oneStrategy.getOne())), true);
}

protected ListenExecutorBuilder(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.serverlessworkflow.api.WorkflowReader;
import io.serverlessworkflow.impl.json.JsonUtils;
Expand All @@ -27,7 +28,6 @@
import java.util.concurrent.CompletableFuture;
import java.util.stream.Stream;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand Down Expand Up @@ -58,15 +58,16 @@ void testEventListened(String listen, String emit, JsonNode expectedResult, Obje
assertThat(waitingInstance.outputAsJsonNode()).isEqualTo(expectedResult);
}

@Test
void testUntilConsumed() throws IOException {
@ParameterizedTest
@MethodSource("eventsListenerParameters")
void testEventsListened(String listen, String emit1, String emit2, JsonNode expectedResult)
throws IOException {
WorkflowDefinition listenDefinition =
appl.workflowDefinition(
WorkflowReader.readWorkflowFromClasspath("listen-to-any-until-consumed.yaml"));
appl.workflowDefinition(WorkflowReader.readWorkflowFromClasspath(listen));
WorkflowDefinition emitDoctorDefinition =
appl.workflowDefinition(WorkflowReader.readWorkflowFromClasspath("emit-doctor.yaml"));
appl.workflowDefinition(WorkflowReader.readWorkflowFromClasspath(emit1));
WorkflowDefinition emitOutDefinition =
appl.workflowDefinition(WorkflowReader.readWorkflowFromClasspath("emit-out.yaml"));
appl.workflowDefinition(WorkflowReader.readWorkflowFromClasspath(emit2));
WorkflowInstance waitingInstance = listenDefinition.instance(Map.of());
CompletableFuture<JsonNode> future = waitingInstance.start();
assertThat(waitingInstance.status()).isEqualTo(WorkflowStatus.RUNNING);
Expand All @@ -77,16 +78,30 @@ void testUntilConsumed() throws IOException {
emitOutDefinition.instance(Map.of()).start().join();
assertThat(future).isCompleted();
assertThat(waitingInstance.status()).isEqualTo(WorkflowStatus.COMPLETED);
assertThat(waitingInstance.outputAsJsonNode()).isEqualTo(temperature());
assertThat(waitingInstance.outputAsJsonNode()).isEqualTo(expectedResult);
}

private static Stream<Arguments> eventListenerParameters() {
return Stream.of(
Arguments.of("listen-to-any.yaml", "emit.yaml", cruellaDeVil(), Map.of()),
Arguments.of("listen-to-any.yaml", "emit.yaml", array(cruellaDeVil()), Map.of()),
Arguments.of(
"listen-to-any-filter.yaml", "emit-doctor.yaml", doctor(), Map.of("temperature", 39)));
}

private static Stream<Arguments> eventsListenerParameters() {
return Stream.of(
Arguments.of(
"listen-to-all.yaml",
"emit-doctor.yaml",
"emit.yaml",
array(temperature(), cruellaDeVil())),
Arguments.of(
"listen-to-any-until-consumed.yaml",
"emit-doctor.yaml",
"emit-out.yaml",
array(temperature())));
}

private static JsonNode cruellaDeVil() {
ObjectMapper mapper = JsonUtils.mapper();
ObjectNode node = mapper.createObjectNode();
Expand All @@ -97,21 +112,24 @@ private static JsonNode cruellaDeVil() {
mapper
.createArrayNode()
.add(mapper.createObjectNode().put("breed", "dalmatian").put("quantity", 101)));
return mapper.createArrayNode().add(node);
return node;
}

private static JsonNode doctor() {
ObjectMapper mapper = JsonUtils.mapper();
ObjectNode node = mapper.createObjectNode();
node.put("temperature", 39);
ObjectNode node = temperature();
node.put("isSick", true);
return mapper.createArrayNode().add(node);
return array(node);
}

private static JsonNode temperature() {
ObjectMapper mapper = JsonUtils.mapper();
ObjectNode node = mapper.createObjectNode();
private static ObjectNode temperature() {
ObjectNode node = JsonUtils.mapper().createObjectNode();
node.put("temperature", 39);
return mapper.createArrayNode().add(node);
return node;
}

private static JsonNode array(JsonNode... jsonNodes) {
ArrayNode arrayNode = JsonUtils.mapper().createArrayNode();
for (JsonNode node : jsonNodes) arrayNode.add(node);
return arrayNode;
}
}
15 changes: 15 additions & 0 deletions impl/core/src/test/resources/listen-to-all.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
document:
dsl: '1.0.0-alpha5'
namespace: test
name: listen-to-all
version: '0.1.0'
do:
- callDoctor:
listen:
to:
all:
- with:
type: com.fake-hospital.vitals.measurements.temperature
data: ${ .temperature > 38 }
- with:
type: com.petstore.order.placed.v1
Loading