001/* 002 * Copyright 2012-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.test.autoconfigure.restdocs; 018 019import io.restassured.builder.RequestSpecBuilder; 020import io.restassured.specification.RequestSpecification; 021 022import org.springframework.beans.factory.ObjectProvider; 023import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 024import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 025import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 026import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; 027import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type; 028import org.springframework.boot.context.properties.EnableConfigurationProperties; 029import org.springframework.context.annotation.Bean; 030import org.springframework.context.annotation.Configuration; 031import org.springframework.restdocs.RestDocumentationContextProvider; 032import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation; 033import org.springframework.restdocs.mockmvc.MockMvcRestDocumentationConfigurer; 034import org.springframework.restdocs.mockmvc.RestDocumentationResultHandler; 035import org.springframework.restdocs.restassured3.RestAssuredRestDocumentation; 036import org.springframework.restdocs.restassured3.RestAssuredRestDocumentationConfigurer; 037import org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation; 038import org.springframework.restdocs.webtestclient.WebTestClientRestDocumentationConfigurer; 039 040/** 041 * {@link EnableAutoConfiguration Auto-configuration} for Spring REST Docs. 042 * 043 * @author Andy Wilkinson 044 * @author EddĂș MelĂ©ndez 045 * @author Roman Zaynetdinov 046 * @since 1.4.0 047 */ 048@Configuration 049@ConditionalOnWebApplication 050public class RestDocsAutoConfiguration { 051 052 @Configuration 053 @ConditionalOnClass(MockMvcRestDocumentation.class) 054 @ConditionalOnWebApplication(type = Type.SERVLET) 055 @EnableConfigurationProperties(RestDocsProperties.class) 056 static class RestDocsMockMvcConfiguration { 057 058 @Bean 059 @ConditionalOnMissingBean 060 public MockMvcRestDocumentationConfigurer restDocsMockMvcConfigurer( 061 ObjectProvider<RestDocsMockMvcConfigurationCustomizer> configurationCustomizers, 062 RestDocumentationContextProvider contextProvider) { 063 MockMvcRestDocumentationConfigurer configurer = MockMvcRestDocumentation 064 .documentationConfiguration(contextProvider); 065 configurationCustomizers.orderedStream() 066 .forEach((configurationCustomizer) -> configurationCustomizer 067 .customize(configurer)); 068 return configurer; 069 } 070 071 @Bean 072 public RestDocsMockMvcBuilderCustomizer restDocumentationConfigurer( 073 RestDocsProperties properties, 074 MockMvcRestDocumentationConfigurer configurer, 075 ObjectProvider<RestDocumentationResultHandler> resultHandler) { 076 return new RestDocsMockMvcBuilderCustomizer(properties, configurer, 077 resultHandler.getIfAvailable()); 078 } 079 080 } 081 082 @Configuration 083 @ConditionalOnClass({ RequestSpecification.class, 084 RestAssuredRestDocumentation.class }) 085 @EnableConfigurationProperties(RestDocsProperties.class) 086 static class RestDocsRestAssuredConfiguration { 087 088 @Bean 089 @ConditionalOnMissingBean 090 public RequestSpecification restDocsRestAssuredConfigurer( 091 ObjectProvider<RestDocsRestAssuredConfigurationCustomizer> configurationCustomizers, 092 RestDocumentationContextProvider contextProvider) { 093 RestAssuredRestDocumentationConfigurer configurer = RestAssuredRestDocumentation 094 .documentationConfiguration(contextProvider); 095 configurationCustomizers.orderedStream() 096 .forEach((configurationCustomizer) -> configurationCustomizer 097 .customize(configurer)); 098 return new RequestSpecBuilder().addFilter(configurer).build(); 099 } 100 101 @Bean 102 public RestDocsRestAssuredBuilderCustomizer restAssuredBuilderCustomizer( 103 RestDocsProperties properties, RequestSpecification configurer) { 104 return new RestDocsRestAssuredBuilderCustomizer(properties, configurer); 105 } 106 107 } 108 109 @Configuration 110 @ConditionalOnClass(WebTestClientRestDocumentation.class) 111 @ConditionalOnWebApplication(type = Type.REACTIVE) 112 @EnableConfigurationProperties(RestDocsProperties.class) 113 static class RestDocsWebTestClientConfiguration { 114 115 @Bean 116 @ConditionalOnMissingBean 117 public WebTestClientRestDocumentationConfigurer restDocsWebTestClientConfigurer( 118 ObjectProvider<RestDocsWebTestClientConfigurationCustomizer> configurationCustomizers, 119 RestDocumentationContextProvider contextProvider) { 120 WebTestClientRestDocumentationConfigurer configurer = WebTestClientRestDocumentation 121 .documentationConfiguration(contextProvider); 122 configurationCustomizers.orderedStream() 123 .forEach((configurationCustomizer) -> configurationCustomizer 124 .customize(configurer)); 125 return configurer; 126 } 127 128 @Bean 129 public RestDocsWebTestClientBuilderCustomizer restDocumentationConfigurer( 130 RestDocsProperties properties, 131 WebTestClientRestDocumentationConfigurer configurer) { 132 return new RestDocsWebTestClientBuilderCustomizer(properties, configurer); 133 } 134 135 } 136 137}