diff --git a/demo/sample-quarkus/src/test/java/org/apache/camel/demo/FoodMarketApplicationTest.java b/demo/sample-quarkus/src/test/java/org/apache/camel/demo/FoodMarketApplicationTest.java index 968432c5..e36318d9 100644 --- a/demo/sample-quarkus/src/test/java/org/apache/camel/demo/FoodMarketApplicationTest.java +++ b/demo/sample-quarkus/src/test/java/org/apache/camel/demo/FoodMarketApplicationTest.java @@ -21,6 +21,9 @@ import io.quarkus.test.junit.QuarkusTest; import jakarta.inject.Inject; +import org.apache.camel.demo.behavior.VerifyBookingCompletedMail; +import org.apache.camel.demo.behavior.WaitForEntityPersisted; +import org.apache.camel.demo.behavior.WaitForProductCreated; import org.apache.camel.demo.model.Booking; import org.apache.camel.demo.model.Product; import org.apache.camel.demo.model.Supply; @@ -31,18 +34,15 @@ import org.citrusframework.annotations.CitrusEndpoint; import org.citrusframework.annotations.CitrusResource; import org.citrusframework.kafka.endpoint.KafkaEndpoint; -import org.citrusframework.mail.message.MailMessage; import org.citrusframework.mail.server.MailServer; import org.citrusframework.quarkus.CitrusSupport; import org.junit.jupiter.api.Test; -import static org.citrusframework.actions.ExecuteSQLAction.Builder.sql; import static org.citrusframework.actions.ReceiveMessageAction.Builder.receive; import static org.citrusframework.actions.SendMessageAction.Builder.send; import static org.citrusframework.actions.SleepAction.Builder.delay; import static org.citrusframework.container.Iterate.Builder.iterate; import static org.citrusframework.container.Parallel.Builder.parallel; -import static org.citrusframework.container.RepeatOnErrorUntilTrue.Builder.repeatOnError; import static org.citrusframework.dsl.JsonSupport.marshal; @QuarkusTest @@ -81,28 +81,14 @@ void shouldCompleteOnSupply() { .endpoint(products) .message().body(marshal(product))); - t.then(repeatOnError() - .condition((i, context) -> i > 25) - .autoSleep(500) - .actions(sql().dataSource(dataSource) - .query() - .statement("select count(id) as found from product where product.name='%s'".formatted(product.getName())) - .validate("found", "1")) - ); + t.then(t.applyBehavior(new WaitForProductCreated(product, dataSource))); Booking booking = new Booking("citrus-test", product, 100, 0.99D); t.when(send() .endpoint(bookings) .message().body(marshal(booking))); - t.then(repeatOnError() - .condition((i, context) -> i > 25) - .autoSleep(500) - .actions(sql().dataSource(dataSource) - .query() - .statement("select count(id) as found from booking where booking.status='PENDING'") - .validate("found", "1")) - ); + t.then(t.applyBehavior(new WaitForEntityPersisted(booking, dataSource))); Supply supply = new Supply(product, 100, 0.99D); t.when(send() @@ -122,16 +108,7 @@ void shouldCompleteOnSupply() { .message().body(marshal(shippingEvent)) )); - t.then(receive() - .endpoint(mailServer) - .message(MailMessage.request("foodmarket@quarkus.io", "%s@quarkus.io".formatted(completedEvent.getClient()), "Booking completed!") - .body("Hey %s, your booking %s has been completed.".formatted(completedEvent.getClient(), completedEvent.getProduct()), "text/plain")) - ); - - t.then(send() - .endpoint(mailServer) - .message(MailMessage.response()) - ); + t.then(t.applyBehavior(new VerifyBookingCompletedMail(booking, mailServer))); } @Test @@ -143,14 +120,7 @@ void shouldCompleteOnBooking() { .endpoint(supplies) .message().body(marshal(supply))); - t.then(repeatOnError() - .condition((i, context) -> i > 25) - .autoSleep(500) - .actions(sql().dataSource(dataSource) - .query() - .statement("select count(id) as found from supply where supply.status='AVAILABLE'") - .validate("found", "1")) - ); + t.then(t.applyBehavior(new WaitForEntityPersisted(supply, dataSource))); Booking booking = new Booking("citrus-test", product, 100, 0.99D); t.when(send() @@ -170,16 +140,7 @@ void shouldCompleteOnBooking() { .message().body(marshal(shippingEvent)) )); - t.then(receive() - .endpoint(mailServer) - .message(MailMessage.request("foodmarket@quarkus.io", "%s@quarkus.io".formatted(completedEvent.getClient()), "Booking completed!") - .body("Hey %s, your booking %s has been completed.".formatted(completedEvent.getClient(), completedEvent.getProduct()), "text/plain")) - ); - - t.then(send() - .endpoint(mailServer) - .message(MailMessage.response()) - ); + t.then(t.applyBehavior(new VerifyBookingCompletedMail(booking, mailServer))); } @Test diff --git a/demo/sample-quarkus/src/test/java/org/apache/camel/demo/behavior/VerifyBookingCompletedMail.java b/demo/sample-quarkus/src/test/java/org/apache/camel/demo/behavior/VerifyBookingCompletedMail.java new file mode 100644 index 00000000..d28ae604 --- /dev/null +++ b/demo/sample-quarkus/src/test/java/org/apache/camel/demo/behavior/VerifyBookingCompletedMail.java @@ -0,0 +1,36 @@ +package org.apache.camel.demo.behavior; + +import org.apache.camel.demo.model.Booking; +import org.citrusframework.TestActionRunner; +import org.citrusframework.TestBehavior; +import org.citrusframework.mail.message.MailMessage; +import org.citrusframework.mail.server.MailServer; + +import static org.citrusframework.actions.ReceiveMessageAction.Builder.receive; +import static org.citrusframework.actions.SendMessageAction.Builder.send; + +public class VerifyBookingCompletedMail implements TestBehavior { + + private final Booking booking; + private final MailServer mailServer; + + public VerifyBookingCompletedMail(Booking booking, MailServer mailServer) { + this.booking = booking; + this.mailServer = mailServer; + } + + @Override + public void apply(TestActionRunner t) { + t.run(receive() + .endpoint(mailServer) + .message(MailMessage.request("foodmarket@quarkus.io", "%s@quarkus.io".formatted(booking.getClient()), "Booking completed!") + .body("Hey %s, your booking %s has been completed." + .formatted(booking.getClient(), booking.getProduct().getName()), "text/plain")) + ); + + t.run(send() + .endpoint(mailServer) + .message(MailMessage.response()) + ); + } +} diff --git a/demo/sample-quarkus/src/test/java/org/apache/camel/demo/behavior/WaitForEntityPersisted.java b/demo/sample-quarkus/src/test/java/org/apache/camel/demo/behavior/WaitForEntityPersisted.java new file mode 100644 index 00000000..32ebdd60 --- /dev/null +++ b/demo/sample-quarkus/src/test/java/org/apache/camel/demo/behavior/WaitForEntityPersisted.java @@ -0,0 +1,43 @@ +package org.apache.camel.demo.behavior; + +import javax.sql.DataSource; + +import org.apache.camel.demo.model.Booking; +import org.apache.camel.demo.model.Supply; +import org.citrusframework.TestActionRunner; +import org.citrusframework.TestBehavior; + +import static org.citrusframework.actions.ExecuteSQLAction.Builder.sql; +import static org.citrusframework.container.RepeatOnErrorUntilTrue.Builder.repeatOnError; + +public class WaitForEntityPersisted implements TestBehavior { + + private final String entityName; + private final String status; + private final DataSource dataSource; + + public WaitForEntityPersisted(Booking booking, DataSource dataSource) { + this.entityName = "booking"; + this.status = booking.getStatus().name(); + this.dataSource = dataSource; + } + + public WaitForEntityPersisted(Supply supply, DataSource dataSource) { + this.entityName = "supply"; + this.status = supply.getStatus().name(); + this.dataSource = dataSource; + } + + @Override + public void apply(TestActionRunner t) { + t.run(repeatOnError() + .condition((i, context) -> i > 25) + .autoSleep(500) + .actions(sql().dataSource(dataSource) + .query() + .statement("select count(id) as found from %s where %s.status='%s'" + .formatted(entityName, entityName, status)) + .validate("found", "1")) + ); + } +} diff --git a/demo/sample-quarkus/src/test/java/org/apache/camel/demo/behavior/WaitForProductCreated.java b/demo/sample-quarkus/src/test/java/org/apache/camel/demo/behavior/WaitForProductCreated.java new file mode 100644 index 00000000..762ae177 --- /dev/null +++ b/demo/sample-quarkus/src/test/java/org/apache/camel/demo/behavior/WaitForProductCreated.java @@ -0,0 +1,34 @@ +package org.apache.camel.demo.behavior; + +import javax.sql.DataSource; + +import org.apache.camel.demo.model.Product; +import org.citrusframework.TestActionRunner; +import org.citrusframework.TestBehavior; + +import static org.citrusframework.actions.ExecuteSQLAction.Builder.sql; +import static org.citrusframework.container.RepeatOnErrorUntilTrue.Builder.repeatOnError; + +public class WaitForProductCreated implements TestBehavior { + + private final Product product; + private final DataSource dataSource; + + public WaitForProductCreated(Product product, DataSource dataSource) { + this.product = product; + this.dataSource = dataSource; + } + + @Override + public void apply(TestActionRunner t) { + t.run(repeatOnError() + .condition((i, context) -> i > 25) + .autoSleep(500) + .actions(sql().dataSource(dataSource) + .query() + .statement("select count(id) as found from product where product.name='%s'" + .formatted(product.getName())) + .validate("found", "1")) + ); + } +}