CAMEL-11308: Add workaround for jackson to map POJO BigDecimal as Dobule so it 
works with MongoDB again.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/798ed729
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/798ed729
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/798ed729

Branch: refs/heads/camel-2.19.x
Commit: 798ed7296948175cf6e35882102939e63527ee48
Parents: ebb4678
Author: Claus Ibsen <davscl...@apache.org>
Authored: Fri Oct 6 15:25:36 2017 +0200
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Fri Oct 6 15:25:57 2017 +0200

----------------------------------------------------------------------
 components/camel-mongodb/pom.xml                | 52 ++++++++---------
 .../converters/MongoDbFallbackConverter.java    |  9 +++
 .../mongodb/MongoDbBigDecimalConverterTest.java | 60 +++++++++++++++++++
 .../src/test/resources/log4j2.properties        |  2 +-
 .../converters/MongoDbFallbackConverter.java    | 17 ++++++
 .../MongoDbBigDecimalConverterTest.java         | 61 ++++++++++++++++++++
 6 files changed, 174 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/798ed729/components/camel-mongodb/pom.xml
----------------------------------------------------------------------
diff --git a/components/camel-mongodb/pom.xml b/components/camel-mongodb/pom.xml
index 4355ea0..b6018ba 100644
--- a/components/camel-mongodb/pom.xml
+++ b/components/camel-mongodb/pom.xml
@@ -43,7 +43,7 @@
       <artifactId>camel-core</artifactId>
     </dependency>
 
-       <!-- JSON is vital to MongoDB, so we absolutely require camel-jackson 
-->
+         <!-- JSON is vital to MongoDB, so we absolutely require camel-jackson 
-->
     <dependency>
       <groupId>org.apache.camel</groupId>
       <artifactId>camel-jackson</artifactId>
@@ -95,30 +95,30 @@
     </dependency>
   </dependencies>
 
-    <!-- skip tests on AIX and HP-UX -->
-    <profiles>
-        <profile>
-            <id>aix</id>
-            <activation>
-                <os>
-                    <family>AIX</family>
-                </os>
-            </activation>
-            <properties>
-                <skipTests>true</skipTests>
-            </properties>
-        </profile>
-        <profile>
-            <id>hpux</id>
-            <activation>
-                <os>
-                    <family>HP-UX</family>
-                </os>
-            </activation>
-            <properties>
-                <skipTests>true</skipTests>
-            </properties>
-        </profile>
-    </profiles>
+  <!-- skip tests on AIX and HP-UX -->
+  <profiles>
+    <profile>
+      <id>aix</id>
+      <activation>
+        <os>
+          <family>AIX</family>
+        </os>
+      </activation>
+      <properties>
+        <skipTests>true</skipTests>
+      </properties>
+    </profile>
+    <profile>
+      <id>hpux</id>
+      <activation>
+        <os>
+          <family>HP-UX</family>
+        </os>
+      </activation>
+      <properties>
+        <skipTests>true</skipTests>
+      </properties>
+    </profile>
+  </profiles>
 
 </project>

http://git-wip-us.apache.org/repos/asf/camel/blob/798ed729/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/converters/MongoDbFallbackConverter.java
----------------------------------------------------------------------
diff --git 
a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/converters/MongoDbFallbackConverter.java
 
b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/converters/MongoDbFallbackConverter.java
index 134c2f9..a055f67 100644
--- 
a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/converters/MongoDbFallbackConverter.java
+++ 
b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/converters/MongoDbFallbackConverter.java
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.component.mongodb.converters;
 
+import java.math.BigDecimal;
 import java.util.Map;
 
 import com.fasterxml.jackson.databind.ObjectMapper;
@@ -72,6 +73,14 @@ public final class MongoDbFallbackConverter {
         // okay then fallback and use jackson
         if (type == DBObject.class) {
             Map<?, ?> m = OBJECT_MAPPER.convertValue(value, Map.class);
+            // workaround problem with mongodb for BigDecimal should be Double
+            for (Map.Entry entry : m.entrySet()) {
+                Object v = entry.getValue();
+                if (v instanceof BigDecimal) {
+                    v = Double.valueOf(v.toString());
+                    entry.setValue(v);
+                }
+            }
             return new BasicDBObject(m);
         }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/798ed729/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbBigDecimalConverterTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbBigDecimalConverterTest.java
 
b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbBigDecimalConverterTest.java
new file mode 100644
index 0000000..e07ecf6
--- /dev/null
+++ 
b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbBigDecimalConverterTest.java
@@ -0,0 +1,60 @@
+/**
+ * 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.mongodb;
+
+import java.math.BigDecimal;
+
+import com.mongodb.BasicDBObject;
+import com.mongodb.DBObject;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.Test;
+
+public class MongoDbBigDecimalConverterTest extends AbstractMongoDbTest {
+
+    private class NumberClass {
+        // CHECKSTYLE:OFF
+        public String _id = "testBigDecimalConvert";
+        // CHECKSTYLE:ON
+
+        public BigDecimal aNumber = new BigDecimal(0);
+
+        public BigDecimal bNumber = new BigDecimal(12345L);
+    }
+    @Test
+    public void testBigDecimalAutoConversion() {
+        assertEquals(0, testCollection.count());
+        NumberClass testClass = new NumberClass();
+        Object result = template.requestBody("direct:insert", testClass);
+        assertTrue(result instanceof BasicDBObject);
+        DBObject b = testCollection.find(new BasicDBObject("_id", 
testClass._id)).first();
+        assertNotNull("No record with 'testInsertString' _id", b);
+
+        assertTrue(testClass.aNumber.equals(new BigDecimal((double) 
b.get("aNumber"))));
+        assertEquals(testClass.bNumber, new BigDecimal((double) 
b.get("bNumber")));
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() {
+                from("direct:insert")
+                    
.to("mongodb:myDb?database={{mongodb.testDb}}&collection={{mongodb.testCollection}}&operation=insert&writeConcern=SAFE");
+            }
+        };
+    }
+}
+

http://git-wip-us.apache.org/repos/asf/camel/blob/798ed729/components/camel-mongodb/src/test/resources/log4j2.properties
----------------------------------------------------------------------
diff --git a/components/camel-mongodb/src/test/resources/log4j2.properties 
b/components/camel-mongodb/src/test/resources/log4j2.properties
index 3a830b1..f982316 100644
--- a/components/camel-mongodb/src/test/resources/log4j2.properties
+++ b/components/camel-mongodb/src/test/resources/log4j2.properties
@@ -24,5 +24,5 @@ appender.out.type = Console
 appender.out.name = out
 appender.out.layout.type = PatternLayout
 appender.out.layout.pattern = [%30.30t] %-30.30c{1} %-5p %m%n
-rootLogger.level = WARN
+rootLogger.level = INFO
 rootLogger.appenderRef.file.ref = file

http://git-wip-us.apache.org/repos/asf/camel/blob/798ed729/components/camel-mongodb3/src/main/java/org/apache/camel/component/mongodb3/converters/MongoDbFallbackConverter.java
----------------------------------------------------------------------
diff --git 
a/components/camel-mongodb3/src/main/java/org/apache/camel/component/mongodb3/converters/MongoDbFallbackConverter.java
 
b/components/camel-mongodb3/src/main/java/org/apache/camel/component/mongodb3/converters/MongoDbFallbackConverter.java
index 3fbeb46..83b1b48 100644
--- 
a/components/camel-mongodb3/src/main/java/org/apache/camel/component/mongodb3/converters/MongoDbFallbackConverter.java
+++ 
b/components/camel-mongodb3/src/main/java/org/apache/camel/component/mongodb3/converters/MongoDbFallbackConverter.java
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.component.mongodb3.converters;
 
+import java.math.BigDecimal;
 import java.util.Map;
 
 import com.fasterxml.jackson.databind.ObjectMapper;
@@ -74,12 +75,28 @@ public final class MongoDbFallbackConverter {
         // okay then fallback and use jackson
         if (type == DBObject.class) {
             Map<?, ?> m = OBJECT_MAPPER.convertValue(value, Map.class);
+            // workaround problem with mongodb for BigDecimal should be Double
+            mapMongoDBBigDecimalIssue(m);
             return new BasicDBObject(m);
         } else if (type == Document.class) {
             Map<String, Object> m = OBJECT_MAPPER.convertValue(value, 
Map.class);
+            // workaround problem with mongodb for BigDecimal should be Double
+            mapMongoDBBigDecimalIssue(m);
             return new Document(m);
         }
 
         return null;
     }
+
+    @SuppressWarnings("unchecked")
+    private static void mapMongoDBBigDecimalIssue(Map<?, ?> m) {
+        // workaround problem with mongodb for BigDecimal should be Double
+        for (Map.Entry entry : m.entrySet()) {
+            Object v = entry.getValue();
+            if (v instanceof BigDecimal) {
+                v = Double.valueOf(v.toString());
+                entry.setValue(v);
+            }
+        }
+    }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/798ed729/components/camel-mongodb3/src/test/java/org/apache/camel/component/mongodb3/MongoDbBigDecimalConverterTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-mongodb3/src/test/java/org/apache/camel/component/mongodb3/MongoDbBigDecimalConverterTest.java
 
b/components/camel-mongodb3/src/test/java/org/apache/camel/component/mongodb3/MongoDbBigDecimalConverterTest.java
new file mode 100644
index 0000000..fa08850
--- /dev/null
+++ 
b/components/camel-mongodb3/src/test/java/org/apache/camel/component/mongodb3/MongoDbBigDecimalConverterTest.java
@@ -0,0 +1,61 @@
+/**
+ * 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.mongodb3;
+
+import java.math.BigDecimal;
+
+import com.mongodb.BasicDBObject;
+import org.apache.camel.builder.RouteBuilder;
+import org.bson.Document;
+import org.junit.Test;
+
+public class MongoDbBigDecimalConverterTest extends AbstractMongoDbTest {
+
+    private class NumberClass {
+        // CHECKSTYLE:OFF
+        public String _id = "testBigDecimalConvert";
+        // CHECKSTYLE:ON
+
+        public BigDecimal aNumber = new BigDecimal(0);
+
+        public BigDecimal bNumber = new BigDecimal(12345L);
+    }
+
+    @Test
+    public void testBigDecimalAutoConversion() {
+        assertEquals(0, testCollection.count());
+        NumberClass testClass = new NumberClass();
+        Object result = template.requestBody("direct:insert", testClass);
+        assertTrue(result instanceof Document);
+        Document b = testCollection.find(new BasicDBObject("_id", 
testClass._id)).first();
+        assertNotNull("No record with 'testInsertString' _id", b);
+
+        assertTrue(testClass.aNumber.equals(new BigDecimal((double) 
b.get("aNumber"))));
+        assertEquals(testClass.bNumber, new BigDecimal((double) 
b.get("bNumber")));
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() {
+                from("direct:insert")
+                    
.to("mongodb3:myDb?database={{mongodb.testDb}}&collection={{mongodb.testCollection}}&operation=insert");
+            }
+        };
+    }
+}
+

Reply via email to