This is an automated email from the ASF dual-hosted git repository.
lburgazzoli 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 b53f408 CAMEL-12559: camel-testcontainers : add documentation
b53f408 is described below
commit b53f4088b9b6cd17daecc5c482c4e84093700d69
Author: lburgazzoli <[email protected]>
AuthorDate: Wed Jun 27 16:38:44 2018 +0200
CAMEL-12559: camel-testcontainers : add documentation
---
.../src/main/docs/testcontainers.adoc | 116 +++++++++++++++++++++
.../ContainerAwareTestSupportIT.java | 2 +-
2 files changed, 117 insertions(+), 1 deletion(-)
diff --git a/components/camel-testcontainers/src/main/docs/testcontainers.adoc
b/components/camel-testcontainers/src/main/docs/testcontainers.adoc
new file mode 100644
index 0000000..41067ac
--- /dev/null
+++ b/components/camel-testcontainers/src/main/docs/testcontainers.adoc
@@ -0,0 +1,116 @@
+## Testcontainers
+
+*Available since 2.22.0*
+
+Testing camel components is sometime complex because the t3th party system a
component interacts with does not provide testing facilities and/or is only
available as native application and to reduce this complexity, *Camel
Testcontainers* 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]
+----
+<dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-testcontainers</artifactId>
+ <version>x.x.x</version>
+ <!-- use the same version as your Camel core version -->
+ <scope>test</scope>
+</dependency>
+----
+
+[TIP]
+====
+To learn more about testcontainers, please have a look at the official
documentation https://www.testcontainers.org
+====
+
+To leverage testcontaners 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 that one container, you can override the following
method:
+[source, java]
+----
+protected List<GenericContainer<?>> 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"
+ );
+ }
+}
+----
diff --git
a/components/camel-testcontainers/src/test/java/org/apache/camel/test/testcontainers/ContainerAwareTestSupportIT.java
b/components/camel-testcontainers/src/test/java/org/apache/camel/test/testcontainers/ContainerAwareTestSupportIT.java
index ffaf6f0..8bbe332 100644
---
a/components/camel-testcontainers/src/test/java/org/apache/camel/test/testcontainers/ContainerAwareTestSupportIT.java
+++
b/components/camel-testcontainers/src/test/java/org/apache/camel/test/testcontainers/ContainerAwareTestSupportIT.java
@@ -34,7 +34,7 @@ public class ContainerAwareTestSupportIT extends
ContainerAwareTestSupport {
@Override
protected GenericContainer<?> createContainer() {
- return new GenericContainer("consul:1.0.7")
+ return new GenericContainer<>("consul:1.0.7")
.withNetworkAliases("myconsul")
.withExposedPorts(8500)
.waitingFor(Wait.forLogMessageContaining("Synced node info", 1))