Skip to content

Commit

Permalink
Fix apache#2604 to add a camel-sql case in jta integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zhfeng authored and jamesnetherton committed May 27, 2021
1 parent 1be3bbc commit b13fa9c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 9 deletions.
4 changes: 4 additions & 0 deletions integration-tests/jta/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-sql</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,39 @@ public Response postInTx(@PathParam("policy") String policy, String message) thr
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
public Response jdbc(String message) throws Exception {
String response = request("direct:jdbc", message);
LOG.infof("Got response from jdbc: %s", response);
return Response
.created(new URI("https://camel.apache.org/"))
.entity(response)
.build();
}

@Path("/sqltx")
@POST
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
public Response sqltx(String message) throws Exception {
String response = request("direct:sqltx", message);
LOG.infof("Got response from sqltx: %s", response);
return Response
.created(new URI("https://camel.apache.org/"))
.entity(response)
.build();
}

private String request(String endpoint, String message) throws Exception {
LOG.infof("message is %s", message);
MockEndpoint mockEndpoint = context.getEndpoint("mock:txResult", MockEndpoint.class);
mockEndpoint.reset();
if (!message.equals("fail")) {
mockEndpoint.expectedMessageCount(1);
mockEndpoint.message(0).body().isEqualTo(message);
}
final String response = producerTemplate.requestBody("direct:transaction", message, String.class);
final String response = producerTemplate.requestBody(endpoint, message, String.class);
mockEndpoint.assertIsSatisfied(15000);

LOG.infof("Got response from jta: %s", response);
return Response
.created(new URI("https://camel.apache.org/"))
.entity(response)
.build();
return response;
}

@Path("/mock")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void configure() throws Exception {
from("direct:not_supported")
.transacted("PROPAGATION_NOT_SUPPORTED").transform().constant("not_supported");

from("direct:transaction")
from("direct:jdbc")
.transacted()
.setHeader("message", body())
.to("jms:queue:txTest?connectionFactory=#xaConnectionFactory&disableReplyTo=true")
Expand All @@ -64,6 +64,21 @@ public void configure() throws Exception {
.transform().simple("${header.message} added")
.endChoice();

from("direct:sqltx")
.transacted()
.setHeader("message", body())
.to("jms:queue:txTest?connectionFactory=#xaConnectionFactory&disableReplyTo=true")
.to("sql:insert into example(message) values (:#message)")
.choice()
.when(header("message").startsWith("fail"))
.log("Failing forever with exception")
.process(x -> {
throw new RuntimeException("Fail");
})
.otherwise()
.transform().simple("${header.message} added")
.endChoice();

from("jms:queue:txTest?connectionFactory=#xaConnectionFactory")
.to("mock:txResult");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,20 +138,29 @@ public void testInTx() {

@Test
public void testJdbcInTx() {
testTx("/jta/jdbc");
}

@Test
public void testSqlInTx() {
testTx("/jta/sqltx");
}

private void testTx(String url) {
final String msg = java.util.UUID.randomUUID().toString().replace("-", "");

RestAssured.given()
.contentType(ContentType.TEXT)
.body(msg)
.post("/jta/jdbc")
.post(url)
.then()
.statusCode(201)
.body(is(msg + " added"));

RestAssured.given()
.contentType(ContentType.TEXT)
.body("fail")
.post("/jta/jdbc")
.post(url)
.then()
.statusCode(500);

Expand Down

0 comments on commit b13fa9c

Please sign in to comment.