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

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

commit 0c65887e936fa87f7f3d27a1db0d90264d581df7
Author: Andrea Cosentino <anco...@gmail.com>
AuthorDate: Tue May 15 14:09:17 2018 +0200

    CAMEL-12512 - camel-consul - Option to inject Consul client
---
 .../component/consul/ConsulConfiguration.java      | 19 ++++-
 .../camel/component/consul/ConsulEndpoint.java     |  4 +-
 .../component/consul/ConsulClientKeyValueTest.java | 63 +++++++++++++++++
 .../component/consul/ConsulClientTestSupport.java  | 80 ++++++++++++++++++++++
 .../springboot/ConsulComponentConfiguration.java   | 14 ++++
 5 files changed, 178 insertions(+), 2 deletions(-)

diff --git 
a/components/camel-consul/src/main/java/org/apache/camel/component/consul/ConsulConfiguration.java
 
b/components/camel-consul/src/main/java/org/apache/camel/component/consul/ConsulConfiguration.java
index 80e665b..ed6f7aa 100644
--- 
a/components/camel-consul/src/main/java/org/apache/camel/component/consul/ConsulConfiguration.java
+++ 
b/components/camel-consul/src/main/java/org/apache/camel/component/consul/ConsulConfiguration.java
@@ -20,10 +20,14 @@ import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.spi.UriParam;
 import org.apache.camel.spi.UriParams;
 
+import com.orbitz.consul.Consul;
+
 @UriParams
 public class ConsulConfiguration extends ConsulClientConfiguration {
     @UriParam
     private String key;
+    @UriParam(label = "common")
+    private Consul consulClient;
     @UriParam(label = "producer")
     private String action;
     @UriParam(label = "producer", defaultValue = "false")
@@ -65,12 +69,25 @@ public class ConsulConfiguration extends 
ConsulClientConfiguration {
     public void setKey(String key) {
         this.key = key;
     }
+    
+
+    public Consul getConsulClient() {
+               return consulClient;
+       }
+
+    /**
+     * Reference to a `com.orbitz.consul.Consul` in the
+     * registry.
+     */
+       public void setConsulClient(Consul consulClient) {
+               this.consulClient = consulClient;
+       }
 
     // ****************************************
     // Copy
     // ****************************************
 
-    @Override
+       @Override
     public ConsulConfiguration copy() {
         try {
             return (ConsulConfiguration)super.clone();
diff --git 
a/components/camel-consul/src/main/java/org/apache/camel/component/consul/ConsulEndpoint.java
 
b/components/camel-consul/src/main/java/org/apache/camel/component/consul/ConsulEndpoint.java
index c8d1c24..447f1a9 100644
--- 
a/components/camel-consul/src/main/java/org/apache/camel/component/consul/ConsulEndpoint.java
+++ 
b/components/camel-consul/src/main/java/org/apache/camel/component/consul/ConsulEndpoint.java
@@ -99,8 +99,10 @@ public class ConsulEndpoint extends DefaultEndpoint {
     }
 
     public synchronized Consul getConsul() throws Exception {
-        if (consul == null) {
+        if (consul == null && 
ObjectHelper.isEmpty(getConfiguration().getConsulClient())) {
             consul = configuration.createConsulClient(getCamelContext());
+        } else {
+               consul = getConfiguration().getConsulClient();
         }
 
         return consul;
diff --git 
a/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulClientKeyValueTest.java
 
b/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulClientKeyValueTest.java
new file mode 100644
index 0000000..7753245
--- /dev/null
+++ 
b/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulClientKeyValueTest.java
@@ -0,0 +1,63 @@
+/**
+ * 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.consul;
+
+import java.util.Optional;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.consul.endpoint.ConsulKeyValueActions;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Test;
+
+public class ConsulClientKeyValueTest extends ConsulClientTestSupport {
+
+    @Test
+    public void testKeyPut() throws Exception {
+        String key = generateKey();
+        String val = generateRandomString();
+
+        MockEndpoint mock = getMockEndpoint("mock:kv");
+        mock.expectedMinimumMessageCount(1);
+        mock.expectedBodiesReceived(val);
+        mock.expectedHeaderReceived(ConsulConstants.CONSUL_RESULT, true);
+
+        fluentTemplate()
+            .withHeader(ConsulConstants.CONSUL_ACTION, 
ConsulKeyValueActions.PUT)
+            .withHeader(ConsulConstants.CONSUL_KEY, key)
+            .withBody(val)
+            .to("direct:kv")
+            .send();
+
+        mock.assertIsSatisfied();
+
+        Optional<String> keyVal = 
getConsul().keyValueClient().getValueAsString(key);
+
+        assertTrue(keyVal.isPresent());
+        assertEquals(val, keyVal.get());
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() {
+                from("direct:kv")
+                    .to("consul:kv?consulClient=#consulClient")
+                        .to("mock:kv");
+            }
+        };
+    }
+}
diff --git 
a/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulClientTestSupport.java
 
b/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulClientTestSupport.java
new file mode 100644
index 0000000..c0d9b78
--- /dev/null
+++ 
b/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulClientTestSupport.java
@@ -0,0 +1,80 @@
+/**
+ * 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.consul;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.UUID;
+
+import com.orbitz.consul.Consul;
+import com.orbitz.consul.KeyValueClient;
+import org.apache.camel.impl.JndiRegistry;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Rule;
+import org.junit.rules.TestName;
+
+public class ConsulClientTestSupport extends CamelTestSupport {
+    public static final String CONSUL_HOST = 
System.getProperty("camel.consul.host", Consul.DEFAULT_HTTP_HOST);
+    public static final int CONSUL_PORT = 
Integer.getInteger("camel.consul.port", Consul.DEFAULT_HTTP_PORT);
+    public static final String CONSUL_URL = String.format("http://%s:%d";, 
CONSUL_HOST, CONSUL_PORT);
+    public static final String KV_PREFIX = "/camel";
+
+    @Rule
+    public final TestName testName = new TestName();
+    
+    public Consul consul = Consul.builder().withUrl(CONSUL_URL).build();
+
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry registry = super.createRegistry();
+
+        ConsulComponent component = new ConsulComponent();
+        component.setUrl(CONSUL_URL);
+
+        registry.bind("consul", component);
+        registry.bind("consulClient", consul);
+
+        return registry;
+    }
+
+    protected Consul getConsul() {
+        return consul;
+    }
+
+    protected KeyValueClient getKeyValueClient() {
+        return getConsul().keyValueClient();
+    }
+
+    protected String generateRandomString() {
+        return UUID.randomUUID().toString();
+    }
+
+    protected String[] generateRandomArrayOfStrings(int size) {
+        String[] array = new String[size];
+        Arrays.setAll(array, i -> generateRandomString());
+
+        return array;
+    }
+
+    protected List<String> generateRandomListOfStrings(int size) {
+        return Arrays.asList(generateRandomArrayOfStrings(size));
+    }
+
+    protected String generateKey() {
+        return KV_PREFIX + "/" + testName.getMethodName() + "/" + 
generateRandomString();
+    }
+}
diff --git 
a/platforms/spring-boot/components-starter/camel-consul-starter/src/main/java/org/apache/camel/component/consul/springboot/ConsulComponentConfiguration.java
 
b/platforms/spring-boot/components-starter/camel-consul-starter/src/main/java/org/apache/camel/component/consul/springboot/ConsulComponentConfiguration.java
index b634a1e..799c90b 100644
--- 
a/platforms/spring-boot/components-starter/camel-consul-starter/src/main/java/org/apache/camel/component/consul/springboot/ConsulComponentConfiguration.java
+++ 
b/platforms/spring-boot/components-starter/camel-consul-starter/src/main/java/org/apache/camel/component/consul/springboot/ConsulComponentConfiguration.java
@@ -20,6 +20,7 @@ import java.math.BigInteger;
 import java.util.List;
 import java.util.Set;
 import javax.annotation.Generated;
+import com.orbitz.consul.Consul;
 import com.orbitz.consul.option.ConsistencyMode;
 import org.apache.camel.spring.boot.ComponentConfigurationPropertiesCommon;
 import org.apache.camel.util.jsse.SSLContextParameters;
@@ -173,6 +174,11 @@ public class ConsulComponentConfiguration
          */
         private String key;
         /**
+         * Reference to a `com.orbitz.consul.Consul` in the registry.
+         */
+        @NestedConfigurationProperty
+        private Consul consulClient;
+        /**
          * The Consul agent URL
          */
         private String url;
@@ -275,6 +281,14 @@ public class ConsulComponentConfiguration
             this.key = key;
         }
 
+        public Consul getConsulClient() {
+            return consulClient;
+        }
+
+        public void setConsulClient(Consul consulClient) {
+            this.consulClient = consulClient;
+        }
+
         public String getUrl() {
             return url;
         }

-- 
To stop receiving notification emails like this one, please contact
acosent...@apache.org.

Reply via email to