From 458718c44389bf0ccd8f9d488633b8839361a1d3 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 29 Jul 2015 16:25:04 +0100 Subject: [PATCH] Improve test coverage of RestDocumentationResultHandler --- .../restdocs/templates/default-fields.snippet | 10 --- .../RestDocumentationIntegrationTests.java | 81 +++++++++++++++++++ 2 files changed, 81 insertions(+), 10 deletions(-) delete mode 100644 spring-restdocs/src/main/resources/org/springframework/restdocs/templates/default-fields.snippet diff --git a/spring-restdocs/src/main/resources/org/springframework/restdocs/templates/default-fields.snippet b/spring-restdocs/src/main/resources/org/springframework/restdocs/templates/default-fields.snippet deleted file mode 100644 index 497f5a03..00000000 --- a/spring-restdocs/src/main/resources/org/springframework/restdocs/templates/default-fields.snippet +++ /dev/null @@ -1,10 +0,0 @@ -|=== -|Path|Type|Description - -{{#fields}} -|{{path}} -|{{type}} -|{{description}} - -{{/fields}} -|=== \ No newline at end of file diff --git a/spring-restdocs/src/test/java/org/springframework/restdocs/RestDocumentationIntegrationTests.java b/spring-restdocs/src/test/java/org/springframework/restdocs/RestDocumentationIntegrationTests.java index b2ba9bb6..bc84de53 100644 --- a/spring-restdocs/src/test/java/org/springframework/restdocs/RestDocumentationIntegrationTests.java +++ b/spring-restdocs/src/test/java/org/springframework/restdocs/RestDocumentationIntegrationTests.java @@ -23,6 +23,9 @@ import static org.junit.Assert.assertTrue; import static org.springframework.restdocs.RestDocumentation.document; import static org.springframework.restdocs.RestDocumentation.modifyResponseTo; import static org.springframework.restdocs.RestDocumentationRequestBuilders.get; +import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.linkWithRel; +import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath; +import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName; import static org.springframework.restdocs.response.ResponsePostProcessors.maskLinks; import static org.springframework.restdocs.response.ResponsePostProcessors.prettyPrintContent; import static org.springframework.restdocs.response.ResponsePostProcessors.removeHeaders; @@ -108,6 +111,84 @@ public class RestDocumentationIntegrationTests { "http-request.adoc", "http-response.adoc", "curl-request.adoc"); } + @Test + public void links() throws Exception { + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context) + .apply(new RestDocumentationConfigurer()).build(); + + mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andDo(document("links").withLinks( + linkWithRel("rel").description("The description"))); + + assertExpectedSnippetFilesExist(new File("build/generated-snippets/links"), + "http-request.adoc", "http-response.adoc", "curl-request.adoc", + "links.adoc"); + } + + @Test + public void pathParameters() throws Exception { + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context) + .apply(new RestDocumentationConfigurer()).build(); + + mockMvc.perform(get("{foo}", "/").accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andDo(document("links").withPathParameters( + parameterWithName("foo").description("The description"))); + + assertExpectedSnippetFilesExist(new File("build/generated-snippets/links"), + "http-request.adoc", "http-response.adoc", "curl-request.adoc", + "path-parameters.adoc"); + } + + @Test + public void queryParameters() throws Exception { + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context) + .apply(new RestDocumentationConfigurer()).build(); + + mockMvc.perform(get("/").param("foo", "bar").accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andDo(document("links").withQueryParameters( + parameterWithName("foo").description("The description"))); + + assertExpectedSnippetFilesExist(new File("build/generated-snippets/links"), + "http-request.adoc", "http-response.adoc", "curl-request.adoc", + "query-parameters.adoc"); + } + + @Test + public void requestFields() throws Exception { + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context) + .apply(new RestDocumentationConfigurer()).build(); + + mockMvc.perform( + get("/").param("foo", "bar").content("{\"a\":\"alpha\"}") + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andDo(document("links").withRequestFields( + fieldWithPath("a").description("The description"))); + + assertExpectedSnippetFilesExist(new File("build/generated-snippets/links"), + "http-request.adoc", "http-response.adoc", "curl-request.adoc", + "request-fields.adoc"); + } + + @Test + public void responseFields() throws Exception { + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context) + .apply(new RestDocumentationConfigurer()).build(); + + mockMvc.perform(get("/").param("foo", "bar").accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andDo(document("links").withResponseFields( + fieldWithPath("a").description("The description"), + fieldWithPath("links").description("Links to other resources"))); + + assertExpectedSnippetFilesExist(new File("build/generated-snippets/links"), + "http-request.adoc", "http-response.adoc", "curl-request.adoc", + "response-fields.adoc"); + } + @Test public void parameterizedOutputDirectory() throws Exception { MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)