diff --git a/src/main/java/org/wiremock/integrations/testcontainers/WireMockContainer.java b/src/main/java/org/wiremock/integrations/testcontainers/WireMockContainer.java index d3792ac..e3f9104 100644 --- a/src/main/java/org/wiremock/integrations/testcontainers/WireMockContainer.java +++ b/src/main/java/org/wiremock/integrations/testcontainers/WireMockContainer.java @@ -15,6 +15,13 @@ */ package org.wiremock.integrations.testcontainers; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.wait.strategy.Wait; +import org.testcontainers.containers.wait.strategy.WaitStrategy; +import org.testcontainers.images.builder.Transferable; +import org.testcontainers.shaded.com.google.common.io.Resources; +import org.testcontainers.utility.MountableFile; + import java.io.File; import java.io.IOException; import java.net.URL; @@ -30,13 +37,6 @@ import java.util.stream.Collectors; import java.util.stream.Stream; -import org.testcontainers.containers.GenericContainer; -import org.testcontainers.containers.wait.strategy.Wait; -import org.testcontainers.containers.wait.strategy.WaitStrategy; -import org.testcontainers.images.builder.Transferable; -import org.testcontainers.shaded.com.google.common.io.Resources; -import org.testcontainers.utility.MountableFile; - /** * Provisions WireMock standalone server as a container. * Designed to follow the WireMock Docker image ({@code wiremock/wiremock}) structure and configuration, @@ -50,19 +50,16 @@ public class WireMockContainer extends GenericContainer { private static final String FILES_DIR = "/home/wiremock/__files/"; private static final String EXTENSIONS_DIR = "/var/wiremock/extensions/"; - private static final WaitStrategy DEFAULT_WAITER = Wait .forHttp("/__admin/mappings") .withMethod("GET") .forStatusCode(200); - private static final int PORT = 8080; - private final StringBuilder wireMockArgs; - private final Map mappingStubs = new HashMap<>(); private final Map mappingFiles = new HashMap<>(); private final Map extensions = new HashMap<>(); + private boolean isBannerDisabled = true; public WireMockContainer() { this(DEFAULT_TAG); @@ -78,6 +75,24 @@ public WireMockContainer(String image, String version) { setWaitStrategy(DEFAULT_WAITER); } + /** + * Disables the banner when starting the WireMock container. + * @return this instance + */ + public WireMockContainer withoutBanner() { + isBannerDisabled = true; + return this; + } + + /** + * Enable the banner when starting the WireMock container. + * @return this instance + */ + public WireMockContainer withBanner() { + isBannerDisabled = false; + return this; + } + /** * Adds CLI argument to the WireMock call. * @param arg Argument @@ -223,6 +238,10 @@ protected void configure() { wireMockArgs.append(String.join(",", extensionClassNames)); } + if (isBannerDisabled) { + this.withCliArg("--disable-banner"); + } + // Add CLI arguments withCommand(wireMockArgs.toString()); } @@ -231,7 +250,7 @@ private static final class Stub { final String name; final String json; - public Stub (String name, String json) { + public Stub(String name, String json) { this.name = name; this.json = json; } @@ -246,5 +265,4 @@ public Extension(String id) { this.id = id; } } - } diff --git a/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerJUnit5Test.java b/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerJUnit5Test.java index da0f7fc..fcb24e8 100644 --- a/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerJUnit5Test.java +++ b/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerJUnit5Test.java @@ -1,8 +1,5 @@ package org.wiremock.integrations.testcontainers; -import static org.assertj.core.api.Assertions.assertThat; - -import java.net.http.HttpResponse; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; @@ -10,46 +7,50 @@ import org.testcontainers.junit.jupiter.Testcontainers; import org.wiremock.integrations.testcontainers.testsupport.http.TestHttpClient; +import java.net.http.HttpResponse; + +import static org.assertj.core.api.Assertions.assertThat; + @Testcontainers public class WireMockContainerJUnit5Test { - @Container - public WireMockContainer wiremockServer = new WireMockContainer("2.35.0") - .withMapping("hello", WireMockContainerTest.class, "hello-world.json") - .withMapping("hello-resource", WireMockContainerTest.class, "hello-world-resource.json") - .withFileFromResource("hello-world-resource-response.xml", WireMockContainerTest.class, - "hello-world-resource-response.xml"); - - - @ParameterizedTest - @ValueSource(strings = { - "hello", - "/hello" - }) - public void helloWorld(String path) throws Exception { - // given - String url = wiremockServer.getUrl(path); - - // when - HttpResponse response = TestHttpClient.newInstance().get(url); - - // then - assertThat(response.body()) - .as("Wrong response body") - .contains("Hello, world!"); - } - - @Test - public void helloWorldFromFile() throws Exception { - // given - String url = wiremockServer.getUrl("/hello-from-file"); - - // when - HttpResponse response = TestHttpClient.newInstance().get(url); - - // then - assertThat(response.body()) - .as("Wrong response body") - .contains("Hello, world!"); - } + @Container + public WireMockContainer wiremockServer = new WireMockContainer("2.35.0") + .withMapping("hello", WireMockContainerTest.class, "hello-world.json") + .withMapping("hello-resource", WireMockContainerTest.class, "hello-world-resource.json") + .withFileFromResource("hello-world-resource-response.xml", WireMockContainerTest.class, + "hello-world-resource-response.xml"); + + + @ParameterizedTest + @ValueSource(strings = { + "hello", + "/hello" + }) + public void helloWorld(String path) throws Exception { + // given + String url = wiremockServer.getUrl(path); + + // when + HttpResponse response = TestHttpClient.newInstance().get(url); + + // then + assertThat(response.body()) + .as("Wrong response body") + .contains("Hello, world!"); + } + + @Test + public void helloWorldFromFile() throws Exception { + // given + String url = wiremockServer.getUrl("/hello-from-file"); + + // when + HttpResponse response = TestHttpClient.newInstance().get(url); + + // then + assertThat(response.body()) + .as("Wrong response body") + .contains("Hello, world!"); + } } diff --git a/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerUnitTest.java b/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerUnitTest.java new file mode 100644 index 0000000..5efebd9 --- /dev/null +++ b/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerUnitTest.java @@ -0,0 +1,44 @@ +package org.wiremock.integrations.testcontainers; + +import org.junit.jupiter.api.Test; + +import java.util.Arrays; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class WireMockContainerUnitTest { + + @Test + public void bannerIsByDefaultDisabled() { + WireMockContainer wireMockContainer = new WireMockContainer("2.35.0"); + wireMockContainer.configure(); + + String[] startUpArgs = wireMockContainer.getCommandParts(); + + assertTrue(Arrays.asList(startUpArgs).contains("--disable-banner")); + } + + @Test + public void enableBanner() { + WireMockContainer wireMockContainerSpy = new WireMockContainer("2.35.0") + .withBanner(); + wireMockContainerSpy.configure(); + + String[] startUpArgs = wireMockContainerSpy.getCommandParts(); + + assertFalse(Arrays.asList(startUpArgs).contains("--disable-banner")); + } + + @Test + public void disableBanner() { + WireMockContainer wireMockContainerSpy = new WireMockContainer("2.35.0") + .withBanner() + .withoutBanner(); + wireMockContainerSpy.configure(); + + String[] startUpArgs = wireMockContainerSpy.getCommandParts(); + + assertTrue(Arrays.asList(startUpArgs).contains("--disable-banner")); + } +}