/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbInsertBatchTest.java

https://github.com/gnodet/camel · Java · 74 lines · 47 code · 11 blank · 16 comment · 0 complexity · 456f34c172031dfef35b7e76ef10c97c MD5 · raw file

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.camel.component.mongodb;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import com.mongodb.BasicDBObject;
  21. import org.apache.camel.Exchange;
  22. import org.apache.camel.builder.RouteBuilder;
  23. import org.bson.Document;
  24. import org.junit.jupiter.api.Test;
  25. import static org.junit.jupiter.api.Assertions.*;
  26. public class MongoDbInsertBatchTest extends AbstractMongoDbTest {
  27. @Test
  28. public void testInsertBatch() {
  29. assertEquals(0, testCollection.countDocuments());
  30. Document a = new Document(MongoDbConstants.MONGO_ID, "testInsert1");
  31. a.append("MyId", 1).toJson();
  32. Document b = new Document(MongoDbConstants.MONGO_ID, "testInsert2");
  33. b.append("MyId", 2).toJson();
  34. Document c = new Document(MongoDbConstants.MONGO_ID, "testInsert3");
  35. c.append("MyId", 3).toJson();
  36. List<Document> taxGroupList = new ArrayList<>();
  37. taxGroupList.add(a);
  38. taxGroupList.add(b);
  39. taxGroupList.add(c);
  40. Exchange out = fluentTemplate.to("direct:insert").withBody(taxGroupList).send();
  41. List oid = out.getMessage().getHeader(MongoDbConstants.OID, List.class);
  42. assertNotNull(oid);
  43. assertEquals(3, oid.size());
  44. Document out1 = testCollection.find(new BasicDBObject("_id", oid.get(0))).first();
  45. assertNotNull(out1);
  46. assertEquals(1, out1.getInteger("MyId"));
  47. Document out2 = testCollection.find(new BasicDBObject("_id", oid.get(1))).first();
  48. assertNotNull(out2);
  49. assertEquals(2, out2.getInteger("MyId"));
  50. Document out3 = testCollection.find(new BasicDBObject("_id", oid.get(2))).first();
  51. assertNotNull(out3);
  52. assertEquals(3, out3.getInteger("MyId"));
  53. }
  54. @Override
  55. protected RouteBuilder createRouteBuilder() throws Exception {
  56. return new RouteBuilder() {
  57. public void configure() {
  58. from("direct:insert")
  59. .to("mongodb:myDb?database={{mongodb.testDb}}&collection={{mongodb.testCollection}}&operation=insert");
  60. }
  61. };
  62. }
  63. }