From 6d9e4a1ee9940d649db9fd3671ee1615da30a3dd Mon Sep 17 00:00:00 2001 From: Tomas Bjerre Date: Sun, 6 Oct 2024 10:23:38 +0200 Subject: [PATCH] feat: enable predefining port with property (refs #33) --- CHANGELOG.md | 8 +-- README.md | 2 +- .../wiremock/spring/ConfigureWireMock.java | 8 +++ .../internal/WireMockServerCreator.java | 27 +++++++- .../java/app/UsePortFromPropertyTest.java | 63 +++++++++++++++++++ 5 files changed, 100 insertions(+), 8 deletions(-) create mode 100644 wiremock-spring-boot-example/src/test/java/app/UsePortFromPropertyTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 59bffe6..cc519c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,4 @@ -# wiremock-spring-boot changelog - -Changelog of wiremock-spring-boot. - -## 3.0.0 (2024-10-01) +## 3.0.0 ### Breaking changes @@ -11,6 +7,7 @@ Changelog of wiremock-spring-boot. ### Features +- enable predefining port with property ([2672b](https://github.com/wiremock/wiremock-spring-boot/commit/2672b2120888a7c) Tomas Bjerre) [#33](https://github.com/wiremock/wiremock-spring-boot/issues/33) - simplifies configuration of most simple test case ([215aa](https://github.com/wiremock/wiremock-spring-boot/commit/215aaff1c87a83f) Tomas Bjerre) - prefixing logs from each mock with their names ([66da1](https://github.com/wiremock/wiremock-spring-boot/commit/66da1e556acbb79) Tomas Bjerre) - logging properties and where mappings were found ([7a425](https://github.com/wiremock/wiremock-spring-boot/commit/7a4255030c3b4e5) Tomas Bjerre) @@ -93,7 +90,6 @@ Changelog of wiremock-spring-boot. [a0ddd](https://github.com/wiremock/wiremock-spring-boot/commit/a0ddd4b200af13c) felgentraeger *2024-03-05 07:22:08* - ## v2.1.3 (2024-09-24) ### Other changes diff --git a/README.md b/README.md index 4693ced..f5e4ac9 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ * Automatically sets Spring environment properties * Does not pollute Spring application context with extra beans -It is originally forked from [Wiremock Spring Boot](https://github.com/maciejwalkowiak/wiremock-spring-boot). +It is originally forked from [WireMock Spring Boot](https://github.com/maciejwalkowiak/wiremock-spring-boot). ## How to install diff --git a/src/main/java/org/wiremock/spring/ConfigureWireMock.java b/src/main/java/org/wiremock/spring/ConfigureWireMock.java index f4ab9b9..4896b4c 100644 --- a/src/main/java/org/wiremock/spring/ConfigureWireMock.java +++ b/src/main/java/org/wiremock/spring/ConfigureWireMock.java @@ -29,6 +29,14 @@ */ String name() default "wiremock"; + /** + * If the port property is found in Spring {@link org.springframework.context.ApplicationContext} + * it will be used. Enables a user to predefine a static port with a property. + * + * @return true if enabled, else false. + */ + boolean usePortFromPredefinedPropertyIfFound() default false; + /** * Names of Spring properties to inject the {@link WireMockServer#port()} * diff --git a/src/main/java/org/wiremock/spring/internal/WireMockServerCreator.java b/src/main/java/org/wiremock/spring/internal/WireMockServerCreator.java index 919bc9f..b48ea67 100644 --- a/src/main/java/org/wiremock/spring/internal/WireMockServerCreator.java +++ b/src/main/java/org/wiremock/spring/internal/WireMockServerCreator.java @@ -18,6 +18,7 @@ import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.event.ContextClosedEvent; +import org.springframework.core.env.ConfigurableEnvironment; import org.wiremock.spring.ConfigureWireMock; import org.wiremock.spring.WireMockConfigurationCustomizer; @@ -30,8 +31,10 @@ public WireMockServerCreator(final String name) { public WireMockServer createWireMockServer( final ConfigurableApplicationContext context, final ConfigureWireMock options) { + final int serverPort = this.getServerProperty(context.getEnvironment(), options); + final WireMockConfiguration serverOptions = - options().port(options.port()).notifier(new Slf4jNotifier(options.name())); + options().port(serverPort).notifier(new Slf4jNotifier(options.name())); this.configureFilesUnderClasspath(options.filesUnderClasspath(), "/" + options.name()) .ifPresentOrElse( present -> this.usingFilesUnderClasspath(serverOptions, present), @@ -107,6 +110,28 @@ public WireMockServer createWireMockServer( return newServer; } + private int getServerProperty( + final ConfigurableEnvironment environment, final ConfigureWireMock options) { + if (!options.usePortFromPredefinedPropertyIfFound()) { + return options.port(); + } + return Arrays.stream(options.portProperties()) + .filter(StringUtils::isNotBlank) + .filter(propertyName -> environment.containsProperty(propertyName)) + .map( + propertyName -> { + final int predefinedPropertyValue = + Integer.parseInt(environment.getProperty(propertyName)); + this.logger.info( + "Found predefined port in property with name '{}' on port: {}", + propertyName, + predefinedPropertyValue); + return predefinedPropertyValue; + }) + .findFirst() + .orElse(options.port()); + } + private WireMockConfiguration usingFilesUnderClasspath( final WireMockConfiguration serverOptions, final String resource) { this.logger.info("Serving WireMock mappings from classpath resource: " + resource); diff --git a/wiremock-spring-boot-example/src/test/java/app/UsePortFromPropertyTest.java b/wiremock-spring-boot-example/src/test/java/app/UsePortFromPropertyTest.java new file mode 100644 index 0000000..00bf9ed --- /dev/null +++ b/wiremock-spring-boot-example/src/test/java/app/UsePortFromPropertyTest.java @@ -0,0 +1,63 @@ +package app; + +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.anyRequestedFor; +import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl; +import static com.github.tomakehurst.wiremock.client.WireMock.get; +import static org.assertj.core.api.Assertions.assertThat; + +import com.github.tomakehurst.wiremock.WireMockServer; +import com.github.tomakehurst.wiremock.client.WireMock; +import io.restassured.RestAssured; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.context.SpringBootTest; +import org.wiremock.spring.ConfigureWireMock; +import org.wiremock.spring.EnableWireMock; +import org.wiremock.spring.InjectWireMock; + +@SpringBootTest(properties = {"wm1.server.port=65432", "wm2.server.port=54321"}) +@EnableWireMock({ + @ConfigureWireMock( + name = "wm1", + portProperties = "wm1.server.port", + baseUrlProperties = "wm1.server.url", + usePortFromPredefinedPropertyIfFound = true), + @ConfigureWireMock(name = "wm2", portProperties = "wm2.server.port") +}) +class UsePortFromPropertyTest { + + @InjectWireMock("wm1") + private WireMockServer wm1; + + @Value("${wm1.server.port}") + private int wm1Port; + + @Value("${wm1.server.url}") + private String wm1Url; + + private WireMock wm1Client; + + @Value("${wm2.server.port}") + private int wm2Port; + + @BeforeEach + public void before() { + this.wm1Client = WireMock.create().port(this.wm1Port).build(); + } + + @Test + void testThatPortCanBePredefinedInPropertyAndOptionallyDisabled() { + assertThat(this.wm1Port).isEqualTo(65432); + assertThat(this.wm1Url).contains(String.valueOf(this.wm1Port)); + + assertThat(this.wm2Port).isNotEqualTo(54321).isNotEqualTo(this.wm1Port); + + this.wm1Client.register(get("/wm1_configured_in_test").willReturn(aResponse().withStatus(202))); + + RestAssured.when().get(this.wm1Url + "/wm1_configured_in_test").then().statusCode(202); + + assertThat(this.wm1Client.find(anyRequestedFor(anyUrl()))).hasSize(1); + } +}