From a0c3686292a82c43ebcafd48c672fc5c83cac012 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Wed, 9 Dec 2020 09:29:29 +0100 Subject: [PATCH] Setting current static WireMock instance when server is already running without this change when the server was already running we wouldn't set the static WireMock's instance to that running server. That would result in still pointing to a previously started WireMock instance with this change we're setting the static server instance to the running server fixes gh-1425 --- .../wiremock/WireMockApplicationListener.java | 3 +- .../wiremock/WireMockConfiguration.java | 33 +++++++++++---- ...onfigureWireMockAdditionalImportTests.java | 40 ++++++++++++------- 3 files changed, 53 insertions(+), 23 deletions(-) diff --git a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockApplicationListener.java b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockApplicationListener.java index ee8dc75cae..b8d2e940ff 100644 --- a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockApplicationListener.java +++ b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockApplicationListener.java @@ -108,7 +108,8 @@ public class WireMockApplicationListener int port = SocketUtils.findAvailableTcpPort(minPort, maxPort); source.put(portProperty, port); if (log.isDebugEnabled()) { - log.debug("Registered property source for property [" + portProperty + "] with value [" + port + "]"); + log.debug("Registered property source for property [" + portProperty + + "] with value [" + port + "]"); } source.put(dynamicPortProperty, true); } diff --git a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockConfiguration.java b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockConfiguration.java index ead892468b..91d7804e9b 100644 --- a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockConfiguration.java +++ b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockConfiguration.java @@ -144,8 +144,11 @@ public class WireMockConfiguration implements SmartLifecycle { private void printRegistrationLog() { int httpPort = this.server.isRunning() ? this.server.port() : -1; - int httpsPort = this.server.isRunning() && this.server.getOptions().httpsSettings().enabled() ? this.server.httpsPort() : -1; - log.debug("Registering WireMock [" + this.server + "] at http port [" + httpPort + "] and https port [" + httpsPort + "]"); + int httpsPort = this.server.isRunning() + && this.server.getOptions().httpsSettings().enabled() + ? this.server.httpsPort() : -1; + log.debug("Registering WireMock [" + this.server + "] at http port [" + httpPort + + "] and https port [" + httpsPort + "]"); } private void reRegisterServer() { @@ -165,15 +168,21 @@ public class WireMockConfiguration implements SmartLifecycle { this.server = new WireMockServer(this.options); if (log.isDebugEnabled()) { int httpPort = this.server.isRunning() ? this.server.port() : -1; - int httpsPort = this.server.isRunning() && this.server.getOptions().httpsSettings().enabled() ? this.server.httpsPort() : -1; - log.debug("Created new server [" + this.server + "] at http port [" + httpPort + "] and https port [" + httpsPort + "]"); + int httpsPort = this.server.isRunning() + && this.server.getOptions().httpsSettings().enabled() + ? this.server.httpsPort() : -1; + log.debug("Created new server [" + this.server + "] at http port [" + + httpPort + "] and https port [" + httpsPort + "]"); } } start(); if (log.isDebugEnabled()) { int httpPort = this.server.isRunning() ? this.server.port() : -1; - int httpsPort = this.server.isRunning() && this.server.getOptions().httpsSettings().enabled() ? this.server.httpsPort() : -1; - log.debug("Started server [" + this.server + "] at http port [" + httpPort + "] and https port [" + httpsPort + "]"); + int httpsPort = this.server.isRunning() + && this.server.getOptions().httpsSettings().enabled() + ? this.server.httpsPort() : -1; + log.debug("Started server [" + this.server + "] at http port [" + httpPort + + "] and https port [" + httpsPort + "]"); } logRegisteredMappings(); } @@ -265,11 +274,14 @@ public class WireMockConfiguration implements SmartLifecycle { public void start() { if (isRunning()) { int httpPort = this.server.isRunning() ? this.server.port() : -1; - int httpsPort = this.server.isRunning() && this.server.getOptions().httpsSettings().enabled() ? this.server.httpsPort() : -1; + int httpsPort = this.server.isRunning() + && this.server.getOptions().httpsSettings().enabled() + ? this.server.httpsPort() : -1; if (log.isDebugEnabled()) { log.debug("Server [" + this.server + "] is already running at http port [" + httpPort + "] / https port [" + httpsPort + "]"); } + updateCurrentServer(); return; } this.server.start(); @@ -280,7 +292,12 @@ public class WireMockConfiguration implements SmartLifecycle { WireMock.configureFor(new WireMock(this.server)); this.running = true; if (log.isDebugEnabled() && this.server.isRunning()) { - log.debug("Started WireMock at port [" + this.server.port() + "]. It has [" + int httpPort = this.server.isRunning() ? this.server.port() : -1; + int httpsPort = this.server.isRunning() + && this.server.getOptions().httpsSettings().enabled() + ? this.server.httpsPort() : -1; + log.debug("Server [" + this.server + "] is already running at http port [" + + httpPort + "] / https port [" + httpsPort + "]. It has [" + this.server.getStubMappings().size() + "] mappings registered"); } } diff --git a/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockAdditionalImportTests.java b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockAdditionalImportTests.java index 2673a1e6a7..d3ea6d5537 100644 --- a/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockAdditionalImportTests.java +++ b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockAdditionalImportTests.java @@ -42,7 +42,9 @@ import org.springframework.web.bind.annotation.RestController; import org.springframework.web.reactive.function.client.WebClient; // issue 1541 -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = TestConfiguration.class, properties = "base-url=http://localhost:${wiremock.server.port}") +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, + classes = TestConfiguration.class, + properties = "base-url=http://localhost:${wiremock.server.port}") @AutoConfigureWireMock(port = 0) @Import(ExtraConfig.class) public class AutoConfigureWireMockAdditionalImportTests { @@ -53,7 +55,9 @@ public class AutoConfigureWireMockAdditionalImportTests { } @Nested - @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = TestConfiguration.class, properties = "base-url=http://localhost:${wiremock.server.port}") + @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, + classes = TestConfiguration.class, + properties = "base-url=http://localhost:${wiremock.server.port}") @AutoConfigureWireMock(port = 0) class SecondControllerTest { @@ -61,17 +65,21 @@ public class AutoConfigureWireMockAdditionalImportTests { void test(@Autowired WebTestClient webTestClient) { // arrange WireMock.stubFor(WireMock.get(WireMock.urlMatching("/find-all")) - .willReturn(ResponseDefinitionBuilder.okForJson(Collections.singletonList(new TestItem("my-name"))))); + .willReturn(ResponseDefinitionBuilder.okForJson( + Collections.singletonList(new TestItem("my-name"))))); // act - List responseBody = webTestClient.get().uri("find-all") - .exchange().expectStatus().is2xxSuccessful() - .expectBody(new ParameterizedTypeReference>() { }).returnResult().getResponseBody(); + List responseBody = webTestClient.get().uri("find-all").exchange() + .expectStatus().is2xxSuccessful() + .expectBody(new ParameterizedTypeReference>() { + }).returnResult().getResponseBody(); // assert Assertions.assertThat(responseBody.get(0).getName()).isEqualTo("my-name"); } + } + } @Component @@ -89,7 +97,8 @@ class TestConfiguration { } @Bean - TestController testController(WebClient webClient, @Value("${base-url}") String baseUrl) { + TestController testController(WebClient webClient, + @Value("${base-url}") String baseUrl) { return new TestController(webClient, baseUrl); } @@ -105,16 +114,17 @@ class TestController { TestController(WebClient webClient, String baseUrl) { this.webClient = webClient; this.baseUrl = baseUrl; - System.out.println("Creating with URL [" + this.baseUrl + "] HASH [" + this.hashCode() + "]"); + System.out.println("Creating with URL [" + this.baseUrl + "] HASH [" + + this.hashCode() + "]"); } @GetMapping("find-all") public Mono> findAll() { - System.out.println("Will send a request to [" + this.baseUrl + "] HASH [" + this.hashCode() + "]"); - return webClient - .get() - .uri(baseUrl + "/find-all") - .retrieve().bodyToMono(new ParameterizedTypeReference>() {}); + System.out.println("Will send a request to [" + this.baseUrl + "] HASH [" + + this.hashCode() + "]"); + return webClient.get().uri(baseUrl + "/find-all").retrieve() + .bodyToMono(new ParameterizedTypeReference>() { + }); } } @@ -123,7 +133,8 @@ class TestItem { private String name; - TestItem() { } + TestItem() { + } TestItem(String name) { this.name = name; @@ -136,4 +147,5 @@ class TestItem { public void setName(String name) { this.name = name; } + }