You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2020/10/14 09:08:44 UTC

[camel] branch master updated: CAMEL-15646: camel-mongodb - Batch insert does not work running on spring boot.

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

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new c42e80c  CAMEL-15646: camel-mongodb - Batch insert does not work running on spring boot.
c42e80c is described below

commit c42e80c2b3bfc80312fc7becae28f5d9de16a559
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Oct 14 10:59:17 2020 +0200

    CAMEL-15646: camel-mongodb - Batch insert does not work running on spring boot.
---
 .../camel/component/mongodb/MongoDbProducer.java   | 35 ++++++----
 .../component/mongodb/MongoDbInsertBatchTest.java  | 74 ++++++++++++++++++++++
 2 files changed, 96 insertions(+), 13 deletions(-)

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 53a844e..f95805d 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
@@ -17,6 +17,8 @@
 package org.apache.camel.component.mongodb;
 
 import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
@@ -233,7 +235,7 @@ public class MongoDbProducer extends DefaultProducer {
     }
 
     @SuppressWarnings("rawtypes")
-    private List<Document> attemptConvertToList(List insertList, Exchange exchange) throws CamelMongoDbException {
+    private List<Document> attemptConvertToList(Collection insertList, Exchange exchange) throws CamelMongoDbException {
         List<Document> documentList = new ArrayList<>(insertList.size());
         TypeConverter converter = exchange.getContext().getTypeConverter();
         for (Object item : insertList) {
@@ -424,28 +426,35 @@ public class MongoDbProducer extends DefaultProducer {
     private Function<Exchange, Object> createDoInsert() {
         return exchange -> {
             MongoCollection<Document> dbCol = calculateCollection(exchange);
+
+            // is it batch or single insert
             boolean singleInsert = true;
-            Object insert = exchange.getContext().getTypeConverter().tryConvertTo(Document.class, exchange,
-                    exchange.getIn().getBody());
-            // body could not be converted to Document, check to see if it's of
-            // type List<Document>
-            if (insert == null) {
-                insert = exchange.getIn().getBody(List.class);
+            Object insert = exchange.getIn().getBody();
+            boolean array = insert != null && insert.getClass().isArray();
+            if (array) {
+                Object[] arr = (Object[]) insert;
+                insert = Arrays.asList(arr);
+            }
+            if (insert instanceof Collection) {
                 // if the body of type List was obtained, ensure that all items
                 // are of type Document and cast the List to List<Document>
-                if (insert != null) {
-                    singleInsert = false;
-                    insert = attemptConvertToList((List<?>) insert, exchange);
-                } else {
+                singleInsert = false;
+                insert = attemptConvertToList((Collection<?>) insert, exchange);
+            } else {
+                // okay its not a list, then maybe its a document
+                if (!(insert instanceof Document)) {
+                    // try to convert to document
+                    insert = exchange.getContext().getTypeConverter().tryConvertTo(Document.class, exchange, insert);
+                }
+                if (insert == null) {
                     throw new CamelMongoDbException(
                             "MongoDB operation = insert, Body is not conversible to type Document nor List<Document>");
                 }
             }
 
             if (singleInsert) {
-                Document insertObject = Document.class.cast(insert);
+                Document insertObject = (Document) insert;
                 dbCol.insertOne(insertObject);
-
                 exchange.getIn().setHeader(OID, insertObject.get(MONGO_ID));
             } else {
                 @SuppressWarnings("unchecked")
diff --git a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbInsertBatchTest.java b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbInsertBatchTest.java
new file mode 100644
index 0000000..6999a72
--- /dev/null
+++ b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbInsertBatchTest.java
@@ -0,0 +1,74 @@
+/*
+ * 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.util.ArrayList;
+import java.util.List;
+
+import com.mongodb.BasicDBObject;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.bson.Document;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+public class MongoDbInsertBatchTest extends AbstractMongoDbTest {
+
+    @Test
+    public void testInsertBatch() {
+        assertEquals(0, testCollection.countDocuments());
+
+        Document a = new Document(MongoDbConstants.MONGO_ID, "testInsert1");
+        a.append("MyId", 1).toJson();
+        Document b = new Document(MongoDbConstants.MONGO_ID, "testInsert2");
+        b.append("MyId", 2).toJson();
+        Document c = new Document(MongoDbConstants.MONGO_ID, "testInsert3");
+        c.append("MyId", 3).toJson();
+
+        List<Document> taxGroupList = new ArrayList<>();
+        taxGroupList.add(a);
+        taxGroupList.add(b);
+        taxGroupList.add(c);
+
+        Exchange out = fluentTemplate.to("direct:insert").withBody(taxGroupList).send();
+
+        List oid = out.getMessage().getHeader(MongoDbConstants.OID, List.class);
+        assertNotNull(oid);
+        assertEquals(3, oid.size());
+
+        Document out1 = testCollection.find(new BasicDBObject("_id", oid.get(0))).first();
+        assertNotNull(out1);
+        assertEquals(1, out1.getInteger("MyId"));
+        Document out2 = testCollection.find(new BasicDBObject("_id", oid.get(1))).first();
+        assertNotNull(out2);
+        assertEquals(2, out2.getInteger("MyId"));
+        Document out3 = testCollection.find(new BasicDBObject("_id", oid.get(2))).first();
+        assertNotNull(out3);
+        assertEquals(3, out3.getInteger("MyId"));
+    }
+
+    @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");
+            }
+        };
+    }
+}