Skip to content

Commit 5051fe9

Browse files
julian-michelmannJulian Akkayaoleg-nenashev
authored
Disable WireMock banner in the CLI output by default (#42)
* Disable banner by default when container is not running verbose * Fix format Co-authored-by: Oleg Nenashev <[email protected]> * Do static import instead of wildcard * add method with banner instead of showing the banner when starting verbose * Add separate unit test class * Remove mockito dependency * Add a method "withBanner" to enable the banner --------- Co-authored-by: Julian Akkaya <[email protected]> Co-authored-by: Oleg Nenashev <[email protected]>
1 parent 6a0fecf commit 5051fe9

File tree

3 files changed

+118
-55
lines changed

3 files changed

+118
-55
lines changed

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
}
Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,56 @@
11
package org.wiremock.integrations.testcontainers;
22

3-
import static org.assertj.core.api.Assertions.assertThat;
4-
5-
import java.net.http.HttpResponse;
63
import org.junit.jupiter.api.Test;
74
import org.junit.jupiter.params.ParameterizedTest;
85
import org.junit.jupiter.params.provider.ValueSource;
96
import org.testcontainers.junit.jupiter.Container;
107
import org.testcontainers.junit.jupiter.Testcontainers;
118
import org.wiremock.integrations.testcontainers.testsupport.http.TestHttpClient;
129

10+
import java.net.http.HttpResponse;
11+
12+
import static org.assertj.core.api.Assertions.assertThat;
13+
1314
@Testcontainers
1415
public class WireMockContainerJUnit5Test {
1516

16-
@Container
17-
public WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
18-
.withMapping("hello", WireMockContainerTest.class, "hello-world.json")
19-
.withMapping("hello-resource", WireMockContainerTest.class, "hello-world-resource.json")
20-
.withFileFromResource("hello-world-resource-response.xml", WireMockContainerTest.class,
21-
"hello-world-resource-response.xml");
22-
23-
24-
@ParameterizedTest
25-
@ValueSource(strings = {
26-
"hello",
27-
"/hello"
28-
})
29-
public void helloWorld(String path) throws Exception {
30-
// given
31-
String url = wiremockServer.getUrl(path);
32-
33-
// when
34-
HttpResponse<String> response = TestHttpClient.newInstance().get(url);
35-
36-
// then
37-
assertThat(response.body())
38-
.as("Wrong response body")
39-
.contains("Hello, world!");
40-
}
41-
42-
@Test
43-
public void helloWorldFromFile() throws Exception {
44-
// given
45-
String url = wiremockServer.getUrl("/hello-from-file");
46-
47-
// when
48-
HttpResponse<String> response = TestHttpClient.newInstance().get(url);
49-
50-
// then
51-
assertThat(response.body())
52-
.as("Wrong response body")
53-
.contains("Hello, world!");
54-
}
17+
@Container
18+
public WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
19+
.withMapping("hello", WireMockContainerTest.class, "hello-world.json")
20+
.withMapping("hello-resource", WireMockContainerTest.class, "hello-world-resource.json")
21+
.withFileFromResource("hello-world-resource-response.xml", WireMockContainerTest.class,
22+
"hello-world-resource-response.xml");
23+
24+
25+
@ParameterizedTest
26+
@ValueSource(strings = {
27+
"hello",
28+
"/hello"
29+
})
30+
public void helloWorld(String path) throws Exception {
31+
// given
32+
String url = wiremockServer.getUrl(path);
33+
34+
// when
35+
HttpResponse<String> response = TestHttpClient.newInstance().get(url);
36+
37+
// then
38+
assertThat(response.body())
39+
.as("Wrong response body")
40+
.contains("Hello, world!");
41+
}
42+
43+
@Test
44+
public void helloWorldFromFile() throws Exception {
45+
// given
46+
String url = wiremockServer.getUrl("/hello-from-file");
47+
48+
// when
49+
HttpResponse<String> response = TestHttpClient.newInstance().get(url);
50+
51+
// then
52+
assertThat(response.body())
53+
.as("Wrong response body")
54+
.contains("Hello, world!");
55+
}
5556
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.wiremock.integrations.testcontainers;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.Arrays;
6+
7+
import static org.junit.jupiter.api.Assertions.assertFalse;
8+
import static org.junit.jupiter.api.Assertions.assertTrue;
9+
10+
public class WireMockContainerUnitTest {
11+
12+
@Test
13+
public void bannerIsByDefaultDisabled() {
14+
WireMockContainer wireMockContainer = new WireMockContainer("2.35.0");
15+
wireMockContainer.configure();
16+
17+
String[] startUpArgs = wireMockContainer.getCommandParts();
18+
19+
assertTrue(Arrays.asList(startUpArgs).contains("--disable-banner"));
20+
}
21+
22+
@Test
23+
public void enableBanner() {
24+
WireMockContainer wireMockContainerSpy = new WireMockContainer("2.35.0")
25+
.withBanner();
26+
wireMockContainerSpy.configure();
27+
28+
String[] startUpArgs = wireMockContainerSpy.getCommandParts();
29+
30+
assertFalse(Arrays.asList(startUpArgs).contains("--disable-banner"));
31+
}
32+
33+
@Test
34+
public void disableBanner() {
35+
WireMockContainer wireMockContainerSpy = new WireMockContainer("2.35.0")
36+
.withBanner()
37+
.withoutBanner();
38+
wireMockContainerSpy.configure();
39+
40+
String[] startUpArgs = wireMockContainerSpy.getCommandParts();
41+
42+
assertTrue(Arrays.asList(startUpArgs).contains("--disable-banner"));
43+
}
44+
}

0 commit comments

Comments
 (0)