This is an automated email from the ASF dual-hosted git repository. jamesnetherton pushed a commit to branch 2.13.x in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git
commit ad9c3b1f255c7e2acd214b29fb5f5ab800c60a6c Author: Lucia Drozdová <[email protected]> AuthorDate: Fri Mar 24 14:27:02 2023 +0100 Ref #4596: Expand JDBC tests - named parameters and samples (#4655) * Ref #4596: Expand JDBC tests - named parameters and samples * Fix imports --- integration-tests/jdbc/pom.xml | 80 ++++++++++++++- .../quarkus/component/jdbc/CamelResource.java | 78 ++++++++++++++- .../camel/quarkus/component/jdbc/JdbcRoutes.java | 67 +++++++++++++ .../quarkus/component/jdbc/CamelJdbcTest.java | 111 +++++++++++++++++++++ 4 files changed, 333 insertions(+), 3 deletions(-) diff --git a/integration-tests/jdbc/pom.xml b/integration-tests/jdbc/pom.xml index 11f753b626..c9fc80fb88 100644 --- a/integration-tests/jdbc/pom.xml +++ b/integration-tests/jdbc/pom.xml @@ -17,7 +17,8 @@ limitations under the License. --> -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> +<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.apache.camel.quarkus</groupId> @@ -39,6 +40,14 @@ <groupId>org.apache.camel.quarkus</groupId> <artifactId>camel-quarkus-log</artifactId> </dependency> + <dependency> + <groupId>org.apache.camel.quarkus</groupId> + <artifactId>camel-quarkus-timer</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel.quarkus</groupId> + <artifactId>camel-quarkus-bean</artifactId> + </dependency> <dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-resteasy</artifactId> @@ -47,6 +56,18 @@ <groupId>io.quarkus</groupId> <artifactId>quarkus-jdbc-h2</artifactId> </dependency> + <dependency> + <groupId>org.apache.camel.quarkus</groupId> + <artifactId>camel-quarkus-direct</artifactId> + </dependency> + <dependency> + <groupId>io.quarkus</groupId> + <artifactId>quarkus-resteasy-jsonb</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel.quarkus</groupId> + <artifactId>camel-quarkus-mock</artifactId> + </dependency> <!-- test dependencies --> <dependency> @@ -64,6 +85,11 @@ <artifactId>rest-assured</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>org.awaitility</groupId> + <artifactId>awaitility</artifactId> + <scope>test</scope> + </dependency> </dependencies> @@ -104,6 +130,19 @@ </activation> <dependencies> <!-- The following dependencies guarantee that this module is built after them. You can update them by running `mvn process-resources -Pformat -N` from the source tree root directory --> + <dependency> + <groupId>org.apache.camel.quarkus</groupId> + <artifactId>camel-quarkus-direct-deployment</artifactId> + <version>${project.version}</version> + <type>pom</type> + <scope>test</scope> + <exclusions> + <exclusion> + <groupId>*</groupId> + <artifactId>*</artifactId> + </exclusion> + </exclusions> + </dependency> <dependency> <groupId>org.apache.camel.quarkus</groupId> <artifactId>camel-quarkus-jdbc-deployment</artifactId> @@ -130,6 +169,45 @@ </exclusion> </exclusions> </dependency> + <dependency> + <groupId>org.apache.camel.quarkus</groupId> + <artifactId>camel-quarkus-mock-deployment</artifactId> + <version>${project.version}</version> + <type>pom</type> + <scope>test</scope> + <exclusions> + <exclusion> + <groupId>*</groupId> + <artifactId>*</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>org.apache.camel.quarkus</groupId> + <artifactId>camel-quarkus-timer-deployment</artifactId> + <version>${project.version}</version> + <type>pom</type> + <scope>test</scope> + <exclusions> + <exclusion> + <groupId>*</groupId> + <artifactId>*</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>org.apache.camel.quarkus</groupId> + <artifactId>camel-quarkus-bean-deployment</artifactId> + <version>${project.version}</version> + <type>pom</type> + <scope>test</scope> + <exclusions> + <exclusion> + <groupId>*</groupId> + <artifactId>*</artifactId> + </exclusion> + </exclusions> + </dependency> </dependencies> </profile> </profiles> diff --git a/integration-tests/jdbc/src/main/java/org/apache/camel/quarkus/component/jdbc/CamelResource.java b/integration-tests/jdbc/src/main/java/org/apache/camel/quarkus/component/jdbc/CamelResource.java index 90312e8767..6bdc09f67f 100644 --- a/integration-tests/jdbc/src/main/java/org/apache/camel/quarkus/component/jdbc/CamelResource.java +++ b/integration-tests/jdbc/src/main/java/org/apache/camel/quarkus/component/jdbc/CamelResource.java @@ -17,10 +17,11 @@ package org.apache.camel.quarkus.component.jdbc; import java.sql.Connection; -import java.sql.SQLException; import java.sql.Statement; +import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; import javax.annotation.PostConstruct; import javax.enterprise.context.ApplicationScoped; @@ -35,7 +36,9 @@ import javax.ws.rs.core.MediaType; import io.agroal.api.AgroalDataSource; import io.quarkus.agroal.DataSource; +import org.apache.camel.CamelContext; import org.apache.camel.ProducerTemplate; +import org.apache.camel.component.mock.MockEndpoint; import org.apache.camel.quarkus.component.jdbc.model.Camel; @Path("/test") @@ -48,18 +51,27 @@ public class CamelResource { @Inject ProducerTemplate template; + @Inject + CamelContext context; + @PostConstruct - void postConstruct() throws SQLException { + void postConstruct() throws Exception { try (Connection con = dataSource.getConnection()) { try (Statement statement = con.createStatement()) { try { statement.execute("drop table camels"); + statement.execute("drop table camelsGenerated"); } catch (Exception ignored) { } statement.execute("create table camels (id int primary key, species varchar(255))"); + statement.execute("create table camelsGenerated (id int primary key auto_increment, species varchar(255))"); + statement.execute("create table camelsProcessed (id int primary key auto_increment, species varchar(255))"); + statement.execute("insert into camelsGenerated (species) values ('Camelus status'), ('Camelus linus')"); statement.execute("insert into camels (id, species) values (1, 'Camelus dromedarius')"); statement.execute("insert into camels (id, species) values (2, 'Camelus bactrianus')"); statement.execute("insert into camels (id, species) values (3, 'Camelus ferus')"); + + context.getRouteController().startRoute("jdbc-poll"); } } } @@ -110,4 +122,66 @@ public class CamelResource { public String executeStatement(String statement) throws Exception { return template.requestBody("jdbc:camel-ds", statement, String.class); } + + @Path("/generated-keys/rows") + @GET + @Produces(MediaType.APPLICATION_JSON) + public List generatedKeysRows() throws Exception { + return template.requestBodyAndHeader("direct://get-generated-keys", + "insert into camelsGenerated (species) values ('Camelus testus'), ('Camelus legendarius')", + "CamelRetrieveGeneratedKeys", "true", ArrayList.class); + } + + @Path("/headers/insert") + @GET + @Produces(MediaType.APPLICATION_JSON) + public String headersFromInsertOrUpdate() throws Exception { + return template.requestBodyAndHeader("direct://get-headers", + "insert into camelsGenerated (species) values ('Camelus status'), ('Camelus linus')", + "CamelRetrieveGeneratedKeys", "true", String.class); + } + + @Path("/headers/select") + @GET + @Produces(MediaType.APPLICATION_JSON) + public String headersFromSelect() throws Exception { + return template.requestBody("direct://get-headers", "select * from camelsGenerated", String.class); + } + + @Path("/named-parameters/headers-as-parameters") + @GET + @Produces(MediaType.APPLICATION_JSON) + public String headersAsParameters() throws Exception { + return template.requestBodyAndHeader("direct://headers-as-parameters", + "select * from camels where id < :?idmax order by id", + "idmax", "3", String.class); + } + + @Path("/named-parameters/headers-as-parameters-map") + @GET + @Produces(MediaType.APPLICATION_JSON) + public String headersAsParametersMap() throws Exception { + Map<String, String> headersMap = Map.of("idmax", "3", "specs", "Camelus bactrianus"); + return template.requestBodyAndHeader("direct://headers-as-parameters", + "select * from camels where id < :?idmax and species = :?specs order by id", + "CamelJdbcParameters", headersMap, String.class); + } + + @Path("/interval-polling") + @GET + @Produces(MediaType.APPLICATION_JSON) + public void intervalPolling(String selectResult) throws Exception { + MockEndpoint mockEndpoint = context.getEndpoint("mock:interval-polling", MockEndpoint.class); + mockEndpoint.expectedBodiesReceived(selectResult); + + mockEndpoint.assertIsSatisfied(); + } + + @Path("/move-between-datasources") + @POST + @Produces(MediaType.APPLICATION_JSON) + public String moveBetweenDatasources() throws Exception { + return template.requestBody("direct://move-between-datasources", null, String.class); + } + } diff --git a/integration-tests/jdbc/src/main/java/org/apache/camel/quarkus/component/jdbc/JdbcRoutes.java b/integration-tests/jdbc/src/main/java/org/apache/camel/quarkus/component/jdbc/JdbcRoutes.java new file mode 100644 index 0000000000..ad47740886 --- /dev/null +++ b/integration-tests/jdbc/src/main/java/org/apache/camel/quarkus/component/jdbc/JdbcRoutes.java @@ -0,0 +1,67 @@ +/* + * 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.quarkus.component.jdbc; + +import javax.enterprise.context.ApplicationScoped; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.jboss.logging.Logger; + +@ApplicationScoped +public class JdbcRoutes extends RouteBuilder { + private static final Logger LOG = Logger.getLogger(JdbcRoutes.class); + + @Override + public void configure() { + from("direct://get-generated-keys") + .to("jdbc:camel-ds") + .process(new Processor() { + @Override + public void process(Exchange exchange) throws Exception { + Object in = exchange.getIn().getHeader("CamelGeneratedKeysRows"); + exchange.getIn().setBody(in); + } + }); + + from("direct://get-headers") + .to("jdbc:camel-ds") + .process(new Processor() { + @Override + public void process(Exchange exchange) throws Exception { + Object in = exchange.getIn().getHeaders(); + exchange.getIn().setBody(in); + } + }); + + from("direct://headers-as-parameters") + .to("jdbc:camel-ds?useHeadersAsParameters=true"); + + from("timer://interval-polling?delay=2000&repeatCount=1").routeId("jdbc-poll").autoStartup(false) + .setBody(constant("select * from camelsGenerated order by id desc")) + .to("jdbc:camel-ds") + .to("mock:interval-polling"); + + from("direct://move-between-datasources") + .setBody(constant("select * from camels")) + .to("jdbc:camel-ds") + .split(body()) + .setBody(simple("insert into camelsProcessed values('${body[ID]}','${body[SPECIES]}')")) + .to("jdbc:camel-ds"); + } +} diff --git a/integration-tests/jdbc/src/test/java/org/apache/camel/quarkus/component/jdbc/CamelJdbcTest.java b/integration-tests/jdbc/src/test/java/org/apache/camel/quarkus/component/jdbc/CamelJdbcTest.java index f29fefe0f4..9fe7dad4af 100644 --- a/integration-tests/jdbc/src/test/java/org/apache/camel/quarkus/component/jdbc/CamelJdbcTest.java +++ b/integration-tests/jdbc/src/test/java/org/apache/camel/quarkus/component/jdbc/CamelJdbcTest.java @@ -16,6 +16,8 @@ */ package org.apache.camel.quarkus.component.jdbc; +import java.util.List; + import io.quarkus.test.common.QuarkusTestResource; import io.quarkus.test.h2.H2DatabaseTestResource; import io.quarkus.test.junit.QuarkusTest; @@ -23,7 +25,11 @@ import io.restassured.RestAssured; import io.restassured.http.ContentType; import org.junit.jupiter.api.Test; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; +import static org.junit.jupiter.api.Assertions.assertTrue; @QuarkusTest @QuarkusTestResource(H2DatabaseTestResource.class) @@ -53,4 +59,109 @@ public class CamelJdbcTest { .post("/test/execute") .then().body(is("[{ID=3}, {ID=2}, {ID=1}]")); } + + @Test + void testCamelRetrieveGeneratedKeysHeader() { + List generatedIDs = RestAssured.given() + .get("test/generated-keys/rows") + .then().extract().body() + .jsonPath().getList("ID"); + + String selectResult = RestAssured.given() + .contentType(ContentType.TEXT).body("select id from camelsGenerated") + .post("/test/execute") + .then().extract().body().asString(); + + generatedIDs.forEach(generatedID -> assertTrue(selectResult.contains(generatedID.toString()))); + } + + @Test + void testHeadersFromInsertOrUpdateQuery() { + RestAssured.given() + .get("test/headers/insert") + .then() + .body(containsString("CamelGeneratedKeysRowCount=2")) + .and() + .body(containsString("CamelJdbcUpdateCount=2")) + .and() + .body(containsString("CamelRetrieveGeneratedKeys=true")) + .and() + .body(not(containsString("CamelJdbcRowCount"))) + .and() + .body(not(containsString("CamelJdbcColumnNames"))) + .and() + .body(not(containsString("CamelJdbcParameters"))) + .and() + .body(not(containsString("CamelGeneratedColumns"))); + } + + @Test + void testHeadersFromSelectQuery() { + RestAssured.given() + .get("test/headers/select") + .then() + .body(not(containsString("CamelGeneratedKeysRowCount"))) + .and() + .body(not(containsString("CamelJdbcUpdateCount"))) + .and() + .body(not(containsString("CamelRetrieveGeneratedKeys"))) + .and() + .body(not(containsString("CamelJdbcParameters"))) + .and() + .body(not(containsString("CamelGeneratedColumns"))) + .and() + .body(containsString("CamelJdbcRowCount")) + .and() + .body(containsString("CamelJdbcColumnNames=[ID, SPECIES]")); + } + + @Test + void testNamedParameters() { + RestAssured.given() + .get("test/named-parameters/headers-as-parameters") + .then() + .body(containsString("{ID=1, SPECIES=Camelus dromedarius}")) + .and() + .body(containsString("{ID=2, SPECIES=Camelus bactrianus}")); + } + + @Test + void testCamelJdbcParametersHeader() { + RestAssured.given() + .get("test/named-parameters/headers-as-parameters-map") + .then() + .body(containsString("{ID=2, SPECIES=Camelus bactrianus}")); + } + + @Test + void testTimeIntervalDatabasePolling() { + String selectResult = RestAssured.given() + .contentType(ContentType.TEXT).body("select * from camelsGenerated order by id desc") + .post("/test/execute") + .then().extract().body().asString(); + + RestAssured.given() + .body(selectResult) + .get("/test/interval-polling") + .then() + .statusCode(204); + } + + @Test + void testMoveDataBetweenDatasources() { + String camelsDbResult = RestAssured.given() + .contentType(ContentType.TEXT).body("select * from camels order by id desc") + .post("/test/execute") + .then().extract().body().asString(); + + RestAssured.given() + .post("test/move-between-datasources"); + + RestAssured.given() + .contentType(ContentType.TEXT).body("select * from camelsProcessed order by id desc") + .post("/test/execute") + .then() + .body(equalTo(camelsDbResult)); + } + }
