This is an automated email from the ASF dual-hosted git repository. acosentino pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
commit 86b61ba6b3eb7e2139e804f05ae3e22dc056e4b8 Author: Pasquale Congiusti <[email protected]> AuthorDate: Tue Oct 1 08:24:20 2019 +0200 Adding support for meta and verifier extension Fixes CAMEL-14022 --- .../camel/component/mongodb/MongoDbComponent.java | 4 + .../component/mongodb/conf/MongoConfiguration.java | 85 ++++++++++++ .../mongodb/meta/MongoDBMetaExtension.java | 104 ++++++++++++++ .../verifier/MongoComponentVerifierExtension.java | 101 ++++++++++++++ .../component/mongodb/AbstractMongoDbTest.java | 24 ++++ .../component/mongodb/EmbedMongoConfiguration.java | 2 +- .../mongodb/meta/MongoDbMetaExtensionTest.java | 153 +++++++++++++++++++++ .../verifier/MongoDbVerifierExtensionTest.java | 140 +++++++++++++++++++ 8 files changed, 612 insertions(+), 1 deletion(-) diff --git a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbComponent.java b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbComponent.java index 032620c..c0a9838 100644 --- a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbComponent.java +++ b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbComponent.java @@ -24,6 +24,8 @@ import java.util.Set; import com.mongodb.MongoClient; import org.apache.camel.CamelContext; import org.apache.camel.Endpoint; +import org.apache.camel.component.mongodb.meta.MongoDBMetaExtension; +import org.apache.camel.component.mongodb.verifier.MongoComponentVerifierExtension; import org.apache.camel.spi.annotations.Component; import org.apache.camel.support.DefaultComponent; @@ -50,6 +52,8 @@ public class MongoDbComponent extends DefaultComponent { public MongoDbComponent(CamelContext context) { super(context); + registerExtension(MongoComponentVerifierExtension::new); + registerExtension(MongoDBMetaExtension::new); } @Override diff --git a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/conf/MongoConfiguration.java b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/conf/MongoConfiguration.java new file mode 100644 index 0000000..1d8509f --- /dev/null +++ b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/conf/MongoConfiguration.java @@ -0,0 +1,85 @@ +/* + * 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.conf; + +import java.util.Map; + +public class MongoConfiguration { + + private String host = "localhost:27017"; + private String user; + private String password; + private String adminDB = "admin"; + + public MongoConfiguration() { + super(); + } + + public MongoConfiguration(Map<String, String> parameters) { + super(); + this.host = parameters.get("host"); + this.user = parameters.get("user"); + this.password = parameters.get("password"); + // Optional parameters + String optionalAdminDB = parameters.getOrDefault("adminDB", ""); + if (!"".equals(optionalAdminDB)) { + this.adminDB = optionalAdminDB; + } + } + + public String getHost() { + return host; + } + + public void setHost(String host) { + this.host = host; + } + + public String getUser() { + return user; + } + + public void setUser(String user) { + this.user = user; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getAdminDB() { + return adminDB; + } + + public void setAdminDB(String adminDB) { + this.adminDB = adminDB; + } + + public String getMongoClientURI() { + return String.format("mongodb://%s:%s@%s/%s", this.user, this.password, this.host, this.adminDB); + } + + @Override + public String toString() { + return "MongoConfiguration [host(s)=" + host + ", user=" + user + ", password=***, adminDB=" + adminDB + "]"; + } + +} diff --git a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/meta/MongoDBMetaExtension.java b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/meta/MongoDBMetaExtension.java new file mode 100644 index 0000000..5c493f3 --- /dev/null +++ b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/meta/MongoDBMetaExtension.java @@ -0,0 +1,104 @@ +/* + * 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.meta; + +import java.io.IOException; +import java.util.Map; +import java.util.Optional; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.mongodb.MongoClient; +import com.mongodb.MongoClientURI; +import com.mongodb.client.model.Filters; +import org.apache.camel.CamelContext; +import org.apache.camel.component.extension.metadata.AbstractMetaDataExtension; +import org.apache.camel.component.extension.metadata.MetaDataBuilder; +import org.apache.camel.component.mongodb.MongoDbComponent; +import org.apache.camel.component.mongodb.conf.MongoConfiguration; +import org.bson.Document; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static org.apache.camel.util.CastUtils.cast; + +public class MongoDBMetaExtension extends AbstractMetaDataExtension { + + private static final Logger LOGGER = LoggerFactory.getLogger(MongoDBMetaExtension.class); + + private ObjectMapper objectMapper = new ObjectMapper(); + + public MongoDBMetaExtension() { + this(null); + } + + public MongoDBMetaExtension(CamelContext context) { + super(context); + } + + @Override + public Optional<MetaData> meta(Map<String, Object> parameters) { + Map<String, String> textParameters = cast(parameters); + LOGGER.debug("Fetching mongodb meta information with params: {}", textParameters); + + MongoConfiguration mongoConf = new MongoConfiguration(textParameters); + MongoClientURI connectionURI = new MongoClientURI(mongoConf.getMongoClientURI()); + + JsonNode collectionInfoRoot = null; + try (MongoClient mongoConnection = new MongoClient(connectionURI)) { + Document collectionInfo = mongoConnection.getDatabase(textParameters.get("database")) + .listCollections() + .filter(Filters.eq("name", textParameters.get("collection"))).first(); + LOGGER.debug("Collection info: {}", collectionInfo); + if (collectionInfo == null) { + LOGGER.error("Cannot read information for collection {}.{}", textParameters.get("database"), + textParameters.get("collection")); + return Optional.empty(); + } + String collectionInfoJson = collectionInfo.toJson(); + collectionInfoRoot = objectMapper.readTree(collectionInfoJson.replaceAll("bsonType", "type")); + } catch (IOException e) { + LOGGER.error("Error occurred while reading schema information", e); + return Optional.empty(); + } + + JsonNode schemaRoot = collectionInfoRoot.path("options").path("validator").path("$jsonSchema"); + + if (!schemaRoot.isMissingNode()) { + ObjectNode root = (ObjectNode) schemaRoot; + root.put("$schema", "http://json-schema.org/schema#"); + root.put("id", String.format("urn:jsonschema:%s:%s:%s)", + "org:apache:camel:component:mongodb", + textParameters.get("database"), + textParameters.get("collection"))); + + return Optional.of( + MetaDataBuilder.on(getCamelContext()) + .withAttribute(MetaData.CONTENT_TYPE, "application/schema+json") + .withAttribute(MetaData.JAVA_TYPE, JsonNode.class) + .withPayload(root) + .build() + ); + } else { + LOGGER.warn("Cannot retrieve info for : {}.{} collection. Likely the collection has not been provided with a validator", + textParameters.get("database"), + textParameters.get("collection")); + return Optional.empty(); + } + } +} diff --git a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/verifier/MongoComponentVerifierExtension.java b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/verifier/MongoComponentVerifierExtension.java new file mode 100644 index 0000000..99d05c9 --- /dev/null +++ b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/verifier/MongoComponentVerifierExtension.java @@ -0,0 +1,101 @@ +/* + * 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.verifier; + +import java.util.Map; + +import com.mongodb.MongoClient; +import com.mongodb.MongoClientOptions; +import com.mongodb.MongoClientURI; +import com.mongodb.MongoSecurityException; +import com.mongodb.MongoTimeoutException; +import org.apache.camel.component.extension.verifier.DefaultComponentVerifierExtension; +import org.apache.camel.component.extension.verifier.ResultBuilder; +import org.apache.camel.component.extension.verifier.ResultErrorBuilder; +import org.apache.camel.component.extension.verifier.ResultErrorHelper; +import org.apache.camel.component.mongodb.conf.MongoConfiguration; +import org.bson.Document; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static org.apache.camel.util.CastUtils.cast; + +public class MongoComponentVerifierExtension extends DefaultComponentVerifierExtension { + private static final Logger LOG = LoggerFactory.getLogger(MongoComponentVerifierExtension.class); + + private static final int CONNECTION_TIMEOUT = 2000; + + public MongoComponentVerifierExtension() { + super("mongodb"); + } + + @Override + public Result verifyParameters(Map<String, Object> parameters) { + ResultBuilder builder = ResultBuilder.withStatusAndScope(Result.Status.OK, Scope.PARAMETERS) + .error(ResultErrorHelper.requiresOption("host", parameters)) + .error(ResultErrorHelper.requiresOption("user", parameters)) + .error(ResultErrorHelper.requiresOption("password", parameters)); + return builder.build(); + } + + @Override + public Result verifyConnectivity(Map<String, Object> parameters) { + return ResultBuilder.withStatusAndScope(Result.Status.OK, Scope.CONNECTIVITY) + .error(parameters, this::verifyCredentials) + .build(); + } + + private void verifyCredentials(ResultBuilder builder, Map<String, Object> parameters) { + MongoConfiguration mongoConf = new MongoConfiguration(cast(parameters)); + MongoClientOptions.Builder optionsBuilder = MongoClientOptions.builder(); + // Avoid retry and long timeout + optionsBuilder.connectTimeout(CONNECTION_TIMEOUT); + optionsBuilder.serverSelectionTimeout(CONNECTION_TIMEOUT); + optionsBuilder.maxWaitTime(CONNECTION_TIMEOUT); + MongoClientURI connectionURI = new MongoClientURI(mongoConf.getMongoClientURI(), optionsBuilder); + + LOG.info("Testing connection against {}", connectionURI); + try (MongoClient mongoClient = new MongoClient(connectionURI)) { + // Just ping the server + mongoClient.getDatabase(connectionURI.getDatabase()).runCommand(Document.parse("{ ping: 1 }")); + LOG.info("Testing connection successful!"); + } catch (MongoSecurityException e) { + ResultErrorBuilder errorBuilder = ResultErrorBuilder.withCodeAndDescription( + VerificationError.StandardCode.AUTHENTICATION, + String.format("Unable to authenticate %s against %s authentication database!", mongoConf.getUser(), mongoConf.getAdminDB())); + builder.error(errorBuilder.build()); + } catch (MongoTimeoutException e) { + // When there is any connection exception, the driver tries to reconnect until timeout is reached + // wrapping the original socket exception into a timeout exception + String description = null; + VerificationError.StandardCode code = VerificationError.StandardCode.GENERIC; + if (e.getMessage().contains("com.mongodb.MongoSecurityException")) { + code = VerificationError.StandardCode.AUTHENTICATION; + description = String.format("Unable to authenticate %s against %s authentication database!", mongoConf.getUser(), mongoConf.getAdminDB()); + } else if (e.getMessage().contains("com.mongodb.MongoSocket") && e.getMessage().contains("Exception")) { + description = String.format("Unable to connect to %s!", mongoConf.getHost()); + } else { + description = String.format("Generic exception while connecting to %s!", mongoConf.getHost()); + } + ResultErrorBuilder errorBuilder = ResultErrorBuilder.withCodeAndDescription( + code, + String.format(description)); + builder.error(errorBuilder.build()); + } + } +} + diff --git a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/AbstractMongoDbTest.java b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/AbstractMongoDbTest.java index ea35154..ad361d3 100644 --- a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/AbstractMongoDbTest.java +++ b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/AbstractMongoDbTest.java @@ -34,6 +34,11 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext public abstract class AbstractMongoDbTest extends CamelTestSupport { + protected static final String SCHEME = "mongodb"; + protected static final String HOST = "localhost:" + EmbedMongoConfiguration.PORT; + protected static final String USER = "test-user"; + protected static final String PASSWORD = "test-pwd"; + protected static MongoClient mongo; protected static MongoDatabase db; protected static MongoCollection<Document> testCollection; @@ -83,6 +88,25 @@ public abstract class AbstractMongoDbTest extends CamelTestSupport { return ctx; } + /** + * Useful to simulate the presence of an authenticated user with + * name {@value #USER} and password {@value #PASSWORD} + */ + protected void createAuthorizationUser() { + MongoDatabase admin = mongo.getDatabase("admin"); + MongoCollection<Document> usersCollection = admin.getCollection("system.users"); + if (usersCollection.count() == 0) { + usersCollection.insertOne(Document.parse("{\n" + " \"_id\": \"admin.test-user\",\n" + + " \"user\": \"test-user\",\n" + " \"db\": \"admin\",\n" + " \"credentials\": {\n" + + " \"SCRAM-SHA-1\": {\n" + " \"iterationCount\": 10000,\n" + + " \"salt\": \"gmmPAoNdvFSWCV6PGnNcAw==\",\n" + + " \"storedKey\": \"qE9u1Ax7Y40hisNHL2b8/LAvG7s=\",\n" + + " \"serverKey\": \"RefeJcxClt9JbOP/VnrQ7YeQh6w=\"\n" + " }\n" + " },\n" + + " \"roles\": [\n" + " {\n" + " \"role\": \"readWrite\",\n" + + " \"db\": \"test\"\n" + " }\n" + " ]\n" + "}" + "")); + } + } + protected void pumpDataIntoTestCollection() { // there should be 100 of each String[] scientists = {"Einstein", "Darwin", "Copernicus", "Pasteur", "Curie", "Faraday", "Newton", "Bohr", "Galilei", "Maxwell"}; diff --git a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/EmbedMongoConfiguration.java b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/EmbedMongoConfiguration.java index 3cec1f0..0c81499 100644 --- a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/EmbedMongoConfiguration.java +++ b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/EmbedMongoConfiguration.java @@ -43,7 +43,7 @@ import static org.springframework.util.SocketUtils.findAvailableTcpPort; @Configuration public class EmbedMongoConfiguration { - private static final int PORT = findAvailableTcpPort(); + public static final int PORT = findAvailableTcpPort(); static { try { diff --git a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/meta/MongoDbMetaExtensionTest.java b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/meta/MongoDbMetaExtensionTest.java new file mode 100644 index 0000000..51ee8a5 --- /dev/null +++ b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/meta/MongoDbMetaExtensionTest.java @@ -0,0 +1,153 @@ +/* + * 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.meta; + +import java.util.HashMap; +import java.util.Map; + +import com.fasterxml.jackson.databind.JsonNode; +import com.mongodb.client.model.CreateCollectionOptions; +import com.mongodb.client.model.Filters; +import com.mongodb.client.model.ValidationOptions; +import org.apache.camel.component.extension.MetaDataExtension; +import org.apache.camel.component.mongodb.AbstractMongoDbTest; +import org.apache.camel.component.mongodb.MongoDbComponent; +import org.bson.Document; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class MongoDbMetaExtensionTest extends AbstractMongoDbTest { + + private static final Logger LOGGER = LoggerFactory.getLogger(MongoDbMetaExtensionTest.class); + + // We simulate the presence of an authenticated user + @Before + public void createAuthorizationUser() { + super.createAuthorizationUser(); + } + + @Override + public boolean isUseRouteBuilder() { + return false; + } + + protected MongoDbComponent getComponent() { + return context().getComponent(SCHEME, MongoDbComponent.class); + } + + @Test + public void testValidMetaData() throws Exception { + // When + final String database = "test"; + final String collection = "validatedCollection"; + MongoDbComponent component = this.getComponent(); + // Given + Document jsonSchema = Document.parse("{ \n" + + " bsonType: \"object\", \n" + + " required: [ \"name\", \"surname\", \"email\" ], \n" + + " properties: { \n" + + " name: { \n" + + " bsonType: \"string\", \n" + + " description: \"required and must be a string\" }, \n" + + " surname: { \n" + + " bsonType: \"string\", \n" + + " description: \"required and must be a string\" }, \n" + + " email: { \n" + + " bsonType: \"string\", \n" + + " pattern: \"^.+@.+$\", \n" + + " description: \"required and must be a valid email address\" }, \n" + + " year_of_birth: { \n" + + " bsonType: \"int\", \n" + + " minimum: 1900, \n" + + " maximum: 2018,\n" + + " description: \"the value must be in the range 1900-2018\" }, \n" + + " gender: { \n" + + " enum: [ \"M\", \"F\" ], \n" + + " description: \"can be only M or F\" } \n" + + " }}"); + ValidationOptions collOptions = new ValidationOptions().validator(Filters.jsonSchema(jsonSchema)); + AbstractMongoDbTest.mongo.getDatabase(database).createCollection(collection, + new CreateCollectionOptions().validationOptions(collOptions)); + Map<String, Object> parameters = new HashMap<>(); + parameters.put("database", database); + parameters.put("collection", collection); + parameters.put("host", HOST); + parameters.put("user", USER); + parameters.put("password", PASSWORD); + + MetaDataExtension.MetaData result = component.getExtension(MetaDataExtension.class).get().meta(parameters).orElseThrow(UnsupportedOperationException::new); + // Then + assertEquals("application/schema+json", result.getAttribute(MetaDataExtension.MetaData.CONTENT_TYPE)); + assertEquals(JsonNode.class, result.getAttribute(MetaDataExtension.MetaData.JAVA_TYPE)); + assertNotNull(result.getPayload(JsonNode.class)); + assertNotNull(result.getPayload(JsonNode.class).get("properties")); + assertNotNull(result.getPayload(JsonNode.class).get("$schema")); + assertEquals("http://json-schema.org/schema#", result.getPayload(JsonNode.class).get("$schema").asText()); + assertNotNull(result.getPayload(JsonNode.class).get("id")); + assertNotNull(result.getPayload(JsonNode.class).get("type")); + } + + @Test(expected = IllegalArgumentException.class) + public void testMissingCollection() throws Exception { + // When + final String database = "test"; + final String collection = "missingCollection"; + MongoDbComponent component = this.getComponent(); + // Given + Map<String, Object> parameters = new HashMap<>(); + parameters.put("database", database); + parameters.put("collection", collection); + parameters.put("host", HOST); + parameters.put("user", USER); + parameters.put("password", PASSWORD); + + // Then + MetaDataExtension.MetaData result = component.getExtension(MetaDataExtension.class).get().meta(parameters).orElseThrow(IllegalArgumentException::new); + } + + @Test(expected = IllegalArgumentException.class) + public void testMissingParameters() throws Exception { + // When + MongoDbComponent component = this.getComponent(); + // Given + Map<String, Object> parameters = new HashMap<>(); + // Then + MetaDataExtension.MetaData result = component.getExtension(MetaDataExtension.class).get().meta(parameters).orElseThrow(IllegalArgumentException::new); + } + + @Test(expected = UnsupportedOperationException.class) + public void testNotValidatedCollection() throws Exception { + // When + final String database = "test"; + final String collection = "notValidatedCollection"; + MongoDbComponent component = this.getComponent(); + AbstractMongoDbTest.mongo.getDatabase(database).createCollection(collection); + // Given + Map<String, Object> parameters = new HashMap<>(); + parameters.put("database", database); + parameters.put("collection", collection); + parameters.put("host", HOST); + parameters.put("user", USER); + parameters.put("password", PASSWORD); + + // Then + MetaDataExtension.MetaData result = component.getExtension(MetaDataExtension.class).get().meta(parameters).orElseThrow(UnsupportedOperationException::new); + } + +} diff --git a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/verifier/MongoDbVerifierExtensionTest.java b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/verifier/MongoDbVerifierExtensionTest.java new file mode 100644 index 0000000..010bc98 --- /dev/null +++ b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/verifier/MongoDbVerifierExtensionTest.java @@ -0,0 +1,140 @@ +/* + * 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.verifier; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.camel.Component; +import org.apache.camel.component.extension.ComponentVerifierExtension; +import org.apache.camel.component.mongodb.AbstractMongoDbTest; +import org.apache.camel.component.mongodb.MongoDbComponent; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class MongoDbVerifierExtensionTest extends AbstractMongoDbTest { + + private static final Logger LOGGER = LoggerFactory.getLogger(MongoDbVerifierExtensionTest.class); + + // We simulate the presence of an authenticated user + @Before + public void createAuthorizationUser() { + super.createAuthorizationUser(); + } + + @Override + public boolean isUseRouteBuilder() { + return false; + } + + protected MongoDbComponent getComponent() { + return context().getComponent(SCHEME, MongoDbComponent.class); + } + + protected ComponentVerifierExtension getExtension() { + Component component = context().getComponent(SCHEME); + ComponentVerifierExtension verifier = component.getExtension(ComponentVerifierExtension.class).orElseThrow(IllegalStateException::new); + + return verifier; + } + + @Test + public void verifyConnectionOK() { + //When + Map<String, Object> parameters = new HashMap<>(); + parameters.put("host", HOST); + parameters.put("user", USER); + parameters.put("password", PASSWORD); + //Given + ComponentVerifierExtension.Result result = getExtension().verify(ComponentVerifierExtension.Scope.CONNECTIVITY, parameters); + //Then + assertEquals(ComponentVerifierExtension.Result.Status.OK, result.getStatus()); + } + + @Test + public void verifyConnectionKO() { + //When + Map<String, Object> parameters = new HashMap<>(); + parameters.put("host", "notReachableHost"); + parameters.put("user", USER); + parameters.put("password", PASSWORD); + //Given + ComponentVerifierExtension.Result result = getExtension().verify(ComponentVerifierExtension.Scope.CONNECTIVITY, parameters); + //Then + assertEquals(ComponentVerifierExtension.Result.Status.ERROR, result.getStatus()); + assertTrue(result.getErrors().get(0).getDescription().startsWith("Unable to connect")); + } + + @Test + public void verifyConnectionMissingParams() { + //When + Map<String, Object> parameters = new HashMap<>(); + parameters.put("host", HOST); + parameters.put("user", USER); + //Given + ComponentVerifierExtension.Result result = getExtension().verify(ComponentVerifierExtension.Scope.PARAMETERS, parameters); + //Then + assertEquals(ComponentVerifierExtension.Result.Status.ERROR, result.getStatus()); + assertTrue(result.getErrors().get(0).getDescription().startsWith("password should be set")); + } + + @Test + public void verifyConnectionNotAuthenticated() { + //When + Map<String, Object> parameters = new HashMap<>(); + parameters.put("host", HOST); + parameters.put("user", USER); + parameters.put("password", "wrongPassword"); + //Given + ComponentVerifierExtension.Result result = getExtension().verify(ComponentVerifierExtension.Scope.CONNECTIVITY, parameters); + //Then + assertEquals(ComponentVerifierExtension.Result.Status.ERROR, result.getStatus()); + assertTrue(result.getErrors().get(0).getDescription().startsWith("Unable to authenticate")); + } + + @Test + public void verifyConnectionAdminDBKO() { + //When + Map<String, Object> parameters = new HashMap<>(); + parameters.put("host", HOST); + parameters.put("user", USER); + parameters.put("password", PASSWORD); + parameters.put("adminDB", "someAdminDB"); + //Given + ComponentVerifierExtension.Result result = getExtension().verify(ComponentVerifierExtension.Scope.CONNECTIVITY, parameters); + //Then + assertEquals(ComponentVerifierExtension.Result.Status.ERROR, result.getStatus()); + assertTrue(result.getErrors().get(0).getDescription().startsWith("Unable to authenticate")); + } + + @Test + public void verifyConnectionPortKO() { + //When + Map<String, Object> parameters = new HashMap<>(); + parameters.put("host", "localhost:12343"); + parameters.put("user", USER); + parameters.put("password", PASSWORD); + //Given + ComponentVerifierExtension.Result result = getExtension().verify(ComponentVerifierExtension.Scope.CONNECTIVITY, parameters); + //Then + assertEquals(ComponentVerifierExtension.Result.Status.ERROR, result.getStatus()); + assertTrue(result.getErrors().get(0).getDescription().startsWith("Unable to connect")); + } + +}
