Skip to content

Commit 96a3951

Browse files
committed
#1: Migration to testcontainers
1 parent b179d62 commit 96a3951

File tree

7 files changed

+32
-53
lines changed

7 files changed

+32
-53
lines changed

README.md

+17-26
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Spring RestTemplate calls multiple servers that are secured with multiple client certificate - setup with Docker Compose & Tested with docker-compose-rule
1+
Multiple Spring Boot servers that are secured with different client certificates - called by RestTemplate
22
=============================
33
[![Build Status](https://travis-ci.org/jonashackt/spring-boot-rest-clientcertificates-docker-compose.svg?branch=master)](https://travis-ci.org/jonashackt/spring-boot-rest-clientcertificates-docker-compose)
44

@@ -40,47 +40,38 @@ mvn clean install
4040
Only, if you want to check manually, you can do a `docker-compose up -d` and open your Browser with [http:localhost:8080/swagger-ui.html] and fire up a GET-Request to /secretservers with Swagger :)
4141

4242

43-
# Integrationtesting with [docker-compose-rule](https://github.com/palantir/docker-compose-rule)
43+
# Integrationtesting with [testcontainers](https://www.testcontainers.org)
4444

4545
As client-bob only has access to the DNS aliases `server-alice` and `server-tom`, if it itself is part of the Docker (Compose) network and these aliases are used to access both client certificate secured endpoints, we need another way to run an Integration test inside the Docker network scope.
4646

47-
Therefore we use the [docker-compose-rule](https://github.com/palantir/docker-compose-rule) and the __docker-network-client__ that just calls __client-bob__ inside the Docker network.
47+
Therefore we use the [testcontainers](https://www.testcontainers.org) and the __docker-network-client__ that just calls __client-bob__ inside the Docker network.
4848

49-
docker-compose-rule needs a special Maven repository to be added in `docker-network-client`, because it is only served on Bintray.
49+
testcontainers could be []simply integrated by via Maven](https://www.testcontainers.org/usage.html#maven-dependencies):
5050

5151
```
5252
<dependency>
53-
<groupId>com.palantir.docker.compose</groupId>
54-
<artifactId>docker-compose-rule-junit4</artifactId>
55-
<version>${docker-compose-rule-junit4.version}</version>
53+
<groupId>org.testcontainers</groupId>
54+
<artifactId>testcontainers</artifactId>
55+
<version>NewestTestcontainersVersionOnMavenCentral</version>
5656
<scope>test</scope>
5757
</dependency>
58-
59-
</dependencies>
60-
61-
<repositories>
62-
<repository>
63-
<id>bintray</id>
64-
<name>Bintray Maven Repository - as docker-compose-rule-junit4 is only available there</name>
65-
<url>https://dl.bintray.com/palantir/releases</url>
66-
<layout>default</layout>
67-
</repository>
68-
</repositories>
6958
```
7059

7160
And the code you need, to fire up all Docker Compose services / Docker Containers is really simple:
7261

7362
```
7463
package de.jonashackt;
7564
76-
import com.palantir.docker.compose.DockerComposeRule;
77-
import com.palantir.docker.compose.connection.waiting.HealthChecks;
7865
import org.apache.http.HttpStatus;
7966
import org.junit.ClassRule;
8067
import org.junit.Test;
8168
import org.junit.runner.RunWith;
8269
import org.springframework.test.context.ContextConfiguration;
8370
import org.springframework.test.context.junit4.SpringRunner;
71+
import org.testcontainers.containers.DockerComposeContainer;
72+
import org.testcontainers.containers.wait.strategy.Wait;
73+
74+
import java.io.File;
8475
8576
import static io.restassured.RestAssured.when;
8677
import static org.hamcrest.Matchers.containsString;
@@ -90,12 +81,12 @@ import static org.hamcrest.Matchers.containsString;
9081
public class ClientTest {
9182
9283
@ClassRule
93-
public static DockerComposeRule docker = DockerComposeRule.builder()
94-
.file("../docker-compose.yml")
95-
.waitingForService("server-alice", HealthChecks.toHaveAllPortsOpen())
96-
.waitingForService("server-tom", HealthChecks.toHaveAllPortsOpen())
97-
.waitingForService("client-bob", HealthChecks.toRespondOverHttp(8080, (port) -> port.inFormat("http://$HOST:$EXTERNAL_PORT/swagger-ui.html")))
98-
.build();
84+
public static DockerComposeContainer services =
85+
new DockerComposeContainer(new File("../docker-compose.yml"))
86+
.withExposedService("server-alice", 8443,Wait.forListeningPort())
87+
.withExposedService("server-tom", 8443, Wait.forListeningPort())
88+
.withExposedService("client-bob", 8080, Wait.forHttp("/swagger-ui.html").forStatusCode(200)).withLocalCompose(true);
89+
9990
10091
@Test
10192
public void is_client_bob_able_to_call_all_servers_with_client_certs() {

client-bob/pom.xml

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
44
<modelVersion>4.0.0</modelVersion>
55

6-
<groupId>de.jonashackt</groupId>
76
<artifactId>client-bob</artifactId>
87
<version>0.0.1-SNAPSHOT</version>
98
<packaging>jar</packaging>

docker-compose.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: '3.3'
1+
version: '3.7'
22

33
services:
44

docker-network-client/pom.xml

+5-15
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
44
<modelVersion>4.0.0</modelVersion>
55

6-
<groupId>de.jonashackt</groupId>
76
<artifactId>docker-network-client</artifactId>
87
<version>0.0.1-SNAPSHOT</version>
98
<packaging>jar</packaging>
@@ -17,8 +16,8 @@
1716
<properties>
1817
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1918
<java.version>1.8</java.version>
20-
<rest-assured.version>3.0.3</rest-assured.version>
21-
<docker-compose-rule-junit4.version>0.33.0</docker-compose-rule-junit4.version>
19+
<rest-assured.version>3.1.1</rest-assured.version>
20+
<testcontainers.version>1.9.1</testcontainers.version>
2221
</properties>
2322

2423
<dependencies>
@@ -42,21 +41,12 @@
4241
</dependency>
4342

4443
<dependency>
45-
<groupId>com.palantir.docker.compose</groupId>
46-
<artifactId>docker-compose-rule-junit4</artifactId>
47-
<version>${docker-compose-rule-junit4.version}</version>
44+
<groupId>org.testcontainers</groupId>
45+
<artifactId>testcontainers</artifactId>
46+
<version>${testcontainers.version}</version>
4847
<scope>test</scope>
4948
</dependency>
5049

5150
</dependencies>
5251

53-
<repositories>
54-
<repository>
55-
<id>bintray</id>
56-
<name>Bintray Maven Repository - as docker-compose-rule-junit4 is only available there</name>
57-
<url>https://dl.bintray.com/palantir/releases</url>
58-
<layout>default</layout>
59-
</repository>
60-
</repositories>
61-
6252
</project>

docker-network-client/src/test/java/de/jonashackt/ClientTest.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package de.jonashackt;
22

3-
import com.palantir.docker.compose.DockerComposeRule;
4-
import com.palantir.docker.compose.connection.waiting.HealthChecks;
53
import org.apache.http.HttpStatus;
64
import org.junit.ClassRule;
75
import org.junit.Test;
86
import org.junit.runner.RunWith;
97
import org.springframework.test.context.ContextConfiguration;
108
import org.springframework.test.context.junit4.SpringRunner;
9+
import org.testcontainers.containers.DockerComposeContainer;
10+
import org.testcontainers.containers.wait.strategy.Wait;
11+
12+
import java.io.File;
1113

1214
import static io.restassured.RestAssured.when;
1315
import static org.hamcrest.Matchers.containsString;
@@ -17,12 +19,11 @@
1719
public class ClientTest {
1820

1921
@ClassRule
20-
public static DockerComposeRule docker = DockerComposeRule.builder()
21-
.file("../docker-compose.yml")
22-
.waitingForService("server-alice", HealthChecks.toHaveAllPortsOpen())
23-
.waitingForService("server-tom", HealthChecks.toHaveAllPortsOpen())
24-
.waitingForService("client-bob", HealthChecks.toRespondOverHttp(8080, (port) -> port.inFormat("http://$HOST:$EXTERNAL_PORT/swagger-ui.html")))
25-
.build();
22+
public static DockerComposeContainer services =
23+
new DockerComposeContainer(new File("../docker-compose.yml"))
24+
.withExposedService("server-alice", 8443,Wait.forListeningPort())
25+
.withExposedService("server-tom", 8443, Wait.forListeningPort())
26+
.withExposedService("client-bob", 8080, Wait.forHttp("/swagger-ui.html").forStatusCode(200)).withLocalCompose(true);
2627

2728
@Test
2829
public void is_client_bob_able_to_call_all_servers_with_client_certs() {

server-alice/pom.xml

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
44
<modelVersion>4.0.0</modelVersion>
55

6-
<groupId>de.jonashackt</groupId>
76
<artifactId>server-alice</artifactId>
87
<version>0.0.1-SNAPSHOT</version>
98
<packaging>jar</packaging>

server-tom/pom.xml

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
44
<modelVersion>4.0.0</modelVersion>
55

6-
<groupId>de.jonashackt</groupId>
76
<artifactId>server-tom</artifactId>
87
<version>0.0.1-SNAPSHOT</version>
98
<packaging>jar</packaging>

0 commit comments

Comments
 (0)