Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,20 @@ within your unit test, based on [WireMock Docker](https://github.com/wiremock/wi

While you can run [WireMock Java](https://github.com/wiremock/wiremock)
with the same result for the most of the use-cases,
it might be helpful to isolate JVMs or to run on
it might be helpful to isolate JVMs or to run on
Java versions and platforms not supported by WireMock.
A common example is using Wiremock 3.x with Java 1.8.

## Compatibility

The module is compatible with the following WireMock versions:

- WireMock (aka WireMock Java) `2.0.0` and above
- WireMock (aka WireMock Java) `3.0.0` beta versions

Other WireMock implementations may work but have not been tested yet.
Please feel free to contribute the integration tests and compatibility layers!

## Usage

### Importing the dependency
Expand Down Expand Up @@ -49,7 +59,7 @@ import static org.assertj.core.api.Assertions.assertThat;
class WireMockContainerJunit5Test {

@Container
WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
WireMockContainer wiremockServer = new WireMockContainer("wiremock/wiremock:2.35.0")
.withMapping("hello", WireMockContainerJunit5Test.class, "hello-world.json");

@Test
Expand Down Expand Up @@ -84,7 +94,7 @@ import static org.assertj.core.api.Assertions.assertThat;
public class WireMockContainerJunit4Test {

@Rule
public WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
public WireMockContainer wiremockServer = new WireMockContainer("wiremock/wiremock:2.35.0")
.withMapping("hello", WireMockContainerJunit4Test.class, "hello-world.json");

@Test
Expand Down Expand Up @@ -191,7 +201,7 @@ import static org.assertj.core.api.Assertions.assertThat;
class WireMockContainerExtensionJunit5Test {

@Container
WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
WireMockContainer wiremockServer = new WireMockContainer("wiremock/wiremock:2.35.0")
.withMapping("json-body-transformer", WireMockContainerExtensionJunit5Test.class, "json-body-transformer.json")
.withExtension("JSON Body Transformer",
Collections.singleton("com.ninecookies.wiremock.extensions.JsonBodyTransformer"),
Expand Down Expand Up @@ -232,7 +242,7 @@ import static org.assertj.core.api.Assertions.assertThat;
public class WireMockContainerExtensionJunit4Test {

@Rule
public WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
public WireMockContainer wiremockServer = new WireMockContainer("wiremock/wiremock:2.35.0")
.withMapping("json-body-transformer", WireMockContainerExtensionJunit4Test.class, "json-body-transformer.json")
.withExtension("JSON Body Transformer",
Collections.singleton("com.ninecookies.wiremock.extensions.JsonBodyTransformer"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.testcontainers.containers.wait.strategy.WaitStrategy;
import org.testcontainers.images.builder.Transferable;
import org.testcontainers.shaded.com.google.common.io.Resources;
import org.testcontainers.utility.ComparableVersion;
import org.testcontainers.utility.DockerImageName;
import org.testcontainers.utility.MountableFile;

import java.io.File;
Expand All @@ -43,8 +45,13 @@
* but other images can be included too at your own risk.
*/
public class WireMockContainer extends GenericContainer<WireMockContainer> {
private static final String DEFAULT_IMAGE_NAME = "wiremock/wiremock";
private static final String DEFAULT_TAG = "latest";

public static final String OFFICIAL_IMAGE_NAME = "wiremock/wiremock";
private static final String WIREMOCK_2_LATEST_TAG = "2.35.0";
/*package*/ static final String WIREMOCK_2_MINIMUM_SUPPORTED_VERSION = "2.0.0";

public static final DockerImageName WIREMOCK_2_LATEST =
DockerImageName.parse(OFFICIAL_IMAGE_NAME).withTag(WIREMOCK_2_LATEST_TAG);

private static final String MAPPINGS_DIR = "/home/wiremock/mappings/";
private static final String FILES_DIR = "/home/wiremock/__files/";
Expand All @@ -61,16 +68,28 @@ public class WireMockContainer extends GenericContainer<WireMockContainer> {
private final Map<String, Extension> extensions = new HashMap<>();
private boolean isBannerDisabled = true;

public WireMockContainer() {
this(DEFAULT_TAG);
/**
* Create image from the specified full image name (repo, image, tag)
*/
public WireMockContainer(String image) {
this(DockerImageName.parse(image));
}

public WireMockContainer(String version) {
this(DEFAULT_IMAGE_NAME, version);
}
public WireMockContainer(DockerImageName dockerImage) {
super(dockerImage);
dockerImage.assertCompatibleWith(new DockerImageName(OFFICIAL_IMAGE_NAME));

// Verify the minimum version for the official image
final ComparableVersion version = new ComparableVersion(dockerImage.getVersionPart());
if (!version.isSemanticVersion()) { // Accept only images when compatibility is declared explicitly
// TODO: We cannot extract compatibleSubstituteFor from Testcontainers API - https://github.com/testcontainers/testcontainers-java/issues/7305
} else {
boolean isLessThanBaseVersion = version.isLessThan(WIREMOCK_2_MINIMUM_SUPPORTED_VERSION);
if (OFFICIAL_IMAGE_NAME.equals(dockerImage.getUnversionedPart()) && isLessThanBaseVersion) {
throw new IllegalArgumentException("For the official image, the WireMock version must be >= " + WIREMOCK_2_MINIMUM_SUPPORTED_VERSION);
}
}

public WireMockContainer(String image, String version) {
super(image + ":" + version);
wireMockArgs = new StringBuilder();
setWaitStrategy(DEFAULT_WAITER);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

class WireMockContainerBannerTest {

WireMockContainer wireMockContainer = new WireMockContainer("2.35.0");
WireMockContainer wireMockContainer = new WireMockContainer(WireMockContainer.WIREMOCK_2_LATEST);
Copy link
Copy Markdown
Contributor

@bitxon bitxon Jul 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe wold be better to use constructor without params in all tests

Copy link
Copy Markdown
Member Author

@oleg-nenashev oleg-nenashev Jul 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will later create a TestConfig class, similar to wiremock/wiremock-docker#67. It will include a static method for tests


@Test
void bannerIsByDefaultDisabled() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class WireMockContainerExtensionTest {
private static final Logger LOGGER = LoggerFactory.getLogger(WireMockContainerExtensionTest.class);

@Container
WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
WireMockContainer wiremockServer = new WireMockContainer(WireMockContainer.WIREMOCK_2_LATEST)
.withLogConsumer(new Slf4jLogConsumer(LOGGER))
.withStartupTimeout(Duration.ofSeconds(60))
.withMapping("json-body-transformer", WireMockContainerExtensionTest.class, "json-body-transformer.json")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class WireMockContainerExtensionsCombinationTest {
private static final Logger LOGGER = LoggerFactory.getLogger(WireMockContainerExtensionsCombinationTest.class);

@Container
WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
WireMockContainer wiremockServer = new WireMockContainer(WireMockContainer.WIREMOCK_2_LATEST)
.withLogConsumer(new Slf4jLogConsumer(LOGGER))
.withMapping("json-body-transformer", WireMockContainerExtensionsCombinationTest.class, "json-body-transformer.json")
.withExtension("Webhook",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class WireMockContainerExtensionsWebhookTest {

TestHttpServer applicationServer = TestHttpServer.newInstance();
@Container
WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
WireMockContainer wiremockServer = new WireMockContainer(WireMockContainer.WIREMOCK_2_LATEST)
.withLogConsumer(new Slf4jLogConsumer(LOGGER))
.withCliArg("--global-response-templating")
.withMapping("webhook-callback-template", WireMockContainerExtensionsWebhookTest.class, "webhook-callback-template.json")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public class WireMockContainerJunit4Test {

@Rule
public WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
public WireMockContainer wiremockServer = new WireMockContainer(WireMockContainer.WIREMOCK_2_LATEST)
.withMapping("hello", WireMockContainerTest.class, "hello-world.json")
.withMapping("hello-resource", WireMockContainerTest.class, "hello-world-resource.json")
.withFileFromResource("hello-world-resource-response.xml", WireMockContainerTest.class, "hello-world-resource-response.xml");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
class WireMockContainerTest {

@Container
WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
WireMockContainer wiremockServer = new WireMockContainer(WireMockContainer.WIREMOCK_2_LATEST)
.withMapping("hello", WireMockContainerTest.class, "hello-world.json")
.withMapping("hello-resource", WireMockContainerTest.class, "hello-world-resource.json")
.withFileFromResource("hello-world-resource-response.xml", WireMockContainerTest.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package org.wiremock.integrations.testcontainers;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.testcontainers.utility.DockerImageName;

import static org.assertj.core.api.Assertions.assertThat;

public class WireMockContainerUnitTest {

@Test
public void shouldInitWithDefault() {
WireMockContainer container = new WireMockContainer(WireMockContainer.WIREMOCK_2_LATEST);
}

@Test
public void shouldInitWithHigherCompatibleVersion() {
WireMockContainer container = new WireMockContainer(
new DockerImageName(WireMockContainer.OFFICIAL_IMAGE_NAME, "2.100.0")
);
}

@Test
public void shouldFailForOlderImage() {
IllegalArgumentException ex = Assertions.assertThrows(IllegalArgumentException.class, () -> {
WireMockContainer container = new WireMockContainer(
new DockerImageName(WireMockContainer.OFFICIAL_IMAGE_NAME, "1.239.0"));
});
assertThat(ex.getMessage())
.as("Wrong exception message")
.contains("For the official image, the WireMock version must be >= " + WireMockContainer.WIREMOCK_2_MINIMUM_SUPPORTED_VERSION);
}

@Test
public void shouldInitWithVersionedTestImagesWithSubstitution() {
// TODO: Should it be accepted by default
WireMockContainer container = new WireMockContainer(
new DockerImageName(WireMockContainer.WIREMOCK_2_LATEST.getUnversionedPart(),
WireMockContainer.WIREMOCK_2_LATEST.getVersionPart()+ "-test")
.asCompatibleSubstituteFor(WireMockContainer.WIREMOCK_2_LATEST));
}

@Test
public void shouldInitWithVersionSubstitution() {
WireMockContainer container = new WireMockContainer(
new DockerImageName(WireMockContainer.OFFICIAL_IMAGE_NAME, "test")
.asCompatibleSubstituteFor(WireMockContainer.WIREMOCK_2_LATEST));
}

@Test
@Disabled("Requires https://github.com/testcontainers/testcontainers-java/issues/7305")
public void shouldFailForUnversionedImage() {
IllegalStateException ex = Assertions.assertThrows(IllegalStateException.class, () -> {
WireMockContainer container = new WireMockContainer(
new DockerImageName(WireMockContainer.OFFICIAL_IMAGE_NAME, "test"));
});
assertThat(ex.getMessage())
.as("Wrong exception message")
.contains("Failed to verify that image")
.contains("is a compatible substitute for '" + WireMockContainer.OFFICIAL_IMAGE_NAME + "'");
}

@Test
public void shouldFailCustomImageWithoutSubstitution() {
IllegalStateException ex = Assertions.assertThrows(IllegalStateException.class, () -> {
WireMockContainer container = new WireMockContainer(
new DockerImageName("mycorp/mywiremockimage", WireMockContainer.WIREMOCK_2_LATEST.getVersionPart()));
});
assertThat(ex.getMessage())
.as("Wrong exception message")
.contains("Failed to verify that image")
.contains("is a compatible substitute for '" + WireMockContainer.OFFICIAL_IMAGE_NAME + "'");
}

}