|
18 | 18 | import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.enrich; |
19 | 19 | import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.enrichOutput; |
20 | 20 | import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.function; |
| 21 | +import static org.assertj.core.api.Assertions.within; |
21 | 22 |
|
22 | 23 | import io.serverlessworkflow.api.types.Workflow; |
23 | 24 | import io.serverlessworkflow.fluent.func.FuncWorkflowBuilder; |
@@ -48,13 +49,13 @@ void test_enrich_with_model_in_workflow() { |
48 | 49 | function("returnEnriched", (Long enrichedValue) -> enrichedValue, Long.class) |
49 | 50 | .inputFrom( |
50 | 51 | FuncDSL.enrich( |
51 | | - (lastState, rootInputModel) -> { |
| 52 | + (Long lastState, WorkflowModel rootInputModel) -> { |
52 | 53 | softly.assertThat(lastState).isEqualTo(15L); |
53 | 54 | Long originalInput = rootInputModel.as(Long.class).orElse(0L); |
54 | 55 | softly.assertThat(originalInput).isEqualTo(10L); |
55 | 56 | return lastState + originalInput; |
56 | | - }, |
57 | | - Long.class))) |
| 57 | + }), |
| 58 | + Long.class)) |
58 | 59 | .build(); |
59 | 60 |
|
60 | 61 | WorkflowApplication app = WorkflowApplication.builder().build(); |
@@ -178,4 +179,283 @@ void test_enrich_output_with_input_in_workflow() { |
178 | 179 |
|
179 | 180 | softly.assertAll(); |
180 | 181 | } |
| 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 | + } |
181 | 461 | } |
0 commit comments