|
| 1 | +package com.baeldung.restdocs; |
| 2 | + |
| 3 | +import static java.util.Collections.singletonList; |
| 4 | +import static org.springframework.restdocs.headers.HeaderDocumentation.headerWithName; |
| 5 | +import static org.springframework.restdocs.headers.HeaderDocumentation.responseHeaders; |
| 6 | +import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.linkWithRel; |
| 7 | +import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.links; |
| 8 | +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; |
| 9 | +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration; |
| 10 | +import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.delete; |
| 11 | +import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get; |
| 12 | +import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.patch; |
| 13 | +import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post; |
| 14 | +import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.put; |
| 15 | +import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest; |
| 16 | +import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessResponse; |
| 17 | +import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint; |
| 18 | +import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath; |
| 19 | +import static org.springframework.restdocs.payload.PayloadDocumentation.requestFields; |
| 20 | +import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields; |
| 21 | +import static org.springframework.restdocs.request.RequestDocumentation.*; |
| 22 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 23 | +import static org.springframework.util.StringUtils.collectionToDelimitedString; |
| 24 | + |
| 25 | +import java.util.HashMap; |
| 26 | +import java.util.Map; |
| 27 | + |
| 28 | +import org.junit.jupiter.api.BeforeEach; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 31 | +import org.springframework.beans.factory.annotation.Autowired; |
| 32 | +import org.springframework.boot.test.context.SpringBootTest; |
| 33 | +import org.springframework.hateoas.MediaTypes; |
| 34 | +import org.springframework.restdocs.RestDocumentationContextProvider; |
| 35 | +import org.springframework.restdocs.RestDocumentationExtension; |
| 36 | +import org.springframework.restdocs.constraints.ConstraintDescriptions; |
| 37 | +import static org.springframework.restdocs.payload.PayloadDocumentation.subsectionWithPath; |
| 38 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 39 | +import org.springframework.test.web.servlet.MockMvc; |
| 40 | +import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
| 41 | +import org.springframework.web.context.WebApplicationContext; |
| 42 | + |
| 43 | +import com.baeldung.restdocs.CrudInput; |
| 44 | +import com.baeldung.restdocs.SpringRestDocsApplication; |
| 45 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 46 | + |
| 47 | +@ExtendWith({ RestDocumentationExtension.class, SpringExtension.class }) |
| 48 | +@SpringBootTest(classes = SpringRestDocsApplication.class) |
| 49 | +public class ApiDocumentationJUnit5IntegrationTest { |
| 50 | + |
| 51 | + @Autowired |
| 52 | + private ObjectMapper objectMapper; |
| 53 | + |
| 54 | + private MockMvc mockMvc; |
| 55 | + |
| 56 | + @BeforeEach |
| 57 | + public void setUp(WebApplicationContext webApplicationContext, RestDocumentationContextProvider restDocumentation) { |
| 58 | + this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext) |
| 59 | + .apply(documentationConfiguration(restDocumentation)) |
| 60 | + .alwaysDo(document("{method-name}", preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint()))) |
| 61 | + .build(); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void indexExample() throws Exception { |
| 66 | + this.mockMvc.perform(get("/")) |
| 67 | + .andExpect(status().isOk()) |
| 68 | + .andDo(document("index-example", preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint()), links(linkWithRel("crud").description("The CRUD resource")), responseFields(subsectionWithPath("_links").description("Links to other resources")), |
| 69 | + responseHeaders(headerWithName("Content-Type").description("The Content-Type of the payload, e.g. `application/hal+json`")))); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void crudGetExample() throws Exception { |
| 74 | + |
| 75 | + Map<String, String> tag = new HashMap<>(); |
| 76 | + tag.put("name", "GET"); |
| 77 | + |
| 78 | + String tagLocation = this.mockMvc.perform(get("/crud").contentType(MediaTypes.HAL_JSON) |
| 79 | + .content(this.objectMapper.writeValueAsString(tag))) |
| 80 | + .andExpect(status().isOk()) |
| 81 | + .andReturn() |
| 82 | + .getResponse() |
| 83 | + .getHeader("Location"); |
| 84 | + |
| 85 | + Map<String, Object> crud = new HashMap<>(); |
| 86 | + crud.put("id", 1L); |
| 87 | + crud.put("title", "Sample Model"); |
| 88 | + crud.put("body", "http://www.baeldung.com/"); |
| 89 | + crud.put("tags", singletonList(tagLocation)); |
| 90 | + |
| 91 | + ConstraintDescriptions desc = new ConstraintDescriptions(CrudInput.class); |
| 92 | + |
| 93 | + this.mockMvc.perform(get("/crud").contentType(MediaTypes.HAL_JSON) |
| 94 | + .content(this.objectMapper.writeValueAsString(crud))) |
| 95 | + .andExpect(status().isOk()) |
| 96 | + .andDo(document("crud-get-example", preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint()), requestFields(fieldWithPath("id").description("The id of the input" + collectionToDelimitedString(desc.descriptionsForProperty("id"), ". ")), |
| 97 | + fieldWithPath("title").description("The title of the input"), fieldWithPath("body").description("The body of the input"), fieldWithPath("tags").description("An array of tag resource URIs")))); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void crudCreateExample() throws Exception { |
| 102 | + Map<String, String> tag = new HashMap<>(); |
| 103 | + tag.put("name", "CREATE"); |
| 104 | + |
| 105 | + String tagLocation = this.mockMvc.perform(post("/crud").contentType(MediaTypes.HAL_JSON) |
| 106 | + .content(this.objectMapper.writeValueAsString(tag))) |
| 107 | + .andExpect(status().isCreated()) |
| 108 | + .andReturn() |
| 109 | + .getResponse() |
| 110 | + .getHeader("Location"); |
| 111 | + |
| 112 | + Map<String, Object> crud = new HashMap<>(); |
| 113 | + crud.put("id", 2L); |
| 114 | + crud.put("title", "Sample Model"); |
| 115 | + crud.put("body", "http://www.baeldung.com/"); |
| 116 | + crud.put("tags", singletonList(tagLocation)); |
| 117 | + |
| 118 | + this.mockMvc.perform(post("/crud").contentType(MediaTypes.HAL_JSON) |
| 119 | + .content(this.objectMapper.writeValueAsString(crud))) |
| 120 | + .andExpect(status().isCreated()) |
| 121 | + .andDo(document("crud-create-example", preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint()), requestFields(fieldWithPath("id").description("The id of the input"), fieldWithPath("title").description("The title of the input"), |
| 122 | + fieldWithPath("body").description("The body of the input"), fieldWithPath("tags").description("An array of tag resource URIs")))); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void crudDeleteExample() throws Exception { |
| 127 | + this.mockMvc.perform(delete("/crud/{id}", 10)) |
| 128 | + .andExpect(status().isOk()) |
| 129 | + .andDo(document("crud-delete-example", pathParameters(parameterWithName("id").description("The id of the input to delete")))); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + public void crudPatchExample() throws Exception { |
| 134 | + |
| 135 | + Map<String, String> tag = new HashMap<>(); |
| 136 | + tag.put("name", "PATCH"); |
| 137 | + |
| 138 | + String tagLocation = this.mockMvc.perform(patch("/crud/{id}", 10).contentType(MediaTypes.HAL_JSON) |
| 139 | + .content(this.objectMapper.writeValueAsString(tag))) |
| 140 | + .andExpect(status().isOk()) |
| 141 | + .andReturn() |
| 142 | + .getResponse() |
| 143 | + .getHeader("Location"); |
| 144 | + |
| 145 | + Map<String, Object> crud = new HashMap<>(); |
| 146 | + crud.put("title", "Sample Model Patch"); |
| 147 | + crud.put("body", "http://www.baeldung.com/"); |
| 148 | + crud.put("tags", singletonList(tagLocation)); |
| 149 | + |
| 150 | + this.mockMvc.perform(patch("/crud/{id}", 10).contentType(MediaTypes.HAL_JSON) |
| 151 | + .content(this.objectMapper.writeValueAsString(crud))) |
| 152 | + .andExpect(status().isOk()); |
| 153 | + } |
| 154 | + |
| 155 | + @Test |
| 156 | + public void crudPutExample() throws Exception { |
| 157 | + Map<String, String> tag = new HashMap<>(); |
| 158 | + tag.put("name", "PUT"); |
| 159 | + |
| 160 | + String tagLocation = this.mockMvc.perform(put("/crud/{id}", 10).contentType(MediaTypes.HAL_JSON) |
| 161 | + .content(this.objectMapper.writeValueAsString(tag))) |
| 162 | + .andExpect(status().isAccepted()) |
| 163 | + .andReturn() |
| 164 | + .getResponse() |
| 165 | + .getHeader("Location"); |
| 166 | + |
| 167 | + Map<String, Object> crud = new HashMap<>(); |
| 168 | + crud.put("title", "Sample Model"); |
| 169 | + crud.put("body", "http://www.baeldung.com/"); |
| 170 | + crud.put("tags", singletonList(tagLocation)); |
| 171 | + |
| 172 | + this.mockMvc.perform(put("/crud/{id}", 10).contentType(MediaTypes.HAL_JSON) |
| 173 | + .content(this.objectMapper.writeValueAsString(crud))) |
| 174 | + .andExpect(status().isAccepted()); |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + public void contextLoads() { |
| 179 | + } |
| 180 | + |
| 181 | +} |
0 commit comments