From f7eb0894c9d2f56f6ffa21937979058893753cdb Mon Sep 17 00:00:00 2001 From: kapishmalik Date: Sat, 14 Oct 2023 21:40:20 +0530 Subject: [PATCH] Add healthcheck endpoint in waiting strategy --- .../testcontainers/WireMockContainer.java | 15 ++++++- ...ContainerWithWireMockVersionThreeTest.java | 40 +++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerWithWireMockVersionThreeTest.java diff --git a/src/main/java/org/wiremock/integrations/testcontainers/WireMockContainer.java b/src/main/java/org/wiremock/integrations/testcontainers/WireMockContainer.java index f5c08d8..cbcb9f7 100644 --- a/src/main/java/org/wiremock/integrations/testcontainers/WireMockContainer.java +++ b/src/main/java/org/wiremock/integrations/testcontainers/WireMockContainer.java @@ -50,6 +50,8 @@ public class WireMockContainer extends GenericContainer { private static final String WIREMOCK_2_LATEST_TAG = "2.35.0"; /*package*/ static final String WIREMOCK_2_MINIMUM_SUPPORTED_VERSION = "2.0.0"; + static final String WIREMOCK_HEALTH_CHECK_SUPPORT_MINIMUM_VERSION = "3.0.0-1"; + public static final DockerImageName WIREMOCK_2_LATEST = DockerImageName.parse(OFFICIAL_IMAGE_NAME).withTag(WIREMOCK_2_LATEST_TAG); @@ -61,6 +63,11 @@ public class WireMockContainer extends GenericContainer { .forHttp("/__admin/mappings") .withMethod("GET") .forStatusCode(200); + + private static final WaitStrategy HEALTH_CHECK_ENDPOINT_WAITER = Wait + .forHttp("/__admin/health") + .withMethod("GET") + .forStatusCode(200); private static final int PORT = 8080; private final StringBuilder wireMockArgs; private final Map mappingStubs = new HashMap<>(); @@ -91,7 +98,13 @@ public WireMockContainer(DockerImageName dockerImage) { } wireMockArgs = new StringBuilder(); - setWaitStrategy(DEFAULT_WAITER); + + if (version.isGreaterThanOrEqualTo(WIREMOCK_HEALTH_CHECK_SUPPORT_MINIMUM_VERSION)) { + setWaitStrategy(HEALTH_CHECK_ENDPOINT_WAITER); + } + else { + setWaitStrategy(DEFAULT_WAITER); + } } /** diff --git a/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerWithWireMockVersionThreeTest.java b/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerWithWireMockVersionThreeTest.java new file mode 100644 index 0000000..3d1710b --- /dev/null +++ b/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerWithWireMockVersionThreeTest.java @@ -0,0 +1,40 @@ +package org.wiremock.integrations.testcontainers; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.wiremock.integrations.testcontainers.testsupport.http.HttpResponse; +import org.wiremock.integrations.testcontainers.testsupport.http.TestHttpClient; + +import static org.assertj.core.api.Assertions.assertThat; + +@Testcontainers +public class WireMockContainerWithWireMockVersionThreeTest +{ + @Container + WireMockContainer wiremockServer = new WireMockContainer("wiremock/wiremock:" + WireMockContainer.WIREMOCK_HEALTH_CHECK_SUPPORT_MINIMUM_VERSION) + .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" + }) + void helloWorld(String path) throws Exception { + // given + String url = wiremockServer.getUrl(path); + + // when + HttpResponse response = new TestHttpClient().get(url); + + // then + assertThat(response.getBody()) + .as("Wrong response body") + .contains("Hello, world!"); + } +}