Skip to content

Commit

Permalink
Improve Quarkus sample demo
Browse files Browse the repository at this point in the history
- Add JDBC data source to Citrus tests
- Wait for persistence layer in tests
  • Loading branch information
christophd committed Nov 10, 2023
1 parent 5533f01 commit a56a6f4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 25 deletions.
5 changes: 5 additions & 0 deletions demo/sample-quarkus/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@
<artifactId>citrus-mail</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.citrusframework</groupId>
<artifactId>citrus-sql</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.citrusframework</groupId>
<artifactId>citrus-validation-text</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@

package org.apache.camel.demo;

import com.fasterxml.jackson.databind.ObjectMapper;
import javax.sql.DataSource;

import io.quarkus.test.junit.QuarkusTest;
import jakarta.inject.Inject;
import org.apache.camel.demo.model.Booking;
import org.apache.camel.demo.model.Product;
import org.apache.camel.demo.model.Supply;
Expand All @@ -34,11 +36,13 @@
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
Expand Down Expand Up @@ -67,29 +71,43 @@ class FoodMarketApplicationTest {
@CitrusResource
private TestCaseRunner t;

@CitrusResource
private ObjectMapper mapper;
@Inject
DataSource dataSource;

@Test
void shouldCompleteOnSupply() {
Product product = new Product("Watermelon");
t.when(send()
.endpoint(products)
.message().body(marshal(product, mapper)));

t.then(delay().seconds(3L));
.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"))
);

Booking booking = new Booking("citrus-test", product, 100, 0.99D);
t.when(send()
.endpoint(bookings)
.message().body(marshal(booking, mapper)));

t.then(delay().milliseconds(1500L));
.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"))
);

Supply supply = new Supply(product, 100, 0.99D);
t.when(send()
.endpoint(supplies)
.message().body(marshal(supply, mapper)));
.message().body(marshal(supply)));

BookingCompletedEvent completedEvent = BookingCompletedEvent.from(booking);
completedEvent.setStatus(Booking.Status.COMPLETED.name());
Expand All @@ -98,10 +116,10 @@ void shouldCompleteOnSupply() {
t.then(parallel().actions(
receive()
.endpoint(completed)
.message().body(marshal(completedEvent, mapper)),
.message().body(marshal(completedEvent)),
receive()
.endpoint(shipping)
.message().body(marshal(shippingEvent, mapper))
.message().body(marshal(shippingEvent))
));

t.then(receive()
Expand All @@ -120,17 +138,24 @@ void shouldCompleteOnSupply() {
void shouldCompleteOnBooking() {
Product product = new Product("Pineapple");

Supply supply = new Supply(product, 100, 0.99D);
Supply supply = new Supply(product, 100, 0.90D);
t.when(send()
.endpoint(supplies)
.message().body(marshal(supply, mapper)));

t.then(delay().milliseconds(1500L));
.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"))
);

Booking booking = new Booking("citrus-test", product, 100, 0.99D);
t.when(send()
.endpoint(bookings)
.message().body(marshal(booking, mapper)));
.endpoint(bookings)
.message().body(marshal(booking)));

BookingCompletedEvent completedEvent = BookingCompletedEvent.from(booking);
completedEvent.setStatus(Booking.Status.COMPLETED.name());
Expand All @@ -139,10 +164,10 @@ void shouldCompleteOnBooking() {
t.then(parallel().actions(
receive()
.endpoint(completed)
.message().body(marshal(completedEvent, mapper)),
.message().body(marshal(completedEvent)),
receive()
.endpoint(shipping)
.message().body(marshal(shippingEvent, mapper))
.message().body(marshal(shippingEvent))
));

t.then(receive()
Expand All @@ -168,7 +193,7 @@ void shouldCompleteAllMatchingBookings() {
.actions(
send()
.endpoint(bookings)
.message().body(marshal(booking, mapper))));
.message().body(marshal(booking))));
t.variable("booking", booking);

t.$(delay().milliseconds(1000L));
Expand All @@ -183,20 +208,20 @@ void shouldCompleteAllMatchingBookings() {
t.then(parallel().actions(
send()
.endpoint(supplies)
.message().body(marshal(supply, mapper)),
.message().body(marshal(supply)),
iterate()
.condition((i, context) -> i < 10)
.actions(
receive()
.endpoint(completed)
.message().body(marshal(completedEvent, mapper))
.message().body(marshal(completedEvent))
),
iterate()
.condition((i, context) -> i < 10)
.actions(
receive()
.endpoint(shipping)
.message().body(marshal(shippingEvent, mapper))
.message().body(marshal(shippingEvent))
)
));
}
Expand Down

0 comments on commit a56a6f4

Please sign in to comment.