Remove the need for manual documentCurlRequest/Response calls

Previously, whenever a MockMvc perform call was made and the request
and response were to be documented, calls to documentCurlRequest and
documentCurlResponse had to be made.

This commit updates the REST documentation framework to make the
documentation of the request and response automatic. It makes use of
MockMvc’s MockMvcConfigurer that was introduced in Spring 4.1. Applying
RestDocumentationConfiguration once will cause all requests and
responses to be documented:

this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
		.apply(new RestDocumentationConfiguration()).build();

The path to which the requests and response documentation snippets will
be written is determined by the class and method in which the MockMvc
perform call is made and is currently of the form
shortClassName/methodName(Request|Response|RequestResponse).asciidoc
where shortClassName is the name of the documentation class without its
package. This works for both @Test methods and any non-private methods
that are called from the test method. The methods must be non-private as
a CGLib proxy is used to intercept the calls and configure the output
path.
This commit is contained in:
Andy Wilkinson
2014-10-23 11:37:24 +01:00
parent b7904bcad7
commit c823296db0
14 changed files with 373 additions and 380 deletions

View File

@@ -19,14 +19,9 @@ package com.example.notes;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.springframework.restdocs.core.RestDocumentationRequestPostProcessors.port;
import static org.springframework.restdocs.core.RestDocumentationResultHandlers.documentCurlRequest;
import static org.springframework.restdocs.core.RestDocumentationResultHandlers.documentCurlRequestAndResponse;
import static org.springframework.restdocs.core.RestDocumentationResultHandlers.documentCurlResponse;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@@ -42,7 +37,8 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.hateoas.MediaTypes;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.restdocs.core.RestDocumentationConfiguration;
import org.springframework.restdocs.core.RestDocumentationJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
@@ -53,7 +49,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jayway.jsonpath.JsonPath;
@RunWith(SpringJUnit4ClassRunner.class)
@RunWith(RestDocumentationJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
public class GettingStartedDocumentation {
@@ -68,17 +64,16 @@ public class GettingStartedDocumentation {
@Before
public void setUp() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(new RestDocumentationConfiguration()).build();
}
@Test
public void index() throws Exception {
this.mockMvc.perform(get("/").with(port(8080)).accept(MediaTypes.HAL_JSON))
this.mockMvc.perform(get("/").accept(MediaTypes.HAL_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("_links.notes", is(notNullValue())))
.andExpect(jsonPath("_links.tags", is(notNullValue())))
.andDo(documentCurlRequest("index/access_with_curl_request.asciidoc"))
.andDo(documentCurlResponse("index/access_with_curl_response.asciidoc"));
.andExpect(jsonPath("_links.tags", is(notNullValue())));
}
@Test
@@ -98,63 +93,49 @@ public class GettingStartedDocumentation {
getTagsForExistingNote(noteLocation);
}
private String createNote() throws Exception {
String createNote() throws Exception {
Map<String, String> note = new HashMap<String, String>();
note.put("title", "Note creation with cURL");
note.put("body", "An example of how to create a note using cURL");
String noteLocation = this.mockMvc
.perform(
post("/notes").with(port(8080)).contentType(MediaTypes.HAL_JSON)
.content(objectMapper.writeValueAsString(note)))
post("/notes").contentType(MediaTypes.HAL_JSON).content(
objectMapper.writeValueAsString(note)))
.andExpect(status().isCreated())
.andExpect(header().string("Location", notNullValue()))
.andDo(documentCurlRequest("notes/create_with_curl_request.asciidoc")
.includeResponseHeaders())
.andDo(documentCurlResponse("notes/create_with_curl_response.asciidoc")
.includeResponseHeaders()).andReturn().getResponse()
.getHeader("Location");
.andExpect(header().string("Location", notNullValue())).andReturn()
.getResponse().getHeader("Location");
return noteLocation;
}
private void getNote(String noteLocation) throws Exception {
this.mockMvc.perform(get(noteLocation).with(port(8080)))
.andExpect(status().isOk())
void getNote(String noteLocation) throws Exception {
this.mockMvc.perform(get(noteLocation)).andExpect(status().isOk())
.andExpect(jsonPath("title", is(notNullValue())))
.andExpect(jsonPath("body", is(notNullValue())))
.andExpect(jsonPath("_links.tags", is(notNullValue())))
.andDo(documentCurlRequest("notes/get_with_curl_request.asciidoc"))
.andDo(documentCurlResponse("notes/get_with_curl_response.asciidoc"));
.andExpect(jsonPath("_links.tags", is(notNullValue())));
}
private String createTag() throws Exception, JsonProcessingException {
String createTag() throws Exception, JsonProcessingException {
Map<String, String> tag = new HashMap<String, String>();
tag.put("name", "getting-started");
String tagLocation = this.mockMvc
.perform(
post("/tags").with(port(8080)).contentType(MediaTypes.HAL_JSON)
.content(objectMapper.writeValueAsString(tag)))
post("/tags").contentType(MediaTypes.HAL_JSON).content(
objectMapper.writeValueAsString(tag)))
.andExpect(status().isCreated())
.andExpect(header().string("Location", notNullValue()))
.andDo(documentCurlRequest("tags/create_with_curl_request.asciidoc")
.includeResponseHeaders())
.andDo(documentCurlResponse("tags/create_with_curl_response.asciidoc")
.includeResponseHeaders()).andReturn().getResponse()
.getHeader("Location");
.andExpect(header().string("Location", notNullValue())).andReturn()
.getResponse().getHeader("Location");
return tagLocation;
}
private void getTag(String tagLocation) throws Exception {
this.mockMvc.perform(get(tagLocation).with(port(8080)))
.andExpect(status().isOk())
void getTag(String tagLocation) throws Exception {
this.mockMvc.perform(get(tagLocation)).andExpect(status().isOk())
.andExpect(jsonPath("name", is(notNullValue())))
.andExpect(jsonPath("_links.notes", is(notNullValue())))
.andDo(documentCurlRequest("tags/get_with_curl_request.asciidoc"))
.andDo(documentCurlResponse("tags/get_with_curl_response.asciidoc"));
.andExpect(jsonPath("_links.notes", is(notNullValue())));
}
private String createTaggedNote(String tag) throws Exception {
String createTaggedNote(String tag) throws Exception {
Map<String, Object> note = new HashMap<String, Object>();
note.put("title", "Tagged note creation with cURL");
note.put("body", "An example of how to create a tagged note using cURL");
@@ -162,77 +143,48 @@ public class GettingStartedDocumentation {
String noteLocation = this.mockMvc
.perform(
post("/notes").with(port(8080)).contentType(MediaTypes.HAL_JSON)
.content(objectMapper.writeValueAsString(note)))
post("/notes").contentType(MediaTypes.HAL_JSON).content(
objectMapper.writeValueAsString(note)))
.andExpect(status().isCreated())
.andExpect(header().string("Location", notNullValue()))
.andDo(documentCurlRequest(
"notes/create_tagged_with_curl_request.asciidoc")
.includeResponseHeaders())
.andDo(documentCurlResponse(
"notes/create_tagged_with_curl_response.asciidoc")
.includeResponseHeaders()).andReturn().getResponse()
.getHeader("Location");
.andExpect(header().string("Location", notNullValue())).andReturn()
.getResponse().getHeader("Location");
return noteLocation;
}
private void getTaggedNote(String tagLocation) throws Exception {
this.mockMvc
.perform(get(tagLocation).with(port(8080)))
.andExpect(status().isOk())
void getTaggedNote(String tagLocation) throws Exception {
this.mockMvc.perform(get(tagLocation)).andExpect(status().isOk())
.andExpect(jsonPath("title", is(notNullValue())))
.andExpect(jsonPath("body", is(notNullValue())))
.andExpect(jsonPath("_links.tags", is(notNullValue())))
.andDo(documentCurlRequestAndResponse("notes/get_tagged_with_curl.asciidoc"));
.andExpect(jsonPath("_links.tags", is(notNullValue())));
}
private void getTags(String taggedNoteLocation) throws Exception {
void getTags(String taggedNoteLocation) throws Exception {
String tagsLocation = getLink(this.mockMvc.perform(get(taggedNoteLocation))
.andReturn(), "tags");
this.mockMvc
.perform(get(tagsLocation).with(port(8080)))
.andExpect(status().isOk())
.andExpect(jsonPath("_embedded.tags", hasSize(1)))
.andDo(documentCurlRequest("notes/get_tags_for_tagged_with_curl_request.asciidoc"))
.andDo(documentCurlResponse("notes/get_tags_for_tagged_with_curl_response.asciidoc"));
this.mockMvc.perform(get(tagsLocation)).andExpect(status().isOk())
.andExpect(jsonPath("_embedded.tags", hasSize(1)));
}
private void tagExistingNote(String noteLocation, String tagLocation)
throws Exception {
void tagExistingNote(String noteLocation, String tagLocation) throws Exception {
Map<String, Object> update = new HashMap<String, Object>();
update.put("tags", Arrays.asList(tagLocation));
this.mockMvc
.perform(
patch(noteLocation).with(port(8080))
.contentType(MediaTypes.HAL_JSON)
.content(objectMapper.writeValueAsString(update)))
.andExpect(status().isNoContent())
.andDo(documentCurlRequest(
"notes/tag_existing_with_curl_request.asciidoc")
.includeResponseHeaders())
.andDo(documentCurlResponse(
"notes/tag_existing_with_curl_response.asciidoc")
.includeResponseHeaders());
this.mockMvc.perform(
patch(noteLocation).contentType(MediaTypes.HAL_JSON).content(
objectMapper.writeValueAsString(update))).andExpect(
status().isNoContent());
}
private void getTaggedExistingNote(String tagLocation) throws Exception {
this.mockMvc
.perform(get(tagLocation).with(port(8080)))
.andExpect(status().isOk())
.andDo(documentCurlRequestAndResponse("notes/get_tagged_existing_with_curl.asciidoc"));
void getTaggedExistingNote(String tagLocation) throws Exception {
this.mockMvc.perform(get(tagLocation)).andExpect(status().isOk());
}
private void getTagsForExistingNote(String taggedNoteLocation) throws Exception {
void getTagsForExistingNote(String taggedNoteLocation) throws Exception {
String tagsLocation = getLink(this.mockMvc.perform(get(taggedNoteLocation))
.andReturn(), "tags");
this.mockMvc
.perform(get(tagsLocation).with(port(8080)))
.andExpect(status().isOk())
.andExpect(jsonPath("_embedded.tags", hasSize(1)))
.andDo(print())
.andDo(documentCurlRequestAndResponse("notes/get_tags_for_existing_with_curl.asciidoc"));
this.mockMvc.perform(get(tagsLocation)).andExpect(status().isOk())
.andExpect(jsonPath("_embedded.tags", hasSize(1)));
}
private String getLink(MvcResult result, String href)