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

https://github.com/apache/camel · Java · 133 lines · 84 code · 14 blank · 35 comment · 0 complexity · 18ed19f4b0174d509a99c52581d3d794 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.verifier;
  18. import java.util.HashMap;
  19. import java.util.Map;
  20. import org.apache.camel.Component;
  21. import org.apache.camel.component.extension.ComponentVerifierExtension;
  22. import org.apache.camel.component.mongodb.AbstractMongoDbTest;
  23. import org.junit.jupiter.api.BeforeEach;
  24. import org.junit.jupiter.api.Test;
  25. import static org.junit.jupiter.api.Assertions.assertEquals;
  26. import static org.junit.jupiter.api.Assertions.assertTrue;
  27. public class MongoDbVerifierExtensionTest extends AbstractMongoDbTest {
  28. // We simulate the presence of an authenticated user
  29. @BeforeEach
  30. public void createAuthorizationUser() {
  31. super.createAuthorizationUser();
  32. }
  33. @Override
  34. public boolean isUseRouteBuilder() {
  35. return false;
  36. }
  37. protected ComponentVerifierExtension getExtension() {
  38. Component component = context().getComponent(SCHEME);
  39. ComponentVerifierExtension verifier = component.getExtension(ComponentVerifierExtension.class).orElseThrow(IllegalStateException::new);
  40. return verifier;
  41. }
  42. @Test
  43. public void verifyConnectionOK() {
  44. //When
  45. Map<String, Object> parameters = new HashMap<>();
  46. parameters.put("host", container.getConnectionAddress());
  47. parameters.put("user", USER);
  48. parameters.put("password", PASSWORD);
  49. //Given
  50. ComponentVerifierExtension.Result result = getExtension().verify(ComponentVerifierExtension.Scope.CONNECTIVITY, parameters);
  51. //Then
  52. assertEquals(ComponentVerifierExtension.Result.Status.OK, result.getStatus());
  53. }
  54. @Test
  55. public void verifyConnectionKO() {
  56. //When
  57. Map<String, Object> parameters = new HashMap<>();
  58. parameters.put("host", "notReachable.host");
  59. parameters.put("user", USER);
  60. parameters.put("password", PASSWORD);
  61. //Given
  62. ComponentVerifierExtension.Result result = getExtension().verify(ComponentVerifierExtension.Scope.CONNECTIVITY, parameters);
  63. //Then
  64. assertEquals(ComponentVerifierExtension.Result.Status.ERROR, result.getStatus());
  65. assertTrue(result.getErrors().get(0).getDescription().startsWith("Unable to connect"));
  66. }
  67. @Test
  68. public void verifyConnectionMissingParams() {
  69. //When
  70. Map<String, Object> parameters = new HashMap<>();
  71. parameters.put("host", container.getConnectionAddress());
  72. parameters.put("user", USER);
  73. //Given
  74. ComponentVerifierExtension.Result result = getExtension().verify(ComponentVerifierExtension.Scope.PARAMETERS, parameters);
  75. //Then
  76. assertEquals(ComponentVerifierExtension.Result.Status.ERROR, result.getStatus());
  77. assertTrue(result.getErrors().get(0).getDescription().startsWith("password should be set"));
  78. }
  79. @Test
  80. public void verifyConnectionNotAuthenticated() {
  81. //When
  82. Map<String, Object> parameters = new HashMap<>();
  83. parameters.put("host", container.getConnectionAddress());
  84. parameters.put("user", USER);
  85. parameters.put("password", "wrongPassword");
  86. //Given
  87. ComponentVerifierExtension.Result result = getExtension().verify(ComponentVerifierExtension.Scope.CONNECTIVITY, parameters);
  88. //Then
  89. assertEquals(ComponentVerifierExtension.Result.Status.ERROR, result.getStatus());
  90. assertTrue(result.getErrors().get(0).getDescription().startsWith("Unable to authenticate"));
  91. }
  92. @Test
  93. public void verifyConnectionAdminDBKO() {
  94. //When
  95. Map<String, Object> parameters = new HashMap<>();
  96. parameters.put("host", container.getConnectionAddress());
  97. parameters.put("user", USER);
  98. parameters.put("password", PASSWORD);
  99. parameters.put("adminDB", "someAdminDB");
  100. //Given
  101. ComponentVerifierExtension.Result result = getExtension().verify(ComponentVerifierExtension.Scope.CONNECTIVITY, parameters);
  102. //Then
  103. assertEquals(ComponentVerifierExtension.Result.Status.ERROR, result.getStatus());
  104. assertTrue(result.getErrors().get(0).getDescription().startsWith("Unable to authenticate"));
  105. }
  106. @Test
  107. public void verifyConnectionPortKO() {
  108. //When
  109. Map<String, Object> parameters = new HashMap<>();
  110. parameters.put("host", "localhost:12343");
  111. parameters.put("user", USER);
  112. parameters.put("password", PASSWORD);
  113. //Given
  114. ComponentVerifierExtension.Result result = getExtension().verify(ComponentVerifierExtension.Scope.CONNECTIVITY, parameters);
  115. //Then
  116. assertEquals(ComponentVerifierExtension.Result.Status.ERROR, result.getStatus());
  117. assertTrue(result.getErrors().get(0).getDescription().startsWith("Unable to connect"));
  118. }
  119. }