Skip to content

Commit

Permalink
feat: enable predefining port with property (refs #33)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Oct 6, 2024
1 parent f2c35d0 commit 6d9e4a1
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 8 deletions.
8 changes: 2 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
# wiremock-spring-boot changelog

Changelog of wiremock-spring-boot.

## 3.0.0 (2024-10-01)
## 3.0.0

### Breaking changes

Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/wiremock/spring/ConfigureWireMock.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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),
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 6d9e4a1

Please sign in to comment.