Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Commit f757f33

Browse files
authored
Added more tests (#47)
* Added more tests * Refactored tests and added more tests * Reverted changes in index.html
1 parent ada6529 commit f757f33

File tree

4 files changed

+289
-1
lines changed

4 files changed

+289
-1
lines changed

spring-auto-restdocs-core/src/main/java/capital/scalable/restdocs/util/FieldUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import static org.apache.commons.lang3.StringUtils.uncapitalize;
2020

21-
public class FieldUtil {
21+
public final class FieldUtil {
2222
private FieldUtil() {
2323
// utils
2424
}

spring-auto-restdocs-core/src/test/java/capital/scalable/restdocs/payload/JacksonRequestFieldSnippetTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static org.mockito.Mockito.when;
2323

2424
import javax.validation.constraints.Size;
25+
import java.util.List;
2526

2627
import capital.scalable.restdocs.constraints.ConstraintReader;
2728
import capital.scalable.restdocs.javadoc.JavadocReader;
@@ -93,12 +94,44 @@ public void noRequestBody() throws Exception {
9394
.build());
9495
}
9596

97+
@Test
98+
public void listRequest() throws Exception {
99+
ObjectMapper mapper = new ObjectMapper();
100+
mapper.setVisibility(mapper.getSerializationConfig().getDefaultVisibilityChecker()
101+
.withFieldVisibility(JsonAutoDetect.Visibility.ANY));
102+
103+
HandlerMethod handlerMethod = new HandlerMethod(new TestResource(), "addItems", List.class);
104+
JavadocReader javadocReader = mock(JavadocReader.class);
105+
when(javadocReader.resolveFieldComment(Item.class, "field1"))
106+
.thenReturn("A string");
107+
when(javadocReader.resolveFieldComment(Item.class, "field2"))
108+
.thenReturn("An integer");
109+
110+
this.snippet.expectRequestFields().withContents(
111+
tableWithHeader("Path", "Type", "Optional", "Description")
112+
.row("[].field1", "String", "true", "A string")
113+
.row("[].field2", "Integer", "true", "An integer"));
114+
115+
new JacksonRequestFieldSnippet().document(operationBuilder
116+
.attribute(HandlerMethod.class.getName(), handlerMethod)
117+
.attribute(ObjectMapper.class.getName(), mapper)
118+
.attribute(JavadocReader.class.getName(), javadocReader)
119+
.attribute(ConstraintReader.class.getName(), mock(ConstraintReader.class))
120+
.request("http://localhost")
121+
.content("{\"field1\":\"test\"}")
122+
.build());
123+
}
124+
96125
private static class TestResource {
97126

98127
public void addItem(@RequestBody Item item) {
99128
// NOOP
100129
}
101130

131+
public void addItems(@RequestBody List<Item> items) {
132+
// NOOP
133+
}
134+
102135
public void addItem2() {
103136
// NOOP
104137
}

spring-auto-restdocs-core/src/test/java/capital/scalable/restdocs/payload/JacksonResponseFieldSnippetTest.java

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424

2525
import javax.validation.constraints.DecimalMax;
2626
import javax.validation.constraints.DecimalMin;
27+
import javax.xml.ws.Response;
2728
import java.math.BigDecimal;
29+
import java.util.Collections;
30+
import java.util.List;
2831

2932
import capital.scalable.restdocs.constraints.ConstraintReader;
3033
import capital.scalable.restdocs.javadoc.JavadocReader;
@@ -34,6 +37,7 @@
3437
import org.junit.Test;
3538
import org.springframework.data.domain.Page;
3639
import org.springframework.data.domain.PageImpl;
40+
import org.springframework.http.ResponseEntity;
3741
import org.springframework.restdocs.AbstractSnippetTests;
3842
import org.springframework.restdocs.templates.TemplateFormat;
3943
import org.springframework.web.method.HandlerMethod;
@@ -79,6 +83,34 @@ public void simpleResponse() throws Exception {
7983
.build());
8084
}
8185

86+
@Test
87+
public void listResponse() throws Exception {
88+
ObjectMapper mapper = new ObjectMapper();
89+
mapper.setVisibility(mapper.getSerializationConfig().getDefaultVisibilityChecker()
90+
.withFieldVisibility(JsonAutoDetect.Visibility.ANY));
91+
92+
HandlerMethod handlerMethod = new HandlerMethod(new TestResource(), "getItems");
93+
JavadocReader javadocReader = mock(JavadocReader.class);
94+
when(javadocReader.resolveFieldComment(Item.class, "field1"))
95+
.thenReturn("A string");
96+
when(javadocReader.resolveFieldComment(Item.class, "field2"))
97+
.thenReturn("A decimal");
98+
99+
this.snippet.expectResponseFields().withContents(
100+
tableWithPrefix("\n",
101+
tableWithHeader("Path", "Type", "Optional", "Description")
102+
.row("[].field1", "String", "true", "A string")
103+
.row("[].field2", "Decimal", "true", "A decimal")));
104+
105+
new JacksonResponseFieldSnippet().document(operationBuilder
106+
.attribute(HandlerMethod.class.getName(), handlerMethod)
107+
.attribute(ObjectMapper.class.getName(), mapper)
108+
.attribute(JavadocReader.class.getName(), javadocReader)
109+
.attribute(ConstraintReader.class.getName(), mock(ConstraintReader.class))
110+
.request("http://localhost")
111+
.build());
112+
}
113+
82114
@Test
83115
public void noResponseBody() throws Exception {
84116
ObjectMapper mapper = new ObjectMapper();
@@ -132,6 +164,41 @@ public void pageResponse() throws Exception {
132164
.build());
133165
}
134166

167+
@Test
168+
public void responseEntityResponse() throws Exception {
169+
ObjectMapper mapper = new ObjectMapper();
170+
mapper.setVisibility(mapper.getSerializationConfig().getDefaultVisibilityChecker()
171+
.withFieldVisibility(JsonAutoDetect.Visibility.ANY));
172+
173+
HandlerMethod handlerMethod = new HandlerMethod(new TestResource(), "responseEntityItem");
174+
JavadocReader javadocReader = mock(JavadocReader.class);
175+
when(javadocReader.resolveFieldComment(Item.class, "field1"))
176+
.thenReturn("A string");
177+
when(javadocReader.resolveFieldComment(Item.class, "field2"))
178+
.thenReturn("A decimal");
179+
180+
ConstraintReader constraintReader = mock(ConstraintReader.class);
181+
when(constraintReader.isMandatory(NotBlank.class)).thenReturn(true);
182+
when(constraintReader.getOptionalMessages(Item.class, "field1"))
183+
.thenReturn(singletonList("false"));
184+
when(constraintReader.getConstraintMessages(Item.class, "field2"))
185+
.thenReturn(singletonList("A constraint"));
186+
187+
this.snippet.expectResponseFields().withContents(
188+
tableWithPrefix("\n",
189+
tableWithHeader("Path", "Type", "Optional", "Description")
190+
.row("field1", "String", "false", "A string")
191+
.row("field2", "Decimal", "true", "A decimal +\nA constraint")));
192+
193+
new JacksonResponseFieldSnippet().document(operationBuilder
194+
.attribute(HandlerMethod.class.getName(), handlerMethod)
195+
.attribute(ObjectMapper.class.getName(), mapper)
196+
.attribute(JavadocReader.class.getName(), javadocReader)
197+
.attribute(ConstraintReader.class.getName(), constraintReader)
198+
.request("http://localhost")
199+
.build());
200+
}
201+
135202
private String paginationPrefix() {
136203
if ("adoc".equals(templateFormat.getFileExtension())) {
137204
return "Standard <<overview-pagination,paging>> response where `content` field is"
@@ -148,12 +215,21 @@ public Item getItem() {
148215
return new Item("test");
149216
}
150217

218+
public List<Item> getItems() {
219+
return Collections.singletonList(new Item("test"));
220+
}
221+
151222
public void noItem() {
223+
// NOOP
152224
}
153225

154226
public Page<Item> pagedItems() {
155227
return new PageImpl<>(singletonList(new Item("test")));
156228
}
229+
230+
public ResponseEntity<Item> responseEntityItem() {
231+
return ResponseEntity.ok(new Item("test"));
232+
}
157233
}
158234

159235
private static class Item {
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
package capital.scalable.restdocs.response;
2+
3+
import static java.nio.charset.StandardCharsets.UTF_8;
4+
import static java.util.Collections.singletonList;
5+
import static org.hamcrest.CoreMatchers.is;
6+
import static org.junit.Assert.assertThat;
7+
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
8+
import static org.springframework.http.MediaType.IMAGE_JPEG_VALUE;
9+
import static org.springframework.http.MediaType.IMAGE_PNG_VALUE;
10+
11+
import com.fasterxml.jackson.databind.ObjectMapper;
12+
import org.junit.Before;
13+
import org.junit.Test;
14+
import org.springframework.http.HttpHeaders;
15+
import org.springframework.http.HttpStatus;
16+
import org.springframework.restdocs.operation.OperationResponse;
17+
import org.springframework.restdocs.operation.preprocess.OperationPreprocessor;
18+
import org.springframework.restdocs.operation.preprocess.OperationResponsePreprocessor;
19+
import org.springframework.restdocs.operation.preprocess.Preprocessors;
20+
21+
public class ResponseModifyingPreprocessorsTest {
22+
23+
private ObjectMapper mapper;
24+
25+
@Before
26+
public void setup() {
27+
mapper = new ObjectMapper();
28+
}
29+
30+
@Test
31+
public void shouldShortenJsonArraysWithCombinedShortener() {
32+
preprocessorTest(new TestData()
33+
.content("[1,2,3,4]")
34+
.contentType(APPLICATION_JSON_VALUE)
35+
.preprocessor(ResponseModifyingPreprocessors.shortenContent(mapper))
36+
.expectedResult("[1,2,3]"));
37+
}
38+
39+
@Test
40+
public void shouldShortenBinaryContentWithCombinedShortener() {
41+
preprocessorTest(new TestData()
42+
.content("does not matter")
43+
.contentType(IMAGE_PNG_VALUE)
44+
.preprocessor(ResponseModifyingPreprocessors.shortenContent(mapper))
45+
.expectedResult("<binary>"));
46+
}
47+
48+
@Test
49+
public void shouldShortenJsonArrays() {
50+
preprocessorTest(new TestData()
51+
.content("[1,2,3,4]")
52+
.contentType(APPLICATION_JSON_VALUE)
53+
.preprocessor(
54+
ResponseModifyingPreprocessors.limitJsonArrayLength(mapper))
55+
.expectedResult("[1,2,3]"));
56+
}
57+
58+
@Test
59+
public void shouldShortenNestedJsonArrays() {
60+
preprocessorTest(new TestData()
61+
.content("{\"x\":{\"y\":[1,2,3,4,5]}}")
62+
.contentType(APPLICATION_JSON_VALUE)
63+
.preprocessor(ResponseModifyingPreprocessors.limitJsonArrayLength(mapper))
64+
.expectedResult("{\"x\":{\"y\":[1,2,3]}}"));
65+
}
66+
67+
@Test
68+
public void shouldShortenJpegAsBinary() {
69+
preprocessorTest(new TestData()
70+
.content("does not matter")
71+
.contentType(IMAGE_JPEG_VALUE)
72+
.preprocessor(ResponseModifyingPreprocessors.replaceBinaryContent())
73+
.expectedResult("<binary>"));
74+
}
75+
76+
@Test
77+
public void shouldShortenPdfAsBinary() {
78+
preprocessorTest(new TestData()
79+
.content("does not matter")
80+
.contentType("application/pdf")
81+
.preprocessor(ResponseModifyingPreprocessors.replaceBinaryContent())
82+
.expectedResult("<binary>"));
83+
}
84+
85+
private void preprocessorTest(TestData testData) {
86+
// given
87+
OperationResponse unprocessedResponse =
88+
new OperationResponseFake(testData.getContent(), testData.getContentType());
89+
// when
90+
OperationResponse processedResponse =
91+
testData.getPreprocessor().preprocess(unprocessedResponse);
92+
// then
93+
assertThat(processedResponse.getContentAsString(), is(testData.getExpectedResult()));
94+
}
95+
96+
private static class TestData {
97+
private String content;
98+
99+
private String contentType;
100+
101+
private OperationResponsePreprocessor preprocessor;
102+
103+
private String expectedResult;
104+
105+
String getContent() {
106+
return content;
107+
}
108+
109+
TestData content(String content) {
110+
this.content = content;
111+
return this;
112+
}
113+
114+
String getContentType() {
115+
return contentType;
116+
}
117+
118+
TestData contentType(String contentType) {
119+
this.contentType = contentType;
120+
return this;
121+
}
122+
123+
OperationResponsePreprocessor getPreprocessor() {
124+
return preprocessor;
125+
}
126+
127+
TestData preprocessor(OperationResponsePreprocessor preprocessor) {
128+
this.preprocessor = preprocessor;
129+
return this;
130+
}
131+
132+
TestData preprocessor(OperationPreprocessor preprocessor) {
133+
this.preprocessor = Preprocessors.preprocessResponse(preprocessor);
134+
return this;
135+
}
136+
137+
String getExpectedResult() {
138+
return expectedResult;
139+
}
140+
141+
TestData expectedResult(String expectedResult) {
142+
this.expectedResult = expectedResult;
143+
return this;
144+
}
145+
}
146+
147+
private static class OperationResponseFake implements OperationResponse {
148+
149+
private final String content;
150+
151+
private final HttpHeaders httpHeaders;
152+
153+
OperationResponseFake(String content, String contentType) {
154+
this.content = content;
155+
this.httpHeaders = new HttpHeaders();
156+
this.httpHeaders.put(HttpHeaders.CONTENT_TYPE, singletonList(contentType));
157+
}
158+
159+
@Override
160+
public HttpStatus getStatus() {
161+
return HttpStatus.OK;
162+
}
163+
164+
@Override
165+
public HttpHeaders getHeaders() {
166+
return httpHeaders;
167+
}
168+
169+
@Override
170+
public byte[] getContent() {
171+
return content.getBytes(UTF_8);
172+
}
173+
174+
@Override
175+
public String getContentAsString() {
176+
return content;
177+
}
178+
}
179+
}

0 commit comments

Comments
 (0)