From bfbfe89351ae4c08d2562eecf4853b8c306c5ef9 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Tue, 8 Dec 2020 14:12:27 +0100 Subject: [PATCH] Added better logging messages for WireMock configuration --- .../wiremock/WireMockApplicationListener.java | 6 +- .../wiremock/WireMockConfiguration.java | 27 ++-- ...onfigureWireMockAdditionalImportTests.java | 139 ++++++++++++++++++ 3 files changed, 162 insertions(+), 10 deletions(-) create mode 100644 spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockAdditionalImportTests.java 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 34bcb8de6e..ee8dc75cae 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 @@ -105,7 +105,11 @@ public class WireMockApplicationListener addPropertySource(propertySources); Map source = ((MapPropertySource) propertySources.get("wiremock")) .getSource(); - source.put(portProperty, SocketUtils.findAvailableTcpPort(minPort, maxPort)); + int port = SocketUtils.findAvailableTcpPort(minPort, maxPort); + source.put(portProperty, port); + if (log.isDebugEnabled()) { + 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 85f1601622..ead892468b 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 @@ -124,7 +124,7 @@ public class WireMockConfiguration implements SmartLifecycle { private void reRegisterBeans() { if (!this.beanFactory.containsBean(WIREMOCK_SERVER_BEAN_NAME)) { if (log.isDebugEnabled()) { - log.debug("Registering WireMock [" + this.server + "] instance"); + printRegistrationLog(); } this.beanFactory.registerSingleton(WIREMOCK_SERVER_BEAN_NAME, this.server); } @@ -136,12 +136,18 @@ public class WireMockConfiguration implements SmartLifecycle { } this.beanFactory.destroySingleton(WIREMOCK_SERVER_BEAN_NAME); if (log.isDebugEnabled()) { - log.debug("Registering WireMock [" + this.server + "] instance"); + printRegistrationLog(); } this.beanFactory.registerSingleton(WIREMOCK_SERVER_BEAN_NAME, this.server); } } + 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 + "]"); + } + private void reRegisterServer() { if (log.isTraceEnabled()) { log.trace("Creating a new server at http port [" @@ -158,14 +164,16 @@ public class WireMockConfiguration implements SmartLifecycle { else if (this.server == null) { this.server = new WireMockServer(this.options); if (log.isDebugEnabled()) { - log.debug("Created new server [" + this.server + "] at port [" - + port(this.server) + "]"); + 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 + "]"); } } start(); if (log.isDebugEnabled()) { - log.debug("Started server [" + this.server + "] at port [" + port(this.server) - + "]"); + 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 + "]"); } logRegisteredMappings(); } @@ -256,10 +264,11 @@ public class WireMockConfiguration implements SmartLifecycle { @Override public void start() { if (isRunning()) { - int port = port(this.server); + int httpPort = this.server.isRunning() ? this.server.port() : -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 port [" - + port + "]"); + log.debug("Server [" + this.server + "] is already running at http port [" + + httpPort + "] / https port [" + httpsPort + "]"); } return; } 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 new file mode 100644 index 0000000000..ec74471d6d --- /dev/null +++ b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockAdditionalImportTests.java @@ -0,0 +1,139 @@ +/* + * Copyright 2013-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.contract.wiremock; + +import java.util.Collections; +import java.util.List; + +import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder; +import com.github.tomakehurst.wiremock.client.WireMock; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import reactor.core.publisher.Mono; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.stereotype.Component; +import org.springframework.test.web.reactive.server.WebTestClient; +import org.springframework.web.bind.annotation.GetMapping; +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}") +@AutoConfigureWireMock(port = 0) +@Import(ExtraConfig.class) +public class AutoConfigureWireMockAdditionalImportTests { + + @AfterEach + public void resetWiremock() { + WireMock.reset(); + } + + @Nested + @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = TestConfiguration.class, properties = "base-url=http://localhost:${wiremock.server.port}") + @AutoConfigureWireMock(port = 0) + class SecondControllerTest { + + @Test + void test(@Autowired WebTestClient webTestClient) { + // arrange + WireMock.stubFor(WireMock.get(WireMock.urlMatching("/find-all")) + .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(); + + // assert + Assertions.assertThat(responseBody.get(0).getName()).isEqualTo("my-name"); + } + } +} + +@Component +class ExtraConfig { + +} + +@Configuration(proxyBeanMethods = false) +@EnableAutoConfiguration +class TestConfiguration { + + @Bean + public WebClient webClient(WebClient.Builder builder) { + return builder.build(); + } + + @Bean + TestController testController(WebClient webClient, @Value("${base-url}") String baseUrl) { + return new TestController(webClient, baseUrl); + } + +} + +@RestController +class TestController { + + private final WebClient webClient; + + private final String baseUrl; + + public TestController(WebClient webClient, String baseUrl) { + this.webClient = webClient; + this.baseUrl = baseUrl; + 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>() {}); + } + +} + +class TestItem { + + private String name; + + public TestItem() { } + + public TestItem(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} \ No newline at end of file