Skip to content

Commit eb7734e

Browse files
committed
Adapt to WireMock 3.1.0 with extension removal, split out WireMock 2 ITs
1 parent 782356f commit eb7734e

File tree

8 files changed

+224
-10
lines changed

8 files changed

+224
-10
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@
168168
<dependency>
169169
<groupId>org.wiremock</groupId>
170170
<artifactId>wiremock-webhooks-extension</artifactId>
171-
<version>3.0.1</version>
171+
<version>2.35.1</version>
172172
</dependency>
173173
</artifactItems>
174174
<outputDirectory>${project.build.directory}/test-wiremock-extension</outputDirectory>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
public class TestConfig {
66

77
private static final String DEFAULT_TEST_TAG =
8-
System.getProperty("wiremock.testcontainer.defaultTag", "3.0.1-1");
8+
System.getProperty("wiremock.testcontainer.defaultTag", "3.1.0-1");
99
private static final String WIREMOCK_2_TEST_TAG =
10-
System.getProperty("wiremock.testcontainer.wiremock2Tag", "2.35.0-1");
10+
System.getProperty("wiremock.testcontainer.wiremock2Tag", "2.35.1-1");
1111

1212
public static final DockerImageName WIREMOCK_DEFAULT_IMAGE =
1313
DockerImageName.parse(WireMockContainer.OFFICIAL_IMAGE_NAME).withTag(DEFAULT_TEST_TAG);
Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@
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

@@ -59,13 +59,10 @@ class WireMockContainerExtensionsWebhookTest {
5959
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-3.0.1.jar").toFile()))
62+
.withMapping("webhook-callback-template", WireMockContainerWebhooksTest.class, "webhook-callback-template.json")
63+
.withExtension("org.wiremock.webhooks.Webhooks")
6664
.withAccessToHost(true); // Force the host access mechanism
6765

68-
6966
@Test
7067
void callbackUsingJsonStub() throws Exception {
7168
// given
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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.GenericContainer;
22+
import org.testcontainers.containers.output.Slf4jLogConsumer;
23+
import org.testcontainers.junit.jupiter.Container;
24+
import org.testcontainers.junit.jupiter.Testcontainers;
25+
import org.wiremock.integrations.testcontainers.TestConfig;
26+
import org.wiremock.integrations.testcontainers.WireMockContainer;
27+
import org.wiremock.integrations.testcontainers.testsupport.http.HttpResponse;
28+
import org.wiremock.integrations.testcontainers.testsupport.http.TestHttpClient;
29+
import org.wiremock.integrations.testcontainers.testsupport.http.TestHttpServer;
30+
31+
import java.nio.file.Paths;
32+
import java.time.Duration;
33+
import java.util.Collections;
34+
35+
import static org.assertj.core.api.Assertions.assertThat;
36+
import static org.testcontainers.Testcontainers.exposeHostPorts;
37+
import static org.testcontainers.shaded.org.awaitility.Awaitility.await;
38+
39+
/**
40+
* Tests the WireMock Webhook extension and TestContainers Networking
41+
* For this type of tests we should use following steps:
42+
* <p>
43+
* Use {@link GenericContainer#withAccessToHost(boolean)} to force the host access mechanism
44+
* <p>
45+
* Use {@link org.testcontainers.Testcontainers#exposeHostPorts(int...)} to expose host machine ports to containers
46+
* <p>
47+
* Use {@link GenericContainer#INTERNAL_HOST_HOSTNAME} to calculate hostname for callback
48+
*
49+
* @see <a href="https://www.testcontainers.org/features/networking/">Testcontainers Networking</a>
50+
*/
51+
@Testcontainers
52+
class WebhooksExtensionTest {
53+
54+
private static final Logger LOGGER = LoggerFactory.getLogger(WebhooksExtensionTest.class);
55+
private static final String WIREMOCK_PATH = "/wiremock/callback-trigger";
56+
private static final String APPLICATION_PATH = "/application/callback-receiver";
57+
58+
59+
TestHttpServer applicationServer = TestHttpServer.newInstance();
60+
@Container
61+
WireMockContainer wiremockServer = new WireMockContainer(TestConfig.WIREMOCK_2_IMAGE)
62+
.withLogConsumer(new Slf4jLogConsumer(LOGGER))
63+
.withCliArg("--global-response-templating")
64+
.withMapping("webhook-callback-template", WebhooksExtensionTest.class, "webhook-callback-template.json")
65+
.withExtensions("Webhooks",
66+
Collections.singleton("org.wiremock.webhooks.Webhooks"),
67+
Collections.singleton(Paths.get("target", "test-wiremock-extension", "wiremock-webhooks-extension-2.35.1.jar").toFile()))
68+
.withAccessToHost(true); // Force the host access mechanism
69+
70+
71+
@Test
72+
void callbackUsingJsonStub() throws Exception {
73+
// given
74+
exposeHostPorts(applicationServer.getPort()); // Exposing host ports to the container
75+
76+
String wiremockUrl = wiremockServer.getUrl(WIREMOCK_PATH);
77+
String applicationCallbackUrl = String.format("http://%s:%d%s", GenericContainer.INTERNAL_HOST_HOSTNAME, applicationServer.getPort(), APPLICATION_PATH);
78+
79+
// when
80+
HttpResponse response = new TestHttpClient().post(
81+
wiremockUrl,
82+
"{\"callbackMethod\": \"PUT\", \"callbackUrl\": \"" + applicationCallbackUrl + "\"}"
83+
);
84+
85+
// then
86+
assertThat(response).as("Wiremock Response").isNotNull().satisfies(it -> {
87+
assertThat(it.getStatusCode()).as("Wiremock Response Status").isEqualTo(200);
88+
assertThat(it.getBody()).as("Wiremock Response Body")
89+
.contains("Please wait callback")
90+
.contains("PUT")
91+
.contains(applicationCallbackUrl);
92+
});
93+
94+
await().atMost(Duration.ofMillis(5000)).untilAsserted(() -> {
95+
assertThat(applicationServer.getRecordedRequests()).as("Received Callback")
96+
.hasSize(1)
97+
.first().usingRecursiveComparison()
98+
.isEqualTo(new TestHttpServer.RecordedRequest("PUT", APPLICATION_PATH, "Async processing Finished"));
99+
});
100+
}
101+
102+
}
Lines changed: 68 additions & 0 deletions
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.1.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)