Skip to content
Open
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 @@ -25,6 +25,7 @@
import io.serverlessworkflow.impl.WorkflowApplication;
import io.serverlessworkflow.impl.WorkflowContextData;
import io.serverlessworkflow.impl.WorkflowDefinition;
import io.serverlessworkflow.impl.WorkflowInstance;
import io.serverlessworkflow.impl.WorkflowModel;
import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -160,4 +161,82 @@ void test_input_with_inputFrom_fluent_way() {

softly.assertAll();
}

@Test
void test_output_with_outputAs() {

SoftAssertions softly = new SoftAssertions();

Workflow workflow =
FuncWorkflowBuilder.workflow("enrichOutputWithTaskOutputTest")
.tasks(
function(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hi @matheusandre1, it is close. You need to test what you added here. Instead use the enrichOutputWithTaskTest workflow use that one you added on testDoTaskRunWorkflow test:

    Workflow wf =
        WorkflowBuilder.workflow("parentFlow")
            .tasks(
                d ->
                    d.workflow(
                        "runChild",
                        w ->
                            w.namespace("org.acme")
                                .name("childFlow")
                                .version("1.0.0")
                                .input(Map.of("id", 42, "region", "us-east"))
                                .await(false)
                                .returnType(RunTaskConfiguration.ProcessReturnType.NONE)))
            .build();

"add5",
(Long input) -> {
softly.assertThat(input).isEqualTo(10L);
return input + 5;
},
Long.class)
.outputAs(
(object, workflowContext, taskContextData) -> {
Long taskOutput = output(taskContextData, Long.class);
softly.assertThat(taskOutput).isEqualTo(15L);
Long input = input(workflowContext, Long.class);
softly.assertThat(input).isEqualTo(10L);
return input + taskOutput;
},
Long.class))
.build();

try (WorkflowApplication app = WorkflowApplication.builder().build()) {
WorkflowDefinition def = app.workflowDefinition(workflow);

WorkflowModel model = def.instance(10L).start().join();
Number number = model.asNumber().orElseThrow();

softly.assertThat(number.longValue()).isEqualTo(25L);
}

softly.assertAll();
}

@Test
void test_input_with_exportAs() {

SoftAssertions softly = new SoftAssertions();

Workflow workflow =
FuncWorkflowBuilder.workflow("enrichExportWithInputTest")
.tasks(
function(
"add5",
(Long input) -> {
softly.assertThat(input).isEqualTo(10L);
return input + 5;
},
Long.class)
.exportAs(
(Long object,
WorkflowContextData workflowContext,
TaskContextData taskContextData) -> {
Long taskOutput = output(taskContextData, Long.class);
softly.assertThat(taskOutput).isEqualTo(15L);
Long input = input(workflowContext, Long.class);
softly.assertThat(input).isEqualTo(10L);
return input + taskOutput;
}))
.build();

try (WorkflowApplication app = WorkflowApplication.builder().build()) {
WorkflowDefinition def = app.workflowDefinition(workflow);

WorkflowInstance instance = def.instance(10L);
instance.start().join();
Number number = instance.context().asNumber().orElseThrow();

softly.assertThat(number.longValue()).isEqualTo(25L);
}

softly.assertAll();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public abstract class BaseTaskItemListBuilder<SELF extends BaseTaskItemListBuild
protected final String TYPE_TRY = "try";
protected final String TYPE_HTTP = "http";
protected final String TYPE_OPENAPI = "openapi";
protected final String TYPE_WORKFLOW = "workflow";

private final List<TaskItem> list;
private final int offset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,10 @@ public DoTaskBuilder openapi(String name, Consumer<CallOpenAPITaskBuilder> items
this.listBuilder().openapi(name, itemsConfigurer);
return this;
}

@Override
public DoTaskBuilder workflow(String name, Consumer<WorkflowTaskBuilder> itemsConfigurer) {
this.listBuilder().workflow(name, itemsConfigurer);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,17 @@ public TaskItemListBuilder openapi(

return addTaskItem(new TaskItem(name, task));
}

@Override
public TaskItemListBuilder workflow(String name, Consumer<WorkflowTaskBuilder> itemsConfigurer) {
name = defaultNameAndRequireConfig(name, itemsConfigurer, TYPE_WORKFLOW);

final WorkflowTaskBuilder workflowTaskBuilder = new WorkflowTaskBuilder();
itemsConfigurer.accept(workflowTaskBuilder);

final Task task = new Task();
task.setRunTask(workflowTaskBuilder.build());

return addTaskItem(new TaskItem(name, task));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.serverlessworkflow.fluent.spec;

import io.serverlessworkflow.api.types.RunTask;
import io.serverlessworkflow.api.types.RunTaskConfigurationUnion;
import io.serverlessworkflow.api.types.RunWorkflow;
import io.serverlessworkflow.api.types.SubflowConfiguration;
import io.serverlessworkflow.fluent.spec.spi.WorkflowTaskFluent;

public class WorkflowTaskBuilder extends TaskBaseBuilder<WorkflowTaskBuilder>
implements WorkflowTaskFluent<WorkflowTaskBuilder> {

private final RunTask task;
private final RunWorkflow configuration;
private final SubflowConfiguration workflow;

WorkflowTaskBuilder() {
this.task = new RunTask();
this.configuration = new RunWorkflow();
this.workflow = new SubflowConfiguration();
this.configuration.setWorkflow(this.workflow);
this.task.setRun(new RunTaskConfigurationUnion().withRunWorkflow(this.configuration));
this.setTask(task);
}

@Override
public WorkflowTaskBuilder self() {
return this;
}

public RunWorkflow config() {
return this.configuration;
}

public SubflowConfiguration workflow() {
return this.workflow;
}

public RunTask build() {
return this.task;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.serverlessworkflow.fluent.spec.configurers;

import io.serverlessworkflow.fluent.spec.WorkflowTaskBuilder;
import java.util.function.Consumer;

@FunctionalInterface
public interface WorkflowConfigurer extends Consumer<WorkflowTaskBuilder> {}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import io.serverlessworkflow.fluent.spec.configurers.TasksConfigurer;
import io.serverlessworkflow.fluent.spec.configurers.TryCatchConfigurer;
import io.serverlessworkflow.fluent.spec.configurers.TryConfigurer;
import io.serverlessworkflow.fluent.spec.configurers.WorkflowConfigurer;
import io.serverlessworkflow.types.Errors;
import java.net.URI;
import java.util.List;
Expand Down Expand Up @@ -101,6 +102,18 @@ public static CallOpenAPISpec openapi() {
return new CallOpenAPISpec();
}

public static WorkflowSpec workflow(String namespace, String name, String version) {
return new WorkflowSpec().namespace(namespace).name(name).version(version);
}

public static WorkflowSpec workflow(String namespace, String name) {
return new WorkflowSpec().namespace(namespace).name(name);
}

public static WorkflowSpec workflow() {
return new WorkflowSpec();
}

/**
* Convenience for defining a {@code use} block with a single secret.
*
Expand Down Expand Up @@ -636,6 +649,10 @@ public static TasksConfigurer call(CallOpenAPIConfigurer configurer) {
return list -> list.openapi(configurer);
}

public static TasksConfigurer workflow(WorkflowConfigurer configurer) {
return list -> list.workflow(configurer);
}

/**
* Create a {@link TasksConfigurer} that adds an OpenAPI call task with an explicit name.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.serverlessworkflow.fluent.spec.dsl;

import io.serverlessworkflow.api.types.RunTaskConfiguration;
import io.serverlessworkflow.fluent.spec.WorkflowTaskBuilder;
import io.serverlessworkflow.fluent.spec.configurers.WorkflowConfigurer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

public final class WorkflowSpec implements WorkflowConfigurer {

private final List<Consumer<WorkflowTaskBuilder>> steps = new ArrayList<>();

public WorkflowSpec namespace(String namespace) {
steps.add(b -> b.namespace(namespace));
return this;
}

public WorkflowSpec name(String name) {
steps.add(b -> b.name(name));
return this;
}

public WorkflowSpec version(String version) {
steps.add(b -> b.version(version));
return this;
}

public WorkflowSpec input(Map<String, Object> input) {
steps.add(b -> b.input(input));
return this;
}

public WorkflowSpec input(String key, Object value) {
steps.add(b -> b.input(key, value));
return this;
}

public WorkflowSpec await(boolean await) {
steps.add(b -> b.await(await));
return this;
}

public WorkflowSpec returnType(RunTaskConfiguration.ProcessReturnType returnType) {
steps.add(b -> b.returnType(returnType));
return this;
}

@Override
public void accept(WorkflowTaskBuilder builder) {
for (var s : steps) {
s.accept(builder);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.serverlessworkflow.fluent.spec.SwitchTaskBuilder;
import io.serverlessworkflow.fluent.spec.TaskItemListBuilder;
import io.serverlessworkflow.fluent.spec.TryTaskBuilder;
import io.serverlessworkflow.fluent.spec.WorkflowTaskBuilder;

/**
* Documents the exposed fluent `do` DSL.
Expand All @@ -44,4 +45,5 @@ public interface DoFluent<T>
ForkFluent<ForkTaskBuilder, T>,
ListenFluent<ListenTaskBuilder, T>,
RaiseFluent<RaiseTaskBuilder, T>,
CallOpenAPIFluent<CallOpenAPITaskBuilder, T> {}
CallOpenAPIFluent<CallOpenAPITaskBuilder, T>,
WorkflowFluent<WorkflowTaskBuilder, T> {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.serverlessworkflow.fluent.spec.spi;

import io.serverlessworkflow.fluent.spec.TaskBaseBuilder;
import java.util.UUID;
import java.util.function.Consumer;

public interface WorkflowFluent<SELF extends TaskBaseBuilder<SELF>, LIST> {

LIST workflow(String name, Consumer<SELF> itemsConfigurer);

default LIST workflow(Consumer<SELF> itemsConfigurer) {
return this.workflow(UUID.randomUUID().toString(), itemsConfigurer);
}
}
Loading
Loading