ppalaga commented on a change in pull request #2694: URL: https://github.com/apache/camel-quarkus/pull/2694#discussion_r642444486
########## File path: integration-tests/sql/src/main/java/org/apache/camel/quarkus/component/sql/it/SqlRoutes.java ########## @@ -0,0 +1,148 @@ +/* + * 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.sql.it; + +import java.io.*; +import java.nio.file.Path; +import java.sql.SQLException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CopyOnWriteArrayList; + +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.inject.Produces; +import javax.inject.Inject; +import javax.inject.Named; +import javax.transaction.TransactionManager; +import javax.transaction.UserTransaction; + +import io.agroal.api.AgroalDataSource; +import org.apache.camel.AggregationStrategy; +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.processor.aggregate.jdbc.JdbcAggregationRepository; +import org.apache.camel.processor.idempotent.jdbc.JdbcMessageIdRepository; +import org.springframework.transaction.jta.JtaTransactionManager; + +@ApplicationScoped +public class SqlRoutes extends RouteBuilder { + + @Inject + @Named("results") + Map<String, List> results; + + @Inject + TransactionManager tm; + + @Inject + UserTransaction userTransaction; + + @Inject + AgroalDataSource dataSource; + + @Inject + SqlDbInitializer sqlDbInitializer; + + @Override + public void configure() throws IOException, SQLException { + //db has to be initialized before routes are started + sqlDbInitializer.initDb(); + + from("sql:select * from projects where processed = false order by id?initialDelay=0&delay=50&consumer.onConsume=update projects set processed = true where id = :#id") + .id("consumerRoute").autoStartup(false) + .process(e -> results.get("consumerRoute").add(e.getMessage().getBody(Map.class))); + + from("sql:classpath:sql/selectProjects.sql?initialDelay=0&delay=50&consumer.onConsume=update projects set processed = true") + .id("consumerClasspathRoute").autoStartup(false) + .process(e -> results.get("consumerClasspathRoute").add(e.getMessage().getBody(Map.class))); + + Path tmpFile = createTmpFileFrom("sql/selectProjects.sql"); + from("sql:file:" + tmpFile + + "?initialDelay=0&delay=50&consumer.onConsume=update projects set processed = true") + .id("consumerFileRoute").autoStartup(false) + .process(e -> results.get("consumerFileRoute").add(e.getMessage().getBody(Map.class))); + + from("direct:transacted") + .transacted("PROPAGATION_REQUIRED") + .to("sql:overriddenByTheHeader") + .process(e -> { + if (e.getIn().getHeader("rollback", boolean.class)) { + throw new Exception("forced Exception"); + } + }); + + // Idempotent Repository + JdbcMessageIdRepository repo = new JdbcMessageIdRepository(dataSource, "idempotentRepo"); + from("direct:idempotent") + .idempotentConsumer(header("messageId"), repo) + .process(e -> results.get("idempotentRoute").add(e.getMessage().getBody(String.class))); + + //aggregation repository + JdbcAggregationRepository aggregationRepo = new JdbcAggregationRepository( + new JtaTransactionManager(userTransaction, tm), "aggregation", dataSource); + from("direct:aggregation") + .aggregate(header("messageId"), new MyAggregationStrategy()) + // use our created jdbc repo as aggregation repository + .completionSize(4).aggregationRepository(aggregationRepo) + .process(e -> results.get("aggregationRoute").add(e.getMessage().getBody(String.class))); + + } + + private Path createTmpFileFrom(String file) throws IOException { + File tmpFile = File.createTempFile("selectProjects-", ".sql"); + tmpFile.deleteOnExit(); + try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(file)) { //todo quick workaround + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + int c; + while ((c = is.read()) >= 0) { + out.write(c); + } + new FileOutputStream(tmpFile).write(out.toByteArray()); Review comment: Not closing a FileOutputStream may lead to surprising reads and perhaps also file being locked on Windows. `java.nio.file.Files.write(Path, byte[], OpenOption...)` might be a good replacement for this line, or even better the whole try block could be replaced by `java.nio.file.Files.copy(InputStream, Path, CopyOption...)` ########## File path: integration-tests/sql/src/test/java/org/apache/camel/quarkus/component/sql/it/SqlTest.java ########## @@ -66,4 +78,169 @@ public void testSqlStoredComponent() { .statusCode(200) .body(is("15")); } + + @Test + public void testConsumer() throws InterruptedException { + testConsumer(1, "consumerRoute"); + } + + @Test + public void testClasspathConsumer() throws InterruptedException { + testConsumer(2, "consumerClasspathRoute"); + } + + @Test + public void testFileConsumer() throws InterruptedException { + testConsumer(3, "consumerFileRoute"); + } + + private void testConsumer(int id, String routeId) throws InterruptedException { + route(routeId, "start", "Started"); + + Map project = CollectionHelper.mapOf("ID", id, "PROJECT", routeId, "LICENSE", "222", "PROCESSED", false); + Map updatedProject = CollectionHelper.mapOf("ID", id, "PROJECT", routeId, "LICENSE", "XXX", "PROCESSED", false); + + postMapWithParam("/sql/insert", + "table", "projects", + project) + .statusCode(201); + + //wait for the record to be caught + Thread.sleep(500); Review comment: Could we please await? 500 ms might be too much on a laptop and too little on a CI. ########## File path: integration-tests/sql/src/test/java/org/apache/camel/quarkus/component/sql/it/SqlTest.java ########## @@ -66,4 +78,169 @@ public void testSqlStoredComponent() { .statusCode(200) .body(is("15")); } + + @Test + public void testConsumer() throws InterruptedException { + testConsumer(1, "consumerRoute"); + } + + @Test + public void testClasspathConsumer() throws InterruptedException { + testConsumer(2, "consumerClasspathRoute"); + } + + @Test + public void testFileConsumer() throws InterruptedException { + testConsumer(3, "consumerFileRoute"); + } + + private void testConsumer(int id, String routeId) throws InterruptedException { + route(routeId, "start", "Started"); + + Map project = CollectionHelper.mapOf("ID", id, "PROJECT", routeId, "LICENSE", "222", "PROCESSED", false); + Map updatedProject = CollectionHelper.mapOf("ID", id, "PROJECT", routeId, "LICENSE", "XXX", "PROCESSED", false); + + postMapWithParam("/sql/insert", + "table", "projects", + project) + .statusCode(201); + + //wait for the record to be caught + Thread.sleep(500); + + RestAssured.get("/sql/get/results/" + routeId) + .then() + .statusCode(200) + .body("size()", is(1), "$", hasItem(project)); + + //update + postMapWithParam("/sql/update", + "table", "projects", + updatedProject) + .statusCode(201); + + Thread.sleep(500); Review comment: Could we please await? 500 ms might be too much on a laptop and too little on a CI. ########## File path: integration-tests/sql/src/test/java/org/apache/camel/quarkus/component/sql/it/SqlTest.java ########## @@ -66,4 +78,169 @@ public void testSqlStoredComponent() { .statusCode(200) .body(is("15")); } + + @Test + public void testConsumer() throws InterruptedException { + testConsumer(1, "consumerRoute"); + } + + @Test + public void testClasspathConsumer() throws InterruptedException { + testConsumer(2, "consumerClasspathRoute"); + } + + @Test + public void testFileConsumer() throws InterruptedException { + testConsumer(3, "consumerFileRoute"); + } + + private void testConsumer(int id, String routeId) throws InterruptedException { + route(routeId, "start", "Started"); + + Map project = CollectionHelper.mapOf("ID", id, "PROJECT", routeId, "LICENSE", "222", "PROCESSED", false); + Map updatedProject = CollectionHelper.mapOf("ID", id, "PROJECT", routeId, "LICENSE", "XXX", "PROCESSED", false); + + postMapWithParam("/sql/insert", + "table", "projects", + project) + .statusCode(201); + + //wait for the record to be caught + Thread.sleep(500); + + RestAssured.get("/sql/get/results/" + routeId) + .then() + .statusCode(200) + .body("size()", is(1), "$", hasItem(project)); + + //update + postMapWithParam("/sql/update", + "table", "projects", + updatedProject) + .statusCode(201); + + Thread.sleep(500); + + RestAssured.get("/sql/get/results/" + routeId) + .then() + .statusCode(200) + .body("size()", is(1), "$", hasItem(updatedProject)); + + route(routeId, "stop", "Stopped"); + } + + @Test + public void testTransacted() throws InterruptedException { + + postMap("/sql/toDirect/transacted", CollectionHelper.mapOf(SqlConstants.SQL_QUERY, + "insert into projects values (5, 'Transacted', 'ASF', false)", + "rollback", false)) + .statusCode(204); + + postMap("/sql/toDirect/transacted", CollectionHelper.mapOf(SqlConstants.SQL_QUERY, + "select * from projects where project = 'Transacted'")) + .statusCode(200) + .body("size()", is(1)); + + postMap("/sql/toDirect/transacted", CollectionHelper.mapOf(SqlConstants.SQL_QUERY, + "insert into projects values (6, 'Transacted', 'ASF', false)", + "rollback", true)) + .statusCode(200) + .body(is("java.lang.Exception:forced Exception")); + + postMap("/sql/toDirect/transacted", + CollectionHelper.mapOf(SqlConstants.SQL_QUERY, "select * from projects where project = 'Transacted'")) + .statusCode(200) + .body("size()", is(1)); + } + + @Test + public void testDefaultErrorCode() throws InterruptedException { + postMap("/sql/toDirect/transacted", CollectionHelper.mapOf(SqlConstants.SQL_QUERY, "select * from NOT_EXIST order id")) + .statusCode(200) + .body(startsWith("org.springframework.jdbc.BadSqlGrammarException")); + } + + @Test + public void testIdempotentRepository() { + // add value with key 1 + postMapWithParam("/sql/toDirect/idempotent", + "body", "one", + CollectionHelper.mapOf("messageId", "1")) + .statusCode(200); + + // add value with key 2 + postMapWithParam("/sql/toDirect/idempotent", + "body", "two", + CollectionHelper.mapOf("messageId", "2")) + .statusCode(200); + + // add same value with key 3 + postMapWithParam("/sql/toDirect/idempotent", + "body", "three", + CollectionHelper.mapOf("messageId", "3")) + .statusCode(200); + + // add another value with key 1 -- this one is supposed to be skipped + postMapWithParam("/sql/toDirect/idempotent", + "body", "four", + CollectionHelper.mapOf("messageId", "1")) + .statusCode(200); + + // get all values from the result map + await().atMost(5, TimeUnit.SECONDS).until(() -> (Iterable<? extends String>) RestAssured + .get("/sql/get/results/idempotentRoute").then().extract().as(List.class), + containsInAnyOrder("one", "two", "three")); + } + + @Test + @Disabled //see https://github.com/apache/camel-quarkus/issues/2693 + public void testAggregationRepository() { + postMapWithParam("/sql/toDirect/aggregation", "body", "A", CollectionHelper.mapOf("messageId", "123")) + .statusCode(200); + + postMapWithParam("/sql/toDirect/aggregation", "body", "B", CollectionHelper.mapOf("messageId", "123")) + .statusCode(200); + + postMapWithParam("/sql/toDirect/aggregation", "body", "C", CollectionHelper.mapOf("messageId", "123")) + .statusCode(200); + + postMapWithParam("/sql/toDirect/aggregation", "body", "D", CollectionHelper.mapOf("messageId", "123")) + .statusCode(200); Review comment: Not packing the RestAssured code into a method would make it easier to understand what this actually does. Maybe it is just me? What do others think? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
