Skip to content

Commit 78a0bd2

Browse files
Merge pull request #237 from wiremock/simplify-ext-config-v2
Simplify Extentions api - Remove overhead with id and plugins
2 parents 982e75a + ec8f8e5 commit 78a0bd2

5 files changed

Lines changed: 49 additions & 49 deletions

File tree

src/main/java/org/wiremock/integrations/testcontainers/WireMockContainer.java

Lines changed: 41 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@
3030
import java.nio.charset.StandardCharsets;
3131
import java.nio.file.Files;
3232
import java.nio.file.Path;
33-
import java.util.ArrayList;
3433
import java.util.Collection;
3534
import java.util.Collections;
3635
import java.util.HashMap;
36+
import java.util.LinkedHashSet;
3737
import java.util.List;
3838
import java.util.Map;
39+
import java.util.Set;
3940
import java.util.stream.Collectors;
4041
import java.util.stream.Stream;
4142

@@ -81,7 +82,8 @@ public class WireMockContainer extends GenericContainer<WireMockContainer> {
8182
private final StringBuilder wireMockArgs;
8283
private final Map<String, Stub> mappingStubs = new HashMap<>();
8384
private final Map<String, MountableFile> mappingFiles = new HashMap<>();
84-
private final Map<String, WireMockPlugin> plugins = new HashMap<>();
85+
private final Set<String> extensionClassNames = new LinkedHashSet<>();
86+
private final Set<File> extensionJars = new LinkedHashSet<>();
8587
private boolean isBannerDisabled = true;
8688

8789
private File rootDir = new File("src/test/resources");
@@ -292,74 +294,74 @@ public WireMockContainer withFileFromResource(Class<?> resource, String filename
292294
}
293295

294296
/**
295-
* Add extension that will be loaded from the specified JAR files.
296-
* In the internal engine, it will be handled as a single plugin.
297+
* Add an extension that will be loaded from the specified JAR file.
297298
* @param classNames Class names of the extension to be included
298-
* @param jars JARs to be included into the container
299+
* @param jar JAR to be included into the container
299300
* @return this instance
300301
*/
301-
public WireMockContainer withExtensions(Collection<String> classNames, Collection<File> jars) {
302-
return withExtensions(WireMockPlugin.guessPluginId(classNames, jars), classNames, jars);
302+
public WireMockContainer withExtension(Collection<String> classNames, File jar) {
303+
return withExtensions(classNames, Collections.singleton(jar));
304+
}
305+
306+
// TODO: Add actual usage for the extension name
307+
/**
308+
* Add an extension that will be loaded from the specified JAR file.
309+
* @param extensionName Name of the extension to be included
310+
* @param classNames Class names of the extension to be included
311+
* @param jars JAR to be included in the container
312+
* @return this instance
313+
*/
314+
public WireMockContainer withExtension(String extensionName, Collection<String> classNames, Collection<File> jars) {
315+
return withExtensions(classNames, jars);
303316
}
304317

305318
/**
306-
* Add extension that will be loaded from the specified JAR files.
307-
* In the internal engine, it will be handled as a single plugin.
308-
* @param id Identifier top use
319+
* Add extension that will be loaded from the specified JAR file.
309320
* @param classNames Class names of the extension to be included
310321
* @param jars JARs to be included into the container
311322
* @return this instance
312323
*/
313-
public WireMockContainer withExtensions(String id, Collection<String> classNames, Collection<File> jars) {
314-
final WireMockPlugin extension = new WireMockPlugin(id)
315-
.withExtensions(classNames)
316-
.withJars(jars);
317-
return withPlugin(extension);
324+
public WireMockContainer withExtensions(Collection<String> classNames, Collection<File> jars) {
325+
extensionClassNames.addAll(classNames);
326+
extensionJars.addAll(jars);
327+
return this;
318328
}
319329

320330
/**
321-
* Add extension that will be loaded from the specified directory with JAR files.
322-
* In the internal engine, it will be handled as a single plugin.
331+
* Add an extension or multiple extensions that will be loaded from the specified directory with JAR files.
323332
* @param classNames Class names of the extension to be included
324-
* @param jarDirectory Directory that stores all JARs
333+
* @param jarsDirectory Directory that stores all JARs
325334
* @return this instance
326335
*/
327-
public WireMockContainer withExtensions(Collection<String> classNames, File jarDirectory) {
328-
final List<File> jarsInTheDirectory;
329-
try (Stream<Path> walk = Files.walk(jarDirectory.toPath())) {
330-
jarsInTheDirectory = walk
336+
public WireMockContainer withExtensions(Collection<String> classNames, Path jarsDirectory) {
337+
if (!Files.isDirectory(jarsDirectory)) {
338+
throw new IllegalArgumentException("Path must refers to directory " + jarsDirectory);
339+
}
340+
try (Stream<Path> walk = Files.walk(jarsDirectory)) {
341+
342+
final List<File> jarsInTheDirectory = walk
331343
.filter(p -> !Files.isDirectory(p))
332344
.map(Path::toFile)
333345
.filter(f -> f.toString().endsWith(".jar"))
334346
.collect(Collectors.toList());
347+
return withExtensions(classNames, jarsInTheDirectory);
348+
335349
} catch (IOException e) {
336-
throw new IllegalArgumentException("Cannot list JARs in the directory " + jarDirectory, e);
350+
throw new IllegalArgumentException("Cannot list JARs in the directory " + jarsDirectory, e);
337351
}
338-
339-
return withExtensions(classNames, jarsInTheDirectory);
340352
}
341353

342354
/**
343355
* Add extension that will be loaded from the classpath.
344356
* This method can be used if the extension is a part of the WireMock bundle,
345-
* or a Jar is already added via {@link #withExtensions(Collection, Collection)}}.
346-
* In the internal engine, it will be handled as a single plugin.
357+
* or a Jar is already added via {@link #withExtensions(Collection, Collection)}}
347358
* @param className Class name of the extension
348359
* @return this instance
349360
*/
350361
public WireMockContainer withExtension(String className) {
351362
return withExtensions(Collections.singleton(className), Collections.emptyList());
352363
}
353364

354-
private WireMockContainer withPlugin(WireMockPlugin plugin) {
355-
String pluginId = plugin.getPluginId();
356-
if (plugins.containsKey(pluginId)) {
357-
throw new IllegalArgumentException("The plugin is already included: " + pluginId);
358-
}
359-
plugins.put(pluginId, plugin);
360-
return this;
361-
}
362-
363365
public String getBaseUrl() {
364366
return String.format("http://%s:%d", getHost(), getPort());
365367
}
@@ -390,14 +392,10 @@ protected void configure() {
390392
withCopyToContainer(mount.getValue(), CONTAINER_FILES_DIR + mount.getKey());
391393
}
392394

393-
final ArrayList<String> extensionClassNames = new ArrayList<>();
394-
for (Map.Entry<String, WireMockPlugin> entry : plugins.entrySet()) {
395-
final WireMockPlugin ext = entry.getValue();
396-
extensionClassNames.addAll(ext.getExtensionClassNames());
397-
for (File jar : ext.getJars()) {
398-
withCopyToContainer(MountableFile.forHostPath(jar.toPath()), EXTENSIONS_DIR + jar.getName());
399-
}
395+
for (File jar : extensionJars) {
396+
withCopyToContainer(MountableFile.forHostPath(jar.toPath()), EXTENSIONS_DIR + jar.getName());
400397
}
398+
401399
if (!extensionClassNames.isEmpty()) {
402400
wireMockArgs.append(" --extensions ");
403401
wireMockArgs.append(String.join(",", extensionClassNames));

src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerExtensionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class WireMockContainerExtensionTest {
4141
.withLogConsumer(new Slf4jLogConsumer(LOGGER))
4242
.withStartupTimeout(Duration.ofSeconds(60))
4343
.withMapping("json-body-transformer", WireMockContainerExtensionTest.class, "json-body-transformer.json")
44-
.withExtensions("JSON Body Transformer",
44+
.withExtension("JSON Body Transformer",
4545
Collections.singleton("com.ninecookies.wiremock.extensions.JsonBodyTransformer"),
4646
Collections.singleton(Paths.get("target", "test-wiremock-extension", "wiremock-extensions-0.4.1-jar-with-dependencies.jar").toFile()));
4747

src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerExtensionsCombinationTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.wiremock.integrations.testcontainers.testsupport.http.TestHttpClient;
2525

2626
import java.nio.file.Paths;
27+
import java.util.Arrays;
2728
import java.util.Collections;
2829

2930
import static org.assertj.core.api.Assertions.assertThat;
@@ -39,10 +40,11 @@ class WireMockContainerExtensionsCombinationTest {
3940
WireMockContainer wiremockServer = new WireMockContainer(TestConfig.WIREMOCK_DEFAULT_IMAGE)
4041
.withLogConsumer(new Slf4jLogConsumer(LOGGER))
4142
.withMapping("json-body-transformer", WireMockContainerExtensionsCombinationTest.class, "json-body-transformer.json")
42-
.withExtensions("Webhook",
43+
44+
.withExtension("Webhook",
4345
Collections.singleton("org.wiremock.webhooks.Webhooks"),
4446
Collections.singleton(Paths.get("target", "test-wiremock-extension", "wiremock-webhooks-extension-3.5.4.jar").toFile()))
45-
.withExtensions("JSON Body Transformer",
47+
.withExtension("JSON Body Transformer",
4648
Collections.singleton("com.ninecookies.wiremock.extensions.JsonBodyTransformer"),
4749
Collections.singleton(Paths.get("target", "test-wiremock-extension", "wiremock-extensions-0.4.1-jar-with-dependencies.jar").toFile()));
4850

src/test/java/org/wiremock/integrations/testcontainers/wiremock2/WebhooksExtensionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class WebhooksExtensionTest {
5959
.withLogConsumer(new Slf4jLogConsumer(LOGGER))
6060
.withCliArg("--global-response-templating")
6161
.withMapping("webhook-callback-template", WebhooksExtensionTest.class, "webhook-callback-template.json")
62-
.withExtensions("Webhooks",
62+
.withExtension("Webhooks",
6363
Collections.singleton("org.wiremock.webhooks.Webhooks"),
6464
Collections.singleton(Paths.get("target", "test-wiremock-extension", "wiremock-webhooks-extension-2.35.0.jar").toFile()))
6565
.withAccessToHost(true); // Force the host access mechanism

src/test/java/org/wiremock/integrations/testcontainers/wiremock2/WireMockContainerExtensionsCombinationTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ class WireMockContainerExtensionsCombinationTest {
4141
WireMockContainer wiremockServer = new WireMockContainer(TestConfig.WIREMOCK_2_IMAGE)
4242
.withLogConsumer(new Slf4jLogConsumer(LOGGER))
4343
.withMapping("json-body-transformer", WireMockContainerExtensionsCombinationTest.class, "json-body-transformer.json")
44-
.withExtensions("Webhook",
44+
.withExtension("Webhook",
4545
Collections.singleton("org.wiremock.webhooks.Webhooks"),
4646
Collections.singleton(Paths.get("target", "test-wiremock-extension", "wiremock-webhooks-extension-2.35.0.jar").toFile()))
47-
.withExtensions("JSON Body Transformer",
47+
.withExtension("JSON Body Transformer",
4848
Collections.singleton("com.ninecookies.wiremock.extensions.JsonBodyTransformer"),
4949
Collections.singleton(Paths.get("target", "test-wiremock-extension", "wiremock-extensions-0.4.1-jar-with-dependencies.jar").toFile()));
5050

0 commit comments

Comments
 (0)