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

https://github.com/apache/camel · Java · 75 lines · 48 code · 11 blank · 16 comment · 0 complexity · c49f9912f215b3eeb3b5fc9fc93c0c38 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.integration;
  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.apache.camel.component.mongodb.MongoDbConstants;
  24. import org.bson.Document;
  25. import org.junit.jupiter.api.Test;
  26. import static org.junit.jupiter.api.Assertions.*;
  27. public class MongoDbInsertBatchIT extends AbstractMongoDbITSupport {
  28. @Test
  29. public void testInsertBatch() {
  30. assertEquals(0, testCollection.countDocuments());
  31. Document a = new Document(MongoDbConstants.MONGO_ID, "testInsert1");
  32. a.append("MyId", 1).toJson();
  33. Document b = new Document(MongoDbConstants.MONGO_ID, "testInsert2");
  34. b.append("MyId", 2).toJson();
  35. Document c = new Document(MongoDbConstants.MONGO_ID, "testInsert3");
  36. c.append("MyId", 3).toJson();
  37. List<Document> taxGroupList = new ArrayList<>();
  38. taxGroupList.add(a);
  39. taxGroupList.add(b);
  40. taxGroupList.add(c);
  41. Exchange out = fluentTemplate.to("direct:insert").withBody(taxGroupList).send();
  42. List oid = out.getMessage().getHeader(MongoDbConstants.OID, List.class);
  43. assertNotNull(oid);
  44. assertEquals(3, oid.size());
  45. Document out1 = testCollection.find(new BasicDBObject("_id", oid.get(0))).first();
  46. assertNotNull(out1);
  47. assertEquals(1, out1.getInteger("MyId"));
  48. Document out2 = testCollection.find(new BasicDBObject("_id", oid.get(1))).first();
  49. assertNotNull(out2);
  50. assertEquals(2, out2.getInteger("MyId"));
  51. Document out3 = testCollection.find(new BasicDBObject("_id", oid.get(2))).first();
  52. assertNotNull(out3);
  53. assertEquals(3, out3.getInteger("MyId"));
  54. }
  55. @Override
  56. protected RouteBuilder createRouteBuilder() throws Exception {
  57. return new RouteBuilder() {
  58. public void configure() {
  59. from("direct:insert")
  60. .to("mongodb:myDb?database={{mongodb.testDb}}&collection={{mongodb.testCollection}}&operation=insert");
  61. }
  62. };
  63. }
  64. }