From commits-return-78153-archive-asf-public=cust-asf.ponee.io@camel.apache.org Fri Oct 4 16:14:56 2019 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [207.244.88.153]) by mx-eu-01.ponee.io (Postfix) with SMTP id 71C1A180674 for ; Fri, 4 Oct 2019 18:14:56 +0200 (CEST) Received: (qmail 42774 invoked by uid 500); 4 Oct 2019 16:14:55 -0000 Mailing-List: contact commits-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@camel.apache.org Delivered-To: mailing list commits@camel.apache.org Received: (qmail 42691 invoked by uid 99); 4 Oct 2019 16:14:55 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 04 Oct 2019 16:14:55 +0000 Received: by gitbox.apache.org (ASF Mail Server at gitbox.apache.org, from userid 33) id 82AFE890A4; Fri, 4 Oct 2019 16:14:55 +0000 (UTC) Date: Fri, 04 Oct 2019 16:14:51 +0000 To: "commits@camel.apache.org" Subject: [camel] 01/01: CAMEL-14307: Created camel-testcontainers-junit5 from camel-testcontainers MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit From: aldettinger@apache.org In-Reply-To: <157020569059.16429.7549464550541939516@gitbox.apache.org> References: <157020569059.16429.7549464550541939516@gitbox.apache.org> X-Git-Host: gitbox.apache.org X-Git-Repo: camel X-Git-Refname: refs/heads/CAMEL-13965 X-Git-Reftype: branch X-Git-Rev: c6ad2be5c64b0bb8cf0f5a16a71f53a5d56a71b5 X-Git-NotificationType: diff X-Git-Multimail-Version: 1.5.dev Auto-Submitted: auto-generated Message-Id: <20191004161455.82AFE890A4@gitbox.apache.org> This is an automated email from the ASF dual-hosted git repository. aldettinger pushed a commit to branch CAMEL-13965 in repository https://gitbox.apache.org/repos/asf/camel.git commit c6ad2be5c64b0bb8cf0f5a16a71f53a5d56a71b5 Author: aldettinger AuthorDate: Fri Oct 4 11:05:05 2019 +0200 CAMEL-14307: Created camel-testcontainers-junit5 from camel-testcontainers --- apache-camel/pom.xml | 5 + apache-camel/src/main/descriptors/common-bin.xml | 1 + bom/camel-bom/pom.xml | 5 + components/camel-consul/pom.xml | 10 ++ .../camel/component/consul/ConsulCatalogTest.java | 16 +-- .../component/consul/ConsulClientKeyValueTest.java | 5 +- .../component/consul/ConsulCoordinatesTest.java | 20 +-- .../camel/component/consul/ConsulEventTest.java | 6 +- .../component/consul/ConsulEventWatchTest.java | 2 +- .../camel/component/consul/ConsulHealthTest.java | 12 +- .../camel/component/consul/ConsulKeyValueTest.java | 5 +- .../component/consul/ConsulKeyValueWatchTest.java | 2 +- .../camel/component/consul/ConsulRegistryTest.java | 21 ++-- .../component/consul/ConsulRegistryUtilsTest.java | 6 +- .../camel/component/consul/ConsulSessionTest.java | 18 +-- .../camel/component/consul/ConsulTestSupport.java | 11 +- .../cloud/ConsulDefaultServiceCallRouteTest.java | 2 +- .../cloud/ConsulRibbonServiceCallRouteTest.java | 2 +- .../ConsulServiceCallWithRegistrationTest.java | 10 +- .../consul/cloud/ConsulServiceDiscoveryTest.java | 5 +- .../cloud/ConsulServiceRegistrationTestBase.java | 5 +- .../consul/cloud/ConsulServiceRegistryTest.java | 4 +- .../apache/camel/test/junit5/CamelTestSupport.java | 7 ++ .../pom.xml | 126 ++++++------------- .../src/main/docs/testcontainers-junit5.adoc | 136 ++++++++++++++++++++ .../junit5/ContainerAwareTestSupport.java | 108 ++++++++++++++++ .../junit5/ContainerPropertiesFunction.java | 74 +++++++++++ .../test/testcontainers/junit5/Containers.java | 84 +++++++++++++ .../camel/test/testcontainers/junit5/Wait.java | 69 +++++++++++ .../junit5/ContainerAwareTestSupportIT.java | 55 +++++++++ .../src/test/resources/log4j2.properties | 30 +++++ components/pom.xml | 1 + docs/components/modules/ROOT/nav.adoc | 1 + .../modules/ROOT/pages/testcontainers-junit5.adoc | 137 +++++++++++++++++++++ parent/pom.xml | 5 + .../camel-spring-boot-dependencies/pom.xml | 5 + 36 files changed, 858 insertions(+), 153 deletions(-) diff --git a/apache-camel/pom.xml b/apache-camel/pom.xml index f0279da..505dd0a 100644 --- a/apache-camel/pom.xml +++ b/apache-camel/pom.xml @@ -1523,6 +1523,11 @@ org.apache.camel + camel-testcontainers-junit5 + ${project.version} + + + org.apache.camel camel-testcontainers-spring ${project.version} diff --git a/apache-camel/src/main/descriptors/common-bin.xml b/apache-camel/src/main/descriptors/common-bin.xml index 6438386..404e758 100644 --- a/apache-camel/src/main/descriptors/common-bin.xml +++ b/apache-camel/src/main/descriptors/common-bin.xml @@ -325,6 +325,7 @@ org.apache.camel:camel-test-spring org.apache.camel:camel-test-spring-junit5 org.apache.camel:camel-testcontainers + org.apache.camel:camel-testcontainers-junit5 org.apache.camel:camel-testcontainers-spring org.apache.camel:camel-thrift org.apache.camel:camel-tika diff --git a/bom/camel-bom/pom.xml b/bom/camel-bom/pom.xml index 041167d..a15a7cb 100644 --- a/bom/camel-bom/pom.xml +++ b/bom/camel-bom/pom.xml @@ -2935,6 +2935,11 @@ org.apache.camel + camel-testcontainers-junit5 + ${project.version} + + + org.apache.camel camel-testcontainers-spring ${project.version} diff --git a/components/camel-consul/pom.xml b/components/camel-consul/pom.xml index c17d1a4..65eef4f 100644 --- a/components/camel-consul/pom.xml +++ b/components/camel-consul/pom.xml @@ -55,6 +55,16 @@ org.apache.camel + camel-testcontainers-junit5 + test + + + org.junit.vintage + junit-vintage-engine + test + + + org.apache.camel camel-mock test diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulCatalogTest.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulCatalogTest.java index d96305b..097daec 100644 --- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulCatalogTest.java +++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulCatalogTest.java @@ -21,8 +21,8 @@ import java.util.List; import com.orbitz.consul.model.health.Node; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.consul.endpoint.ConsulCatalogActions; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; public class ConsulCatalogTest extends ConsulTestSupport { @@ -34,9 +34,9 @@ public class ConsulCatalogTest extends ConsulTestSupport { .to("direct:consul") .request(List.class); - Assert.assertFalse(ref.isEmpty()); - Assert.assertFalse(res.isEmpty()); - Assert.assertEquals(ref, res); + Assertions.assertFalse(ref.isEmpty()); + Assertions.assertFalse(res.isEmpty()); + Assertions.assertEquals(ref, res); } @Test @@ -47,9 +47,9 @@ public class ConsulCatalogTest extends ConsulTestSupport { .to("direct:consul") .request(List.class); - Assert.assertFalse(ref.isEmpty()); - Assert.assertFalse(res.isEmpty()); - Assert.assertEquals(ref, res); + Assertions.assertFalse(ref.isEmpty()); + Assertions.assertFalse(res.isEmpty()); + Assertions.assertEquals(ref, res); } @Override diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulClientKeyValueTest.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulClientKeyValueTest.java index ef6cc27..749c22f 100644 --- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulClientKeyValueTest.java +++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulClientKeyValueTest.java @@ -24,7 +24,10 @@ import org.apache.camel.BindToRegistry; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.consul.endpoint.ConsulKeyValueActions; import org.apache.camel.component.mock.MockEndpoint; -import org.junit.Test; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; public class ConsulClientKeyValueTest extends ConsulTestSupport { diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulCoordinatesTest.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulCoordinatesTest.java index 1b9d280..fc8a855 100644 --- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulCoordinatesTest.java +++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulCoordinatesTest.java @@ -22,9 +22,9 @@ import com.orbitz.consul.model.coordinate.Coordinate; import com.orbitz.consul.model.coordinate.Datacenter; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.consul.endpoint.ConsulCoordinatesActions; -import org.junit.Assert; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; public class ConsulCoordinatesTest extends ConsulTestSupport { @@ -36,12 +36,12 @@ public class ConsulCoordinatesTest extends ConsulTestSupport { .to("direct:consul") .request(List.class); - Assert.assertFalse(ref.isEmpty()); - Assert.assertFalse(res.isEmpty()); - Assert.assertEquals(ref, res); + Assertions.assertFalse(ref.isEmpty()); + Assertions.assertFalse(res.isEmpty()); + Assertions.assertEquals(ref, res); } - @Ignore("Disabled as it seems that nodes is always 0") + @Disabled("Disabled as it seems that nodes is always 0") @Test public void testNodes() throws Exception { List ref = getConsul().coordinateClient().getNodes(); @@ -50,9 +50,9 @@ public class ConsulCoordinatesTest extends ConsulTestSupport { .to("direct:consul") .request(List.class); - Assert.assertFalse(ref.isEmpty()); - Assert.assertFalse(res.isEmpty()); - Assert.assertEquals(ref, res); + Assertions.assertFalse(ref.isEmpty()); + Assertions.assertFalse(res.isEmpty()); + Assertions.assertEquals(ref, res); } @Override diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulEventTest.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulEventTest.java index 6daed84..c2f4d51 100644 --- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulEventTest.java +++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulEventTest.java @@ -23,7 +23,11 @@ import com.orbitz.consul.model.event.Event; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.consul.endpoint.ConsulEventActions; import org.apache.camel.component.mock.MockEndpoint; -import org.junit.Test; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; public class ConsulEventTest extends ConsulTestSupport { diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulEventWatchTest.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulEventWatchTest.java index 8550295..b546e1c 100644 --- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulEventWatchTest.java +++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulEventWatchTest.java @@ -21,7 +21,7 @@ import java.util.List; import com.orbitz.consul.EventClient; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.mock.MockEndpoint; -import org.junit.Test; +import org.junit.jupiter.api.Test; public class ConsulEventWatchTest extends ConsulTestSupport { private String key; diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulHealthTest.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulHealthTest.java index 79e2cfe..2f6c9ef 100644 --- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulHealthTest.java +++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulHealthTest.java @@ -27,8 +27,10 @@ import com.orbitz.consul.model.agent.Registration; import com.orbitz.consul.model.health.ServiceHealth; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.consul.endpoint.ConsulHealthActions; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertTrue; public class ConsulHealthTest extends ConsulTestSupport { private AgentClient client; @@ -85,9 +87,9 @@ public class ConsulHealthTest extends ConsulTestSupport { .to("direct:consul") .request(List.class); - Assert.assertEquals(2, ref.size()); - Assert.assertEquals(2, res.size()); - Assert.assertEquals(ref, res); + Assertions.assertEquals(2, ref.size()); + Assertions.assertEquals(2, res.size()); + Assertions.assertEquals(ref, res); assertTrue(registrations.stream().anyMatch( r -> r.getPort().isPresent() && r.getPort().get() == res.get(0).getService().getPort() && r.getId().equalsIgnoreCase(res.get(0).getService().getId()) diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulKeyValueTest.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulKeyValueTest.java index dc03889..6c96fc4 100644 --- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulKeyValueTest.java +++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulKeyValueTest.java @@ -21,7 +21,10 @@ import java.util.Optional; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.consul.endpoint.ConsulKeyValueActions; import org.apache.camel.component.mock.MockEndpoint; -import org.junit.Test; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; public class ConsulKeyValueTest extends ConsulTestSupport { diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulKeyValueWatchTest.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulKeyValueWatchTest.java index 716e25e..7dcfab9 100644 --- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulKeyValueWatchTest.java +++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulKeyValueWatchTest.java @@ -22,7 +22,7 @@ import java.util.Random; import com.orbitz.consul.KeyValueClient; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.mock.MockEndpoint; -import org.junit.Test; +import org.junit.jupiter.api.Test; public class ConsulKeyValueWatchTest extends ConsulTestSupport { private String key; diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulRegistryTest.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulRegistryTest.java index 67cc2fd..a6fa6a7 100644 --- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulRegistryTest.java +++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulRegistryTest.java @@ -23,14 +23,15 @@ import java.util.Set; import com.orbitz.consul.Consul; import org.apache.camel.NoSuchBeanException; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import org.testcontainers.containers.GenericContainer; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; /** * Unit test for Camel Registry implementation for Consul @@ -49,7 +50,7 @@ public class ConsulRegistryTest implements Serializable { } } - @BeforeClass + @BeforeAll public static void setUp() { container = ConsulTestSupport.consulContainer(); container.start(); @@ -57,7 +58,7 @@ public class ConsulRegistryTest implements Serializable { registry = new ConsulRegistry(container.getContainerIpAddress(), container.getMappedPort(Consul.DEFAULT_HTTP_PORT)); } - @AfterClass + @AfterAll public static void tearDown() { container.stop(); } @@ -165,8 +166,8 @@ public class ConsulRegistryTest implements Serializable { } - @Test(expected = NoSuchBeanException.class) + @Test public void deleteNonExisting() { - registry.remove("nonExisting"); + assertThrows(NoSuchBeanException.class, () -> registry.remove("nonExisting")); } } diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulRegistryUtilsTest.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulRegistryUtilsTest.java index f56045d..221e569 100644 --- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulRegistryUtilsTest.java +++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulRegistryUtilsTest.java @@ -21,10 +21,10 @@ import java.util.Arrays; import java.util.List; import org.apache.camel.component.consul.ConsulRegistry.ConsulRegistryUtils; -import org.junit.Test; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; /** * @author Peter Palaga diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulSessionTest.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulSessionTest.java index 63fa765..300feaa 100644 --- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulSessionTest.java +++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulSessionTest.java @@ -24,8 +24,8 @@ import com.orbitz.consul.model.session.SessionCreatedResponse; import com.orbitz.consul.model.session.SessionInfo; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.consul.endpoint.ConsulSessionActions; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; public class ConsulSessionTest extends ConsulTestSupport { @@ -40,8 +40,8 @@ public class ConsulSessionTest extends ConsulTestSupport { .to("direct:consul") .request(List.class); - Assert.assertEquals(sessions, list.size()); - Assert.assertFalse(list.stream().anyMatch(s -> s.getName().isPresent() && s.getName().get().equals(name))); + Assertions.assertEquals(sessions, list.size()); + Assertions.assertFalse(list.stream().anyMatch(s -> s.getName().isPresent() && s.getName().get().equals(name))); } SessionCreatedResponse res = fluentTemplate() @@ -50,7 +50,7 @@ public class ConsulSessionTest extends ConsulTestSupport { .to("direct:consul") .request(SessionCreatedResponse.class); - Assert.assertNotNull(res.getId()); + Assertions.assertNotNull(res.getId()); { List list = fluentTemplate() @@ -58,8 +58,8 @@ public class ConsulSessionTest extends ConsulTestSupport { .to("direct:consul") .request(List.class); - Assert.assertEquals(sessions + 1, list.size()); - Assert.assertTrue(list.stream().anyMatch(s -> s.getName().isPresent() && s.getName().get().equals(name))); + Assertions.assertEquals(sessions + 1, list.size()); + Assertions.assertTrue(list.stream().anyMatch(s -> s.getName().isPresent() && s.getName().get().equals(name))); } { @@ -74,8 +74,8 @@ public class ConsulSessionTest extends ConsulTestSupport { .to("direct:consul") .request(List.class); - Assert.assertEquals(sessions, list.size()); - Assert.assertFalse(list.stream().anyMatch(s -> s.getName().isPresent() && s.getName().get().equals(name))); + Assertions.assertEquals(sessions, list.size()); + Assertions.assertFalse(list.stream().anyMatch(s -> s.getName().isPresent() && s.getName().get().equals(name))); } } diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulTestSupport.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulTestSupport.java index 5dff0b3..2fecc6d 100644 --- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulTestSupport.java +++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulTestSupport.java @@ -24,10 +24,8 @@ import com.orbitz.consul.Consul; import com.orbitz.consul.KeyValueClient; import org.apache.camel.BindToRegistry; -import org.apache.camel.test.testcontainers.ContainerAwareTestSupport; -import org.apache.camel.test.testcontainers.Wait; -import org.junit.Rule; -import org.junit.rules.TestName; +import org.apache.camel.test.testcontainers.junit5.ContainerAwareTestSupport; +import org.apache.camel.test.testcontainers.junit5.Wait; import org.testcontainers.containers.GenericContainer; public class ConsulTestSupport extends ContainerAwareTestSupport { @@ -35,9 +33,6 @@ public class ConsulTestSupport extends ContainerAwareTestSupport { public static final String CONTAINER_NAME = "consul"; public static final String KV_PREFIX = "/camel"; - @Rule - public final TestName testName = new TestName(); - @BindToRegistry("consul") public ConsulComponent getConsulComponent() { ConsulComponent component = new ConsulComponent(); @@ -69,7 +64,7 @@ public class ConsulTestSupport extends ContainerAwareTestSupport { } protected String generateKey() { - return KV_PREFIX + "/" + testName.getMethodName() + "/" + generateRandomString(); + return KV_PREFIX + "/" + getCurrentTestName() + "/" + generateRandomString(); } protected String consulUrl() { diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulDefaultServiceCallRouteTest.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulDefaultServiceCallRouteTest.java index c21ed04..1f054d0 100644 --- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulDefaultServiceCallRouteTest.java +++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulDefaultServiceCallRouteTest.java @@ -26,7 +26,7 @@ import org.apache.camel.RoutesBuilder; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.consul.ConsulTestSupport; import org.apache.camel.test.AvailablePortFinder; -import org.junit.Test; +import org.junit.jupiter.api.Test; public class ConsulDefaultServiceCallRouteTest extends ConsulTestSupport { private static final String SERVICE_NAME = "http-service"; diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulRibbonServiceCallRouteTest.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulRibbonServiceCallRouteTest.java index eb7c769..ec26483 100644 --- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulRibbonServiceCallRouteTest.java +++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulRibbonServiceCallRouteTest.java @@ -26,7 +26,7 @@ import org.apache.camel.RoutesBuilder; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.consul.ConsulTestSupport; import org.apache.camel.test.AvailablePortFinder; -import org.junit.Test; +import org.junit.jupiter.api.Test; public class ConsulRibbonServiceCallRouteTest extends ConsulTestSupport { private static final String SERVICE_NAME = "http-service"; diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceCallWithRegistrationTest.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceCallWithRegistrationTest.java index e7f6a80..506fcfd 100644 --- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceCallWithRegistrationTest.java +++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceCallWithRegistrationTest.java @@ -23,9 +23,12 @@ import org.apache.camel.CamelExecutionException; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.consul.ConsulTestSupport; import org.apache.camel.impl.cloud.ServiceRegistrationRoutePolicy; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.util.SocketUtils; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + public class ConsulServiceCallWithRegistrationTest extends ConsulTestSupport { private static final String SERVICE_HOST = "localhost"; @@ -93,7 +96,7 @@ public class ConsulServiceCallWithRegistrationTest extends ConsulTestSupport { assertEquals("ping on " + port, template.requestBody("direct:start", "ping", String.class)); } - @Test(expected = CamelExecutionException.class) + @Test public void testServiceCallFailure() throws Exception { final int port = SocketUtils.findAvailableTcpPort(); final String serviceId = UUID.randomUUID().toString(); @@ -126,7 +129,6 @@ public class ConsulServiceCallWithRegistrationTest extends ConsulTestSupport { context.start(); - template.requestBody("direct:start", "ping", String.class); - fail("Should have failed"); + assertThrows(CamelExecutionException.class, () -> template.requestBody("direct:start", "ping", String.class)); } } diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceDiscoveryTest.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceDiscoveryTest.java index 8023c29..d0277e8 100644 --- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceDiscoveryTest.java +++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceDiscoveryTest.java @@ -29,9 +29,12 @@ import org.apache.camel.cloud.ServiceDiscovery; import org.apache.camel.component.consul.ConsulConfiguration; import org.apache.camel.component.consul.ConsulTestSupport; import org.assertj.core.api.Assertions; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.util.SocketUtils; +import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + public class ConsulServiceDiscoveryTest extends ConsulTestSupport { private AgentClient client; private List registrations; diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceRegistrationTestBase.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceRegistrationTestBase.java index b9a3bed..231cf88 100644 --- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceRegistrationTestBase.java +++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceRegistrationTestBase.java @@ -28,9 +28,12 @@ import com.orbitz.consul.model.health.ServiceHealth; import org.apache.camel.CamelContext; import org.apache.camel.cloud.ServiceDefinition; import org.apache.camel.component.consul.ConsulTestSupport; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.util.SocketUtils; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + public abstract class ConsulServiceRegistrationTestBase extends ConsulTestSupport { protected static final String SERVICE_ID = UUID.randomUUID().toString(); protected static final String SERVICE_NAME = "my-service"; diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceRegistryTest.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceRegistryTest.java index 2c7f574..b4e0df0 100644 --- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceRegistryTest.java +++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulServiceRegistryTest.java @@ -24,7 +24,9 @@ import com.orbitz.consul.model.catalog.CatalogService; import com.orbitz.consul.model.health.ServiceHealth; import org.apache.camel.component.consul.ConsulTestSupport; import org.apache.camel.impl.cloud.DefaultServiceDefinition; -import org.junit.Test; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; public class ConsulServiceRegistryTest extends ConsulTestSupport { @Override diff --git a/components/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/CamelTestSupport.java b/components/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/CamelTestSupport.java index 31b45c5..3b74fbb 100644 --- a/components/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/CamelTestSupport.java +++ b/components/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/CamelTestSupport.java @@ -295,6 +295,13 @@ public abstract class CamelTestSupport implements BeforeEachCallback, AfterAllCa } /** + * Gets the name of the current test being executed. + */ + public final String getCurrentTestName() { + return currentTestName; + } + + /** * Override to enable debugger *

* Is default false diff --git a/components/camel-consul/pom.xml b/components/camel-testcontainers-junit5/pom.xml similarity index 57% copy from components/camel-consul/pom.xml copy to components/camel-testcontainers-junit5/pom.xml index c17d1a4..b80f32d 100644 --- a/components/camel-consul/pom.xml +++ b/components/camel-testcontainers-junit5/pom.xml @@ -18,8 +18,7 @@ --> - + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 @@ -28,86 +27,30 @@ 3.0.0-SNAPSHOT - camel-consul + camel-testcontainers-junit5 jar - Camel :: Consul - Camel Consul support + + Camel :: Testcontainers :: JUnit 5 + Camel support for testcontainers with JUnit 5 + 3.0.0 + + org.apache.camel - camel-cloud + camel-test-junit5 - com.orbitz.consul - consul-client - ${consul-client-version} + org.testcontainers + testcontainers + ${testcontainers-version} - - - org.apache.camel - camel-testcontainers-spring - test - - - org.apache.camel - camel-mock - test - - - org.apache.camel - camel-log - test - - - org.apache.camel - camel-bean - test - - - org.apache.camel - camel-file - test - - - org.apache.camel - camel-timer - test - - - org.apache.camel - camel-direct - test - - - org.apache.camel - camel-http - test - - - org.apache.camel - camel-jetty - test - - - org.apache.camel - camel-undertow - test - - - org.apache.camel - camel-master - test - - - org.apache.camel - camel-service - test - + org.assertj assertj-core @@ -115,11 +58,6 @@ test - org.apache.camel - camel-ribbon - test - - org.apache.logging.log4j log4j-api test @@ -134,12 +72,12 @@ log4j-slf4j-impl test - + - - consul-skip-tests + + testcontainers-skip-tests true @@ -154,10 +92,9 @@ - - + - consul-tests-docker-file + testcontainers-integration-tests-docker-file /var/run/docker.sock @@ -166,21 +103,30 @@ - maven-surefire-plugin + org.apache.maven.plugins + maven-failsafe-plugin ${skipTests} true + + + + integration-test + verify + + + - + - consul-tests-docker-env + testcontainers-integration-tests-docker-env env.DOCKER_HOST @@ -189,17 +135,25 @@ - maven-surefire-plugin + org.apache.maven.plugins + maven-failsafe-plugin ${skipTests} true + + + + integration-test + verify + + + - diff --git a/components/camel-testcontainers-junit5/src/main/docs/testcontainers-junit5.adoc b/components/camel-testcontainers-junit5/src/main/docs/testcontainers-junit5.adoc new file mode 100644 index 0000000..53353a3 --- /dev/null +++ b/components/camel-testcontainers-junit5/src/main/docs/testcontainers-junit5.adoc @@ -0,0 +1,136 @@ += Testcontainers + +*Available since 3.0.0* + +Testing camel components is sometime complex because the 3rd party system a component interacts with does not provide testing facilities and/or is only available as a native application. To reduce this complexity, *Camel Testcontainers JUnit 5* extends standard camel test support providing a way to create and interact with containerized applications. + +In order to define leverage testcontainers, add the following dependency to your pom: + +[source,xml] +---- + + org.apache.camel + camel-testcontainers-junit5 + x.x.x + + test + +---- + +[TIP] +==== +To learn more about testcontainers, please have a look at the https://www.testcontainers.org[official documentation] +==== + +To leverage testcontainers in your tests, you can use `ContainerAwareTestSupport` which is an extension of `CamelTestSupport` which: + +- create/destroy containers according to camel context lifecycle +- inject a custom `PropertiesFunction` to access container specific options from properties + + +To create a container you need to override: +[source, java] +---- +protected GenericContainer createContainer() +---- + +If you need to create more than one container, you can override the following method: +[source, java] +---- +protected List> createContainers() +---- + +The methods above are invoked before the camel context starts and blocks until the containers are reported to be alive. Containers are destroyed once the camel context stops. + +[source,java] +.Example +---- +@Override +protected GenericContainer createContainer() { + return new GenericContainer<>("consul:1.0.7") + .withNetworkAliases("myconsul") // <1> + .withExposedPorts(8500) + .waitingFor(Wait.forLogMessageContaining("Synced node info", 1)) // <2> + .withCommand( + "agent", + "-dev", + "-server", + "-bootstrap", + "-client", + "0.0.0.0", + "-log-level", + "trace" + ); +} +---- +<1> container name/alias +<2> container startup condition + +[IMPORTANT] +==== +It is important to give a name/alias to the container so you can then use properties functions to retrieve information such as container's host and port mapping. +==== +[TIP] +==== +You may need to wait for some condition to be satisfied before starting your test, to do so you need to configure the test suite to wait for such event using `GenericContainer.waitingFor`. Testcontainers provide a number of ready to use waiting strategy, for more info see the official testcontainers documentation. +==== + +Camel Testcontainer provides a PropertiesFunction implementation that can be used to: + +- retrieve the container host with the following syntax `container:host:$\{container-name\}` +- retrieve the port mapping with the following syntax `container:port:$\{exposed-port\}@$\{container-name\}` + +[source,java] +.Example +---- +public class MyTest extends ContainerAwareTestSupport { + @Test + public void testPropertyPlaceholders() throws Exception { + GenericContainer container = getContainer("myconsul"); + + String host = context.resolvePropertyPlaceholders("{{container:host:myconsul}}"); + assertThat(host).isEqualTo(container.getContainerIpAddress()); + + String port = context.resolvePropertyPlaceholders("{{container:port:8500@myconsul}}"); + assertThat(port).isEqualTo("" + container.getMappedPort(8500)); + } + + @Override + protected GenericContainer createContainer() { + return new GenericContainer<>("consul:1.0.7") + .withNetworkAliases("myconsul") + .withExposedPorts(8500) + .waitingFor(Wait.forLogMessageContaining("Synced node info", 1)) + .withCommand( + "agent", + "-dev", + "-server", + "-bootstrap", + "-client", + "0.0.0.0", + "-log-level", + "trace" + ); + } +} +---- + +== Migrating Camel Testcontainers Tests from JUnit 4 to JUnit 5 +Find below some hints to help in migrating camel testcontainers tests from JUnit 4 to JUnit 5. + +=== Referencing the Camel Testcontainers JUnit5 library in your project +Projects using `camel-testcontainers` would need to use `camel-testcontainers-junit5`. For instance, maven users would update their pom.xml file as below: +---- + + org.apache.camel + camel-testcontainers-junit5 + test + +---- + +Tips: It's possible to run JUnit4 & JUnit5 based camel tests side by side including the following dependencies `camel-testcontainers`, +`camel-testcontainers-junit5` and `junit-vintage-engine`. This configuration allows to migrate a camel test at once. + +=== Typical migration steps linked to JUnit 5 support in Camel Testcontainers +* Migration steps linked to xref:components::test-junit5.adoc[JUnit 5 support in Camel Test itself] should have been applied first +* Imports of `org.apache.camel.test.testcontainers.\*` should be replaced with `org.apache.camel.test.testcontainers.junit5.*` diff --git a/components/camel-testcontainers-junit5/src/main/java/org/apache/camel/test/testcontainers/junit5/ContainerAwareTestSupport.java b/components/camel-testcontainers-junit5/src/main/java/org/apache/camel/test/testcontainers/junit5/ContainerAwareTestSupport.java new file mode 100644 index 0000000..3c4a13a --- /dev/null +++ b/components/camel-testcontainers-junit5/src/main/java/org/apache/camel/test/testcontainers/junit5/ContainerAwareTestSupport.java @@ -0,0 +1,108 @@ +/* + * 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.testcontainers.junit5; + +import java.util.Collections; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.TimeUnit; + +import org.apache.camel.CamelContext; +import org.apache.camel.component.properties.PropertiesComponent; +import org.apache.camel.test.junit5.CamelTestSupport; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.Network; + +public class ContainerAwareTestSupport extends CamelTestSupport { + private List> containers = new CopyOnWriteArrayList<>(); + + // ****************** + // Setup + // ****************** + + @Override + protected void setupResources() throws Exception { + super.setupResources(); + + containers.clear(); + containers.addAll(createContainers()); + + final Network network = containerNetwork(); + final long timeout = containersStartupTimeout(); + + Containers.start(containers, network, timeout); + } + + @Override + protected void cleanupResources() throws Exception { + super.cleanupResources(); + + Containers.stop(containers, containerShutdownTimeout()); + } + + @Override + protected CamelContext createCamelContext() throws Exception { + final CamelContext context = super.createCamelContext(); + final PropertiesComponent pc = (PropertiesComponent)context.getPropertiesComponent(); + + pc.addFunction(new ContainerPropertiesFunction(containers)); + + return context; + } + + // ****************** + // Containers set-up + // ****************** + + protected GenericContainer createContainer() { + return null; + } + + protected List> createContainers() { + GenericContainer container = createContainer(); + + return container == null ? Collections.emptyList() : Collections.singletonList(container); + } + + protected long containersStartupTimeout() { + return TimeUnit.MINUTES.toSeconds(1); + } + + protected long containerShutdownTimeout() { + return TimeUnit.MINUTES.toSeconds(1); + } + + protected Network containerNetwork() { + return null; + } + + // ****************** + // Helpers + // ****************** + + protected GenericContainer getContainer(String containerName) { + return Containers.lookup(containers, containerName); + } + + protected String getContainerHost(String containerName) { + return getContainer(containerName).getContainerIpAddress(); + } + + protected int getContainerPort(String containerName, int originalPort) { + return getContainer(containerName).getMappedPort(originalPort); + } +} diff --git a/components/camel-testcontainers-junit5/src/main/java/org/apache/camel/test/testcontainers/junit5/ContainerPropertiesFunction.java b/components/camel-testcontainers-junit5/src/main/java/org/apache/camel/test/testcontainers/junit5/ContainerPropertiesFunction.java new file mode 100644 index 0000000..cb2862c --- /dev/null +++ b/components/camel-testcontainers-junit5/src/main/java/org/apache/camel/test/testcontainers/junit5/ContainerPropertiesFunction.java @@ -0,0 +1,74 @@ +/* + * 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.testcontainers.junit5; + +import java.util.List; + +import org.apache.camel.component.properties.PropertiesFunction; +import org.apache.camel.util.ObjectHelper; +import org.apache.camel.util.StringHelper; +import org.testcontainers.containers.GenericContainer; + +public class ContainerPropertiesFunction implements PropertiesFunction { + private final List> containers; + + public ContainerPropertiesFunction(List> containers) { + this.containers = ObjectHelper.notNull(containers, "Containers"); + } + + @Override + public String getName() { + return "container"; + } + + @Override + public String apply(String remainder) { + final String type = StringHelper.before(remainder, ":"); + final String query = StringHelper.after(remainder, ":"); + + if (ObjectHelper.isEmpty(type)) { + throw new IllegalArgumentException("container function syntax is container:type:query"); + } + + if ("host".equalsIgnoreCase(type)) { + String name = StringHelper.after(remainder, ":"); + + if (ObjectHelper.isEmpty(name)) { + throw new IllegalArgumentException("unable to determine container name"); + } + + return Containers.lookup(containers, StringHelper.after(remainder, ":")).getContainerIpAddress(); + } + + if ("port".equalsIgnoreCase(type)) { + String port = StringHelper.before(query, "@"); + String name = StringHelper.after(query, "@"); + + if (ObjectHelper.isEmpty(port)) { + throw new IllegalArgumentException("unable to determine original port"); + } + + if (ObjectHelper.isEmpty(name)) { + throw new IllegalArgumentException("unable to determine container name"); + } + + return Integer.toString(Containers.lookup(containers, name).getMappedPort(Integer.parseInt(port))); + } + + throw new IllegalArgumentException("Unsupported type: " + type); + } +} diff --git a/components/camel-testcontainers-junit5/src/main/java/org/apache/camel/test/testcontainers/junit5/Containers.java b/components/camel-testcontainers-junit5/src/main/java/org/apache/camel/test/testcontainers/junit5/Containers.java new file mode 100644 index 0000000..7e26024 --- /dev/null +++ b/components/camel-testcontainers-junit5/src/main/java/org/apache/camel/test/testcontainers/junit5/Containers.java @@ -0,0 +1,84 @@ +/* + * 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.testcontainers.junit5; + +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import static java.util.stream.Collectors.joining; + +import org.apache.camel.util.ObjectHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.Network; +import org.testcontainers.containers.output.Slf4jLogConsumer; + +public final class Containers { + private static final Logger LOGGER = LoggerFactory.getLogger(Containers.class); + + private Containers() { + } + + public static void start(List> containers, Network network, long timeout) throws Exception { + final CountDownLatch latch = new CountDownLatch(containers.size()); + + for (GenericContainer container : containers) { + if (ObjectHelper.isEmpty(container.getNetworkAliases())) { + throw new IllegalStateException("Container should have at least a network alias"); + } + + if (network != null) { + container.withNetwork(network); + } + + // Add custom logger + container.withLogConsumer(new Slf4jLogConsumer(LOGGER).withPrefix(container.getNetworkAliases().stream().collect(joining(",")))); + + new Thread(() -> { + container.start(); + latch.countDown(); + }).start(); + } + + latch.await(timeout, TimeUnit.SECONDS); + } + + public static void stop(List> containers, long timeout) throws Exception { + final CountDownLatch latch = new CountDownLatch(containers.size()); + + for (GenericContainer container : containers) { + new Thread(() -> { + container.stop(); + latch.countDown(); + }).start(); + } + + latch.await(timeout, TimeUnit.SECONDS); + } + + public static GenericContainer lookup(List> containers, String containerName) { + for (GenericContainer container : containers) { + if (container.getNetworkAliases().contains(containerName)) { + return container; + } + } + + throw new IllegalArgumentException("No container with name " + containerName + " found"); + } +} diff --git a/components/camel-testcontainers-junit5/src/main/java/org/apache/camel/test/testcontainers/junit5/Wait.java b/components/camel-testcontainers-junit5/src/main/java/org/apache/camel/test/testcontainers/junit5/Wait.java new file mode 100644 index 0000000..258b8b2 --- /dev/null +++ b/components/camel-testcontainers-junit5/src/main/java/org/apache/camel/test/testcontainers/junit5/Wait.java @@ -0,0 +1,69 @@ +/* + * 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.testcontainers.junit5; + +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import java.util.function.Predicate; + +import com.github.dockerjava.api.DockerClient; +import org.testcontainers.DockerClientFactory; +import org.testcontainers.containers.ContainerLaunchException; +import org.testcontainers.containers.output.OutputFrame; +import org.testcontainers.containers.output.WaitingConsumer; +import org.testcontainers.containers.wait.strategy.AbstractWaitStrategy; +import org.testcontainers.containers.wait.strategy.WaitStrategy; +import org.testcontainers.utility.LogUtils; + +public class Wait extends org.testcontainers.containers.wait.strategy.Wait { + /** + * Convenience method to return a WaitStrategy for log messages using a + * predicate. + * + * @param predicate the predicate to apply to log messages + * @param times the number of times the pattern is expected + * @return WaitStrategy + */ + public static WaitStrategy forLogPredicate(Predicate predicate, int times) { + return new AbstractWaitStrategy() { + @Override + protected void waitUntilReady() { + final DockerClient client = DockerClientFactory.instance().client(); + final WaitingConsumer waitingConsumer = new WaitingConsumer(); + + LogUtils.followOutput(client, waitStrategyTarget.getContainerId(), waitingConsumer); + + try { + waitingConsumer.waitUntil(predicate, startupTimeout.getSeconds(), TimeUnit.SECONDS, times); + } catch (TimeoutException e) { + throw new ContainerLaunchException("Timed out"); + } + } + }; + } + + /** + * Convenience method to return a WaitStrategy for log messages. + * + * @param text the text to find + * @param times the number of times the pattern is expected + * @return WaitStrategy + */ + public static WaitStrategy forLogMessageContaining(String text, int times) { + return forLogPredicate(u -> u.getUtf8String().contains(text), times); + } +} diff --git a/components/camel-testcontainers-junit5/src/test/java/org/apache/camel/test/testcontainers/junit5/ContainerAwareTestSupportIT.java b/components/camel-testcontainers-junit5/src/test/java/org/apache/camel/test/testcontainers/junit5/ContainerAwareTestSupportIT.java new file mode 100644 index 0000000..560b8e2 --- /dev/null +++ b/components/camel-testcontainers-junit5/src/test/java/org/apache/camel/test/testcontainers/junit5/ContainerAwareTestSupportIT.java @@ -0,0 +1,55 @@ +/* + * 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.testcontainers.junit5; + +import org.apache.camel.test.testcontainers.junit5.ContainerAwareTestSupport; +import org.apache.camel.test.testcontainers.junit5.Wait; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; +import org.testcontainers.containers.GenericContainer; + +public class ContainerAwareTestSupportIT extends ContainerAwareTestSupport { + @Test + public void testPropertyPlaceholders() throws Exception { + final GenericContainer container = getContainer("myconsul"); + + final String host = context.resolvePropertyPlaceholders("{{container:host:myconsul}}"); + Assertions.assertThat(host).isEqualTo(container.getContainerIpAddress()); + + final String port = context.resolvePropertyPlaceholders("{{container:port:8500@myconsul}}"); + Assertions.assertThat(port).isEqualTo("" + container.getMappedPort(8500)); + } + + @Override + protected GenericContainer createContainer() { + return new GenericContainer<>("consul:1.5.3") + .withNetworkAliases("myconsul") + .withExposedPorts(8500) + .waitingFor(Wait.forLogMessageContaining("Synced node info", 1)) + .withCommand( + "agent", + "-dev", + "-server", + "-bootstrap", + "-client", + "0.0.0.0", + "-log-level", + "trace" + ); + } + +} diff --git a/components/camel-testcontainers-junit5/src/test/resources/log4j2.properties b/components/camel-testcontainers-junit5/src/test/resources/log4j2.properties new file mode 100644 index 0000000..7db2ad4 --- /dev/null +++ b/components/camel-testcontainers-junit5/src/test/resources/log4j2.properties @@ -0,0 +1,30 @@ +## --------------------------------------------------------------------------- +## 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. +## --------------------------------------------------------------------------- + +appender.file.type = File +appender.file.name = file +appender.file.fileName = target/camel-testcontainers.log +appender.file.layout.type = PatternLayout +appender.file.layout.pattern = %d %-5p %c{1} - %m %n +appender.out.type = Console +appender.out.name = out +appender.out.layout.type = PatternLayout +appender.out.layout.pattern = [%30.30t] %-30.30c{1} %-5p %m%n +logger.spring.name = org.apache.camel.test.testcontainers +logger.spring.level = DEBUG +rootLogger.level = INFO +rootLogger.appenderRef.file.ref = file diff --git a/components/pom.xml b/components/pom.xml index a4133d1..a650994 100644 --- a/components/pom.xml +++ b/components/pom.xml @@ -67,6 +67,7 @@ camel-testcontainers-spring camel-test-junit5 camel-test-spring-junit5 + camel-testcontainers-junit5 camel-blueprint camel-spring camel-groovy diff --git a/docs/components/modules/ROOT/nav.adoc b/docs/components/modules/ROOT/nav.adoc index c75996a..ffe054b 100644 --- a/docs/components/modules/ROOT/nav.adoc +++ b/docs/components/modules/ROOT/nav.adoc @@ -353,6 +353,7 @@ * xref:test-spring-junit5.adoc[Camel Test Spring JUnit 5] * xref:test-spring.adoc[Test Spring] * xref:test.adoc[Test Module] +* xref:testcontainers-junit5.adoc[Testcontainers] * xref:testcontainers-spring.adoc[Testcontainers Spring] * xref:testcontainers.adoc[Testcontainers] * xref:thrift-component.adoc[Thrift Component] diff --git a/docs/components/modules/ROOT/pages/testcontainers-junit5.adoc b/docs/components/modules/ROOT/pages/testcontainers-junit5.adoc new file mode 100644 index 0000000..4f30b89 --- /dev/null +++ b/docs/components/modules/ROOT/pages/testcontainers-junit5.adoc @@ -0,0 +1,137 @@ += Testcontainers +:page-source: components/camel-testcontainers-junit5/src/main/docs/testcontainers-junit5.adoc + +*Available since 3.0.0* + +Testing camel components is sometime complex because the 3rd party system a component interacts with does not provide testing facilities and/or is only available as a native application. To reduce this complexity, *Camel Testcontainers JUnit 5* extends standard camel test support providing a way to create and interact with containerized applications. + +In order to define leverage testcontainers, add the following dependency to your pom: + +[source,xml] +---- + + org.apache.camel + camel-testcontainers-junit5 + x.x.x + + test + +---- + +[TIP] +==== +To learn more about testcontainers, please have a look at the official documentation https://www.testcontainers.org +==== + +To leverage testcontainers in your tests, you can use `ContainerAwareTestSupport` which is an extension of `CamelTestSupport` which: + +- create/destroy containers according to camel context lifecycle +- inject a custom `PropertiesFunction` to access container specific options from properties + + +To create a container you need to override: +[source, java] +---- +protected GenericContainer createContainer() +---- + +If you need to create more than one container, you can override the following method: +[source, java] +---- +protected List> createContainers() +---- + +The methods above are invoked before the camel context starts and blocks until the containers are reported to be alive. Containers are destroyed once the camel context stops. + +[source,java] +.Example +---- +@Override +protected GenericContainer createContainer() { + return new GenericContainer<>("consul:1.0.7") + .withNetworkAliases("myconsul") // <1> + .withExposedPorts(8500) + .waitingFor(Wait.forLogMessageContaining("Synced node info", 1)) // <2> + .withCommand( + "agent", + "-dev", + "-server", + "-bootstrap", + "-client", + "0.0.0.0", + "-log-level", + "trace" + ); +} +---- +<1> container name/alias +<2> container startup condition + +[IMPORTANT] +==== +It is important to give a name/alias to the container so you can then use properties functions to retrieve information such as container's host and port mapping. +==== +[TIP] +==== +You may need to wait for some condition to be satisfied before starting your test, to do so you need to configure the test suite to wait for such event using `GenericContainer.waitingFor`. Testcontainers provide a number of ready to use waiting strategy, for more info see the official testcontainers documentation. +==== + +Camel Testcontainer provides a PropertiesFunction implementation that can be used to: + +- retrieve the container host with the following syntax `container:host:$\{container-name\}` +- retrieve the port mapping with the following syntax `container:port:$\{exposed-port\}@$\{container-name\}` + +[source,java] +.Example +---- +public class MyTest extends ContainerAwareTestSupport { + @Test + public void testPropertyPlaceholders() throws Exception { + GenericContainer container = getContainer("myconsul"); + + String host = context.resolvePropertyPlaceholders("{{container:host:myconsul}}"); + assertThat(host).isEqualTo(container.getContainerIpAddress()); + + String port = context.resolvePropertyPlaceholders("{{container:port:8500@myconsul}}"); + assertThat(port).isEqualTo("" + container.getMappedPort(8500)); + } + + @Override + protected GenericContainer createContainer() { + return new GenericContainer<>("consul:1.0.7") + .withNetworkAliases("myconsul") + .withExposedPorts(8500) + .waitingFor(Wait.forLogMessageContaining("Synced node info", 1)) + .withCommand( + "agent", + "-dev", + "-server", + "-bootstrap", + "-client", + "0.0.0.0", + "-log-level", + "trace" + ); + } +} +---- + +== Migrating Camel Testcontainers Tests from JUnit 4 to JUnit 5 +Find below some hints to help in migrating camel testcontainers tests from JUnit 4 to JUnit 5. + +=== Referencing the Camel Testcontainers JUnit5 library in your project +Projects using `camel-testcontainers` would need to use `camel-testcontainers-junit5`. For instance, maven users would update their pom.xml file as below: +---- + + org.apache.camel + camel-testcontainers-junit5 + test + +---- + +Tips: It's possible to run JUnit4 & JUnit5 based camel tests side by side including the following dependencies `camel-testcontainers`, +`camel-testcontainers-junit5` and `junit-vintage-engine`. This configuration allows to migrate a camel test at once. + +=== Typical migration steps linked to JUnit 5 support in Camel Testcontainers +* Migration steps linked to xref:components::test-junit5.adoc[JUnit 5 support in Camel Test itself] should have been applied first +* Imports of `org.apache.camel.test.testcontainers.\*` should be replaced with `org.apache.camel.test.testcontainers.junit5.*` diff --git a/parent/pom.xml b/parent/pom.xml index 9c669a9..eee95cc 100644 --- a/parent/pom.xml +++ b/parent/pom.xml @@ -2272,6 +2272,11 @@ org.apache.camel + camel-testcontainers-junit5 + ${project.version} + + + org.apache.camel camel-testcontainers-spring ${project.version} diff --git a/platforms/spring-boot/spring-boot-dm/camel-spring-boot-dependencies/pom.xml b/platforms/spring-boot/spring-boot-dm/camel-spring-boot-dependencies/pom.xml index ad12838..637bd7b 100644 --- a/platforms/spring-boot/spring-boot-dm/camel-spring-boot-dependencies/pom.xml +++ b/platforms/spring-boot/spring-boot-dm/camel-spring-boot-dependencies/pom.xml @@ -3165,6 +3165,11 @@ org.apache.camel + camel-testcontainers-junit5 + ${project.version} + + + org.apache.camel camel-testcontainers-spring ${project.version}