You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by jb...@apache.org on 2021/01/18 15:33:30 UTC

[karaf-decanter] branch master updated: [KARAF-6983] Add property "replaceDotsByUnderscores" to the configuration file

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

jbonofre pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/karaf-decanter.git


The following commit(s) were added to refs/heads/master by this push:
     new 20af607  [KARAF-6983] Add property "replaceDotsByUnderscores" to the configuration file
     new 6275c72  Merge pull request #221 from paulsp/KARAF-6983
20af607 is described below

commit 20af607ab1f7f0925751d6da307781023eab1a9e
Author: Paul Spencer <pa...@apache.org>
AuthorDate: Sun Jan 3 13:32:13 2021 -0500

    [KARAF-6983] Add property "replaceDotsByUnderscores" to the configuration file
    
    - Create the org.apache.karaf.decanter.marshaller.json.cfg for the JsonMarshaller
    - Add property "replaceDotsByUnderscores" with the default value to the config file
    - Add unit tests for the property
    - Add the config file to the assembly
---
 assembly/src/main/feature/feature.xml              |  1 +
 marshaller/json/pom.xml                            | 26 ++++++++++-
 .../org.apache.karaf.decanter.marshaller.json.cfg  | 24 ++++++++++
 .../decanter/marshaller/json/JsonMarshaller.java   |  5 +-
 .../marshaller/json/TestJsonMarshaller.java        | 53 ++++++++++++++++++++++
 5 files changed, 107 insertions(+), 2 deletions(-)

diff --git a/assembly/src/main/feature/feature.xml b/assembly/src/main/feature/feature.xml
index f8fe563..53b32b6 100644
--- a/assembly/src/main/feature/feature.xml
+++ b/assembly/src/main/feature/feature.xml
@@ -30,6 +30,7 @@ org.apache.felix.eventadmin.IgnoreTimeout=org.apache.karaf.decanter.
         <bundle>mvn:org.apache.karaf.decanter/org.apache.karaf.decanter.api/${project.version}</bundle>
         <bundle>mvn:org.apache.karaf.decanter.marshaller/org.apache.karaf.decanter.marshaller.raw/${project.version}</bundle>
         <bundle>mvn:org.apache.karaf.decanter.marshaller/org.apache.karaf.decanter.marshaller.json/${project.version}</bundle>
+        <configfile finalname="/etc/org.apache.karaf.decanter.marshaller.json.cfg">mvn:org.apache.karaf.decanter.marshaller/org.apache.karaf.decanter.marshaller.json/${project.version}/cfg</configfile>
         <bundle>mvn:org.apache.karaf.decanter.marshaller/org.apache.karaf.decanter.marshaller.csv/${project.version}</bundle>
         <bundle>mvn:org.apache.karaf.decanter.parser/org.apache.karaf.decanter.parser.identity/${project.version}</bundle>
         <configfile finalname="/etc/org.apache.karaf.decanter.parser.split.cfg">mvn:org.apache.karaf.decanter.parser/org.apache.karaf.decanter.parser.split/${project.version}/cfg</configfile>
diff --git a/marshaller/json/pom.xml b/marshaller/json/pom.xml
index 35fd6db..a461444 100644
--- a/marshaller/json/pom.xml
+++ b/marshaller/json/pom.xml
@@ -48,5 +48,29 @@
             <scope>test</scope>
         </dependency>
     </dependencies>
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.codehaus.mojo</groupId>
+                <artifactId>build-helper-maven-plugin</artifactId>
+                <executions>
+                    <execution>
+                        <phase>package</phase>
+                        <goals>
+                            <goal>attach-artifact</goal>
+                        </goals>
+                        <configuration>
+                            <artifacts>
+                                <artifact>
+                                    <file>src/main/cfg/org.apache.karaf.decanter.marshaller.json.cfg</file>
+                                    <type>cfg</type>
+                                </artifact>
+                            </artifacts>
+                        </configuration>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
 
-</project>
\ No newline at end of file
+</project>
diff --git a/marshaller/json/src/main/cfg/org.apache.karaf.decanter.marshaller.json.cfg b/marshaller/json/src/main/cfg/org.apache.karaf.decanter.marshaller.json.cfg
new file mode 100644
index 0000000..b2690dd
--- /dev/null
+++ b/marshaller/json/src/main/cfg/org.apache.karaf.decanter.marshaller.json.cfg
@@ -0,0 +1,24 @@
+################################################################################
+#
+#    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.
+#
+################################################################################
+
+#
+# Decanter JSON marshaller configuration
+#
+
+# replaceDotsByUnderscores=true
diff --git a/marshaller/json/src/main/java/org/apache/karaf/decanter/marshaller/json/JsonMarshaller.java b/marshaller/json/src/main/java/org/apache/karaf/decanter/marshaller/json/JsonMarshaller.java
index 79347df..b006c51 100644
--- a/marshaller/json/src/main/java/org/apache/karaf/decanter/marshaller/json/JsonMarshaller.java
+++ b/marshaller/json/src/main/java/org/apache/karaf/decanter/marshaller/json/JsonMarshaller.java
@@ -59,7 +59,10 @@ public class JsonMarshaller implements Marshaller {
 
     @Activate
     public void activate(ComponentContext componentContext) {
-        Dictionary<String, Object> config = componentContext.getProperties();
+	activate(componentContext.getProperties());
+    }
+
+    public void activate(Dictionary<String, Object> config) {
         replaceDotsByUnderscores = (config.get("replaceDotsByUnderscores") != null) ? 
             Boolean.valueOf((String) config.get("replaceDotsByUnderscores")) : true;
     }
diff --git a/marshaller/json/src/test/java/org/apache/karaf/decanter/marshaller/json/TestJsonMarshaller.java b/marshaller/json/src/test/java/org/apache/karaf/decanter/marshaller/json/TestJsonMarshaller.java
index 7abb5f4..b5b8e71 100644
--- a/marshaller/json/src/test/java/org/apache/karaf/decanter/marshaller/json/TestJsonMarshaller.java
+++ b/marshaller/json/src/test/java/org/apache/karaf/decanter/marshaller/json/TestJsonMarshaller.java
@@ -17,6 +17,8 @@
 package org.apache.karaf.decanter.marshaller.json;
 
 import java.io.StringReader;
+import java.util.Dictionary;
+import java.util.Hashtable;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -34,6 +36,10 @@ public class TestJsonMarshaller {
 
     private static final long EXPECTED_TIMESTAMP = 1454428780634L;
     private static final String EXPECTED_TOPIC = "testTopic";
+    private static final String DOT_KEY = "test.key";
+    private static final String DOT_VALUE = "test.value";
+    private static final String EXPECTED_DOT_KEY = "test.key";
+    private static final String EXPECTED_UNDERLINE_KEY = "test_key";
 
    @Test
    public void testMarshal() throws Exception {
@@ -53,6 +59,53 @@ public class TestJsonMarshaller {
    }
 
    @Test
+   public void testMarshalWithDot() throws Exception {
+       JsonMarshaller marshaller = new JsonMarshaller();
+
+       Dictionary<String, Object> config = new Hashtable<>();
+       config.put("replaceDotsByUnderscores", "false");
+       marshaller.activate(config);
+
+       Map<String, Object> map = new HashMap<>();
+       map.put(DOT_KEY, DOT_VALUE);
+       String jsonSt = marshaller.marshal(new Event(EXPECTED_TOPIC, map));
+       System.out.println(jsonSt);
+       JsonReader reader = Json.createReader(new StringReader(jsonSt));
+       JsonObject jsonO = reader.readObject();
+       Assert.assertEquals("Value", DOT_VALUE, jsonO.getString(EXPECTED_DOT_KEY));
+        try {
+           jsonO.getString(EXPECTED_UNDERLINE_KEY);
+	   Assert.fail("Key "+ EXPECTED_UNDERLINE_KEY + " exists");
+       } catch (NullPointerException e) {
+	   // This is expected
+       }
+   }
+
+
+   @Test
+   public void testMarshalWithUnderscore() throws Exception {
+       JsonMarshaller marshaller = new JsonMarshaller();
+
+       Dictionary<String, Object> config = new Hashtable<>();
+       config.put("replaceDotsByUnderscores", "true");
+       marshaller.activate(config);
+
+       Map<String, Object> map = new HashMap<>();
+       map.put(DOT_KEY, DOT_VALUE);
+       String jsonSt = marshaller.marshal(new Event(EXPECTED_TOPIC, map));
+       System.out.println(jsonSt);
+       JsonReader reader = Json.createReader(new StringReader(jsonSt));
+       JsonObject jsonO = reader.readObject();
+       Assert.assertEquals("Value", DOT_VALUE, jsonO.getString(EXPECTED_UNDERLINE_KEY));
+       try {
+           jsonO.getString(EXPECTED_DOT_KEY);
+	   Assert.fail("Key "+ EXPECTED_DOT_KEY + " exists");
+       } catch (NullPointerException e) {
+	   // This is expected
+       }
+   }
+
+   @Test
    public void testInnerMap() throws Exception {
        Marshaller marshaller = new JsonMarshaller();