This is an automated email from the ASF dual-hosted git repository.

lburgazzoli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git


The following commit(s) were added to refs/heads/master by this push:
     new 692b3e6  Use RestAssured in JDBC test #42
692b3e6 is described below

commit 692b3e66aef9bf791d9d93351fae1eb0b1962352
Author: lburgazzoli <lburgazz...@gmail.com>
AuthorDate: Mon Jul 8 20:21:20 2019 +0200

    Use RestAssured in JDBC test #42
---
 integration-tests/jdbc/pom.xml                     |  4 ++
 .../{CamelLifecycle.java => CamelResource.java}    | 53 +++++++++++++---------
 .../java/io/quarkus/it/camel/jdbc/CamelRoute.java  | 15 ++----
 .../jdbc/src/main/resources/application.properties | 10 ++--
 .../io/quarkus/it/camel/jdbc/CamelJdbcTest.java    | 49 ++++++--------------
 5 files changed, 62 insertions(+), 69 deletions(-)

diff --git a/integration-tests/jdbc/pom.xml b/integration-tests/jdbc/pom.xml
index 32a6435..9043bd6 100644
--- a/integration-tests/jdbc/pom.xml
+++ b/integration-tests/jdbc/pom.xml
@@ -39,6 +39,10 @@
         </dependency>
         <dependency>
             <groupId>io.quarkus</groupId>
+            <artifactId>quarkus-resteasy</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>io.quarkus</groupId>
             <artifactId>quarkus-jdbc-h2</artifactId>
         </dependency>
 
diff --git 
a/integration-tests/jdbc/src/main/java/io/quarkus/it/camel/jdbc/CamelLifecycle.java
 
b/integration-tests/jdbc/src/main/java/io/quarkus/it/camel/jdbc/CamelResource.java
similarity index 61%
rename from 
integration-tests/jdbc/src/main/java/io/quarkus/it/camel/jdbc/CamelLifecycle.java
rename to 
integration-tests/jdbc/src/main/java/io/quarkus/it/camel/jdbc/CamelResource.java
index e5e3b18..b84b919 100644
--- 
a/integration-tests/jdbc/src/main/java/io/quarkus/it/camel/jdbc/CamelLifecycle.java
+++ 
b/integration-tests/jdbc/src/main/java/io/quarkus/it/camel/jdbc/CamelResource.java
@@ -19,35 +19,33 @@ package io.quarkus.it.camel.jdbc;
 import java.sql.Connection;
 import java.sql.SQLException;
 import java.sql.Statement;
-
+import javax.annotation.PostConstruct;
 import javax.enterprise.context.ApplicationScoped;
-import javax.enterprise.event.Observes;
 import javax.inject.Inject;
-import javax.sql.DataSource;
+import javax.ws.rs.Consumes;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
 
-import org.apache.camel.quarkus.core.runtime.CamelRuntime;
-import org.apache.camel.quarkus.core.runtime.InitializingEvent;
-import org.apache.camel.quarkus.core.runtime.StartingEvent;
-import org.jboss.logging.Logger;
+import io.agroal.api.AgroalDataSource;
+import io.quarkus.agroal.DataSource;
+import org.apache.camel.ProducerTemplate;
 
+@Path("/test")
 @ApplicationScoped
-public class CamelLifecycle {
-
-    private static final Logger log = Logger.getLogger(CamelLifecycle.class);
-
+public class CamelResource {
     @Inject
-    CamelRuntime runtime;
+    @DataSource("camel-ds")
+    AgroalDataSource dataSource;
 
     @Inject
-    DataSource dataSource;
-
-    public void initializing(@Observes InitializingEvent event) {
-        log.debug("Binding camelsDs");
-        runtime.getRegistry().bind("camelsDs", dataSource);
-    }
+    ProducerTemplate template;
 
-    public void starting(@Observes StartingEvent event) throws SQLException {
-        log.debug("Initializing camels table");
+    @PostConstruct
+    void postConstruct() throws SQLException {
         try (Connection con = dataSource.getConnection()) {
             try (Statement statement = con.createStatement()) {
                 try {
@@ -59,8 +57,21 @@ public class CamelLifecycle {
                 statement.execute("insert into camels (id, species) values (2, 
'Camelus bactrianus')");
                 statement.execute("insert into camels (id, species) values (3, 
'Camelus ferus')");
             }
-            log.info("Initialized camels table");
         }
     }
 
+    @Path("/species/{id}")
+    @GET
+    @Produces(MediaType.TEXT_PLAIN)
+    public String getSpeciesById(@PathParam("id") String id ) throws Exception 
{
+        return template.requestBody("direct:execute", "select species from 
camels where id = " + id, String.class);
+    }
+
+    @Path("/execute")
+    @POST
+    @Consumes(MediaType.TEXT_PLAIN)
+    @Produces(MediaType.TEXT_PLAIN)
+    public String executeStatement(String statement) throws Exception {
+        return template.requestBody("direct:execute", statement, String.class);
+    }
 }
diff --git 
a/integration-tests/jdbc/src/main/java/io/quarkus/it/camel/jdbc/CamelRoute.java 
b/integration-tests/jdbc/src/main/java/io/quarkus/it/camel/jdbc/CamelRoute.java
index a2255d4..da66741 100644
--- 
a/integration-tests/jdbc/src/main/java/io/quarkus/it/camel/jdbc/CamelRoute.java
+++ 
b/integration-tests/jdbc/src/main/java/io/quarkus/it/camel/jdbc/CamelRoute.java
@@ -16,20 +16,15 @@
  */
 package io.quarkus.it.camel.jdbc;
 
-import javax.enterprise.context.ApplicationScoped;
-
 import org.apache.camel.builder.RouteBuilder;
 
-@ApplicationScoped
-public class CamelRoute extends RouteBuilder {
 
+public class CamelRoute extends RouteBuilder {
     @Override
     public void configure() {
-        from("timer:jdbc?repeatCount=1")
-                .setBody(constant("select species from camels where id = 1"))
-                .to("jdbc:camelsDs")
-                .convertBodyTo(String.class)
-                .to("file:target?fileName=out.txt");
+        from("direct:execute")
+            .to("jdbc:camel-ds")
+            .to("log:jdbc-result?")
+            .convertBodyTo(String.class);
     }
-
 }
diff --git a/integration-tests/jdbc/src/main/resources/application.properties 
b/integration-tests/jdbc/src/main/resources/application.properties
index 433ec0e..be1cfcb 100644
--- a/integration-tests/jdbc/src/main/resources/application.properties
+++ b/integration-tests/jdbc/src/main/resources/application.properties
@@ -16,7 +16,9 @@
 ## ---------------------------------------------------------------------------
 quarkus.camel.disable-xml=true
 quarkus.camel.disable-jaxb=true
-quarkus.datasource.url=jdbc:h2:tcp://localhost/mem:test
-quarkus.datasource.driver=org.h2.Driver
-quarkus.datasource.max-size=8
-quarkus.datasource.min-size=2
\ No newline at end of file
+quarkus.camel.dump-routes=true
+
+quarkus.datasource.camel-ds.url=jdbc:h2:tcp://localhost/mem:test
+quarkus.datasource.camel-ds.driver=org.h2.Driver
+quarkus.datasource.camel-ds.max-size=8
+quarkus.datasource.camel-ds.min-size=2
\ No newline at end of file
diff --git 
a/integration-tests/jdbc/src/test/java/io/quarkus/it/camel/jdbc/CamelJdbcTest.java
 
b/integration-tests/jdbc/src/test/java/io/quarkus/it/camel/jdbc/CamelJdbcTest.java
index b59852f..3c7410d 100644
--- 
a/integration-tests/jdbc/src/test/java/io/quarkus/it/camel/jdbc/CamelJdbcTest.java
+++ 
b/integration-tests/jdbc/src/test/java/io/quarkus/it/camel/jdbc/CamelJdbcTest.java
@@ -16,46 +16,27 @@
  */
 package io.quarkus.it.camel.jdbc;
 
-import java.nio.charset.StandardCharsets;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.nio.file.Paths;
-
+import io.quarkus.test.junit.QuarkusTest;
+import io.restassured.RestAssured;
+import io.restassured.http.ContentType;
 import org.junit.jupiter.api.Test;
 
-import io.quarkus.test.junit.QuarkusTest;
+import static org.hamcrest.Matchers.is;
 
 @QuarkusTest
 public class CamelJdbcTest {
 
     @Test
-    public void selectToFile() throws Throwable {
-        final long timeoutMs = 10000;
-        final long deadline = System.currentTimeMillis() + timeoutMs;
-        final Path outCsv = Paths.get("target/out.txt");
-        Throwable lastException = null;
-        final String expectedCsv = "[{SPECIES=Camelus dromedarius}]";
-        while (System.currentTimeMillis() <= deadline) {
-            try {
-                Thread.sleep(100);
-                if (!Files.exists(outCsv)) {
-                    lastException = new AssertionError(String.format("%s does 
not exist", outCsv));
-                } else {
-                    final String actual = new 
String(Files.readAllBytes(outCsv), StandardCharsets.UTF_8);
-                    if (expectedCsv.equals(actual)) {
-                        /* Test passed */
-                        return;
-                    } else {
-                        lastException = new 
AssertionError(String.format("expected: <%s> but was: <%s>", expectedCsv, 
actual));
-                    }
-                }
-            } catch (InterruptedException e) {
-                Thread.currentThread().interrupt();
-                throw e;
-            } catch (Exception e) {
-                lastException = e;
-            }
-        }
-        throw lastException;
+    void testGetSpeciesById() {
+        
RestAssured.when().get("/test/species/1").then().body(is("[{SPECIES=Camelus 
dromedarius}]"));
+        
RestAssured.when().get("/test/species/2").then().body(is("[{SPECIES=Camelus 
bactrianus}]"));
+        
RestAssured.when().get("/test/species/3").then().body(is("[{SPECIES=Camelus 
ferus}]"));
+    }
+    @Test
+    void testExecuteStatement() {
+        RestAssured.given()
+            .contentType(ContentType.TEXT).body("select id from camels order 
by id desc")
+            .post("/test/execute")
+            .then().body(is("[{ID=3}, {ID=2}, {ID=1}]"));
     }
 }

Reply via email to