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 2019/08/12 09:57:33 UTC

[camel] 04/07: CAMEL-13857: camel-microprofile-config: should implement LoadablePropertiesSource

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

commit e2564f391d9bf40f028e176871827eccc2567925
Author: lburgazzoli <lb...@gmail.com>
AuthorDate: Mon Aug 12 07:15:58 2019 +0200

    CAMEL-13857: camel-microprofile-config: should implement LoadablePropertiesSource
---
 components/camel-microprofile-config/pom.xml       |  5 ++++
 .../config/CamelMicroProfilePropertiesSource.java  | 31 +++++++++++++++++-----
 .../CamelMicroProfilePropertiesSourceTest.java     | 12 +++++++++
 3 files changed, 42 insertions(+), 6 deletions(-)

diff --git a/components/camel-microprofile-config/pom.xml b/components/camel-microprofile-config/pom.xml
index f939abc..50eaa70 100644
--- a/components/camel-microprofile-config/pom.xml
+++ b/components/camel-microprofile-config/pom.xml
@@ -73,6 +73,11 @@
             <scope>test</scope>
         </dependency>
         <dependency>
+            <groupId>org.assertj</groupId>
+            <artifactId>assertj-core</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
             <groupId>org.apache.logging.log4j</groupId>
             <artifactId>log4j-api</artifactId>
             <scope>test</scope>
diff --git a/components/camel-microprofile-config/src/main/java/org/apache/camel/component/microprofile/config/CamelMicroProfilePropertiesSource.java b/components/camel-microprofile-config/src/main/java/org/apache/camel/component/microprofile/config/CamelMicroProfilePropertiesSource.java
index 9c6b21c..a4e0524 100644
--- a/components/camel-microprofile-config/src/main/java/org/apache/camel/component/microprofile/config/CamelMicroProfilePropertiesSource.java
+++ b/components/camel-microprofile-config/src/main/java/org/apache/camel/component/microprofile/config/CamelMicroProfilePropertiesSource.java
@@ -16,9 +16,9 @@
  */
 package org.apache.camel.component.microprofile.config;
 
-import java.util.Optional;
+import java.util.Properties;
 
-import org.apache.camel.spi.PropertiesSource;
+import org.apache.camel.spi.LoadablePropertiesSource;
 import org.apache.camel.support.service.ServiceSupport;
 import org.eclipse.microprofile.config.Config;
 import org.eclipse.microprofile.config.ConfigProvider;
@@ -27,7 +27,7 @@ import org.eclipse.microprofile.config.ConfigProvider;
  * The microprofile-config component is used for bridging the Eclipse MicroProfile Config with Camels
  * properties component. This allows to use configuration management from Eclipse MicroProfile with Camel.
  */
-public class CamelMicroProfilePropertiesSource extends ServiceSupport implements PropertiesSource {
+public class CamelMicroProfilePropertiesSource extends ServiceSupport implements LoadablePropertiesSource {
 
     private Config config;
 
@@ -41,16 +41,35 @@ public class CamelMicroProfilePropertiesSource extends ServiceSupport implements
         if (config == null) {
             config = ConfigProvider.getConfig();
         }
-        Optional<String> value = config.getOptionalValue(name, String.class);
-        return value.orElse(null);
+        return config.getOptionalValue(name, String.class).orElse(null);
     }
 
     @Override
-    protected void doStart() throws Exception {
+    public Properties loadProperties() {
+        if (config == null) {
+            config = ConfigProvider.getConfig();
+        }
+
+        Properties answer = new Properties();
+
+        for (String key: config.getPropertyNames()) {
+            answer.put(key, config.getValue(key, String.class));
+        }
+
+        return answer;
+    }
+
+    @Override
+    protected void doInit() throws Exception {
         config = ConfigProvider.getConfig();
     }
 
     @Override
+    protected void doStart() throws Exception {
+        // noop
+    }
+
+    @Override
     protected void doStop() throws Exception {
         // noop
     }
diff --git a/components/camel-microprofile-config/src/test/java/org/apache/camel/component/microprofile/config/CamelMicroProfilePropertiesSourceTest.java b/components/camel-microprofile-config/src/test/java/org/apache/camel/component/microprofile/config/CamelMicroProfilePropertiesSourceTest.java
index 16370bc..3ac6ff2 100644
--- a/components/camel-microprofile-config/src/test/java/org/apache/camel/component/microprofile/config/CamelMicroProfilePropertiesSourceTest.java
+++ b/components/camel-microprofile-config/src/test/java/org/apache/camel/component/microprofile/config/CamelMicroProfilePropertiesSourceTest.java
@@ -23,9 +23,11 @@ import io.smallrye.config.SmallRyeConfigBuilder;
 import org.apache.camel.CamelContext;
 import org.apache.camel.RoutesBuilder;
 import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.spi.PropertiesComponent;
 import org.apache.camel.spi.PropertiesSource;
 import org.apache.camel.spi.Registry;
 import org.apache.camel.test.junit4.CamelTestSupport;
+import org.assertj.core.api.Assertions;
 import org.eclipse.microprofile.config.Config;
 import org.eclipse.microprofile.config.spi.ConfigProviderResolver;
 import org.junit.Test;
@@ -68,6 +70,16 @@ public class CamelMicroProfilePropertiesSourceTest extends CamelTestSupport {
     }
 
     @Test
+    public void testLoadAll() throws Exception {
+        PropertiesComponent pc = context.getComponent("properties", PropertiesComponent.class);
+        Properties properties = pc.loadProperties();
+
+        Assertions.assertThat(properties.get("start")).isEqualTo("direct:start");
+        Assertions.assertThat(properties.get("hi")).isEqualTo("World");
+        Assertions.assertThat(properties.get("my-mock")).isEqualTo("result");
+    }
+
+    @Test
     public void testMicroProfileConfig() throws Exception {
         getMockEndpoint("mock:result").expectedBodiesReceived("Hello World from Camel");