You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2020/11/18 13:19:46 UTC

[camel] branch master updated: Added test-infra services for JDBC (#4621)

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 23c4c44  Added test-infra services for JDBC (#4621)
23c4c44 is described below

commit 23c4c4418fa5852a333cc7fbb4c0886a4647f0b3
Author: Otavio Rodolfo Piske <or...@users.noreply.github.com>
AuthorDate: Wed Nov 18 14:19:27 2020 +0100

    Added test-infra services for JDBC (#4621)
    
    Also converted the camel-pgevent test cases to use the new infra
---
 components/camel-pgevent/pom.xml                   | 20 ++++++-
 .../apache/camel/pgevent/PgEventTestSupport.java   | 64 ++++++++--------------
 parent/pom.xml                                     | 10 ++++
 test-infra/camel-test-infra-jdbc/pom.xml           | 62 +++++++++++++++++++++
 .../src/main/resources/META-INF/MANIFEST.MF        |  0
 .../test/infra/jdbc/common/JDBCProperties.java     | 26 +++++++++
 .../jdbc/services/JDBCLocalContainerService.java   | 64 ++++++++++++++++++++++
 .../infra/jdbc/services/JDBCRemoteService.java     | 48 ++++++++++++++++
 .../test/infra/jdbc/services/JDBCService.java      | 38 +++++++++++++
 .../infra/jdbc/services/JDBCServiceBuilder.java    | 58 ++++++++++++++++++++
 test-infra/pom.xml                                 |  1 +
 11 files changed, 348 insertions(+), 43 deletions(-)

diff --git a/components/camel-pgevent/pom.xml b/components/camel-pgevent/pom.xml
index b730284..ae39245 100644
--- a/components/camel-pgevent/pom.xml
+++ b/components/camel-pgevent/pom.xml
@@ -66,11 +66,29 @@
             <scope>test</scope>
         </dependency>
 
+        <!-- test infra -->
         <dependency>
             <groupId>org.apache.camel</groupId>
-            <artifactId>camel-testcontainers-junit5</artifactId>
+            <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-jdbc</artifactId>
+            <version>${project.version}</version>
+            <type>test-jar</type>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.testcontainers</groupId>
+            <artifactId>postgresql</artifactId>
+            <version>${testcontainers-version}</version>
+        </dependency>
+
     </dependencies>
 
     <profiles>
diff --git a/components/camel-pgevent/src/test/java/org/apache/camel/pgevent/PgEventTestSupport.java b/components/camel-pgevent/src/test/java/org/apache/camel/pgevent/PgEventTestSupport.java
index 41dfec6..bb8a8e3 100644
--- a/components/camel-pgevent/src/test/java/org/apache/camel/pgevent/PgEventTestSupport.java
+++ b/components/camel-pgevent/src/test/java/org/apache/camel/pgevent/PgEventTestSupport.java
@@ -16,70 +16,50 @@
  */
 package org.apache.camel.pgevent;
 
-import org.apache.camel.test.testcontainers.junit5.ContainerAwareTestSupport;
-import org.junit.jupiter.api.AfterEach;
-import org.junit.jupiter.api.BeforeEach;
+import org.apache.camel.test.infra.jdbc.services.JDBCService;
+import org.apache.camel.test.infra.jdbc.services.JDBCServiceBuilder;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.extension.RegisterExtension;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.testcontainers.containers.GenericContainer;
+import org.testcontainers.containers.PostgreSQLContainer;
 import org.testcontainers.containers.output.Slf4jLogConsumer;
-import org.testcontainers.containers.wait.strategy.Wait;
-import org.testcontainers.utility.TestcontainersConfiguration;
 
-public class PgEventTestSupport extends ContainerAwareTestSupport {
+public class PgEventTestSupport extends CamelTestSupport {
+    @RegisterExtension
+    public static JDBCService service;
 
-    public static final String CONTAINER_NAME = "pg-event";
     protected static final String TEST_MESSAGE_BODY = "Test Camel PGEvent";
     protected static final String POSTGRES_USER = "postgres";
     protected static final String POSTGRES_PASSWORD = "mysecretpassword";
     protected static final String POSTGRES_DB = "postgres";
 
     private static final Logger LOG = LoggerFactory.getLogger(PgEventTestSupport.class);
-    private static final int POSTGRES_PORT = 5432;
-    private static final String POSTGRES_IMAGE = "postgres:13.0";
 
-    protected GenericContainer container;
+    private static PostgreSQLContainer container;
 
-    @BeforeEach
-    public void beforeEach() {
-        container = createContainer();
-        container.start();
-    }
-
-    @AfterEach
-    public void afterEach() {
-        if (container != null) {
-            container.stop();
-        }
-    }
+    static {
+        final String postgresImage = "postgres:13.0";
 
-    @Override
-    protected GenericContainer<?> createContainer() {
-        LOG.info(TestcontainersConfiguration.getInstance().toString());
+        container = new PostgreSQLContainer(postgresImage);
 
-        GenericContainer<?> container = new GenericContainer(POSTGRES_IMAGE)
-                .withCommand("postgres -c wal_level=logical")
-                .withExposedPorts(POSTGRES_PORT)
-                .withNetworkAliases(CONTAINER_NAME)
-                .withEnv("POSTGRES_USER", POSTGRES_USER)
-                .withEnv("POSTGRES_PASSWORD", POSTGRES_PASSWORD)
-                .withEnv("POSTGRES_DB", POSTGRES_DB)
-                .withLogConsumer(new Slf4jLogConsumer(LOG))
-                .waitingFor(Wait.forListeningPort());
-        return container;
-    }
+        container.withUsername(POSTGRES_USER)
+                .withPassword(POSTGRES_PASSWORD)
+                .withDatabaseName(POSTGRES_DB)
+                .withLogConsumer(new Slf4jLogConsumer(LOG));
 
-    public String getAuthority() {
-        return String.format("%s:%s", getContainer(CONTAINER_NAME).getContainerIpAddress(),
-                getContainer(CONTAINER_NAME).getMappedPort(POSTGRES_PORT));
+        // Let the JDBC Service handle container lifecycle
+        service = JDBCServiceBuilder.newBuilder()
+                .withContainer(container)
+                .build();
     }
 
     public Integer getMappedPort() {
-        return getContainer(CONTAINER_NAME).getMappedPort(POSTGRES_PORT);
+        return container.getMappedPort(PostgreSQLContainer.POSTGRESQL_PORT);
     }
 
     public String getHost() {
-        return getContainer(CONTAINER_NAME).getHost();
+        return container.getHost();
     }
 
 }
diff --git a/parent/pom.xml b/parent/pom.xml
index a86c5f4..533dc93 100644
--- a/parent/pom.xml
+++ b/parent/pom.xml
@@ -3529,6 +3529,16 @@
                 <version>${testcontainers-version}</version>
             </dependency>
             <dependency>
+                <groupId>org.testcontainers</groupId>
+                <artifactId>jdbc</artifactId>
+                <version>${testcontainers-version}</version>
+            </dependency>
+            <dependency>
+                <groupId>org.testcontainers</groupId>
+                <artifactId>postgresql</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-jdbc/pom.xml b/test-infra/camel-test-infra-jdbc/pom.xml
new file mode 100644
index 0000000..13fd255
--- /dev/null
+++ b/test-infra/camel-test-infra-jdbc/pom.xml
@@ -0,0 +1,62 @@
+<?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-jdbc</artifactId>
+    <name>Camel :: Test Infra :: JDBC</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>jdbc</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-jdbc/src/main/resources/META-INF/MANIFEST.MF b/test-infra/camel-test-infra-jdbc/src/main/resources/META-INF/MANIFEST.MF
new file mode 100644
index 0000000..e69de29
diff --git a/test-infra/camel-test-infra-jdbc/src/test/java/org/apache/camel/test/infra/jdbc/common/JDBCProperties.java b/test-infra/camel-test-infra-jdbc/src/test/java/org/apache/camel/test/infra/jdbc/common/JDBCProperties.java
new file mode 100644
index 0000000..d736589
--- /dev/null
+++ b/test-infra/camel-test-infra-jdbc/src/test/java/org/apache/camel/test/infra/jdbc/common/JDBCProperties.java
@@ -0,0 +1,26 @@
+/*
+ * 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.jdbc.common;
+
+public final class JDBCProperties {
+    public static final String JDBC_CONNECTION_URL = "jdbc.connection.url";
+
+    private JDBCProperties() {
+
+    }
+}
diff --git a/test-infra/camel-test-infra-jdbc/src/test/java/org/apache/camel/test/infra/jdbc/services/JDBCLocalContainerService.java b/test-infra/camel-test-infra-jdbc/src/test/java/org/apache/camel/test/infra/jdbc/services/JDBCLocalContainerService.java
new file mode 100644
index 0000000..499e74a
--- /dev/null
+++ b/test-infra/camel-test-infra-jdbc/src/test/java/org/apache/camel/test/infra/jdbc/services/JDBCLocalContainerService.java
@@ -0,0 +1,64 @@
+/*
+ * 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.jdbc.services;
+
+import org.apache.camel.test.infra.common.services.ContainerService;
+import org.apache.camel.test.infra.jdbc.common.JDBCProperties;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.testcontainers.containers.JdbcDatabaseContainer;
+
+public class JDBCLocalContainerService<T extends JdbcDatabaseContainer<T>> implements JDBCService, ContainerService<T> {
+    private static final Logger LOG = LoggerFactory.getLogger(JDBCLocalContainerService.class);
+
+    private final T container;
+
+    public JDBCLocalContainerService(T container) {
+        this.container = container;
+    }
+
+    @Override
+    public void registerProperties() {
+        System.setProperty(JDBCProperties.JDBC_CONNECTION_URL, jdbcUrl());
+    }
+
+    @Override
+    public String jdbcUrl() {
+        return container.getJdbcUrl();
+    }
+
+    @Override
+    public T getContainer() {
+        return container;
+    }
+
+    @Override
+    public void initialize() {
+        LOG.info("Trying to start the database container");
+        container.start();
+
+        registerProperties();
+        LOG.info("Database instance available via JDBC url {}", jdbcUrl());
+    }
+
+    @Override
+    public void shutdown() {
+        LOG.info("Stopping the database instance");
+        container.stop();
+    }
+}
diff --git a/test-infra/camel-test-infra-jdbc/src/test/java/org/apache/camel/test/infra/jdbc/services/JDBCRemoteService.java b/test-infra/camel-test-infra-jdbc/src/test/java/org/apache/camel/test/infra/jdbc/services/JDBCRemoteService.java
new file mode 100644
index 0000000..4433a10
--- /dev/null
+++ b/test-infra/camel-test-infra-jdbc/src/test/java/org/apache/camel/test/infra/jdbc/services/JDBCRemoteService.java
@@ -0,0 +1,48 @@
+/*
+ * 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.jdbc.services;
+
+import org.apache.camel.test.infra.jdbc.common.JDBCProperties;
+
+public class JDBCRemoteService implements JDBCService {
+    private static final String CONNECTION_URL;
+
+    static {
+        CONNECTION_URL = System.getProperty(JDBCProperties.JDBC_CONNECTION_URL);
+    }
+
+    @Override
+    public void registerProperties() {
+        // NO-OP
+    }
+
+    @Override
+    public String jdbcUrl() {
+        return CONNECTION_URL;
+    }
+
+    @Override
+    public void initialize() {
+        registerProperties();
+    }
+
+    @Override
+    public void shutdown() {
+        // NO-OP
+    }
+}
diff --git a/test-infra/camel-test-infra-jdbc/src/test/java/org/apache/camel/test/infra/jdbc/services/JDBCService.java b/test-infra/camel-test-infra-jdbc/src/test/java/org/apache/camel/test/infra/jdbc/services/JDBCService.java
new file mode 100644
index 0000000..ced0a29
--- /dev/null
+++ b/test-infra/camel-test-infra-jdbc/src/test/java/org/apache/camel/test/infra/jdbc/services/JDBCService.java
@@ -0,0 +1,38 @@
+/*
+ * 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.jdbc.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 JDBCService extends TestService, BeforeAllCallback, AfterAllCallback {
+
+    String jdbcUrl();
+
+    @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-jdbc/src/test/java/org/apache/camel/test/infra/jdbc/services/JDBCServiceBuilder.java b/test-infra/camel-test-infra-jdbc/src/test/java/org/apache/camel/test/infra/jdbc/services/JDBCServiceBuilder.java
new file mode 100644
index 0000000..44e8395
--- /dev/null
+++ b/test-infra/camel-test-infra-jdbc/src/test/java/org/apache/camel/test/infra/jdbc/services/JDBCServiceBuilder.java
@@ -0,0 +1,58 @@
+/*
+ * 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.jdbc.services;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.testcontainers.containers.JdbcDatabaseContainer;
+
+public final class JDBCServiceBuilder {
+    private static final Logger LOG = LoggerFactory.getLogger(JDBCServiceBuilder.class);
+
+    private JdbcDatabaseContainer<?> container;
+
+    protected JDBCServiceBuilder() {
+    }
+
+    public static JDBCServiceBuilder newBuilder() {
+        JDBCServiceBuilder jdbcServiceBuilder = new JDBCServiceBuilder();
+
+        return jdbcServiceBuilder;
+    }
+
+    public JDBCServiceBuilder withContainer(JdbcDatabaseContainer<?> container) {
+        this.container = container;
+
+        return this;
+    }
+
+    public JDBCService build() {
+        String instanceType = System.getProperty("jdbc.instance.type");
+
+        if (instanceType == null || instanceType.isEmpty()) {
+            LOG.info("Creating a new messaging local container service");
+            return new JDBCLocalContainerService(container);
+        }
+
+        if (instanceType.equals("remote")) {
+            return new JDBCRemoteService();
+        }
+
+        throw new UnsupportedOperationException("Invalid messaging instance type");
+    }
+}
diff --git a/test-infra/pom.xml b/test-infra/pom.xml
index 1def8ae..0b2302e 100644
--- a/test-infra/pom.xml
+++ b/test-infra/pom.xml
@@ -52,6 +52,7 @@
         <module>camel-test-infra-artemis</module>
         <module>camel-test-infra-dispatch-router</module>
         <module>camel-test-infra-hdfs</module>
+        <module>camel-test-infra-jdbc</module>
 
     </modules>
 </project>
\ No newline at end of file