Skip to content

Commit 7f47523

Browse files
committed
Add test to be sure about the convertTaskOutput method
Signed-off-by: Matheus Cruz <matheuscruz.dev@gmail.com>
1 parent da2d5a5 commit 7f47523

2 files changed

Lines changed: 291 additions & 24 deletions

File tree

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

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -292,36 +292,23 @@ public static FuncPredicateEventConfigurer event(String type) {
292292
* <pre>{@code
293293
* function("processData", (Long input) -> input + 5, Long.class),
294294
* function("combineData", (Long enrichedValue) -> enrichedValue, Long.class)
295-
* .inputFrom(enrich((lastState, rootInputModel) -> {
296-
* Long rootInput = rootInputModel.as(Long.class).orElse(0L);
297-
* return lastState + rootInput;
298-
* }, Long.class))
295+
* .inputFrom(
296+
* FuncDSL.enrich((Long lastState, WorkflowModel rootInputModel) -> {
297+
* Long rootInput = rootInputModel.as(Long.class).orElse(0L);
298+
* return lastState + rootInput;
299+
* }),
300+
* Long.class)
299301
* }</pre>
300302
*
301303
* @param fn the enrichment function that receives typed lastState and WorkflowModel rootInput
302-
* @param lastStateClass the class of the last state type
303304
* @param <T> the type of the last state
304305
* @param <R> the type of the enriched result
305306
* @return a JavaContextFunction that can be used with inputFrom
306307
*/
307-
public static <T, R> JavaContextFunction<T, R> enrich(
308-
EnrichWithModelBiFunction<T, R> fn, Class<T> lastStateClass) {
308+
public static <T, R> JavaContextFunction<T, R> enrich(EnrichWithModelBiFunction<T, R> fn) {
309309
return (lastState, workflowContext) -> {
310-
Objects.requireNonNull(lastStateClass, "lastStateClass must not be null");
311310
final WorkflowModel rootInput = workflowContext.instanceData().input();
312-
final WorkflowModel lastStateModel =
313-
workflowContext.definition().application().modelFactory().fromAny(lastState);
314-
final T typedLastState =
315-
lastStateModel
316-
.as(lastStateClass)
317-
.orElseThrow(
318-
() ->
319-
new IllegalArgumentException(
320-
"Cannot convert lastState of type "
321-
+ (lastState != null ? lastState.getClass().getName() : "null")
322-
+ " to "
323-
+ lastStateClass.getName()));
324-
return fn.apply(typedLastState, rootInput);
311+
return fn.apply(lastState, rootInput);
325312
};
326313
}
327314

impl/test/src/test/java/io/serverlessworkflow/impl/test/FuncDSLEnrichWithTest.java

Lines changed: 283 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.enrich;
1919
import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.enrichOutput;
2020
import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.function;
21+
import static org.assertj.core.api.Assertions.within;
2122

2223
import io.serverlessworkflow.api.types.Workflow;
2324
import io.serverlessworkflow.fluent.func.FuncWorkflowBuilder;
@@ -48,13 +49,13 @@ void test_enrich_with_model_in_workflow() {
4849
function("returnEnriched", (Long enrichedValue) -> enrichedValue, Long.class)
4950
.inputFrom(
5051
FuncDSL.enrich(
51-
(lastState, rootInputModel) -> {
52+
(Long lastState, WorkflowModel rootInputModel) -> {
5253
softly.assertThat(lastState).isEqualTo(15L);
5354
Long originalInput = rootInputModel.as(Long.class).orElse(0L);
5455
softly.assertThat(originalInput).isEqualTo(10L);
5556
return lastState + originalInput;
56-
},
57-
Long.class)))
57+
}),
58+
Long.class))
5859
.build();
5960

6061
WorkflowApplication app = WorkflowApplication.builder().build();
@@ -178,4 +179,283 @@ void test_enrich_output_with_input_in_workflow() {
178179

179180
softly.assertAll();
180181
}
182+
183+
@Test
184+
void test_enrichOutput_number_conversion_long_to_integer() {
185+
SoftAssertions softly = new SoftAssertions();
186+
187+
Workflow workflow =
188+
FuncWorkflowBuilder.workflow("numberConversionLongToInteger")
189+
.tasks(
190+
function(
191+
"returnLong",
192+
(Long input) -> {
193+
softly.assertThat(input).isEqualTo(100L);
194+
return input;
195+
},
196+
Long.class)
197+
.outputAs(
198+
enrichOutput(
199+
(taskOutput, rootInputModel) -> {
200+
// taskOutput is Long, convert to Integer
201+
softly.assertThat(taskOutput).isInstanceOf(Integer.class);
202+
softly.assertThat(taskOutput).isEqualTo(100);
203+
return taskOutput * 2;
204+
},
205+
Integer.class)))
206+
.build();
207+
208+
WorkflowApplication app = WorkflowApplication.builder().build();
209+
WorkflowDefinition def = app.workflowDefinition(workflow);
210+
211+
WorkflowModel model = def.instance(100L).start().join();
212+
Number number = model.asNumber().orElseThrow();
213+
214+
softly.assertThat(number.intValue()).isEqualTo(200);
215+
softly.assertAll();
216+
}
217+
218+
@Test
219+
void test_enrichOutput_number_conversion_integer_to_long() {
220+
SoftAssertions softly = new SoftAssertions();
221+
222+
Workflow workflow =
223+
FuncWorkflowBuilder.workflow("numberConversionIntegerToLong")
224+
.tasks(
225+
function(
226+
"returnLong",
227+
(Long input) -> {
228+
softly.assertThat(input).isEqualTo(50L);
229+
return input.intValue();
230+
},
231+
Long.class)
232+
.outputAs(
233+
enrichOutput(
234+
(taskOutput, rootInputModel) -> {
235+
// taskOutput is Integer (from function), convert to Long
236+
softly.assertThat(taskOutput).isInstanceOf(Long.class);
237+
softly.assertThat(taskOutput).isEqualTo(50L);
238+
return taskOutput + 50L;
239+
},
240+
Long.class)))
241+
.build();
242+
243+
WorkflowApplication app = WorkflowApplication.builder().build();
244+
WorkflowDefinition def = app.workflowDefinition(workflow);
245+
246+
WorkflowModel model = def.instance(50L).start().join();
247+
Number number = model.asNumber().orElseThrow();
248+
249+
softly.assertThat(number.longValue()).isEqualTo(100L);
250+
softly.assertAll();
251+
}
252+
253+
@Test
254+
void test_enrichOutput_number_conversion_long_to_double() {
255+
SoftAssertions softly = new SoftAssertions();
256+
257+
Workflow workflow =
258+
FuncWorkflowBuilder.workflow("numberConversionLongToDouble")
259+
.tasks(
260+
function(
261+
"returnLong",
262+
(Long input) -> {
263+
softly.assertThat(input).isEqualTo(100L);
264+
return input;
265+
},
266+
Long.class)
267+
.outputAs(
268+
enrichOutput(
269+
(taskOutput, rootInputModel) -> {
270+
// taskOutput is Long, convert to Double
271+
softly.assertThat(taskOutput).isInstanceOf(Double.class);
272+
softly.assertThat(taskOutput).isEqualTo(100.0);
273+
return taskOutput * 1.5;
274+
},
275+
Double.class)))
276+
.build();
277+
278+
WorkflowApplication app = WorkflowApplication.builder().build();
279+
WorkflowDefinition def = app.workflowDefinition(workflow);
280+
281+
WorkflowModel model = def.instance(100L).start().join();
282+
Number number = model.asNumber().orElseThrow();
283+
284+
softly.assertThat(number.doubleValue()).isEqualTo(150.0);
285+
softly.assertAll();
286+
}
287+
288+
@Test
289+
void test_enrichOutput_number_conversion_double_to_float() {
290+
SoftAssertions softly = new SoftAssertions();
291+
292+
Workflow workflow =
293+
FuncWorkflowBuilder.workflow("numberConversionDoubleToFloat")
294+
.tasks(
295+
function(
296+
"returnLong",
297+
(Long input) -> {
298+
softly.assertThat(input).isEqualTo(300L);
299+
return input.doubleValue();
300+
},
301+
Long.class)
302+
.outputAs(
303+
enrichOutput(
304+
(taskOutput, rootInputModel) -> {
305+
// taskOutput is Double (from function), convert to Float
306+
softly.assertThat(taskOutput).isInstanceOf(Float.class);
307+
softly.assertThat(taskOutput).isEqualTo(300.0f);
308+
return taskOutput / 100.0f;
309+
},
310+
Float.class)))
311+
.build();
312+
313+
WorkflowApplication app = WorkflowApplication.builder().build();
314+
WorkflowDefinition def = app.workflowDefinition(workflow);
315+
316+
WorkflowModel model = def.instance(300L).start().join();
317+
Number number = model.asNumber().orElseThrow();
318+
319+
softly.assertThat(number.floatValue()).isCloseTo(3.0f, within(0.01f));
320+
softly.assertAll();
321+
}
322+
323+
@Test
324+
void test_enrichOutput_number_conversion_integer_to_short() {
325+
SoftAssertions softly = new SoftAssertions();
326+
327+
Workflow workflow =
328+
FuncWorkflowBuilder.workflow("numberConversionIntegerToShort")
329+
.tasks(
330+
function(
331+
"returnLong",
332+
(Long input) -> {
333+
softly.assertThat(input).isEqualTo(100L);
334+
return input.intValue();
335+
},
336+
Long.class)
337+
.outputAs(
338+
enrichOutput(
339+
(taskOutput, rootInputModel) -> {
340+
// taskOutput is Integer (from function), convert to Short
341+
softly.assertThat(taskOutput).isInstanceOf(Short.class);
342+
softly.assertThat(taskOutput).isEqualTo((short) 100);
343+
return (short) (taskOutput + 50);
344+
},
345+
Short.class)))
346+
.build();
347+
348+
WorkflowApplication app = WorkflowApplication.builder().build();
349+
WorkflowDefinition def = app.workflowDefinition(workflow);
350+
351+
WorkflowModel model = def.instance(100L).start().join();
352+
Number number = model.asNumber().orElseThrow();
353+
354+
softly.assertThat(number.shortValue()).isEqualTo((short) 150);
355+
softly.assertAll();
356+
}
357+
358+
@Test
359+
void test_enrichOutput_number_conversion_integer_to_byte() {
360+
SoftAssertions softly = new SoftAssertions();
361+
362+
Workflow workflow =
363+
FuncWorkflowBuilder.workflow("numberConversionIntegerToByte")
364+
.tasks(
365+
function(
366+
"returnLong",
367+
(Long input) -> {
368+
softly.assertThat(input).isEqualTo(50L);
369+
return input.intValue();
370+
},
371+
Long.class)
372+
.outputAs(
373+
enrichOutput(
374+
(taskOutput, rootInputModel) -> {
375+
// taskOutput is Integer (from function), convert to Byte
376+
softly.assertThat(taskOutput).isInstanceOf(Byte.class);
377+
softly.assertThat(taskOutput).isEqualTo((byte) 50);
378+
return (byte) (taskOutput + 10);
379+
},
380+
Byte.class)))
381+
.build();
382+
383+
WorkflowApplication app = WorkflowApplication.builder().build();
384+
WorkflowDefinition def = app.workflowDefinition(workflow);
385+
386+
WorkflowModel model = def.instance(50L).start().join();
387+
Number number = model.asNumber().orElseThrow();
388+
389+
softly.assertThat(number.byteValue()).isEqualTo((byte) 60);
390+
softly.assertAll();
391+
}
392+
393+
@Test
394+
void test_enrichOutput_null_handling() {
395+
SoftAssertions softly = new SoftAssertions();
396+
397+
Workflow workflow =
398+
FuncWorkflowBuilder.workflow("nullHandling")
399+
.tasks(
400+
function(
401+
"returnNull",
402+
(Long input) -> {
403+
softly.assertThat(input).isEqualTo(10L);
404+
return null;
405+
},
406+
Long.class)
407+
.outputAs(
408+
enrichOutput(
409+
(taskOutput, rootInputModel) -> {
410+
// taskOutput should be null
411+
softly.assertThat(taskOutput).isNull();
412+
return 42L;
413+
},
414+
Long.class)))
415+
.build();
416+
417+
WorkflowApplication app = WorkflowApplication.builder().build();
418+
WorkflowDefinition def = app.workflowDefinition(workflow);
419+
420+
WorkflowModel model = def.instance(10L).start().join();
421+
Number number = model.asNumber().orElseThrow();
422+
423+
softly.assertThat(number.longValue()).isEqualTo(42L);
424+
softly.assertAll();
425+
}
426+
427+
@Test
428+
void test_enrichOutput_same_type_no_conversion() {
429+
SoftAssertions softly = new SoftAssertions();
430+
431+
Workflow workflow =
432+
FuncWorkflowBuilder.workflow("sameTypeNoConversion")
433+
.tasks(
434+
function(
435+
"returnLong",
436+
(Long input) -> {
437+
softly.assertThat(input).isEqualTo(100L);
438+
return input;
439+
},
440+
Long.class)
441+
.outputAs(
442+
enrichOutput(
443+
(taskOutput, rootInputModel) -> {
444+
// taskOutput is already Long, no conversion needed
445+
softly.assertThat(taskOutput).isInstanceOf(Long.class);
446+
softly.assertThat(taskOutput).isEqualTo(100L);
447+
return taskOutput * 2;
448+
},
449+
Long.class)))
450+
.build();
451+
452+
WorkflowApplication app = WorkflowApplication.builder().build();
453+
WorkflowDefinition def = app.workflowDefinition(workflow);
454+
455+
WorkflowModel model = def.instance(100L).start().join();
456+
Number number = model.asNumber().orElseThrow();
457+
458+
softly.assertThat(number.longValue()).isEqualTo(200L);
459+
softly.assertAll();
460+
}
181461
}

0 commit comments

Comments
 (0)