Skip to content

Commit e8a3f90

Browse files
author
Julian Akkaya
committed
Merge branch 'main' into feature/java8-compatibility
# Conflicts: # src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerJUnit5Test.java
2 parents b932b77 + bc5fd23 commit e8a3f90

File tree

13 files changed

+382
-49
lines changed

13 files changed

+382
-49
lines changed

README.md

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,10 @@ P.S: Javadoc is coming soon!
3939
```java
4040
import static org.assertj.core.api.Assertions.assertThat;
4141

42-
import java.net.http.HttpClient;
43-
import java.net.http.HttpRequest;
44-
import java.net.http.HttpResponse;
42+
import java.net.http.*;
4543
import java.time.Duration;
4644
import org.junit.jupiter.api.Test;
47-
import org.testcontainers.junit.jupiter.Container;
48-
import org.testcontainers.junit.jupiter.Testcontainers;
45+
import org.testcontainers.junit.jupiter.*;
4946

5047
@Testcontainers
5148
public class WireMockContainerJUnit5Test {
@@ -77,15 +74,16 @@ public class WireMockContainerJUnit5Test {
7774

7875
#### Sample Code using JUnit 4
7976

77+
<details>
78+
<summary>
79+
Show Code
80+
</summary>
81+
8082
```java
8183
import org.wiremock.integrations.testcontainers.WireMockContainer;
82-
import org.junit.Rule;
83-
import org.junit.Test;
84-
84+
import org.junit.*;
8585
import java.net.URI;
86-
import java.net.http.HttpClient;
87-
import java.net.http.HttpRequest;
88-
import java.net.http.HttpResponse;
86+
import java.net.http.*;
8987
import java.time.Duration;
9088

9189
public class WireMockContainerTest {
@@ -112,7 +110,8 @@ public class WireMockContainerTest {
112110
}
113111
}
114112
```
115-
113+
</details>
114+
116115
### Using WireMock extensions
117116

118117
The API supports adding [WireMock extensions](https://wiremock.org/docs/extending-wiremock/)
@@ -215,7 +214,13 @@ public class WireMockContainerExtensionJUnit5Test {
215214

216215
##### Sample code using JUnit 4
217216

217+
<details>
218+
<summary>
219+
Show Code
220+
</summary>
221+
218222
```java
223+
219224
public class WireMockContainerExtensionTest {
220225
@Rule
221226
public WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
@@ -238,7 +243,8 @@ public class WireMockContainerExtensionTest {
238243
.contains("Hello, John Doe!");
239244
}
240245
}
241-
```
246+
```
247+
</details>
242248

243249
## Contributing
244250

pom.xml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<testcontainers.version>1.18.1</testcontainers.version>
2424
<junit.version>5.9.3</junit.version>
2525
<assertj.version>3.24.2</assertj.version>
26+
<awaitility.version>4.2.0</awaitility.version>
2627
<project.scm.id>github</project.scm.id>
2728
</properties>
2829

@@ -88,6 +89,12 @@
8889
<artifactId>assertj-core</artifactId>
8990
<scope>test</scope>
9091
</dependency>
92+
<dependency>
93+
<groupId>org.awaitility</groupId>
94+
<artifactId>awaitility</artifactId>
95+
<version>${awaitility.version}</version>
96+
<scope>test</scope>
97+
</dependency>
9198
<dependency>
9299
<groupId>ch.qos.logback</groupId>
93100
<artifactId>logback-classic</artifactId>
@@ -114,7 +121,7 @@
114121
<plugin>
115122
<groupId>org.apache.maven.plugins</groupId>
116123
<artifactId>maven-source-plugin</artifactId>
117-
<version>3.2.1</version>
124+
<version>3.3.0</version>
118125
<executions>
119126
<execution>
120127
<id>attach-sources</id>
@@ -142,7 +149,7 @@
142149
<plugin>
143150
<groupId>org.apache.maven.plugins</groupId>
144151
<artifactId>maven-dependency-plugin</artifactId>
145-
<version>3.5.0</version>
152+
<version>3.6.0</version>
146153
<executions>
147154
<execution>
148155
<id>copy</id>

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

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
*/
1616
package org.wiremock.integrations.testcontainers;
1717

18+
import org.testcontainers.containers.GenericContainer;
19+
import org.testcontainers.containers.wait.strategy.Wait;
20+
import org.testcontainers.containers.wait.strategy.WaitStrategy;
21+
import org.testcontainers.images.builder.Transferable;
22+
import org.testcontainers.shaded.com.google.common.io.Resources;
23+
import org.testcontainers.utility.MountableFile;
24+
1825
import java.io.File;
1926
import java.io.IOException;
2027
import java.net.URL;
@@ -30,13 +37,6 @@
3037
import java.util.stream.Collectors;
3138
import java.util.stream.Stream;
3239

33-
import org.testcontainers.containers.GenericContainer;
34-
import org.testcontainers.containers.wait.strategy.Wait;
35-
import org.testcontainers.containers.wait.strategy.WaitStrategy;
36-
import org.testcontainers.images.builder.Transferable;
37-
import org.testcontainers.shaded.com.google.common.io.Resources;
38-
import org.testcontainers.utility.MountableFile;
39-
4040
/**
4141
* Provisions WireMock standalone server as a container.
4242
* Designed to follow the WireMock Docker image ({@code wiremock/wiremock}) structure and configuration,
@@ -50,19 +50,16 @@ public class WireMockContainer extends GenericContainer<WireMockContainer> {
5050
private static final String FILES_DIR = "/home/wiremock/__files/";
5151

5252
private static final String EXTENSIONS_DIR = "/var/wiremock/extensions/";
53-
5453
private static final WaitStrategy DEFAULT_WAITER = Wait
5554
.forHttp("/__admin/mappings")
5655
.withMethod("GET")
5756
.forStatusCode(200);
58-
5957
private static final int PORT = 8080;
60-
6158
private final StringBuilder wireMockArgs;
62-
6359
private final Map<String, Stub> mappingStubs = new HashMap<>();
6460
private final Map<String, MountableFile> mappingFiles = new HashMap<>();
6561
private final Map<String, Extension> extensions = new HashMap<>();
62+
private boolean isBannerDisabled = true;
6663

6764
public WireMockContainer() {
6865
this(DEFAULT_TAG);
@@ -78,6 +75,24 @@ public WireMockContainer(String image, String version) {
7875
setWaitStrategy(DEFAULT_WAITER);
7976
}
8077

78+
/**
79+
* Disables the banner when starting the WireMock container.
80+
* @return this instance
81+
*/
82+
public WireMockContainer withoutBanner() {
83+
isBannerDisabled = true;
84+
return this;
85+
}
86+
87+
/**
88+
* Enable the banner when starting the WireMock container.
89+
* @return this instance
90+
*/
91+
public WireMockContainer withBanner() {
92+
isBannerDisabled = false;
93+
return this;
94+
}
95+
8196
/**
8297
* Adds CLI argument to the WireMock call.
8398
* @param arg Argument
@@ -223,6 +238,10 @@ protected void configure() {
223238
wireMockArgs.append(String.join(",", extensionClassNames));
224239
}
225240

241+
if (isBannerDisabled) {
242+
this.withCliArg("--disable-banner");
243+
}
244+
226245
// Add CLI arguments
227246
withCommand(wireMockArgs.toString());
228247
}
@@ -231,7 +250,7 @@ private static final class Stub {
231250
final String name;
232251
final String json;
233252

234-
public Stub (String name, String json) {
253+
public Stub(String name, String json) {
235254
this.name = name;
236255
this.json = json;
237256
}
@@ -246,5 +265,4 @@ public Extension(String id) {
246265
this.id = id;
247266
}
248267
}
249-
250268
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.slf4j.Logger;
2222
import org.slf4j.LoggerFactory;
2323
import org.testcontainers.containers.output.Slf4jLogConsumer;
24+
import org.wiremock.integrations.testcontainers.testsupport.http.HttpResponse;
2425
import org.wiremock.integrations.testcontainers.testsupport.http.TestHttpClient;
2526

2627
import java.nio.file.Paths;
@@ -57,10 +58,10 @@ public void testJSONBodyTransformer() throws Exception {
5758
String body = "{\"name\":\"John Doe\"}";
5859

5960
// when
60-
String response = new TestHttpClient().post(url, body);
61+
HttpResponse response = new TestHttpClient().post(url, body);
6162

6263
// then
63-
assertThat(response)
64+
assertThat(response.getBody())
6465
.as("Wrong response body")
6566
.contains("Hello, John Doe!");
6667
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.slf4j.Logger;
2222
import org.slf4j.LoggerFactory;
2323
import org.testcontainers.containers.output.Slf4jLogConsumer;
24+
import org.wiremock.integrations.testcontainers.testsupport.http.HttpResponse;
2425
import org.wiremock.integrations.testcontainers.testsupport.http.TestHttpClient;
2526

2627
import java.nio.file.Paths;
@@ -56,20 +57,17 @@ public void before() {
5657
public void testJSONBodyTransformer() throws Exception {
5758

5859
LOGGER.warn("Waiting for WireMock to start");
59-
TimeUnit.SECONDS.sleep(20);
60-
6160

6261
// given
6362
String url = wiremockServer.getUrl("/json-body-transformer");
6463
String body = "{\"name\":\"John Doe\"}";
6564

6665
// when
67-
String response = new TestHttpClient().post(url, body);
66+
HttpResponse response = new TestHttpClient().post(url, body);
6867

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

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.junit.jupiter.params.provider.ValueSource;
66
import org.testcontainers.junit.jupiter.Container;
77
import org.testcontainers.junit.jupiter.Testcontainers;
8+
import org.wiremock.integrations.testcontainers.testsupport.http.HttpResponse;
89
import org.wiremock.integrations.testcontainers.testsupport.http.TestHttpClient;
910

1011
import static org.assertj.core.api.Assertions.assertThat;
@@ -30,10 +31,10 @@ public void helloWorld(String path) throws Exception {
3031
String url = wiremockServer.getUrl(path);
3132

3233
// when
33-
String response = new TestHttpClient().get(url);
34+
HttpResponse response = new TestHttpClient().get(url);
3435

3536
// then
36-
assertThat(response)
37+
assertThat(response.getBody())
3738
.as("Wrong response body")
3839
.contains("Hello, world!");
3940
}
@@ -44,10 +45,10 @@ public void helloWorldFromFile() throws Exception {
4445
String url = wiremockServer.getUrl("/hello-from-file");
4546

4647
// when
47-
String response = new TestHttpClient().get(url);
48+
HttpResponse response = new TestHttpClient().get(url);
4849

4950
// then
50-
assertThat(response)
51+
assertThat(response.getBody())
5152
.as("Wrong response body")
5253
.contains("Hello, world!");
5354
}

0 commit comments

Comments
 (0)