Skip to content

Commit 8987c8a

Browse files
wolfchkovVolchkov Andrey
and
Volchkov Andrey
authored
adding testcontainer for wiremock (#1449)
Co-authored-by: Volchkov Andrey <[email protected]>
1 parent 74d2ba4 commit 8987c8a

File tree

12 files changed

+360
-0
lines changed

12 files changed

+360
-0
lines changed

README.adoc

+2
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,8 @@ embedded:
333333
=== link:embedded-cockroachdb/README.adoc[embedded-cockroachdb]
334334
=== link:embedded-git/README.adoc[embedded-git]
335335

336+
=== link:embedded-wiremock/README.adoc[embedded-wiremock]
337+
336338

337339
== How to contribute
338340

embedded-wiremock/README.adoc

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
=== embedded-wiremock
2+
3+
==== Maven dependency
4+
5+
.pom.xml
6+
[source,xml]
7+
----
8+
<dependency>
9+
<groupId>com.playtika.testcontainers</groupId>
10+
<artifactId>embedded-wiremock</artifactId>
11+
<scope>test</scope>
12+
</dependency>
13+
----
14+
15+
==== Consumes (via `bootstrap.properties`)
16+
17+
* `embedded.wiremock.enabled` `(true|false, default is true)`
18+
* `embedded.wiremock.reuseContainer` `(true|false, default is false)`
19+
* `embedded.wiremock.dockerImage` `(default is 'wiremock/wiremock:2.32.0')`
20+
* `embedded.wiremock.host` `(default is 'localhost')`
21+
* `embedded.wiremock.port` `(int, default is 8990)`
22+
23+
24+
==== Produces
25+
26+
* `embedded.wiremock.host`
27+
* `embedded.wiremock.port` (mapped HTTP port)
28+
* `embedded.wiremock.networkAlias`
29+
* `embedded.wiremock.internalPort`
30+
* Bean `GenericContainer<?> embeddedWiremock`
31+
32+
33+
==== Example
34+
35+
Add wiremock dependency with test scope (to use wiremock client to configure stubs, etc.):
36+
37+
.pom.xml
38+
[source,xml]
39+
----
40+
<dependency>
41+
<groupId>com.github.tomakehurst</groupId>
42+
<artifactId>wiremock</artifactId>
43+
<scope>test</scope>
44+
</dependency>
45+
----
46+
47+
Set up and use the wiremock:
48+
49+
[source,java]
50+
----
51+
class SomeTest {
52+
//...
53+
@Value("${embedded.wiremock.host}")
54+
String wiremockHost;
55+
56+
@Value("${embedded.wiremock.port}")
57+
int wiremockPort;
58+
59+
@BeforeEach
60+
void setUp() {
61+
WireMock.configureFor(wiremockHost, wiremockPort);
62+
}
63+
64+
@Test
65+
void doTest() {
66+
// configure stub
67+
stubFor(get("/say-hello")
68+
.willReturn(ok("Hello world!")));
69+
// test something
70+
}
71+
//...
72+
}
73+
----

embedded-wiremock/pom.xml

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<artifactId>testcontainers-spring-boot-parent</artifactId>
8+
<groupId>com.playtika.testcontainers</groupId>
9+
<version>3.0.0-RC9</version>
10+
<relativePath>../testcontainers-spring-boot-parent</relativePath>
11+
</parent>
12+
13+
<artifactId>embedded-wiremock</artifactId>
14+
15+
<properties>
16+
<wiremock.version>2.27.2</wiremock.version>
17+
</properties>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>com.playtika.testcontainers</groupId>
22+
<artifactId>testcontainers-common</artifactId>
23+
</dependency>
24+
25+
<dependency>
26+
<groupId>com.github.tomakehurst</groupId>
27+
<artifactId>wiremock</artifactId>
28+
<version>${wiremock.version}</version>
29+
<scope>test</scope>
30+
<exclusions>
31+
<exclusion>
32+
<groupId>commons-logging</groupId>
33+
<artifactId>commons-logging</artifactId>
34+
</exclusion>
35+
</exclusions>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>io.rest-assured</groupId>
40+
<artifactId>rest-assured</artifactId>
41+
<scope>test</scope>
42+
<exclusions>
43+
<exclusion>
44+
<groupId>commons-logging</groupId>
45+
<artifactId>commons-logging</artifactId>
46+
</exclusion>
47+
</exclusions>
48+
</dependency>
49+
</dependencies>
50+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.playtika.testcontainers.wiremock;
2+
3+
import com.playtika.testcontainer.common.spring.DockerPresenceBootstrapConfiguration;
4+
import com.playtika.testcontainer.common.utils.ContainerUtils;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
7+
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
8+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
9+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
10+
import org.springframework.context.annotation.Bean;
11+
import org.springframework.context.annotation.Configuration;
12+
import org.springframework.core.annotation.Order;
13+
import org.springframework.core.env.ConfigurableEnvironment;
14+
import org.springframework.core.env.MapPropertySource;
15+
import org.testcontainers.containers.GenericContainer;
16+
import org.testcontainers.containers.wait.strategy.Wait;
17+
import org.testcontainers.containers.wait.strategy.WaitStrategy;
18+
19+
import java.util.LinkedHashMap;
20+
21+
import static com.playtika.testcontainer.common.utils.ContainerUtils.configureCommonsAndStart;
22+
import static org.springframework.core.Ordered.HIGHEST_PRECEDENCE;
23+
24+
@Slf4j
25+
@Order(HIGHEST_PRECEDENCE)
26+
@Configuration
27+
@ConditionalOnExpression("${embedded.containers.enabled:true}")
28+
@AutoConfigureAfter(DockerPresenceBootstrapConfiguration.class)
29+
@ConditionalOnProperty(name = "embedded.wiremock.enabled", havingValue = "true", matchIfMissing = true)
30+
@EnableConfigurationProperties(WiremockProperties.class)
31+
public class EmbeddedWiremockBootstrapConfiguration {
32+
33+
static final String BEAN_NAME_EMBEDDED_WIREMOCK = "embeddedWiremock";
34+
private static final String WIREMOCK_NETWORK_ALIAS = "wiremock.testcontainer.docker";
35+
private static final WaitStrategy DEFAULT_WAITER = Wait.forHttp("/__admin/mappings")
36+
.withMethod("GET")
37+
.forStatusCode(200);
38+
39+
@Bean(value = BEAN_NAME_EMBEDDED_WIREMOCK, destroyMethod = "stop")
40+
public GenericContainer<?> wiremockContainer(ConfigurableEnvironment environment,
41+
WiremockProperties properties) {
42+
GenericContainer<?> wiremock =
43+
new GenericContainer<>(ContainerUtils.getDockerImageName(properties))
44+
.waitingFor(DEFAULT_WAITER)
45+
.withCommand("--port " + properties.getPort())
46+
.withExposedPorts(properties.getPort())
47+
.withNetworkAliases(WIREMOCK_NETWORK_ALIAS);
48+
49+
wiremock = configureCommonsAndStart(wiremock, properties, log);
50+
registerWiremockEnvironment(wiremock, environment, properties);
51+
return wiremock;
52+
}
53+
54+
private void registerWiremockEnvironment(GenericContainer<?> container, ConfigurableEnvironment environment, WiremockProperties properties) {
55+
Integer mappedPort = container.getMappedPort(properties.getPort());
56+
String host = container.getHost();
57+
58+
LinkedHashMap<String, Object> map = new LinkedHashMap<>();
59+
map.put("embedded.wiremock.port", mappedPort);
60+
map.put("embedded.wiremock.host", host);
61+
map.put("embedded.wiremock.networkAlias", WIREMOCK_NETWORK_ALIAS);
62+
map.put("embedded.wiremock.internalPort", properties.getPort());
63+
64+
log.info("Started wiremock. Connection Details: {}", map);
65+
66+
MapPropertySource propertySource = new MapPropertySource("embeddedWiremockInfo", map);
67+
environment.getPropertySources().addFirst(propertySource);
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.playtika.testcontainers.wiremock;
2+
3+
import com.playtika.testcontainer.common.properties.CommonContainerProperties;
4+
import lombok.Data;
5+
import lombok.EqualsAndHashCode;
6+
import org.springframework.boot.context.properties.ConfigurationProperties;
7+
8+
@Data
9+
@EqualsAndHashCode(callSuper = true)
10+
@ConfigurationProperties("embedded.wiremock")
11+
public class WiremockProperties extends CommonContainerProperties {
12+
13+
private String host = "localhost";
14+
private int port = 8990;
15+
16+
// https://hub.docker.com/r/wiremock/wiremock
17+
@Override
18+
public String getDefaultDockerImage() {
19+
// Please don`t remove this comment.
20+
// renovate: datasource=docker
21+
return "wiremock/wiremock:2.32.0";
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"properties": [
3+
{
4+
"name": "embedded.wiremock.enabled",
5+
"type": "java.lang.String",
6+
"defaultValue": "true"
7+
},
8+
{
9+
"name": "embedded.wiremock.dockerImage",
10+
"type": "java.lang.String",
11+
"defaultValue": "wiremock/wiremock:2.32.0"
12+
},
13+
{
14+
"name": "embedded.wiremock.host",
15+
"type": "java.lang.String",
16+
"defaultValue": "localhost"
17+
},
18+
{
19+
"name": "embedded.wiremock.port",
20+
"type": "java.lang.Integer",
21+
"description": "The container internal port. Will be overwritten with mapped port.",
22+
"defaultValue": "8990"
23+
}
24+
]
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
2+
com.playtika.testcontainers.wiremock.EmbeddedWiremockBootstrapConfiguration
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.playtika.testcontainers.wiremock.EmbeddedWiremockBootstrapConfiguration
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.playtika.testcontainers.wiremock;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.boot.autoconfigure.AutoConfigurations;
5+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
6+
import org.testcontainers.containers.Container;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
10+
public class DisableWiremockTest {
11+
12+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
13+
.withConfiguration(AutoConfigurations.of(EmbeddedWiremockBootstrapConfiguration.class));
14+
15+
@Test
16+
public void shouldSkipEmbeddedWiremockBootstrapConfiguration() {
17+
contextRunner
18+
.withPropertyValues(
19+
"embedded.wiremock.enabled=false"
20+
)
21+
.run((context) -> assertThat(context)
22+
.hasNotFailed()
23+
.doesNotHaveBean(Container.class));
24+
}
25+
26+
@Test
27+
public void shouldSkipEmbeddedWiremockBootstrapConfigurationWhenContainersDisabled() {
28+
contextRunner
29+
.withPropertyValues(
30+
"embedded.containers.enabled=false"
31+
)
32+
.run((context) -> assertThat(context)
33+
.hasNotFailed()
34+
.doesNotHaveBean(Container.class));
35+
}
36+
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.playtika.testcontainers.wiremock;
2+
3+
import com.github.tomakehurst.wiremock.client.WireMock;
4+
import io.restassured.RestAssured;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.beans.factory.annotation.Value;
10+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
11+
import org.springframework.boot.test.context.SpringBootTest;
12+
import org.springframework.context.annotation.Configuration;
13+
import org.springframework.core.env.ConfigurableEnvironment;
14+
15+
import static com.github.tomakehurst.wiremock.client.WireMock.get;
16+
import static com.github.tomakehurst.wiremock.client.WireMock.ok;
17+
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
18+
import static io.restassured.RestAssured.given;
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.hamcrest.Matchers.equalTo;
21+
22+
@Slf4j
23+
@SpringBootTest(
24+
classes = EmbeddedWiremockBootstrapConfigurationTest.TestConfiguration.class,
25+
properties = {
26+
"embedded.wiremock.enabled=true"
27+
}
28+
)
29+
public class EmbeddedWiremockBootstrapConfigurationTest {
30+
31+
@Autowired
32+
ConfigurableEnvironment environment;
33+
34+
@Value("${embedded.wiremock.host}")
35+
String wiremockHost;
36+
37+
@Value("${embedded.wiremock.port}")
38+
int wiremockPort;
39+
40+
@BeforeEach
41+
void setUp() {
42+
WireMock.configureFor(wiremockHost, wiremockPort);
43+
RestAssured.port = wiremockPort;
44+
}
45+
46+
@Test
47+
void shouldRequestWiremockStub() {
48+
stubFor(get("/say-hello")
49+
.willReturn(ok("Hello world!")));
50+
51+
given()
52+
.get("/say-hello")
53+
.then()
54+
.assertThat()
55+
.log().all()
56+
.statusCode(200)
57+
.body(equalTo("Hello world!"));
58+
}
59+
60+
@Test
61+
public void propertiesAreAvailable() {
62+
assertThat(environment.getProperty("embedded.wiremock.port")).isNotEmpty();
63+
assertThat(environment.getProperty("embedded.wiremock.host")).isNotEmpty();
64+
assertThat(environment.getProperty("embedded.wiremock.networkAlias")).isNotEmpty();
65+
assertThat(environment.getProperty("embedded.wiremock.internalPort")).isNotEmpty();
66+
}
67+
68+
@EnableAutoConfiguration
69+
@Configuration
70+
static class TestConfiguration {
71+
}
72+
}

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
<module>embedded-git</module>
8181
<module>embedded-zookeeper</module>
8282
<module>embedded-couchbase</module>
83+
<module>embedded-wiremock</module>
8384
</modules>
8485

8586
<properties>

testcontainers-spring-boot-bom/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,11 @@
220220
<artifactId>embedded-git</artifactId>
221221
<version>${project.version}</version>
222222
</dependency>
223+
<dependency>
224+
<groupId>com.playtika.testcontainers</groupId>
225+
<artifactId>embedded-wiremock</artifactId>
226+
<version>${project.version}</version>
227+
</dependency>
223228
</dependencies>
224229
</dependencyManagement>
225230
</project>

0 commit comments

Comments
 (0)