Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ void testSwitch() throws InterruptedException, ExecutionException {
Workflow workflow =
new Workflow()
.withDocument(
new Document().withNamespace("test").withName("testSwith").withVersion("1.0"))
new Document().withNamespace("test").withName("testSwitch").withVersion("1.0"))
.withDo(
List.of(
new TaskItem(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
*/
package io.serverless.workflow.impl;

import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.function;
import static org.assertj.core.api.Assertions.assertThat;

import io.serverlessworkflow.api.types.FlowDirectiveEnum;
import io.serverlessworkflow.api.types.Workflow;
import io.serverlessworkflow.fluent.func.FuncWorkflowBuilder;
import io.serverlessworkflow.impl.WorkflowApplication;
import io.serverlessworkflow.impl.WorkflowDefinition;
import io.serverlessworkflow.impl.WorkflowInstance;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ExecutionException;
Expand All @@ -47,6 +49,23 @@ void testJavaFunction() throws InterruptedException, ExecutionException {
}
}

@Test
void textExportFunction() {
try (WorkflowApplication app = WorkflowApplication.builder().build()) {
final Workflow workflow =
FuncWorkflowBuilder.workflow("testExportFunction")
.tasks(
function(JavaFunctions::getName, Person.class)
.exportAs(
(prevContext, w, t) -> t.output().asText().orElseThrow() + " Tirado"))
.build();
WorkflowInstance instance =
app.workflowDefinition(workflow).instance(new Person("Francisco", 33));
assertThat(instance.start().join().asText().orElseThrow()).isEqualTo("Francisco Javierito");
assertThat(instance.context().asText().orElseThrow()).isEqualTo("Francisco Javierito Tirado");
}
}

@Test
void testForLoop() throws InterruptedException, ExecutionException {
try (WorkflowApplication app = WorkflowApplication.builder().build()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class WorkflowContext implements WorkflowContextData {
WorkflowContext(WorkflowDefinition definition, WorkflowMutableInstance instance) {
this.definition = definition;
this.instance = instance;
this.context = definition.application().modelFactory().fromNull();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the actual fix (pretty straightforward)
The rest of the PR is adding unit test for context, both for yaml, which was already working without this fix and for Fluent, which was failing without the fix

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
import io.serverlessworkflow.impl.WorkflowApplication;
import io.serverlessworkflow.impl.WorkflowDefinition;
import io.serverlessworkflow.impl.WorkflowException;
import io.serverlessworkflow.impl.WorkflowInstance;
import io.serverlessworkflow.impl.WorkflowModel;
import io.serverlessworkflow.impl.jackson.JsonUtils;
import java.io.IOException;
import java.time.Instant;
import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletionException;
import java.util.function.Consumer;
import java.util.stream.Stream;
Expand Down Expand Up @@ -76,7 +78,8 @@ private static Stream<Arguments> provideParameters() {
args(
"workflows-samples/for-sum.yaml",
Map.of("input", Arrays.asList(1, 2, 3)),
o -> assertThat(o).isEqualTo(6)),
o -> assertThat(o).isEqualTo(6),
c -> assertThat(c).isEqualTo(Map.of("incr", Arrays.asList(2, 3, 4)))),
args(
"workflows-samples/switch-then-loop.yaml",
Map.of("count", 1),
Expand Down Expand Up @@ -116,16 +119,36 @@ private static Stream<Arguments> provideParameters() {
}

private static Arguments args(
String fileName, Map<String, Object> input, Consumer<Object> instance) {
String fileName,
Map<String, Object> input,
Consumer<Object> modelAssert,
Optional<Consumer<Object>> context) {
return Arguments.of(
fileName,
(Consumer<WorkflowDefinition>)
d ->
instance.accept(
d.instance(input)
.start()
.thenApply(model -> JsonUtils.toJavaValue(JsonUtils.modelToJson(model)))
.join()));
d -> {
WorkflowInstance instance = d.instance(input);
modelAssert.accept(
instance
.start()
.thenApply(model -> JsonUtils.toJavaValue(JsonUtils.modelToJson(model)))
.join());
context.ifPresent(
c -> c.accept(JsonUtils.toJavaValue(JsonUtils.fromValue(instance.context()))));
});
}

private static Arguments args(
String fileName,
Map<String, Object> input,
Consumer<Object> instance,
Consumer<Object> context) {
return args(fileName, input, instance, Optional.of(context));
}

private static Arguments args(
String fileName, Map<String, Object> input, Consumer<Object> instance) {
return args(fileName, input, instance, Optional.empty());
}

private static Arguments argsJson(
Expand Down
3 changes: 3 additions & 0 deletions impl/test/src/test/resources/workflows-samples/for-sum.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ do:
- accumulate:
set:
counter: ${.counter+$number}
export:
as: if .incr==null then {incr:[$number+1]} else .incr+=[$number+1] end
output:
as: .counter