Clean up Spring REST Docs examples

This commit is contained in:
David Morley 2016-05-23 06:06:48 -05:00
parent 28d6a0113b
commit e7ec50034a
2 changed files with 325 additions and 265 deletions

View File

@ -1,29 +1,6 @@
package com.example; package com.example;
import static org.springframework.restdocs.headers.HeaderDocumentation.headerWithName; import com.fasterxml.jackson.databind.ObjectMapper;
import static org.springframework.restdocs.headers.HeaderDocumentation.responseHeaders;
import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.linkWithRel;
import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.links;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.delete;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.patch;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.put;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessResponse;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.restdocs.snippet.Attributes.key;
import static org.springframework.restdocs.payload.PayloadDocumentation.requestFields;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
@ -39,146 +16,219 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.util.StringUtils;
import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.WebApplicationContext;
import com.fasterxml.jackson.databind.ObjectMapper; import java.util.HashMap;
import java.util.Map;
import static java.util.Collections.singletonList;
import static org.springframework.restdocs.headers.HeaderDocumentation.headerWithName;
import static org.springframework.restdocs.headers.HeaderDocumentation.responseHeaders;
import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.linkWithRel;
import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.links;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.*;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.*;
import static org.springframework.restdocs.payload.PayloadDocumentation.*;
import static org.springframework.restdocs.snippet.Attributes.key;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.util.StringUtils.collectionToDelimitedString;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SpringRestDocsApplication.class) @SpringApplicationConfiguration(classes = SpringRestDocsApplication.class)
@WebAppConfiguration @WebAppConfiguration
public class ApiDocumentation { public class ApiDocumentation {
@Rule @Rule
public final RestDocumentation restDocumentation = new RestDocumentation("target/generated-snippets"); public final RestDocumentation restDocumentation = new RestDocumentation("target/generated-snippets");
@Autowired @Autowired
private WebApplicationContext context; private WebApplicationContext context;
@Autowired @Autowired
private ObjectMapper objectMapper; private ObjectMapper objectMapper;
private RestDocumentationResultHandler document; private RestDocumentationResultHandler document;
private MockMvc mockMvc; private MockMvc mockMvc;
@Before @Before
public void setUp() { public void setUp() {
this.document = document("{method-name}",preprocessRequest(prettyPrint()),preprocessResponse(prettyPrint())); this.document = document("{method-name}", preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint()));
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).apply(documentationConfiguration(this.restDocumentation)).alwaysDo(this.document).build(); this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
} .apply(documentationConfiguration(this.restDocumentation))
.alwaysDo(this.document)
.build();
}
@Test @Test
public void headersExample() throws Exception { public void headersExample() throws Exception {
this.document.snippets(responseHeaders(headerWithName("Content-Type").description("The Content-Type of the payload, e.g. `application/hal+json`"))); this.document.snippets(responseHeaders(headerWithName("Content-Type")
this.mockMvc.perform(get("/")).andExpect(status().isOk()); .description("The Content-Type of the payload, e.g. `application/hal+json`")));
} this.mockMvc.perform(get("/"))
.andExpect(status().isOk());
}
@Test @Test
public void indexExample() throws Exception { public void indexExample() throws Exception {
this.document.snippets(links(linkWithRel("crud").description("The <<resources-tags,Tags resource>>")),responseFields(fieldWithPath("_links").description("<<resources-index-links,Links>> to other resources"))); this.document.snippets(
this.mockMvc.perform(get("/")).andExpect(status().isOk()); links(linkWithRel("crud").description("The <<resources-tags,Tags resource>>")),
} responseFields(fieldWithPath("_links").description("<<resources-index-links,Links>> to other resources"))
);
this.mockMvc.perform(get("/"))
.andExpect(status().isOk());
}
@Test @Test
public void crudGetExample() throws Exception { public void crudGetExample() throws Exception {
Map<String, String> tag = new HashMap<String, String>(); Map<String, String> tag = new HashMap<>();
tag.put("name", "GET"); tag.put("name", "GET");
String tagLocation =this.mockMvc.perform(get("/crud").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(tag))).andExpect(status().isOk()).andReturn().getResponse().getHeader("Location"); String tagLocation = this.mockMvc.perform(get("/crud")
.contentType(MediaTypes.HAL_JSON)
.content(this.objectMapper.writeValueAsString(tag)))
.andExpect(status().isOk())
.andReturn()
.getResponse()
.getHeader("Location");
Map<String, Object> crud = new HashMap<String, Object>(); Map<String, Object> crud = new HashMap<>();
crud.put("title", "Sample Model"); crud.put("title", "Sample Model");
crud.put("body", "http://www.baeldung.com/"); crud.put("body", "http://www.baeldung.com/");
crud.put("tags", Arrays.asList(tagLocation)); crud.put("tags", singletonList(tagLocation));
this.mockMvc.perform(get("/crud").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(crud))).andExpect(status().isOk()); this.mockMvc.perform(get("/crud")
} .contentType(MediaTypes.HAL_JSON)
.content(this.objectMapper.writeValueAsString(crud)))
.andExpect(status().isOk());
}
@Test @Test
public void crudCreateExample() throws Exception { public void crudCreateExample() throws Exception {
Map<String, String> tag = new HashMap<String, String>(); Map<String, String> tag = new HashMap<>();
tag.put("name", "CREATE"); tag.put("name", "CREATE");
String tagLocation =this.mockMvc.perform(post("/crud").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(tag))).andExpect(status().isCreated()).andReturn().getResponse().getHeader("Location"); String tagLocation = this.mockMvc.perform(post("/crud")
.contentType(MediaTypes.HAL_JSON)
.content(this.objectMapper.writeValueAsString(tag)))
.andExpect(status().isCreated())
.andReturn()
.getResponse()
.getHeader("Location");
Map<String, Object> crud = new HashMap<String, Object>(); Map<String, Object> crud = new HashMap<>();
crud.put("title", "Sample Model"); crud.put("title", "Sample Model");
crud.put("body", "http://www.baeldung.com/"); crud.put("body", "http://www.baeldung.com/");
crud.put("tags", Arrays.asList(tagLocation)); crud.put("tags", singletonList(tagLocation));
ConstrainedFields fields = new ConstrainedFields(CrudInput.class); ConstrainedFields fields = new ConstrainedFields(CrudInput.class);
this.document.snippets(requestFields(fields.withPath("title").description("The title of the note"),fields.withPath("body").description("The body of the note"),fields.withPath("tags").description("An array of tag resource URIs"))); this.document.snippets(requestFields(
this.mockMvc.perform(post("/crud").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(crud))).andExpect(status().isCreated()); fields.withPath("title").description("The title of the note"),
fields.withPath("body").description("The body of the note"),
fields.withPath("tags").description("An array of tag resource URIs")));
this.mockMvc.perform(post("/crud").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(crud))).andExpect(status().isCreated());
} }
@Test @Test
public void crudDeleteExample() throws Exception { public void crudDeleteExample() throws Exception {
Map<String, String> tag = new HashMap<String, String>(); Map<String, String> tag = new HashMap<>();
tag.put("name", "DELETE"); tag.put("name", "DELETE");
String tagLocation =this.mockMvc.perform(delete("/crud/10").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(tag))).andExpect(status().isOk()).andReturn().getResponse().getHeader("Location"); String tagLocation = this.mockMvc.perform(delete("/crud/10")
Map<String, Object> crud = new HashMap<String, Object>(); .contentType(MediaTypes.HAL_JSON)
crud.put("title", "Sample Model"); .content(this.objectMapper.writeValueAsString(tag)))
crud.put("body", "http://www.baeldung.com/"); .andExpect(status().isOk())
crud.put("tags", Arrays.asList(tagLocation)); .andReturn()
.getResponse()
.getHeader("Location");
this.mockMvc.perform(delete("/crud/10").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(crud))).andExpect(status().isOk()); Map<String, Object> crud = new HashMap<>();
} crud.put("title", "Sample Model");
crud.put("body", "http://www.baeldung.com/");
crud.put("tags", singletonList(tagLocation));
@Test this.mockMvc.perform(delete("/crud/10")
public void crudPatchExample() throws Exception { .contentType(MediaTypes.HAL_JSON)
.content(this.objectMapper.writeValueAsString(crud)))
.andExpect(status().isOk());
}
Map<String, String> tag = new HashMap<String, String>(); @Test
tag.put("name", "PATCH"); public void crudPatchExample() throws Exception {
String tagLocation =this.mockMvc.perform(patch("/crud/10").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(tag))).andExpect(status().isNoContent()).andReturn().getResponse().getHeader("Location"); Map<String, String> tag = new HashMap<>();
Map<String, Object> crud = new HashMap<String, Object>(); tag.put("name", "PATCH");
crud.put("title", "Sample Model");
crud.put("body", "http://www.baeldung.com/");
crud.put("tags", Arrays.asList(tagLocation));
this.mockMvc.perform(patch("/crud/10").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(crud))).andExpect(status().isNoContent()); String tagLocation = this.mockMvc.perform(patch("/crud/10")
} .contentType(MediaTypes.HAL_JSON)
.content(this.objectMapper.writeValueAsString(tag)))
.andExpect(status().isNoContent())
.andReturn()
.getResponse()
.getHeader("Location");
Map<String, Object> crud = new HashMap<>();
crud.put("title", "Sample Model");
crud.put("body", "http://www.baeldung.com/");
crud.put("tags", singletonList(tagLocation));
this.mockMvc.perform(patch("/crud/10")
.contentType(MediaTypes.HAL_JSON)
.content(this.objectMapper.writeValueAsString(crud)))
.andExpect(status().isNoContent());
}
@Test @Test
public void crudPutExample() throws Exception { public void crudPutExample() throws Exception {
Map<String, String> tag = new HashMap<String, String>(); Map<String, String> tag = new HashMap<>();
tag.put("name", "PUT"); tag.put("name", "PUT");
String tagLocation =this.mockMvc.perform(put("/crud/10").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(tag))).andExpect(status().isAccepted()).andReturn().getResponse().getHeader("Location"); String tagLocation = this.mockMvc.perform(put("/crud/10")
Map<String, Object> crud = new HashMap<String, Object>(); .contentType(MediaTypes.HAL_JSON)
crud.put("title", "Sample Model"); .content(this.objectMapper.writeValueAsString(tag)))
crud.put("body", "http://www.baeldung.com/"); .andExpect(status().isAccepted())
crud.put("tags", Arrays.asList(tagLocation)); .andReturn()
.getResponse()
.getHeader("Location");
this.mockMvc.perform(put("/crud/10").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(crud))).andExpect(status().isAccepted()); Map<String, Object> crud = new HashMap<>();
} crud.put("title", "Sample Model");
crud.put("body", "http://www.baeldung.com/");
crud.put("tags", singletonList(tagLocation));
this.mockMvc.perform(put("/crud/10")
.contentType(MediaTypes.HAL_JSON)
.content(this.objectMapper.writeValueAsString(crud)))
.andExpect(status().isAccepted());
}
@Test @Test
public void contextLoads() { public void contextLoads() {
} }
private static class ConstrainedFields { private static class ConstrainedFields {
private final ConstraintDescriptions constraintDescriptions; private final ConstraintDescriptions constraintDescriptions;
ConstrainedFields(Class<?> input) { ConstrainedFields(Class<?> input) {
this.constraintDescriptions = new ConstraintDescriptions(input); this.constraintDescriptions = new ConstraintDescriptions(input);
} }
private FieldDescriptor withPath(String path) { private FieldDescriptor withPath(String path) {
return fieldWithPath(path).attributes(key("constraints").value(StringUtils.collectionToDelimitedString(this.constraintDescriptions.descriptionsForProperty(path), ". "))); return fieldWithPath(path)
} .attributes(key("constraints")
} .value(collectionToDelimitedString(this.constraintDescriptions.descriptionsForProperty(path), ". ")));
}
}
} }

View File

@ -1,25 +1,8 @@
package com.example; package com.example;
import static org.hamcrest.Matchers.hasSize; import com.fasterxml.jackson.core.JsonProcessingException;
import static org.hamcrest.Matchers.is; import com.fasterxml.jackson.databind.ObjectMapper;
import static org.hamcrest.Matchers.notNullValue; import com.jayway.jsonpath.JsonPath;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.patch;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessResponse;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint;
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;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
@ -35,114 +18,141 @@ import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.WebApplicationContext;
import com.fasterxml.jackson.core.JsonProcessingException; import java.io.UnsupportedEncodingException;
import com.fasterxml.jackson.databind.ObjectMapper; import java.util.Arrays;
import com.jayway.jsonpath.JsonPath; import java.util.HashMap;
import java.util.Map;
import static org.hamcrest.Matchers.*;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.*;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SpringRestDocsApplication.class) @SpringApplicationConfiguration(classes = SpringRestDocsApplication.class)
@WebAppConfiguration @WebAppConfiguration
public class GettingStartedDocumentation { public class GettingStartedDocumentation {
@Rule @Rule
public final RestDocumentation restDocumentation = new RestDocumentation("target/generated-snippets"); public final RestDocumentation restDocumentation = new RestDocumentation("target/generated-snippets");
@Autowired @Autowired
private ObjectMapper objectMapper; private ObjectMapper objectMapper;
@Autowired @Autowired
private WebApplicationContext context; private WebApplicationContext context;
private MockMvc mockMvc; private MockMvc mockMvc;
@Before @Before
public void setUp() { public void setUp() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context) this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(documentationConfiguration(this.restDocumentation)).alwaysDo(document("{method-name}/{step}/", .apply(documentationConfiguration(this.restDocumentation))
preprocessRequest(prettyPrint()),preprocessResponse(prettyPrint()))).build(); .alwaysDo(document("{method-name}/{step}/",
} preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint())))
.build();
}
@Test @Test
public void index() throws Exception { public void index() throws Exception {
this.mockMvc.perform(get("/").accept(MediaTypes.HAL_JSON)).andExpect(status().isOk()).andExpect(jsonPath("_links.crud", is(notNullValue()))).andExpect(jsonPath("_links.crud", is(notNullValue()))); this.mockMvc.perform(get("/").accept(MediaTypes.HAL_JSON))
} .andExpect(status().isOk())
.andExpect(jsonPath("_links.crud", is(notNullValue())))
.andExpect(jsonPath("_links.crud", is(notNullValue())));
}
// String createNote() throws Exception {
//@Test // Map<String, String> note = new HashMap<String, String>();
public void creatingANote() throws JsonProcessingException, Exception { // note.put("title", "Note creation with cURL");
String noteLocation = createNote(); // note.put("body", "An example of how to create a note using curl");
MvcResult note = getNote(noteLocation); // String noteLocation = this.mockMvc.perform(post("/crud")
String tagLocation = createTag(); // .contentType(MediaTypes.HAL_JSON)
getTag(tagLocation); // .content(objectMapper.writeValueAsString(note)))
String taggedNoteLocation = createTaggedNote(tagLocation); // .andExpect(status().isCreated())
MvcResult taggedNote = getNote(taggedNoteLocation); // .andExpect(header().string("Location", notNullValue()))
getTags(getLink(taggedNote, "note-tags")); // .andReturn()
tagExistingNote(noteLocation, tagLocation); // .getResponse()
getTags(getLink(note, "note-tags")); // .getHeader("Location");
} // return noteLocation;
// }
String createNote() throws Exception { //
Map<String, String> note = new HashMap<String, String>(); // MvcResult getNote(String noteLocation) throws Exception {
note.put("title", "Note creation with cURL"); // return this.mockMvc.perform(get(noteLocation))
note.put("body", "An example of how to create a note using cURL"); // .andExpect(status().isOk())
String noteLocation = this.mockMvc.perform(post("/crud").contentType(MediaTypes.HAL_JSON).content(objectMapper.writeValueAsString(note))).andExpect(status().isCreated()).andExpect(header().string("Location", notNullValue())).andReturn().getResponse().getHeader("Location"); // .andExpect(jsonPath("title", is(notNullValue())))
return noteLocation; // .andExpect(jsonPath("body", is(notNullValue())))
} // .andExpect(jsonPath("_links.crud", is(notNullValue())))
// .andReturn();
MvcResult getNote(String noteLocation) throws Exception { // }
return this.mockMvc.perform(get(noteLocation)).andExpect(status().isOk()).andExpect(jsonPath("title", is(notNullValue()))).andExpect(jsonPath("body", is(notNullValue()))).andExpect(jsonPath("_links.crud", is(notNullValue()))).andReturn(); //
} //
// String createTag() throws Exception, JsonProcessingException {
// Map<String, String> tag = new HashMap<String, String>();
String createTag() throws Exception, JsonProcessingException { // tag.put("name", "getting-started");
Map<String, String> tag = new HashMap<String, String>(); // String tagLocation = this.mockMvc.perform(post("/crud")
tag.put("name", "getting-started"); // .contentType(MediaTypes.HAL_JSON)
String tagLocation = this.mockMvc.perform(post("/crud").contentType(MediaTypes.HAL_JSON).content(objectMapper.writeValueAsString(tag))).andExpect(status().isCreated()).andExpect(header().string("Location", notNullValue())).andReturn().getResponse().getHeader("Location"); // .content(objectMapper.writeValueAsString(tag)))
return tagLocation; // .andExpect(status().isCreated())
} // .andExpect(header().string("Location", notNullValue()))
// .andReturn()
void getTag(String tagLocation) throws Exception { // .getResponse()
this.mockMvc.perform(get(tagLocation)).andExpect(status().isOk()) // .getHeader("Location");
.andExpect(jsonPath("name", is(notNullValue()))) // return tagLocation;
.andExpect(jsonPath("_links.tagged-notes", is(notNullValue()))); // }
} //
// void getTag(String tagLocation) throws Exception {
String createTaggedNote(String tag) throws Exception { // this.mockMvc.perform(get(tagLocation)).andExpect(status().isOk())
Map<String, Object> note = new HashMap<String, Object>(); // .andExpect(jsonPath("name", is(notNullValue())))
note.put("title", "Tagged note creation with cURL"); // .andExpect(jsonPath("_links.tagged-notes", is(notNullValue())));
note.put("body", "An example of how to create a tagged note using cURL"); // }
note.put("tags", Arrays.asList(tag)); //
// String createTaggedNote(String tag) throws Exception {
String noteLocation = this.mockMvc.perform(post("/notes").contentType(MediaTypes.HAL_JSON).content(objectMapper.writeValueAsString(note))) // Map<String, Object> note = new HashMap<String, Object>();
.andExpect(status().isCreated()).andExpect(header().string("Location", notNullValue())).andReturn().getResponse().getHeader("Location"); // note.put("title", "Tagged note creation with cURL");
return noteLocation; // note.put("body", "An example of how to create a tagged note using cURL");
} // note.put("tags", Arrays.asList(tag));
//
void getTags(String noteTagsLocation) throws Exception { // String noteLocation = this.mockMvc.perform(post("/notes")
this.mockMvc.perform(get(noteTagsLocation)) // .contentType(MediaTypes.HAL_JSON)
.andExpect(status().isOk()) // .content(objectMapper.writeValueAsString(note)))
.andExpect(jsonPath("_embedded.tags", hasSize(1))); // .andExpect(status().isCreated())
} // .andExpect(header().string("Location", notNullValue()))
// .andReturn()
void tagExistingNote(String noteLocation, String tagLocation) throws Exception { // .getResponse()
Map<String, Object> update = new HashMap<String, Object>(); // .getHeader("Location");
update.put("tags", Arrays.asList(tagLocation)); // return noteLocation;
this.mockMvc.perform(patch(noteLocation).contentType(MediaTypes.HAL_JSON).content(objectMapper.writeValueAsString(update))).andExpect(status().isNoContent()); // }
} //
// void getTags(String noteTagsLocation) throws Exception {
MvcResult getTaggedExistingNote(String noteLocation) throws Exception { // this.mockMvc.perform(get(noteTagsLocation))
return this.mockMvc.perform(get(noteLocation)).andExpect(status().isOk()).andReturn(); // .andExpect(status().isOk())
} // .andExpect(jsonPath("_embedded.tags", hasSize(1)));
// }
void getTagsForExistingNote(String noteTagsLocation) throws Exception { //
this.mockMvc.perform(get(noteTagsLocation)) // void tagExistingNote(String noteLocation, String tagLocation) throws Exception {
.andExpect(status().isOk()).andExpect(jsonPath("_embedded.tags", hasSize(1))); // Map<String, Object> update = new HashMap<String, Object>();
} // update.put("tags", Arrays.asList(tagLocation));
// this.mockMvc.perform(patch(noteLocation)
private String getLink(MvcResult result, String rel) // .contentType(MediaTypes.HAL_JSON)
throws UnsupportedEncodingException { // .content(objectMapper.writeValueAsString(update)))
return JsonPath.parse(result.getResponse().getContentAsString()).read("_links." + rel + ".href"); // .andExpect(status().isNoContent());
} // }
//
// MvcResult getTaggedExistingNote(String noteLocation) throws Exception {
// return this.mockMvc.perform(get(noteLocation)).andExpect(status().isOk()).andReturn();
// }
//
// void getTagsForExistingNote(String noteTagsLocation) throws Exception {
// this.mockMvc.perform(get(noteTagsLocation))
// .andExpect(status().isOk()).andExpect(jsonPath("_embedded.tags", hasSize(1)));
// }
//
// private String getLink(MvcResult result, String rel)
// throws UnsupportedEncodingException {
// return JsonPath.parse(result.getResponse().getContentAsString()).read("_links." + rel + ".href");
// }
} }