Skip to content

Commit

Permalink
Improve Quarkus sample demo
Browse files Browse the repository at this point in the history
- Introduce test behavior
  • Loading branch information
christophd committed Nov 10, 2023
1 parent a56a6f4 commit f175601
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -122,16 +108,7 @@ void shouldCompleteOnSupply() {
.message().body(marshal(shippingEvent))
));

t.then(receive()
.endpoint(mailServer)
.message(MailMessage.request("[email protected]", "%[email protected]".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
Expand All @@ -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()
Expand All @@ -170,16 +140,7 @@ void shouldCompleteOnBooking() {
.message().body(marshal(shippingEvent))
));

t.then(receive()
.endpoint(mailServer)
.message(MailMessage.request("[email protected]", "%[email protected]".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
Expand Down
Original file line number Diff line number Diff line change
@@ -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("[email protected]", "%[email protected]".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())
);
}
}
Original file line number Diff line number Diff line change
@@ -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"))
);
}
}
Original file line number Diff line number Diff line change
@@ -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"))
);
}
}

0 comments on commit f175601

Please sign in to comment.