From d38c6c1c847cb71eb350224d814f71666f4792be Mon Sep 17 00:00:00 2001 From: Julian Akkaya Date: Mon, 22 May 2023 11:55:18 +0200 Subject: [PATCH 1/7] Disable banner by default when container is not running verbose --- pom.xml | 6 + .../testcontainers/WireMockContainer.java | 35 +++--- .../WireMockContainerJUnit5Test.java | 112 +++++++++++------- 3 files changed, 93 insertions(+), 60 deletions(-) diff --git a/pom.xml b/pom.xml index 999f9d7..eaf2cb5 100644 --- a/pom.xml +++ b/pom.xml @@ -94,6 +94,12 @@ provided true + + org.mockito + mockito-core + 5.3.1 + test + diff --git a/src/main/java/org/wiremock/integrations/testcontainers/WireMockContainer.java b/src/main/java/org/wiremock/integrations/testcontainers/WireMockContainer.java index d3792ac..26ffa13 100644 --- a/src/main/java/org/wiremock/integrations/testcontainers/WireMockContainer.java +++ b/src/main/java/org/wiremock/integrations/testcontainers/WireMockContainer.java @@ -15,28 +15,23 @@ */ 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; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; 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 +45,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); @@ -85,6 +77,9 @@ public WireMockContainer(String image, String version) { */ public WireMockContainer withCliArg(String arg) { //TODO: Switch to framework with proper CLI escaping + if (arg.contains("--verbose")) { + isBannerDisabled = false; + } wireMockArgs.append(' ').append(arg); return this; } @@ -223,6 +218,10 @@ protected void configure() { wireMockArgs.append(String.join(",", extensionClassNames)); } + if(isBannerDisabled) { + this.withCliArg("--disable-banner"); + } + // Add CLI arguments withCommand(wireMockArgs.toString()); } @@ -231,7 +230,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; } diff --git a/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerJUnit5Test.java b/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerJUnit5Test.java index da0f7fc..015a23d 100644 --- a/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerJUnit5Test.java +++ b/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerJUnit5Test.java @@ -1,55 +1,83 @@ 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; +import org.mockito.Mockito; import org.testcontainers.junit.jupiter.Container; 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; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + @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!"); + } + + @Test + public void defaultBannerDisabled() { + WireMockContainer wireMockContainerSpy = Mockito.spy(new WireMockContainer("2.35.0")); + + wireMockContainerSpy.configure(); + + verify(wireMockContainerSpy).withCliArg("--disable-banner"); + } + + @ParameterizedTest + @ValueSource(strings = { + "--verbose", + " --verbose ", + "--verbose --help" + }) + public void showBannerWhenStartingVerbose(String verboseArg) { + WireMockContainer wireMockContainerSpy = Mockito.spy(new WireMockContainer("2.35.0")); + wireMockContainerSpy.withCliArg(verboseArg); + + wireMockContainerSpy.configure(); + + verify(wireMockContainerSpy, times(0)).withCliArg("--disable-banner"); + } } From e55d491b228bf301d57703b153e5caa21bc3daae Mon Sep 17 00:00:00 2001 From: Julian Michelmann <45766492+julian-michelmann@users.noreply.github.com> Date: Tue, 23 May 2023 11:39:19 +0200 Subject: [PATCH 2/7] Fix format Co-authored-by: Oleg Nenashev --- .../wiremock/integrations/testcontainers/WireMockContainer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/wiremock/integrations/testcontainers/WireMockContainer.java b/src/main/java/org/wiremock/integrations/testcontainers/WireMockContainer.java index 26ffa13..e4f3c70 100644 --- a/src/main/java/org/wiremock/integrations/testcontainers/WireMockContainer.java +++ b/src/main/java/org/wiremock/integrations/testcontainers/WireMockContainer.java @@ -218,7 +218,7 @@ protected void configure() { wireMockArgs.append(String.join(",", extensionClassNames)); } - if(isBannerDisabled) { + if (isBannerDisabled) { this.withCliArg("--disable-banner"); } From 65ff4780954dc3ff20f637de1082659d43e56dd1 Mon Sep 17 00:00:00 2001 From: Julian Akkaya Date: Tue, 23 May 2023 11:56:31 +0200 Subject: [PATCH 3/7] Do static import instead of wildcard --- .../integrations/testcontainers/WireMockContainer.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/wiremock/integrations/testcontainers/WireMockContainer.java b/src/main/java/org/wiremock/integrations/testcontainers/WireMockContainer.java index 26ffa13..4474769 100644 --- a/src/main/java/org/wiremock/integrations/testcontainers/WireMockContainer.java +++ b/src/main/java/org/wiremock/integrations/testcontainers/WireMockContainer.java @@ -28,7 +28,12 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; -import java.util.*; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.stream.Collectors; import java.util.stream.Stream; From 006a641ecf9fd32733cea4df1e02b35a1f273699 Mon Sep 17 00:00:00 2001 From: Julian Akkaya Date: Tue, 23 May 2023 12:39:44 +0200 Subject: [PATCH 4/7] add method with banner instead of showing the banner when starting verbose --- .../testcontainers/WireMockContainer.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/wiremock/integrations/testcontainers/WireMockContainer.java b/src/main/java/org/wiremock/integrations/testcontainers/WireMockContainer.java index 185c790..64eea17 100644 --- a/src/main/java/org/wiremock/integrations/testcontainers/WireMockContainer.java +++ b/src/main/java/org/wiremock/integrations/testcontainers/WireMockContainer.java @@ -74,7 +74,16 @@ public WireMockContainer(String image, String version) { wireMockArgs = new StringBuilder(); setWaitStrategy(DEFAULT_WAITER); } - + + /** + * Enables the banner when starting WireMock container. + * @return this instance + */ + public WireMockContainer withBanner() { + isBannerDisabled = false; + return this; + } + /** * Adds CLI argument to the WireMock call. * @param arg Argument @@ -82,9 +91,6 @@ public WireMockContainer(String image, String version) { */ public WireMockContainer withCliArg(String arg) { //TODO: Switch to framework with proper CLI escaping - if (arg.contains("--verbose")) { - isBannerDisabled = false; - } wireMockArgs.append(' ').append(arg); return this; } @@ -250,5 +256,4 @@ public Extension(String id) { this.id = id; } } - } From 52508ef759e7fc4a9ad1fe53c9baf78b49571f84 Mon Sep 17 00:00:00 2001 From: Julian Akkaya Date: Tue, 23 May 2023 12:40:30 +0200 Subject: [PATCH 5/7] Add separate unit test class --- .../WireMockContainerJUnit5Test.java | 24 ---------------- .../WireMockContainerUnitTest.java | 28 +++++++++++++++++++ 2 files changed, 28 insertions(+), 24 deletions(-) create mode 100644 src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerUnitTest.java diff --git a/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerJUnit5Test.java b/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerJUnit5Test.java index 015a23d..7b36ca9 100644 --- a/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerJUnit5Test.java +++ b/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerJUnit5Test.java @@ -56,28 +56,4 @@ public void helloWorldFromFile() throws Exception { .as("Wrong response body") .contains("Hello, world!"); } - - @Test - public void defaultBannerDisabled() { - WireMockContainer wireMockContainerSpy = Mockito.spy(new WireMockContainer("2.35.0")); - - wireMockContainerSpy.configure(); - - verify(wireMockContainerSpy).withCliArg("--disable-banner"); - } - - @ParameterizedTest - @ValueSource(strings = { - "--verbose", - " --verbose ", - "--verbose --help" - }) - public void showBannerWhenStartingVerbose(String verboseArg) { - WireMockContainer wireMockContainerSpy = Mockito.spy(new WireMockContainer("2.35.0")); - wireMockContainerSpy.withCliArg(verboseArg); - - wireMockContainerSpy.configure(); - - verify(wireMockContainerSpy, times(0)).withCliArg("--disable-banner"); - } } 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..3421344 --- /dev/null +++ b/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerUnitTest.java @@ -0,0 +1,28 @@ +package org.wiremock.integrations.testcontainers; + +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +public class WireMockContainerUnitTest { + + @Test + public void defaultBannerDisabled() { + WireMockContainer wireMockContainerSpy = Mockito.spy(new WireMockContainer("2.35.0")); + + wireMockContainerSpy.configure(); + + verify(wireMockContainerSpy).withCliArg("--disable-banner"); + } + + @Test + public void enableBanner() { + WireMockContainer wireMockContainerSpy = Mockito.spy(new WireMockContainer("2.35.0")).withBanner(); + + wireMockContainerSpy.configure(); + + verify(wireMockContainerSpy, times(0)).withCliArg("--disable-banner"); + } +} From 12115da88cd2fa7e02cfc3d83b4eae8572efa2b0 Mon Sep 17 00:00:00 2001 From: Julian Akkaya Date: Tue, 23 May 2023 17:58:00 +0200 Subject: [PATCH 6/7] Remove mockito dependency --- pom.xml | 6 ----- .../WireMockContainerJUnit5Test.java | 3 --- .../WireMockContainerUnitTest.java | 23 +++++++++++-------- 3 files changed, 13 insertions(+), 19 deletions(-) diff --git a/pom.xml b/pom.xml index eaf2cb5..999f9d7 100644 --- a/pom.xml +++ b/pom.xml @@ -94,12 +94,6 @@ provided true - - org.mockito - mockito-core - 5.3.1 - test - diff --git a/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerJUnit5Test.java b/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerJUnit5Test.java index 7b36ca9..fcb24e8 100644 --- a/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerJUnit5Test.java +++ b/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerJUnit5Test.java @@ -3,7 +3,6 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; -import org.mockito.Mockito; import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Testcontainers; import org.wiremock.integrations.testcontainers.testsupport.http.TestHttpClient; @@ -11,8 +10,6 @@ import java.net.http.HttpResponse; import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; @Testcontainers public class WireMockContainerJUnit5Test { diff --git a/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerUnitTest.java b/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerUnitTest.java index 3421344..6596b9c 100644 --- a/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerUnitTest.java +++ b/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerUnitTest.java @@ -1,28 +1,31 @@ package org.wiremock.integrations.testcontainers; import org.junit.jupiter.api.Test; -import org.mockito.Mockito; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; +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 defaultBannerDisabled() { - WireMockContainer wireMockContainerSpy = Mockito.spy(new WireMockContainer("2.35.0")); + public void bannerIsByDefaultDisabled() { + WireMockContainer wireMockContainer = new WireMockContainer("2.35.0"); + wireMockContainer.configure(); - wireMockContainerSpy.configure(); + String[] startUpArgs = wireMockContainer.getCommandParts(); - verify(wireMockContainerSpy).withCliArg("--disable-banner"); + assertTrue(Arrays.asList(startUpArgs).contains("--disable-banner")); } @Test public void enableBanner() { - WireMockContainer wireMockContainerSpy = Mockito.spy(new WireMockContainer("2.35.0")).withBanner(); - + WireMockContainer wireMockContainerSpy = new WireMockContainer("2.35.0").withBanner(); wireMockContainerSpy.configure(); - verify(wireMockContainerSpy, times(0)).withCliArg("--disable-banner"); + String[] startUpArgs = wireMockContainerSpy.getCommandParts(); + + assertFalse(Arrays.asList(startUpArgs).contains("--disable-banner")); } } From a6355f379735cb54b3b0c7efa3321f9c248d7530 Mon Sep 17 00:00:00 2001 From: Julian Akkaya Date: Tue, 23 May 2023 18:20:55 +0200 Subject: [PATCH 7/7] Add a method "withBanner" to enable the banner --- .../testcontainers/WireMockContainer.java | 13 +++++++++++-- .../testcontainers/WireMockContainerUnitTest.java | 15 ++++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/wiremock/integrations/testcontainers/WireMockContainer.java b/src/main/java/org/wiremock/integrations/testcontainers/WireMockContainer.java index 64eea17..e3f9104 100644 --- a/src/main/java/org/wiremock/integrations/testcontainers/WireMockContainer.java +++ b/src/main/java/org/wiremock/integrations/testcontainers/WireMockContainer.java @@ -74,9 +74,18 @@ public WireMockContainer(String image, String version) { wireMockArgs = new StringBuilder(); setWaitStrategy(DEFAULT_WAITER); } - + + /** + * Disables the banner when starting the WireMock container. + * @return this instance + */ + public WireMockContainer withoutBanner() { + isBannerDisabled = true; + return this; + } + /** - * Enables the banner when starting WireMock container. + * Enable the banner when starting the WireMock container. * @return this instance */ public WireMockContainer withBanner() { diff --git a/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerUnitTest.java b/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerUnitTest.java index 6596b9c..5efebd9 100644 --- a/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerUnitTest.java +++ b/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerUnitTest.java @@ -21,11 +21,24 @@ public void bannerIsByDefaultDisabled() { @Test public void enableBanner() { - WireMockContainer wireMockContainerSpy = new WireMockContainer("2.35.0").withBanner(); + 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")); + } }