minor formatting work

This commit is contained in:
eugenp 2016-07-26 11:34:56 +03:00
parent bf78a04b68
commit 152d9b38de
6 changed files with 173 additions and 245 deletions

View File

@ -19,8 +19,7 @@ public class CrudInput {
private final List<URI> tagUris;
@JsonCreator
public CrudInput(@JsonProperty("title") String title,
@JsonProperty("body") String body, @JsonProperty("tags") List<URI> tagUris) {
public CrudInput(@JsonProperty("title") String title, @JsonProperty("body") String body, @JsonProperty("tags") List<URI> tagUris) {
this.title = title;
this.body = body;
this.tagUris = tagUris == null ? Collections.<URI> emptyList() : tagUris;

View File

@ -1,6 +1,5 @@
package com.example;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import org.springframework.hateoas.ResourceSupport;

View File

@ -56,55 +56,35 @@ public class ApiDocumentation {
@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();
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).apply(documentationConfiguration(this.restDocumentation)).alwaysDo(this.document).build();
}
@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());
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());
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");
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());
this.mockMvc.perform(get("/crud").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(crud))).andExpect(status().isOk());
}
@Test
@ -112,13 +92,7 @@ public class ApiDocumentation {
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");
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");
@ -126,13 +100,9 @@ public class ApiDocumentation {
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.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
@ -141,23 +111,14 @@ public class ApiDocumentation {
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");
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());
this.mockMvc.perform(delete("/crud/10").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(crud))).andExpect(status().isOk());
}
@Test
@ -166,51 +127,31 @@ public class ApiDocumentation {
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");
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());
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");
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());
this.mockMvc.perform(put("/crud/10").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(crud))).andExpect(status().isAccepted());
}
@Test
public void contextLoads() {
}
@ -224,11 +165,8 @@ public class ApiDocumentation {
}
private FieldDescriptor withPath(String path) {
return fieldWithPath(path)
.attributes(key("constraints")
.value(collectionToDelimitedString(this.constraintDescriptions.descriptionsForProperty(path), ". ")));
return fieldWithPath(path).attributes(key("constraints").value(collectionToDelimitedString(this.constraintDescriptions.descriptionsForProperty(path), ". ")));
}
}
}

View File

@ -46,22 +46,14 @@ public class GettingStartedDocumentation {
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();
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())));
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 {