zbendhiba commented on code in PR #489: URL: https://github.com/apache/camel-quarkus-examples/pull/489#discussion_r2995531832
########## saga/saga-integration-tests/src/test/java/org/apache/camel/example/saga/SagaBasicTest.java: ########## @@ -0,0 +1,101 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.camel.example.saga; + +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.concurrent.CompletableFuture; +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.Test; + +import static org.awaitility.Awaitility.await; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Basic integration tests for Saga example. + * Tests verify saga orchestration, service participation, routing, and compensation. + * Note: Payment service has 15% random failure rate to test compensation scenarios. + */ +@QuarkusTest +@QuarkusTestResource(SagaTestResource.class) +public class SagaBasicTest { + + private static final String LOG_FILE = "target/quarkus.log"; + + /** + * Test saga orchestration with LRA - accepts both success and compensation outcomes. + * Payment service has 15% random failure rate, so either scenario is valid. + */ + @Test + public void testSagaWithLRAAndRandomOutcomes() throws Exception { + // Trigger saga asynchronously (may timeout on payment failure, which is expected) + CompletableFuture.runAsync(() -> { + try { + RestAssured.given() + .queryParam("id", 1) + .post("/api/saga"); + } catch (Exception e) { + // Expected - request may timeout on payment failure + } + }); + + // Wait for saga to start and process fully + // In native mode, we need to wait longer for all messages to be logged + await().atMost(60, TimeUnit.SECONDS).untilAsserted(() -> { + String log = new String(Files.readAllBytes(Paths.get(LOG_FILE)), StandardCharsets.UTF_8); + assertTrue(log.contains("Executing saga #1"), "Saga should start with LRA"); + + // Wait until we see evidence of completion (success or failure) + boolean completed = log.contains("done for order #1") + || log.contains("fails!") + || log.contains("cancelled"); + assertTrue(completed, "Saga should complete with either success or compensation"); + }); + + // Give logs a moment to flush (important in native mode) + Thread.sleep(1000); Review Comment: Was there a particular reason that prevents using await() here too ########## saga/saga-integration-tests/src/test/java/org/apache/camel/example/saga/SagaBasicTest.java: ########## @@ -0,0 +1,101 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.camel.example.saga; + +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.concurrent.CompletableFuture; +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.Test; + +import static org.awaitility.Awaitility.await; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Basic integration tests for Saga example. + * Tests verify saga orchestration, service participation, routing, and compensation. + * Note: Payment service has 15% random failure rate to test compensation scenarios. + */ +@QuarkusTest +@QuarkusTestResource(SagaTestResource.class) +public class SagaBasicTest { + + private static final String LOG_FILE = "target/quarkus.log"; + + /** + * Test saga orchestration with LRA - accepts both success and compensation outcomes. + * Payment service has 15% random failure rate, so either scenario is valid. + */ + @Test + public void testSagaWithLRAAndRandomOutcomes() throws Exception { + // Trigger saga asynchronously (may timeout on payment failure, which is expected) + CompletableFuture.runAsync(() -> { + try { + RestAssured.given() + .queryParam("id", 1) + .post("/api/saga"); + } catch (Exception e) { + // Expected - request may timeout on payment failure + } + }); + + // Wait for saga to start and process fully + // In native mode, we need to wait longer for all messages to be logged + await().atMost(60, TimeUnit.SECONDS).untilAsserted(() -> { + String log = new String(Files.readAllBytes(Paths.get(LOG_FILE)), StandardCharsets.UTF_8); + assertTrue(log.contains("Executing saga #1"), "Saga should start with LRA"); + + // Wait until we see evidence of completion (success or failure) + boolean completed = log.contains("done for order #1") + || log.contains("fails!") + || log.contains("cancelled"); + assertTrue(completed, "Saga should complete with either success or compensation"); + }); + + // Give logs a moment to flush (important in native mode) + Thread.sleep(1000); + + String log = new String(Files.readAllBytes(Paths.get(LOG_FILE)), StandardCharsets.UTF_8); + + // Verify LRA coordinator is used + assertTrue(log.contains("lra-coordinator"), "Should use LRA coordinator"); + + // Verify services participated + assertTrue(log.contains("Buying train") || log.contains("Buying flight"), + "Services should participate"); + + // Check outcome - either success or compensation is valid + boolean hasSuccess = log.contains("done for order #1"); + boolean hasFailure = log.contains("fails!"); + boolean hasCompensation = log.contains("cancelled"); + + if (hasFailure || hasCompensation) { + System.out.println("Saga #1: Compensation scenario tested (payment failed, saga rolled back)"); + } else if (hasSuccess) { + System.out.println("Saga #1: Success scenario tested (all payments completed)"); + } + + // Either outcome is valid - test passes + assertTrue(hasSuccess || hasFailure, Review Comment: Are we certain that if a test failure, it's due to Payment service down? I'm afraid we miss at some point that the saga flow is down. Unless you know this type of failure is definitely coming from paying system. otherwise, maybe we can add a number of retries to the unit test -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
