Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id BE9EF200D19 for ; Fri, 6 Oct 2017 15:26:09 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id BD19E1609D0; Fri, 6 Oct 2017 13:26:09 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id B17861609DF for ; Fri, 6 Oct 2017 15:26:08 +0200 (CEST) Received: (qmail 13768 invoked by uid 500); 6 Oct 2017 13:26:07 -0000 Mailing-List: contact commits-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@camel.apache.org Delivered-To: mailing list commits@camel.apache.org Received: (qmail 13759 invoked by uid 99); 6 Oct 2017 13:26:07 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 06 Oct 2017 13:26:07 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 3D3D8DFF0F; Fri, 6 Oct 2017 13:26:05 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: davsclaus@apache.org To: commits@camel.apache.org Date: Fri, 06 Oct 2017 13:26:06 -0000 Message-Id: <6b8336e038f54cbbb9eba87217ec1de6@git.apache.org> In-Reply-To: <0bd1fc54098e4a7ebc121bbe2e73927e@git.apache.org> References: <0bd1fc54098e4a7ebc121bbe2e73927e@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [2/2] camel git commit: CAMEL-11308: Add workaround for jackson to map POJO BigDecimal as Dobule so it works with MongoDB again. archived-at: Fri, 06 Oct 2017 13:26:09 -0000 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 Authored: Fri Oct 6 15:25:36 2017 +0200 Committer: Claus Ibsen 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 @@ camel-core - + org.apache.camel camel-jackson @@ -95,30 +95,30 @@ - - - - aix - - - AIX - - - - true - - - - hpux - - - HP-UX - - - - true - - - + + + + aix + + + AIX + + + + true + + + + hpux + + + HP-UX + + + + true + + + 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 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"); + } + }; + } +} +