Skip to content

Commit b3286fa

Browse files
authored
Merge pull request #512 from fjtirado/api_start_changes
Api start changes
2 parents 96000ad + c3124e2 commit b3286fa

File tree

4 files changed

+54
-36
lines changed

4 files changed

+54
-36
lines changed

impl/core/src/main/java/io/serverlessworkflow/impl/WorkflowDefinition.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static WorkflowDefinition of(WorkflowApplication application, Workflow workflow,
7373
application, workflow, application.resourceLoaderFactory().getResourceLoader(path));
7474
}
7575

76-
public WorkflowInstance execute(Object input) {
76+
public WorkflowInstance instance(Object input) {
7777
return new WorkflowInstance(this, JsonUtils.fromValue(input));
7878
}
7979

impl/core/src/main/java/io/serverlessworkflow/impl/WorkflowInstance.java

+24-10
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,25 @@ public class WorkflowInstance {
2828
private final String id;
2929
private final JsonNode input;
3030

31-
private final Instant startedAt;
31+
private WorkflowContext workflowContext;
32+
private WorkflowDefinition definition;
33+
private Instant startedAt;
34+
private Instant completedAt;
35+
private volatile JsonNode output;
3236
private CompletableFuture<JsonNode> completableFuture;
33-
private final WorkflowContext workflowContext;
3437

3538
WorkflowInstance(WorkflowDefinition definition, JsonNode input) {
3639
this.id = definition.idFactory().get();
3740
this.input = input;
41+
this.definition = definition;
42+
this.status = new AtomicReference<>(WorkflowStatus.PENDING);
3843
definition.inputSchemaValidator().ifPresent(v -> v.validate(input));
44+
}
45+
46+
public CompletableFuture<JsonNode> start() {
3947
this.startedAt = Instant.now();
4048
this.workflowContext = new WorkflowContext(definition, this);
41-
this.status = new AtomicReference<>(WorkflowStatus.RUNNING);
49+
this.status.set(WorkflowStatus.RUNNING);
4250
this.completableFuture =
4351
TaskExecutorHelper.processTaskList(
4452
definition.startTask(),
@@ -49,18 +57,20 @@ public class WorkflowInstance {
4957
.map(f -> f.apply(workflowContext, null, input))
5058
.orElse(input))
5159
.thenApply(this::whenCompleted);
60+
return completableFuture;
5261
}
5362

5463
private JsonNode whenCompleted(JsonNode node) {
55-
JsonNode model =
64+
output =
5665
workflowContext
5766
.definition()
5867
.outputFilter()
5968
.map(f -> f.apply(workflowContext, null, node))
6069
.orElse(node);
61-
workflowContext.definition().outputSchemaValidator().ifPresent(v -> v.validate(model));
70+
workflowContext.definition().outputSchemaValidator().ifPresent(v -> v.validate(output));
6271
status.compareAndSet(WorkflowStatus.RUNNING, WorkflowStatus.COMPLETED);
63-
return model;
72+
completedAt = Instant.now();
73+
return output;
6474
}
6575

6676
public String id() {
@@ -71,6 +81,10 @@ public Instant startedAt() {
7181
return startedAt;
7282
}
7383

84+
public Instant completedAt() {
85+
return completedAt;
86+
}
87+
7488
public JsonNode input() {
7589
return input;
7690
}
@@ -83,11 +97,11 @@ public void status(WorkflowStatus state) {
8397
this.status.set(state);
8498
}
8599

86-
public CompletableFuture<Object> output() {
87-
return outputAsJsonNode().thenApply(JsonUtils::toJavaValue);
100+
public Object output() {
101+
return JsonUtils.toJavaValue(outputAsJsonNode());
88102
}
89103

90-
public CompletableFuture<JsonNode> outputAsJsonNode() {
91-
return completableFuture.thenApply(this::whenCompleted);
104+
public JsonNode outputAsJsonNode() {
105+
return output;
92106
}
93107
}

impl/core/src/test/java/io/serverlessworkflow/impl/WorkflowDefinitionTest.java

+24-22
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
import com.fasterxml.jackson.databind.JsonNode;
2424
import com.fasterxml.jackson.databind.node.ArrayNode;
25-
import com.fasterxml.jackson.databind.node.ObjectNode;
2625
import io.serverlessworkflow.impl.json.JsonUtils;
2726
import java.io.IOException;
2827
import java.time.Instant;
@@ -59,29 +58,25 @@ private static Stream<Arguments> provideParameters() {
5958
args(
6059
"switch-then-string.yaml",
6160
Map.of("orderType", "electronic"),
62-
o ->
63-
assertThat(o.output().join())
64-
.isEqualTo(Map.of("validate", true, "status", "fulfilled"))),
61+
o -> assertThat(o).isEqualTo(Map.of("validate", true, "status", "fulfilled"))),
6562
args(
6663
"switch-then-string.yaml",
6764
Map.of("orderType", "physical"),
6865
o ->
69-
assertThat(o.output().join())
66+
assertThat(o)
7067
.isEqualTo(Map.of("inventory", "clear", "items", 1, "address", "Elmer St"))),
7168
args(
7269
"switch-then-string.yaml",
7370
Map.of("orderType", "unknown"),
74-
o ->
75-
assertThat(o.output().join())
76-
.isEqualTo(Map.of("log", "warn", "message", "something's wrong"))),
71+
o -> assertThat(o).isEqualTo(Map.of("log", "warn", "message", "something's wrong"))),
7772
args(
7873
"for-sum.yaml",
7974
Map.of("input", Arrays.asList(1, 2, 3)),
80-
o -> assertThat(o.output().join()).isEqualTo(6)),
75+
o -> assertThat(o).isEqualTo(6)),
8176
args(
8277
"for-collect.yaml",
8378
Map.of("input", Arrays.asList(1, 2, 3)),
84-
o -> assertThat(o.output().join()).isEqualTo(Map.of("output", Arrays.asList(2, 4, 6)))),
79+
o -> assertThat(o).isEqualTo(Map.of("output", Arrays.asList(2, 4, 6)))),
8580
args(
8681
"simple-expression.yaml",
8782
Map.of("input", Arrays.asList(1, 2, 3)),
@@ -97,16 +92,25 @@ private static Stream<Arguments> provideParameters() {
9792
args(
9893
"fork.yaml",
9994
Map.of(),
100-
o ->
101-
assertThat(((ObjectNode) o.outputAsJsonNode().join()).get("patientId").asText())
102-
.isIn("John", "Smith")),
103-
args("fork-no-compete.yaml", Map.of(), WorkflowDefinitionTest::checkNotCompeteOuput));
95+
o -> assertThat(((Map<String, Object>) o).get("patientId")).isIn("John", "Smith")),
96+
argsJson("fork-no-compete.yaml", Map.of(), WorkflowDefinitionTest::checkNotCompeteOuput));
10497
}
10598

10699
private static Arguments args(
107-
String fileName, Map<String, Object> input, Consumer<WorkflowInstance> instance) {
100+
String fileName, Map<String, Object> input, Consumer<Object> instance) {
101+
return Arguments.of(
102+
fileName,
103+
(Consumer<WorkflowDefinition>)
104+
d ->
105+
instance.accept(
106+
d.instance(input).start().thenApply(JsonUtils::toJavaValue).join()));
107+
}
108+
109+
private static Arguments argsJson(
110+
String fileName, Map<String, Object> input, Consumer<JsonNode> instance) {
108111
return Arguments.of(
109-
fileName, (Consumer<WorkflowDefinition>) d -> instance.accept(d.execute(input)));
112+
fileName,
113+
(Consumer<WorkflowDefinition>) d -> instance.accept(d.instance(input).start().join()));
110114
}
111115

112116
private static <T extends Throwable> Arguments args(
@@ -117,8 +121,7 @@ private static <T extends Throwable> Arguments args(
117121
d ->
118122
checkWorkflowException(
119123
catchThrowableOfType(
120-
CompletionException.class,
121-
() -> d.execute(Map.of()).outputAsJsonNode().join()),
124+
CompletionException.class, () -> d.instance(Map.of()).start().join()),
122125
consumer,
123126
clazz));
124127
}
@@ -129,8 +132,7 @@ private static <T extends Throwable> void checkWorkflowException(
129132
consumer.accept(clazz.cast(ex.getCause()));
130133
}
131134

132-
private static void checkNotCompeteOuput(WorkflowInstance instance) {
133-
JsonNode out = instance.outputAsJsonNode().join();
135+
private static void checkNotCompeteOuput(JsonNode out) {
134136
assertThat(out).isInstanceOf(ArrayNode.class);
135137
assertThat(out).hasSize(2);
136138
ArrayNode array = (ArrayNode) out;
@@ -156,8 +158,8 @@ private static void checkWorkflowException(WorkflowException ex) {
156158
assertThat(ex.getWorflowError().instance()).isEqualTo("do/0/notImplemented");
157159
}
158160

159-
private static void checkSpecialKeywords(WorkflowInstance obj) {
160-
Map<String, Object> result = (Map<String, Object>) obj.output().join();
161+
private static void checkSpecialKeywords(Object obj) {
162+
Map<String, Object> result = (Map<String, Object>) obj;
161163
assertThat(Instant.ofEpochMilli((long) result.get("startedAt")))
162164
.isAfterOrEqualTo(before)
163165
.isBeforeOrEqualTo(Instant.now());

impl/http/src/test/java/io/serverlessworkflow/impl/HTTPWorkflowDefinitionTest.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.assertj.core.api.Assertions.assertThat;
2020
import static org.assertj.core.api.Assertions.catchThrowableOfType;
2121

22+
import io.serverlessworkflow.impl.json.JsonUtils;
2223
import java.io.IOException;
2324
import java.util.Map;
2425
import java.util.stream.Stream;
@@ -44,8 +45,9 @@ void testWorkflowExecution(String fileName, Object input, Condition<Object> cond
4445
throws IOException {
4546
assertThat(
4647
appl.workflowDefinition(readWorkflowFromClasspath(fileName))
47-
.execute(input)
48-
.output()
48+
.instance(input)
49+
.start()
50+
.thenApply(JsonUtils::toJavaValue)
4951
.join())
5052
.is(condition);
5153
}
@@ -60,7 +62,7 @@ void testWrongSchema(String fileName) {
6062
IllegalArgumentException exception =
6163
catchThrowableOfType(
6264
IllegalArgumentException.class,
63-
() -> appl.workflowDefinition(readWorkflowFromClasspath(fileName)).execute(Map.of()));
65+
() -> appl.workflowDefinition(readWorkflowFromClasspath(fileName)).instance(Map.of()));
6466
assertThat(exception)
6567
.isNotNull()
6668
.hasMessageContaining("There are JsonSchema validation errors");

0 commit comments

Comments
 (0)