Skip to content

Commit fc20e5d

Browse files
committed
[Fix #1280] Support both casting to JsonNode and Collection
Signed-off-by: fjtirado <ftirados@redhat.com>
1 parent 3ea385e commit fc20e5d

File tree

5 files changed

+139
-0
lines changed

5 files changed

+139
-0
lines changed

experimental/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,6 @@
4949
<module>lambda-fluent</module>
5050
<module>fluent</module>
5151
<module>model</module>
52+
<module>test</module>
5253
</modules>
5354
</project>

experimental/test/pom.xml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<parent>
4+
<groupId>io.serverlessworkflow</groupId>
5+
<artifactId>serverlessworkflow-experimental</artifactId>
6+
<version>8.0.0-SNAPSHOT</version>
7+
</parent>
8+
<artifactId>serverlessworkflow-experimental-test</artifactId>
9+
<name>Serverless Workflow :: Experimental :: Test</name>
10+
<dependencies>
11+
<dependency>
12+
<groupId>org.junit.jupiter</groupId>
13+
<artifactId>junit-jupiter-engine</artifactId>
14+
<scope>test</scope>
15+
</dependency>
16+
<dependency>
17+
<groupId>org.mockito</groupId>
18+
<artifactId>mockito-core</artifactId>
19+
<scope>test</scope>
20+
</dependency>
21+
<dependency>
22+
<groupId>org.assertj</groupId>
23+
<artifactId>assertj-core</artifactId>
24+
<scope>test</scope>
25+
</dependency>
26+
<dependency>
27+
<groupId>io.serverlessworkflow</groupId>
28+
<artifactId>serverlessworkflow-experimental-fluent-func</artifactId>
29+
<scope>test</scope>
30+
</dependency>
31+
<dependency>
32+
<groupId>io.serverlessworkflow</groupId>
33+
<artifactId>serverlessworkflow-experimental-lambda</artifactId>
34+
<scope>test</scope>
35+
</dependency>
36+
<dependency>
37+
<groupId>io.serverlessworkflow</groupId>
38+
<artifactId>serverlessworkflow-impl-model</artifactId>
39+
<version>${project.version}</version>
40+
<scope>test</scope>
41+
</dependency>
42+
</dependencies>
43+
</project>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverlessworkflow.fluent.test;
17+
18+
import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.emitJson;
19+
import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.listen;
20+
import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.toOne;
21+
import static org.assertj.core.api.Assertions.assertThat;
22+
23+
import com.fasterxml.jackson.databind.node.ArrayNode;
24+
import io.serverlessworkflow.api.types.Workflow;
25+
import io.serverlessworkflow.fluent.func.FuncWorkflowBuilder;
26+
import io.serverlessworkflow.impl.WorkflowApplication;
27+
import io.serverlessworkflow.impl.WorkflowModel;
28+
import java.util.Collection;
29+
import java.util.Map;
30+
import java.util.concurrent.CompletableFuture;
31+
import org.junit.jupiter.api.Test;
32+
33+
/**
34+
* Tests for the Event Filter DSL specification. Verifies that the fluent builder correctly wires
35+
* the payload parsing and contextual lambdas into the final Workflow definitions.
36+
*/
37+
class FuncEventFilterTest {
38+
39+
@Test
40+
void testListenToOneCollection() {
41+
runIt(
42+
FuncWorkflowBuilder.workflow("listenToOneReviewCol")
43+
.tasks(
44+
listen("waitReview", toOne("org.acme.test.review"))
45+
.outputAs((Collection<?> node) -> node.iterator().next()))
46+
.build());
47+
}
48+
49+
@Test
50+
void testListenToOneNode() {
51+
runIt(
52+
FuncWorkflowBuilder.workflow("listenToOneReviewNode")
53+
.tasks(
54+
listen("waitReview", toOne("org.acme.test.review"))
55+
.outputAs((ArrayNode node) -> node.get(0)))
56+
.build());
57+
}
58+
59+
private Workflow reviewEmitter() {
60+
return FuncWorkflowBuilder.workflow("emitReview")
61+
.tasks(emitJson("draftReady", "org.acme.test.review", Review.class))
62+
.build();
63+
}
64+
65+
private void runIt(Workflow listen) {
66+
Review review = new Review("Torrente", "espectacular", 5);
67+
try (WorkflowApplication app = WorkflowApplication.builder().build()) {
68+
CompletableFuture<WorkflowModel> waiting =
69+
app.workflowDefinition(listen).instance(Map.of()).start();
70+
app.workflowDefinition(reviewEmitter()).instance(review).start().join();
71+
assertThat(waiting.join().as(Review.class).orElseThrow()).isEqualTo(review);
72+
}
73+
}
74+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverlessworkflow.fluent.test;
17+
18+
record Review(String title, String description, int rating) {}

impl/model/src/main/java/io/serverlessworkflow/impl/model/jackson/JacksonModelCollection.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ public class JacksonModelCollection implements WorkflowModelCollection {
3838

3939
@Override
4040
public <T> Optional<T> as(Class<T> clazz) {
41+
if (clazz.equals(Collection.class)) {
42+
return Optional.of(clazz.cast(this));
43+
}
4144
return clazz.isAssignableFrom(ArrayNode.class)
4245
? Optional.of(clazz.cast(node))
4346
: Optional.empty();

0 commit comments

Comments
 (0)