@@ -56,83 +56,53 @@ public class ApiDocumentation {
5656 @ Before
5757 public void setUp () {
5858 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 ();
59+ this .mockMvc = MockMvcBuilders .webAppContextSetup (this .context ).apply (documentationConfiguration (this .restDocumentation )).alwaysDo (this .document ).build ();
6360 }
6461
65-
6662 @ Test
6763 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 ());
64+ this .document .snippets (responseHeaders (headerWithName ("Content-Type" ).description ("The Content-Type of the payload, e.g. `application/hal+json`" )));
65+ this .mockMvc .perform (get ("/" )).andExpect (status ().isOk ());
7266 }
7367
7468 @ Test
7569 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 ());
70+ this .document .snippets (links (linkWithRel ("crud" ).description ("The <<resources-tags,Tags resource>>" )), responseFields (fieldWithPath ("_links" ).description ("<<resources-index-links,Links>> to other resources" )));
71+ this .mockMvc .perform (get ("/" )).andExpect (status ().isOk ());
8272 }
8373
84-
8574 @ Test
8675 public void crudGetExample () throws Exception {
8776
8877 Map <String , String > tag = new HashMap <>();
8978 tag .put ("name" , "GET" );
9079
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" );
80+ String tagLocation = this .mockMvc .perform (get ("/crud" ).contentType (MediaTypes .HAL_JSON ).content (this .objectMapper .writeValueAsString (tag ))).andExpect (status ().isOk ()).andReturn ().getResponse ().getHeader ("Location" );
9881
9982 Map <String , Object > crud = new HashMap <>();
10083 crud .put ("title" , "Sample Model" );
10184 crud .put ("body" , "http://www.baeldung.com/" );
10285 crud .put ("tags" , singletonList (tagLocation ));
10386
104- this .mockMvc .perform (get ("/crud" )
105- .contentType (MediaTypes .HAL_JSON )
106- .content (this .objectMapper .writeValueAsString (crud )))
107- .andExpect (status ().isOk ());
87+ this .mockMvc .perform (get ("/crud" ).contentType (MediaTypes .HAL_JSON ).content (this .objectMapper .writeValueAsString (crud ))).andExpect (status ().isOk ());
10888 }
10989
11090 @ Test
11191 public void crudCreateExample () throws Exception {
11292 Map <String , String > tag = new HashMap <>();
11393 tag .put ("name" , "CREATE" );
11494
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" );
95+ String tagLocation = this .mockMvc .perform (post ("/crud" ).contentType (MediaTypes .HAL_JSON ).content (this .objectMapper .writeValueAsString (tag ))).andExpect (status ().isCreated ()).andReturn ().getResponse ().getHeader ("Location" );
12296
12397 Map <String , Object > crud = new HashMap <>();
12498 crud .put ("title" , "Sample Model" );
12599 crud .put ("body" , "http://www.baeldung.com/" );
126100 crud .put ("tags" , singletonList (tagLocation ));
127101
128102 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" )));
103+ 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" )));
133104 this .mockMvc .perform (post ("/crud" ).contentType (MediaTypes .HAL_JSON ).content (this .objectMapper .writeValueAsString (crud ))).andExpect (status ().isCreated ());
134105
135-
136106 }
137107
138108 @ Test
@@ -141,23 +111,14 @@ public void crudDeleteExample() throws Exception {
141111 Map <String , String > tag = new HashMap <>();
142112 tag .put ("name" , "DELETE" );
143113
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" );
114+ String tagLocation = this .mockMvc .perform (delete ("/crud/10" ).contentType (MediaTypes .HAL_JSON ).content (this .objectMapper .writeValueAsString (tag ))).andExpect (status ().isOk ()).andReturn ().getResponse ().getHeader ("Location" );
151115
152116 Map <String , Object > crud = new HashMap <>();
153117 crud .put ("title" , "Sample Model" );
154118 crud .put ("body" , "http://www.baeldung.com/" );
155119 crud .put ("tags" , singletonList (tagLocation ));
156120
157- this .mockMvc .perform (delete ("/crud/10" )
158- .contentType (MediaTypes .HAL_JSON )
159- .content (this .objectMapper .writeValueAsString (crud )))
160- .andExpect (status ().isOk ());
121+ this .mockMvc .perform (delete ("/crud/10" ).contentType (MediaTypes .HAL_JSON ).content (this .objectMapper .writeValueAsString (crud ))).andExpect (status ().isOk ());
161122 }
162123
163124 @ Test
@@ -166,51 +127,31 @@ public void crudPatchExample() throws Exception {
166127 Map <String , String > tag = new HashMap <>();
167128 tag .put ("name" , "PATCH" );
168129
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" );
130+ String tagLocation = this .mockMvc .perform (patch ("/crud/10" ).contentType (MediaTypes .HAL_JSON ).content (this .objectMapper .writeValueAsString (tag ))).andExpect (status ().isNoContent ()).andReturn ().getResponse ().getHeader ("Location" );
176131
177132 Map <String , Object > crud = new HashMap <>();
178133 crud .put ("title" , "Sample Model" );
179134 crud .put ("body" , "http://www.baeldung.com/" );
180135 crud .put ("tags" , singletonList (tagLocation ));
181136
182- this .mockMvc .perform (patch ("/crud/10" )
183- .contentType (MediaTypes .HAL_JSON )
184- .content (this .objectMapper .writeValueAsString (crud )))
185- .andExpect (status ().isNoContent ());
137+ this .mockMvc .perform (patch ("/crud/10" ).contentType (MediaTypes .HAL_JSON ).content (this .objectMapper .writeValueAsString (crud ))).andExpect (status ().isNoContent ());
186138 }
187139
188-
189140 @ Test
190141 public void crudPutExample () throws Exception {
191142 Map <String , String > tag = new HashMap <>();
192143 tag .put ("name" , "PUT" );
193144
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" );
145+ String tagLocation = this .mockMvc .perform (put ("/crud/10" ).contentType (MediaTypes .HAL_JSON ).content (this .objectMapper .writeValueAsString (tag ))).andExpect (status ().isAccepted ()).andReturn ().getResponse ().getHeader ("Location" );
201146
202147 Map <String , Object > crud = new HashMap <>();
203148 crud .put ("title" , "Sample Model" );
204149 crud .put ("body" , "http://www.baeldung.com/" );
205150 crud .put ("tags" , singletonList (tagLocation ));
206151
207- this .mockMvc .perform (put ("/crud/10" )
208- .contentType (MediaTypes .HAL_JSON )
209- .content (this .objectMapper .writeValueAsString (crud )))
210- .andExpect (status ().isAccepted ());
152+ this .mockMvc .perform (put ("/crud/10" ).contentType (MediaTypes .HAL_JSON ).content (this .objectMapper .writeValueAsString (crud ))).andExpect (status ().isAccepted ());
211153 }
212154
213-
214155 @ Test
215156 public void contextLoads () {
216157 }
@@ -224,11 +165,8 @@ private static class ConstrainedFields {
224165 }
225166
226167 private FieldDescriptor withPath (String path ) {
227- return fieldWithPath (path )
228- .attributes (key ("constraints" )
229- .value (collectionToDelimitedString (this .constraintDescriptions .descriptionsForProperty (path ), ". " )));
168+ return fieldWithPath (path ).attributes (key ("constraints" ).value (collectionToDelimitedString (this .constraintDescriptions .descriptionsForProperty (path ), ". " )));
230169 }
231170 }
232171
233-
234172}
0 commit comments