Skip to content

Commit

Permalink
Adding listen to all test
Browse files Browse the repository at this point in the history
Signed-off-by: Francisco Javier Tirado Sarti <[email protected]>
  • Loading branch information
fjtirado committed Jan 27, 2025
1 parent 5d6a4b5 commit dc2223b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 19 deletions.
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

0 comments on commit dc2223b

Please sign in to comment.