This is an automated email from the ASF dual-hosted git repository.

orpiske 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 9ff73ea  Make the Cassandra test infra-structure reusable (#4504)
9ff73ea is described below

commit 9ff73eae1a70fcedd73ccf272cddd66b4d63676e
Author: Otavio Rodolfo Piske <orpi...@users.noreply.github.com>
AuthorDate: Fri Oct 23 15:53:22 2020 +0200

    Make the Cassandra test infra-structure reusable (#4504)
    
    - moved the Cassandra test service infra-structure from CKC
    - converted the camel-cassandra component tests to use the reusable 
test-infra
---
 components/camel-cassandraql/pom.xml               | 17 +++++-
 .../component/cassandra/BaseCassandraTest.java     | 36 ++++-------
 parent/pom.xml                                     |  5 ++
 .../{ => camel-test-infra-cassandra}/pom.xml       | 40 ++++++++-----
 .../src/main/resources/META-INF/MANIFEST.MF        |  0
 .../services/CassandraLocalContainerService.java   | 70 ++++++++++++++++++++++
 .../infra/cassandra/services/CassandraService.java | 60 +++++++++++++++++++
 .../services/CassandraServiceFactory.java          | 44 ++++++++++++++
 .../cassandra/services/RemoteCassandraService.java | 68 +++++++++++++++++++++
 test-infra/pom.xml                                 |  1 +
 10 files changed, 297 insertions(+), 44 deletions(-)

diff --git a/components/camel-cassandraql/pom.xml 
b/components/camel-cassandraql/pom.xml
index f7c3c8e..4065f26 100644
--- a/components/camel-cassandraql/pom.xml
+++ b/components/camel-cassandraql/pom.xml
@@ -97,10 +97,21 @@
             <version>${snakeyaml-version}</version>
             <scope>test</scope>
         </dependency>
+
+        <!-- test infra -->
         <dependency>
-            <groupId>org.testcontainers</groupId>
-            <artifactId>cassandra</artifactId>
-            <version>${testcontainers-version}</version>
+            <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-cassandra</artifactId>
+            <version>${project.version}</version>
+            <type>test-jar</type>
             <scope>test</scope>
         </dependency>
     </dependencies>
diff --git 
a/components/camel-cassandraql/src/test/java/org/apache/camel/component/cassandra/BaseCassandraTest.java
 
b/components/camel-cassandraql/src/test/java/org/apache/camel/component/cassandra/BaseCassandraTest.java
index f00e38a..687aa3e 100644
--- 
a/components/camel-cassandraql/src/test/java/org/apache/camel/component/cassandra/BaseCassandraTest.java
+++ 
b/components/camel-cassandraql/src/test/java/org/apache/camel/component/cassandra/BaseCassandraTest.java
@@ -23,41 +23,25 @@ import java.time.Duration;
 import com.datastax.oss.driver.api.core.CqlSession;
 import com.datastax.oss.driver.api.core.config.DefaultDriverOption;
 import com.datastax.oss.driver.api.core.config.DriverConfigLoader;
+import org.apache.camel.test.infra.cassandra.services.CassandraService;
+import org.apache.camel.test.infra.cassandra.services.CassandraServiceFactory;
 import org.apache.camel.test.junit5.CamelTestSupport;
-import org.junit.jupiter.api.AfterAll;
-import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.extension.ExtensionContext;
-import org.testcontainers.containers.CassandraContainer;
-import org.testcontainers.containers.GenericContainer;
+import org.junit.jupiter.api.extension.RegisterExtension;
 import org.testcontainers.shaded.org.apache.commons.io.IOUtils;
 
 public abstract class BaseCassandraTest extends CamelTestSupport {
 
+    @RegisterExtension
+    public static CassandraService service = 
CassandraServiceFactory.createService()
+            .withInitScript("initScript.cql")
+            .withNetworkAliases("cassandra");
+
     public static final String KEYSPACE_NAME = "camel_ks";
     public static final String DATACENTER_NAME = "datacenter1";
-    private static final int ORIGINAL_PORT = 9042;
 
-    private static GenericContainer<?> container;
     private CqlSession session;
 
-    @BeforeAll
-    public static void beforeAll() {
-        container = new 
CassandraContainer().withInitScript("initScript.cql").withNetworkAliases("cassandra")
-                .withExposedPorts(ORIGINAL_PORT);
-        container.start();
-    }
-
-    @AfterAll
-    public static void afterAll() {
-        try {
-            if (container != null) {
-                container.stop();
-            }
-        } catch (Exception e) {
-            // ignored
-        }
-    }
-
     @Override
     public void beforeEach(ExtensionContext context) throws Exception {
         super.beforeEach(context);
@@ -96,7 +80,7 @@ public abstract class BaseCassandraTest extends 
CamelTestSupport {
     public CqlSession getSession() {
         if (session == null) {
             InetSocketAddress endpoint
-                    = new InetSocketAddress(container.getContainerIpAddress(), 
container.getMappedPort(ORIGINAL_PORT));
+                    = new InetSocketAddress(service.getCassandraHost(), 
service.getCQL3Port());
             //create a new session
             session = CqlSession.builder()
                     .withLocalDatacenter(DATACENTER_NAME)
@@ -109,6 +93,6 @@ public abstract class BaseCassandraTest extends 
CamelTestSupport {
     }
 
     public String getUrl() {
-        return container.getContainerIpAddress() + ":" + 
container.getMappedPort(ORIGINAL_PORT);
+        return service.getCQL3Endpoint();
     }
 }
diff --git a/parent/pom.xml b/parent/pom.xml
index d01b139..3fda22a 100644
--- a/parent/pom.xml
+++ b/parent/pom.xml
@@ -3484,6 +3484,11 @@
                 <version>${testcontainers-version}</version>
             </dependency>
             <dependency>
+                <groupId>org.testcontainers</groupId>
+                <artifactId>cassandra</artifactId>
+                <version>${testcontainers-version}</version>
+            </dependency>
+            <dependency>
                 <groupId>org.assertj</groupId>
                 <artifactId>assertj-core</artifactId>
                 <version>${assertj-version}</version>
diff --git a/test-infra/pom.xml b/test-infra/camel-test-infra-cassandra/pom.xml
similarity index 59%
copy from test-infra/pom.xml
copy to test-infra/camel-test-infra-cassandra/pom.xml
index 7187c44..78828be 100644
--- a/test-infra/pom.xml
+++ b/test-infra/camel-test-infra-cassandra/pom.xml
@@ -20,25 +20,35 @@
          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-parent</artifactId>
+        <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>
-        <relativePath>../parent</relativePath>
     </parent>
-
     <modelVersion>4.0.0</modelVersion>
-    <artifactId>test-infra</artifactId>
 
-    <packaging>pom</packaging>
-    <name>Camel :: Test Infra</name>
-    <description>Test infrastructure for Camel</description>
+    <artifactId>camel-test-infra-cassandra</artifactId>
+    <name>Camel :: Test Infra :: Cassandra</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>cassandra</artifactId>
+        </dependency>
+    </dependencies>
+
 
-    <modules>
-        <module>camel-test-infra-common</module>
-        <module>camel-test-infra-kafka</module>
-        <module>camel-test-infra-parent</module>
-        <module>camel-test-infra-aws-common</module>
-        <module>camel-test-infra-aws-v1</module>
-        <module>camel-test-infra-aws-v2</module>
-    </modules>
 </project>
\ No newline at end of file
diff --git 
a/test-infra/camel-test-infra-cassandra/src/main/resources/META-INF/MANIFEST.MF 
b/test-infra/camel-test-infra-cassandra/src/main/resources/META-INF/MANIFEST.MF
new file mode 100644
index 0000000..e69de29
diff --git 
a/test-infra/camel-test-infra-cassandra/src/test/java/org/apache/camel/test/infra/cassandra/services/CassandraLocalContainerService.java
 
b/test-infra/camel-test-infra-cassandra/src/test/java/org/apache/camel/test/infra/cassandra/services/CassandraLocalContainerService.java
new file mode 100644
index 0000000..9677aef
--- /dev/null
+++ 
b/test-infra/camel-test-infra-cassandra/src/test/java/org/apache/camel/test/infra/cassandra/services/CassandraLocalContainerService.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.cassandra.services;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.testcontainers.containers.CassandraContainer;
+
+/**
+ * A service for a local instance of Apache Cassandra running with 
TestContainers
+ */
+public class CassandraLocalContainerService implements 
CassandraService<CassandraLocalContainerService> {
+    private static final Logger LOG = 
LoggerFactory.getLogger(CassandraLocalContainerService.class);
+
+    private CassandraContainer container;
+
+    public CassandraLocalContainerService() {
+        container = new CassandraContainer();
+    }
+
+    @Override
+    public CassandraLocalContainerService withInitScript(String initScript) {
+        container.withInitScript(initScript);
+
+        return this;
+    }
+
+    @Override
+    public CassandraLocalContainerService withNetworkAliases(String network) {
+        container.withNetworkAliases(network);
+        return this;
+    }
+
+    @Override
+    public int getCQL3Port() {
+        return container.getMappedPort(CassandraContainer.CQL_PORT);
+    }
+
+    @Override
+    public String getCassandraHost() {
+        return container.getHost();
+    }
+
+    @Override
+    public void initialize() {
+        container.start();
+
+        LOG.info("Cassandra server running at address {}", getCQL3Endpoint());
+    }
+
+    @Override
+    public void shutdown() {
+        LOG.info("Stopping the Cassandra container");
+        container.stop();
+    }
+}
diff --git 
a/test-infra/camel-test-infra-cassandra/src/test/java/org/apache/camel/test/infra/cassandra/services/CassandraService.java
 
b/test-infra/camel-test-infra-cassandra/src/test/java/org/apache/camel/test/infra/cassandra/services/CassandraService.java
new file mode 100644
index 0000000..7d451cf
--- /dev/null
+++ 
b/test-infra/camel-test-infra-cassandra/src/test/java/org/apache/camel/test/infra/cassandra/services/CassandraService.java
@@ -0,0 +1,60 @@
+/*
+ * 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.cassandra.services;
+
+import org.junit.jupiter.api.extension.AfterAllCallback;
+import org.junit.jupiter.api.extension.BeforeAllCallback;
+import org.junit.jupiter.api.extension.ExtensionContext;
+
+/**
+ * Represents an endpoint to a Cassandra instance
+ */
+public interface CassandraService<T extends CassandraService<T>> extends 
BeforeAllCallback, AfterAllCallback {
+
+    int getCQL3Port();
+
+    default String getCQL3Endpoint() {
+        return String.format("%s:%d", getCassandraHost(), getCQL3Port());
+    }
+
+    String getCassandraHost();
+
+    T withInitScript(String initScript);
+
+    T withNetworkAliases(String network);
+
+    /**
+     * Perform any initialization necessary
+     */
+    void initialize();
+
+    /**
+     * Shuts down the service after the test has completed
+     */
+    void shutdown();
+
+    @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-cassandra/src/test/java/org/apache/camel/test/infra/cassandra/services/CassandraServiceFactory.java
 
b/test-infra/camel-test-infra-cassandra/src/test/java/org/apache/camel/test/infra/cassandra/services/CassandraServiceFactory.java
new file mode 100644
index 0000000..a0a3aa8
--- /dev/null
+++ 
b/test-infra/camel-test-infra-cassandra/src/test/java/org/apache/camel/test/infra/cassandra/services/CassandraServiceFactory.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.cassandra.services;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public final class CassandraServiceFactory {
+    private static final Logger LOG = 
LoggerFactory.getLogger(CassandraServiceFactory.class);
+
+    private CassandraServiceFactory() {
+
+    }
+
+    public static CassandraService createService() {
+        String instanceType = System.getProperty("cassandra.instance.type");
+
+        if (instanceType == null || 
instanceType.equals("local-cassandra-container")) {
+            return new CassandraLocalContainerService();
+        }
+
+        if (instanceType.equals("remote")) {
+            return new RemoteCassandraService();
+        }
+
+        LOG.error("Cassandra instance must be one of 
'local-cassandra-container' or 'remote");
+        throw new UnsupportedOperationException("Invalid Cassandra instance 
type");
+    }
+
+}
diff --git 
a/test-infra/camel-test-infra-cassandra/src/test/java/org/apache/camel/test/infra/cassandra/services/RemoteCassandraService.java
 
b/test-infra/camel-test-infra-cassandra/src/test/java/org/apache/camel/test/infra/cassandra/services/RemoteCassandraService.java
new file mode 100644
index 0000000..cd0d5fd
--- /dev/null
+++ 
b/test-infra/camel-test-infra-cassandra/src/test/java/org/apache/camel/test/infra/cassandra/services/RemoteCassandraService.java
@@ -0,0 +1,68 @@
+/*
+ * 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.cassandra.services;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * A remote instance of Apache Cassandra
+ */
+public class RemoteCassandraService implements 
CassandraService<RemoteCassandraService> {
+    private static final Logger LOG = 
LoggerFactory.getLogger(RemoteCassandraService.class);
+
+    private static final int DEFAULT_CQL_PORT = 9042;
+
+    @Override
+    public RemoteCassandraService withInitScript(String initScript) {
+        LOG.warn("The init script is not supported on the remote cassandra 
service.");
+        return this;
+    }
+
+    @Override
+    public RemoteCassandraService withNetworkAliases(String network) {
+        LOG.warn("The network aliases configuration is not supported on the 
remote cassandra service.");
+
+        return this;
+    }
+
+    @Override
+    public int getCQL3Port() {
+        String strPort = System.getProperty("cassandra.cql3.port");
+
+        if (strPort != null) {
+            return Integer.parseInt(strPort);
+        }
+
+        return DEFAULT_CQL_PORT;
+    }
+
+    @Override
+    public String getCassandraHost() {
+        return System.getProperty("cassandra.host");
+    }
+
+    @Override
+    public void initialize() {
+        // NO-OP
+    }
+
+    @Override
+    public void shutdown() {
+        // NO-OP
+    }
+}
diff --git a/test-infra/pom.xml b/test-infra/pom.xml
index 7187c44..f3008fd 100644
--- a/test-infra/pom.xml
+++ b/test-infra/pom.xml
@@ -40,5 +40,6 @@
         <module>camel-test-infra-aws-common</module>
         <module>camel-test-infra-aws-v1</module>
         <module>camel-test-infra-aws-v2</module>
+        <module>camel-test-infra-cassandra</module>
     </modules>
 </project>
\ No newline at end of file

Reply via email to