|
| 1 | +package io.quarkiverse.flow.it; |
| 2 | + |
| 3 | + |
| 4 | +import io.quarkiverse.flow.Flow; |
| 5 | +import io.quarkus.test.junit.QuarkusTest; |
| 6 | +import io.serverlessworkflow.api.types.Workflow; |
| 7 | +import io.serverlessworkflow.fluent.func.FuncWorkflowBuilder; |
| 8 | +import io.serverlessworkflow.impl.WorkflowContextData; |
| 9 | +import io.serverlessworkflow.impl.WorkflowModel; |
| 10 | +import io.smallrye.mutiny.Uni; |
| 11 | +import jakarta.enterprise.context.ApplicationScoped; |
| 12 | +import jakarta.inject.Inject; |
| 13 | +import org.junit.jupiter.api.Assertions; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +import java.net.URI; |
| 17 | +import java.util.Objects; |
| 18 | +import java.util.function.Function; |
| 19 | + |
| 20 | +import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.function; |
| 21 | +import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.http; |
| 22 | + |
| 23 | +@QuarkusTest |
| 24 | +public class Call4PapersTest { |
| 25 | + |
| 26 | + @Inject |
| 27 | + Call4PapersFlow flow; |
| 28 | + |
| 29 | + @Test |
| 30 | + void should_execute_correctly() { |
| 31 | + var submission = new Call4PapersFlow.ProposalSubmission( |
| 32 | + "Reactive Workflows with Quarkus", |
| 33 | + "This paper explores reactive workflow patterns...", |
| 34 | + "Jane Developer" |
| 35 | + ); |
| 36 | + WorkflowModel workflowModel = flow.startInstance(submission).await().indefinitely(); |
| 37 | + |
| 38 | + Assertions.assertNotNull(workflowModel); |
| 39 | + } |
| 40 | + |
| 41 | + @ApplicationScoped |
| 42 | + public static class Call4PapersFlow extends Flow { |
| 43 | + |
| 44 | + @Override |
| 45 | + public Workflow descriptor() { |
| 46 | + return FuncWorkflowBuilder.workflow("call4papers") |
| 47 | + .tasks( |
| 48 | + // Step 1: Validate proposal with inputFrom transformation |
| 49 | + function("validateProposal", (Proposal input) -> { |
| 50 | + System.out.println("Validating proposal: " + input.title()); |
| 51 | + return input; |
| 52 | + }, Proposal.class) |
| 53 | + .inputFrom((ProposalSubmission submission) -> new Proposal( |
| 54 | + submission.title(), |
| 55 | + submission.proposal(), // Maps to abstractText |
| 56 | + submission.author()), ProposalSubmission.class), |
| 57 | + |
| 58 | + // Step 2: Score proposal with outputAs transformation |
| 59 | + function("scoreProposal", (Proposal input) -> { |
| 60 | + Integer score = calculateScore(input.abstractText()); |
| 61 | + System.out.println("Score calculated having the result as: " + score); |
| 62 | + return score; |
| 63 | + }, Proposal.class) |
| 64 | + .outputAs((Long score) -> { |
| 65 | + return new ProposalScore(score, score >= 7); |
| 66 | + }, Long.class), |
| 67 | + |
| 68 | + // Step 3: Prepare notification with exportAs using workflow context |
| 69 | + function("prepareNotification", proposalScore -> new ProposalScore(proposalScore.score, proposalScore.accepted), ProposalScore.class) |
| 70 | + .exportAs((ProposalScore proposalScore, WorkflowContextData workflowContext) -> { |
| 71 | + // Access original workflow input to get title and author |
| 72 | + WorkflowModel originalInput = workflowContext.instanceData().input(); |
| 73 | + ProposalSubmission submission = originalInput.as(ProposalSubmission.class).orElseThrow(); |
| 74 | + |
| 75 | + // Create enriched payload combining original input with score |
| 76 | + return new NotificationPayload( |
| 77 | + submission.title(), |
| 78 | + submission.author(), |
| 79 | + proposalScore.score(), |
| 80 | + proposalScore.accepted()); |
| 81 | + }, ProposalScore.class).inputFrom(Function.identity(), ProposalScore.class), |
| 82 | + |
| 83 | + // Step 4: Send notification via HTTP |
| 84 | + http("sendNotification") |
| 85 | + .POST() |
| 86 | + .body("${.}") // Uses the NotificationPayload from exportAs |
| 87 | + .header("Content-Type", "application/json") |
| 88 | + .uri(URI.create("http://localhost:9999/notifications"))) |
| 89 | + .build(); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Calculate a score for the proposal based on its abstract. |
| 94 | + * In a real implementation, this might use NLP, keyword analysis, etc. |
| 95 | + */ |
| 96 | + private Integer calculateScore(String abstractText) { |
| 97 | + // Simple scoring: longer abstracts get higher scores |
| 98 | + int length = abstractText.length(); |
| 99 | + if (length > 500) |
| 100 | + return 9; |
| 101 | + if (length > 300) |
| 102 | + return 7; |
| 103 | + if (length > 150) |
| 104 | + return 5; |
| 105 | + return 3; |
| 106 | + } |
| 107 | + |
| 108 | + // Data types representing different stages of the workflow |
| 109 | + |
| 110 | + /** |
| 111 | + * External DTO received from the API |
| 112 | + */ |
| 113 | + public record ProposalSubmission(String title, String proposal, String author) { |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Internal domain model used for processing |
| 118 | + */ |
| 119 | + public record Proposal(String title, String abstractText, String author) { |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Scoring result persisted in workflow data |
| 124 | + */ |
| 125 | + public record ProposalScore(long score, boolean accepted) { |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Final notification payload enriched with data from multiple sources |
| 130 | + */ |
| 131 | + public record NotificationPayload(String title, String author, long score, boolean accepted) { |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments