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/12/01 15:40:58 UTC

[camel] branch master updated: Migrates camel-nats to the new test infra (#4706)

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 98e06884 Migrates camel-nats to the new test infra (#4706)
98e06884 is described below

commit 98e06884635b456c1b32ea01b5064e5ff6411eff
Author: Otavio Rodolfo Piske <or...@users.noreply.github.com>
AuthorDate: Tue Dec 1 16:40:35 2020 +0100

    Migrates camel-nats to the new test infra (#4706)
---
 components/camel-nats/pom.xml                      | 14 +++-
 .../component/nats/NatsAuthConsumerLoadTest.java   |  2 +-
 .../camel/component/nats/NatsAuthTestSupport.java  | 45 ++---------
 .../nats/NatsAuthTokenConsumerLoadTest.java        |  2 +-
 .../component/nats/NatsAuthTokenTestSupport.java   | 40 ++--------
 .../camel/component/nats/NatsConsumerLoadTest.java |  2 +-
 .../nats/NatsConsumerWithConnectionLoadTest.java   |  4 +-
 .../component/nats/NatsTLSAuthTestSupport.java     | 45 ++---------
 .../camel/component/nats/NatsTestSupport.java      | 35 ++-------
 test-infra/camel-test-infra-nats/pom.xml           | 60 +++++++++++++++
 .../src/main/resources/META-INF/MANIFEST.MF        |  0
 .../test/infra/nats/common/NatsProperties.java     | 13 ++--
 .../services/NatsLocalContainerAuthService.java    | 46 +++++++++++
 .../NatsLocalContainerAuthTokenService.java        | 44 +++++++++++
 .../nats/services/NatsLocalContainerService.java   | 88 ++++++++++++++++++++++
 .../services/NatsLocalContainerTLSAuthService.java | 37 +++++++++
 .../infra/nats/services/NatsRemoteService.java     | 24 +++++-
 .../test/infra/nats/services/NatsService.java      | 22 +++++-
 .../infra/nats/services/NatsServiceFactory.java    | 25 +++++-
 .../apache/camel/test/infra/nats/services}/ca.pem  |  0
 .../apache/camel/test/infra/nats/services}/key.pem |  0
 .../camel/test/infra/nats/services}/server.pem     |  0
 test-infra/pom.xml                                 |  1 +
 23 files changed, 392 insertions(+), 157 deletions(-)

diff --git a/components/camel-nats/pom.xml b/components/camel-nats/pom.xml
index a6030da..fd5f291 100644
--- a/components/camel-nats/pom.xml
+++ b/components/camel-nats/pom.xml
@@ -56,9 +56,21 @@
             <artifactId>log4j-slf4j-impl</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-nats</artifactId>
+            <version>${project.version}</version>
+            <type>test-jar</type>
             <scope>test</scope>
         </dependency>
     </dependencies>
diff --git a/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsAuthConsumerLoadTest.java b/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsAuthConsumerLoadTest.java
index 2993ce7..faf9761 100644
--- a/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsAuthConsumerLoadTest.java
+++ b/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsAuthConsumerLoadTest.java
@@ -35,7 +35,7 @@ public class NatsAuthConsumerLoadTest extends NatsAuthTestSupport {
     @Test
     public void testLoadConsumer() throws InterruptedException, IOException, TimeoutException {
         mockResultEndpoint.setExpectedMessageCount(100);
-        Options options = new Options.Builder().server("nats://" + getNatsBrokerUrl()).build();
+        Options options = new Options.Builder().server("nats://" + service.getServiceAddress()).build();
         Connection connection = Nats.connect(options);
 
         for (int i = 0; i < 100; i++) {
diff --git a/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsAuthTestSupport.java b/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsAuthTestSupport.java
index 382a0cd..27121cf 100644
--- a/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsAuthTestSupport.java
+++ b/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsAuthTestSupport.java
@@ -17,49 +17,20 @@
 package org.apache.camel.component.nats;
 
 import org.apache.camel.CamelContext;
-import org.apache.camel.test.testcontainers.junit5.ContainerAwareTestSupport;
-import org.apache.camel.test.testcontainers.junit5.Wait;
-import org.testcontainers.containers.GenericContainer;
+import org.apache.camel.test.infra.nats.services.NatsLocalContainerAuthService;
+import org.apache.camel.test.infra.nats.services.NatsLocalContainerService;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.extension.RegisterExtension;
 
-import static org.apache.camel.component.nats.NatsTestConstants.CONTAINER_IMAGE;
-
-public class NatsAuthTestSupport extends ContainerAwareTestSupport {
-
-    public static final String CONTAINER_NAME = "nats-auth";
-    public static final String USERNAME = "admin";
-    public static final String PASSWORD = "password";
-
-    @Override
-    protected GenericContainer<?> createContainer() {
-        return natsContainer();
-    }
-
-    public static GenericContainer natsContainer() {
-        return new GenericContainer(CONTAINER_IMAGE)
-                .withNetworkAliases(CONTAINER_NAME)
-                .waitingFor(Wait.forLogMessageContaining("Server is ready", 1))
-                .withCommand(
-                        "-DV",
-                        "--user",
-                        USERNAME,
-                        "--pass",
-                        PASSWORD);
-    }
-
-    public String getNatsBrokerUrl() {
-        return String.format(
-                "%s:%s@%s:%d",
-                USERNAME,
-                PASSWORD,
-                getContainerHost(CONTAINER_NAME),
-                getContainerPort(CONTAINER_NAME, 4222));
-    }
+public class NatsAuthTestSupport extends CamelTestSupport {
+    @RegisterExtension
+    static NatsLocalContainerService service = new NatsLocalContainerAuthService();
 
     @Override
     protected CamelContext createCamelContext() throws Exception {
         CamelContext context = super.createCamelContext();
         NatsComponent nats = context.getComponent("nats", NatsComponent.class);
-        nats.setServers(getNatsBrokerUrl());
+        nats.setServers(service.getServiceAddress());
         return context;
     }
 }
diff --git a/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsAuthTokenConsumerLoadTest.java b/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsAuthTokenConsumerLoadTest.java
index 13c404d..f02dd7a 100644
--- a/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsAuthTokenConsumerLoadTest.java
+++ b/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsAuthTokenConsumerLoadTest.java
@@ -35,7 +35,7 @@ public class NatsAuthTokenConsumerLoadTest extends NatsAuthTokenTestSupport {
     @Test
     public void testLoadConsumer() throws InterruptedException, IOException, TimeoutException {
         mockResultEndpoint.setExpectedMessageCount(100);
-        Options options = new Options.Builder().server("nats://" + getNatsBrokerUrl()).build();
+        Options options = new Options.Builder().server("nats://" + service.getServiceAddress()).build();
         Connection connection = Nats.connect(options);
 
         for (int i = 0; i < 100; i++) {
diff --git a/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsAuthTokenTestSupport.java b/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsAuthTokenTestSupport.java
index 1040124..432ebf1 100644
--- a/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsAuthTokenTestSupport.java
+++ b/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsAuthTokenTestSupport.java
@@ -17,45 +17,19 @@
 package org.apache.camel.component.nats;
 
 import org.apache.camel.CamelContext;
-import org.apache.camel.test.testcontainers.junit5.ContainerAwareTestSupport;
-import org.apache.camel.test.testcontainers.junit5.Wait;
-import org.testcontainers.containers.GenericContainer;
+import org.apache.camel.test.infra.nats.services.NatsLocalContainerAuthTokenService;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.extension.RegisterExtension;
 
-import static org.apache.camel.component.nats.NatsTestConstants.CONTAINER_IMAGE;
-
-public class NatsAuthTokenTestSupport extends ContainerAwareTestSupport {
-
-    public static final String CONTAINER_NAME = "nats-auth-token";
-    public static final String TOKEN = "!admin23456";
-
-    @Override
-    protected GenericContainer<?> createContainer() {
-        return natsContainer();
-    }
-
-    public static GenericContainer natsContainer() {
-        return new GenericContainer(CONTAINER_IMAGE)
-                .withNetworkAliases(CONTAINER_NAME)
-                .waitingFor(Wait.forLogMessageContaining("Server is ready", 1))
-                .withCommand(
-                        "-DV",
-                        "-auth",
-                        TOKEN);
-    }
-
-    public String getNatsBrokerUrl() {
-        return String.format(
-                "%s@%s:%d",
-                TOKEN,
-                getContainerHost(CONTAINER_NAME),
-                getContainerPort(CONTAINER_NAME, 4222));
-    }
+public class NatsAuthTokenTestSupport extends CamelTestSupport {
+    @RegisterExtension
+    static NatsLocalContainerAuthTokenService service = new NatsLocalContainerAuthTokenService();
 
     @Override
     protected CamelContext createCamelContext() throws Exception {
         CamelContext context = super.createCamelContext();
         NatsComponent nats = context.getComponent("nats", NatsComponent.class);
-        nats.setServers(getNatsBrokerUrl());
+        nats.setServers(service.getServiceAddress());
         return context;
     }
 
diff --git a/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsConsumerLoadTest.java b/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsConsumerLoadTest.java
index 2ee2e92..c6121e5 100644
--- a/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsConsumerLoadTest.java
+++ b/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsConsumerLoadTest.java
@@ -32,7 +32,7 @@ public class NatsConsumerLoadTest extends NatsTestSupport {
     @Test
     public void testLoadConsumer() throws Exception {
         mockResultEndpoint.setExpectedMessageCount(10000);
-        Options options = new Options.Builder().server("nats://" + getNatsBrokerUrl()).build();
+        Options options = new Options.Builder().server("nats://" + service.getServiceAddress()).build();
         Connection connection = Nats.connect(options);
 
         for (int i = 0; i < 10000; i++) {
diff --git a/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsConsumerWithConnectionLoadTest.java b/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsConsumerWithConnectionLoadTest.java
index 12b5550..4316f6d 100644
--- a/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsConsumerWithConnectionLoadTest.java
+++ b/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsConsumerWithConnectionLoadTest.java
@@ -39,7 +39,7 @@ public class NatsConsumerWithConnectionLoadTest extends NatsTestSupport {
     @BindToRegistry("connection")
     public Connection connection() throws Exception {
         Builder options = new Options.Builder();
-        options.server("nats://" + getNatsBrokerUrl());
+        options.server("nats://" + service.getServiceAddress());
         connection = Nats.connect(options.build());
         return connection;
     }
@@ -48,7 +48,7 @@ public class NatsConsumerWithConnectionLoadTest extends NatsTestSupport {
     public void testLoadConsumer() throws Exception {
         mockResultEndpoint.setExpectedMessageCount(100);
         mockResultEndpoint1.setExpectedMessageCount(0);
-        Options options = new Options.Builder().server("nats://" + getNatsBrokerUrl()).build();
+        Options options = new Options.Builder().server("nats://" + service.getServiceAddress()).build();
         Connection connection = Nats.connect(options);
 
         for (int i = 0; i < 100; i++) {
diff --git a/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsTLSAuthTestSupport.java b/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsTLSAuthTestSupport.java
index 2f9e68a..3429a08 100644
--- a/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsTLSAuthTestSupport.java
+++ b/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsTLSAuthTestSupport.java
@@ -17,49 +17,20 @@
 package org.apache.camel.component.nats;
 
 import org.apache.camel.CamelContext;
-import org.apache.camel.test.testcontainers.junit5.ContainerAwareTestSupport;
-import org.apache.camel.test.testcontainers.junit5.Wait;
-import org.testcontainers.containers.BindMode;
-import org.testcontainers.containers.GenericContainer;
+import org.apache.camel.test.infra.nats.services.NatsLocalContainerService;
+import org.apache.camel.test.infra.nats.services.NatsLocalContainerTLSAuthService;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.extension.RegisterExtension;
 
-import static org.apache.camel.component.nats.NatsTestConstants.CONTAINER_IMAGE;
-
-/*Certificates used for tests with TLS authentication come from:
- *https://github.com/nats-io/jnats/tree/master/src/test/resources */
-public class NatsTLSAuthTestSupport extends ContainerAwareTestSupport {
-
-    public static final String CONTAINER_NAME = "nats-tls";
-
-    @Override
-    protected GenericContainer<?> createContainer() {
-        return natsContainer();
-    }
-
-    public static GenericContainer natsContainer() {
-        return new GenericContainer(CONTAINER_IMAGE)
-                .withNetworkAliases(CONTAINER_NAME)
-                .withClasspathResourceMapping("org/apache/camel/component/nats", "/nats", BindMode.READ_ONLY)
-                .waitingFor(Wait.forLogMessageContaining("Server is ready", 1))
-                .withCommand(
-                        "--tls",
-                        "--tlscert=/nats/server.pem",
-                        "--tlskey=/nats/key.pem",
-                        "--tlsverify",
-                        "--tlscacert=/nats/ca.pem");
-    }
-
-    public String getNatsBrokerUrl() {
-        return String.format(
-                "%s:%d",
-                getContainerHost(CONTAINER_NAME),
-                getContainerPort(CONTAINER_NAME, 4222));
-    }
+public class NatsTLSAuthTestSupport extends CamelTestSupport {
+    @RegisterExtension
+    static NatsLocalContainerService service = new NatsLocalContainerTLSAuthService();
 
     @Override
     protected CamelContext createCamelContext() throws Exception {
         CamelContext context = super.createCamelContext();
         NatsComponent nats = context.getComponent("nats", NatsComponent.class);
-        nats.setServers(getNatsBrokerUrl());
+        nats.setServers(service.getServiceAddress());
         return context;
     }
 
diff --git a/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsTestSupport.java b/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsTestSupport.java
index 0fe765c..b6904b4 100644
--- a/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsTestSupport.java
+++ b/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsTestSupport.java
@@ -17,39 +17,20 @@
 package org.apache.camel.component.nats;
 
 import org.apache.camel.CamelContext;
-import org.apache.camel.test.testcontainers.junit5.ContainerAwareTestSupport;
-import org.apache.camel.test.testcontainers.junit5.Wait;
-import org.testcontainers.containers.GenericContainer;
+import org.apache.camel.test.infra.nats.services.NatsService;
+import org.apache.camel.test.infra.nats.services.NatsServiceFactory;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.extension.RegisterExtension;
 
-import static org.apache.camel.component.nats.NatsTestConstants.CONTAINER_IMAGE;
-
-public class NatsTestSupport extends ContainerAwareTestSupport {
-
-    public static final String CONTAINER_NAME = "nats";
-
-    @Override
-    protected GenericContainer<?> createContainer() {
-        return natsContainer();
-    }
-
-    public static GenericContainer natsContainer() {
-        return new GenericContainer(CONTAINER_IMAGE)
-                .withNetworkAliases(CONTAINER_NAME)
-                .waitingFor(Wait.forLogMessageContaining("Listening for route connections", 1));
-    }
-
-    public String getNatsBrokerUrl() {
-        return String.format(
-                "%s:%d",
-                getContainerHost(CONTAINER_NAME),
-                getContainerPort(CONTAINER_NAME, 4222));
-    }
+public class NatsTestSupport extends CamelTestSupport {
+    @RegisterExtension
+    static NatsService service = NatsServiceFactory.createService();
 
     @Override
     protected CamelContext createCamelContext() throws Exception {
         CamelContext context = super.createCamelContext();
         NatsComponent nats = context.getComponent("nats", NatsComponent.class);
-        nats.setServers(getNatsBrokerUrl());
+        nats.setServers(service.getServiceAddress());
         return context;
     }
 }
diff --git a/test-infra/camel-test-infra-nats/pom.xml b/test-infra/camel-test-infra-nats/pom.xml
new file mode 100644
index 0000000..a6916b8
--- /dev/null
+++ b/test-infra/camel-test-infra-nats/pom.xml
@@ -0,0 +1,60 @@
+<?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-nats</artifactId>
+    <name>Camel :: Test Infra :: Nats</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>
+    </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-nats/src/main/resources/META-INF/MANIFEST.MF b/test-infra/camel-test-infra-nats/src/main/resources/META-INF/MANIFEST.MF
new file mode 100644
index 0000000..e69de29
diff --git a/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsTestConstants.java b/test-infra/camel-test-infra-nats/src/test/java/org/apache/camel/test/infra/nats/common/NatsProperties.java
similarity index 67%
copy from components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsTestConstants.java
copy to test-infra/camel-test-infra-nats/src/test/java/org/apache/camel/test/infra/nats/common/NatsProperties.java
index 3f73f8a..79860b7 100644
--- a/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsTestConstants.java
+++ b/test-infra/camel-test-infra-nats/src/test/java/org/apache/camel/test/infra/nats/common/NatsProperties.java
@@ -14,13 +14,16 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.camel.component.nats;
 
-public final class NatsTestConstants {
+package org.apache.camel.test.infra.nats.common;
 
-    public static final String CONTAINER_IMAGE = "nats:2.1.9";
+public final class NatsProperties {
+    public static final String SERVICE_ADDRESS = "nat.service.address";
+    public static final String ACCESS_USERNAME = "nats.access.username";
+    public static final String ACCESS_PASSWORD = "nats.access.password";
+    public static final String ACCESS_TOKEN = "nats.access.token";
 
-    private NatsTestConstants() {
-    }
+    private NatsProperties() {
 
+    }
 }
diff --git a/test-infra/camel-test-infra-nats/src/test/java/org/apache/camel/test/infra/nats/services/NatsLocalContainerAuthService.java b/test-infra/camel-test-infra-nats/src/test/java/org/apache/camel/test/infra/nats/services/NatsLocalContainerAuthService.java
new file mode 100644
index 0000000..02cdf95
--- /dev/null
+++ b/test-infra/camel-test-infra-nats/src/test/java/org/apache/camel/test/infra/nats/services/NatsLocalContainerAuthService.java
@@ -0,0 +1,46 @@
+/*
+ * 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.nats.services;
+
+import org.apache.camel.test.infra.nats.common.NatsProperties;
+import org.testcontainers.containers.wait.strategy.Wait;
+
+public class NatsLocalContainerAuthService extends NatsLocalContainerService {
+    private static final String USERNAME = "admin";
+    private static final String PASSWORD = "password";
+
+    protected void initContainer(String imageName) {
+        super.initContainer(imageName);
+
+        getContainer()
+                .waitingFor(Wait.forLogMessage(".*Server.*is.*ready.*", 1))
+                .withCommand("-DV", "--user", USERNAME, "--pass", PASSWORD);
+    }
+
+    @Override
+    public void registerProperties() {
+        super.registerProperties();
+
+        System.setProperty(NatsProperties.ACCESS_USERNAME, USERNAME);
+        System.setProperty(NatsProperties.ACCESS_PASSWORD, PASSWORD);
+    }
+
+    @Override
+    public String getServiceAddress() {
+        return String.format("%s:%s@%s:%d", USERNAME, PASSWORD, getHost(), getPort());
+    }
+}
diff --git a/test-infra/camel-test-infra-nats/src/test/java/org/apache/camel/test/infra/nats/services/NatsLocalContainerAuthTokenService.java b/test-infra/camel-test-infra-nats/src/test/java/org/apache/camel/test/infra/nats/services/NatsLocalContainerAuthTokenService.java
new file mode 100644
index 0000000..7b6e3b5
--- /dev/null
+++ b/test-infra/camel-test-infra-nats/src/test/java/org/apache/camel/test/infra/nats/services/NatsLocalContainerAuthTokenService.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.nats.services;
+
+import org.apache.camel.test.infra.nats.common.NatsProperties;
+import org.testcontainers.containers.wait.strategy.Wait;
+
+public class NatsLocalContainerAuthTokenService extends NatsLocalContainerService {
+    public static final String TOKEN = "!admin23456";
+
+    protected void initContainer(String imageName) {
+        super.initContainer(imageName);
+
+        getContainer()
+                .waitingFor(Wait.forLogMessage(".*Server.*is.*ready.*", 1))
+                .withCommand("-DV", "-auth", TOKEN);
+    }
+
+    @Override
+    public void registerProperties() {
+        super.registerProperties();
+
+        System.setProperty(NatsProperties.ACCESS_TOKEN, TOKEN);
+    }
+
+    @Override
+    public String getServiceAddress() {
+        return String.format("%s@%s:%d", TOKEN, getHost(), getPort());
+    }
+}
diff --git a/test-infra/camel-test-infra-nats/src/test/java/org/apache/camel/test/infra/nats/services/NatsLocalContainerService.java b/test-infra/camel-test-infra-nats/src/test/java/org/apache/camel/test/infra/nats/services/NatsLocalContainerService.java
new file mode 100644
index 0000000..6c50c3e
--- /dev/null
+++ b/test-infra/camel-test-infra-nats/src/test/java/org/apache/camel/test/infra/nats/services/NatsLocalContainerService.java
@@ -0,0 +1,88 @@
+/*
+ * 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.nats.services;
+
+import org.apache.camel.test.infra.common.services.ContainerService;
+import org.apache.camel.test.infra.nats.common.NatsProperties;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.testcontainers.containers.GenericContainer;
+import org.testcontainers.containers.wait.strategy.Wait;
+
+public class NatsLocalContainerService implements NatsService, ContainerService<GenericContainer> {
+    public static final String CONTAINER_IMAGE = "nats:2.1.9";
+    public static final String CONTAINER_NAME = "nats";
+    private static final int PORT = 4222;
+
+    private static final Logger LOG = LoggerFactory.getLogger(NatsLocalContainerService.class);
+    private GenericContainer container;
+
+    public NatsLocalContainerService() {
+        String imageName = System.getProperty("nats.container", CONTAINER_IMAGE);
+
+        initContainer(imageName);
+    }
+
+    public NatsLocalContainerService(String imageName) {
+        initContainer(imageName);
+    }
+
+    protected void initContainer(String imageName) {
+        container = new GenericContainer(imageName)
+                .withNetworkAliases(CONTAINER_NAME)
+                .withExposedPorts(PORT)
+                .waitingFor(Wait.forLogMessage(".*Listening.*for.*route.*connections.*", 1));
+    }
+
+    @Override
+    public void registerProperties() {
+        System.setProperty(NatsProperties.SERVICE_ADDRESS, getServiceAddress());
+    }
+
+    @Override
+    public void initialize() {
+        LOG.info("Trying to start the Nats container");
+        container.start();
+
+        registerProperties();
+        LOG.info("Nats instance running at {}", getServiceAddress());
+    }
+
+    @Override
+    public void shutdown() {
+        LOG.info("Stopping the Nats container");
+        container.stop();
+    }
+
+    @Override
+    public GenericContainer getContainer() {
+        return container;
+    }
+
+    protected String getHost() {
+        return container.getHost();
+    }
+
+    protected int getPort() {
+        return container.getMappedPort(PORT);
+    }
+
+    @Override
+    public String getServiceAddress() {
+        return String.format("%s:%d", getHost(), getPort());
+    }
+}
diff --git a/test-infra/camel-test-infra-nats/src/test/java/org/apache/camel/test/infra/nats/services/NatsLocalContainerTLSAuthService.java b/test-infra/camel-test-infra-nats/src/test/java/org/apache/camel/test/infra/nats/services/NatsLocalContainerTLSAuthService.java
new file mode 100644
index 0000000..371beaf
--- /dev/null
+++ b/test-infra/camel-test-infra-nats/src/test/java/org/apache/camel/test/infra/nats/services/NatsLocalContainerTLSAuthService.java
@@ -0,0 +1,37 @@
+/*
+ * 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.nats.services;
+
+import org.testcontainers.containers.BindMode;
+import org.testcontainers.containers.wait.strategy.Wait;
+
+public class NatsLocalContainerTLSAuthService extends NatsLocalContainerService {
+    /*Certificates used for tests with TLS authentication come from:
+     *https://github.com/nats-io/jnats/tree/master/src/test/resources */
+    protected void initContainer(String imageName) {
+        super.initContainer(imageName);
+
+        getContainer()
+                .waitingFor(Wait.forLogMessage(".*Server.*is.*ready.*", 1))
+                .withClasspathResourceMapping("org/apache/camel/test/infra/nats/services", "/nats", BindMode.READ_ONLY)
+                .withCommand("--tls",
+                        "--tlscert=/nats/server.pem",
+                        "--tlskey=/nats/key.pem",
+                        "--tlsverify",
+                        "--tlscacert=/nats/ca.pem");
+    }
+}
diff --git a/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsTestConstants.java b/test-infra/camel-test-infra-nats/src/test/java/org/apache/camel/test/infra/nats/services/NatsRemoteService.java
similarity index 61%
copy from components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsTestConstants.java
copy to test-infra/camel-test-infra-nats/src/test/java/org/apache/camel/test/infra/nats/services/NatsRemoteService.java
index 3f73f8a..0bd880a 100644
--- a/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsTestConstants.java
+++ b/test-infra/camel-test-infra-nats/src/test/java/org/apache/camel/test/infra/nats/services/NatsRemoteService.java
@@ -14,13 +14,29 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.camel.component.nats;
+package org.apache.camel.test.infra.nats.services;
 
-public final class NatsTestConstants {
+import org.apache.camel.test.infra.nats.common.NatsProperties;
 
-    public static final String CONTAINER_IMAGE = "nats:2.1.9";
+public class NatsRemoteService implements NatsService {
 
-    private NatsTestConstants() {
+    @Override
+    public void registerProperties() {
+        // NO-OP
     }
 
+    @Override
+    public void initialize() {
+        registerProperties();
+    }
+
+    @Override
+    public void shutdown() {
+        // NO-OP
+    }
+
+    @Override
+    public String getServiceAddress() {
+        return System.getProperty(NatsProperties.SERVICE_ADDRESS);
+    }
 }
diff --git a/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsTestConstants.java b/test-infra/camel-test-infra-nats/src/test/java/org/apache/camel/test/infra/nats/services/NatsService.java
similarity index 54%
copy from components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsTestConstants.java
copy to test-infra/camel-test-infra-nats/src/test/java/org/apache/camel/test/infra/nats/services/NatsService.java
index 3f73f8a..4449af5 100644
--- a/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsTestConstants.java
+++ b/test-infra/camel-test-infra-nats/src/test/java/org/apache/camel/test/infra/nats/services/NatsService.java
@@ -14,13 +14,27 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.camel.component.nats;
+package org.apache.camel.test.infra.nats.services;
 
-public final class NatsTestConstants {
+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 static final String CONTAINER_IMAGE = "nats:2.1.9";
+/**
+ * Test infra service for Nats
+ */
+public interface NatsService extends BeforeAllCallback, AfterAllCallback, TestService {
+
+    String getServiceAddress();
 
-    private NatsTestConstants() {
+    @Override
+    default void beforeAll(ExtensionContext extensionContext) throws Exception {
+        initialize();
     }
 
+    @Override
+    default void afterAll(ExtensionContext extensionContext) throws Exception {
+        shutdown();
+    }
 }
diff --git a/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsTestConstants.java b/test-infra/camel-test-infra-nats/src/test/java/org/apache/camel/test/infra/nats/services/NatsServiceFactory.java
similarity index 50%
rename from components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsTestConstants.java
rename to test-infra/camel-test-infra-nats/src/test/java/org/apache/camel/test/infra/nats/services/NatsServiceFactory.java
index 3f73f8a..3f3c89e 100644
--- a/components/camel-nats/src/test/java/org/apache/camel/component/nats/NatsTestConstants.java
+++ b/test-infra/camel-test-infra-nats/src/test/java/org/apache/camel/test/infra/nats/services/NatsServiceFactory.java
@@ -14,13 +14,30 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.camel.component.nats;
+package org.apache.camel.test.infra.nats.services;
 
-public final class NatsTestConstants {
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
-    public static final String CONTAINER_IMAGE = "nats:2.1.9";
+public final class NatsServiceFactory {
+    private static final Logger LOG = LoggerFactory.getLogger(NatsServiceFactory.class);
+
+    private NatsServiceFactory() {
 
-    private NatsTestConstants() {
     }
 
+    public static NatsService createService() {
+        String instanceType = System.getProperty("nats.instance.type");
+
+        if (instanceType == null || instanceType.equals("local-nats-container")) {
+            return new NatsLocalContainerService();
+        }
+
+        if (instanceType.equals("remote")) {
+            return new NatsRemoteService();
+        }
+
+        LOG.error("Nats instance must be one of 'local-nats-container' or 'remote");
+        throw new UnsupportedOperationException("Invalid Nats instance type");
+    }
 }
diff --git a/components/camel-nats/src/test/resources/org/apache/camel/component/nats/ca.pem b/test-infra/camel-test-infra-nats/src/test/resources/org/apache/camel/test/infra/nats/services/ca.pem
similarity index 100%
rename from components/camel-nats/src/test/resources/org/apache/camel/component/nats/ca.pem
rename to test-infra/camel-test-infra-nats/src/test/resources/org/apache/camel/test/infra/nats/services/ca.pem
diff --git a/components/camel-nats/src/test/resources/org/apache/camel/component/nats/key.pem b/test-infra/camel-test-infra-nats/src/test/resources/org/apache/camel/test/infra/nats/services/key.pem
similarity index 100%
rename from components/camel-nats/src/test/resources/org/apache/camel/component/nats/key.pem
rename to test-infra/camel-test-infra-nats/src/test/resources/org/apache/camel/test/infra/nats/services/key.pem
diff --git a/components/camel-nats/src/test/resources/org/apache/camel/component/nats/server.pem b/test-infra/camel-test-infra-nats/src/test/resources/org/apache/camel/test/infra/nats/services/server.pem
similarity index 100%
rename from components/camel-nats/src/test/resources/org/apache/camel/component/nats/server.pem
rename to test-infra/camel-test-infra-nats/src/test/resources/org/apache/camel/test/infra/nats/services/server.pem
diff --git a/test-infra/pom.xml b/test-infra/pom.xml
index a297446..7d96ca6 100644
--- a/test-infra/pom.xml
+++ b/test-infra/pom.xml
@@ -59,5 +59,6 @@
         <module>camel-test-infra-hbase</module>
         <module>camel-test-infra-infinispan</module>
         <module>camel-test-infra-minio</module>
+        <module>camel-test-infra-nats</module>
     </modules>
 </project>