diff --git a/src/main/java/org/wiremock/integrations/testcontainers/WireMockContainer.java b/src/main/java/org/wiremock/integrations/testcontainers/WireMockContainer.java index 5c96d65..274fd5f 100644 --- a/src/main/java/org/wiremock/integrations/testcontainers/WireMockContainer.java +++ b/src/main/java/org/wiremock/integrations/testcontainers/WireMockContainer.java @@ -30,12 +30,13 @@ 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.LinkedHashSet; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -65,7 +66,8 @@ public class WireMockContainer extends GenericContainer { private final StringBuilder wireMockArgs; private final Map mappingStubs = new HashMap<>(); private final Map mappingFiles = new HashMap<>(); - private final Map extensions = new HashMap<>(); + private final Set extensionClassNames = new LinkedHashSet<>(); + private final Set extensionJars = new LinkedHashSet<>(); private boolean isBannerDisabled = true; /** @@ -111,7 +113,7 @@ public WireMockContainer withBanner() { isBannerDisabled = false; return this; } - + /** * Adds CLI argument to the WireMock call. * @param arg Argument @@ -170,51 +172,59 @@ public WireMockContainer withFileFromResource(String name, Class resource, St /** * Add extension that will be loaded from the specified JAR file. - * @param id Unique ID of the extension, for logging purposes + * @param classNames Class names of the extension to be included + * @param jar JAR to be included into the container + * @return this instance + */ + public WireMockContainer withExtension(Collection classNames, File jar) { + return withExtension(classNames, Collections.singleton(jar)); + } + + /** + * Add extension that will be loaded from the specified JAR file. * @param classNames Class names of the extension to be included * @param jars JARs to be included into the container * @return this instance */ - public WireMockContainer withExtension(String id, Collection classNames, Collection jars) { - final Extension extension = new Extension(id); - extension.extensionClassNames.addAll(classNames); - extension.jars.addAll(jars); - extensions.put(id, extension); + public WireMockContainer withExtension(Collection classNames, Collection jars) { + extensionClassNames.addAll(classNames); + extensionJars.addAll(jars); return this; } /** * Add extension that will be loaded from the specified directory with JAR files. - * @param id Unique ID of the extension, for logging purposes * @param classNames Class names of the extension to be included - * @param jarDirectory Directory that stores all JARs + * @param jarsDirectory Directory that stores all JARs * @return this instance */ - public WireMockContainer withExtension(String id, Collection classNames, File jarDirectory) { - final List jarsInTheDirectory; - try (Stream walk = Files.walk(jarDirectory.toPath())) { - jarsInTheDirectory = walk + public WireMockContainer withExtension(Collection classNames, Path jarsDirectory) { + if (!Files.isDirectory(jarsDirectory)) { + throw new IllegalArgumentException("Path must refers to directory " + jarsDirectory); + } + try (Stream walk = Files.walk(jarsDirectory)) { + + final List jarsInTheDirectory = walk .filter(p -> !Files.isDirectory(p)) .map(Path::toFile) .filter(f -> f.toString().endsWith(".jar")) .collect(Collectors.toList()); + return withExtension(classNames, jarsInTheDirectory); + } catch (IOException e) { - throw new IllegalArgumentException("Cannot list JARs in the directory " + jarDirectory, e); + throw new IllegalArgumentException("Cannot list JARs in the directory " + jarsDirectory, e); } - - return withExtension(id, classNames, jarsInTheDirectory); } /** * Add extension that will be loaded from the classpath. * This method can be used if the extension is a part of the WireMock bundle, - * or a Jar is already added via {@link #withExtension(String, Collection, Collection)}} - * @param id Unique ID of the extension, for logging purposes + * or a Jar is already added via {@link #withExtension(Collection, Collection)}} * @param className Class name of the extension * @return this instance */ - public WireMockContainer withExtension(String id, String className) { - return withExtension(id, Collections.singleton(className), Collections.emptyList()); + public WireMockContainer withExtension(String className) { + return withExtension(Collections.singleton(className), Collections.emptyList()); } public String getBaseUrl() { @@ -244,13 +254,8 @@ protected void configure() { withCopyToContainer(mount.getValue(), FILES_DIR + mount.getKey()); } - final ArrayList extensionClassNames = new ArrayList<>(); - for (Map.Entry entry : extensions.entrySet()) { - final Extension ext = entry.getValue(); - extensionClassNames.addAll(ext.extensionClassNames); - for (File jar : ext.jars) { - withCopyToContainer(MountableFile.forHostPath(jar.toPath()), EXTENSIONS_DIR + jar.getName()); - } + for (File jar : extensionJars) { + withCopyToContainer(MountableFile.forHostPath(jar.toPath()), EXTENSIONS_DIR + jar.getName()); } if (!extensionClassNames.isEmpty()) { wireMockArgs.append(" --extensions "); @@ -274,14 +279,4 @@ public Stub(String name, String json) { this.json = json; } } - - private static final class Extension { - final String id; - final List jars = new ArrayList<>(); - final List extensionClassNames = new ArrayList<>(); - - public Extension(String id) { - this.id = id; - } - } } diff --git a/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerExtensionTest.java b/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerExtensionTest.java index 440269c..fb83432 100644 --- a/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerExtensionTest.java +++ b/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerExtensionTest.java @@ -44,9 +44,9 @@ class WireMockContainerExtensionTest { .withLogConsumer(new Slf4jLogConsumer(LOGGER)) .withStartupTimeout(Duration.ofSeconds(60)) .withMapping("json-body-transformer", WireMockContainerExtensionTest.class, "json-body-transformer.json") - .withExtension("JSON Body Transformer", + .withExtension( Collections.singleton("com.ninecookies.wiremock.extensions.JsonBodyTransformer"), - Collections.singleton(Paths.get("target", "test-wiremock-extension", "wiremock-extensions-0.4.1-jar-with-dependencies.jar").toFile())); + Paths.get("target", "test-wiremock-extension", "wiremock-extensions-0.4.1-jar-with-dependencies.jar").toFile()); @Test void testJSONBodyTransformer() throws Exception { diff --git a/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerExtensionsCombinationTest.java b/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerExtensionsCombinationTest.java index 353b019..d4ebbfd 100644 --- a/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerExtensionsCombinationTest.java +++ b/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerExtensionsCombinationTest.java @@ -25,7 +25,7 @@ import org.wiremock.integrations.testcontainers.testsupport.http.TestHttpClient; import java.nio.file.Paths; -import java.util.Collections; +import java.util.Arrays; import static org.assertj.core.api.Assertions.assertThat; @@ -42,12 +42,9 @@ class WireMockContainerExtensionsCombinationTest { WireMockContainer wiremockServer = new WireMockContainer(WireMockContainer.WIREMOCK_2_LATEST) .withLogConsumer(new Slf4jLogConsumer(LOGGER)) .withMapping("json-body-transformer", WireMockContainerExtensionsCombinationTest.class, "json-body-transformer.json") - .withExtension("Webhook", - Collections.singleton("org.wiremock.webhooks.Webhooks"), - Collections.singleton(Paths.get("target", "test-wiremock-extension", "wiremock-webhooks-extension-2.35.0.jar").toFile())) - .withExtension("JSON Body Transformer", - Collections.singleton("com.ninecookies.wiremock.extensions.JsonBodyTransformer"), - Collections.singleton(Paths.get("target", "test-wiremock-extension", "wiremock-extensions-0.4.1-jar-with-dependencies.jar").toFile())); + .withExtension( + Arrays.asList("org.wiremock.webhooks.Webhooks", "com.ninecookies.wiremock.extensions.JsonBodyTransformer"), + Paths.get("target", "test-wiremock-extension")); @Test void testJSONBodyTransformer() throws Exception { diff --git a/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerExtensionsWebhookTest.java b/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerExtensionsWebhookTest.java index 0428f6b..0d9e794 100644 --- a/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerExtensionsWebhookTest.java +++ b/src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerExtensionsWebhookTest.java @@ -60,7 +60,7 @@ class WireMockContainerExtensionsWebhookTest { .withLogConsumer(new Slf4jLogConsumer(LOGGER)) .withCliArg("--global-response-templating") .withMapping("webhook-callback-template", WireMockContainerExtensionsWebhookTest.class, "webhook-callback-template.json") - .withExtension("Webhook", + .withExtension( Collections.singleton("org.wiremock.webhooks.Webhooks"), Collections.singleton(Paths.get("target", "test-wiremock-extension", "wiremock-webhooks-extension-2.35.0.jar").toFile())) .withAccessToHost(true); // Force the host access mechanism