Clean up Spring REST Docs examples
This commit is contained in:
parent
28d6a0113b
commit
e7ec50034a
|
@ -1,29 +1,6 @@
|
|||
package com.example;
|
||||
|
||||
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.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 com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
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.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.util.StringUtils;
|
||||
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)
|
||||
@SpringApplicationConfiguration(classes = SpringRestDocsApplication.class)
|
||||
@WebAppConfiguration
|
||||
public class ApiDocumentation {
|
||||
|
||||
@Rule
|
||||
public final RestDocumentation restDocumentation = new RestDocumentation("target/generated-snippets");
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext context;
|
||||
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
private RestDocumentationResultHandler document;
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
this.document = document("{method-name}",preprocessRequest(prettyPrint()),preprocessResponse(prettyPrint()));
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).apply(documentationConfiguration(this.restDocumentation)).alwaysDo(this.document).build();
|
||||
}
|
||||
|
||||
@Rule
|
||||
public final RestDocumentation restDocumentation = new RestDocumentation("target/generated-snippets");
|
||||
|
||||
@Test
|
||||
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.mockMvc.perform(get("/")).andExpect(status().isOk());
|
||||
}
|
||||
|
||||
@Test
|
||||
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.mockMvc.perform(get("/")).andExpect(status().isOk());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void crudGetExample() throws Exception {
|
||||
|
||||
Map<String, String> tag = new HashMap<String, String>();
|
||||
tag.put("name", "GET");
|
||||
@Autowired
|
||||
private WebApplicationContext context;
|
||||
|
||||
String tagLocation =this.mockMvc.perform(get("/crud").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(tag))).andExpect(status().isOk()).andReturn().getResponse().getHeader("Location");
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
Map<String, Object> crud = new HashMap<String, Object>();
|
||||
crud.put("title", "Sample Model");
|
||||
crud.put("body", "http://www.baeldung.com/");
|
||||
crud.put("tags", Arrays.asList(tagLocation));
|
||||
|
||||
this.mockMvc.perform(get("/crud").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(crud))).andExpect(status().isOk());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void crudCreateExample() throws Exception {
|
||||
Map<String, String> tag = new HashMap<String, String>();
|
||||
tag.put("name", "CREATE");
|
||||
private RestDocumentationResultHandler document;
|
||||
|
||||
String tagLocation =this.mockMvc.perform(post("/crud").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(tag))).andExpect(status().isCreated()).andReturn().getResponse().getHeader("Location");
|
||||
private MockMvc mockMvc;
|
||||
|
||||
Map<String, Object> crud = new HashMap<String, Object>();
|
||||
crud.put("title", "Sample Model");
|
||||
crud.put("body", "http://www.baeldung.com/");
|
||||
crud.put("tags", Arrays.asList(tagLocation));
|
||||
@Before
|
||||
public void setUp() {
|
||||
this.document = document("{method-name}", preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint()));
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
|
||||
.apply(documentationConfiguration(this.restDocumentation))
|
||||
.alwaysDo(this.document)
|
||||
.build();
|
||||
}
|
||||
|
||||
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.mockMvc.perform(post("/crud").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(crud))).andExpect(status().isCreated());
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void crudDeleteExample() throws Exception {
|
||||
|
||||
Map<String, String> tag = new HashMap<String, String>();
|
||||
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");
|
||||
Map<String, Object> crud = new HashMap<String, Object>();
|
||||
crud.put("title", "Sample Model");
|
||||
crud.put("body", "http://www.baeldung.com/");
|
||||
crud.put("tags", Arrays.asList(tagLocation));
|
||||
|
||||
this.mockMvc.perform(delete("/crud/10").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(crud))).andExpect(status().isOk());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void crudPatchExample() throws Exception {
|
||||
|
||||
Map<String, String> tag = new HashMap<String, String>();
|
||||
tag.put("name", "PATCH");
|
||||
|
||||
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<String, Object>();
|
||||
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());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void crudPutExample() throws Exception {
|
||||
Map<String, String> tag = new HashMap<String, String>();
|
||||
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");
|
||||
Map<String, Object> crud = new HashMap<String, Object>();
|
||||
crud.put("title", "Sample Model");
|
||||
crud.put("body", "http://www.baeldung.com/");
|
||||
crud.put("tags", Arrays.asList(tagLocation));
|
||||
|
||||
this.mockMvc.perform(put("/crud/10").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(crud))).andExpect(status().isAccepted());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
private static class ConstrainedFields {
|
||||
|
||||
private final ConstraintDescriptions constraintDescriptions;
|
||||
@Test
|
||||
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.mockMvc.perform(get("/"))
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
@Test
|
||||
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.mockMvc.perform(get("/"))
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void crudGetExample() throws Exception {
|
||||
|
||||
Map<String, String> tag = new HashMap<>();
|
||||
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");
|
||||
|
||||
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(get("/crud")
|
||||
.contentType(MediaTypes.HAL_JSON)
|
||||
.content(this.objectMapper.writeValueAsString(crud)))
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void crudCreateExample() throws Exception {
|
||||
Map<String, String> tag = new HashMap<>();
|
||||
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");
|
||||
|
||||
Map<String, Object> crud = new HashMap<>();
|
||||
crud.put("title", "Sample Model");
|
||||
crud.put("body", "http://www.baeldung.com/");
|
||||
crud.put("tags", singletonList(tagLocation));
|
||||
|
||||
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.mockMvc.perform(post("/crud").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(crud))).andExpect(status().isCreated());
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void crudDeleteExample() throws Exception {
|
||||
|
||||
Map<String, String> tag = new HashMap<>();
|
||||
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");
|
||||
|
||||
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(delete("/crud/10")
|
||||
.contentType(MediaTypes.HAL_JSON)
|
||||
.content(this.objectMapper.writeValueAsString(crud)))
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void crudPatchExample() throws Exception {
|
||||
|
||||
Map<String, String> tag = new HashMap<>();
|
||||
tag.put("name", "PATCH");
|
||||
|
||||
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
|
||||
public void crudPutExample() throws Exception {
|
||||
Map<String, String> tag = new HashMap<>();
|
||||
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");
|
||||
|
||||
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
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
private static class ConstrainedFields {
|
||||
|
||||
private final ConstraintDescriptions constraintDescriptions;
|
||||
|
||||
ConstrainedFields(Class<?> input) {
|
||||
this.constraintDescriptions = new ConstraintDescriptions(input);
|
||||
}
|
||||
|
||||
private FieldDescriptor withPath(String path) {
|
||||
return fieldWithPath(path)
|
||||
.attributes(key("constraints")
|
||||
.value(collectionToDelimitedString(this.constraintDescriptions.descriptionsForProperty(path), ". ")));
|
||||
}
|
||||
}
|
||||
|
||||
ConstrainedFields(Class<?> input) {
|
||||
this.constraintDescriptions = new ConstraintDescriptions(input);
|
||||
}
|
||||
|
||||
private FieldDescriptor withPath(String path) {
|
||||
return fieldWithPath(path).attributes(key("constraints").value(StringUtils.collectionToDelimitedString(this.constraintDescriptions.descriptionsForProperty(path), ". ")));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -1,25 +1,8 @@
|
|||
package com.example;
|
||||
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
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 com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.jayway.jsonpath.JsonPath;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
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.web.context.WebApplicationContext;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.jayway.jsonpath.JsonPath;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.Arrays;
|
||||
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)
|
||||
@SpringApplicationConfiguration(classes = SpringRestDocsApplication.class)
|
||||
@WebAppConfiguration
|
||||
public class GettingStartedDocumentation {
|
||||
|
||||
@Rule
|
||||
public final RestDocumentation restDocumentation = new RestDocumentation("target/generated-snippets");
|
||||
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper;
|
||||
@Rule
|
||||
public final RestDocumentation restDocumentation = new RestDocumentation("target/generated-snippets");
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext context;
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
|
||||
.apply(documentationConfiguration(this.restDocumentation)).alwaysDo(document("{method-name}/{step}/",
|
||||
preprocessRequest(prettyPrint()),preprocessResponse(prettyPrint()))).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
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())));
|
||||
}
|
||||
|
||||
|
||||
//@Test
|
||||
public void creatingANote() throws JsonProcessingException, Exception {
|
||||
String noteLocation = createNote();
|
||||
MvcResult note = getNote(noteLocation);
|
||||
String tagLocation = createTag();
|
||||
getTag(tagLocation);
|
||||
String taggedNoteLocation = createTaggedNote(tagLocation);
|
||||
MvcResult taggedNote = getNote(taggedNoteLocation);
|
||||
getTags(getLink(taggedNote, "note-tags"));
|
||||
tagExistingNote(noteLocation, tagLocation);
|
||||
getTags(getLink(note, "note-tags"));
|
||||
}
|
||||
|
||||
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("/crud").contentType(MediaTypes.HAL_JSON).content(objectMapper.writeValueAsString(note))).andExpect(status().isCreated()).andExpect(header().string("Location", notNullValue())).andReturn().getResponse().getHeader("Location");
|
||||
return noteLocation;
|
||||
}
|
||||
@Autowired
|
||||
private WebApplicationContext context;
|
||||
|
||||
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>();
|
||||
tag.put("name", "getting-started");
|
||||
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");
|
||||
return tagLocation;
|
||||
}
|
||||
private MockMvc mockMvc;
|
||||
|
||||
void getTag(String tagLocation) throws Exception {
|
||||
this.mockMvc.perform(get(tagLocation)).andExpect(status().isOk())
|
||||
.andExpect(jsonPath("name", is(notNullValue())))
|
||||
.andExpect(jsonPath("_links.tagged-notes", is(notNullValue())));
|
||||
}
|
||||
|
||||
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");
|
||||
note.put("tags", Arrays.asList(tag));
|
||||
@Before
|
||||
public void setUp() {
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
|
||||
.apply(documentationConfiguration(this.restDocumentation))
|
||||
.alwaysDo(document("{method-name}/{step}/",
|
||||
preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint())))
|
||||
.build();
|
||||
}
|
||||
|
||||
String noteLocation = this.mockMvc.perform(post("/notes").contentType(MediaTypes.HAL_JSON).content(objectMapper.writeValueAsString(note)))
|
||||
.andExpect(status().isCreated()).andExpect(header().string("Location", notNullValue())).andReturn().getResponse().getHeader("Location");
|
||||
return noteLocation;
|
||||
}
|
||||
|
||||
void getTags(String noteTagsLocation) throws Exception {
|
||||
this.mockMvc.perform(get(noteTagsLocation))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("_embedded.tags", hasSize(1)));
|
||||
}
|
||||
@Test
|
||||
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())));
|
||||
}
|
||||
|
||||
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).contentType(MediaTypes.HAL_JSON).content(objectMapper.writeValueAsString(update))).andExpect(status().isNoContent());
|
||||
}
|
||||
// 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("/crud")
|
||||
// .contentType(MediaTypes.HAL_JSON)
|
||||
// .content(objectMapper.writeValueAsString(note)))
|
||||
// .andExpect(status().isCreated())
|
||||
// .andExpect(header().string("Location", notNullValue()))
|
||||
// .andReturn()
|
||||
// .getResponse()
|
||||
// .getHeader("Location");
|
||||
// return noteLocation;
|
||||
// }
|
||||
//
|
||||
// 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>();
|
||||
// tag.put("name", "getting-started");
|
||||
// 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");
|
||||
// return tagLocation;
|
||||
// }
|
||||
//
|
||||
// void getTag(String tagLocation) throws Exception {
|
||||
// this.mockMvc.perform(get(tagLocation)).andExpect(status().isOk())
|
||||
// .andExpect(jsonPath("name", is(notNullValue())))
|
||||
// .andExpect(jsonPath("_links.tagged-notes", is(notNullValue())));
|
||||
// }
|
||||
//
|
||||
// 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");
|
||||
// note.put("tags", Arrays.asList(tag));
|
||||
//
|
||||
// String noteLocation = this.mockMvc.perform(post("/notes")
|
||||
// .contentType(MediaTypes.HAL_JSON)
|
||||
// .content(objectMapper.writeValueAsString(note)))
|
||||
// .andExpect(status().isCreated())
|
||||
// .andExpect(header().string("Location", notNullValue()))
|
||||
// .andReturn()
|
||||
// .getResponse()
|
||||
// .getHeader("Location");
|
||||
// return noteLocation;
|
||||
// }
|
||||
//
|
||||
// void getTags(String noteTagsLocation) throws Exception {
|
||||
// this.mockMvc.perform(get(noteTagsLocation))
|
||||
// .andExpect(status().isOk())
|
||||
// .andExpect(jsonPath("_embedded.tags", hasSize(1)));
|
||||
// }
|
||||
//
|
||||
// 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)
|
||||
// .contentType(MediaTypes.HAL_JSON)
|
||||
// .content(objectMapper.writeValueAsString(update)))
|
||||
// .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");
|
||||
// }
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue