Add auto-configuration for REST Docs with REST Assured

See gh-9643
This commit is contained in:
Eddú Meléndez
2017-06-20 20:40:17 -05:00
committed by Andy Wilkinson
parent a1e26eaeea
commit 98b2267a5b
9 changed files with 379 additions and 22 deletions

View File

@@ -16,8 +16,12 @@
package org.springframework.boot.test.autoconfigure.restdocs;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.specification.RequestSpecification;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
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;
@@ -29,40 +33,77 @@ import org.springframework.restdocs.RestDocumentationContextProvider;
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation;
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentationConfigurer;
import org.springframework.restdocs.mockmvc.RestDocumentationResultHandler;
import org.springframework.restdocs.restassured3.RestAssuredRestDocumentation;
import org.springframework.restdocs.restassured3.RestAssuredRestDocumentationConfigurer;
/**
* {@link EnableAutoConfiguration Auto-configuration} for Spring REST Docs.
*
* @author Andy Wilkinson
* @author Eddú Meléndez
* @since 1.4.0
*/
@Configuration
@ConditionalOnWebApplication(type = Type.SERVLET)
@EnableConfigurationProperties
public class RestDocsAutoConfiguration {
@Bean
@ConditionalOnMissingBean(MockMvcRestDocumentationConfigurer.class)
public MockMvcRestDocumentationConfigurer restDocsMockMvcConfigurer(
ObjectProvider<RestDocsMockMvcConfigurationCustomizer> configurationCustomizerProvider,
RestDocumentationContextProvider contextProvider) {
MockMvcRestDocumentationConfigurer configurer = MockMvcRestDocumentation
.documentationConfiguration(contextProvider);
RestDocsMockMvcConfigurationCustomizer configurationCustomizer = configurationCustomizerProvider
.getIfAvailable();
if (configurationCustomizer != null) {
configurationCustomizer.customize(configurer);
@Configuration
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass(MockMvcRestDocumentation.class)
static class RestDocsMockMvcAutoConfiguration {
@Bean
@ConditionalOnMissingBean(MockMvcRestDocumentationConfigurer.class)
public MockMvcRestDocumentationConfigurer restDocsMockMvcConfigurer(
ObjectProvider<RestDocsMockMvcConfigurationCustomizer> configurationCustomizerProvider,
RestDocumentationContextProvider contextProvider) {
MockMvcRestDocumentationConfigurer configurer = MockMvcRestDocumentation
.documentationConfiguration(contextProvider);
RestDocsMockMvcConfigurationCustomizer configurationCustomizer = configurationCustomizerProvider
.getIfAvailable();
if (configurationCustomizer != null) {
configurationCustomizer.customize(configurer);
}
return configurer;
}
return configurer;
@Bean
@ConfigurationProperties(prefix = "spring.test.restdocs")
public RestDocsMockMvcBuilderCustomizer restDocumentationConfigurer(
MockMvcRestDocumentationConfigurer configurer,
ObjectProvider<RestDocumentationResultHandler> resultHandler) {
return new RestDocsMockMvcBuilderCustomizer(configurer,
resultHandler.getIfAvailable());
}
}
@Bean
@ConfigurationProperties(prefix = "spring.test.restdocs")
public RestDocsMockMvcBuilderCustomizer restDocumentationConfigurer(
MockMvcRestDocumentationConfigurer configurer,
ObjectProvider<RestDocumentationResultHandler> resultHandler) {
return new RestDocsMockMvcBuilderCustomizer(configurer,
resultHandler.getIfAvailable());
@Configuration
@ConditionalOnClass({ RequestSpecification.class, RestAssuredRestDocumentation.class })
static class RestDocsRestAssuredAutoConfiguration {
@Bean
@ConditionalOnMissingBean(RequestSpecification.class)
public RequestSpecification restDocsRestAssuredConfigurer(
ObjectProvider<RestDocsRestAssuredConfigurationCustomizer> configurationCustomizerProvider,
RestDocumentationContextProvider contextProvider) {
RestAssuredRestDocumentationConfigurer configurer = RestAssuredRestDocumentation
.documentationConfiguration(contextProvider);
RestDocsRestAssuredConfigurationCustomizer configurationCustomizer = configurationCustomizerProvider.getIfAvailable();
if (configurationCustomizer != null) {
configurationCustomizer.customize(configurer);
}
return new RequestSpecBuilder().addFilter(configurer).build();
}
@Bean
@ConfigurationProperties(prefix = "spring.test.restdocs")
public RestDocsRestAssuredBuilderCustomizer restAssuredBuilderCustomizer(
RequestSpecification configurer) {
return new RestDocsRestAssuredBuilderCustomizer(configurer);
}
}
}

View File

@@ -0,0 +1,77 @@
/*
* Copyright 2012-2017 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 io.restassured.specification.RequestSpecification;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.StringUtils;
/**
* A customizer that configures Spring REST Docs with RestAssured.
*
* @author Eddú Meléndez
*/
class RestDocsRestAssuredBuilderCustomizer implements InitializingBean {
private final RequestSpecification delegate;
private String uriScheme;
private String uriHost;
private Integer uriPort;
RestDocsRestAssuredBuilderCustomizer(RequestSpecification delegate) {
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);
}
}
}

View File

@@ -0,0 +1,41 @@
/*
* Copyright 2012-2017 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.restdocs.restassured3.RestAssuredRestDocumentationConfigurer;
/**
* A customizer for {@link RestAssuredRestDocumentationConfigurer}. If a
* {@code RestDocsRestAssuredConfigurationCustomizer} bean is found in the application
* context it will be {@link #customize called} to customize the
* {@code RestAssuredRestDocumentationConfigurer} before it is applied. Intended for use
* only when the attributes on {@link AutoConfigureRestDocs} do not provide sufficient
* customization.
*
* @author Eddú Meléndez
* @since 2.0.0
*/
@FunctionalInterface
public interface RestDocsRestAssuredConfigurationCustomizer {
/**
* Customize the given {@code configurer}.
* @param configurer the configurer
*/
void customize(RestAssuredRestDocumentationConfigurer configurer);
}

View File

@@ -0,0 +1,95 @@
/*
* Copyright 2012-2017 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 java.io.File;
import io.restassured.specification.RequestSpecification;
import org.assertj.core.api.Condition;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.restdocs.restassured3.RestAssuredRestDocumentationConfigurer;
import org.springframework.restdocs.templates.TemplateFormats;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.FileSystemUtils;
import static io.restassured.RestAssured.given;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.CoreMatchers.is;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest;
import static org.springframework.restdocs.restassured3.RestAssuredRestDocumentation.document;
import static org.springframework.restdocs.restassured3.operation.preprocess.RestAssuredPreprocessors.modifyUris;
/**
* Tests for {@link AutoConfigureRestDocs}.
*
* @author Eddú Meléndez
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@AutoConfigureRestDocs
public class RestAssuredAutoConfigurationAdvancedConfigurationIntegrationTests {
@LocalServerPort
private int port;
@Before
public void deleteSnippets() {
FileSystemUtils.deleteRecursively(new File("target/generated-snippets"));
}
@Autowired
private RequestSpecification documentationSpec;
@Test
public void snippetGeneration() throws Exception {
given(this.documentationSpec)
.filter(document("default-snippets", preprocessRequest(modifyUris()
.scheme("https").host("api.example.com").removePort()))).when()
.port(this.port).get("/").then().assertThat().statusCode(is(200));
File defaultSnippetsDir = new File("target/generated-snippets/default-snippets");
assertThat(defaultSnippetsDir).exists();
assertThat(new File(defaultSnippetsDir, "curl-request.md")).has(
contentContaining("'https://api.example.com/'"));
assertThat(new File(defaultSnippetsDir, "http-request.md")).has(
contentContaining("api.example.com"));
assertThat(new File(defaultSnippetsDir, "http-response.md")).isFile();
}
private Condition<File> contentContaining(String toContain) {
return new ContentContainingCondition(toContain);
}
@TestConfiguration
public static class CustomizationConfiguration implements
RestDocsRestAssuredConfigurationCustomizer {
@Override
public void customize(RestAssuredRestDocumentationConfigurer configurer) {
configurer.snippets().withTemplateFormat(TemplateFormats.markdown());
}
}
}

View File

@@ -0,0 +1,81 @@
/*
* Copyright 2012-2017 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 java.io.File;
import io.restassured.specification.RequestSpecification;
import org.assertj.core.api.Condition;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.FileSystemUtils;
import static io.restassured.RestAssured.given;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.CoreMatchers.is;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest;
import static org.springframework.restdocs.restassured3.RestAssuredRestDocumentation.document;
import static org.springframework.restdocs.restassured3.operation.preprocess.RestAssuredPreprocessors.modifyUris;
/**
* Tests for {@link RestDocsAutoConfiguration}
*
* @author Eddú Meléndez
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@AutoConfigureRestDocs
public class RestAssuredAutoConfigurationIntegrationTests {
@LocalServerPort
private int port;
@Before
public void deleteSnippets() {
FileSystemUtils.deleteRecursively(new File("target/generated-snippets"));
}
@Autowired
private RequestSpecification documentationSpec;
@Test
public void defaultSnippetsAreWritten() throws Exception {
given(this.documentationSpec)
.filter(document("default-snippets", preprocessRequest(modifyUris()
.scheme("https").host("api.example.com").removePort()))).when()
.port(this.port).get("/").then().assertThat().statusCode(is(200));
File defaultSnippetsDir = new File("target/generated-snippets/default-snippets");
assertThat(defaultSnippetsDir).exists();
assertThat(new File(defaultSnippetsDir, "curl-request.adoc")).has(
contentContaining("'https://api.example.com/'"));
assertThat(new File(defaultSnippetsDir, "http-request.adoc")).has(
contentContaining("api.example.com"));
assertThat(new File(defaultSnippetsDir, "http-response.adoc")).isFile();
}
private Condition<File> contentContaining(String toContain) {
return new ContentContainingCondition(toContain);
}
}

View File

@@ -17,13 +17,14 @@
package org.springframework.boot.test.autoconfigure.restdocs;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration;
/**
* Test application used with {@link AutoConfigureRestDocs} tests.
*
* @author Andy Wilkinson
*/
@SpringBootApplication
@SpringBootApplication(exclude = SecurityAutoConfiguration.class)
public class RestDocsTestApplication {
}