Skip to content

Commit 036b418

Browse files
committed
Add then to the Java DSL
Signed-off-by: Matheus Cruz <matheuscruz.dev@gmail.com>
1 parent 3b36c05 commit 036b418

File tree

4 files changed

+136
-1
lines changed

4 files changed

+136
-1
lines changed

experimental/fluent/func/src/main/java/io/serverlessworkflow/fluent/func/dsl/Step.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,21 @@ public SELF when(String jqExpr) {
6262
return self();
6363
}
6464

65+
/**
66+
* Queue a {@code then(taskName)} to be applied on the concrete builder. Directs the workflow
67+
* engine to jump to the named task after this one completes.
68+
*
69+
* @param taskName the name of the next task to execute
70+
* @return this step for further chaining
71+
* @see <a
72+
* href="https://github.com/serverlessworkflow/specification/blob/main/dsl-reference.md#task">DSL
73+
* Reference - Task</a>
74+
*/
75+
public SELF then(String taskName) {
76+
postConfigurers.add(b -> ((TaskBaseBuilder<?>) b).then(taskName));
77+
return self();
78+
}
79+
6580
// ---------------------------------------------------------------------------
6681
// FuncTaskTransformations passthroughs: EXPORT (fn/context/filter + JQ)
6782
// ---------------------------------------------------------------------------

experimental/fluent/func/src/test/java/io/serverlessworkflow/fluent/func/FuncDSLTest.java

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
package io.serverlessworkflow.fluent.func;
1717

1818
import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.call;
19+
import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.consume;
1920
import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.emit;
2021
import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.event;
2122
import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.function;
2223
import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.get;
2324
import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.http;
2425
import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.listen;
2526
import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.toOne;
26-
import static io.serverlessworkflow.fluent.spec.dsl.DSL.auth;
2727
import static io.serverlessworkflow.fluent.spec.dsl.DSL.use;
2828
import static org.junit.jupiter.api.Assertions.assertEquals;
2929
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
@@ -432,4 +432,48 @@ void call_with_preconfigured_http_spec() {
432432
.getUse());
433433
assertEquals(Map.of("foo", "bar"), http.getWith().getBody());
434434
}
435+
436+
@Test
437+
@DisplayName("function(name, fn).then(taskName)")
438+
void function_step_then_task_name_sets_flow_directive() {
439+
Workflow wf =
440+
FuncWorkflowBuilder.workflow("intelligent-newsletter")
441+
.tasks(
442+
function("myfunction", String::trim, String.class).then("otherTask"),
443+
function("otherTask", String::strip, String.class))
444+
.build();
445+
446+
List<TaskItem> items = wf.getDo();
447+
assertEquals(2, items.size());
448+
449+
Task t = items.get(0).getTask();
450+
assertNotNull(t.getCallTask(), "CallTask expected");
451+
452+
CallJava callJava = (CallJava) t.getCallTask().get();
453+
assertNotNull(callJava.getThen(), "then() should be set on the task");
454+
assertEquals("otherTask", callJava.getThen().getString(), "then() should point to 'otherTask'");
455+
}
456+
457+
@Test
458+
@DisplayName("consume(name, Consumer, Class).then(taskName) should jump to otherTask")
459+
void consume_step_then_task_name_sets_flow_directive() {
460+
461+
Workflow wf =
462+
FuncWorkflowBuilder.workflow("intelligent-newsletter")
463+
.tasks(
464+
consume("sendNewsletter", (String s) -> {}, String.class).then("otherTask"),
465+
function("nextTask", String::strip, String.class),
466+
function("otherTask", String::strip, String.class))
467+
.build();
468+
469+
List<TaskItem> items = wf.getDo();
470+
assertEquals(3, items.size());
471+
472+
Task t = items.get(0).getTask();
473+
assertNotNull(t.getCallTask(), "CallTask expected for consume step");
474+
475+
CallJava callJava = (CallJava) t.getCallTask().get();
476+
assertNotNull(callJava.getThen(), "then() should be set on the consume task");
477+
assertEquals("otherTask", callJava.getThen().getString(), "then() should point to 'otherTask'");
478+
}
435479
}

impl/test/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,18 @@
9494
<artifactId>grpc-netty</artifactId>
9595
<scope>test</scope>
9696
</dependency>
97+
<dependency>
98+
<groupId>io.serverlessworkflow</groupId>
99+
<artifactId>serverlessworkflow-experimental-fluent-func</artifactId>
100+
<scope>test</scope>
101+
<version>${project.version}</version>
102+
</dependency>
103+
<dependency>
104+
<groupId>io.serverlessworkflow</groupId>
105+
<artifactId>serverlessworkflow-experimental-lambda</artifactId>
106+
<scope>test</scope>
107+
<version>${project.version}</version>
108+
</dependency>
97109
</dependencies>
98110
<build>
99111
<resources>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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.impl.test;
17+
18+
import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.consume;
19+
import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.function;
20+
21+
import io.serverlessworkflow.api.types.Workflow;
22+
import io.serverlessworkflow.fluent.func.FuncWorkflowBuilder;
23+
import io.serverlessworkflow.impl.WorkflowApplication;
24+
import io.serverlessworkflow.impl.WorkflowDefinition;
25+
import java.util.Map;
26+
import java.util.concurrent.atomic.AtomicInteger;
27+
import org.junit.jupiter.api.Assertions;
28+
import org.junit.jupiter.api.Test;
29+
30+
public class ThenTest {
31+
32+
@Test
33+
void test() {
34+
AtomicInteger a = new AtomicInteger();
35+
AtomicInteger b = new AtomicInteger();
36+
37+
Workflow wf =
38+
FuncWorkflowBuilder.workflow("intelligent-newsletter")
39+
.tasks(
40+
consume("sendNewsletter", (String s) -> {}, String.class).then("otherTask"),
41+
function(
42+
"nextTask",
43+
(v) -> {
44+
a.incrementAndGet();
45+
return v.strip();
46+
},
47+
String.class),
48+
function(
49+
"otherTask",
50+
(v) -> {
51+
b.incrementAndGet();
52+
return v.strip();
53+
},
54+
String.class))
55+
.build();
56+
57+
WorkflowApplication app = WorkflowApplication.builder().build();
58+
WorkflowDefinition def = app.workflowDefinition(wf);
59+
def.instance(Map.of()).start().join();
60+
61+
Assertions.assertEquals(1, b.get(), "otherTask should execute");
62+
Assertions.assertEquals(0, a.get(), "nextTask should not execute");
63+
}
64+
}

0 commit comments

Comments
 (0)