This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push: new 22f5c3d Move the MongoDB test infrastructure from CKC (#4557) 22f5c3d is described below commit 22f5c3d26d112a361f2d2907931f168938ff19ad Author: Otavio Rodolfo Piske <orpi...@users.noreply.github.com> AuthorDate: Tue Nov 3 19:34:04 2020 +0100 Move the MongoDB test infrastructure from CKC (#4557) Also adjust the tests from the camel-mongodb and camel-mongodb-gridfs to use the reusable test infra. --- components/camel-mongodb-gridfs/pom.xml | 19 ++++- .../mongodb/gridfs/AbstractMongoDbTest.java | 24 ++---- .../component/mongodb/gridfs/MongoDbContainer.java | 85 --------------------- components/camel-mongodb/pom.xml | 26 ++++++- .../component/mongodb/AbstractMongoDbTest.java | 25 ++---- .../mongodb/MongoDbConnectionBeansTest.java | 9 ++- .../camel/component/mongodb/MongoDbContainer.java | 83 -------------------- .../mongodb/MongoDbFindOperationTest.java | 88 +++++++++++++++++++++- .../mongodb/meta/MongoDbMetaExtensionTest.java | 6 +- .../verifier/MongoDbVerifierExtensionTest.java | 8 +- parent/pom.xml | 5 ++ test-infra/camel-test-infra-mongodb/pom.xml | 64 ++++++++++++++++ .../src/main/resources/META-INF/MANIFEST.MF | 0 .../services/MongoDBLocalContainerService.java | 70 +++++++++++++++++ .../mongodb/services/MongoDBRemoteService.java | 40 ++++++++++ .../infra/mongodb/services/MongoDBService.java | 50 ++++++++++++ .../mongodb/services/MongoDBServiceFactory.java | 44 +++++++++++ test-infra/pom.xml | 1 + 18 files changed, 431 insertions(+), 216 deletions(-) diff --git a/components/camel-mongodb-gridfs/pom.xml b/components/camel-mongodb-gridfs/pom.xml index cd98f5f..8147e5e 100644 --- a/components/camel-mongodb-gridfs/pom.xml +++ b/components/camel-mongodb-gridfs/pom.xml @@ -60,10 +60,27 @@ <version>${mongo-java-driver-version}</version> </dependency> + <!-- test infra --> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-test-infra-common</artifactId> + <version>${project.version}</version> + <type>test-jar</type> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-test-infra-mongodb</artifactId> + <version>${project.version}</version> + <type>test-jar</type> + <scope>test</scope> + </dependency> + <!-- test --> <dependency> <groupId>org.apache.camel</groupId> - <artifactId>camel-testcontainers-junit5</artifactId> + <artifactId>camel-test-junit5</artifactId> <scope>test</scope> </dependency> diff --git a/components/camel-mongodb-gridfs/src/test/java/org/apache/camel/component/mongodb/gridfs/AbstractMongoDbTest.java b/components/camel-mongodb-gridfs/src/test/java/org/apache/camel/component/mongodb/gridfs/AbstractMongoDbTest.java index 2f65fc7..64a9365 100644 --- a/components/camel-mongodb-gridfs/src/test/java/org/apache/camel/component/mongodb/gridfs/AbstractMongoDbTest.java +++ b/components/camel-mongodb-gridfs/src/test/java/org/apache/camel/component/mongodb/gridfs/AbstractMongoDbTest.java @@ -17,19 +17,22 @@ package org.apache.camel.component.mongodb.gridfs; import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; import com.mongodb.client.gridfs.GridFSBucket; import com.mongodb.client.gridfs.GridFSBuckets; import org.apache.camel.CamelContext; +import org.apache.camel.test.infra.mongodb.services.MongoDBService; +import org.apache.camel.test.infra.mongodb.services.MongoDBServiceFactory; import org.apache.camel.test.junit5.CamelTestSupport; -import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.extension.RegisterExtension; public abstract class AbstractMongoDbTest extends CamelTestSupport { + @RegisterExtension + public static MongoDBService service = MongoDBServiceFactory.createService(); protected static final String FILE_NAME = "filename.for.db.txt"; protected static final String FILE_DATA = "This is some stuff to go into the db"; - protected static MongoDbContainer container; protected MongoClient mongo; protected GridFSBucket gridFSBucket; @@ -37,19 +40,6 @@ public abstract class AbstractMongoDbTest extends CamelTestSupport { return this.getClass().getSimpleName(); } - @BeforeAll - public static void doBeforeAll() { - container = new MongoDbContainer(); - container.start(); - } - - @AfterAll - public static void doAfterAll() { - if (container != null) { - container.stop(); - } - } - @Override @AfterEach public void tearDown() throws Exception { @@ -60,7 +50,7 @@ public abstract class AbstractMongoDbTest extends CamelTestSupport { @Override protected CamelContext createCamelContext() throws Exception { - mongo = container.createClient(); + mongo = MongoClients.create(service.getReplicaSetUrl()); gridFSBucket = GridFSBuckets.create(mongo.getDatabase("test"), getBucket()); CamelContext context = super.createCamelContext(); diff --git a/components/camel-mongodb-gridfs/src/test/java/org/apache/camel/component/mongodb/gridfs/MongoDbContainer.java b/components/camel-mongodb-gridfs/src/test/java/org/apache/camel/component/mongodb/gridfs/MongoDbContainer.java deleted file mode 100644 index 108a449..0000000 --- a/components/camel-mongodb-gridfs/src/test/java/org/apache/camel/component/mongodb/gridfs/MongoDbContainer.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * 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.gridfs; - -import com.mongodb.client.MongoClient; -import com.mongodb.client.MongoClients; -import org.bson.Document; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testcontainers.containers.GenericContainer; -import org.testcontainers.containers.output.Slf4jLogConsumer; -import org.testcontainers.containers.wait.strategy.Wait; - -public class MongoDbContainer extends GenericContainer { - private static final Logger LOGGER = LoggerFactory.getLogger(MongoDbContainer.class); - private static final String CONTAINER_NAME = "mongo"; - private static final int MONGODB_PORT = 27017; - private static final String MONGO_IMAGE = "mongo:4.0"; - - public MongoDbContainer() { - super(MONGO_IMAGE); - - setWaitStrategy(Wait.forListeningPort()); - - withNetworkAliases(CONTAINER_NAME); - withExposedPorts(MONGODB_PORT); - withLogConsumer(new Slf4jLogConsumer(LOGGER)); - withCommand( - "--replSet", "replicationName", - "--oplogSize", "5000", - "--syncdelay", "0", - "--noauth", - "--noprealloc", - "--smallfiles"); - } - - @Override - public void start() { - super.start(); - - Document d = MongoClients.create(getConnectionURI()) - .getDatabase("admin") - .runCommand(new Document("replSetInitiate", new Document())); - - LOGGER.info("replSetInitiate: {}", d); - LOGGER.info("waiting to become master"); - - try { - execInContainer( - "/bin/bash", - "-c", - "until mongo --eval \"printjson(rs.isMaster())\" | grep ismaster | grep true > /dev/null 2>&1; do sleep 1; done"); - } catch (Exception e) { - throw new RuntimeException(e); - } - - LOGGER.info("started"); - } - - public String getConnectionAddress() { - return getContainerIpAddress() + ":" + getMappedPort(MONGODB_PORT); - } - - public String getConnectionURI() { - return "mongodb://" + getConnectionAddress(); - } - - public MongoClient createClient() { - return MongoClients.create(getConnectionURI()); - } -} diff --git a/components/camel-mongodb/pom.xml b/components/camel-mongodb/pom.xml index 3373730..eede22a 100644 --- a/components/camel-mongodb/pom.xml +++ b/components/camel-mongodb/pom.xml @@ -64,12 +64,36 @@ <version>${mongo-java-driver-version}</version> </dependency> + <!-- test infra --> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-test-infra-common</artifactId> + <version>${project.version}</version> + <type>test-jar</type> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-test-infra-mongodb</artifactId> + <version>${project.version}</version> + <type>test-jar</type> + <scope>test</scope> + </dependency> + <!-- test dependencies --> <dependency> <groupId>org.apache.camel</groupId> - <artifactId>camel-testcontainers-spring-junit5</artifactId> + <artifactId>camel-test-junit5</artifactId> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-test-spring-junit5</artifactId> <scope>test</scope> </dependency> + <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> 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 9482b56..bd48b68 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 @@ -19,29 +19,33 @@ package org.apache.camel.component.mongodb; import java.util.Formatter; import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import org.apache.camel.CamelContext; import org.apache.camel.CamelExecutionException; import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.test.infra.mongodb.services.MongoDBService; +import org.apache.camel.test.infra.mongodb.services.MongoDBServiceFactory; import org.apache.camel.test.junit5.CamelTestSupport; import org.apache.camel.util.IOHelper; import org.apache.camel.util.ObjectHelper; import org.bson.Document; -import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.extension.RegisterExtension; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; public abstract class AbstractMongoDbTest extends CamelTestSupport { + @RegisterExtension + public static MongoDBService service = MongoDBServiceFactory.createService(); + protected static final String SCHEME = "mongodb"; protected static final String USER = "test-user"; protected static final String PASSWORD = "test-pwd"; - protected static MongoDbContainer container; protected static MongoClient mongo; protected static MongoDatabase db; protected static MongoCollection<Document> testCollection; @@ -51,24 +55,11 @@ public abstract class AbstractMongoDbTest extends CamelTestSupport { protected static String testCollectionName; protected static String dynamicCollectionName; - @BeforeAll - public static void doBeforeAll() { - container = new MongoDbContainer(); - container.start(); - } - - @AfterAll - public static void doAfterAll() { - if (container != null) { - container.stop(); - } - } - @Override public void doPreSetup() throws Exception { super.doPreSetup(); - mongo = container.createClient(); + mongo = MongoClients.create(service.getReplicaSetUrl()); db = mongo.getDatabase(dbName); } diff --git a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbConnectionBeansTest.java b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbConnectionBeansTest.java index 1a47bde..eac74a9 100644 --- a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbConnectionBeansTest.java +++ b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbConnectionBeansTest.java @@ -17,6 +17,7 @@ package org.apache.camel.component.mongodb; import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; import org.apache.camel.Endpoint; import org.junit.jupiter.api.Test; @@ -28,7 +29,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows; public class MongoDbConnectionBeansTest extends AbstractMongoDbTest { @Test public void checkConnectionFromProperties() { - MongoClient client = container.createClient(); + MongoClient client = MongoClients.create(service.getReplicaSetUrl()); context.getComponent(SCHEME, MongoDbComponent.class).setMongoConnection(null); context.getRegistry().bind("myDb", client); @@ -41,7 +42,7 @@ public class MongoDbConnectionBeansTest extends AbstractMongoDbTest { @Test public void checkConnectionFromBean() { - MongoClient client = container.createClient(); + MongoClient client = MongoClients.create(service.getReplicaSetUrl()); context.getComponent(SCHEME, MongoDbComponent.class).setMongoConnection(null); context.getRegistry().bind("myDb", client); @@ -53,8 +54,8 @@ public class MongoDbConnectionBeansTest extends AbstractMongoDbTest { @Test public void checkConnectionBothExisting() { - MongoClient client1 = container.createClient(); - MongoClient client2 = container.createClient(); + MongoClient client1 = MongoClients.create(service.getReplicaSetUrl()); + MongoClient client2 = MongoClients.create(service.getReplicaSetUrl()); context.getComponent(SCHEME, MongoDbComponent.class).setMongoConnection(null); context.getRegistry().bind("myDb", client1); diff --git a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbContainer.java b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbContainer.java deleted file mode 100644 index d88a252..0000000 --- a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbContainer.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * 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; - -import com.mongodb.client.MongoClient; -import com.mongodb.client.MongoClients; -import org.bson.Document; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testcontainers.containers.GenericContainer; -import org.testcontainers.containers.output.Slf4jLogConsumer; -import org.testcontainers.containers.wait.strategy.Wait; - -public class MongoDbContainer extends GenericContainer { - private static final Logger LOGGER = LoggerFactory.getLogger(MongoDbContainer.class); - private static final String CONTAINER_NAME = "mongo"; - private static final int MONGODB_PORT = 27017; - private static final String MONGO_IMAGE = "mongo:4.4"; - - public MongoDbContainer() { - super(MONGO_IMAGE); - - setWaitStrategy(Wait.forListeningPort()); - - withNetworkAliases(CONTAINER_NAME); - withExposedPorts(MONGODB_PORT); - withLogConsumer(new Slf4jLogConsumer(LOGGER)); - withCommand( - "--replSet", "replicationName", - "--oplogSize", "5000", - "--syncdelay", "0", - "--noauth"); - } - - @Override - public void start() { - super.start(); - - Document d = MongoClients.create(getConnectionURI()) - .getDatabase("admin") - .runCommand(new Document("replSetInitiate", new Document())); - - LOGGER.info("replSetInitiate: {}", d); - LOGGER.info("waiting to become master"); - - try { - execInContainer( - "/bin/bash", - "-c", - "until mongo --eval \"printjson(rs.isMaster())\" | grep ismaster | grep true > /dev/null 2>&1; do sleep 1; done"); - } catch (Exception e) { - throw new RuntimeException(e); - } - - LOGGER.info("started"); - } - - public String getConnectionAddress() { - return getContainerIpAddress() + ":" + getMappedPort(MONGODB_PORT); - } - - public String getConnectionURI() { - return "mongodb://" + getConnectionAddress(); - } - - public MongoClient createClient() { - return MongoClients.create(getConnectionURI()); - } -} diff --git a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbFindOperationTest.java b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbFindOperationTest.java index 88cec2d..a76ccae 100644 --- a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbFindOperationTest.java +++ b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbFindOperationTest.java @@ -16,18 +16,30 @@ */ package org.apache.camel.component.mongodb; +import java.util.Formatter; import java.util.HashMap; import java.util.List; import java.util.Map; +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoDatabase; import com.mongodb.client.model.Projections; +import org.apache.camel.CamelContext; import org.apache.camel.Exchange; import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.test.infra.mongodb.services.MongoDBLocalContainerService; +import org.apache.camel.test.junit5.CamelTestSupport; +import org.apache.camel.util.IOHelper; import org.apache.commons.lang3.ObjectUtils; import org.bson.Document; import org.bson.conversions.Bson; import org.bson.types.ObjectId; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.testcontainers.containers.wait.strategy.Wait; import static com.mongodb.client.model.Filters.eq; import static org.apache.camel.component.mongodb.MongoDbConstants.MONGO_ID; @@ -37,7 +49,81 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; -public class MongoDbFindOperationTest extends AbstractMongoDbTest { +public class MongoDbFindOperationTest extends CamelTestSupport { + + @RegisterExtension + public static MongoDBLocalContainerService service; + + protected static String dbName = "test"; + protected static String testCollectionName; + + private static MongoClient mongo; + private static MongoDatabase db; + private static MongoCollection<Document> testCollection; + + static { + + // This one requires Mongo 4.4. This is related to + // "CAMEL-15604 support allowDiskUse for MongoDB find operations" + service = new MongoDBLocalContainerService("mongo:4.4"); + + service.getContainer() + .waitingFor(Wait.forListeningPort()) + .withCommand( + "--replSet", "replicationName", + "--oplogSize", "5000", + "--syncdelay", "0", + "--noauth"); + } + + @Override + public void doPreSetup() throws Exception { + super.doPreSetup(); + + mongo = MongoClients.create(service.getReplicaSetUrl()); + db = mongo.getDatabase(dbName); + } + + @Override + protected void doPostSetup() { + // Refresh the test collection - drop it and recreate it. We don't do + // this for the database because MongoDB would create large + // store files each time + testCollectionName = "camelTest"; + testCollection = db.getCollection(testCollectionName, Document.class); + testCollection.drop(); + testCollection = db.getCollection(testCollectionName, Document.class); + } + + @Override + protected CamelContext createCamelContext() throws Exception { + MongoDbComponent component = new MongoDbComponent(); + component.setMongoConnection(mongo); + + @SuppressWarnings("deprecation") + CamelContext ctx = new DefaultCamelContext(); + ctx.getPropertiesComponent().setLocation("classpath:mongodb.test.properties"); + + ctx.addComponent("mongodb", component); + + return ctx; + } + + protected void pumpDataIntoTestCollection() { + // there should be 100 of each + String[] scientists + = { "Einstein", "Darwin", "Copernicus", "Pasteur", "Curie", "Faraday", "Newton", "Bohr", "Galilei", "Maxwell" }; + for (int i = 1; i <= 1000; i++) { + int index = i % scientists.length; + Formatter f = new Formatter(); + String doc + = f.format("{\"_id\":\"%d\", \"scientist\":\"%s\", \"fixedField\": \"fixedValue\"}", i, scientists[index]) + .toString(); + IOHelper.close(f); + testCollection.insertOne(Document.parse(doc)); + } + assertEquals(1000L, testCollection.countDocuments(), "Data pumping of 1000 entries did not complete entirely"); + } @Test public void testFindAllNoCriteriaOperation() throws Exception { 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 index 65f5853..bf42a1f 100644 --- 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 @@ -86,7 +86,7 @@ public class MongoDbMetaExtensionTest extends AbstractMongoDbTest { Map<String, Object> parameters = new HashMap<>(); parameters.put("database", database); parameters.put("collection", collection); - parameters.put("host", container.getConnectionAddress()); + parameters.put("host", service.getConnectionAddress()); parameters.put("user", USER); parameters.put("password", PASSWORD); @@ -113,7 +113,7 @@ public class MongoDbMetaExtensionTest extends AbstractMongoDbTest { Map<String, Object> parameters = new HashMap<>(); parameters.put("database", database); parameters.put("collection", collection); - parameters.put("host", container.getConnectionAddress()); + parameters.put("host", service.getConnectionAddress()); parameters.put("user", USER); parameters.put("password", PASSWORD); @@ -147,7 +147,7 @@ public class MongoDbMetaExtensionTest extends AbstractMongoDbTest { Map<String, Object> parameters = new HashMap<>(); parameters.put("database", database); parameters.put("collection", collection); - parameters.put("host", container.getConnectionAddress()); + parameters.put("host", service.getConnectionAddress()); parameters.put("user", USER); parameters.put("password", PASSWORD); 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 index eeb3609..5997fbd 100644 --- 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 @@ -52,7 +52,7 @@ public class MongoDbVerifierExtensionTest extends AbstractMongoDbTest { public void verifyConnectionOK() { //When Map<String, Object> parameters = new HashMap<>(); - parameters.put("host", container.getConnectionAddress()); + parameters.put("host", service.getConnectionAddress()); parameters.put("user", USER); parameters.put("password", PASSWORD); //Given @@ -81,7 +81,7 @@ public class MongoDbVerifierExtensionTest extends AbstractMongoDbTest { public void verifyConnectionMissingParams() { //When Map<String, Object> parameters = new HashMap<>(); - parameters.put("host", container.getConnectionAddress()); + parameters.put("host", service.getConnectionAddress()); parameters.put("user", USER); //Given ComponentVerifierExtension.Result result @@ -95,7 +95,7 @@ public class MongoDbVerifierExtensionTest extends AbstractMongoDbTest { public void verifyConnectionNotAuthenticated() { //When Map<String, Object> parameters = new HashMap<>(); - parameters.put("host", container.getConnectionAddress()); + parameters.put("host", service.getConnectionAddress()); parameters.put("user", USER); parameters.put("password", "wrongPassword"); //Given @@ -110,7 +110,7 @@ public class MongoDbVerifierExtensionTest extends AbstractMongoDbTest { public void verifyConnectionAdminDBKO() { //When Map<String, Object> parameters = new HashMap<>(); - parameters.put("host", container.getConnectionAddress()); + parameters.put("host", service.getConnectionAddress()); parameters.put("user", USER); parameters.put("password", PASSWORD); parameters.put("adminDB", "someAdminDB"); diff --git a/parent/pom.xml b/parent/pom.xml index d0ce9e6..6c35285 100644 --- a/parent/pom.xml +++ b/parent/pom.xml @@ -3519,6 +3519,11 @@ <version>${testcontainers-version}</version> </dependency> <dependency> + <groupId>org.testcontainers</groupId> + <artifactId>mongodb</artifactId> + <version>${testcontainers-version}</version> + </dependency> + <dependency> <groupId>org.assertj</groupId> <artifactId>assertj-core</artifactId> <version>${assertj-version}</version> diff --git a/test-infra/camel-test-infra-mongodb/pom.xml b/test-infra/camel-test-infra-mongodb/pom.xml new file mode 100644 index 0000000..df667a6 --- /dev/null +++ b/test-infra/camel-test-infra-mongodb/pom.xml @@ -0,0 +1,64 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + 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. +--> + +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <parent> + <artifactId>camel-test-infra-parent</artifactId> + <groupId>org.apache.camel</groupId> + <relativePath>../camel-test-infra-parent/pom.xml</relativePath> + <version>3.7.0-SNAPSHOT</version> + </parent> + <modelVersion>4.0.0</modelVersion> + + <artifactId>camel-test-infra-mongodb</artifactId> + <name>Camel :: Test Infra :: MongoDB</name> + + <dependencies> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-test-infra-common</artifactId> + <version>${project.version}</version> + <type>test-jar</type> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.testcontainers</groupId> + <artifactId>testcontainers</artifactId> + </dependency> + + <dependency> + <groupId>org.testcontainers</groupId> + <artifactId>mongodb</artifactId> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-jar-plugin</artifactId> + </plugin> + </plugins> + </build> + + +</project> \ No newline at end of file diff --git a/test-infra/camel-test-infra-mongodb/src/main/resources/META-INF/MANIFEST.MF b/test-infra/camel-test-infra-mongodb/src/main/resources/META-INF/MANIFEST.MF new file mode 100644 index 0000000..e69de29 diff --git a/test-infra/camel-test-infra-mongodb/src/test/java/org/apache/camel/test/infra/mongodb/services/MongoDBLocalContainerService.java b/test-infra/camel-test-infra-mongodb/src/test/java/org/apache/camel/test/infra/mongodb/services/MongoDBLocalContainerService.java new file mode 100644 index 0000000..5ad644a --- /dev/null +++ b/test-infra/camel-test-infra-mongodb/src/test/java/org/apache/camel/test/infra/mongodb/services/MongoDBLocalContainerService.java @@ -0,0 +1,70 @@ +/* + * 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.test.infra.mongodb.services; + +import org.apache.camel.test.infra.common.services.ContainerService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testcontainers.containers.MongoDBContainer; + +public class MongoDBLocalContainerService implements MongoDBService, ContainerService<MongoDBContainer> { + private static final Logger LOG = LoggerFactory.getLogger(MongoDBLocalContainerService.class); + private static final int DEFAULT_MONGODB_PORT = 27017; + private final MongoDBContainer container; + + public MongoDBLocalContainerService() { + this(System.getProperty("mongodb.container")); + } + + public MongoDBLocalContainerService(String containerName) { + if (containerName == null || containerName.isEmpty()) { + container = new MongoDBContainer(); + } else { + container = new MongoDBContainer(containerName); + } + } + + @Override + public String getReplicaSetUrl() { + return String.format("mongodb://%s:%s", container.getContainerIpAddress(), + container.getMappedPort(DEFAULT_MONGODB_PORT)); + } + + @Override + public String getConnectionAddress() { + return container.getContainerIpAddress() + ":" + container.getMappedPort(DEFAULT_MONGODB_PORT); + } + + @Override + public void initialize() { + LOG.info("Trying to start the MongoDB service"); + container.start(); + LOG.info("MongoDB service running at {}", container.getReplicaSetUrl()); + } + + @Override + public void shutdown() { + LOG.info("Stopping the MongoDB container"); + container.stop(); + } + + @Override + public MongoDBContainer getContainer() { + return container; + } +} diff --git a/test-infra/camel-test-infra-mongodb/src/test/java/org/apache/camel/test/infra/mongodb/services/MongoDBRemoteService.java b/test-infra/camel-test-infra-mongodb/src/test/java/org/apache/camel/test/infra/mongodb/services/MongoDBRemoteService.java new file mode 100644 index 0000000..5715228 --- /dev/null +++ b/test-infra/camel-test-infra-mongodb/src/test/java/org/apache/camel/test/infra/mongodb/services/MongoDBRemoteService.java @@ -0,0 +1,40 @@ +/* + * 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.test.infra.mongodb.services; + +public class MongoDBRemoteService implements MongoDBService { + + public String getReplicaSetUrl() { + return System.getProperty("mongodb.url"); + } + + @Override + public String getConnectionAddress() { + return System.getProperty("mongodb.connection.address"); + } + + @Override + public void initialize() { + // NO-OP + } + + @Override + public void shutdown() { + // NO-OP + } +} diff --git a/test-infra/camel-test-infra-mongodb/src/test/java/org/apache/camel/test/infra/mongodb/services/MongoDBService.java b/test-infra/camel-test-infra-mongodb/src/test/java/org/apache/camel/test/infra/mongodb/services/MongoDBService.java new file mode 100644 index 0000000..767b352 --- /dev/null +++ b/test-infra/camel-test-infra-mongodb/src/test/java/org/apache/camel/test/infra/mongodb/services/MongoDBService.java @@ -0,0 +1,50 @@ +/* + * 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.test.infra.mongodb.services; + +import org.apache.camel.test.infra.common.services.TestService; +import org.junit.jupiter.api.extension.AfterAllCallback; +import org.junit.jupiter.api.extension.BeforeAllCallback; +import org.junit.jupiter.api.extension.ExtensionContext; + +public interface MongoDBService extends BeforeAllCallback, AfterAllCallback, TestService { + + /** + * The replica set URL in the format mongodb://host:port + * + * @return the replica set URL + */ + String getReplicaSetUrl(); + + /** + * The connection address in the format host:port + * + * @return the connection address + */ + String getConnectionAddress(); + + @Override + default void beforeAll(ExtensionContext extensionContext) throws Exception { + initialize(); + } + + @Override + default void afterAll(ExtensionContext extensionContext) throws Exception { + shutdown(); + } +} diff --git a/test-infra/camel-test-infra-mongodb/src/test/java/org/apache/camel/test/infra/mongodb/services/MongoDBServiceFactory.java b/test-infra/camel-test-infra-mongodb/src/test/java/org/apache/camel/test/infra/mongodb/services/MongoDBServiceFactory.java new file mode 100644 index 0000000..552073a --- /dev/null +++ b/test-infra/camel-test-infra-mongodb/src/test/java/org/apache/camel/test/infra/mongodb/services/MongoDBServiceFactory.java @@ -0,0 +1,44 @@ +/* + * 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.test.infra.mongodb.services; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public final class MongoDBServiceFactory { + private static final Logger LOG = LoggerFactory.getLogger(MongoDBServiceFactory.class); + + private MongoDBServiceFactory() { + + } + + public static MongoDBService createService() { + String instanceType = System.getProperty("mongodb.instance.type"); + + if (instanceType == null || instanceType.equals("local-mongodb-container")) { + return new MongoDBLocalContainerService(); + } + + if (instanceType.equals("remote")) { + return new MongoDBRemoteService(); + } + + LOG.error("MongoDB instance must be one of 'local-mongodb-container' or 'remote"); + throw new UnsupportedOperationException("Invalid MongoDB instance type"); + } +} diff --git a/test-infra/pom.xml b/test-infra/pom.xml index 57d737a..002eb03 100644 --- a/test-infra/pom.xml +++ b/test-infra/pom.xml @@ -43,5 +43,6 @@ <module>camel-test-infra-cassandra</module> <module>camel-test-infra-elasticsearch</module> <module>camel-test-infra-couchbase</module> + <module>camel-test-infra-mongodb</module> </modules> </project> \ No newline at end of file