You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2020/03/29 14:19:41 UTC

[camel] branch master updated: Fixed test and allow to configure global influxdb on component level.

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 38a96ae  Fixed test and allow to configure global influxdb on component level.
38a96ae is described below

commit 38a96aeef8e9d474ae9cf67a8f42f5bc87ce79e7
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sun Mar 29 16:19:17 2020 +0200

    Fixed test and allow to configure global influxdb on component level.
---
 components/camel-influxdb/pom.xml                  |  2 +-
 .../influxdb/InfluxDbComponentConfigurer.java      |  5 +++
 .../apache/camel/component/influxdb/influxdb.json  |  1 +
 .../src/main/docs/influxdb-component.adoc          |  3 +-
 .../component/influxdb/InfluxDbComponent.java      | 22 ++++++++++
 .../camel/component/influxdb/InfluxDbEndpoint.java | 12 ------
 .../camel/component/influxdb/InfluxDbProducer.java | 17 --------
 .../converters/CamelInfluxDbConverters.java        | 22 +++-------
 .../component/influxdb/AbstractInfluxDbTest.java   | 19 +++++----
 .../influxdb/MockedInfluxDbConfiguration.java      | 49 ----------------------
 .../dsl/InfluxdbComponentBuilderFactory.java       | 12 ++++++
 11 files changed, 58 insertions(+), 106 deletions(-)

diff --git a/components/camel-influxdb/pom.xml b/components/camel-influxdb/pom.xml
index 541f403..50cc64a 100644
--- a/components/camel-influxdb/pom.xml
+++ b/components/camel-influxdb/pom.xml
@@ -61,7 +61,7 @@
         <!-- test dependencies -->
         <dependency>
             <groupId>org.apache.camel</groupId>
-            <artifactId>camel-test-spring</artifactId>
+            <artifactId>camel-test</artifactId>
             <scope>test</scope>
         </dependency>
         <dependency>
diff --git a/components/camel-influxdb/src/generated/java/org/apache/camel/component/influxdb/InfluxDbComponentConfigurer.java b/components/camel-influxdb/src/generated/java/org/apache/camel/component/influxdb/InfluxDbComponentConfigurer.java
index 39befc1..5da23d2 100644
--- a/components/camel-influxdb/src/generated/java/org/apache/camel/component/influxdb/InfluxDbComponentConfigurer.java
+++ b/components/camel-influxdb/src/generated/java/org/apache/camel/component/influxdb/InfluxDbComponentConfigurer.java
@@ -21,6 +21,8 @@ public class InfluxDbComponentConfigurer extends PropertyConfigurerSupport imple
         switch (ignoreCase ? name.toLowerCase() : name) {
         case "basicpropertybinding":
         case "basicPropertyBinding": target.setBasicPropertyBinding(property(camelContext, boolean.class, value)); return true;
+        case "influxdb":
+        case "influxDB": target.setInfluxDB(property(camelContext, org.influxdb.InfluxDB.class, value)); return true;
         case "lazystartproducer":
         case "lazyStartProducer": target.setLazyStartProducer(property(camelContext, boolean.class, value)); return true;
         default: return false;
@@ -31,6 +33,7 @@ public class InfluxDbComponentConfigurer extends PropertyConfigurerSupport imple
     public Map<String, Object> getAllOptions(Object target) {
         Map<String, Object> answer = new CaseInsensitiveMap();
         answer.put("basicPropertyBinding", boolean.class);
+        answer.put("influxDB", org.influxdb.InfluxDB.class);
         answer.put("lazyStartProducer", boolean.class);
         return answer;
     }
@@ -41,6 +44,8 @@ public class InfluxDbComponentConfigurer extends PropertyConfigurerSupport imple
         switch (ignoreCase ? name.toLowerCase() : name) {
         case "basicpropertybinding":
         case "basicPropertyBinding": return target.isBasicPropertyBinding();
+        case "influxdb":
+        case "influxDB": return target.getInfluxDB();
         case "lazystartproducer":
         case "lazyStartProducer": return target.isLazyStartProducer();
         default: return null;
diff --git a/components/camel-influxdb/src/generated/resources/org/apache/camel/component/influxdb/influxdb.json b/components/camel-influxdb/src/generated/resources/org/apache/camel/component/influxdb/influxdb.json
index 20bba0d..77b2bdd 100644
--- a/components/camel-influxdb/src/generated/resources/org/apache/camel/component/influxdb/influxdb.json
+++ b/components/camel-influxdb/src/generated/resources/org/apache/camel/component/influxdb/influxdb.json
@@ -19,6 +19,7 @@
     "version": "3.2.0-SNAPSHOT"
   },
   "componentProperties": {
+    "influxDB": { "kind": "property", "displayName": "Influx DB", "group": "producer", "required": false, "type": "object", "javaType": "org.influxdb.InfluxDB", "deprecated": false, "secret": false, "description": "The shared Influx DB to use for all endpoints" },
     "lazyStartProducer": { "kind": "property", "displayName": "Lazy Start Producer", "group": "producer", "label": "producer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "secret": false, "defaultValue": false, "description": "Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the r [...]
     "basicPropertyBinding": { "kind": "property", "displayName": "Basic Property Binding", "group": "advanced", "label": "advanced", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "secret": false, "defaultValue": false, "description": "Whether the component should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities" }
   },
diff --git a/components/camel-influxdb/src/main/docs/influxdb-component.adoc b/components/camel-influxdb/src/main/docs/influxdb-component.adoc
index 31bab88..4757d03 100644
--- a/components/camel-influxdb/src/main/docs/influxdb-component.adoc
+++ b/components/camel-influxdb/src/main/docs/influxdb-component.adoc
@@ -46,13 +46,14 @@ configured in the registry, using the native java driver.
 
 
 // component options: START
-The InfluxDB component supports 2 options, which are listed below.
+The InfluxDB component supports 3 options, which are listed below.
 
 
 
 [width="100%",cols="2,5,^1,2",options="header"]
 |===
 | Name | Description | Default | Type
+| *influxDB* (producer) | The shared Influx DB to use for all endpoints |  | InfluxDB
 | *lazyStartProducer* (producer) | Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed then creating and [...]
 | *basicPropertyBinding* (advanced) | Whether the component should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities | false | boolean
 |===
diff --git a/components/camel-influxdb/src/main/java/org/apache/camel/component/influxdb/InfluxDbComponent.java b/components/camel-influxdb/src/main/java/org/apache/camel/component/influxdb/InfluxDbComponent.java
index 8976233..f137a15 100644
--- a/components/camel-influxdb/src/main/java/org/apache/camel/component/influxdb/InfluxDbComponent.java
+++ b/components/camel-influxdb/src/main/java/org/apache/camel/component/influxdb/InfluxDbComponent.java
@@ -20,11 +20,15 @@ import java.util.Map;
 
 import org.apache.camel.Endpoint;
 import org.apache.camel.spi.annotations.Component;
+import org.apache.camel.support.CamelContextHelper;
 import org.apache.camel.support.DefaultComponent;
+import org.influxdb.InfluxDB;
 
 @Component("influxdb")
 public class InfluxDbComponent extends DefaultComponent {
 
+    private InfluxDB influxDB;
+
     public InfluxDbComponent() {
     }
 
@@ -32,6 +36,24 @@ public class InfluxDbComponent extends DefaultComponent {
     protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
         InfluxDbEndpoint endpoint = new InfluxDbEndpoint(uri, this);
         endpoint.setConnectionBean(remaining);
+        InfluxDB target = influxDB;
+        if (target == null) {
+            // if not using a shared db then lookup
+            target = CamelContextHelper.mandatoryLookup(getCamelContext(), remaining, InfluxDB.class);
+        }
+        endpoint.setInfluxDB(target);
+        setProperties(endpoint, parameters);
         return endpoint;
     }
+
+    public InfluxDB getInfluxDB() {
+        return influxDB;
+    }
+
+    /**
+     * The shared Influx DB to use for all endpoints
+     */
+    public void setInfluxDB(InfluxDB influxDB) {
+        this.influxDB = influxDB;
+    }
 }
diff --git a/components/camel-influxdb/src/main/java/org/apache/camel/component/influxdb/InfluxDbEndpoint.java b/components/camel-influxdb/src/main/java/org/apache/camel/component/influxdb/InfluxDbEndpoint.java
index cb69a11..1de308b 100644
--- a/components/camel-influxdb/src/main/java/org/apache/camel/component/influxdb/InfluxDbEndpoint.java
+++ b/components/camel-influxdb/src/main/java/org/apache/camel/component/influxdb/InfluxDbEndpoint.java
@@ -67,18 +67,6 @@ public class InfluxDbEndpoint extends DefaultEndpoint {
         throw new UnsupportedOperationException("You cannot receive messages from this endpoint");
     }
     
-    @Override
-    protected void doStart() throws Exception {
-        influxDB = CamelContextHelper.mandatoryLookup(getCamelContext(), connectionBean, InfluxDB.class);
-        LOG.debug("Resolved the connection with the name {} as {}", connectionBean, influxDB);
-        super.doStart();  
-    }
-    
-    @Override
-    protected void doStop() throws Exception {
-        super.doStop();
-    }
-
     public InfluxDB getInfluxDB() {
         return influxDB;
     }
diff --git a/components/camel-influxdb/src/main/java/org/apache/camel/component/influxdb/InfluxDbProducer.java b/components/camel-influxdb/src/main/java/org/apache/camel/component/influxdb/InfluxDbProducer.java
index f5b47b0..7988f69 100644
--- a/components/camel-influxdb/src/main/java/org/apache/camel/component/influxdb/InfluxDbProducer.java
+++ b/components/camel-influxdb/src/main/java/org/apache/camel/component/influxdb/InfluxDbProducer.java
@@ -32,7 +32,6 @@ import org.slf4j.LoggerFactory;
 
 /**
  * Producer for the InfluxDB components
- *
  */
 public class InfluxDbProducer extends DefaultProducer {
 
@@ -43,11 +42,6 @@ public class InfluxDbProducer extends DefaultProducer {
 
     public InfluxDbProducer(InfluxDbEndpoint endpoint) {
         super(endpoint);
-
-        if (ObjectHelper.isEmpty(endpoint.getInfluxDB())) {
-            throw new IllegalArgumentException("Can't create a producer when the database connection is null");
-        }
-
         this.connection = endpoint.getInfluxDB();
         this.endpoint = endpoint;
     }
@@ -60,7 +54,6 @@ public class InfluxDbProducer extends DefaultProducer {
      */
     @Override
     public void process(Exchange exchange) throws Exception {
-
         String dataBaseName = calculateDatabaseName(exchange);
         String retentionPolicy = calculateRetentionPolicy(exchange);
         switch (endpoint.getOperation()) {
@@ -81,10 +74,8 @@ public class InfluxDbProducer extends DefaultProducer {
     private void doInsert(Exchange exchange, String dataBaseName, String retentionPolicy) throws InvalidPayloadException {
         if (!endpoint.isBatch()) {
             Point p = exchange.getIn().getMandatoryBody(Point.class);
-
             try {
                 LOG.debug("Writing point {}", p.lineProtocol());
-
                 if (!connection.databaseExists(dataBaseName)) {
                     LOG.debug("Database {} doesn't exist. Creating it...", dataBaseName);
                     connection.createDatabase(dataBaseName);
@@ -95,10 +86,8 @@ public class InfluxDbProducer extends DefaultProducer {
             }
         } else {
             BatchPoints batchPoints = exchange.getIn().getMandatoryBody(BatchPoints.class);
-
             try {
                 LOG.debug("Writing BatchPoints {}", batchPoints.lineProtocol());
-
                 connection.write(batchPoints);
             } catch (Exception ex) {
                 exchange.setException(new CamelInfluxDbException(ex));
@@ -122,33 +111,27 @@ public class InfluxDbProducer extends DefaultProducer {
 
     private String calculateRetentionPolicy(Exchange exchange) {
         String retentionPolicy = exchange.getIn().getHeader(InfluxDbConstants.RETENTION_POLICY_HEADER, String.class);
-
         if (ObjectHelper.isNotEmpty(retentionPolicy)) {
             return retentionPolicy;
         }
-
         return endpoint.getRetentionPolicy();
     }
 
     private String calculateDatabaseName(Exchange exchange) {
         String dbName = exchange.getIn().getHeader(InfluxDbConstants.DBNAME_HEADER, String.class);
-
         if (ObjectHelper.isNotEmpty(dbName)) {
             return dbName;
         }
-
         return endpoint.getDatabaseName();
     }
 
     private String calculateQuery(Exchange exchange) {
         String query = exchange.getIn().getHeader(InfluxDbConstants.INFLUXDB_QUERY, String.class);
-
         if (ObjectHelper.isNotEmpty(query)) {
             return query;
         } else {
             query = endpoint.getQuery();
         }
-
         if (ObjectHelper.isEmpty(query)) {
             throw new IllegalArgumentException("The query option must be set if you want to run a query operation");
         }
diff --git a/components/camel-influxdb/src/main/java/org/apache/camel/component/influxdb/converters/CamelInfluxDbConverters.java b/components/camel-influxdb/src/main/java/org/apache/camel/component/influxdb/converters/CamelInfluxDbConverters.java
index 2d60d2d..0a1f84d 100644
--- a/components/camel-influxdb/src/main/java/org/apache/camel/component/influxdb/converters/CamelInfluxDbConverters.java
+++ b/components/camel-influxdb/src/main/java/org/apache/camel/component/influxdb/converters/CamelInfluxDbConverters.java
@@ -24,34 +24,22 @@ import org.apache.camel.component.influxdb.InfluxDbConstants;
 import org.influxdb.dto.Point;
 
 @Converter(generateLoader = true)
-public final class CamelInfluxDbConverters {
-
-    private CamelInfluxDbConverters() {
-
-    }
+public class CamelInfluxDbConverters {
 
     @Converter
     public static Point fromMapToPoint(Map<String, Object> map) {
-
-        Object measurenmentName = map.get(InfluxDbConstants.MEASUREMENT_NAME);
-
-        if (measurenmentName == null) {
+        Object measurementName = map.get(InfluxDbConstants.MEASUREMENT_NAME);
+        if (measurementName == null) {
             String format = String.format("Unable to find the header for the measurement in:" + map.keySet().toString());
             throw new CamelInfluxDbException(format);
         }
 
-
-        String measurenmentNameString = measurenmentName.toString();
+        String measurenmentNameString = measurementName.toString();
         Point.Builder pointBuilder = Point.measurement(measurenmentNameString);
-
         map.remove(InfluxDbConstants.MEASUREMENT_NAME);
-
         pointBuilder.fields(map);
-        map.put(InfluxDbConstants.MEASUREMENT_NAME, measurenmentName);
-
-
+        map.put(InfluxDbConstants.MEASUREMENT_NAME, measurementName);
         return pointBuilder.build();
-
     }
 
 
diff --git a/components/camel-influxdb/src/test/java/org/apache/camel/component/influxdb/AbstractInfluxDbTest.java b/components/camel-influxdb/src/test/java/org/apache/camel/component/influxdb/AbstractInfluxDbTest.java
index 9dec4d2..dc08b4c 100644
--- a/components/camel-influxdb/src/test/java/org/apache/camel/component/influxdb/AbstractInfluxDbTest.java
+++ b/components/camel-influxdb/src/test/java/org/apache/camel/component/influxdb/AbstractInfluxDbTest.java
@@ -17,22 +17,23 @@
 package org.apache.camel.component.influxdb;
 
 import org.apache.camel.CamelContext;
-import org.apache.camel.spring.SpringCamelContext;
 import org.apache.camel.test.junit4.CamelTestSupport;
-import org.springframework.context.ApplicationContext;
-import org.springframework.context.annotation.AnnotationConfigApplicationContext;
+import org.influxdb.InfluxDB;
+import org.influxdb.impl.InfluxDBImpl;
+
+
+import static org.mockito.Mockito.mock;
 
 public class AbstractInfluxDbTest extends CamelTestSupport {
 
-    protected ApplicationContext applicationContext;
+    private InfluxDB mockedDbConnection = mock(InfluxDBImpl.class);
 
     @Override
     protected CamelContext createCamelContext() throws Exception {
-        applicationContext = new AnnotationConfigApplicationContext(MockedInfluxDbConfiguration.class);
-        CamelContext ctx = new SpringCamelContext(applicationContext);
-        ctx.init();
-        ctx.getPropertiesComponent().setLocation("classpath:influxdb.test.properties");
-        return ctx;
+        CamelContext context = super.createCamelContext();
+        context.getRegistry().bind("influxDbBean", mockedDbConnection);
+        context.getPropertiesComponent().setLocation("classpath:influxdb.test.properties");
+        return context;
     }
 
 }
diff --git a/components/camel-influxdb/src/test/java/org/apache/camel/component/influxdb/MockedInfluxDbConfiguration.java b/components/camel-influxdb/src/test/java/org/apache/camel/component/influxdb/MockedInfluxDbConfiguration.java
deleted file mode 100644
index 83dafb1..0000000
--- a/components/camel-influxdb/src/test/java/org/apache/camel/component/influxdb/MockedInfluxDbConfiguration.java
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * 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.component.influxdb;
-
-import java.net.UnknownHostException;
-
-import org.influxdb.InfluxDB;
-import org.influxdb.impl.InfluxDBImpl;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-
-import static junit.framework.TestCase.assertNotNull;
-import static org.mockito.Mockito.*;
-
-@Configuration
-public class MockedInfluxDbConfiguration {
-
-    private static final Logger LOG = LoggerFactory.getLogger(MockedInfluxDbConfiguration.class);
-
-    @Bean
-    public InfluxDB influxDbBean() throws UnknownHostException {
-
-        if (LOG.isDebugEnabled()) {
-            LOG.debug("Creating new instance of a mocked influx db connection");
-        }
-        InfluxDB mockedDbConnection = mock(InfluxDBImpl.class);
-        //InfluxDB mockedDbConnection = InfluxDBFactory.connect("http://127.0.0.1:8086", "root", "root");
-        assertNotNull(mockedDbConnection);
-        return mockedDbConnection;
-    }
-
-
-}
diff --git a/core/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/InfluxdbComponentBuilderFactory.java b/core/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/InfluxdbComponentBuilderFactory.java
index e1fb6f2..27f0cef 100644
--- a/core/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/InfluxdbComponentBuilderFactory.java
+++ b/core/camel-componentdsl/src/generated/java/org/apache/camel/builder/component/dsl/InfluxdbComponentBuilderFactory.java
@@ -51,6 +51,17 @@ public interface InfluxdbComponentBuilderFactory {
             extends
                 ComponentBuilder<InfluxDbComponent> {
         /**
+         * The shared Influx DB to use for all endpoints.
+         * 
+         * The option is a: <code>org.influxdb.InfluxDB</code> type.
+         * 
+         * Group: producer
+         */
+        default InfluxdbComponentBuilder influxDB(org.influxdb.InfluxDB influxDB) {
+            doSetProperty("influxDB", influxDB);
+            return this;
+        }
+        /**
          * Whether the producer should be started lazy (on the first message).
          * By starting lazy you can use this to allow CamelContext and routes to
          * startup in situations where a producer may otherwise fail during
@@ -102,6 +113,7 @@ public interface InfluxdbComponentBuilderFactory {
                 String name,
                 Object value) {
             switch (name) {
+            case "influxDB": ((InfluxDbComponent) component).setInfluxDB((org.influxdb.InfluxDB) value); return true;
             case "lazyStartProducer": ((InfluxDbComponent) component).setLazyStartProducer((boolean) value); return true;
             case "basicPropertyBinding": ((InfluxDbComponent) component).setBasicPropertyBinding((boolean) value); return true;
             default: return false;