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

https://github.com/gnodet/camel · Java · 84 lines · 52 code · 16 blank · 16 comment · 0 complexity · 5ae2fe0a9225ea3bfc561dc929a56651 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 com.mongodb.client.MongoClient;
  19. import com.mongodb.client.MongoClients;
  20. import org.apache.camel.Endpoint;
  21. import org.junit.jupiter.api.Test;
  22. import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf;
  23. import static org.junit.jupiter.api.Assertions.assertEquals;
  24. import static org.junit.jupiter.api.Assertions.assertNotEquals;
  25. import static org.junit.jupiter.api.Assertions.assertThrows;
  26. public class MongoDbConnectionBeansTest extends AbstractMongoDbTest {
  27. @Test
  28. public void checkConnectionFromProperties() {
  29. MongoClient client = MongoClients.create(service.getReplicaSetUrl());
  30. context.getComponent(SCHEME, MongoDbComponent.class).setMongoConnection(null);
  31. context.getRegistry().bind("myDb", client);
  32. MongoDbEndpoint testEndpoint = context.getEndpoint("mongodb:anyName?mongoConnection=#myDb", MongoDbEndpoint.class);
  33. assertNotEquals("myDb", testEndpoint.getConnectionBean());
  34. assertEquals(client, testEndpoint.getMongoConnection());
  35. }
  36. @Test
  37. public void checkConnectionFromBean() {
  38. MongoClient client = MongoClients.create(service.getReplicaSetUrl());
  39. context.getComponent(SCHEME, MongoDbComponent.class).setMongoConnection(null);
  40. context.getRegistry().bind("myDb", client);
  41. MongoDbEndpoint testEndpoint = context.getEndpoint("mongodb:myDb", MongoDbEndpoint.class);
  42. assertEquals("myDb", testEndpoint.getConnectionBean());
  43. assertEquals(client, testEndpoint.getMongoConnection());
  44. }
  45. @Test
  46. public void checkConnectionBothExisting() {
  47. MongoClient client1 = MongoClients.create(service.getReplicaSetUrl());
  48. MongoClient client2 = MongoClients.create(service.getReplicaSetUrl());
  49. context.getComponent(SCHEME, MongoDbComponent.class).setMongoConnection(null);
  50. context.getRegistry().bind("myDb", client1);
  51. context.getRegistry().bind("myDbS", client2);
  52. MongoDbEndpoint testEndpoint = context.getEndpoint("mongodb:myDb?mongoConnection=#myDbS", MongoDbEndpoint.class);
  53. MongoClient myDbS = context.getRegistry().lookupByNameAndType("myDbS", MongoClient.class);
  54. assertEquals("myDb", testEndpoint.getConnectionBean());
  55. assertEquals(myDbS, testEndpoint.getMongoConnection());
  56. }
  57. @Test
  58. public void checkMissingConnection() {
  59. context.getComponent(SCHEME, MongoDbComponent.class).setMongoConnection(null);
  60. assertThrows(Exception.class, () -> context.getEndpoint("mongodb:anythingNotRelated", MongoDbEndpoint.class));
  61. }
  62. @Test
  63. public void checkConnectionOnComponent() throws Exception {
  64. Endpoint endpoint = context.getEndpoint("mongodb:justARouteName");
  65. assertIsInstanceOf(MongoDbEndpoint.class, endpoint);
  66. assertEquals(mongo, ((MongoDbEndpoint) endpoint).getMongoConnection());
  67. }
  68. }