Skip to content

Commit

Permalink
Add option to customize configuration on @ConfigureWireMock annotat…
Browse files Browse the repository at this point in the history
…ion.
  • Loading branch information
maciejwalkowiak committed Oct 29, 2023
1 parent 4fd8a99 commit 586091b
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.lang.annotation.RetentionPolicy;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import com.github.tomakehurst.wiremock.extension.Extension;

/**
Expand Down Expand Up @@ -50,4 +51,11 @@
* @return the extensions
*/
Class<? extends Extension>[] extensions() default {};

/**
* Customizes {@link WireMockConfiguration} used by {@link WireMockServer} instance. Customizers are ordered by their natural order in this array. Each customizer must have no-arg constructor.
*
* @return the configuration customizers classes
*/
Class<? extends WireMockConfigurationCustomizer>[] configurationCustomizers() default {};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.maciejwalkowiak.wiremock.spring;

import com.github.tomakehurst.wiremock.core.WireMockConfiguration;

/**
* Customizes {@link WireMockConfiguration} programmatically. Can be registered with {@link ConfigureWireMock#configurationCustomizers()}.
* Customizer must have public no-arg constructor.
*/
public interface WireMockConfigurationCustomizer {

void customize(WireMockConfiguration configuration, ConfigureWireMock options);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import org.junit.platform.commons.util.ReflectionUtils;
import org.junit.platform.commons.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -56,8 +57,6 @@ public void customizeContext(ConfigurableApplicationContext context, MergedConte
}

private void resolveOrCreateWireMockServer(ConfigurableApplicationContext context, ConfigureWireMock options) {
LOGGER.info("Configuring WireMockServer with name '{}' on port: {}", options.name(), options.port());

WireMockServer wireMockServer = Store.INSTANCE.findWireMockInstance(context, options.name());

if (wireMockServer == null) {
Expand All @@ -70,6 +69,10 @@ private void resolveOrCreateWireMockServer(ConfigurableApplicationContext contex
serverOptions.extensions(options.extensions());
}

applyCustomizers(options, serverOptions);

LOGGER.info("Configuring WireMockServer with name '{}' on port: {}", options.name(), serverOptions.portNumber());

WireMockServer newServer = new WireMockServer(serverOptions);
newServer.start();

Expand All @@ -95,6 +98,19 @@ private void resolveOrCreateWireMockServer(ConfigurableApplicationContext contex
}
}

private static void applyCustomizers(ConfigureWireMock options, WireMockConfiguration serverOptions) {
for (Class<? extends WireMockConfigurationCustomizer> customizer : options.configurationCustomizers()) {
try {
ReflectionUtils.newInstance(customizer).customize(serverOptions, options);
} catch (Exception e) {
if (e instanceof NoSuchMethodException) {
LOGGER.error("Customizer {} must have a no-arg constructor", customizer, e);
}
throw e;
}
}
}

private String resolveStubLocation(ConfigureWireMock options) {
return StringUtils.isBlank(options.stubLocation()) ? "wiremock/" + options.name() : options.stubLocation();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.maciejwalkowiak.wiremock.spring;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import org.junit.jupiter.api.Test;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.util.TestSocketUtils;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest(classes = WireMockConfigurationCustomizerTests.AppConfiguration.class)
@EnableWireMock({
@ConfigureWireMock(
name = "user-service",
property = "user-service.url",
configurationCustomizers = WireMockConfigurationCustomizerTests.SampleConfigurationCustomizer.class
),
@ConfigureWireMock(
name = "todo-service",
property = "todo-service.url",
configurationCustomizers = WireMockConfigurationCustomizerTests.SampleConfigurationCustomizer.class
),
})
class WireMockConfigurationCustomizerTests {
private static final int USER_SERVICE_PORT = TestSocketUtils.findAvailableTcpPort();
private static final int TODO_SERVICE_PORT = TestSocketUtils.findAvailableTcpPort();

static class SampleConfigurationCustomizer implements WireMockConfigurationCustomizer {

SampleConfigurationCustomizer(String foo) {
}

@Override
public void customize(WireMockConfiguration configuration, ConfigureWireMock options) {
if (options.name().equals("user-service")) {
configuration.port(USER_SERVICE_PORT);
} else {
configuration.port(TODO_SERVICE_PORT);
}
}
}

@SpringBootApplication
static class AppConfiguration {

}

@InjectWireMock("user-service")
private WireMockServer userService;

@InjectWireMock("todo-service")
private WireMockServer todoService;

@Test
void foo() {
assertThat(userService.port()).isEqualTo(USER_SERVICE_PORT);
assertThat(todoService.port()).isEqualTo(TODO_SERVICE_PORT);
}

}

0 comments on commit 586091b

Please sign in to comment.