From 152d9b38de667c2c710e03bdcc9225189dfc97f6 Mon Sep 17 00:00:00 2001 From: eugenp Date: Tue, 26 Jul 2016 11:34:56 +0300 Subject: [PATCH] minor formatting work --- .../main/java/com/example/CRUDController.java | 70 +++---- .../src/main/java/com/example/CrudInput.java | 43 ++-- .../java/com/example/IndexController.java | 13 +- .../example/SpringRestDocsApplication.java | 6 +- .../java/com/example/ApiDocumentation.java | 94 ++------- .../example/GettingStartedDocumentation.java | 192 +++++++++--------- 6 files changed, 173 insertions(+), 245 deletions(-) diff --git a/spring-rest-docs/src/main/java/com/example/CRUDController.java b/spring-rest-docs/src/main/java/com/example/CRUDController.java index 818b29d3a6..ff8c91d6cd 100644 --- a/spring-rest-docs/src/main/java/com/example/CRUDController.java +++ b/spring-rest-docs/src/main/java/com/example/CRUDController.java @@ -17,39 +17,39 @@ import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/crud") public class CRUDController { - - @RequestMapping(method=RequestMethod.GET) - @ResponseStatus(HttpStatus.OK) - public List read(@RequestBody CrudInput crudInput) { - List returnList=new ArrayList(); - returnList.add(crudInput); - return returnList; - } - - @ResponseStatus(HttpStatus.CREATED) - @RequestMapping(method=RequestMethod.POST) - public HttpHeaders save(@RequestBody CrudInput crudInput) { - HttpHeaders httpHeaders = new HttpHeaders(); - httpHeaders.setLocation(linkTo(CRUDController.class).slash(crudInput.getTitle()).toUri()); - return httpHeaders; - } - - @RequestMapping(value = "/{id}", method = RequestMethod.DELETE) - @ResponseStatus(HttpStatus.OK) - HttpHeaders delete(@RequestBody CrudInput crudInput) { - HttpHeaders httpHeaders = new HttpHeaders(); - return httpHeaders; - } - - @RequestMapping(value = "/{id}", method = RequestMethod.PUT) - @ResponseStatus(HttpStatus.ACCEPTED) - void put(@PathVariable("id") long id, @RequestBody CrudInput crudInput) { - - } - - @RequestMapping(value = "/{id}", method = RequestMethod.PATCH) - @ResponseStatus(HttpStatus.NO_CONTENT) - void patch(@PathVariable("id") long id, @RequestBody CrudInput crudInput) { - - } + + @RequestMapping(method = RequestMethod.GET) + @ResponseStatus(HttpStatus.OK) + public List read(@RequestBody CrudInput crudInput) { + List returnList = new ArrayList(); + returnList.add(crudInput); + return returnList; + } + + @ResponseStatus(HttpStatus.CREATED) + @RequestMapping(method = RequestMethod.POST) + public HttpHeaders save(@RequestBody CrudInput crudInput) { + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.setLocation(linkTo(CRUDController.class).slash(crudInput.getTitle()).toUri()); + return httpHeaders; + } + + @RequestMapping(value = "/{id}", method = RequestMethod.DELETE) + @ResponseStatus(HttpStatus.OK) + HttpHeaders delete(@RequestBody CrudInput crudInput) { + HttpHeaders httpHeaders = new HttpHeaders(); + return httpHeaders; + } + + @RequestMapping(value = "/{id}", method = RequestMethod.PUT) + @ResponseStatus(HttpStatus.ACCEPTED) + void put(@PathVariable("id") long id, @RequestBody CrudInput crudInput) { + + } + + @RequestMapping(value = "/{id}", method = RequestMethod.PATCH) + @ResponseStatus(HttpStatus.NO_CONTENT) + void patch(@PathVariable("id") long id, @RequestBody CrudInput crudInput) { + + } } diff --git a/spring-rest-docs/src/main/java/com/example/CrudInput.java b/spring-rest-docs/src/main/java/com/example/CrudInput.java index 3d91b7d45a..36ad67eb21 100644 --- a/spring-rest-docs/src/main/java/com/example/CrudInput.java +++ b/spring-rest-docs/src/main/java/com/example/CrudInput.java @@ -10,33 +10,32 @@ import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; public class CrudInput { - - //@NotBlank - private final String title; - private final String body; + // @NotBlank + private final String title; - private final List tagUris; + private final String body; - @JsonCreator - public CrudInput(@JsonProperty("title") String title, - @JsonProperty("body") String body, @JsonProperty("tags") List tagUris) { - this.title = title; - this.body = body; - this.tagUris = tagUris == null ? Collections.emptyList() : tagUris; - } + private final List tagUris; - public String getTitle() { - return title; - } + @JsonCreator + public CrudInput(@JsonProperty("title") String title, @JsonProperty("body") String body, @JsonProperty("tags") List tagUris) { + this.title = title; + this.body = body; + this.tagUris = tagUris == null ? Collections. emptyList() : tagUris; + } - public String getBody() { - return body; - } + public String getTitle() { + return title; + } - @JsonProperty("tags") - public List getTagUris() { - return this.tagUris; - } + public String getBody() { + return body; + } + + @JsonProperty("tags") + public List getTagUris() { + return this.tagUris; + } } \ No newline at end of file diff --git a/spring-rest-docs/src/main/java/com/example/IndexController.java b/spring-rest-docs/src/main/java/com/example/IndexController.java index a6b4537c43..6b896da416 100644 --- a/spring-rest-docs/src/main/java/com/example/IndexController.java +++ b/spring-rest-docs/src/main/java/com/example/IndexController.java @@ -1,6 +1,5 @@ package com.example; - import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo; import org.springframework.hateoas.ResourceSupport; @@ -12,11 +11,11 @@ import org.springframework.web.bind.annotation.RestController; @RequestMapping("/") public class IndexController { - @RequestMapping(method=RequestMethod.GET) - public ResourceSupport index() { - ResourceSupport index = new ResourceSupport(); - index.add(linkTo(CRUDController.class).withRel("crud")); - return index; - } + @RequestMapping(method = RequestMethod.GET) + public ResourceSupport index() { + ResourceSupport index = new ResourceSupport(); + index.add(linkTo(CRUDController.class).withRel("crud")); + return index; + } } \ No newline at end of file diff --git a/spring-rest-docs/src/main/java/com/example/SpringRestDocsApplication.java b/spring-rest-docs/src/main/java/com/example/SpringRestDocsApplication.java index dd20ef324e..da09f9accc 100644 --- a/spring-rest-docs/src/main/java/com/example/SpringRestDocsApplication.java +++ b/spring-rest-docs/src/main/java/com/example/SpringRestDocsApplication.java @@ -6,7 +6,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringRestDocsApplication { - public static void main(String[] args) { - SpringApplication.run(SpringRestDocsApplication.class, args); - } + public static void main(String[] args) { + SpringApplication.run(SpringRestDocsApplication.class, args); + } } diff --git a/spring-rest-docs/src/test/java/com/example/ApiDocumentation.java b/spring-rest-docs/src/test/java/com/example/ApiDocumentation.java index 195b9dc514..5b753aff0c 100644 --- a/spring-rest-docs/src/test/java/com/example/ApiDocumentation.java +++ b/spring-rest-docs/src/test/java/com/example/ApiDocumentation.java @@ -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 <>")), - responseFields(fieldWithPath("_links").description("<> to other resources")) - ); - this.mockMvc.perform(get("/")) - .andExpect(status().isOk()); + this.document.snippets(links(linkWithRel("crud").description("The <>")), responseFields(fieldWithPath("_links").description("<> to other resources"))); + this.mockMvc.perform(get("/")).andExpect(status().isOk()); } - @Test public void crudGetExample() throws Exception { Map 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 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 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 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 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 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 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 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 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 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), ". "))); } } - } diff --git a/spring-rest-docs/src/test/java/com/example/GettingStartedDocumentation.java b/spring-rest-docs/src/test/java/com/example/GettingStartedDocumentation.java index 7f3c4db1f9..7aea9d303c 100644 --- a/spring-rest-docs/src/test/java/com/example/GettingStartedDocumentation.java +++ b/spring-rest-docs/src/test/java/com/example/GettingStartedDocumentation.java @@ -46,113 +46,105 @@ 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 { -// Map note = new HashMap(); -// 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 tag = new HashMap(); -// 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 note = new HashMap(); -// 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 update = new HashMap(); -// 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"); -// } + // String createNote() throws Exception { + // Map note = new HashMap(); + // 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 tag = new HashMap(); + // 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 note = new HashMap(); + // 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 update = new HashMap(); + // 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"); + // } }