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 a12bb70  Migrates camel-pg-replication-slot to the new test infra 
(#4724)
a12bb70 is described below

commit a12bb70295e9811b83a1000d5b642d6af2d4f990
Author: Otavio Rodolfo Piske <orpi...@users.noreply.github.com>
AuthorDate: Fri Dec 4 18:50:56 2020 +0100

    Migrates camel-pg-replication-slot to the new test infra (#4724)
---
 components/camel-pg-replication-slot/pom.xml       | 14 +++-
 .../integration/PgReplicationSlotCamelTest.java    | 15 ++--
 .../slot/integration/PgReplicationTestSupport.java | 52 +++---------
 test-infra/camel-test-infra-postgres/pom.xml       | 66 +++++++++++++++
 .../src/main/resources/META-INF/MANIFEST.MF        |  0
 .../infra/postgres/common/PostgresProperties.java  | 32 ++++++++
 .../services/PostgresLocalContainerService.java    | 94 ++++++++++++++++++++++
 .../postgres/services/PostgresRemoteService.java   | 68 ++++++++++++++++
 .../infra/postgres/services/PostgresService.java   | 48 +++++++++++
 .../postgres/services/PostgresServiceFactory.java  | 43 ++++++++++
 test-infra/pom.xml                                 |  1 +
 11 files changed, 385 insertions(+), 48 deletions(-)

diff --git a/components/camel-pg-replication-slot/pom.xml 
b/components/camel-pg-replication-slot/pom.xml
index 0a5511d..342b861 100644
--- a/components/camel-pg-replication-slot/pom.xml
+++ b/components/camel-pg-replication-slot/pom.xml
@@ -57,9 +57,21 @@
             <artifactId>mockito-junit-jupiter</artifactId>
             <scope>test</scope>
         </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-testcontainers-junit5</artifactId>
+            <artifactId>camel-test-infra-postgres</artifactId>
+            <version>${project.version}</version>
+            <type>test-jar</type>
             <scope>test</scope>
         </dependency>
 
diff --git 
a/components/camel-pg-replication-slot/src/test/java/org/apache/camel/component/pg/replication/slot/integration/PgReplicationSlotCamelTest.java
 
b/components/camel-pg-replication-slot/src/test/java/org/apache/camel/component/pg/replication/slot/integration/PgReplicationSlotCamelTest.java
index 4e05440..6746ef6 100644
--- 
a/components/camel-pg-replication-slot/src/test/java/org/apache/camel/component/pg/replication/slot/integration/PgReplicationSlotCamelTest.java
+++ 
b/components/camel-pg-replication-slot/src/test/java/org/apache/camel/component/pg/replication/slot/integration/PgReplicationSlotCamelTest.java
@@ -40,10 +40,10 @@ public class PgReplicationSlotCamelTest extends 
PgReplicationTestSupport {
     public void setUp() throws Exception {
         super.setUp();
 
-        String url = String.format("jdbc:postgresql://%s/camel", 
getAuthority());
+        String url = String.format("jdbc:postgresql://%s/camel", 
service.getServiceAddress());
         Properties props = new Properties();
-        props.setProperty("user", "camel");
-        props.setProperty("password", "camel");
+        props.setProperty("user", service.userName());
+        props.setProperty("password", service.password());
 
         this.connection = DriverManager.getConnection(url, props);
         try (Statement statement = this.connection.createStatement()) {
@@ -64,11 +64,12 @@ public class PgReplicationSlotCamelTest extends 
PgReplicationTestSupport {
             @Override
             public void configure() {
 
-                String uriFormat = 
"pg-replication-slot://%s/camel/camel_test_slot:test_decoding?user=%s" +
-                                   
"&password=%s&slotOptions.skip-empty-xacts=true&slotOptions.include-xids=false";
+                String uriFormat
+                        = 
"pg-replication-slot://{{postgres.service.address}}/camel/camel_test_slot:test_decoding?"
+                          + 
"user={{postgres.user.name}}&password={{postgres.user.password}}"
+                          + 
"&slotOptions.skip-empty-xacts=true&slotOptions.include-xids=false";
 
-                String uri = String.format(uriFormat, getAuthority(), 
getUser(), getPassword());
-                from(uri).to(mockEndpoint);
+                from(uriFormat).to(mockEndpoint);
             }
         };
     }
diff --git 
a/components/camel-pg-replication-slot/src/test/java/org/apache/camel/component/pg/replication/slot/integration/PgReplicationTestSupport.java
 
b/components/camel-pg-replication-slot/src/test/java/org/apache/camel/component/pg/replication/slot/integration/PgReplicationTestSupport.java
index dbd74d2..79e301a 100644
--- 
a/components/camel-pg-replication-slot/src/test/java/org/apache/camel/component/pg/replication/slot/integration/PgReplicationTestSupport.java
+++ 
b/components/camel-pg-replication-slot/src/test/java/org/apache/camel/component/pg/replication/slot/integration/PgReplicationTestSupport.java
@@ -17,49 +17,21 @@
 
 package org.apache.camel.component.pg.replication.slot.integration;
 
-import org.apache.camel.test.testcontainers.junit5.ContainerAwareTestSupport;
-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;
-import org.testcontainers.utility.TestcontainersConfiguration;
+import 
org.apache.camel.test.infra.postgres.services.PostgresLocalContainerService;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.extension.RegisterExtension;
+import org.testcontainers.containers.PostgreSQLContainer;
 
-public class PgReplicationTestSupport extends ContainerAwareTestSupport {
+public class PgReplicationTestSupport extends CamelTestSupport {
 
-    public static final String CONTAINER_NAME = "pg-replication-slot";
+    @RegisterExtension
+    static PostgresLocalContainerService service;
 
-    private static final Logger LOG = 
LoggerFactory.getLogger(PgReplicationTestSupport.class);
-    private static final int POSTGRES_PORT = 5432;
-    private static final String POSTGRES_IMAGE = "postgres:13.0";
+    static {
+        PostgreSQLContainer container = new 
PostgreSQLContainer<>(PostgresLocalContainerService.DEFAULT_POSTGRES_CONTAINER)
+                .withDatabaseName("camel")
+                .withCommand("postgres -c wal_level=logical");
 
-    @Override
-    protected GenericContainer<?> createContainer() {
-        LOG.info(TestcontainersConfiguration.getInstance().toString());
-
-        GenericContainer<?> container = new GenericContainer(POSTGRES_IMAGE)
-                .withCommand("postgres -c wal_level=logical")
-                .withNetworkAliases(CONTAINER_NAME)
-                .withExposedPorts(POSTGRES_PORT)
-                .withEnv("POSTGRES_USER", "camel")
-                .withEnv("POSTGRES_PASSWORD", "camel")
-                .withLogConsumer(new Slf4jLogConsumer(LOG))
-                .waitingFor(Wait.forListeningPort());
-
-        return container;
+        service = new PostgresLocalContainerService(container);
     }
-
-    public String getAuthority() {
-        return String.format("%s:%s", 
getContainer(CONTAINER_NAME).getContainerIpAddress(),
-                getContainer(CONTAINER_NAME).getMappedPort(POSTGRES_PORT));
-    }
-
-    public String getUser() {
-        return "camel";
-    }
-
-    public String getPassword() {
-        return "camel";
-    }
-
 }
diff --git a/test-infra/camel-test-infra-postgres/pom.xml 
b/test-infra/camel-test-infra-postgres/pom.xml
new file mode 100644
index 0000000..7ed1e52
--- /dev/null
+++ b/test-infra/camel-test-infra-postgres/pom.xml
@@ -0,0 +1,66 @@
+<?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-postgres</artifactId>
+    <name>Camel :: Test Infra :: Postgres</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>postgresql</artifactId>
+            <version>${testcontainers-version}</version>
+        </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-postgres/src/main/resources/META-INF/MANIFEST.MF 
b/test-infra/camel-test-infra-postgres/src/main/resources/META-INF/MANIFEST.MF
new file mode 100644
index 0000000..e69de29
diff --git 
a/test-infra/camel-test-infra-postgres/src/test/java/org/apache/camel/test/infra/postgres/common/PostgresProperties.java
 
b/test-infra/camel-test-infra-postgres/src/test/java/org/apache/camel/test/infra/postgres/common/PostgresProperties.java
new file mode 100644
index 0000000..d98917c
--- /dev/null
+++ 
b/test-infra/camel-test-infra-postgres/src/test/java/org/apache/camel/test/infra/postgres/common/PostgresProperties.java
@@ -0,0 +1,32 @@
+/*
+ * 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.postgres.common;
+
+public final class PostgresProperties {
+    public static final String SERVICE_ADDRESS = "postgres.service.address";
+    public static final String HOST = "postgres.service.host";
+    public static final String PORT = "postgres.service.port";
+    public static final String USERNAME = "postgres.user.name";
+    public static final String PASSWORD = "postgres.user.password";
+
+    public static final int DEFAULT_PORT = 5432;
+
+    private PostgresProperties() {
+
+    }
+}
diff --git 
a/test-infra/camel-test-infra-postgres/src/test/java/org/apache/camel/test/infra/postgres/services/PostgresLocalContainerService.java
 
b/test-infra/camel-test-infra-postgres/src/test/java/org/apache/camel/test/infra/postgres/services/PostgresLocalContainerService.java
new file mode 100644
index 0000000..338f388
--- /dev/null
+++ 
b/test-infra/camel-test-infra-postgres/src/test/java/org/apache/camel/test/infra/postgres/services/PostgresLocalContainerService.java
@@ -0,0 +1,94 @@
+/*
+ * 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.postgres.services;
+
+import org.apache.camel.test.infra.common.services.ContainerService;
+import org.apache.camel.test.infra.postgres.common.PostgresProperties;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.testcontainers.containers.GenericContainer;
+import org.testcontainers.containers.PostgreSQLContainer;
+
+public class PostgresLocalContainerService implements PostgresService, 
ContainerService<GenericContainer> {
+    public static final String DEFAULT_POSTGRES_CONTAINER = "postgres:13.0";
+    private static final Logger LOG = 
LoggerFactory.getLogger(PostgresLocalContainerService.class);
+    private PostgreSQLContainer container;
+
+    public PostgresLocalContainerService() {
+        String containerName = System.getProperty("postgres.container", 
DEFAULT_POSTGRES_CONTAINER);
+
+        container = new PostgreSQLContainer(containerName);
+    }
+
+    public PostgresLocalContainerService(PostgreSQLContainer container) {
+        this.container = container;
+    }
+
+    @Override
+    public void registerProperties() {
+        System.setProperty(PostgresProperties.SERVICE_ADDRESS, 
getServiceAddress());
+        System.setProperty(PostgresProperties.HOST, host());
+        System.setProperty(PostgresProperties.PORT, String.valueOf(port()));
+        System.setProperty(PostgresProperties.USERNAME, 
container.getUsername());
+        System.setProperty(PostgresProperties.PASSWORD, 
container.getPassword());
+    }
+
+    @Override
+    public void initialize() {
+        LOG.info("Trying to start the Postgres container");
+        container.start();
+
+        registerProperties();
+        LOG.info("Postgres instance running at {}", getServiceAddress());
+    }
+
+    @Override
+    public void shutdown() {
+        LOG.info("Stopping the Postgres container");
+        container.stop();
+    }
+
+    @Override
+    public GenericContainer getContainer() {
+        return container;
+    }
+
+    @Override
+    public String host() {
+        return container.getHost();
+    }
+
+    @Override
+    public int port() {
+        return container.getMappedPort(PostgreSQLContainer.POSTGRESQL_PORT);
+    }
+
+    @Override
+    public String getServiceAddress() {
+        return String.format("%s:%d", host(), port());
+    }
+
+    @Override
+    public String userName() {
+        return container.getUsername();
+    }
+
+    @Override
+    public String password() {
+        return container.getPassword();
+    }
+}
diff --git 
a/test-infra/camel-test-infra-postgres/src/test/java/org/apache/camel/test/infra/postgres/services/PostgresRemoteService.java
 
b/test-infra/camel-test-infra-postgres/src/test/java/org/apache/camel/test/infra/postgres/services/PostgresRemoteService.java
new file mode 100644
index 0000000..e3a7d7d
--- /dev/null
+++ 
b/test-infra/camel-test-infra-postgres/src/test/java/org/apache/camel/test/infra/postgres/services/PostgresRemoteService.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.postgres.services;
+
+import org.apache.camel.test.infra.postgres.common.PostgresProperties;
+
+public class PostgresRemoteService implements PostgresService {
+
+    @Override
+    public void registerProperties() {
+        // NO-OP
+    }
+
+    @Override
+    public void initialize() {
+        registerProperties();
+    }
+
+    @Override
+    public void shutdown() {
+        // NO-OP
+    }
+
+    @Override
+    public String host() {
+        return System.getProperty(PostgresProperties.HOST);
+    }
+
+    @Override
+    public int port() {
+        String port = System.getProperty(PostgresProperties.PORT);
+
+        if (port == null) {
+            return PostgresProperties.DEFAULT_PORT;
+        }
+
+        return Integer.valueOf(port);
+    }
+
+    @Override
+    public String getServiceAddress() {
+        return System.getProperty(PostgresProperties.SERVICE_ADDRESS);
+    }
+
+    @Override
+    public String userName() {
+        return System.getProperty(PostgresProperties.USERNAME);
+    }
+
+    @Override
+    public String password() {
+        return System.getProperty(PostgresProperties.PASSWORD);
+    }
+}
diff --git 
a/test-infra/camel-test-infra-postgres/src/test/java/org/apache/camel/test/infra/postgres/services/PostgresService.java
 
b/test-infra/camel-test-infra-postgres/src/test/java/org/apache/camel/test/infra/postgres/services/PostgresService.java
new file mode 100644
index 0000000..99be0d2
--- /dev/null
+++ 
b/test-infra/camel-test-infra-postgres/src/test/java/org/apache/camel/test/infra/postgres/services/PostgresService.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.postgres.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;
+
+/**
+ * Test infra service for Postgres
+ */
+public interface PostgresService extends BeforeAllCallback, AfterAllCallback, 
TestService {
+
+    String host();
+
+    int port();
+
+    String userName();
+
+    String password();
+
+    String getServiceAddress();
+
+    @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-postgres/src/test/java/org/apache/camel/test/infra/postgres/services/PostgresServiceFactory.java
 
b/test-infra/camel-test-infra-postgres/src/test/java/org/apache/camel/test/infra/postgres/services/PostgresServiceFactory.java
new file mode 100644
index 0000000..1ed0dd2
--- /dev/null
+++ 
b/test-infra/camel-test-infra-postgres/src/test/java/org/apache/camel/test/infra/postgres/services/PostgresServiceFactory.java
@@ -0,0 +1,43 @@
+/*
+ * 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.postgres.services;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public final class PostgresServiceFactory {
+    private static final Logger LOG = 
LoggerFactory.getLogger(PostgresServiceFactory.class);
+
+    private PostgresServiceFactory() {
+
+    }
+
+    public static PostgresService createService() {
+        String instanceType = System.getProperty("postgres.instance.type");
+
+        if (instanceType == null || 
instanceType.equals("local-postgres-container")) {
+            return new PostgresLocalContainerService();
+        }
+
+        if (instanceType.equals("remote")) {
+            return new PostgresRemoteService();
+        }
+
+        LOG.error("Postgres instance must be one of 'local-postgres-container' 
or 'remote");
+        throw new UnsupportedOperationException("Invalid Postgres instance 
type");
+    }
+}
diff --git a/test-infra/pom.xml b/test-infra/pom.xml
index 057bbd0..5eb74b0 100644
--- a/test-infra/pom.xml
+++ b/test-infra/pom.xml
@@ -64,5 +64,6 @@
         <module>camel-test-infra-pulsar</module>
         <module>camel-test-infra-xmpp</module>
         <module>camel-test-infra-zookeeper</module>
+        <module>camel-test-infra-postgres</module>
     </modules>
 </project>

Reply via email to