Skip to content

Commit 188fefe

Browse files
Merge pull request #58 from wiremock/constructors
Align WireMockContainer constructor with the Testcontainers certification requirements
2 parents 4451f38 + 3132211 commit 188fefe

File tree

9 files changed

+125
-20
lines changed

9 files changed

+125
-20
lines changed

README.md

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,20 @@ within your unit test, based on [WireMock Docker](https://github.com/wiremock/wi
1515

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

22+
## Compatibility
23+
24+
The module is compatible with the following WireMock versions:
25+
26+
- WireMock (aka WireMock Java) `2.0.0` and above
27+
- WireMock (aka WireMock Java) `3.0.0` beta versions
28+
29+
Other WireMock implementations may work but have not been tested yet.
30+
Please feel free to contribute the integration tests and compatibility layers!
31+
2232
## Usage
2333

2434
### Importing the dependency
@@ -109,7 +119,7 @@ import static org.assertj.core.api.Assertions.assertThat;
109119
class WireMockContainerJunit5Test {
110120

111121
@Container
112-
WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
122+
WireMockContainer wiremockServer = new WireMockContainer("wiremock/wiremock:2.35.0")
113123
.withMapping("hello", WireMockContainerJunit5Test.class, "hello-world.json");
114124

115125
@Test
@@ -144,7 +154,7 @@ import static org.assertj.core.api.Assertions.assertThat;
144154
public class WireMockContainerJunit4Test {
145155

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

150160
@Test
@@ -251,7 +261,7 @@ import static org.assertj.core.api.Assertions.assertThat;
251261
class WireMockContainerExtensionJunit5Test {
252262

253263
@Container
254-
WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
264+
WireMockContainer wiremockServer = new WireMockContainer("wiremock/wiremock:2.35.0")
255265
.withMapping("json-body-transformer", WireMockContainerExtensionJunit5Test.class, "json-body-transformer.json")
256266
.withExtension("JSON Body Transformer",
257267
Collections.singleton("com.ninecookies.wiremock.extensions.JsonBodyTransformer"),
@@ -292,7 +302,7 @@ import static org.assertj.core.api.Assertions.assertThat;
292302
public class WireMockContainerExtensionJunit4Test {
293303

294304
@Rule
295-
public WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
305+
public WireMockContainer wiremockServer = new WireMockContainer("wiremock/wiremock:2.35.0")
296306
.withMapping("json-body-transformer", WireMockContainerExtensionJunit4Test.class, "json-body-transformer.json")
297307
.withExtension("JSON Body Transformer",
298308
Collections.singleton("com.ninecookies.wiremock.extensions.JsonBodyTransformer"),

src/main/java/org/wiremock/integrations/testcontainers/WireMockContainer.java

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import org.testcontainers.containers.wait.strategy.WaitStrategy;
2121
import org.testcontainers.images.builder.Transferable;
2222
import org.testcontainers.shaded.com.google.common.io.Resources;
23+
import org.testcontainers.utility.ComparableVersion;
24+
import org.testcontainers.utility.DockerImageName;
2325
import org.testcontainers.utility.MountableFile;
2426

2527
import java.io.File;
@@ -43,8 +45,13 @@
4345
* but other images can be included too at your own risk.
4446
*/
4547
public class WireMockContainer extends GenericContainer<WireMockContainer> {
46-
private static final String DEFAULT_IMAGE_NAME = "wiremock/wiremock";
47-
private static final String DEFAULT_TAG = "latest";
48+
49+
public static final String OFFICIAL_IMAGE_NAME = "wiremock/wiremock";
50+
private static final String WIREMOCK_2_LATEST_TAG = "2.35.0";
51+
/*package*/ static final String WIREMOCK_2_MINIMUM_SUPPORTED_VERSION = "2.0.0";
52+
53+
public static final DockerImageName WIREMOCK_2_LATEST =
54+
DockerImageName.parse(OFFICIAL_IMAGE_NAME).withTag(WIREMOCK_2_LATEST_TAG);
4855

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

64-
public WireMockContainer() {
65-
this(DEFAULT_TAG);
71+
/**
72+
* Create image from the specified full image name (repo, image, tag)
73+
*/
74+
public WireMockContainer(String image) {
75+
this(DockerImageName.parse(image));
6676
}
6777

68-
public WireMockContainer(String version) {
69-
this(DEFAULT_IMAGE_NAME, version);
70-
}
78+
public WireMockContainer(DockerImageName dockerImage) {
79+
super(dockerImage);
80+
dockerImage.assertCompatibleWith(new DockerImageName(OFFICIAL_IMAGE_NAME));
81+
82+
// Verify the minimum version for the official image
83+
final ComparableVersion version = new ComparableVersion(dockerImage.getVersionPart());
84+
if (!version.isSemanticVersion()) { // Accept only images when compatibility is declared explicitly
85+
// TODO: We cannot extract compatibleSubstituteFor from Testcontainers API - https://github.com/testcontainers/testcontainers-java/issues/7305
86+
} else {
87+
boolean isLessThanBaseVersion = version.isLessThan(WIREMOCK_2_MINIMUM_SUPPORTED_VERSION);
88+
if (OFFICIAL_IMAGE_NAME.equals(dockerImage.getUnversionedPart()) && isLessThanBaseVersion) {
89+
throw new IllegalArgumentException("For the official image, the WireMock version must be >= " + WIREMOCK_2_MINIMUM_SUPPORTED_VERSION);
90+
}
91+
}
7192

72-
public WireMockContainer(String image, String version) {
73-
super(image + ":" + version);
7493
wireMockArgs = new StringBuilder();
7594
setWaitStrategy(DEFAULT_WAITER);
7695
}

src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerBannerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
class WireMockContainerBannerTest {
2323

24-
WireMockContainer wireMockContainer = new WireMockContainer("2.35.0");
24+
WireMockContainer wireMockContainer = new WireMockContainer(WireMockContainer.WIREMOCK_2_LATEST);
2525

2626
@Test
2727
void bannerIsByDefaultDisabled() {

src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerExtensionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class WireMockContainerExtensionTest {
4040
private static final Logger LOGGER = LoggerFactory.getLogger(WireMockContainerExtensionTest.class);
4141

4242
@Container
43-
WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
43+
WireMockContainer wiremockServer = new WireMockContainer(WireMockContainer.WIREMOCK_2_LATEST)
4444
.withLogConsumer(new Slf4jLogConsumer(LOGGER))
4545
.withStartupTimeout(Duration.ofSeconds(60))
4646
.withMapping("json-body-transformer", WireMockContainerExtensionTest.class, "json-body-transformer.json")

src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerExtensionsCombinationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class WireMockContainerExtensionsCombinationTest {
3939
private static final Logger LOGGER = LoggerFactory.getLogger(WireMockContainerExtensionsCombinationTest.class);
4040

4141
@Container
42-
WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
42+
WireMockContainer wiremockServer = new WireMockContainer(WireMockContainer.WIREMOCK_2_LATEST)
4343
.withLogConsumer(new Slf4jLogConsumer(LOGGER))
4444
.withMapping("json-body-transformer", WireMockContainerExtensionsCombinationTest.class, "json-body-transformer.json")
4545
.withExtension("Webhook",

src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerExtensionsWebhookTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class WireMockContainerExtensionsWebhookTest {
5656

5757
TestHttpServer applicationServer = TestHttpServer.newInstance();
5858
@Container
59-
WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
59+
WireMockContainer wiremockServer = new WireMockContainer(WireMockContainer.WIREMOCK_2_LATEST)
6060
.withLogConsumer(new Slf4jLogConsumer(LOGGER))
6161
.withCliArg("--global-response-templating")
6262
.withMapping("webhook-callback-template", WireMockContainerExtensionsWebhookTest.class, "webhook-callback-template.json")

src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerJunit4Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
public class WireMockContainerJunit4Test {
2626

2727
@Rule
28-
public WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
28+
public WireMockContainer wiremockServer = new WireMockContainer(WireMockContainer.WIREMOCK_2_LATEST)
2929
.withMapping("hello", WireMockContainerTest.class, "hello-world.json")
3030
.withMapping("hello-resource", WireMockContainerTest.class, "hello-world-resource.json")
3131
.withFileFromResource("hello-world-resource-response.xml", WireMockContainerTest.class, "hello-world-resource-response.xml");

src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
class WireMockContainerTest {
3030

3131
@Container
32-
WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
32+
WireMockContainer wiremockServer = new WireMockContainer(WireMockContainer.WIREMOCK_2_LATEST)
3333
.withMapping("hello", WireMockContainerTest.class, "hello-world.json")
3434
.withMapping("hello-resource", WireMockContainerTest.class, "hello-world-resource.json")
3535
.withFileFromResource("hello-world-resource-response.xml", WireMockContainerTest.class,
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package org.wiremock.integrations.testcontainers;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Disabled;
5+
import org.junit.jupiter.api.Test;
6+
import org.testcontainers.utility.DockerImageName;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
10+
public class WireMockContainerUnitTest {
11+
12+
@Test
13+
public void shouldInitWithDefault() {
14+
WireMockContainer container = new WireMockContainer(WireMockContainer.WIREMOCK_2_LATEST);
15+
}
16+
17+
@Test
18+
public void shouldInitWithHigherCompatibleVersion() {
19+
WireMockContainer container = new WireMockContainer(
20+
new DockerImageName(WireMockContainer.OFFICIAL_IMAGE_NAME, "2.100.0")
21+
);
22+
}
23+
24+
@Test
25+
public void shouldFailForOlderImage() {
26+
IllegalArgumentException ex = Assertions.assertThrows(IllegalArgumentException.class, () -> {
27+
WireMockContainer container = new WireMockContainer(
28+
new DockerImageName(WireMockContainer.OFFICIAL_IMAGE_NAME, "1.239.0"));
29+
});
30+
assertThat(ex.getMessage())
31+
.as("Wrong exception message")
32+
.contains("For the official image, the WireMock version must be >= " + WireMockContainer.WIREMOCK_2_MINIMUM_SUPPORTED_VERSION);
33+
}
34+
35+
@Test
36+
public void shouldInitWithVersionedTestImagesWithSubstitution() {
37+
// TODO: Should it be accepted by default
38+
WireMockContainer container = new WireMockContainer(
39+
new DockerImageName(WireMockContainer.WIREMOCK_2_LATEST.getUnversionedPart(),
40+
WireMockContainer.WIREMOCK_2_LATEST.getVersionPart()+ "-test")
41+
.asCompatibleSubstituteFor(WireMockContainer.WIREMOCK_2_LATEST));
42+
}
43+
44+
@Test
45+
public void shouldInitWithVersionSubstitution() {
46+
WireMockContainer container = new WireMockContainer(
47+
new DockerImageName(WireMockContainer.OFFICIAL_IMAGE_NAME, "test")
48+
.asCompatibleSubstituteFor(WireMockContainer.WIREMOCK_2_LATEST));
49+
}
50+
51+
@Test
52+
@Disabled("Requires https://github.com/testcontainers/testcontainers-java/issues/7305")
53+
public void shouldFailForUnversionedImage() {
54+
IllegalStateException ex = Assertions.assertThrows(IllegalStateException.class, () -> {
55+
WireMockContainer container = new WireMockContainer(
56+
new DockerImageName(WireMockContainer.OFFICIAL_IMAGE_NAME, "test"));
57+
});
58+
assertThat(ex.getMessage())
59+
.as("Wrong exception message")
60+
.contains("Failed to verify that image")
61+
.contains("is a compatible substitute for '" + WireMockContainer.OFFICIAL_IMAGE_NAME + "'");
62+
}
63+
64+
@Test
65+
public void shouldFailCustomImageWithoutSubstitution() {
66+
IllegalStateException ex = Assertions.assertThrows(IllegalStateException.class, () -> {
67+
WireMockContainer container = new WireMockContainer(
68+
new DockerImageName("mycorp/mywiremockimage", WireMockContainer.WIREMOCK_2_LATEST.getVersionPart()));
69+
});
70+
assertThat(ex.getMessage())
71+
.as("Wrong exception message")
72+
.contains("Failed to verify that image")
73+
.contains("is a compatible substitute for '" + WireMockContainer.OFFICIAL_IMAGE_NAME + "'");
74+
}
75+
76+
}

0 commit comments

Comments
 (0)