This is an automated email from the ASF dual-hosted git repository.
aldettinger pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git
The following commit(s) were added to refs/heads/main by this push:
new 6f54d09c5c Ref #4596: Create jdbc tests for generated keys and other
headers
6f54d09c5c is described below
commit 6f54d09c5c6a344c69495138cffc30935dd86a12
Author: Lucia Drozdova <[email protected]>
AuthorDate: Mon Mar 13 09:09:55 2023 +0100
Ref #4596: Create jdbc tests for generated keys and other headers
---
integration-tests/jdbc/pom.xml | 11 +++-
.../quarkus/component/jdbc/CamelResource.java | 28 ++++++++++
.../camel/quarkus/component/jdbc/JdbcRoutes.java | 48 ++++++++++++++++
.../quarkus/component/jdbc/CamelJdbcTest.java | 65 ++++++++++++++++++++++
4 files changed, 151 insertions(+), 1 deletion(-)
diff --git a/integration-tests/jdbc/pom.xml b/integration-tests/jdbc/pom.xml
index d52f85c4a9..209c724663 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>
@@ -47,6 +48,14 @@
<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>
<!-- test dependencies -->
<dependency>
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 b54e0a7adf..84ead295cb 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
@@ -19,6 +19,7 @@ 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;
@@ -53,9 +54,11 @@ public class CamelResource {
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("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')");
@@ -109,4 +112,29 @@ 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);
+ }
}
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..843ef19fae
--- /dev/null
+++
b/integration-tests/jdbc/src/main/java/org/apache/camel/quarkus/component/jdbc/JdbcRoutes.java
@@ -0,0 +1,48 @@
+/*
+ * 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 jakarta.enterprise.context.ApplicationScoped;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+
+@ApplicationScoped
+public class JdbcRoutes extends RouteBuilder {
+ @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);
+ }
+ });
+ }
+}
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..45e7255c31 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,10 @@ 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.is;
+import static org.hamcrest.Matchers.not;
+import static org.junit.jupiter.api.Assertions.assertTrue;
@QuarkusTest
@QuarkusTestResource(H2DatabaseTestResource.class)
@@ -53,4 +58,64 @@ 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()
+ .contentType(ContentType.TEXT)
+ .body("insert into camelsGenerated (species) values ('Camelus
status'), ('Camelus linus')")
+ .post("/test/execute");
+
+ 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=2"))
+ .and()
+ .body(containsString("CamelJdbcColumnNames=[ID, SPECIES]"));
+ }
}