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

Add option to customize configuration on @ConfigureWireMock annotation #18

Merged
merged 3 commits into from
Oct 29, 2023
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
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,33 @@ It can be changed with setting `stubLocation` on `@ConfigureWireMock`:
@ConfigureWireMock(name = "...", property = "...", stubLocation = "my-stubs")
```

### Advanced configuration

More advanced configuration can be applied through configuration customizers:

```java
@ConfigureWireMock(
name = "todo-service",
property = "todo-service.url",
configurationCustomizers = SampleConfigurationCustomizer.class
)
```

Where `SampleConfigurationCustomizer` is a class implementing `WireMockConfigurationCustomizer`:

```java
class SampleConfigurationCustomizer implements WireMockConfigurationCustomizer {

@Override
public void customize(WireMockConfiguration configuration, ConfigureWireMock options) {
// apply changes to configuration
}
}
```

> [!IMPORTANT]
> `WireMockConfigurationCustomizer` must have a no-arg constructor.

Sounds good? Consider [❤️ Sponsoring](https://github.com/sponsors/maciejwalkowiak) the project! Thank you!

## 🙏 Credits
Expand Down
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,59 @@
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 = WireMockConfigurationCustomizerTest.AppConfiguration.class)
@EnableWireMock({
@ConfigureWireMock(
name = "user-service",
property = "user-service.url",
configurationCustomizers = WireMockConfigurationCustomizerTest.SampleConfigurationCustomizer.class
),
@ConfigureWireMock(
name = "todo-service",
property = "todo-service.url",
configurationCustomizers = WireMockConfigurationCustomizerTest.SampleConfigurationCustomizer.class
),
})
class WireMockConfigurationCustomizerTest {
private static final int USER_SERVICE_PORT = TestSocketUtils.findAvailableTcpPort();
private static final int TODO_SERVICE_PORT = TestSocketUtils.findAvailableTcpPort();

static class SampleConfigurationCustomizer implements WireMockConfigurationCustomizer {

@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 appliesConfigurationCustomizer() {
assertThat(userService.port()).isEqualTo(USER_SERVICE_PORT);
assertThat(todoService.port()).isEqualTo(TODO_SERVICE_PORT);
}

}
Loading