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 1e29555  integration-tests: make influxdb tests more idiomatic
     new 5c57faa  Merge pull request #1346 from lburgazzoli/influxdb
1e29555 is described below

commit 1e29555032fb10e5dfe6c53afa1f0bda69087fdf
Author: lburgazzoli <[email protected]>
AuthorDate: Fri Jun 12 23:13:31 2020 +0200

    integration-tests: make influxdb tests more idiomatic
---
 .../component/influxdb/it/InfluxdbResource.java    | 98 ++++++++++------------
 .../component/influxdb/it/InfluxdbTest.java        | 22 ++---
 .../influxdb/it/InfluxdbTestResource.java          | 15 ++--
 3 files changed, 59 insertions(+), 76 deletions(-)

diff --git 
a/integration-tests/influxdb/src/main/java/org/apache/camel/quarkus/component/influxdb/it/InfluxdbResource.java
 
b/integration-tests/influxdb/src/main/java/org/apache/camel/quarkus/component/influxdb/it/InfluxdbResource.java
index ad53101..02fea17 100644
--- 
a/integration-tests/influxdb/src/main/java/org/apache/camel/quarkus/component/influxdb/it/InfluxdbResource.java
+++ 
b/integration-tests/influxdb/src/main/java/org/apache/camel/quarkus/component/influxdb/it/InfluxdbResource.java
@@ -16,12 +16,12 @@
  */
 package org.apache.camel.quarkus.component.influxdb.it;
 
-import java.util.List;
 import java.util.stream.Collectors;
 
 import javax.enterprise.context.ApplicationScoped;
-import javax.enterprise.event.Observes;
+import javax.enterprise.inject.Disposes;
 import javax.inject.Inject;
+import javax.inject.Singleton;
 import javax.ws.rs.Consumes;
 import javax.ws.rs.GET;
 import javax.ws.rs.POST;
@@ -29,63 +29,53 @@ import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
 import javax.ws.rs.core.MediaType;
 
-import org.apache.camel.CamelContext;
-import org.apache.camel.Exchange;
-import org.apache.camel.ProducerTemplate;
+import io.quarkus.arc.Unremovable;
+import org.apache.camel.FluentProducerTemplate;
 import org.apache.camel.component.influxdb.InfluxDbConstants;
+import org.eclipse.microprofile.config.inject.ConfigProperty;
 import org.influxdb.InfluxDB;
 import org.influxdb.InfluxDBFactory;
 import org.influxdb.dto.BatchPoints;
 import org.influxdb.dto.Pong;
 import org.influxdb.dto.Query;
 import org.influxdb.dto.QueryResult;
-import org.jboss.logging.Logger;
 
 @Path("/influxdb")
 @ApplicationScoped
 public class InfluxdbResource {
-
-    private static final Logger LOG = Logger.getLogger(InfluxdbResource.class);
-
     public static final String DB_NAME = "myTestTimeSeries";
-
-    public static final String INFLUXDB_CONNECTION_PROPERTY = 
"quarkus.influxdb.connection.url";
-    public static final String INFLUXDB_VERSION = "1.7.10";
-
-    private static final String INFLUXDB_CONNECTION = "http://{{"; + 
INFLUXDB_CONNECTION_PROPERTY + "}}/";
-    private static final String INFLUXDB_CONNECTION_NAME = 
"influxDb_connection";
-    private static final String INFLUXDB_ENDPOINT_URL = "influxdb:" + 
INFLUXDB_CONNECTION_NAME;
+    public static final String INFLUXDB_CONNECTION_PROPERTY = 
"influxdb.connection.url";
+    public static final String INFLUXDB_CONNECTION_NAME = "influxDbConnection";
 
     @Inject
-    ProducerTemplate producerTemplate;
-
-    @Inject
-    CamelContext context;
-
-    private InfluxDB influxDB;
+    FluentProducerTemplate producerTemplate;
 
-    void onStart(@Observes 
org.apache.camel.quarkus.core.CamelMainEvents.BeforeConfigure ev) {
-        influxDB = 
InfluxDBFactory.connect(context.getPropertiesComponent().parseUri(INFLUXDB_CONNECTION));
+    @ConfigProperty(name = INFLUXDB_CONNECTION_PROPERTY)
+    String connectionUrl;
 
-        influxDB.query(new Query("CREATE DATABASE " + DB_NAME));
+    @Unremovable
+    @Singleton
+    @javax.enterprise.inject.Produces
+    InfluxDB createInfluxDbConnection() {
+        InfluxDB influxDbConnection = InfluxDBFactory.connect(connectionUrl);
+        influxDbConnection.query(new Query("CREATE DATABASE " + DB_NAME));
 
-        context.getRegistry().bind(INFLUXDB_CONNECTION_NAME, influxDB);
+        return influxDbConnection;
     }
 
-    void beforeStop(@Observes 
org.apache.camel.quarkus.core.CamelMainEvents.BeforeStop ev) {
-        if (influxDB != null) {
-            influxDB.query(new Query("DROP DATABASE " + DB_NAME, ""));
-            influxDB.close();
-        }
+    void disposeInfluxDbConnection(@Disposes InfluxDB influxDbConnection) {
+        influxDbConnection.query(new Query("DROP DATABASE " + DB_NAME, ""));
+        influxDbConnection.close();
     }
 
     @Path("/ping")
     @GET
     @Produces(MediaType.TEXT_PLAIN)
     public String ping() {
-        Pong pong = producerTemplate.requestBody(INFLUXDB_ENDPOINT_URL + 
"?operation=ping", null, Pong.class);
-
-        return pong.getVersion();
+        return producerTemplate.toF(
+                "influxdb:%s?operation=ping", INFLUXDB_CONNECTION_NAME)
+                .request(Pong.class)
+                .getVersion();
     }
 
     @Path("/insert")
@@ -93,11 +83,10 @@ public class InfluxdbResource {
     @Consumes(MediaType.APPLICATION_JSON)
     @Produces(MediaType.TEXT_PLAIN)
     public boolean insert(Point point) {
-        org.influxdb.dto.Point p = point.toPoint();
-
-        org.influxdb.dto.Point result = producerTemplate.requestBody(
-                INFLUXDB_ENDPOINT_URL + "?databaseName=" + DB_NAME + 
"&operation=insert&retentionPolicy=autogen", p,
-                org.influxdb.dto.Point.class);
+        org.influxdb.dto.Point result = producerTemplate.toF(
+                
"influxdb:%s?databaseName=%s&operation=insert&retentionPolicy=autogen", 
INFLUXDB_CONNECTION_NAME, DB_NAME)
+                .withBody(point.toPoint())
+                .request(org.influxdb.dto.Point.class);
 
         return result != null;
     }
@@ -106,13 +95,12 @@ public class InfluxdbResource {
     @POST
     @Consumes(MediaType.APPLICATION_JSON)
     @Produces(MediaType.TEXT_PLAIN)
-    public String batch(Points points) {
-        BatchPoints p = points.toBatchPoints();
-
-        BatchPoints result = 
producerTemplate.requestBody(INFLUXDB_ENDPOINT_URL + "?batch=true", p,
-                BatchPoints.class);
-
-        return String.valueOf(result.getPoints().size());
+    public int batch(Points points) {
+        return producerTemplate.toF(
+                "influxdb:%s?batch=true", INFLUXDB_CONNECTION_NAME)
+                .withBody(points.toBatchPoints())
+                .request(BatchPoints.class)
+                .getPoints().size();
     }
 
     @Path("/query")
@@ -120,15 +108,15 @@ public class InfluxdbResource {
     @Consumes(MediaType.TEXT_PLAIN)
     @Produces(MediaType.TEXT_PLAIN)
     public String query(String query) throws Exception {
-        Exchange exchange = producerTemplate.request(
-                INFLUXDB_ENDPOINT_URL + "?databaseName=" + DB_NAME + 
"&operation=query&retentionPolicy=autogen",
-                e -> e.getIn().setHeader(InfluxDbConstants.INFLUXDB_QUERY, 
query));
-        List<QueryResult.Result> results = 
exchange.getMessage().getBody(QueryResult.class).getResults();
-        return results.stream()
-                .flatMap(r -> r.getSeries() != null ? r.getSeries().stream() : 
null)
-                .map(s -> s.getName())
+        QueryResult result = producerTemplate.toF(
+                
"influxdb:%s?databaseName=%s&operation=query&retentionPolicy=autogen", 
INFLUXDB_CONNECTION_NAME, DB_NAME)
+                .withHeader(InfluxDbConstants.INFLUXDB_QUERY, query)
+                .request(QueryResult.class);
+
+        return result.getResults().stream()
+                .filter(r -> r.getSeries() != null)
+                .flatMap(r -> r.getSeries().stream())
+                .map(QueryResult.Series::getName)
                 .collect(Collectors.joining(", "));
-
     }
-
 }
diff --git 
a/integration-tests/influxdb/src/test/java/org/apache/camel/quarkus/component/influxdb/it/InfluxdbTest.java
 
b/integration-tests/influxdb/src/test/java/org/apache/camel/quarkus/component/influxdb/it/InfluxdbTest.java
index 86190f7..9d035ab 100644
--- 
a/integration-tests/influxdb/src/test/java/org/apache/camel/quarkus/component/influxdb/it/InfluxdbTest.java
+++ 
b/integration-tests/influxdb/src/test/java/org/apache/camel/quarkus/component/influxdb/it/InfluxdbTest.java
@@ -31,8 +31,8 @@ import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.TestMethodOrder;
 
 import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
 
 @QuarkusTest
 @QuarkusTestResource(InfluxdbTestResource.class)
@@ -42,30 +42,27 @@ class InfluxdbTest {
     @Test
     @Order(1)
     public void pingTest() {
-        
RestAssured.given().get("/influxdb/ping").then().body(is(InfluxdbResource.INFLUXDB_VERSION));
+        
RestAssured.given().get("/influxdb/ping").then().body(is(InfluxdbTestResource.INFLUXDB_VERSION));
     }
 
     @Test
     @Order(2)
     public void insertTest() {
-
         Point point = createBatchPoints().getPoints().get(0);
-        RestAssured.given() //
+        RestAssured.given()
                 .contentType(ContentType.JSON)
                 .body(point)
                 .post("/influxdb/insert")
                 .then()
                 .statusCode(200)
                 .body(is("true"));
-
     }
 
     @Test
     @Order(3)
     public void batchInsertTest() {
-
         Points points = createBatchPoints();
-        RestAssured.given() //
+        RestAssured.given()
                 .contentType(ContentType.JSON)
                 .body(points)
                 .post("/influxdb/batch")
@@ -78,7 +75,7 @@ class InfluxdbTest {
     @Order(4)
     public void queryTest() {
         // result should contain only 1 result with name 'cpu', because 'cpu' 
is only part of batchInsert, which was executed before
-        RestAssured.given() //
+        RestAssured.given()
                 .contentType(ContentType.TEXT)
                 .body("select * from cpu")
                 .post("/influxdb/query")
@@ -92,8 +89,7 @@ class InfluxdbTest {
     public void doesNotAddCamelHeaders() {
         Map<String, Object> pointInMapFormat = new HashMap<>();
         pointInMapFormat.put(InfluxDbConstants.MEASUREMENT_NAME, "testCPU");
-        double value = 99.999999d;
-        pointInMapFormat.put("busy", value);
+        pointInMapFormat.put("busy", 99.999999d);
 
         org.influxdb.dto.Point p = 
CamelInfluxDbConverters.fromMapToPoint(pointInMapFormat);
         assertNotNull(p);
@@ -101,12 +97,10 @@ class InfluxdbTest {
         String line = p.lineProtocol();
 
         assertNotNull(line);
-
-        assertTrue(!line.contains(InfluxDbConstants.MEASUREMENT_NAME));
-
+        assertFalse(line.contains(InfluxDbConstants.MEASUREMENT_NAME));
     }
 
-    private Points createBatchPoints() {
+    private static Points createBatchPoints() {
         Points points = new Points();
         points.setDatabase(InfluxdbResource.DB_NAME);
 
diff --git 
a/integration-tests/influxdb/src/test/java/org/apache/camel/quarkus/component/influxdb/it/InfluxdbTestResource.java
 
b/integration-tests/influxdb/src/test/java/org/apache/camel/quarkus/component/influxdb/it/InfluxdbTestResource.java
index 710de83..d351335 100644
--- 
a/integration-tests/influxdb/src/test/java/org/apache/camel/quarkus/component/influxdb/it/InfluxdbTestResource.java
+++ 
b/integration-tests/influxdb/src/test/java/org/apache/camel/quarkus/component/influxdb/it/InfluxdbTestResource.java
@@ -20,6 +20,7 @@ package org.apache.camel.quarkus.component.influxdb.it;
 import java.util.Map;
 
 import 
org.apache.camel.quarkus.testcontainers.ContainerResourceLifecycleManager;
+import org.apache.camel.quarkus.testcontainers.ContainerSupport;
 import org.apache.camel.util.CollectionHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -28,19 +29,19 @@ import org.testcontainers.containers.wait.strategy.Wait;
 import org.testcontainers.utility.TestcontainersConfiguration;
 
 public class InfluxdbTestResource implements ContainerResourceLifecycleManager 
{
+    public static final Logger LOGGER = 
LoggerFactory.getLogger(InfluxdbTestResource.class);
+    public static final int INFLUXDB_PORT = 8086;
+    public static final String INFLUXDB_VERSION = "1.7.10";
+    public static final String INFLUXDB_IMAGE = "influxdb:" + INFLUXDB_VERSION;
 
-    private static final Logger LOGGER = 
LoggerFactory.getLogger(InfluxdbTestResource.class);
-    private static final int INFLUXDB_PORT = 8086;
-    private static final String INFLUXDB_IMAGE = "influxdb:" + 
InfluxdbResource.INFLUXDB_VERSION;
-
-    private GenericContainer container;
+    private GenericContainer<?> container;
 
     @Override
     public Map<String, String> start() {
         LOGGER.info(TestcontainersConfiguration.getInstance().toString());
 
         try {
-            container = new GenericContainer(INFLUXDB_IMAGE)
+            container = new GenericContainer<>(INFLUXDB_IMAGE)
                     .withExposedPorts(INFLUXDB_PORT)
                     .waitingFor(Wait.forListeningPort());
 
@@ -48,7 +49,7 @@ public class InfluxdbTestResource implements 
ContainerResourceLifecycleManager {
 
             return CollectionHelper.mapOf(
                     InfluxdbResource.INFLUXDB_CONNECTION_PROPERTY,
-                    container.getContainerIpAddress() + ":" + 
container.getMappedPort(INFLUXDB_PORT));
+                    "http://"; + ContainerSupport.getHostAndPort(container, 
INFLUXDB_PORT));
         } catch (Exception e) {
             throw new RuntimeException(e);
         }

Reply via email to