camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hekon...@apache.org
Subject git commit: [CAMEL-7996] Storing OID of the inserted records in the headers.
Date Wed, 05 Nov 2014 12:03:52 GMT
Repository: camel
Updated Branches:
  refs/heads/master cbad2d60b -> 1a6f5c394


[CAMEL-7996] Storing OID of the inserted records in the headers.


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

Branch: refs/heads/master
Commit: 1a6f5c394830a713dfe65221a39a57beb60f9504
Parents: cbad2d6
Author: Henryk Konsek <hekonsek@gmail.com>
Authored: Wed Nov 5 13:03:43 2014 +0100
Committer: Henryk Konsek <hekonsek@gmail.com>
Committed: Wed Nov 5 13:03:43 2014 +0100

----------------------------------------------------------------------
 .../component/mongodb/MongoDbConstants.java     |  1 +
 .../component/mongodb/MongoDbProducer.java      | 13 ++++++--
 .../mongodb/MongoDbOperationsTest.java          | 33 +++++++++++++-------
 3 files changed, 33 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/1a6f5c39/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbConstants.java
----------------------------------------------------------------------
diff --git a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbConstants.java
b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbConstants.java
index 3eff7c5..adc3e3d 100644
--- a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbConstants.java
+++ b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbConstants.java
@@ -37,6 +37,7 @@ public final class MongoDbConstants {
     public static final String LIMIT = "CamelMongoDbLimit";
     public static final String FROM_TAILABLE = "CamelMongoDbTailable";
     public static final String WRITERESULT = "CamelMongoWriteResult";
+    public static final String OID = "CamelMongoOid";
 
     private MongoDbConstants() {
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/1a6f5c39/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 a3fb73a..653dc71 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
@@ -36,6 +36,7 @@ import org.apache.camel.TypeConverter;
 import org.apache.camel.impl.DefaultProducer;
 import org.apache.camel.util.MessageHelper;
 import org.apache.camel.util.ObjectHelper;
+import org.bson.types.ObjectId;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -253,9 +254,17 @@ public class MongoDbProducer extends DefaultProducer {
         WriteResult result;
         WriteConcern wc = extractWriteConcern(exchange);
         if (singleInsert) {
-            result = wc == null ? dbCol.insert((DBObject)insert) : dbCol.insert((DBObject)insert,
wc);
+            DBObject insertObject = (DBObject) insert;
+            result = wc == null ? dbCol.insert(insertObject) : dbCol.insert(insertObject,
wc);
+            exchange.getIn().setHeader(MongoDbConstants.OID, insertObject.get("_id"));
         } else {
-            result = wc == null ? dbCol.insert((List<DBObject>)insert) : dbCol.insert((List<DBObject>)insert,
wc);
+            List<DBObject> insertObjects = (List<DBObject>) insert;
+            result = wc == null ? dbCol.insert(insertObjects) : dbCol.insert(insertObjects,
wc);
+            List<ObjectId> oids = new ArrayList<ObjectId>(insertObjects.size());
+            for (DBObject insertObject : insertObjects) {
+                oids.add((ObjectId) insertObject.get("_id"));
+            }
+            exchange.getIn().setHeader(MongoDbConstants.OID, oids);
         }
 
         Message resultMessage = prepareResponseMessage(exchange, MongoDbOperation.insert);

http://git-wip-us.apache.org/repos/asf/camel/blob/1a6f5c39/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbOperationsTest.java
----------------------------------------------------------------------
diff --git a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbOperationsTest.java
b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbOperationsTest.java
index 51caa1c..0817754 100644
--- a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbOperationsTest.java
+++ b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbOperationsTest.java
@@ -19,12 +19,15 @@ package org.apache.camel.component.mongodb;
 import java.util.Formatter;
 import java.util.List;
 
+import static java.util.Arrays.asList;
+
 import com.mongodb.BasicDBObject;
 import com.mongodb.DBObject;
 import com.mongodb.WriteResult;
 import com.mongodb.util.JSON;
 
 import org.apache.camel.builder.RouteBuilder;
+import org.bson.types.ObjectId;
 import org.junit.Test;
 
 public class MongoDbOperationsTest extends AbstractMongoDbTest {
@@ -51,7 +54,7 @@ public class MongoDbOperationsTest extends AbstractMongoDbTest {
         assertEquals("Dynamic collection should contain 1 record", 1L, result);
         
     }
-    
+
     @Test
     public void testInsertString() throws Exception {
         assertEquals(0, testCollection.count());
@@ -60,19 +63,23 @@ public class MongoDbOperationsTest extends AbstractMongoDbTest {
         DBObject b = testCollection.findOne("testInsertString");
         assertNotNull("No record with 'testInsertString' _id", b);
     }
-    
+
     @Test
-    public void testInsertArrayStrings() throws Exception {
-        assertEquals(0, testCollection.count());
-        Object[] req = new Object[] {"{\"_id\":\"testInsertArrayStrings\", \"scientist\":\"Einstein\"}",
"{\"_id\":\"testInsertArrayStrings2\", \"scientist\":\"Copernicus\"}"};
-        Object result = template.requestBody("direct:insert", req);
-        assertTrue(result instanceof WriteResult);
-        DBObject b = testCollection.findOne("testInsertArrayStrings");
-        assertNotNull("No record with 'testInsertArrayStrings' _id", b);
-        b = testCollection.findOne("testInsertArrayStrings2");
-        assertNotNull("No record with 'testInsertArrayStrings2' _id", b);
+    public void testStoreOid() throws Exception {
+        DBObject dbObject = new BasicDBObject();
+        ObjectId oid = template.requestBody("direct:testStoreOid", dbObject, ObjectId.class);
+        assertEquals(dbObject.get("_id"), oid);
     }
-    
+
+    @Test
+    public void testStoreOids() throws Exception {
+        DBObject firstDbObject = new BasicDBObject();
+        DBObject secondDbObject = new BasicDBObject();
+        List<ObjectId> oids = template.requestBody("direct:testStoreOid", asList(firstDbObject,
secondDbObject), List.class);
+        assertTrue(oids.contains(firstDbObject.get("_id")));
+        assertTrue(oids.contains(secondDbObject.get("_id")));
+    }
+
     @Test
     public void testSave() throws Exception {
         // Prepare test
@@ -225,6 +232,8 @@ public class MongoDbOperationsTest extends AbstractMongoDbTest {
                 
                 from("direct:count").to("mongodb:myDb?database={{mongodb.testDb}}&collection={{mongodb.testCollection}}&operation=count&dynamicity=true");
                 from("direct:insert").to("mongodb:myDb?database={{mongodb.testDb}}&collection={{mongodb.testCollection}}&operation=insert&writeConcern=SAFE");
+                from("direct:testStoreOid").to("mongodb:myDb?database={{mongodb.testDb}}&collection={{mongodb.testCollection}}&operation=insert&writeConcern=SAFE").
+                    setBody().header(MongoDbConstants.OID);
                 from("direct:save").to("mongodb:myDb?database={{mongodb.testDb}}&collection={{mongodb.testCollection}}&operation=save&writeConcern=SAFE");
                 from("direct:update").to("mongodb:myDb?database={{mongodb.testDb}}&collection={{mongodb.testCollection}}&operation=update&writeConcern=SAFE");
                 from("direct:remove").to("mongodb:myDb?database={{mongodb.testDb}}&collection={{mongodb.testCollection}}&operation=remove&writeConcern=SAFE");


Mime
View raw message