[camel-kafka-connector] branch master updated: Reneamed a method to better understanding, added a test and javadoc as well.

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[camel-kafka-connector] branch master updated: Reneamed a method to better understanding, added a test and javadoc as well.

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 0361f55  Reneamed a method to better understanding, added a test and javadoc as well.
0361f55 is described below

commit 0361f55c61e2e73ac1ad6a05b0871b054c39b50d
Author: Andrea Tarocchi <[hidden email]>
AuthorDate: Sat Oct 17 09:35:12 2020 +0200

    Reneamed a method to better understanding, added a test and javadoc as well.
---
 .../apache/camel/kafkaconnector/CamelSinkTask.java |  2 +-
 .../camel/kafkaconnector/CamelSourceTask.java      |  2 +-
 .../camel/kafkaconnector/utils/TaskHelper.java     | 46 +++++++++++++++++++++-
 .../camel/kafkaconnector/utils/TaskHelperTest.java | 26 ++++++++++--
 4 files changed, 69 insertions(+), 7 deletions(-)

diff --git a/core/src/main/java/org/apache/camel/kafkaconnector/CamelSinkTask.java b/core/src/main/java/org/apache/camel/kafkaconnector/CamelSinkTask.java
index e64ee16..f410c50 100644
--- a/core/src/main/java/org/apache/camel/kafkaconnector/CamelSinkTask.java
+++ b/core/src/main/java/org/apache/camel/kafkaconnector/CamelSinkTask.java
@@ -68,7 +68,7 @@ public class CamelSinkTask extends SinkTask {
     public void start(Map<String, String> props) {
         try {
             LOG.info("Starting CamelSinkTask connector task");
-            Map<String, String> actualProps = TaskHelper.mergeProperties(getDefaultConfig(), props);
+            Map<String, String> actualProps = TaskHelper.combineDefaultAndLoadedProperties(getDefaultConfig(), props);
             CamelSinkConnectorConfig config = getCamelSinkConnectorConfig(actualProps);
 
             try {
diff --git a/core/src/main/java/org/apache/camel/kafkaconnector/CamelSourceTask.java b/core/src/main/java/org/apache/camel/kafkaconnector/CamelSourceTask.java
index 247edd3..9845013 100644
--- a/core/src/main/java/org/apache/camel/kafkaconnector/CamelSourceTask.java
+++ b/core/src/main/java/org/apache/camel/kafkaconnector/CamelSourceTask.java
@@ -69,7 +69,7 @@ public class CamelSourceTask extends SourceTask {
     public void start(Map<String, String> props) {
         try {
             LOG.info("Starting CamelSourceTask connector task");
-            Map<String, String> actualProps = TaskHelper.mergeProperties(getDefaultConfig(), props);
+            Map<String, String> actualProps = TaskHelper.combineDefaultAndLoadedProperties(getDefaultConfig(), props);
             CamelSourceConnectorConfig config = getCamelSourceConnectorConfig(actualProps);
 
             try {
diff --git a/core/src/main/java/org/apache/camel/kafkaconnector/utils/TaskHelper.java b/core/src/main/java/org/apache/camel/kafkaconnector/utils/TaskHelper.java
index e7f8c83..87f545e 100644
--- a/core/src/main/java/org/apache/camel/kafkaconnector/utils/TaskHelper.java
+++ b/core/src/main/java/org/apache/camel/kafkaconnector/utils/TaskHelper.java
@@ -35,6 +35,18 @@ public final class TaskHelper {
     private TaskHelper() {
     }
 
+    /**
+     * Try to build a url of a Camel {@link org.apache.camel.Endpoint}.
+     *
+     * @param rcc RuntimeCamelCatalog used to build the url.
+     * @param props properties used to build the url in the form of a key -> value {@link Map}.
+     * @param componentSchema the schema name of the Camel {@link org.apache.camel.Component} used to build the Camel {@link org.apache.camel.Endpoint} url.
+     * @param endpointPropertiesPrefix prefix of all the Camel {@link org.apache.camel.Endpoint} properties.
+     * @param pathPropertiesPrefix prefix of all the properties used in the Camel {@link org.apache.camel.Endpoint} path.
+     *
+     * @return A String representing the built url.
+     * @throws {@link URISyntaxException} in case of uri build failure.
+     */
     public static String buildUrl(RuntimeCamelCatalog rcc, Map<String, String> props, String componentSchema, String endpointPropertiesPrefix, String pathPropertiesPrefix) throws URISyntaxException {
         ComponentModel cm = null;
         if (componentSchema != null) {
@@ -65,7 +77,39 @@ public final class TaskHelper {
         return rcc.asEndpointUri(componentSchema, filteredProps, false);
     }
 
-    public static Map<String, String> mergeProperties(Map<String, String> defaultProps, Map<String, String> loadedProps) {
+    /**
+     * Combines default properties with loaded properties with the following logic:
+     * 1) loaded properties overwrite default properties.
+     * 2) default properties that starts with a loaded property are removed.
+     *
+     * Example:
+     * default properties:
+     *   defaultProperty=defaultValue
+     *   overwrittenDefaultProperty=defaultValue
+     *   camel.component.x.objectProperty=#class:my.package.MyClass
+     *   camel.component.x.objectProperty.removedDefaultField=defaultValue
+     *   camel.component.x.defaultProperty=defaultValue
+     *
+     * loaded properties:
+     *   overwrittenDefaultProperty=loadedValue
+     *   camel.component.x.objectProperty=#class:my.package.MyOtherClass
+     *   camel.component.x.objectProperty.loadedField=loadedValue
+     *   camel.component.x.loadedProperty=loadedValue
+     *
+     * will result in:
+     *   defaultProperty=defaultValue
+     *   overwrittenDefaultProperty=loadedValue
+     *   camel.component.x.objectProperty=#class:my.package.MyOtherClass
+     *   camel.component.x.objectProperty.anotherField=loadedValue
+     *   camel.component.x.loadedProperty=loadedValue
+     *   camel.component.x.defaultProperty=defaultValue
+     *
+     * See org.apache.camel.kafkaconnector.utils.TaskHelperTests for some examples.
+     *
+     * @param defaultProps RuntimeCamelCatalog used to build the url.
+     * @param loadedProps properties used to build the url in the form of a key -> value {@link Map}.
+     */
+    public static Map<String, String> combineDefaultAndLoadedProperties(Map<String, String> defaultProps, Map<String, String> loadedProps) {
         if (loadedProps == null && defaultProps == null) {
             return Collections.emptyMap();
         } else if (loadedProps == null) {
diff --git a/core/src/test/java/org/apache/camel/kafkaconnector/utils/TaskHelperTest.java b/core/src/test/java/org/apache/camel/kafkaconnector/utils/TaskHelperTest.java
index 26ca86e..1f53322 100644
--- a/core/src/test/java/org/apache/camel/kafkaconnector/utils/TaskHelperTest.java
+++ b/core/src/test/java/org/apache/camel/kafkaconnector/utils/TaskHelperTest.java
@@ -43,7 +43,7 @@ public class TaskHelperTest {
 
     @Test
     public void testMergePropertiesNull() {
-        Map<String, String> result = TaskHelper.mergeProperties(null, null);
+        Map<String, String> result = TaskHelper.combineDefaultAndLoadedProperties(null, null);
 
         assertEquals(Collections.EMPTY_MAP, result);
     }
@@ -52,7 +52,7 @@ public class TaskHelperTest {
     public void testMergePropertiesDefaultAreAdded() {
         Map<String, String> defaults = mapOf("property", "defaultValue");
         Map<String, String> loaded = mapOf("anotherProperty", "loadedValue");
-        Map<String, String> result = TaskHelper.mergeProperties(defaults, loaded);
+        Map<String, String> result = TaskHelper.combineDefaultAndLoadedProperties(defaults, loaded);
 
         assertTrue(result.containsKey("property"));
         assertTrue(result.containsKey("anotherProperty"));
@@ -64,7 +64,7 @@ public class TaskHelperTest {
     public void testMergePropertiesLoadedHavePrecedence() {
         Map<String, String> defaults = mapOf("property", "defaultValue");
         Map<String, String> loaded = mapOf("property", "loadedValue");
-        Map<String, String> result = TaskHelper.mergeProperties(defaults, loaded);
+        Map<String, String> result = TaskHelper.combineDefaultAndLoadedProperties(defaults, loaded);
 
         assertTrue(result.containsKey("property"));
         assertEquals("loadedValue", result.get("property"));
@@ -84,7 +84,7 @@ public class TaskHelperTest {
             "camel.component.x.normalProperty", "loadedValue"
         );
 
-        Map<String, String> result = TaskHelper.mergeProperties(defaults, loaded);
+        Map<String, String> result = TaskHelper.combineDefaultAndLoadedProperties(defaults, loaded);
 
         assertEquals("defaultValue", result.get("property"));
         assertEquals("#class:my.package.MyOtherClass", result.get("camel.component.x.objectProperty"));
@@ -94,6 +94,24 @@ public class TaskHelperTest {
     }
 
     @Test
+    public void testMergePropertiesLoadedAndDefaultMergesIfNoPrefixFiltering() {
+        Map<String, String> defaults = mapOf(
+                "camel.component.x.objectProperty", "#class:my.package.MyClass",
+                "camel.component.x.objectProperty.field", "defaultValue"
+        );
+
+        Map<String, String> loaded = mapOf(
+                "camel.component.x.objectProperty.anotherField", "loadedValue"
+        );
+
+        Map<String, String> result = TaskHelper.combineDefaultAndLoadedProperties(defaults, loaded);
+
+        assertEquals("#class:my.package.MyClass", result.get("camel.component.x.objectProperty"));
+        assertEquals("defaultValue", result.get("camel.component.x.objectProperty.field"));
+        assertEquals("loadedValue", result.get("camel.component.x.objectProperty.anotherField"));
+    }
+
+    @Test
     public void testBuildUrlWithRuntimeCatalog() throws URISyntaxException {
         DefaultCamelContext dcc = new DefaultCamelContext();
         RuntimeCamelCatalog rcc = dcc.adapt(ExtendedCamelContext.class).getRuntimeCamelCatalog();