-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding testcontainer for wiremock (#1449)
Co-authored-by: Volchkov Andrey <[email protected]>
- Loading branch information
Showing
12 changed files
with
360 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
=== embedded-wiremock | ||
|
||
==== Maven dependency | ||
|
||
.pom.xml | ||
[source,xml] | ||
---- | ||
<dependency> | ||
<groupId>com.playtika.testcontainers</groupId> | ||
<artifactId>embedded-wiremock</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
---- | ||
|
||
==== Consumes (via `bootstrap.properties`) | ||
|
||
* `embedded.wiremock.enabled` `(true|false, default is true)` | ||
* `embedded.wiremock.reuseContainer` `(true|false, default is false)` | ||
* `embedded.wiremock.dockerImage` `(default is 'wiremock/wiremock:2.32.0')` | ||
* `embedded.wiremock.host` `(default is 'localhost')` | ||
* `embedded.wiremock.port` `(int, default is 8990)` | ||
|
||
|
||
==== Produces | ||
|
||
* `embedded.wiremock.host` | ||
* `embedded.wiremock.port` (mapped HTTP port) | ||
* `embedded.wiremock.networkAlias` | ||
* `embedded.wiremock.internalPort` | ||
* Bean `GenericContainer<?> embeddedWiremock` | ||
|
||
|
||
==== Example | ||
|
||
Add wiremock dependency with test scope (to use wiremock client to configure stubs, etc.): | ||
|
||
.pom.xml | ||
[source,xml] | ||
---- | ||
<dependency> | ||
<groupId>com.github.tomakehurst</groupId> | ||
<artifactId>wiremock</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
---- | ||
|
||
Set up and use the wiremock: | ||
|
||
[source,java] | ||
---- | ||
class SomeTest { | ||
//... | ||
@Value("${embedded.wiremock.host}") | ||
String wiremockHost; | ||
@Value("${embedded.wiremock.port}") | ||
int wiremockPort; | ||
@BeforeEach | ||
void setUp() { | ||
WireMock.configureFor(wiremockHost, wiremockPort); | ||
} | ||
@Test | ||
void doTest() { | ||
// configure stub | ||
stubFor(get("/say-hello") | ||
.willReturn(ok("Hello world!"))); | ||
// test something | ||
} | ||
//... | ||
} | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<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"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<artifactId>testcontainers-spring-boot-parent</artifactId> | ||
<groupId>com.playtika.testcontainers</groupId> | ||
<version>3.0.0-RC9</version> | ||
<relativePath>../testcontainers-spring-boot-parent</relativePath> | ||
</parent> | ||
|
||
<artifactId>embedded-wiremock</artifactId> | ||
|
||
<properties> | ||
<wiremock.version>2.27.2</wiremock.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.playtika.testcontainers</groupId> | ||
<artifactId>testcontainers-common</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.github.tomakehurst</groupId> | ||
<artifactId>wiremock</artifactId> | ||
<version>${wiremock.version}</version> | ||
<scope>test</scope> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>commons-logging</groupId> | ||
<artifactId>commons-logging</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>io.rest-assured</groupId> | ||
<artifactId>rest-assured</artifactId> | ||
<scope>test</scope> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>commons-logging</groupId> | ||
<artifactId>commons-logging</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
</dependencies> | ||
</project> |
69 changes: 69 additions & 0 deletions
69
...ain/java/com/playtika/testcontainers/wiremock/EmbeddedWiremockBootstrapConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.playtika.testcontainers.wiremock; | ||
|
||
import com.playtika.testcontainer.common.spring.DockerPresenceBootstrapConfiguration; | ||
import com.playtika.testcontainer.common.utils.ContainerUtils; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.boot.autoconfigure.AutoConfigureAfter; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.annotation.Order; | ||
import org.springframework.core.env.ConfigurableEnvironment; | ||
import org.springframework.core.env.MapPropertySource; | ||
import org.testcontainers.containers.GenericContainer; | ||
import org.testcontainers.containers.wait.strategy.Wait; | ||
import org.testcontainers.containers.wait.strategy.WaitStrategy; | ||
|
||
import java.util.LinkedHashMap; | ||
|
||
import static com.playtika.testcontainer.common.utils.ContainerUtils.configureCommonsAndStart; | ||
import static org.springframework.core.Ordered.HIGHEST_PRECEDENCE; | ||
|
||
@Slf4j | ||
@Order(HIGHEST_PRECEDENCE) | ||
@Configuration | ||
@ConditionalOnExpression("${embedded.containers.enabled:true}") | ||
@AutoConfigureAfter(DockerPresenceBootstrapConfiguration.class) | ||
@ConditionalOnProperty(name = "embedded.wiremock.enabled", havingValue = "true", matchIfMissing = true) | ||
@EnableConfigurationProperties(WiremockProperties.class) | ||
public class EmbeddedWiremockBootstrapConfiguration { | ||
|
||
static final String BEAN_NAME_EMBEDDED_WIREMOCK = "embeddedWiremock"; | ||
private static final String WIREMOCK_NETWORK_ALIAS = "wiremock.testcontainer.docker"; | ||
private static final WaitStrategy DEFAULT_WAITER = Wait.forHttp("/__admin/mappings") | ||
.withMethod("GET") | ||
.forStatusCode(200); | ||
|
||
@Bean(value = BEAN_NAME_EMBEDDED_WIREMOCK, destroyMethod = "stop") | ||
public GenericContainer<?> wiremockContainer(ConfigurableEnvironment environment, | ||
WiremockProperties properties) { | ||
GenericContainer<?> wiremock = | ||
new GenericContainer<>(ContainerUtils.getDockerImageName(properties)) | ||
.waitingFor(DEFAULT_WAITER) | ||
.withCommand("--port " + properties.getPort()) | ||
.withExposedPorts(properties.getPort()) | ||
.withNetworkAliases(WIREMOCK_NETWORK_ALIAS); | ||
|
||
wiremock = configureCommonsAndStart(wiremock, properties, log); | ||
registerWiremockEnvironment(wiremock, environment, properties); | ||
return wiremock; | ||
} | ||
|
||
private void registerWiremockEnvironment(GenericContainer<?> container, ConfigurableEnvironment environment, WiremockProperties properties) { | ||
Integer mappedPort = container.getMappedPort(properties.getPort()); | ||
String host = container.getHost(); | ||
|
||
LinkedHashMap<String, Object> map = new LinkedHashMap<>(); | ||
map.put("embedded.wiremock.port", mappedPort); | ||
map.put("embedded.wiremock.host", host); | ||
map.put("embedded.wiremock.networkAlias", WIREMOCK_NETWORK_ALIAS); | ||
map.put("embedded.wiremock.internalPort", properties.getPort()); | ||
|
||
log.info("Started wiremock. Connection Details: {}", map); | ||
|
||
MapPropertySource propertySource = new MapPropertySource("embeddedWiremockInfo", map); | ||
environment.getPropertySources().addFirst(propertySource); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
embedded-wiremock/src/main/java/com/playtika/testcontainers/wiremock/WiremockProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.playtika.testcontainers.wiremock; | ||
|
||
import com.playtika.testcontainer.common.properties.CommonContainerProperties; | ||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
@Data | ||
@EqualsAndHashCode(callSuper = true) | ||
@ConfigurationProperties("embedded.wiremock") | ||
public class WiremockProperties extends CommonContainerProperties { | ||
|
||
private String host = "localhost"; | ||
private int port = 8990; | ||
|
||
// https://hub.docker.com/r/wiremock/wiremock | ||
@Override | ||
public String getDefaultDockerImage() { | ||
// Please don`t remove this comment. | ||
// renovate: datasource=docker | ||
return "wiremock/wiremock:2.32.0"; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
embedded-wiremock/src/main/resources/META-INF/additional-spring-configuration-metadata.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"properties": [ | ||
{ | ||
"name": "embedded.wiremock.enabled", | ||
"type": "java.lang.String", | ||
"defaultValue": "true" | ||
}, | ||
{ | ||
"name": "embedded.wiremock.dockerImage", | ||
"type": "java.lang.String", | ||
"defaultValue": "wiremock/wiremock:2.32.0" | ||
}, | ||
{ | ||
"name": "embedded.wiremock.host", | ||
"type": "java.lang.String", | ||
"defaultValue": "localhost" | ||
}, | ||
{ | ||
"name": "embedded.wiremock.port", | ||
"type": "java.lang.Integer", | ||
"description": "The container internal port. Will be overwritten with mapped port.", | ||
"defaultValue": "8990" | ||
} | ||
] | ||
} |
2 changes: 2 additions & 0 deletions
2
embedded-wiremock/src/main/resources/META-INF/spring.factories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
org.springframework.cloud.bootstrap.BootstrapConfiguration=\ | ||
com.playtika.testcontainers.wiremock.EmbeddedWiremockBootstrapConfiguration |
1 change: 1 addition & 0 deletions
1
...esources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
com.playtika.testcontainers.wiremock.EmbeddedWiremockBootstrapConfiguration |
37 changes: 37 additions & 0 deletions
37
...dded-wiremock/src/test/java/com/playtika/testcontainers/wiremock/DisableWiremockTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.playtika.testcontainers.wiremock; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.autoconfigure.AutoConfigurations; | ||
import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||
import org.testcontainers.containers.Container; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class DisableWiremockTest { | ||
|
||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() | ||
.withConfiguration(AutoConfigurations.of(EmbeddedWiremockBootstrapConfiguration.class)); | ||
|
||
@Test | ||
public void shouldSkipEmbeddedWiremockBootstrapConfiguration() { | ||
contextRunner | ||
.withPropertyValues( | ||
"embedded.wiremock.enabled=false" | ||
) | ||
.run((context) -> assertThat(context) | ||
.hasNotFailed() | ||
.doesNotHaveBean(Container.class)); | ||
} | ||
|
||
@Test | ||
public void shouldSkipEmbeddedWiremockBootstrapConfigurationWhenContainersDisabled() { | ||
contextRunner | ||
.withPropertyValues( | ||
"embedded.containers.enabled=false" | ||
) | ||
.run((context) -> assertThat(context) | ||
.hasNotFailed() | ||
.doesNotHaveBean(Container.class)); | ||
} | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
...java/com/playtika/testcontainers/wiremock/EmbeddedWiremockBootstrapConfigurationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.playtika.testcontainers.wiremock; | ||
|
||
import com.github.tomakehurst.wiremock.client.WireMock; | ||
import io.restassured.RestAssured; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.env.ConfigurableEnvironment; | ||
|
||
import static com.github.tomakehurst.wiremock.client.WireMock.get; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.ok; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; | ||
import static io.restassured.RestAssured.given; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
@Slf4j | ||
@SpringBootTest( | ||
classes = EmbeddedWiremockBootstrapConfigurationTest.TestConfiguration.class, | ||
properties = { | ||
"embedded.wiremock.enabled=true" | ||
} | ||
) | ||
public class EmbeddedWiremockBootstrapConfigurationTest { | ||
|
||
@Autowired | ||
ConfigurableEnvironment environment; | ||
|
||
@Value("${embedded.wiremock.host}") | ||
String wiremockHost; | ||
|
||
@Value("${embedded.wiremock.port}") | ||
int wiremockPort; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
WireMock.configureFor(wiremockHost, wiremockPort); | ||
RestAssured.port = wiremockPort; | ||
} | ||
|
||
@Test | ||
void shouldRequestWiremockStub() { | ||
stubFor(get("/say-hello") | ||
.willReturn(ok("Hello world!"))); | ||
|
||
given() | ||
.get("/say-hello") | ||
.then() | ||
.assertThat() | ||
.log().all() | ||
.statusCode(200) | ||
.body(equalTo("Hello world!")); | ||
} | ||
|
||
@Test | ||
public void propertiesAreAvailable() { | ||
assertThat(environment.getProperty("embedded.wiremock.port")).isNotEmpty(); | ||
assertThat(environment.getProperty("embedded.wiremock.host")).isNotEmpty(); | ||
assertThat(environment.getProperty("embedded.wiremock.networkAlias")).isNotEmpty(); | ||
assertThat(environment.getProperty("embedded.wiremock.internalPort")).isNotEmpty(); | ||
} | ||
|
||
@EnableAutoConfiguration | ||
@Configuration | ||
static class TestConfiguration { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters