|
| 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.executors.func; |
| 17 | + |
| 18 | +import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.function; |
| 19 | +import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.input; |
| 20 | +import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.output; |
| 21 | + |
| 22 | +import io.serverlessworkflow.api.types.Workflow; |
| 23 | +import io.serverlessworkflow.fluent.func.FuncWorkflowBuilder; |
| 24 | +import io.serverlessworkflow.impl.WorkflowApplication; |
| 25 | +import io.serverlessworkflow.impl.WorkflowDefinition; |
| 26 | +import io.serverlessworkflow.impl.WorkflowModel; |
| 27 | +import org.assertj.core.api.SoftAssertions; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | + |
| 30 | +public class FuncDSLDataFlowTransformationHelpersTest { |
| 31 | + |
| 32 | + @Test |
| 33 | + void test_input_with_inputFrom() { |
| 34 | + |
| 35 | + SoftAssertions softly = new SoftAssertions(); |
| 36 | + |
| 37 | + Workflow workflow = |
| 38 | + FuncWorkflowBuilder.workflow("reviewSubmissionWithModel") |
| 39 | + .tasks( |
| 40 | + function( |
| 41 | + "add5", |
| 42 | + (Long input) -> { |
| 43 | + softly.assertThat(input).isEqualTo(10L); |
| 44 | + return input + 5; |
| 45 | + }, |
| 46 | + Long.class), |
| 47 | + function("returnEnriched", (Long enrichedValue) -> enrichedValue, Long.class) |
| 48 | + .inputFrom( |
| 49 | + (object, workflowContext) -> { |
| 50 | + softly.assertThat(object).isEqualTo(15L); |
| 51 | + Long input = input(workflowContext, Long.class); |
| 52 | + softly.assertThat(input).isEqualTo(10L); |
| 53 | + return object + input; |
| 54 | + }, |
| 55 | + Long.class)) |
| 56 | + .build(); |
| 57 | + |
| 58 | + try (WorkflowApplication app = WorkflowApplication.builder().build()) { |
| 59 | + WorkflowDefinition def = app.workflowDefinition(workflow); |
| 60 | + WorkflowModel model = def.instance(10L).start().join(); |
| 61 | + Number number = model.asNumber().orElseThrow(); |
| 62 | + softly.assertThat(number.longValue()).isEqualTo(25L); |
| 63 | + } |
| 64 | + |
| 65 | + softly.assertAll(); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void test_input_with_outputAs() { |
| 70 | + |
| 71 | + SoftAssertions softly = new SoftAssertions(); |
| 72 | + |
| 73 | + Workflow workflow = |
| 74 | + FuncWorkflowBuilder.workflow("enrichOutputWithModelTest") |
| 75 | + .tasks( |
| 76 | + function( |
| 77 | + "add5", |
| 78 | + (Long input) -> { |
| 79 | + softly.assertThat(input).isEqualTo(10L); |
| 80 | + return input + 5; |
| 81 | + }, |
| 82 | + Long.class) |
| 83 | + .outputAs( |
| 84 | + (object, workflowContext, taskContextData) -> { |
| 85 | + softly.assertThat(object).isEqualTo(15L); |
| 86 | + Long input = input(workflowContext, Long.class); |
| 87 | + softly.assertThat(input).isEqualTo(10L); |
| 88 | + return input + object; |
| 89 | + }, |
| 90 | + Long.class)) |
| 91 | + .build(); |
| 92 | + |
| 93 | + try (WorkflowApplication app = WorkflowApplication.builder().build()) { |
| 94 | + WorkflowDefinition def = app.workflowDefinition(workflow); |
| 95 | + |
| 96 | + WorkflowModel model = def.instance(10L).start().join(); |
| 97 | + Number number = model.asNumber().orElseThrow(); |
| 98 | + |
| 99 | + softly.assertThat(number.longValue()).isEqualTo(25L); |
| 100 | + } |
| 101 | + |
| 102 | + softly.assertAll(); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void test_output_with_exportAs() { |
| 107 | + |
| 108 | + SoftAssertions softly = new SoftAssertions(); |
| 109 | + |
| 110 | + Workflow workflow = |
| 111 | + FuncWorkflowBuilder.workflow("enrichOutputWithInputTest") |
| 112 | + .tasks( |
| 113 | + function( |
| 114 | + "add5", |
| 115 | + (Long input) -> { |
| 116 | + softly.assertThat(input).isEqualTo(10L); |
| 117 | + return input + 5; |
| 118 | + }, |
| 119 | + Long.class) |
| 120 | + .exportAs( |
| 121 | + (object, workflowContext, taskContextData) -> { |
| 122 | + Long taskOutput = output(taskContextData, Long.class); |
| 123 | + softly.assertThat(taskOutput).isEqualTo(15L); |
| 124 | + return taskOutput * 2; |
| 125 | + })) |
| 126 | + .build(); |
| 127 | + |
| 128 | + try (WorkflowApplication app = WorkflowApplication.builder().build()) { |
| 129 | + WorkflowDefinition def = app.workflowDefinition(workflow); |
| 130 | + WorkflowModel model = def.instance(10L).start().join(); |
| 131 | + Number number = model.asNumber().orElseThrow(); |
| 132 | + softly.assertThat(number.longValue()).isEqualTo(15L); |
| 133 | + } |
| 134 | + |
| 135 | + softly.assertAll(); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + void test_input_with_inputFrom_fluent_way() { |
| 140 | + SoftAssertions softly = new SoftAssertions(); |
| 141 | + |
| 142 | + Workflow workflow = |
| 143 | + FuncWorkflowBuilder.workflow("enrichOutputWithInputTest") |
| 144 | + .tasks( |
| 145 | + function("sumFive", (Long input) -> input + 5, Long.class) |
| 146 | + .inputFrom( |
| 147 | + (object, workflowContext, taskContextData) -> |
| 148 | + input(taskContextData, Long.class) * 2)) |
| 149 | + .build(); |
| 150 | + |
| 151 | + try (WorkflowApplication app = WorkflowApplication.builder().build()) { |
| 152 | + WorkflowDefinition def = app.workflowDefinition(workflow); |
| 153 | + WorkflowModel model = def.instance(10L).start().join(); |
| 154 | + Number number = model.asNumber().orElseThrow(); |
| 155 | + |
| 156 | + softly.assertThat(number.longValue()).isEqualTo(25L); |
| 157 | + } |
| 158 | + |
| 159 | + softly.assertAll(); |
| 160 | + } |
| 161 | +} |
0 commit comments