Repository: camel Updated Branches: refs/heads/camel-2.18.x c69e59a3b -> f04f785ab refs/heads/master 5f1a40010 -> e272e52f7
CAMEL-10443: fixing findById with ObjectId Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/e272e52f Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/e272e52f Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/e272e52f Branch: refs/heads/master Commit: e272e52f76186812f392e7c647e53bc0c4230717 Parents: 5f1a400 Author: Nick Busy <nb...@enfoll.com> Authored: Sun Nov 6 00:04:05 2016 +1100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Mon Nov 7 10:36:09 2016 +0100 ---------------------------------------------------------------------- .../component/mongodb/MongoDbProducer.java | 2 +- .../mongodb/MongoDbFindOperationTest.java | 23 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/e272e52f/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbProducer.java b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbProducer.java index 8b8437c..9a72e7e 100644 --- a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbProducer.java +++ b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbProducer.java @@ -478,7 +478,7 @@ public class MongoDbProducer extends DefaultProducer { return exchange1 -> { try { MongoCollection<BasicDBObject> dbCol = calculateCollection(exchange1); - String id = exchange1.getIn().getMandatoryBody(String.class); + Object id = exchange1.getIn().getMandatoryBody(); BasicDBObject o = new BasicDBObject("_id", id); DBObject ret; http://git-wip-us.apache.org/repos/asf/camel/blob/e272e52f/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbFindOperationTest.java ---------------------------------------------------------------------- diff --git a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbFindOperationTest.java b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbFindOperationTest.java index fca6ef4..148a020 100644 --- a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbFindOperationTest.java +++ b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbFindOperationTest.java @@ -20,12 +20,14 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import com.mongodb.BasicDBObject; import com.mongodb.BasicDBObjectBuilder; import com.mongodb.DBObject; import org.apache.camel.Exchange; import org.apache.camel.builder.RouteBuilder; +import org.bson.types.ObjectId; import org.junit.Test; public class MongoDbFindOperationTest extends AbstractMongoDbTest { @@ -215,6 +217,27 @@ public class MongoDbFindOperationTest extends AbstractMongoDbTest { } + @Test + public void testFindOneByIdWithObjectId() throws Exception { + // Test that the collection has 0 documents in it + assertEquals(0, testCollection.count()); + BasicDBObject insertObject = new BasicDBObject("scientist", "Einstein"); + testCollection.insertOne(insertObject); + assertTrue("The ID of the inserted document should be ObjectId", insertObject.get("_id") instanceof ObjectId); + ObjectId id = (ObjectId) insertObject.get("_id"); + + DBObject result = template.requestBody("direct:findById", id, DBObject.class); + assertTrue("Result is not of type DBObject", result instanceof DBObject); + + assertTrue("The ID of the retrieved DBObject should be ObjectId", result.get("_id") instanceof ObjectId); + assertEquals("The ID of the retrieved DBObject should equal to the inserted", id, result.get("_id")); + assertEquals("The scientist name of the retrieved DBObject should equal Einstein", "Einstein", result.get("scientist")); + + assertNotNull("DBObject in returned list should contain all fields", result.get("_id")); + assertNotNull("DBObject in returned list should contain all fields", result.get("scientist")); + + } + @Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() {