-
Notifications
You must be signed in to change notification settings - Fork 52
Fix #1275 - Improve CloudEvent and CloudEventData handling #1282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
90d4367
Fix workflow test
ricardozanini 28754a0
Enable any type of collection cast on *ModelCollection
ricardozanini 4e7a946
Add tests for jacksonModelFactory
ricardozanini d73f3d7
Incorporate review comments
ricardozanini 281101d
Fix sync block
ricardozanini 3388954
Isolating as method on utils; refactor ObjectMapperFactoryProvider to…
ricardozanini 57ac21f
Adding missing check
ricardozanini File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
...l/lambda/src/test/java/io/serverless/workflow/impl/executors/func/EventFilteringTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| /* | ||
| * 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.serverless.workflow.impl.executors.func; | ||
|
|
||
| import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.consume; | ||
| import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.consumed; | ||
| import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.emitJson; | ||
| import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.function; | ||
| import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.listen; | ||
| import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.switchWhenOrElse; | ||
| import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.to; | ||
| import static org.awaitility.Awaitility.await; | ||
|
|
||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import io.cloudevents.CloudEvent; | ||
| import io.cloudevents.CloudEventData; | ||
| import io.cloudevents.core.builder.CloudEventBuilder; | ||
| import io.serverlessworkflow.api.types.Workflow; | ||
| import io.serverlessworkflow.fluent.func.FuncWorkflowBuilder; | ||
| import io.serverlessworkflow.impl.WorkflowApplication; | ||
| import io.serverlessworkflow.impl.WorkflowDefinition; | ||
| import io.serverlessworkflow.impl.WorkflowInstance; | ||
| import io.serverlessworkflow.impl.WorkflowModel; | ||
| import io.serverlessworkflow.impl.WorkflowStatus; | ||
| import io.serverlessworkflow.impl.events.EventPublisher; | ||
| import java.net.URI; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.time.Duration; | ||
| import java.util.List; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class EventFilteringTest { | ||
|
|
||
| private static final ObjectMapper MAPPER = new ObjectMapper(); | ||
|
|
||
| // --- Mock Service Methods (replacing Quarkus Agents) --- | ||
| public NewsletterDraft writeDraft(NewsletterRequest req) { | ||
| return new NewsletterDraft("Draft: " + req.topic(), "Initial body..."); | ||
| } | ||
|
|
||
| public NewsletterDraft editDraft(HumanReview review) { | ||
| return new NewsletterDraft("Edited Draft", "Fixed based on: " + review.notes()); | ||
| } | ||
|
|
||
| public void sendEmail(NewsletterDraft draft) { | ||
| // Simulates MailService.send | ||
| } | ||
|
|
||
| @Test | ||
| public void testIntelligentNewsletterApprovalPath() throws Exception { | ||
| try (WorkflowApplication app = WorkflowApplication.builder().build()) { | ||
|
|
||
| Workflow workflow = | ||
| FuncWorkflowBuilder.workflow("intelligent-newsletter") | ||
| .tasks( | ||
| function("draftAgent", this::writeDraft).exportAsTaskOutput(), | ||
| emitJson("draftReady", "org.acme.email.review.required", NewsletterDraft.class), | ||
| listen( | ||
| "waitHumanReview", | ||
| to().one( | ||
| consumed("org.acme.newsletter.review.done") | ||
| .extensionByInstanceId("instanceid"))) | ||
| .outputAs( | ||
| (List<CloudEventData> events) -> { | ||
| try { | ||
| return MAPPER.readValue(events.get(0).toBytes(), HumanReview.class); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException("Failed to deserialize HumanReview", e); | ||
| } | ||
| }), | ||
| switchWhenOrElse( | ||
| h -> HumanReview.NEEDS_REVISION.equals(h.status()), | ||
| "humanEditorAgent", | ||
| "sendNewsletter", | ||
| HumanReview.class), | ||
| function("humanEditorAgent", this::editDraft) | ||
| .exportAsTaskOutput() | ||
| .then("draftReady"), | ||
| consume("sendNewsletter", this::sendEmail) | ||
| .inputFrom( | ||
| (payload, wfc, tfc) -> | ||
| payload instanceof HumanReview ? wfc.context() : payload)) | ||
| .build(); | ||
|
|
||
| WorkflowDefinition definition = app.workflowDefinition(workflow); | ||
| WorkflowInstance instance = definition.instance(new NewsletterRequest("Tech Stocks")); | ||
| CompletableFuture<WorkflowModel> future = instance.start(); | ||
|
|
||
| // Wait for it to hit the listen state | ||
| await() | ||
| .atMost(Duration.ofSeconds(5)) | ||
| .until(() -> instance.status() == WorkflowStatus.WAITING); | ||
|
|
||
| EventPublisher publisher = app.eventPublishers().iterator().next(); | ||
|
|
||
| // --- THE NEGATIVE TEST: Fire an event with the WRONG instance id --- | ||
| CloudEvent maliciousEvent = | ||
| CloudEventBuilder.v1() | ||
| .withId("event-wrong") | ||
| .withSource(URI.create("test:/human-editor")) | ||
| .withType("org.acme.newsletter.review.done") | ||
| .withExtension("instanceid", "SOME-OTHER-ID-12345") // Does not match instance.id() | ||
| .withData( | ||
| "application/json", | ||
| "{\"status\":\"APPROVED\", \"notes\":\"Malicious approval\"}" | ||
| .getBytes(StandardCharsets.UTF_8)) | ||
| .build(); | ||
|
|
||
| publisher.publish(maliciousEvent).toCompletableFuture().join(); | ||
|
|
||
| await() | ||
| .pollDelay(Duration.ofMillis(250)) | ||
| .atMost(Duration.ofMillis(500)) | ||
| .until(() -> instance.status() == WorkflowStatus.WAITING && !future.isDone()); | ||
|
|
||
| // --- THE POSITIVE TEST: Fire the CORRECT event --- | ||
| CloudEvent humanReviewEvent = | ||
| CloudEventBuilder.v1() | ||
| .withId("event-123") | ||
| .withSource(URI.create("test:/human-editor")) | ||
| .withType("org.acme.newsletter.review.done") | ||
| .withExtension("instanceid", instance.id()) // Matches exactly | ||
| .withData( | ||
| "application/json", | ||
| "{\"status\":\"APPROVED\", \"notes\":\"Looks good\"}" | ||
| .getBytes(StandardCharsets.UTF_8)) | ||
| .build(); | ||
|
|
||
| publisher.publish(humanReviewEvent).toCompletableFuture().join(); | ||
|
|
||
| // Assert successful completion | ||
| await() | ||
| .atMost(Duration.ofSeconds(5)) | ||
| .until(() -> instance.status() == WorkflowStatus.COMPLETED); | ||
| } | ||
| } | ||
|
|
||
| // --- Mock Domain Models --- | ||
| public record NewsletterRequest(String topic) {} | ||
|
|
||
| public record NewsletterDraft(String title, String body) {} | ||
|
|
||
| public record HumanReview(String status, String notes) { | ||
| public static final String NEEDS_REVISION = "NEEDS_REVISION"; | ||
| public static final String APPROVED = "APPROVED"; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,43 +1,54 @@ | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <parent> | ||
| <groupId>io.serverlessworkflow</groupId> | ||
| <artifactId>serverlessworkflow-experimental</artifactId> | ||
| <version>8.0.0-SNAPSHOT</version> | ||
| </parent> | ||
| <artifactId>serverlessworkflow-experimental-test</artifactId> | ||
| <name>Serverless Workflow :: Experimental :: Test</name> | ||
| <dependencies> | ||
| <dependency> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <parent> | ||
| <groupId>io.serverlessworkflow</groupId> | ||
| <artifactId>serverlessworkflow-experimental</artifactId> | ||
| <version>8.0.0-SNAPSHOT</version> | ||
| </parent> | ||
| <artifactId>serverlessworkflow-experimental-test</artifactId> | ||
| <name>Serverless Workflow :: Experimental :: Test</name> | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.junit.jupiter</groupId> | ||
| <artifactId>junit-jupiter-engine</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.mockito</groupId> | ||
| <artifactId>mockito-core</artifactId> | ||
| <scope>test</scope> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <dependency> | ||
| <groupId>org.assertj</groupId> | ||
| <artifactId>assertj-core</artifactId> | ||
| <scope>test</scope> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>io.serverlessworkflow</groupId> | ||
| <artifactId>serverlessworkflow-experimental-fluent-func</artifactId> | ||
| <scope>test</scope> | ||
| <groupId>org.awaitility</groupId> | ||
| <artifactId>awaitility</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>io.serverlessworkflow</groupId> | ||
| <artifactId>serverlessworkflow-experimental-lambda</artifactId> | ||
| <scope>test</scope> | ||
| <dependency> | ||
| <groupId>ch.qos.logback</groupId> | ||
| <artifactId>logback-classic</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>io.serverlessworkflow</groupId> | ||
| <artifactId>serverlessworkflow-impl-model</artifactId> | ||
| <version>${project.version}</version> | ||
| <scope>test</scope> | ||
| <dependency> | ||
| <groupId>io.serverlessworkflow</groupId> | ||
| <artifactId>serverlessworkflow-experimental-fluent-func</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>io.serverlessworkflow</groupId> | ||
| <artifactId>serverlessworkflow-experimental-lambda</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>io.serverlessworkflow</groupId> | ||
| <artifactId>serverlessworkflow-impl-model</artifactId> | ||
| <version>${project.version}</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
| </dependencies> | ||
| </project> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.