Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verify mappings status in WireMock test container on startup and fail by default when there're no mappings #70

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -17,8 +17,9 @@

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.fasterxml.jackson.databind.JsonNode;
import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper;
import org.testcontainers.shaded.com.google.common.io.Resources;
import org.testcontainers.utility.ComparableVersion;
import org.testcontainers.utility.DockerImageName;
Expand All @@ -36,6 +37,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -57,16 +59,13 @@ 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;
private boolean ignoreEmptyMappings = false;

/**
* Create image from the specified full image name (repo, image, tag)
Expand All @@ -91,7 +90,12 @@ public WireMockContainer(DockerImageName dockerImage) {
}

wireMockArgs = new StringBuilder();
setWaitStrategy(DEFAULT_WAITER);
setWaitStrategy(Wait
.forHttp("/__admin/mappings")
.withMethod("GET")
.forStatusCode(200)
.forResponsePredicate(new WireMockMappingsResponsePredicate())
);
}

/**
Expand All @@ -111,7 +115,23 @@ public WireMockContainer withBanner() {
isBannerDisabled = false;
return this;
}


/**
* Instructs the container to ignore empty mappings.
* Otherwise the startup will be failed if there are no mappings,
* and the test will be forced to fail too
* @param ignore value to set
* @return this instance
*/
public WireMockContainer withIgnoreEmptyMappings(boolean ignore) {
this.ignoreEmptyMappings = ignore;
return this;
}

public boolean isIgnoreEmptyMappings() {
return ignoreEmptyMappings;
}

/**
* Adds CLI argument to the WireMock call.
* @param arg Argument
Expand Down Expand Up @@ -284,4 +304,40 @@ public Extension(String id) {
this.id = id;
}
}

/**
* Verifies that the Test container is started up correctly
*/
private class WireMockMappingsResponsePredicate implements Predicate<String> {

@Override
public boolean test(String s) {
final int mappingsNumber;
try {
ObjectMapper mapper = new ObjectMapper();
JsonNode obj = mapper.readTree(s);
if (!obj.hasNonNull("mappings")) {
mappingsNumber = 0;
} else {
JsonNode mappings = obj.get("mappings");
mappingsNumber = mappings.isArray() ? mappings.size() : 0;
if (mappingsNumber == 0) {
// The container initialized but failed to load mappings
if (!WireMockContainer.this.isIgnoreEmptyMappings()) {
throw new IllegalStateException("WireMock container has initialized, but the mappings are empty. " +
"If that's desired state in the test, use WireMockContainer#withIgnoreEmptyMappings()");
}
}
}
} catch (IOException ex) {
return false;
}

return mappingsNumber > 0;
}
}

private static final class WireMockMappings {
public List<Object> mappings;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.wiremock.integrations.testcontainers;

import org.junit.jupiter.api.Test;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

@Testcontainers
public class WireMockContainerEmptyMapppingTest {

@Container
WireMockContainer wiremockServer = new WireMockContainer(WireMockContainer.WIREMOCK_2_LATEST)
.withIgnoreEmptyMappings(true);

@Test
public void shouldStartupWhenEmptyIsIgnored() {
// noop, all logic is inside the container instance
}

}
Loading