Skip to content

Commit 630acd7

Browse files
committed
formatting, add tests
1 parent 50743f2 commit 630acd7

3 files changed

Lines changed: 368 additions & 2 deletions

File tree

‎spring-5/pom.xml‎

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

Comments
 (0)