Skip to content

Commit e7ec500

Browse files
committed
Clean up Spring REST Docs examples
1 parent 28d6a01 commit e7ec500

2 files changed

Lines changed: 341 additions & 281 deletions

File tree

Lines changed: 208 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,6 @@
11
package com.example;
22

3-
import static org.springframework.restdocs.headers.HeaderDocumentation.headerWithName;
4-
import static org.springframework.restdocs.headers.HeaderDocumentation.responseHeaders;
5-
import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.linkWithRel;
6-
import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.links;
7-
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
8-
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
9-
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
10-
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post;
11-
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.delete;
12-
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.patch;
13-
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.put;
14-
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest;
15-
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessResponse;
16-
import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint;
17-
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
18-
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
19-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
20-
import static org.springframework.restdocs.snippet.Attributes.key;
21-
import static org.springframework.restdocs.payload.PayloadDocumentation.requestFields;
22-
23-
import java.util.Arrays;
24-
import java.util.HashMap;
25-
import java.util.Map;
26-
3+
import com.fasterxml.jackson.databind.ObjectMapper;
274
import org.junit.Before;
285
import org.junit.Rule;
296
import org.junit.Test;
@@ -39,146 +16,219 @@
3916
import org.springframework.test.context.web.WebAppConfiguration;
4017
import org.springframework.test.web.servlet.MockMvc;
4118
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
42-
import org.springframework.util.StringUtils;
4319
import org.springframework.web.context.WebApplicationContext;
4420

45-
import com.fasterxml.jackson.databind.ObjectMapper;
21+
import java.util.HashMap;
22+
import java.util.Map;
23+
24+
import static java.util.Collections.singletonList;
25+
import static org.springframework.restdocs.headers.HeaderDocumentation.headerWithName;
26+
import static org.springframework.restdocs.headers.HeaderDocumentation.responseHeaders;
27+
import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.linkWithRel;
28+
import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.links;
29+
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
30+
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
31+
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.*;
32+
import static org.springframework.restdocs.operation.preprocess.Preprocessors.*;
33+
import static org.springframework.restdocs.payload.PayloadDocumentation.*;
34+
import static org.springframework.restdocs.snippet.Attributes.key;
35+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
36+
import static org.springframework.util.StringUtils.collectionToDelimitedString;
4637

4738
@RunWith(SpringJUnit4ClassRunner.class)
4839
@SpringApplicationConfiguration(classes = SpringRestDocsApplication.class)
4940
@WebAppConfiguration
5041
public class ApiDocumentation {
5142

52-
@Rule
53-
public final RestDocumentation restDocumentation = new RestDocumentation("target/generated-snippets");
54-
55-
@Autowired
56-
private WebApplicationContext context;
57-
58-
@Autowired
59-
private ObjectMapper objectMapper;
60-
61-
private RestDocumentationResultHandler document;
62-
63-
private MockMvc mockMvc;
64-
65-
@Before
66-
public void setUp() {
67-
this.document = document("{method-name}",preprocessRequest(prettyPrint()),preprocessResponse(prettyPrint()));
68-
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).apply(documentationConfiguration(this.restDocumentation)).alwaysDo(this.document).build();
69-
}
70-
71-
72-
@Test
73-
public void headersExample() throws Exception {
74-
this.document.snippets(responseHeaders(headerWithName("Content-Type").description("The Content-Type of the payload, e.g. `application/hal+json`")));
75-
this.mockMvc.perform(get("/")).andExpect(status().isOk());
76-
}
77-
78-
@Test
79-
public void indexExample() throws Exception {
80-
this.document.snippets(links(linkWithRel("crud").description("The <<resources-tags,Tags resource>>")),responseFields(fieldWithPath("_links").description("<<resources-index-links,Links>> to other resources")));
81-
this.mockMvc.perform(get("/")).andExpect(status().isOk());
82-
}
83-
84-
85-
@Test
86-
public void crudGetExample() throws Exception {
87-
88-
Map<String, String> tag = new HashMap<String, String>();
89-
tag.put("name", "GET");
90-
91-
String tagLocation =this.mockMvc.perform(get("/crud").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(tag))).andExpect(status().isOk()).andReturn().getResponse().getHeader("Location");
92-
93-
Map<String, Object> crud = new HashMap<String, Object>();
94-
crud.put("title", "Sample Model");
95-
crud.put("body", "http://www.baeldung.com/");
96-
crud.put("tags", Arrays.asList(tagLocation));
97-
98-
this.mockMvc.perform(get("/crud").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(crud))).andExpect(status().isOk());
99-
}
100-
101-
@Test
102-
public void crudCreateExample() throws Exception {
103-
Map<String, String> tag = new HashMap<String, String>();
104-
tag.put("name", "CREATE");
105-
106-
String tagLocation =this.mockMvc.perform(post("/crud").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(tag))).andExpect(status().isCreated()).andReturn().getResponse().getHeader("Location");
107-
108-
Map<String, Object> crud = new HashMap<String, Object>();
109-
crud.put("title", "Sample Model");
110-
crud.put("body", "http://www.baeldung.com/");
111-
crud.put("tags", Arrays.asList(tagLocation));
112-
113-
ConstrainedFields fields = new ConstrainedFields(CrudInput.class);
114-
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")));
115-
this.mockMvc.perform(post("/crud").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(crud))).andExpect(status().isCreated());
116-
117-
118-
}
119-
120-
@Test
121-
public void crudDeleteExample() throws Exception {
122-
123-
Map<String, String> tag = new HashMap<String, String>();
124-
tag.put("name", "DELETE");
125-
126-
String tagLocation =this.mockMvc.perform(delete("/crud/10").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(tag))).andExpect(status().isOk()).andReturn().getResponse().getHeader("Location");
127-
Map<String, Object> crud = new HashMap<String, Object>();
128-
crud.put("title", "Sample Model");
129-
crud.put("body", "http://www.baeldung.com/");
130-
crud.put("tags", Arrays.asList(tagLocation));
131-
132-
this.mockMvc.perform(delete("/crud/10").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(crud))).andExpect(status().isOk());
133-
}
134-
135-
@Test
136-
public void crudPatchExample() throws Exception {
137-
138-
Map<String, String> tag = new HashMap<String, String>();
139-
tag.put("name", "PATCH");
140-
141-
String tagLocation =this.mockMvc.perform(patch("/crud/10").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(tag))).andExpect(status().isNoContent()).andReturn().getResponse().getHeader("Location");
142-
Map<String, Object> crud = new HashMap<String, Object>();
143-
crud.put("title", "Sample Model");
144-
crud.put("body", "http://www.baeldung.com/");
145-
crud.put("tags", Arrays.asList(tagLocation));
146-
147-
this.mockMvc.perform(patch("/crud/10").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(crud))).andExpect(status().isNoContent());
148-
}
149-
150-
151-
@Test
152-
public void crudPutExample() throws Exception {
153-
Map<String, String> tag = new HashMap<String, String>();
154-
tag.put("name", "PUT");
155-
156-
String tagLocation =this.mockMvc.perform(put("/crud/10").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(tag))).andExpect(status().isAccepted()).andReturn().getResponse().getHeader("Location");
157-
Map<String, Object> crud = new HashMap<String, Object>();
158-
crud.put("title", "Sample Model");
159-
crud.put("body", "http://www.baeldung.com/");
160-
crud.put("tags", Arrays.asList(tagLocation));
161-
162-
this.mockMvc.perform(put("/crud/10").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(crud))).andExpect(status().isAccepted());
163-
}
164-
165-
166-
@Test
167-
public void contextLoads() {
168-
}
169-
170-
private static class ConstrainedFields {
171-
172-
private final ConstraintDescriptions constraintDescriptions;
173-
174-
ConstrainedFields(Class<?> input) {
175-
this.constraintDescriptions = new ConstraintDescriptions(input);
176-
}
177-
178-
private FieldDescriptor withPath(String path) {
179-
return fieldWithPath(path).attributes(key("constraints").value(StringUtils.collectionToDelimitedString(this.constraintDescriptions.descriptionsForProperty(path), ". ")));
180-
}
181-
}
182-
183-
43+
@Rule
44+
public final RestDocumentation restDocumentation = new RestDocumentation("target/generated-snippets");
45+
46+
@Autowired
47+
private WebApplicationContext context;
48+
49+
@Autowired
50+
private ObjectMapper objectMapper;
51+
52+
private RestDocumentationResultHandler document;
53+
54+
private MockMvc mockMvc;
55+
56+
@Before
57+
public void setUp() {
58+
this.document = document("{method-name}", preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint()));
59+
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
60+
.apply(documentationConfiguration(this.restDocumentation))
61+
.alwaysDo(this.document)
62+
.build();
63+
}
64+
65+
66+
@Test
67+
public void headersExample() throws Exception {
68+
this.document.snippets(responseHeaders(headerWithName("Content-Type")
69+
.description("The Content-Type of the payload, e.g. `application/hal+json`")));
70+
this.mockMvc.perform(get("/"))
71+
.andExpect(status().isOk());
72+
}
73+
74+
@Test
75+
public void indexExample() throws Exception {
76+
this.document.snippets(
77+
links(linkWithRel("crud").description("The <<resources-tags,Tags resource>>")),
78+
responseFields(fieldWithPath("_links").description("<<resources-index-links,Links>> to other resources"))
79+
);
80+
this.mockMvc.perform(get("/"))
81+
.andExpect(status().isOk());
82+
}
83+
84+
85+
@Test
86+
public void crudGetExample() throws Exception {
87+
88+
Map<String, String> tag = new HashMap<>();
89+
tag.put("name", "GET");
90+
91+
String tagLocation = this.mockMvc.perform(get("/crud")
92+
.contentType(MediaTypes.HAL_JSON)
93+
.content(this.objectMapper.writeValueAsString(tag)))
94+
.andExpect(status().isOk())
95+
.andReturn()
96+
.getResponse()
97+
.getHeader("Location");
98+
99+
Map<String, Object> crud = new HashMap<>();
100+
crud.put("title", "Sample Model");
101+
crud.put("body", "http://www.baeldung.com/");
102+
crud.put("tags", singletonList(tagLocation));
103+
104+
this.mockMvc.perform(get("/crud")
105+
.contentType(MediaTypes.HAL_JSON)
106+
.content(this.objectMapper.writeValueAsString(crud)))
107+
.andExpect(status().isOk());
108+
}
109+
110+
@Test
111+
public void crudCreateExample() throws Exception {
112+
Map<String, String> tag = new HashMap<>();
113+
tag.put("name", "CREATE");
114+
115+
String tagLocation = this.mockMvc.perform(post("/crud")
116+
.contentType(MediaTypes.HAL_JSON)
117+
.content(this.objectMapper.writeValueAsString(tag)))
118+
.andExpect(status().isCreated())
119+
.andReturn()
120+
.getResponse()
121+
.getHeader("Location");
122+
123+
Map<String, Object> crud = new HashMap<>();
124+
crud.put("title", "Sample Model");
125+
crud.put("body", "http://www.baeldung.com/");
126+
crud.put("tags", singletonList(tagLocation));
127+
128+
ConstrainedFields fields = new ConstrainedFields(CrudInput.class);
129+
this.document.snippets(requestFields(
130+
fields.withPath("title").description("The title of the note"),
131+
fields.withPath("body").description("The body of the note"),
132+
fields.withPath("tags").description("An array of tag resource URIs")));
133+
this.mockMvc.perform(post("/crud").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(crud))).andExpect(status().isCreated());
134+
135+
136+
}
137+
138+
@Test
139+
public void crudDeleteExample() throws Exception {
140+
141+
Map<String, String> tag = new HashMap<>();
142+
tag.put("name", "DELETE");
143+
144+
String tagLocation = this.mockMvc.perform(delete("/crud/10")
145+
.contentType(MediaTypes.HAL_JSON)
146+
.content(this.objectMapper.writeValueAsString(tag)))
147+
.andExpect(status().isOk())
148+
.andReturn()
149+
.getResponse()
150+
.getHeader("Location");
151+
152+
Map<String, Object> crud = new HashMap<>();
153+
crud.put("title", "Sample Model");
154+
crud.put("body", "http://www.baeldung.com/");
155+
crud.put("tags", singletonList(tagLocation));
156+
157+
this.mockMvc.perform(delete("/crud/10")
158+
.contentType(MediaTypes.HAL_JSON)
159+
.content(this.objectMapper.writeValueAsString(crud)))
160+
.andExpect(status().isOk());
161+
}
162+
163+
@Test
164+
public void crudPatchExample() throws Exception {
165+
166+
Map<String, String> tag = new HashMap<>();
167+
tag.put("name", "PATCH");
168+
169+
String tagLocation = this.mockMvc.perform(patch("/crud/10")
170+
.contentType(MediaTypes.HAL_JSON)
171+
.content(this.objectMapper.writeValueAsString(tag)))
172+
.andExpect(status().isNoContent())
173+
.andReturn()
174+
.getResponse()
175+
.getHeader("Location");
176+
177+
Map<String, Object> crud = new HashMap<>();
178+
crud.put("title", "Sample Model");
179+
crud.put("body", "http://www.baeldung.com/");
180+
crud.put("tags", singletonList(tagLocation));
181+
182+
this.mockMvc.perform(patch("/crud/10")
183+
.contentType(MediaTypes.HAL_JSON)
184+
.content(this.objectMapper.writeValueAsString(crud)))
185+
.andExpect(status().isNoContent());
186+
}
187+
188+
189+
@Test
190+
public void crudPutExample() throws Exception {
191+
Map<String, String> tag = new HashMap<>();
192+
tag.put("name", "PUT");
193+
194+
String tagLocation = this.mockMvc.perform(put("/crud/10")
195+
.contentType(MediaTypes.HAL_JSON)
196+
.content(this.objectMapper.writeValueAsString(tag)))
197+
.andExpect(status().isAccepted())
198+
.andReturn()
199+
.getResponse()
200+
.getHeader("Location");
201+
202+
Map<String, Object> crud = new HashMap<>();
203+
crud.put("title", "Sample Model");
204+
crud.put("body", "http://www.baeldung.com/");
205+
crud.put("tags", singletonList(tagLocation));
206+
207+
this.mockMvc.perform(put("/crud/10")
208+
.contentType(MediaTypes.HAL_JSON)
209+
.content(this.objectMapper.writeValueAsString(crud)))
210+
.andExpect(status().isAccepted());
211+
}
212+
213+
214+
@Test
215+
public void contextLoads() {
216+
}
217+
218+
private static class ConstrainedFields {
219+
220+
private final ConstraintDescriptions constraintDescriptions;
221+
222+
ConstrainedFields(Class<?> input) {
223+
this.constraintDescriptions = new ConstraintDescriptions(input);
224+
}
225+
226+
private FieldDescriptor withPath(String path) {
227+
return fieldWithPath(path)
228+
.attributes(key("constraints")
229+
.value(collectionToDelimitedString(this.constraintDescriptions.descriptionsForProperty(path), ". ")));
230+
}
231+
}
232+
233+
184234
}

0 commit comments

Comments
 (0)