Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -50,19 +50,16 @@ public class WireMockContainer extends GenericContainer<WireMockContainer> {
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<String, Stub> mappingStubs = new HashMap<>();
private final Map<String, MountableFile> mappingFiles = new HashMap<>();
private final Map<String, Extension> extensions = new HashMap<>();
private boolean isBannerDisabled = true;

public WireMockContainer() {
this(DEFAULT_TAG);
Expand All @@ -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;
Comment thread
julian-michelmann marked this conversation as resolved.
return this;
}

/**
* Adds CLI argument to the WireMock call.
* @param arg Argument
Expand Down Expand Up @@ -223,6 +238,10 @@ protected void configure() {
wireMockArgs.append(String.join(",", extensionClassNames));
}

if (isBannerDisabled) {
this.withCliArg("--disable-banner");
}

// Add CLI arguments
withCommand(wireMockArgs.toString());
}
Expand All @@ -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;
}
Expand All @@ -246,5 +265,4 @@ public Extension(String id) {
this.id = id;
}
}

}
Original file line number Diff line number Diff line change
@@ -1,55 +1,56 @@
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.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;

@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<String> 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<String> 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<String> 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<String> response = TestHttpClient.newInstance().get(url);

// then
assertThat(response.body())
.as("Wrong response body")
.contains("Hello, world!");
}
}
Original file line number Diff line number Diff line change
@@ -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"));
}
}