Merge pull request #9643 from Eddú Meléndez
* gh-9643: Polish "Add auto-configuration for REST Docs with REST Assured" Add auto-configuration for REST Docs with REST Assuredpull/10380/merge
commit
d8cfae7300
@ -0,0 +1,39 @@
|
|||||||
|
/*
|
||||||
|
* 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.restassured;
|
||||||
|
|
||||||
|
import org.springframework.boot.test.autoconfigure.restdocs.RestDocsRestAssuredConfigurationCustomizer;
|
||||||
|
import org.springframework.boot.test.context.TestConfiguration;
|
||||||
|
import org.springframework.restdocs.restassured3.RestAssuredRestDocumentationConfigurer;
|
||||||
|
import org.springframework.restdocs.templates.TemplateFormats;
|
||||||
|
|
||||||
|
public class AdvancedConfigurationExample {
|
||||||
|
|
||||||
|
// tag::configuration[]
|
||||||
|
@TestConfiguration
|
||||||
|
public static class CustomizationConfiguration
|
||||||
|
implements RestDocsRestAssuredConfigurationCustomizer {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void customize(RestAssuredRestDocumentationConfigurer configurer) {
|
||||||
|
configurer.snippets().withTemplateFormat(TemplateFormats.markdown());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
// end::configuration[]
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,53 @@
|
|||||||
|
/*
|
||||||
|
* 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.restassured;
|
||||||
|
|
||||||
|
// tag::source[]
|
||||||
|
import io.restassured.specification.RequestSpecification;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs;
|
||||||
|
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 static io.restassured.RestAssured.given;
|
||||||
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
|
import static org.springframework.restdocs.restassured3.RestAssuredRestDocumentation.document;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||||
|
@AutoConfigureRestDocs
|
||||||
|
public class UserDocumentationTests {
|
||||||
|
|
||||||
|
@LocalServerPort
|
||||||
|
private int port;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RequestSpecification documentationSpec;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void listUsers() throws Exception {
|
||||||
|
given(this.documentationSpec).filter(document("list-users")).when()
|
||||||
|
.port(this.port).get("/").then().assertThat().statusCode(is(200));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
// end::source[]
|
@ -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 REST Assured.
|
||||||
|
*
|
||||||
|
* @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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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);
|
||||||
|
|
||||||
|
}
|
5
spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsAutoConfigurationAdvancedConfigurationIntegrationTests.java → spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/MockMvcRestDocsAutoConfigurationAdvancedConfigurationIntegrationTests.java
5
spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsAutoConfigurationAdvancedConfigurationIntegrationTests.java → spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/MockMvcRestDocsAutoConfigurationAdvancedConfigurationIntegrationTests.java
4
spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsAutoConfigurationIntegrationTests.java → spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/MockMvcRestDocsAutoConfigurationIntegrationTests.java
4
spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsAutoConfigurationIntegrationTests.java → spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/MockMvcRestDocsAutoConfigurationIntegrationTests.java
@ -0,0 +1,97 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Integration tests for advanced configuration of {@link AutoConfigureRestDocs} with REST
|
||||||
|
* Assured.
|
||||||
|
*
|
||||||
|
* @author Eddú Meléndez
|
||||||
|
*/
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||||
|
@AutoConfigureRestDocs
|
||||||
|
public class RestAssuredRestDocsAutoConfigurationAdvancedConfigurationIntegrationTests {
|
||||||
|
|
||||||
|
@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());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,82 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Integration tests for {@link RestDocsAutoConfiguration} with REST Assured.
|
||||||
|
*
|
||||||
|
* @author Eddú Meléndez
|
||||||
|
*/
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||||
|
@AutoConfigureRestDocs
|
||||||
|
public class RestAssuredRestDocsAutoConfigurationIntegrationTests {
|
||||||
|
|
||||||
|
@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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue