Skip to content

Commit 383a213

Browse files
Introduce Java Fluent DSL (#646)
* Introduce Java Fluent DSL Signed-off-by: Ricardo Zanini <ricardozanini@gmail.com> * Fix taskItems builder - refactor Signed-off-by: Ricardo Zanini <ricardozanini@gmail.com> * Add tests to the lambda module, integrate the DSL Signed-off-by: Ricardo Zanini <ricardozanini@gmail.com> --------- Signed-off-by: Ricardo Zanini <ricardozanini@gmail.com>
1 parent 8399ff3 commit 383a213

File tree

27 files changed

+1504
-253
lines changed

27 files changed

+1504
-253
lines changed

experimental/lambda/pom.xml

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
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-lambda</artifactId>
9-
<name>ServelessWorkflow:: Experimental:: lambda</name>
10-
<dependencies>
11-
<dependency>
12-
<groupId>io.serverlessworkflow</groupId>
13-
<artifactId>serverlessworkflow-experimental-types</artifactId>
14-
</dependency>
15-
<dependency>
16-
<groupId>io.serverlessworkflow</groupId>
17-
<artifactId>serverlessworkflow-impl-core</artifactId>
18-
</dependency>
19-
<dependency>
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>io.serverlessworkflow</groupId>
6+
<artifactId>serverlessworkflow-experimental</artifactId>
7+
<version>8.0.0-SNAPSHOT</version>
8+
</parent>
9+
<artifactId>serverlessworkflow-experimental-lambda</artifactId>
10+
<name>ServelessWorkflow:: Experimental:: lambda</name>
11+
<dependencies>
12+
<dependency>
13+
<groupId>io.serverlessworkflow</groupId>
14+
<artifactId>serverlessworkflow-experimental-types</artifactId>
15+
</dependency>
16+
<dependency>
17+
<groupId>io.serverlessworkflow</groupId>
18+
<artifactId>serverlessworkflow-impl-core</artifactId>
19+
</dependency>
20+
<dependency>
21+
<groupId>io.serverlessworkflow</groupId>
22+
<artifactId>serverlessworkflow-fluent-java</artifactId>
23+
</dependency>
24+
<dependency>
2025
<groupId>org.junit.jupiter</groupId>
2126
<artifactId>junit-jupiter-api</artifactId>
2227
<scope>test</scope>
@@ -41,5 +46,5 @@
4146
<artifactId>logback-classic</artifactId>
4247
<scope>test</scope>
4348
</dependency>
44-
</dependencies>
49+
</dependencies>
4550
</project>

experimental/lambda/src/test/java/io/serverless/workflow/impl/CallTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void testForLoop() throws InterruptedException, ExecutionException {
8080
new Task()
8181
.withForTask(
8282
new ForTaskFunction()
83-
.withWhile(this::isEven)
83+
.withWhile(CallTest::isEven)
8484
.withCollection(v -> (Collection) v)
8585
.withFor(forConfig)
8686
.withDo(
@@ -91,7 +91,7 @@ void testForLoop() throws InterruptedException, ExecutionException {
9191
.withCallTask(
9292
new CallTaskJava(
9393
CallJava.loopFunction(
94-
this::sum,
94+
CallTest::sum,
9595
forConfig.getEach()))))))))));
9696

9797
assertThat(
@@ -124,35 +124,35 @@ void testSwitch() throws InterruptedException, ExecutionException {
124124
new SwitchItem(
125125
"odd",
126126
new SwitchCaseFunction()
127-
.withPredicate(this::isOdd)
127+
.withPredicate(CallTest::isOdd)
128128
.withThen(
129129
new FlowDirective()
130130
.withFlowDirectiveEnum(
131131
FlowDirectiveEnum.END))))))),
132132
new TaskItem(
133133
"java",
134134
new Task()
135-
.withCallTask(new CallTaskJava(CallJava.function(this::zero))))));
135+
.withCallTask(new CallTaskJava(CallJava.function(CallTest::zero))))));
136136

137137
WorkflowDefinition definition = app.workflowDefinition(workflow);
138138
assertThat(definition.instance(3).start().get().asNumber().orElseThrow()).isEqualTo(3);
139139
assertThat(definition.instance(4).start().get().asNumber().orElseThrow()).isEqualTo(0);
140140
}
141141
}
142142

143-
private boolean isEven(Object model, Integer number) {
143+
public static boolean isEven(Object model, Integer number) {
144144
return !isOdd(number);
145145
}
146146

147-
private boolean isOdd(Integer number) {
147+
public static boolean isOdd(Integer number) {
148148
return number % 2 != 0;
149149
}
150150

151-
private int zero(Integer value) {
151+
public static int zero(Integer value) {
152152
return 0;
153153
}
154154

155-
private Integer sum(Object model, Integer item) {
155+
public static Integer sum(Object model, Integer item) {
156156
return model instanceof Collection ? item : (Integer) model + item;
157157
}
158158
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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.serverless.workflow.impl;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
import io.serverlessworkflow.api.types.FlowDirectiveEnum;
21+
import io.serverlessworkflow.api.types.Workflow;
22+
import io.serverlessworkflow.fluent.java.JavaWorkflowBuilder;
23+
import io.serverlessworkflow.impl.WorkflowApplication;
24+
import io.serverlessworkflow.impl.WorkflowDefinition;
25+
import java.util.Collection;
26+
import java.util.List;
27+
import java.util.concurrent.ExecutionException;
28+
import org.junit.jupiter.api.Test;
29+
30+
public class FluentDSLCallTest {
31+
32+
@Test
33+
void testJavaFunction() throws InterruptedException, ExecutionException {
34+
try (WorkflowApplication app = WorkflowApplication.builder().build()) {
35+
final Workflow workflow =
36+
JavaWorkflowBuilder.workflow("testJavaCall")
37+
.tasks(tasks -> tasks.callFn(f -> f.fn(JavaFunctions::getName)))
38+
.build();
39+
assertThat(
40+
app.workflowDefinition(workflow)
41+
.instance(new Person("Francisco", 33))
42+
.start()
43+
.get()
44+
.asText()
45+
.orElseThrow())
46+
.isEqualTo("Francisco Javierito");
47+
}
48+
}
49+
50+
@Test
51+
void testForLoop() throws InterruptedException, ExecutionException {
52+
try (WorkflowApplication app = WorkflowApplication.builder().build()) {
53+
Workflow workflow =
54+
JavaWorkflowBuilder.workflow()
55+
.tasks(
56+
t ->
57+
t.forFn(
58+
f ->
59+
f.whileC(CallTest::isEven)
60+
.collection(v -> (Collection<?>) v)
61+
.tasks(CallTest::sum)))
62+
.build();
63+
64+
assertThat(
65+
app.workflowDefinition(workflow)
66+
.instance(List.of(2, 4, 6))
67+
.start()
68+
.get()
69+
.asNumber()
70+
.orElseThrow())
71+
.isEqualTo(12);
72+
}
73+
}
74+
75+
@Test
76+
void testSwitch() throws InterruptedException, ExecutionException {
77+
try (WorkflowApplication app = WorkflowApplication.builder().build()) {
78+
Workflow workflow =
79+
JavaWorkflowBuilder.workflow()
80+
.tasks(
81+
tasks ->
82+
tasks
83+
.switchFn(
84+
switchOdd ->
85+
switchOdd.items(
86+
item ->
87+
item.when(CallTest::isOdd).then(FlowDirectiveEnum.END)))
88+
.callFn(callJava -> callJava.fn(CallTest::zero)))
89+
.build();
90+
91+
WorkflowDefinition definition = app.workflowDefinition(workflow);
92+
assertThat(definition.instance(3).start().get().asNumber().orElseThrow()).isEqualTo(3);
93+
assertThat(definition.instance(4).start().get().asNumber().orElseThrow()).isEqualTo(0);
94+
}
95+
}
96+
}

experimental/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
<artifactId>serverlessworkflow-experimental-types</artifactId>
3131
<version>${project.version}</version>
3232
</dependency>
33+
<dependency>
34+
<groupId>io.serverlessworkflow</groupId>
35+
<artifactId>serverlessworkflow-fluent-java</artifactId>
36+
<version>${project.version}</version>
37+
</dependency>
3338
</dependencies>
3439
</dependencyManagement>
3540
<modules>

experimental/types/src/main/java/io/serverlessworkflow/api/types/SwitchCaseFunction.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public <T> SwitchCaseFunction withPredicate(Predicate<T> predicate) {
2727
return this;
2828
}
2929

30+
public <T> void setPredicate(Predicate<T> predicate) {
31+
this.predicate = predicate;
32+
}
33+
3034
public Predicate<?> predicate() {
3135
return predicate;
3236
}

fluent/java/pom.xml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>io.serverlessworkflow</groupId>
8+
<artifactId>serverlessworkflow-fluent</artifactId>
9+
<version>8.0.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<name>Serverless Workflow :: Fluent :: Java</name>
13+
<artifactId>serverlessworkflow-fluent-java</artifactId>
14+
15+
<properties>
16+
<maven.compiler.source>17</maven.compiler.source>
17+
<maven.compiler.target>17</maven.compiler.target>
18+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>io.serverlessworkflow</groupId>
24+
<artifactId>serverlessworkflow-experimental-types</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>io.serverlessworkflow</groupId>
28+
<artifactId>serverlessworkflow-types</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>io.serverlessworkflow</groupId>
32+
<artifactId>serverlessworkflow-fluent-standard</artifactId>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>org.junit.jupiter</groupId>
37+
<artifactId>junit-jupiter-api</artifactId>
38+
<scope>test</scope>
39+
</dependency>
40+
</dependencies>
41+
42+
</project>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.java;
17+
18+
import io.serverlessworkflow.api.types.CallJava;
19+
import io.serverlessworkflow.api.types.CallTaskJava;
20+
import io.serverlessworkflow.fluent.standard.TaskBaseBuilder;
21+
import java.util.function.Function;
22+
23+
public class CallTaskJavaBuilder extends TaskBaseBuilder<CallTaskJavaBuilder>
24+
implements JavaTransformationHandlers<CallTaskJavaBuilder> {
25+
26+
private CallTaskJava callTaskJava;
27+
28+
CallTaskJavaBuilder() {
29+
callTaskJava = new CallTaskJava(new CallJava() {});
30+
super.setTask(callTaskJava.getCallJava());
31+
}
32+
33+
@Override
34+
protected CallTaskJavaBuilder self() {
35+
return this;
36+
}
37+
38+
public <T, V> CallTaskJavaBuilder fn(Function<T, V> function) {
39+
this.callTaskJava = new CallTaskJava(CallJava.function(function));
40+
super.setTask(this.callTaskJava.getCallJava());
41+
return this;
42+
}
43+
44+
public CallTaskJava build() {
45+
return this.callTaskJava;
46+
}
47+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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.java;
17+
18+
import io.serverlessworkflow.fluent.standard.BaseDoTaskBuilder;
19+
import java.util.function.Consumer;
20+
21+
public class DoTaskJavaBuilder extends BaseDoTaskBuilder<DoTaskJavaBuilder, TaskItemListJavaBuilder>
22+
implements JavaTransformationHandlers<DoTaskJavaBuilder> {
23+
24+
DoTaskJavaBuilder() {
25+
super(new TaskItemListJavaBuilder());
26+
}
27+
28+
@Override
29+
protected DoTaskJavaBuilder self() {
30+
return this;
31+
}
32+
33+
public DoTaskJavaBuilder callFn(String name, Consumer<CallTaskJavaBuilder> consumer) {
34+
this.innerListBuilder().callJava(name, consumer);
35+
return this;
36+
}
37+
38+
public DoTaskJavaBuilder callFn(Consumer<CallTaskJavaBuilder> consumer) {
39+
this.innerListBuilder().callJava(consumer);
40+
return this;
41+
}
42+
43+
public DoTaskJavaBuilder forFn(String name, Consumer<ForTaskJavaBuilder> consumer) {
44+
this.innerListBuilder().forFn(name, consumer);
45+
return this;
46+
}
47+
48+
public DoTaskJavaBuilder forFn(Consumer<ForTaskJavaBuilder> consumer) {
49+
this.innerListBuilder().forFn(consumer);
50+
return this;
51+
}
52+
53+
public DoTaskJavaBuilder switchFn(String name, Consumer<SwitchTaskJavaBuilder> consumer) {
54+
this.innerListBuilder().switchFn(name, consumer);
55+
return this;
56+
}
57+
58+
public DoTaskJavaBuilder switchFn(Consumer<SwitchTaskJavaBuilder> consumer) {
59+
this.innerListBuilder().switchFn(consumer);
60+
return this;
61+
}
62+
}

0 commit comments

Comments
 (0)