This is an automated email from the ASF dual-hosted git repository.

zhfeng pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git


The following commit(s) were added to refs/heads/main by this push:
     new 212e996be9 Fix #573 to add manual saga tests (#5714)
212e996be9 is described below

commit 212e996be9b49dfaac451d76d7a2b5421178f22c
Author: Zheng Feng <zh.f...@gmail.com>
AuthorDate: Sat Feb 3 00:37:39 2024 +0800

    Fix #573 to add manual saga tests (#5714)
---
 integration-tests/lra/pom.xml                      | 17 +++++++++++++
 .../quarkus/component/lra/it/LraResource.java      | 25 +++++++++++++++++++
 .../camel/quarkus/component/lra/it/LraRoutes.java  | 28 ++++++++++++++++++++++
 .../camel/quarkus/component/lra/it/LraTest.java    | 17 +++++++++++++
 4 files changed, 87 insertions(+)

diff --git a/integration-tests/lra/pom.xml b/integration-tests/lra/pom.xml
index f1bb77d4fd..1f26196b9b 100644
--- a/integration-tests/lra/pom.xml
+++ b/integration-tests/lra/pom.xml
@@ -47,6 +47,10 @@
             <groupId>org.apache.camel.quarkus</groupId>
             <artifactId>camel-quarkus-lra</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.apache.camel.quarkus</groupId>
+            <artifactId>camel-quarkus-mock</artifactId>
+        </dependency>
         <dependency>
             <groupId>io.quarkus</groupId>
             <artifactId>quarkus-resteasy</artifactId>
@@ -180,6 +184,19 @@
                         </exclusion>
                     </exclusions>
                 </dependency>
+                <dependency>
+                    <groupId>org.apache.camel.quarkus</groupId>
+                    <artifactId>camel-quarkus-mock-deployment</artifactId>
+                    <version>${project.version}</version>
+                    <type>pom</type>
+                    <scope>test</scope>
+                    <exclusions>
+                        <exclusion>
+                            <groupId>*</groupId>
+                            <artifactId>*</artifactId>
+                        </exclusion>
+                    </exclusions>
+                </dependency>
             </dependencies>
         </profile>
         <profile>
diff --git 
a/integration-tests/lra/src/main/java/org/apache/camel/quarkus/component/lra/it/LraResource.java
 
b/integration-tests/lra/src/main/java/org/apache/camel/quarkus/component/lra/it/LraResource.java
index 34539f08e0..45089dceeb 100644
--- 
a/integration-tests/lra/src/main/java/org/apache/camel/quarkus/component/lra/it/LraResource.java
+++ 
b/integration-tests/lra/src/main/java/org/apache/camel/quarkus/component/lra/it/LraResource.java
@@ -27,7 +27,9 @@ import jakarta.ws.rs.Produces;
 import jakarta.ws.rs.QueryParam;
 import jakarta.ws.rs.core.MediaType;
 import jakarta.ws.rs.core.Response;
+import org.apache.camel.CamelContext;
 import org.apache.camel.FluentProducerTemplate;
+import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.quarkus.component.lra.it.service.CreditService;
 import org.apache.camel.quarkus.component.lra.it.service.OrderManagerService;
 
@@ -38,6 +40,9 @@ public class LraResource {
     @Inject
     FluentProducerTemplate producerTemplate;
 
+    @Inject
+    CamelContext context;
+
     @Inject
     CreditService creditService;
 
@@ -73,4 +78,24 @@ public class LraResource {
     public int getAvailableCredit() {
         return creditService.getCredit();
     }
+
+    @Path("/manual")
+    @POST
+    @Produces(MediaType.TEXT_PLAIN)
+    public String manualSaga(@QueryParam("id") String id, String body) throws 
Exception {
+        String name = body.equals("fail") || body.equals("timeout") ? 
"compensate" : "complete";
+        MockEndpoint mockEndpoint = context.getEndpoint("mock:" + name, 
MockEndpoint.class);
+        mockEndpoint.reset();
+        mockEndpoint.expectedMessageCount(1);
+        mockEndpoint.expectedHeaderReceived("id", id);
+
+        producerTemplate.to("direct:manualSaga")
+                .withHeader("myid", id)
+                .withBody(body)
+                .send();
+
+        mockEndpoint.assertIsSatisfied(5000);
+        return 
mockEndpoint.getReceivedExchanges().get(0).getMessage().getBody(String.class);
+    }
+
 }
diff --git 
a/integration-tests/lra/src/main/java/org/apache/camel/quarkus/component/lra/it/LraRoutes.java
 
b/integration-tests/lra/src/main/java/org/apache/camel/quarkus/component/lra/it/LraRoutes.java
index 7438b51cd2..c97179ffb5 100644
--- 
a/integration-tests/lra/src/main/java/org/apache/camel/quarkus/component/lra/it/LraRoutes.java
+++ 
b/integration-tests/lra/src/main/java/org/apache/camel/quarkus/component/lra/it/LraRoutes.java
@@ -16,10 +16,13 @@
  */
 package org.apache.camel.quarkus.component.lra.it;
 
+import java.util.concurrent.TimeUnit;
+
 import jakarta.enterprise.context.ApplicationScoped;
 import jakarta.inject.Inject;
 import org.apache.camel.Exchange;
 import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.model.SagaCompletionMode;
 import org.apache.camel.model.SagaPropagation;
 import org.apache.camel.quarkus.component.lra.it.service.CreditService;
 import org.apache.camel.quarkus.component.lra.it.service.OrderManagerService;
@@ -82,5 +85,30 @@ public class LraRoutes extends RouteBuilder {
                     throw new Exception("fail");
                 })
                 .end();
+
+        // ManualSaga
+        from("direct:manualSaga")
+                .saga()
+                .completionMode(SagaCompletionMode.MANUAL)
+                .timeout(1, TimeUnit.SECONDS)
+                .option("id", header("myid"))
+                .completion("direct:complete")
+                .compensation("direct:compensate")
+                .to("mock:endpoint")
+                .choice()
+                .when(body().isEqualTo("fail"))
+                .to("saga:compensate")
+                .when(body().isNotEqualTo("timeout"))
+                .to("saga:complete")
+                .end();
+
+        from("direct:complete")
+                .setBody(constant("complete"))
+                .to("mock:complete");
+
+        from("direct:compensate")
+                .setBody(constant("compensate"))
+                .to("mock:compensate");
+
     }
 }
diff --git 
a/integration-tests/lra/src/test/java/org/apache/camel/quarkus/component/lra/it/LraTest.java
 
b/integration-tests/lra/src/test/java/org/apache/camel/quarkus/component/lra/it/LraTest.java
index 1052958a07..7b18325eb2 100644
--- 
a/integration-tests/lra/src/test/java/org/apache/camel/quarkus/component/lra/it/LraTest.java
+++ 
b/integration-tests/lra/src/test/java/org/apache/camel/quarkus/component/lra/it/LraTest.java
@@ -21,7 +21,10 @@ import java.util.concurrent.TimeUnit;
 import io.quarkus.test.common.QuarkusTestResource;
 import io.quarkus.test.junit.QuarkusTest;
 import io.restassured.RestAssured;
+import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
 
 import static org.awaitility.Awaitility.await;
 
@@ -81,4 +84,18 @@ class LraTest {
                     .asString().equals("5");
         });
     }
+
+    @ParameterizedTest
+    @ValueSource(strings = { "hello", "fail", "timeout" })
+    void testManualSaga(String body) {
+        final String actual = RestAssured.given()
+                .queryParam("id", 1)
+                .body(body)
+                .post("/lra/manual")
+                .then()
+                .statusCode(200)
+                .extract().body().asString();
+
+        Assertions.assertEquals(body.equals("hello") ? "complete" : 
"compensate", actual);
+    }
 }

Reply via email to