From b9bb31cfd4e87f84970ec9b0b1d9ba32f1cc96ad Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 9 Jan 2018 12:11:23 -0800 Subject: [PATCH] Extract RestDocsProperties Extract properties used by RestDocs to their own class. --- .../restdocs/RestDocsAutoConfiguration.java | 18 ++--- .../RestDocsMockMvcBuilderCustomizer.java | 54 ++++---------- .../restdocs/RestDocsProperties.java | 71 +++++++++++++++++++ .../RestDocsRestAssuredBuilderCustomizer.java | 50 ++++--------- ...estDocsWebTestClientBuilderCustomizer.java | 55 ++++---------- 5 files changed, 121 insertions(+), 127 deletions(-) create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsProperties.java diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsAutoConfiguration.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsAutoConfiguration.java index be6c39cf7b0..7b9f363a878 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsAutoConfiguration.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsAutoConfiguration.java @@ -25,7 +25,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type; -import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -47,13 +46,13 @@ import org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation * @since 1.4.0 */ @Configuration -@EnableConfigurationProperties @ConditionalOnWebApplication public class RestDocsAutoConfiguration { @Configuration @ConditionalOnClass(MockMvcRestDocumentation.class) @ConditionalOnWebApplication(type = Type.SERVLET) + @EnableConfigurationProperties(RestDocsProperties.class) static class RestDocsMockMvcAutoConfiguration { @Bean @@ -72,11 +71,11 @@ public class RestDocsAutoConfiguration { } @Bean - @ConfigurationProperties(prefix = "spring.test.restdocs") public RestDocsMockMvcBuilderCustomizer restDocumentationConfigurer( + RestDocsProperties properties, MockMvcRestDocumentationConfigurer configurer, ObjectProvider resultHandler) { - return new RestDocsMockMvcBuilderCustomizer(configurer, + return new RestDocsMockMvcBuilderCustomizer(properties, configurer, resultHandler.getIfAvailable()); } @@ -85,6 +84,7 @@ public class RestDocsAutoConfiguration { @Configuration @ConditionalOnClass({ RequestSpecification.class, RestAssuredRestDocumentation.class }) + @EnableConfigurationProperties(RestDocsProperties.class) static class RestDocsRestAssuredAutoConfiguration { @Bean @@ -103,10 +103,9 @@ public class RestDocsAutoConfiguration { } @Bean - @ConfigurationProperties(prefix = "spring.test.restdocs") public RestDocsRestAssuredBuilderCustomizer restAssuredBuilderCustomizer( - RequestSpecification configurer) { - return new RestDocsRestAssuredBuilderCustomizer(configurer); + RestDocsProperties properties, RequestSpecification configurer) { + return new RestDocsRestAssuredBuilderCustomizer(properties, configurer); } } @@ -114,6 +113,7 @@ public class RestDocsAutoConfiguration { @Configuration @ConditionalOnClass(WebTestClientRestDocumentation.class) @ConditionalOnWebApplication(type = Type.REACTIVE) + @EnableConfigurationProperties(RestDocsProperties.class) static class RestDocsWebTestClientAutoConfiguration { @Bean @@ -132,10 +132,10 @@ public class RestDocsAutoConfiguration { } @Bean - @ConfigurationProperties(prefix = "spring.test.restdocs") public RestDocsWebTestClientBuilderCustomizer restDocumentationConfigurer( + RestDocsProperties properties, WebTestClientRestDocumentationConfigurer configurer) { - return new RestDocsWebTestClientBuilderCustomizer(configurer); + return new RestDocsWebTestClientBuilderCustomizer(properties, configurer); } } diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsMockMvcBuilderCustomizer.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsMockMvcBuilderCustomizer.java index 98b77ab59c9..004693167ce 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsMockMvcBuilderCustomizer.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsMockMvcBuilderCustomizer.java @@ -17,11 +17,12 @@ package org.springframework.boot.test.autoconfigure.restdocs; import org.springframework.beans.factory.InitializingBean; +import org.springframework.boot.context.properties.PropertyMapper; import org.springframework.boot.test.autoconfigure.web.servlet.MockMvcBuilderCustomizer; import org.springframework.restdocs.mockmvc.MockMvcRestDocumentationConfigurer; import org.springframework.restdocs.mockmvc.RestDocumentationResultHandler; +import org.springframework.restdocs.mockmvc.UriConfigurer; import org.springframework.test.web.servlet.setup.ConfigurableMockMvcBuilder; -import org.springframework.util.StringUtils; /** * A {@link MockMvcBuilderCustomizer} that configures Spring REST Docs. @@ -31,57 +32,28 @@ import org.springframework.util.StringUtils; class RestDocsMockMvcBuilderCustomizer implements InitializingBean, MockMvcBuilderCustomizer { + private final RestDocsProperties properties; + private final MockMvcRestDocumentationConfigurer delegate; private final RestDocumentationResultHandler resultHandler; - private String uriScheme; - - private String uriHost; - - private Integer uriPort; - - RestDocsMockMvcBuilderCustomizer(MockMvcRestDocumentationConfigurer delegate, + RestDocsMockMvcBuilderCustomizer(RestDocsProperties properties, + MockMvcRestDocumentationConfigurer delegate, RestDocumentationResultHandler resultHandler) { + this.properties = properties; this.delegate = delegate; this.resultHandler = resultHandler; } - public String getUriScheme() { - return this.uriScheme; - } - - public void setUriScheme(String uriScheme) { - this.uriScheme = uriScheme; - } - - public String getUriHost() { - return this.uriHost; - } - - public void setUriHost(String uriHost) { - this.uriHost = uriHost; - } - - public Integer getUriPort() { - return this.uriPort; - } - - public void setUriPort(Integer uriPort) { - this.uriPort = uriPort; - } - @Override public void afterPropertiesSet() throws Exception { - if (StringUtils.hasText(this.uriScheme)) { - this.delegate.uris().withScheme(this.uriScheme); - } - if (StringUtils.hasText(this.uriHost)) { - this.delegate.uris().withHost(this.uriHost); - } - if (this.uriPort != null) { - this.delegate.uris().withPort(this.uriPort); - } + PropertyMapper map = PropertyMapper.get(); + RestDocsProperties properties = this.properties; + UriConfigurer uri = this.delegate.uris(); + map.from(properties::getUriScheme).whenHasText().to(uri::withScheme); + map.from(properties::getUriHost).whenHasText().to(uri::withHost); + map.from(properties::getUriPort).whenNonNull().to(uri::withPort); } @Override diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsProperties.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsProperties.java new file mode 100644 index 00000000000..b579f86cf82 --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsProperties.java @@ -0,0 +1,71 @@ +/* + * Copyright 2012-2018 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 + * + * http://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.boot.test.autoconfigure.restdocs; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * Configuration properties for Spring REST Docs. + * + * @author Andy Wilkinson + * @author EddĂș MelĂ©ndez + * @author Phillip Webb + * @since 2.0.0 + */ +@ConfigurationProperties("spring.test.restdocs") +public class RestDocsProperties { + + /** + * The URI scheme for to use (for example http). + */ + private String uriScheme; + + /** + * The URI host to use. + */ + private String uriHost; + + /** + * The URI port to use. + */ + private Integer uriPort; + + public String getUriScheme() { + return this.uriScheme; + } + + public void setUriScheme(String uriScheme) { + this.uriScheme = uriScheme; + } + + public String getUriHost() { + return this.uriHost; + } + + public void setUriHost(String uriHost) { + this.uriHost = uriHost; + } + + public Integer getUriPort() { + return this.uriPort; + } + + public void setUriPort(Integer uriPort) { + this.uriPort = uriPort; + } + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsRestAssuredBuilderCustomizer.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsRestAssuredBuilderCustomizer.java index 54c600d3c81..1de100fe5f8 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsRestAssuredBuilderCustomizer.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsRestAssuredBuilderCustomizer.java @@ -19,6 +19,7 @@ package org.springframework.boot.test.autoconfigure.restdocs; import io.restassured.specification.RequestSpecification; import org.springframework.beans.factory.InitializingBean; +import org.springframework.boot.context.properties.PropertyMapper; import org.springframework.util.StringUtils; /** @@ -28,50 +29,25 @@ import org.springframework.util.StringUtils; */ class RestDocsRestAssuredBuilderCustomizer implements InitializingBean { + private final RestDocsProperties properties; + private final RequestSpecification delegate; - private String uriScheme; - - private String uriHost; - - private Integer uriPort; - - RestDocsRestAssuredBuilderCustomizer(RequestSpecification delegate) { + RestDocsRestAssuredBuilderCustomizer(RestDocsProperties properties, + RequestSpecification delegate) { + this.properties = properties; this.delegate = delegate; } - public String getUriScheme() { - return this.uriScheme; - } - - public void setUriScheme(String uriScheme) { - this.uriScheme = uriScheme; - } - - public String getUriHost() { - return this.uriHost; - } - - public void setUriHost(String uriHost) { - this.uriHost = uriHost; - } - - public Integer getUriPort() { - return this.uriPort; - } - - public void setUriPort(Integer uriPort) { - this.uriPort = uriPort; - } - @Override public void afterPropertiesSet() throws Exception { - if (StringUtils.hasText(this.uriScheme) && StringUtils.hasText(this.uriHost)) { - this.delegate.baseUri(this.uriScheme + "://" + this.uriHost); - } - if (this.uriPort != null) { - this.delegate.port(this.uriPort); - } + PropertyMapper map = PropertyMapper.get(); + String host = this.properties.getUriHost(); + map.from(this.properties::getUriScheme) + .when((scheme) -> StringUtils.hasText(scheme) + && StringUtils.hasText(host)) + .to((scheme) -> this.delegate.baseUri(scheme + "://" + host)); + map.from(this.properties::getUriPort).whenNonNull().to(this.delegate::port); } } diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsWebTestClientBuilderCustomizer.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsWebTestClientBuilderCustomizer.java index e7206a3a308..39253b5c7fc 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsWebTestClientBuilderCustomizer.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsWebTestClientBuilderCustomizer.java @@ -29,43 +29,16 @@ import org.springframework.util.StringUtils; */ class RestDocsWebTestClientBuilderCustomizer implements WebTestClientBuilderCustomizer { + private final RestDocsProperties properties; + private final WebTestClientRestDocumentationConfigurer delegate; - private String uriScheme; - - private String uriHost; - - private Integer uriPort; - - RestDocsWebTestClientBuilderCustomizer( + RestDocsWebTestClientBuilderCustomizer(RestDocsProperties properties, WebTestClientRestDocumentationConfigurer delegate) { + this.properties = properties; this.delegate = delegate; } - public String getUriScheme() { - return this.uriScheme; - } - - public void setUriScheme(String uriScheme) { - this.uriScheme = uriScheme; - } - - public String getUriHost() { - return this.uriHost; - } - - public void setUriHost(String uriHost) { - this.uriHost = uriHost; - } - - public Integer getUriPort() { - return this.uriPort; - } - - public void setUriPort(Integer uriPort) { - this.uriPort = uriPort; - } - @Override public void customize(WebTestClient.Builder builder) { customizeBaseUrl(builder); @@ -73,21 +46,23 @@ class RestDocsWebTestClientBuilderCustomizer implements WebTestClientBuilderCust } private void customizeBaseUrl(WebTestClient.Builder builder) { - String scheme = StringUtils.hasText(this.uriScheme) ? this.uriScheme : "http"; - String host = StringUtils.hasText(this.uriHost) ? this.uriHost : "localhost"; - String baseUrl = scheme + "://" + host; - if (!isStandardPort()) { - baseUrl += ":" + this.uriPort; + String scheme = this.properties.getUriScheme(); + String host = this.properties.getUriHost(); + String baseUrl = (StringUtils.hasText(scheme) ? scheme : "http") + "://" + + (StringUtils.hasText(host) ? host : "localhost"); + Integer port = this.properties.getUriPort(); + if (!isStandardPort(scheme, port)) { + baseUrl += ":" + port; } builder.baseUrl(baseUrl); } - private boolean isStandardPort() { - if (this.uriPort == null) { + private boolean isStandardPort(String scheme, Integer port) { + if (port == null) { return true; } - return this.uriScheme.equals("http") && this.uriPort == 80 - || this.uriScheme.equals("https") && this.uriPort.equals(443); + return (scheme.equals("http") && port == 80) + || (scheme.equals("https") && port == 443); } }