Skip to content

Commit

Permalink
adding testcontainer for wiremock (#1449)
Browse files Browse the repository at this point in the history
Co-authored-by: Volchkov Andrey <[email protected]>
  • Loading branch information
wolfchkov and Volchkov Andrey authored Aug 31, 2023
1 parent 74d2ba4 commit 8987c8a
Show file tree
Hide file tree
Showing 12 changed files with 360 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@ embedded:
=== link:embedded-cockroachdb/README.adoc[embedded-cockroachdb]
=== link:embedded-git/README.adoc[embedded-git]

=== link:embedded-wiremock/README.adoc[embedded-wiremock]


== How to contribute

Expand Down
73 changes: 73 additions & 0 deletions embedded-wiremock/README.adoc
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
}
//...
}
----
50 changes: 50 additions & 0 deletions embedded-wiremock/pom.xml
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>
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);
}
}
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";
}
}
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"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
com.playtika.testcontainers.wiremock.EmbeddedWiremockBootstrapConfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.playtika.testcontainers.wiremock.EmbeddedWiremockBootstrapConfiguration
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));
}

}
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 {
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
<module>embedded-git</module>
<module>embedded-zookeeper</module>
<module>embedded-couchbase</module>
<module>embedded-wiremock</module>
</modules>

<properties>
Expand Down
5 changes: 5 additions & 0 deletions testcontainers-spring-boot-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,11 @@
<artifactId>embedded-git</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.playtika.testcontainers</groupId>
<artifactId>embedded-wiremock</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>

0 comments on commit 8987c8a

Please sign in to comment.