Skip to content

Commit 95fe426

Browse files
Merge pull request #72 from wiremock/wiremock-3-by-default
Update the tests to use WireMock 3.0.0 release
2 parents 0186947 + d648624 commit 95fe426

File tree

16 files changed

+256
-19
lines changed

16 files changed

+256
-19
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ A common example is using Wiremock 3.x with Java 1.8.
2424
The module is compatible with the following WireMock versions:
2525

2626
- WireMock (aka WireMock Java) `2.0.0` and above
27-
- WireMock (aka WireMock Java) `3.0.0` beta versions.
27+
- WireMock (aka WireMock Java) `3.x` versions.
2828
Note that the official image for WireMock 3 is yet to be released and verified ([issue #59](https://github.com/wiremock/wiremock-testcontainers-java/issues/59))
2929

3030
Other WireMock implementations may work but have not been tested yet.

pom.xml

+6-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<java.version>1.8</java.version>
3333
<maven.compiler.target>${java.version}</maven.compiler.target>
3434
<maven.compiler.source>${java.version}</maven.compiler.source>
35-
<wiremock.version>2.35.0</wiremock.version>
35+
<wiremock.version>3.5.4</wiremock.version>
3636
<testcontainers.version>1.19.8</testcontainers.version>
3737
<junit.version>5.10.2</junit.version>
3838
<assertj.version>3.26.3</assertj.version>
@@ -178,6 +178,11 @@
178178
<version>0.4.1</version>
179179
<classifier>jar-with-dependencies</classifier>
180180
</artifactItem>
181+
<dependency>
182+
<groupId>org.wiremock</groupId>
183+
<artifactId>wiremock</artifactId>
184+
<version>${wiremock.version}</version>
185+
</dependency>
181186
<dependency>
182187
<groupId>org.wiremock</groupId>
183188
<artifactId>wiremock-webhooks-extension</artifactId>

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,15 @@
4747
public class WireMockContainer extends GenericContainer<WireMockContainer> {
4848

4949
public static final String OFFICIAL_IMAGE_NAME = "wiremock/wiremock";
50-
private static final String WIREMOCK_2_LATEST_TAG = "2.35.0";
50+
private static final String WIREMOCK_2_LATEST_TAG = "2.35.0-1";
51+
private static final String WIREMOCK_3_LATEST_TAG = "3.5.4";
5152
/*package*/ static final String WIREMOCK_2_MINIMUM_SUPPORTED_VERSION = "2.0.0";
52-
5353
static final String WIREMOCK_HEALTH_CHECK_SUPPORT_MINIMUM_VERSION = "3.0.0-1";
5454

55+
/**
56+
* @deprecated Not really guaranteed to be latest. Will be reworked
57+
*/
58+
@Deprecated
5559
public static final DockerImageName WIREMOCK_2_LATEST =
5660
DockerImageName.parse(OFFICIAL_IMAGE_NAME).withTag(WIREMOCK_2_LATEST_TAG);
5761

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.wiremock.integrations.testcontainers;
2+
3+
import org.testcontainers.utility.DockerImageName;
4+
5+
public class TestConfig {
6+
7+
private static final String DEFAULT_TEST_TAG =
8+
System.getProperty("wiremock.testcontainer.defaultTag", "3.5.4");
9+
private static final String WIREMOCK_2_TEST_TAG =
10+
System.getProperty("wiremock.testcontainer.wiremock2Tag", "2.35.1-1");
11+
12+
public static final DockerImageName WIREMOCK_DEFAULT_IMAGE =
13+
DockerImageName.parse(WireMockContainer.OFFICIAL_IMAGE_NAME).withTag(DEFAULT_TEST_TAG);
14+
15+
public static final DockerImageName WIREMOCK_2_IMAGE =
16+
DockerImageName.parse(WireMockContainer.OFFICIAL_IMAGE_NAME).withTag(WIREMOCK_2_TEST_TAG);
17+
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
class WireMockContainerBannerTest {
2323

24-
WireMockContainer wireMockContainer = new WireMockContainer(WireMockContainer.WIREMOCK_2_LATEST);
24+
WireMockContainer wireMockContainer = new WireMockContainer(TestConfig.WIREMOCK_DEFAULT_IMAGE);
2525

2626
@Test
2727
void bannerIsByDefaultDisabled() {

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

+1-1
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(WireMockContainer.WIREMOCK_2_LATEST)
43+
WireMockContainer wiremockServer = new WireMockContainer(TestConfig.WIREMOCK_DEFAULT_IMAGE)
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

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ class WireMockContainerExtensionsCombinationTest {
3939
private static final Logger LOGGER = LoggerFactory.getLogger(WireMockContainerExtensionsCombinationTest.class);
4040

4141
@Container
42-
WireMockContainer wiremockServer = new WireMockContainer(WireMockContainer.WIREMOCK_2_LATEST)
42+
WireMockContainer wiremockServer = new WireMockContainer(TestConfig.WIREMOCK_DEFAULT_IMAGE)
4343
.withLogConsumer(new Slf4jLogConsumer(LOGGER))
4444
.withMapping("json-body-transformer", WireMockContainerExtensionsCombinationTest.class, "json-body-transformer.json")
4545
.withExtensions("Webhook",
4646
Collections.singleton("org.wiremock.webhooks.Webhooks"),
47-
Collections.singleton(Paths.get("target", "test-wiremock-extension", "wiremock-webhooks-extension-2.35.0.jar").toFile()))
47+
Collections.singleton(Paths.get("target", "test-wiremock-extension", "wiremock-webhooks-extension-3.5.4.jar").toFile()))
4848
.withExtensions("JSON Body Transformer",
4949
Collections.singleton("com.ninecookies.wiremock.extensions.JsonBodyTransformer"),
5050
Collections.singleton(Paths.get("target", "test-wiremock-extension", "wiremock-extensions-0.4.1-jar-with-dependencies.jar").toFile()));

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

+1-1
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(WireMockContainer.WIREMOCK_2_LATEST)
28+
public WireMockContainer wiremockServer = new WireMockContainer(TestConfig.WIREMOCK_DEFAULT_IMAGE)
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
class WireMockContainerTest {
3030

3131
@Container
32-
WireMockContainer wiremockServer = new WireMockContainer(WireMockContainer.WIREMOCK_2_LATEST)
32+
WireMockContainer wiremockServer = new WireMockContainer(TestConfig.WIREMOCK_DEFAULT_IMAGE)
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,

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class WireMockContainerUnitTest {
1111

1212
@Test
1313
public void shouldInitWithDefault() {
14-
WireMockContainer container = new WireMockContainer(WireMockContainer.WIREMOCK_2_LATEST);
14+
WireMockContainer container = new WireMockContainer(TestConfig.WIREMOCK_DEFAULT_IMAGE);
1515
}
1616

1717
@Test
+4-8
Original file line numberDiff line numberDiff line change
@@ -47,25 +47,21 @@
4747
* @see <a href="https://www.testcontainers.org/features/networking/">Testcontainers Networking</a>
4848
*/
4949
@Testcontainers
50-
class WireMockContainerExtensionsWebhookTest {
50+
class WireMockContainerWebhooksTest {
5151

52-
private static final Logger LOGGER = LoggerFactory.getLogger(WireMockContainerExtensionsWebhookTest.class);
52+
private static final Logger LOGGER = LoggerFactory.getLogger(WireMockContainerWebhooksTest.class);
5353
private static final String WIREMOCK_PATH = "/wiremock/callback-trigger";
5454
private static final String APPLICATION_PATH = "/application/callback-receiver";
5555

5656

5757
TestHttpServer applicationServer = TestHttpServer.newInstance();
5858
@Container
59-
WireMockContainer wiremockServer = new WireMockContainer(WireMockContainer.WIREMOCK_2_LATEST)
59+
WireMockContainer wiremockServer = new WireMockContainer(TestConfig.WIREMOCK_DEFAULT_IMAGE)
6060
.withLogConsumer(new Slf4jLogConsumer(LOGGER))
6161
.withCliArg("--global-response-templating")
62-
.withMapping("webhook-callback-template", WireMockContainerExtensionsWebhookTest.class, "webhook-callback-template.json")
63-
.withExtensions("Webhook",
64-
Collections.singleton("org.wiremock.webhooks.Webhooks"),
65-
Collections.singleton(Paths.get("target", "test-wiremock-extension", "wiremock-webhooks-extension-2.35.0.jar").toFile()))
62+
.withMapping("webhook-callback-template", WireMockContainerWebhooksTest.class, "webhook-callback-template.json")
6663
.withAccessToHost(true); // Force the host access mechanism
6764

68-
6965
@Test
7066
void callbackUsingJsonStub() throws Exception {
7167
// given
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright (C) 2023 WireMock Inc, Oleg Nenashev and all project contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.wiremock.integrations.testcontainers.wiremock2;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.testcontainers.Testcontainers.exposeHostPorts;
20+
import static org.testcontainers.shaded.org.awaitility.Awaitility.await;
21+
22+
import java.nio.file.Paths;
23+
import java.time.Duration;
24+
import java.util.Collections;
25+
import org.junit.jupiter.api.Test;
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
28+
import org.testcontainers.containers.GenericContainer;
29+
import org.testcontainers.containers.output.Slf4jLogConsumer;
30+
import org.testcontainers.junit.jupiter.Container;
31+
import org.testcontainers.junit.jupiter.Testcontainers;
32+
import org.wiremock.integrations.testcontainers.TestConfig;
33+
import org.wiremock.integrations.testcontainers.WireMockContainer;
34+
import org.wiremock.integrations.testcontainers.testsupport.http.HttpResponse;
35+
import org.wiremock.integrations.testcontainers.testsupport.http.TestHttpClient;
36+
import org.wiremock.integrations.testcontainers.testsupport.http.TestHttpServer;
37+
38+
/**
39+
* Tests the WireMock Webhook extension and TestContainers Networking
40+
* For this type of tests we should use following steps:
41+
* <p>
42+
* Use {@link GenericContainer#withAccessToHost(boolean)} to force the host access mechanism
43+
* <p>
44+
* Use {@link org.testcontainers.Testcontainers#exposeHostPorts(int...)} to expose host machine ports to containers
45+
* <p>
46+
* Use {@link GenericContainer#INTERNAL_HOST_HOSTNAME} to calculate hostname for callback
47+
*
48+
* @see <a href="https://www.testcontainers.org/features/networking/">Testcontainers Networking</a>
49+
*/
50+
@Testcontainers
51+
class WebhooksExtensionTest {
52+
53+
private static final Logger LOGGER = LoggerFactory.getLogger(WebhooksExtensionTest.class);
54+
private static final String WIREMOCK_PATH = "/wiremock/callback-trigger";
55+
private static final String APPLICATION_PATH = "/application/callback-receiver";
56+
57+
58+
TestHttpServer applicationServer = TestHttpServer.newInstance();
59+
@Container
60+
WireMockContainer wiremockServer = new WireMockContainer(TestConfig.WIREMOCK_2_IMAGE)
61+
.withLogConsumer(new Slf4jLogConsumer(LOGGER))
62+
.withCliArg("--global-response-templating")
63+
.withMapping("webhook-callback-template", WebhooksExtensionTest.class, "webhook-callback-template.json")
64+
.withExtensions("Webhooks",
65+
Collections.singleton("org.wiremock.webhooks.Webhooks"),
66+
Collections.singleton(Paths.get("target", "test-wiremock-extension", "wiremock-webhooks-extension-2.35.0.jar").toFile()))
67+
.withAccessToHost(true); // Force the host access mechanism
68+
69+
@Test
70+
void callbackUsingJsonStub() throws Exception {
71+
// given
72+
exposeHostPorts(applicationServer.getPort()); // Exposing host ports to the container
73+
74+
String wiremockUrl = wiremockServer.getUrl(WIREMOCK_PATH);
75+
String applicationCallbackUrl = String.format("http://%s:%d%s", GenericContainer.INTERNAL_HOST_HOSTNAME, applicationServer.getPort(), APPLICATION_PATH);
76+
77+
// when
78+
HttpResponse response = new TestHttpClient().post(
79+
wiremockUrl,
80+
"{\"callbackMethod\": \"PUT\", \"callbackUrl\": \"" + applicationCallbackUrl + "\"}"
81+
);
82+
83+
// then
84+
assertThat(response).as("Wiremock Response").isNotNull().satisfies(it -> {
85+
assertThat(it.getStatusCode()).as("Wiremock Response Status").isEqualTo(200);
86+
assertThat(it.getBody()).as("Wiremock Response Body")
87+
.contains("Please wait callback")
88+
.contains("PUT")
89+
.contains(applicationCallbackUrl);
90+
});
91+
92+
await().atMost(Duration.ofMillis(5000)).untilAsserted(() -> {
93+
assertThat(applicationServer.getRecordedRequests()).as("Received Callback")
94+
.hasSize(1)
95+
.first().usingRecursiveComparison()
96+
.isEqualTo(new TestHttpServer.RecordedRequest("PUT", APPLICATION_PATH, "Async processing Finished"));
97+
});
98+
}
99+
100+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright (C) 2023 WireMock Inc, Oleg Nenashev and all project contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.wiremock.integrations.testcontainers.wiremock2;
17+
18+
import org.junit.jupiter.api.Test;
19+
import org.slf4j.Logger;
20+
import org.slf4j.LoggerFactory;
21+
import org.testcontainers.containers.output.Slf4jLogConsumer;
22+
import org.testcontainers.junit.jupiter.Container;
23+
import org.testcontainers.junit.jupiter.Testcontainers;
24+
import org.wiremock.integrations.testcontainers.TestConfig;
25+
import org.wiremock.integrations.testcontainers.WireMockContainer;
26+
import org.wiremock.integrations.testcontainers.testsupport.http.HttpResponse;
27+
import org.wiremock.integrations.testcontainers.testsupport.http.TestHttpClient;
28+
29+
import java.nio.file.Paths;
30+
import java.util.Collections;
31+
32+
import static org.assertj.core.api.Assertions.assertThat;
33+
34+
/**
35+
* Tests the WireMock extension loading.
36+
* It uses multiple external Jars supplied by the Maven Dependency Plugin.
37+
*/
38+
@Testcontainers
39+
class WireMockContainerExtensionsCombinationTest {
40+
41+
private static final Logger LOGGER = LoggerFactory.getLogger(WireMockContainerExtensionsCombinationTest.class);
42+
43+
@Container
44+
WireMockContainer wiremockServer = new WireMockContainer(TestConfig.WIREMOCK_2_IMAGE)
45+
.withLogConsumer(new Slf4jLogConsumer(LOGGER))
46+
.withMapping("json-body-transformer", WireMockContainerExtensionsCombinationTest.class, "json-body-transformer.json")
47+
.withExtensions("Webhook",
48+
Collections.singleton("org.wiremock.webhooks.Webhooks"),
49+
Collections.singleton(Paths.get("target", "test-wiremock-extension", "wiremock-webhooks-extension-2.35.0.jar").toFile()))
50+
.withExtensions("JSON Body Transformer",
51+
Collections.singleton("com.ninecookies.wiremock.extensions.JsonBodyTransformer"),
52+
Collections.singleton(Paths.get("target", "test-wiremock-extension", "wiremock-extensions-0.4.1-jar-with-dependencies.jar").toFile()));
53+
54+
@Test
55+
void testJSONBodyTransformer() throws Exception {
56+
// given
57+
String url = wiremockServer.getUrl("/json-body-transformer");
58+
String body = "{\"name\":\"John Doe\"}";
59+
60+
// when
61+
HttpResponse response = new TestHttpClient().post(url, body);
62+
63+
// then
64+
assertThat(response.getBody())
65+
.as("Wrong response body")
66+
.contains("Hello, John Doe!");
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"request": {
3+
"method": "POST",
4+
"urlPath": "/wiremock/callback-trigger"
5+
},
6+
"response": {
7+
"status": 200,
8+
"headers": {
9+
"Content-Type": "application/json"
10+
},
11+
"jsonBody": {
12+
"message": "Please wait callback",
13+
"method": "{{jsonPath request.body '$.callbackMethod'}}",
14+
"url": "{{jsonPath request.body '$.callbackUrl'}}"
15+
}
16+
},
17+
"postServeActions": [
18+
{
19+
"name": "webhook",
20+
"parameters": {
21+
"method": "{{jsonPath originalRequest.body '$.callbackMethod'}}",
22+
"url": "{{jsonPath originalRequest.body '$.callbackUrl'}}",
23+
"body": "Async processing Finished",
24+
"delay": {
25+
"type": "fixed",
26+
"milliseconds": 1000
27+
}
28+
}
29+
}
30+
]
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"request": {
3+
"method": "POST",
4+
"url": "/json-body-transformer"
5+
},
6+
"response": {
7+
"status": 201,
8+
"headers": {
9+
"content-type": "application/json"
10+
},
11+
"jsonBody": {
12+
"message": "Hello, $(name)!"
13+
},
14+
"transformers" : ["json-body-transformer"]
15+
}
16+
}

0 commit comments

Comments
 (0)