Apply Eclipse Mars' code formatting

This commit is contained in:
Andy Wilkinson
2016-02-12 20:57:53 +00:00
parent 811e7adf70
commit 74f9e272fd
92 changed files with 1446 additions and 1294 deletions

View File

@@ -129,8 +129,7 @@ public class ConstraintDescriptionsTests {
@Test
public void sizeList() {
assertThat(
this.constraintDescriptions.descriptionsForProperty("sizeList"),
assertThat(this.constraintDescriptions.descriptionsForProperty("sizeList"),
contains("Size must be between 1 and 4 inclusive",
"Size must be between 8 and 10 inclusive"));
}

View File

@@ -70,8 +70,8 @@ public class ResourceBundleConstraintDescriptionResolverTests {
public void defaultMessageDecimalMax() {
Map<String, Object> configuration = new HashMap<>();
configuration.put("value", "9.875");
String description = this.resolver.resolveDescription(new Constraint(
DecimalMax.class.getName(), configuration));
String description = this.resolver.resolveDescription(
new Constraint(DecimalMax.class.getName(), configuration));
assertThat(description, is(equalTo("Must be at most 9.875")));
}
@@ -79,8 +79,8 @@ public class ResourceBundleConstraintDescriptionResolverTests {
public void defaultMessageDecimalMin() {
Map<String, Object> configuration = new HashMap<>();
configuration.put("value", "1.5");
String description = this.resolver.resolveDescription(new Constraint(
DecimalMin.class.getName(), configuration));
String description = this.resolver.resolveDescription(
new Constraint(DecimalMin.class.getName(), configuration));
assertThat(description, is(equalTo("Must be at least 1.5")));
}
@@ -89,16 +89,16 @@ public class ResourceBundleConstraintDescriptionResolverTests {
Map<String, Object> configuration = new HashMap<>();
configuration.put("integer", "2");
configuration.put("fraction", "5");
String description = this.resolver.resolveDescription(new Constraint(Digits.class
.getName(), configuration));
assertThat(description, is(equalTo("Must have at most 2 integral digits and 5 "
+ "fractional digits")));
String description = this.resolver.resolveDescription(
new Constraint(Digits.class.getName(), configuration));
assertThat(description, is(equalTo(
"Must have at most 2 integral digits and 5 " + "fractional digits")));
}
@Test
public void defaultMessageFuture() {
String description = this.resolver.resolveDescription(new Constraint(Future.class
.getName(), Collections.<String, Object>emptyMap()));
String description = this.resolver.resolveDescription(new Constraint(
Future.class.getName(), Collections.<String, Object>emptyMap()));
assertThat(description, is(equalTo("Must be in the future")));
}
@@ -106,8 +106,8 @@ public class ResourceBundleConstraintDescriptionResolverTests {
public void defaultMessageMax() {
Map<String, Object> configuration = new HashMap<>();
configuration.put("value", 10);
String description = this.resolver.resolveDescription(new Constraint(Max.class
.getName(), configuration));
String description = this.resolver
.resolveDescription(new Constraint(Max.class.getName(), configuration));
assertThat(description, is(equalTo("Must be at most 10")));
}
@@ -115,8 +115,8 @@ public class ResourceBundleConstraintDescriptionResolverTests {
public void defaultMessageMin() {
Map<String, Object> configuration = new HashMap<>();
configuration.put("value", 10);
String description = this.resolver.resolveDescription(new Constraint(Min.class
.getName(), configuration));
String description = this.resolver
.resolveDescription(new Constraint(Min.class.getName(), configuration));
assertThat(description, is(equalTo("Must be at least 10")));
}
@@ -129,15 +129,15 @@ public class ResourceBundleConstraintDescriptionResolverTests {
@Test
public void defaultMessageNull() {
String description = this.resolver.resolveDescription(new Constraint(Null.class
.getName(), Collections.<String, Object>emptyMap()));
String description = this.resolver.resolveDescription(new Constraint(
Null.class.getName(), Collections.<String, Object>emptyMap()));
assertThat(description, is(equalTo("Must be null")));
}
@Test
public void defaultMessagePast() {
String description = this.resolver.resolveDescription(new Constraint(Past.class
.getName(), Collections.<String, Object>emptyMap()));
String description = this.resolver.resolveDescription(new Constraint(
Past.class.getName(), Collections.<String, Object>emptyMap()));
assertThat(description, is(equalTo("Must be in the past")));
}
@@ -145,8 +145,8 @@ public class ResourceBundleConstraintDescriptionResolverTests {
public void defaultMessagePattern() {
Map<String, Object> configuration = new HashMap<>();
configuration.put("regexp", "[A-Z][a-z]+");
String description = this.resolver.resolveDescription(new Constraint(
Pattern.class.getName(), configuration));
String description = this.resolver.resolveDescription(
new Constraint(Pattern.class.getName(), configuration));
assertThat(description,
is(equalTo("Must match the regular expression '[A-Z][a-z]+'")));
}
@@ -156,8 +156,8 @@ public class ResourceBundleConstraintDescriptionResolverTests {
Map<String, Object> configuration = new HashMap<>();
configuration.put("min", 2);
configuration.put("max", 10);
String description = this.resolver.resolveDescription(new Constraint(Size.class
.getName(), configuration));
String description = this.resolver
.resolveDescription(new Constraint(Size.class.getName(), configuration));
assertThat(description, is(equalTo("Size must be between 2 and 10 inclusive")));
}
@@ -167,9 +167,10 @@ public class ResourceBundleConstraintDescriptionResolverTests {
@Override
public URL getResource(String name) {
if (name.startsWith("org/springframework/restdocs/constraints/ConstraintDescriptions")) {
return super
.getResource("org/springframework/restdocs/constraints/TestConstraintDescriptions.properties");
if (name.startsWith(
"org/springframework/restdocs/constraints/ConstraintDescriptions")) {
return super.getResource(
"org/springframework/restdocs/constraints/TestConstraintDescriptions.properties");
}
return super.getResource(name);
}
@@ -194,14 +195,14 @@ public class ResourceBundleConstraintDescriptionResolverTests {
@Override
protected Object[][] getContents() {
return new String[][] { { NotNull.class.getName() + ".description",
"Not null" } };
return new String[][] {
{ NotNull.class.getName() + ".description", "Not null" } };
}
};
String description = new ResourceBundleConstraintDescriptionResolver(bundle)
.resolveDescription(new Constraint(NotNull.class.getName(), Collections
.<String, Object>emptyMap()));
.resolveDescription(new Constraint(NotNull.class.getName(),
Collections.<String, Object>emptyMap()));
assertThat(description, is(equalTo("Not null")));
}

View File

@@ -66,10 +66,8 @@ public class ValidatorConstraintResolverTests {
List<Constraint> constraints = this.resolver.resolveForProperty("multiple",
ConstrainedFields.class);
assertThat(constraints, hasSize(2));
assertThat(
constraints,
containsInAnyOrder(constraint(NotNull.class), constraint(Size.class)
.config("min", 8).config("max", 16)));
assertThat(constraints, containsInAnyOrder(constraint(NotNull.class),
constraint(Size.class).config("min", 8).config("max", 16)));
}
@Test

View File

@@ -21,6 +21,7 @@ import java.io.IOException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
@@ -59,101 +60,98 @@ public class CurlRequestSnippetTests {
public void getRequest() throws IOException {
this.snippet.expectCurlRequest("get-request").withContents(
codeBlock("bash").content("$ curl 'http://localhost/foo' -i"));
new CurlRequestSnippet().document(new OperationBuilder("get-request",
this.snippet.getOutputDirectory()).request("http://localhost/foo")
.build());
new CurlRequestSnippet().document(
new OperationBuilder("get-request", this.snippet.getOutputDirectory())
.request("http://localhost/foo").build());
}
@Test
public void nonGetRequest() throws IOException {
this.snippet.expectCurlRequest("non-get-request").withContents(
codeBlock("bash").content("$ curl 'http://localhost/foo' -i -X POST"));
new CurlRequestSnippet().document(new OperationBuilder("non-get-request",
this.snippet.getOutputDirectory()).request("http://localhost/foo")
.method("POST").build());
new CurlRequestSnippet().document(
new OperationBuilder("non-get-request", this.snippet.getOutputDirectory())
.request("http://localhost/foo").method("POST").build());
}
@Test
public void requestWithContent() throws IOException {
this.snippet.expectCurlRequest("request-with-content").withContents(
codeBlock("bash")
this.snippet.expectCurlRequest("request-with-content")
.withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo' -i -d 'content'"));
new CurlRequestSnippet().document(new OperationBuilder("request-with-content",
this.snippet.getOutputDirectory()).request("http://localhost/foo")
.content("content").build());
.content("content").build());
}
@Test
public void getRequestWithQueryString() throws IOException {
this.snippet.expectCurlRequest("request-with-query-string")
.withContents(
codeBlock("bash").content(
"$ curl 'http://localhost/foo?param=value' -i"));
new CurlRequestSnippet().document(new OperationBuilder(
"request-with-query-string", this.snippet.getOutputDirectory()).request(
"http://localhost/foo?param=value").build());
.withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo?param=value' -i"));
new CurlRequestSnippet()
.document(new OperationBuilder("request-with-query-string",
this.snippet.getOutputDirectory())
.request("http://localhost/foo?param=value").build());
}
@Test
public void postRequestWithQueryString() throws IOException {
this.snippet.expectCurlRequest("post-request-with-query-string").withContents(
codeBlock("bash").content(
"$ curl 'http://localhost/foo?param=value' -i -X POST"));
new CurlRequestSnippet().document(new OperationBuilder(
"post-request-with-query-string", this.snippet.getOutputDirectory())
.request("http://localhost/foo?param=value").method("POST").build());
this.snippet.expectCurlRequest("post-request-with-query-string")
.withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo?param=value' -i -X POST"));
new CurlRequestSnippet()
.document(new OperationBuilder("post-request-with-query-string",
this.snippet.getOutputDirectory())
.request("http://localhost/foo?param=value")
.method("POST").build());
}
@Test
public void postRequestWithOneParameter() throws IOException {
this.snippet.expectCurlRequest("post-request-with-one-parameter").withContents(
codeBlock("bash").content(
"$ curl 'http://localhost/foo' -i -X POST -d 'k1=v1'"));
this.snippet.expectCurlRequest("post-request-with-one-parameter")
.withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo' -i -X POST -d 'k1=v1'"));
new CurlRequestSnippet()
.document(new OperationBuilder("post-request-with-one-parameter",
this.snippet.getOutputDirectory())
.request("http://localhost/foo").method("POST").param("k1", "v1")
.build());
this.snippet.getOutputDirectory()).request("http://localhost/foo")
.method("POST").param("k1", "v1").build());
}
@Test
public void postRequestWithMultipleParameters() throws IOException {
this.snippet.expectCurlRequest("post-request-with-multiple-parameters")
.withContents(
codeBlock("bash").content(
"$ curl 'http://localhost/foo' -i -X POST"
+ " -d 'k1=v1&k1=v1-bis&k2=v2'"));
new CurlRequestSnippet().document(new OperationBuilder(
"post-request-with-multiple-parameters", this.snippet
.getOutputDirectory()).request("http://localhost/foo")
.method("POST").param("k1", "v1", "v1-bis").param("k2", "v2").build());
.withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo' -i -X POST"
+ " -d 'k1=v1&k1=v1-bis&k2=v2'"));
new CurlRequestSnippet()
.document(new OperationBuilder("post-request-with-multiple-parameters",
this.snippet.getOutputDirectory()).request("http://localhost/foo")
.method("POST").param("k1", "v1", "v1-bis")
.param("k2", "v2").build());
}
@Test
public void postRequestWithUrlEncodedParameter() throws IOException {
this.snippet
.expectCurlRequest("post-request-with-url-encoded-parameter")
.withContents(
codeBlock("bash").content(
"$ curl 'http://localhost/foo' -i -X POST -d 'k1=a%26b'"));
new CurlRequestSnippet().document(new OperationBuilder(
"post-request-with-url-encoded-parameter", this.snippet
.getOutputDirectory()).request("http://localhost/foo")
.method("POST").param("k1", "a&b").build());
this.snippet.expectCurlRequest("post-request-with-url-encoded-parameter")
.withContents(codeBlock("bash").content(
"$ curl 'http://localhost/foo' -i -X POST -d 'k1=a%26b'"));
new CurlRequestSnippet()
.document(new OperationBuilder("post-request-with-url-encoded-parameter",
this.snippet.getOutputDirectory()).request("http://localhost/foo")
.method("POST").param("k1", "a&b").build());
}
@Test
public void postRequestWithQueryStringAndParameter() throws IOException {
this.snippet
.expectCurlRequest("post-request-with-query-string-and-parameter")
.withContents(
codeBlock("bash")
.content(
"$ curl 'http://localhost/foo?a=alpha' -i -X POST -d 'b=bravo'"));
new CurlRequestSnippet().document(new OperationBuilder(
"post-request-with-query-string-and-parameter", this.snippet
.getOutputDirectory()).request("http://localhost/foo?a=alpha")
.method("POST").param("b", "bravo").build());
this.snippet.expectCurlRequest("post-request-with-query-string-and-parameter")
.withContents(codeBlock("bash").content(
"$ curl 'http://localhost/foo?a=alpha' -i -X POST -d 'b=bravo'"));
new CurlRequestSnippet().document(
new OperationBuilder("post-request-with-query-string-and-parameter",
this.snippet.getOutputDirectory())
.request("http://localhost/foo?a=alpha").method("POST")
.param("b", "bravo").build());
}
@Test
@@ -161,62 +159,60 @@ public class CurlRequestSnippetTests {
this.snippet
.expectCurlRequest(
"post-request-with-overlapping-query-string-and-parameters")
.withContents(
codeBlock("bash")
.content(
"$ curl 'http://localhost/foo?a=alpha' -i -X POST -d 'b=bravo'"));
.withContents(codeBlock("bash").content(
"$ curl 'http://localhost/foo?a=alpha' -i -X POST -d 'b=bravo'"));
new CurlRequestSnippet().document(new OperationBuilder(
"post-request-with-overlapping-query-string-and-parameters", this.snippet
.getOutputDirectory()).request("http://localhost/foo?a=alpha")
.method("POST").param("a", "alpha").param("b", "bravo").build());
"post-request-with-overlapping-query-string-and-parameters",
this.snippet.getOutputDirectory()).request("http://localhost/foo?a=alpha")
.method("POST").param("a", "alpha").param("b", "bravo").build());
}
@Test
public void putRequestWithOneParameter() throws IOException {
this.snippet.expectCurlRequest("put-request-with-one-parameter").withContents(
codeBlock("bash").content(
"$ curl 'http://localhost/foo' -i -X PUT -d 'k1=v1'"));
new CurlRequestSnippet().document(new OperationBuilder(
"put-request-with-one-parameter", this.snippet.getOutputDirectory())
.request("http://localhost/foo").method("PUT").param("k1", "v1").build());
this.snippet.expectCurlRequest("put-request-with-one-parameter")
.withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo' -i -X PUT -d 'k1=v1'"));
new CurlRequestSnippet()
.document(new OperationBuilder("put-request-with-one-parameter",
this.snippet.getOutputDirectory()).request("http://localhost/foo")
.method("PUT").param("k1", "v1").build());
}
@Test
public void putRequestWithMultipleParameters() throws IOException {
this.snippet.expectCurlRequest("put-request-with-multiple-parameters")
.withContents(
codeBlock("bash").content(
"$ curl 'http://localhost/foo' -i -X PUT"
+ " -d 'k1=v1&k1=v1-bis&k2=v2'"));
.withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo' -i -X PUT"
+ " -d 'k1=v1&k1=v1-bis&k2=v2'"));
new CurlRequestSnippet()
.document(new OperationBuilder("put-request-with-multiple-parameters",
this.snippet.getOutputDirectory())
.request("http://localhost/foo").method("PUT").param("k1", "v1")
.param("k1", "v1-bis").param("k2", "v2").build());
this.snippet.getOutputDirectory()).request("http://localhost/foo")
.method("PUT").param("k1", "v1").param("k1", "v1-bis")
.param("k2", "v2").build());
}
@Test
public void putRequestWithUrlEncodedParameter() throws IOException {
this.snippet.expectCurlRequest("put-request-with-url-encoded-parameter")
.withContents(
codeBlock("bash").content(
"$ curl 'http://localhost/foo' -i -X PUT -d 'k1=a%26b'"));
new CurlRequestSnippet().document(new OperationBuilder(
"put-request-with-url-encoded-parameter", this.snippet
.getOutputDirectory()).request("http://localhost/foo")
.method("PUT").param("k1", "a&b").build());
.withContents(codeBlock("bash").content(
"$ curl 'http://localhost/foo' -i -X PUT -d 'k1=a%26b'"));
new CurlRequestSnippet()
.document(new OperationBuilder("put-request-with-url-encoded-parameter",
this.snippet.getOutputDirectory()).request("http://localhost/foo")
.method("PUT").param("k1", "a&b").build());
}
@Test
public void requestWithHeaders() throws IOException {
this.snippet.expectCurlRequest("request-with-headers").withContents(
codeBlock("bash").content(
"$ curl 'http://localhost/foo' -i"
+ " -H 'Content-Type: application/json' -H 'a: alpha'"));
this.snippet.expectCurlRequest("request-with-headers")
.withContents(codeBlock("bash").content("$ curl 'http://localhost/foo' -i"
+ " -H 'Content-Type: application/json' -H 'a: alpha'"));
new CurlRequestSnippet().document(new OperationBuilder("request-with-headers",
this.snippet.getOutputDirectory()).request("http://localhost/foo")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header("a", "alpha").build());
this.snippet.getOutputDirectory())
.request("http://localhost/foo")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_JSON_VALUE)
.header("a", "alpha").build());
}
@Test
@@ -226,11 +222,14 @@ public class CurlRequestSnippetTests {
+ "'metadata={\"description\": \"foo\"}'";
this.snippet.expectCurlRequest("multipart-post-no-original-filename")
.withContents(codeBlock("bash").content(expectedContent));
new CurlRequestSnippet().document(new OperationBuilder(
"multipart-post-no-original-filename", this.snippet.getOutputDirectory())
.request("http://localhost/upload").method("POST")
.header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE)
.part("metadata", "{\"description\": \"foo\"}".getBytes()).build());
new CurlRequestSnippet()
.document(new OperationBuilder("multipart-post-no-original-filename",
this.snippet.getOutputDirectory())
.request("http://localhost/upload").method("POST")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.MULTIPART_FORM_DATA_VALUE)
.part("metadata", "{\"description\": \"foo\"}".getBytes())
.build());
}
@Test
@@ -238,15 +237,17 @@ public class CurlRequestSnippetTests {
String expectedContent = "$ curl 'http://localhost/upload' -i -X POST -H "
+ "'Content-Type: multipart/form-data' -F "
+ "'image=@documents/images/example.png;type=image/png'";
this.snippet.expectCurlRequest("multipart-post-with-content-type").withContents(
codeBlock("bash").content(expectedContent));
new CurlRequestSnippet().document(new OperationBuilder(
"multipart-post-with-content-type", this.snippet.getOutputDirectory())
.request("http://localhost/upload").method("POST")
.header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE)
.part("image", new byte[0])
.header(HttpHeaders.CONTENT_TYPE, MediaType.IMAGE_PNG_VALUE)
.submittedFileName("documents/images/example.png").build());
this.snippet.expectCurlRequest("multipart-post-with-content-type")
.withContents(codeBlock("bash").content(expectedContent));
new CurlRequestSnippet()
.document(new OperationBuilder("multipart-post-with-content-type",
this.snippet.getOutputDirectory())
.request("http://localhost/upload").method("POST")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.MULTIPART_FORM_DATA_VALUE)
.part("image", new byte[0])
.header(HttpHeaders.CONTENT_TYPE, MediaType.IMAGE_PNG_VALUE)
.submittedFileName("documents/images/example.png").build());
}
@Test
@@ -254,14 +255,15 @@ public class CurlRequestSnippetTests {
String expectedContent = "$ curl 'http://localhost/upload' -i -X POST -H "
+ "'Content-Type: multipart/form-data' -F "
+ "'image=@documents/images/example.png'";
this.snippet.expectCurlRequest("multipart-post").withContents(
codeBlock("bash").content(expectedContent));
new CurlRequestSnippet().document(new OperationBuilder("multipart-post",
this.snippet.getOutputDirectory()).request("http://localhost/upload")
.method("POST")
.header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE)
.part("image", new byte[0])
.submittedFileName("documents/images/example.png").build());
this.snippet.expectCurlRequest("multipart-post")
.withContents(codeBlock("bash").content(expectedContent));
new CurlRequestSnippet().document(
new OperationBuilder("multipart-post", this.snippet.getOutputDirectory())
.request("http://localhost/upload").method("POST")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.MULTIPART_FORM_DATA_VALUE)
.part("image", new byte[0])
.submittedFileName("documents/images/example.png").build());
}
@Test
@@ -270,45 +272,46 @@ public class CurlRequestSnippetTests {
+ "'Content-Type: multipart/form-data' -F "
+ "'image=@documents/images/example.png' -F 'a=apple' -F 'a=avocado' "
+ "-F 'b=banana'";
this.snippet.expectCurlRequest("multipart-post-with-parameters").withContents(
codeBlock("bash").content(expectedContent));
new CurlRequestSnippet().document(new OperationBuilder(
"multipart-post-with-parameters", this.snippet.getOutputDirectory())
.request("http://localhost/upload").method("POST")
.header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE)
.part("image", new byte[0])
.submittedFileName("documents/images/example.png").and()
.param("a", "apple", "avocado").param("b", "banana").build());
this.snippet.expectCurlRequest("multipart-post-with-parameters")
.withContents(codeBlock("bash").content(expectedContent));
new CurlRequestSnippet()
.document(new OperationBuilder("multipart-post-with-parameters",
this.snippet.getOutputDirectory())
.request("http://localhost/upload").method("POST")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.MULTIPART_FORM_DATA_VALUE)
.part("image", new byte[0])
.submittedFileName("documents/images/example.png").and()
.param("a", "apple", "avocado").param("b", "banana").build());
}
@Test
public void customAttributes() throws IOException {
this.snippet.expectCurlRequest("custom-attributes").withContents(
containsString("curl request title"));
this.snippet.expectCurlRequest("custom-attributes")
.withContents(containsString("curl request title"));
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("curl-request"))
.willReturn(
new FileSystemResource(
"src/test/resources/custom-snippet-templates/curl-request-with-title.snippet"));
.willReturn(new FileSystemResource(
"src/test/resources/custom-snippet-templates/curl-request-with-title.snippet"));
new CurlRequestSnippet(attributes(key("title").value("curl request title")))
.document(new OperationBuilder("custom-attributes", this.snippet
.getOutputDirectory())
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver))
.request("http://localhost/foo").build());
.document(new OperationBuilder("custom-attributes",
this.snippet.getOutputDirectory())
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver))
.request("http://localhost/foo").build());
}
@Test
public void basicAuthCredentialsAreSuppliedUsingUserOption() throws IOException {
this.snippet.expectCurlRequest("basic-auth").withContents(
codeBlock("bash").content(
"$ curl 'http://localhost/foo' -i -u 'user:secret'"));
new CurlRequestSnippet().document(new OperationBuilder("basic-auth", this.snippet
.getOutputDirectory())
.request("http://localhost/foo")
.header(HttpHeaders.AUTHORIZATION,
"Basic " + Base64Utils.encodeToString("user:secret".getBytes()))
.build());
this.snippet.expectCurlRequest("basic-auth").withContents(codeBlock("bash")
.content("$ curl 'http://localhost/foo' -i -u 'user:secret'"));
new CurlRequestSnippet().document(
new OperationBuilder("basic-auth", this.snippet.getOutputDirectory())
.request("http://localhost/foo")
.header(HttpHeaders.AUTHORIZATION,
"Basic " + Base64Utils
.encodeToString("user:secret".getBytes()))
.build());
}
}

View File

@@ -22,6 +22,7 @@ import java.util.Arrays;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.restdocs.operation.Parameters;
import static org.hamcrest.CoreMatchers.equalTo;
@@ -43,23 +44,23 @@ public class QueryStringParserTests {
@Test
public void noParameters() {
Parameters parameters = this.queryStringParser.parse(URI
.create("http://localhost"));
Parameters parameters = this.queryStringParser
.parse(URI.create("http://localhost"));
assertThat(parameters.size(), is(equalTo(0)));
}
@Test
public void singleParameter() {
Parameters parameters = this.queryStringParser.parse(URI
.create("http://localhost?a=alpha"));
Parameters parameters = this.queryStringParser
.parse(URI.create("http://localhost?a=alpha"));
assertThat(parameters.size(), is(equalTo(1)));
assertThat(parameters, hasEntry("a", Arrays.asList("alpha")));
}
@Test
public void multipleParameters() {
Parameters parameters = this.queryStringParser.parse(URI
.create("http://localhost?a=alpha&b=bravo&c=charlie"));
Parameters parameters = this.queryStringParser
.parse(URI.create("http://localhost?a=alpha&b=bravo&c=charlie"));
assertThat(parameters.size(), is(equalTo(3)));
assertThat(parameters, hasEntry("a", Arrays.asList("alpha")));
assertThat(parameters, hasEntry("b", Arrays.asList("bravo")));
@@ -68,16 +69,16 @@ public class QueryStringParserTests {
@Test
public void multipleParametersWithSameKey() {
Parameters parameters = this.queryStringParser.parse(URI
.create("http://localhost?a=apple&a=avocado"));
Parameters parameters = this.queryStringParser
.parse(URI.create("http://localhost?a=apple&a=avocado"));
assertThat(parameters.size(), is(equalTo(1)));
assertThat(parameters, hasEntry("a", Arrays.asList("apple", "avocado")));
}
@Test
public void encoded() {
Parameters parameters = this.queryStringParser.parse(URI
.create("http://localhost?a=al%26%3Dpha"));
Parameters parameters = this.queryStringParser
.parse(URI.create("http://localhost?a=al%26%3Dpha"));
assertThat(parameters.size(), is(equalTo(1)));
assertThat(parameters, hasEntry("a", Arrays.asList("al&=pha")));
}

View File

@@ -22,6 +22,7 @@ import java.util.Arrays;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.core.io.FileSystemResource;
import org.springframework.restdocs.snippet.SnippetException;
import org.springframework.restdocs.templates.TemplateEngine;
@@ -56,44 +57,51 @@ public class RequestHeadersSnippetTests {
@Test
public void requestWithHeaders() throws IOException {
this.snippet.expectRequestHeaders("request-with-headers").withContents(
tableWithHeader("Name", "Description").row("X-Test", "one")
this.snippet.expectRequestHeaders("request-with-headers")
.withContents(tableWithHeader("Name", "Description").row("X-Test", "one")
.row("Accept", "two").row("Accept-Encoding", "three")
.row("Accept-Language", "four").row("Cache-Control", "five")
.row("Connection", "six"));
new RequestHeadersSnippet(Arrays.asList(
headerWithName("X-Test").description("one"), headerWithName("Accept")
.description("two"), headerWithName("Accept-Encoding")
.description("three"), headerWithName("Accept-Language")
.description("four"), headerWithName("Cache-Control")
.description("five"),
headerWithName("Connection").description("six")))
.document(new OperationBuilder("request-with-headers", this.snippet
.getOutputDirectory()).request("http://localhost")
.header("X-Test", "test").header("Accept", "*/*")
.header("Accept-Encoding", "gzip, deflate")
.header("Accept-Language", "en-US,en;q=0.5")
.header("Cache-Control", "max-age=0")
.header("Connection", "keep-alive").build());
new RequestHeadersSnippet(
Arrays.asList(headerWithName("X-Test").description("one"),
headerWithName("Accept").description("two"),
headerWithName("Accept-Encoding").description("three"),
headerWithName("Accept-Language").description("four"),
headerWithName("Cache-Control").description("five"),
headerWithName("Connection").description("six")))
.document(new OperationBuilder("request-with-headers",
this.snippet.getOutputDirectory())
.request("http://localhost")
.header("X-Test", "test")
.header("Accept", "*/*")
.header("Accept-Encoding",
"gzip, deflate")
.header("Accept-Language", "en-US,en;q=0.5")
.header("Cache-Control", "max-age=0")
.header("Connection", "keep-alive").build());
}
@Test
public void caseInsensitiveRequestHeaders() throws IOException {
this.snippet
.expectRequestHeaders("case-insensitive-request-headers")
.withContents(tableWithHeader("Name", "Description").row("X-Test", "one"));
new RequestHeadersSnippet(Arrays.asList(headerWithName("X-Test").description(
"one"))).document(new OperationBuilder(
"case-insensitive-request-headers", this.snippet.getOutputDirectory())
.request("/").header("X-test", "test").build());
this.snippet.expectRequestHeaders("case-insensitive-request-headers")
.withContents(
tableWithHeader("Name", "Description").row("X-Test", "one"));
new RequestHeadersSnippet(
Arrays.asList(headerWithName("X-Test").description("one")))
.document(new OperationBuilder("case-insensitive-request-headers",
this.snippet.getOutputDirectory()).request("/")
.header("X-test", "test").build());
}
@Test
public void undocumentedRequestHeader() throws IOException {
new RequestHeadersSnippet(Arrays.asList(headerWithName("X-Test").description(
"one"))).document(new OperationBuilder("undocumented-request-header",
this.snippet.getOutputDirectory()).request("http://localhost")
.header("X-Test", "test").header("Accept", "*/*").build());
new RequestHeadersSnippet(
Arrays.asList(headerWithName("X-Test").description("one")))
.document(new OperationBuilder("undocumented-request-header",
this.snippet.getOutputDirectory())
.request("http://localhost")
.header("X-Test", "test").header("Accept", "*/*")
.build());
}
@Test
@@ -102,9 +110,11 @@ public class RequestHeadersSnippetTests {
this.thrown
.expectMessage(equalTo("Headers with the following names were not found"
+ " in the request: [Accept]"));
new RequestHeadersSnippet(Arrays.asList(headerWithName("Accept").description(
"one"))).document(new OperationBuilder("missing-request-headers",
this.snippet.getOutputDirectory()).request("http://localhost").build());
new RequestHeadersSnippet(
Arrays.asList(headerWithName("Accept").description("one")))
.document(new OperationBuilder("missing-request-headers",
this.snippet.getOutputDirectory())
.request("http://localhost").build());
}
@Test
@@ -113,11 +123,13 @@ public class RequestHeadersSnippetTests {
this.thrown
.expectMessage(endsWith("Headers with the following names were not found"
+ " in the request: [Accept]"));
new RequestHeadersSnippet(Arrays.asList(headerWithName("Accept").description(
"one"))).document(new OperationBuilder(
"undocumented-request-header-and-missing-request-header", this.snippet
.getOutputDirectory()).request("http://localhost")
.header("X-Test", "test").build());
new RequestHeadersSnippet(
Arrays.asList(headerWithName("Accept").description("one")))
.document(new OperationBuilder(
"undocumented-request-header-and-missing-request-header",
this.snippet.getOutputDirectory())
.request("http://localhost")
.header("X-Test", "test").build());
}
@Test
@@ -129,21 +141,26 @@ public class RequestHeadersSnippetTests {
.row("Accept-Encoding", "two", "bravo")
.row("Accept", "three", "charlie"));
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("request-headers")).willReturn(
snippetResource("request-headers-with-extra-column"));
given(resolver.resolveTemplateResource("request-headers"))
.willReturn(snippetResource("request-headers-with-extra-column"));
new RequestHeadersSnippet(Arrays.asList(
headerWithName("X-Test").description("one").attributes(
key("foo").value("alpha")),
headerWithName("Accept-Encoding").description("two").attributes(
key("foo").value("bravo")),
headerWithName("Accept").description("three").attributes(
key("foo").value("charlie")))).document(new OperationBuilder(
"request-headers-with-custom-attributes", this.snippet
.getOutputDirectory())
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver)).request("http://localhost")
.header("X-Test", "test").header("Accept-Encoding", "gzip, deflate")
.header("Accept", "*/*").build());
headerWithName("X-Test").description("one")
.attributes(key("foo").value("alpha")),
headerWithName("Accept-Encoding").description("two")
.attributes(key("foo").value("bravo")),
headerWithName("Accept").description("three")
.attributes(key("foo").value("charlie"))))
.document(new OperationBuilder(
"request-headers-with-custom-attributes",
this.snippet.getOutputDirectory())
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(
resolver))
.request("http://localhost")
.header("X-Test", "test")
.header("Accept-Encoding",
"gzip, deflate")
.header("Accept", "*/*").build());
}
@Test
@@ -151,20 +168,22 @@ public class RequestHeadersSnippetTests {
this.snippet.expectRequestHeaders("request-headers-with-custom-attributes")
.withContents(startsWith(".Custom title"));
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("request-headers")).willReturn(
snippetResource("request-headers-with-title"));
new RequestHeadersSnippet(Arrays.asList(headerWithName("X-Test").description(
"one")), attributes(key("title").value("Custom title")))
.document(new OperationBuilder("request-headers-with-custom-attributes",
this.snippet.getOutputDirectory())
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver))
.request("http://localhost").header("X-Test", "test").build());
given(resolver.resolveTemplateResource("request-headers"))
.willReturn(snippetResource("request-headers-with-title"));
new RequestHeadersSnippet(
Arrays.asList(headerWithName("X-Test").description("one")),
attributes(key("title").value("Custom title"))).document(
new OperationBuilder("request-headers-with-custom-attributes",
this.snippet.getOutputDirectory())
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver))
.request("http://localhost")
.header("X-Test", "test").build());
}
private FileSystemResource snippetResource(String name) {
return new FileSystemResource("src/test/resources/custom-snippet-templates/"
+ name + ".snippet");
return new FileSystemResource(
"src/test/resources/custom-snippet-templates/" + name + ".snippet");
}
}

View File

@@ -22,6 +22,7 @@ import java.util.Arrays;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.core.io.FileSystemResource;
import org.springframework.restdocs.snippet.SnippetException;
import org.springframework.restdocs.templates.TemplateEngine;
@@ -56,58 +57,65 @@ public class ResponseHeadersSnippetTests {
@Test
public void responseWithHeaders() throws IOException {
this.snippet.expectResponseHeaders("response-headers").withContents(
tableWithHeader("Name", "Description").row("X-Test", "one")
this.snippet.expectResponseHeaders("response-headers")
.withContents(tableWithHeader("Name", "Description").row("X-Test", "one")
.row("Content-Type", "two").row("Etag", "three")
.row("Cache-Control", "five").row("Vary", "six"));
new ResponseHeadersSnippet(Arrays.asList(
headerWithName("X-Test").description("one"),
headerWithName("Content-Type").description("two"), headerWithName("Etag")
.description("three"), headerWithName("Cache-Control")
.description("five"), headerWithName("Vary").description("six")))
.document(new OperationBuilder("response-headers", this.snippet
.getOutputDirectory()).response().header("X-Test", "test")
.header("Content-Type", "application/json")
.header("Etag", "lskjadldj3ii32l2ij23")
.header("Cache-Control", "max-age=0")
.header("Vary", "User-Agent").build());
new ResponseHeadersSnippet(
Arrays.asList(headerWithName("X-Test").description("one"),
headerWithName("Content-Type").description("two"),
headerWithName("Etag").description("three"),
headerWithName("Cache-Control").description("five"),
headerWithName("Vary").description("six")))
.document(new OperationBuilder("response-headers",
this.snippet.getOutputDirectory()).response()
.header("X-Test", "test")
.header("Content-Type",
"application/json")
.header("Etag", "lskjadldj3ii32l2ij23")
.header("Cache-Control", "max-age=0")
.header("Vary", "User-Agent").build());
}
@Test
public void caseInsensitiveResponseHeaders() throws IOException {
this.snippet
.expectResponseHeaders("case-insensitive-response-headers")
.withContents(tableWithHeader("Name", "Description").row("X-Test", "one"));
new ResponseHeadersSnippet(Arrays.asList(headerWithName("X-Test").description(
"one"))).document(new OperationBuilder(
"case-insensitive-response-headers", this.snippet.getOutputDirectory())
.response().header("X-test", "test").build());
this.snippet.expectResponseHeaders("case-insensitive-response-headers")
.withContents(
tableWithHeader("Name", "Description").row("X-Test", "one"));
new ResponseHeadersSnippet(
Arrays.asList(headerWithName("X-Test").description("one"))).document(
new OperationBuilder("case-insensitive-response-headers",
this.snippet.getOutputDirectory()).response()
.header("X-test", "test").build());
}
@Test
public void responseHeadersWithCustomDescriptorAttributes() throws IOException {
this.snippet.expectResponseHeaders("response-headers-with-custom-attributes")
.withContents(
tableWithHeader("Name", "Description", "Foo")
.row("X-Test", "one", "alpha")
.row("Content-Type", "two", "bravo")
.row("Etag", "three", "charlie"));
.withContents(tableWithHeader("Name", "Description", "Foo")
.row("X-Test", "one", "alpha").row("Content-Type", "two", "bravo")
.row("Etag", "three", "charlie"));
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("response-headers")).willReturn(
snippetResource("response-headers-with-extra-column"));
given(resolver.resolveTemplateResource("response-headers"))
.willReturn(snippetResource("response-headers-with-extra-column"));
new ResponseHeadersSnippet(Arrays.asList(
headerWithName("X-Test").description("one").attributes(
key("foo").value("alpha")),
headerWithName("Content-Type").description("two").attributes(
key("foo").value("bravo")),
headerWithName("Etag").description("three").attributes(
key("foo").value("charlie")))).document(new OperationBuilder(
"response-headers-with-custom-attributes", this.snippet
.getOutputDirectory())
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver)).response()
.header("X-Test", "test").header("Content-Type", "application/json")
.header("Etag", "lskjadldj3ii32l2ij23").build());
headerWithName("X-Test").description("one")
.attributes(key("foo").value("alpha")),
headerWithName("Content-Type").description("two")
.attributes(key("foo").value("bravo")),
headerWithName("Etag").description("three")
.attributes(key("foo").value("charlie"))))
.document(new OperationBuilder(
"response-headers-with-custom-attributes",
this.snippet.getOutputDirectory())
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(
resolver))
.response().header("X-Test", "test")
.header("Content-Type",
"application/json")
.header("Etag", "lskjadldj3ii32l2ij23")
.build());
}
@Test
@@ -115,23 +123,26 @@ public class ResponseHeadersSnippetTests {
this.snippet.expectResponseHeaders("response-headers-with-custom-attributes")
.withContents(startsWith(".Custom title"));
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("response-headers")).willReturn(
snippetResource("response-headers-with-title"));
new ResponseHeadersSnippet(Arrays.asList(headerWithName("X-Test").description(
"one")), attributes(key("title").value("Custom title")))
.document(new OperationBuilder("response-headers-with-custom-attributes",
this.snippet.getOutputDirectory())
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver)).response()
.header("X-Test", "test").build());
given(resolver.resolveTemplateResource("response-headers"))
.willReturn(snippetResource("response-headers-with-title"));
new ResponseHeadersSnippet(
Arrays.asList(headerWithName("X-Test").description("one")),
attributes(key("title").value("Custom title"))).document(
new OperationBuilder("response-headers-with-custom-attributes",
this.snippet.getOutputDirectory())
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver))
.response().header("X-Test", "test").build());
}
@Test
public void undocumentedResponseHeader() throws IOException {
new ResponseHeadersSnippet(Arrays.asList(headerWithName("X-Test").description(
"one"))).document(new OperationBuilder("undocumented-response-header",
this.snippet.getOutputDirectory()).response().header("X-Test", "test")
.header("Content-Type", "*/*").build());
new ResponseHeadersSnippet(
Arrays.asList(headerWithName("X-Test").description("one")))
.document(new OperationBuilder("undocumented-response-header",
this.snippet.getOutputDirectory()).response()
.header("X-Test", "test")
.header("Content-Type", "*/*").build());
}
@Test
@@ -140,10 +151,10 @@ public class ResponseHeadersSnippetTests {
this.thrown
.expectMessage(equalTo("Headers with the following names were not found"
+ " in the response: [Content-Type]"));
new ResponseHeadersSnippet(Arrays.asList(headerWithName("Content-Type")
.description("one"))).document(new OperationBuilder(
"missing-response-headers", this.snippet.getOutputDirectory()).response()
.build());
new ResponseHeadersSnippet(
Arrays.asList(headerWithName("Content-Type").description("one")))
.document(new OperationBuilder("missing-response-headers",
this.snippet.getOutputDirectory()).response().build());
}
@Test
@@ -152,16 +163,17 @@ public class ResponseHeadersSnippetTests {
this.thrown
.expectMessage(endsWith("Headers with the following names were not found"
+ " in the response: [Content-Type]"));
new ResponseHeadersSnippet(Arrays.asList(headerWithName("Content-Type")
.description("one"))).document(new OperationBuilder(
"undocumented-response-header-and-missing-response-header", this.snippet
.getOutputDirectory()).response().header("X-Test", "test")
.build());
new ResponseHeadersSnippet(
Arrays.asList(headerWithName("Content-Type").description("one")))
.document(new OperationBuilder(
"undocumented-response-header-and-missing-response-header",
this.snippet.getOutputDirectory()).response()
.header("X-Test", "test").build());
}
private FileSystemResource snippetResource(String name) {
return new FileSystemResource("src/test/resources/custom-snippet-templates/"
+ name + ".snippet");
return new FileSystemResource(
"src/test/resources/custom-snippet-templates/" + name + ".snippet");
}
}

View File

@@ -20,6 +20,7 @@ import java.io.IOException;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
@@ -53,181 +54,200 @@ public class HttpRequestSnippetTests {
@Test
public void getRequest() throws IOException {
this.snippet.expectHttpRequest("get-request").withContents(
httpRequest(RequestMethod.GET, "/foo").header("Alpha", "a").header(
HttpHeaders.HOST, "localhost"));
this.snippet.expectHttpRequest("get-request")
.withContents(httpRequest(RequestMethod.GET, "/foo").header("Alpha", "a")
.header(HttpHeaders.HOST, "localhost"));
new HttpRequestSnippet().document(new OperationBuilder("get-request",
this.snippet.getOutputDirectory()).request("http://localhost/foo")
.header("Alpha", "a").build());
new HttpRequestSnippet().document(
new OperationBuilder("get-request", this.snippet.getOutputDirectory())
.request("http://localhost/foo").header("Alpha", "a").build());
}
@Test
public void getRequestWithQueryString() throws IOException {
this.snippet.expectHttpRequest("get-request-with-query-string").withContents(
httpRequest(RequestMethod.GET, "/foo?bar=baz").header(HttpHeaders.HOST,
"localhost"));
this.snippet.expectHttpRequest("get-request-with-query-string")
.withContents(httpRequest(RequestMethod.GET, "/foo?bar=baz")
.header(HttpHeaders.HOST, "localhost"));
new HttpRequestSnippet().document(new OperationBuilder(
"get-request-with-query-string", this.snippet.getOutputDirectory())
.request("http://localhost/foo?bar=baz").build());
new HttpRequestSnippet()
.document(new OperationBuilder("get-request-with-query-string",
this.snippet.getOutputDirectory())
.request("http://localhost/foo?bar=baz").build());
}
@Test
public void postRequestWithContent() throws IOException {
String content = "Hello, world";
this.snippet.expectHttpRequest("post-request-with-content").withContents(
httpRequest(RequestMethod.POST, "/foo")
this.snippet.expectHttpRequest("post-request-with-content")
.withContents(httpRequest(RequestMethod.POST, "/foo")
.header(HttpHeaders.HOST, "localhost").content(content)
.header(HttpHeaders.CONTENT_LENGTH, content.getBytes().length));
new HttpRequestSnippet().document(new OperationBuilder(
"post-request-with-content", this.snippet.getOutputDirectory())
.request("http://localhost/foo").method("POST").content(content).build());
new HttpRequestSnippet()
.document(new OperationBuilder("post-request-with-content",
this.snippet.getOutputDirectory()).request("http://localhost/foo")
.method("POST").content(content).build());
}
@Test
public void postRequestWithCharset() throws IOException {
String japaneseContent = "\u30b3\u30f3\u30c6\u30f3\u30c4";
byte[] contentBytes = japaneseContent.getBytes("UTF-8");
this.snippet.expectHttpRequest("post-request-with-charset").withContents(
httpRequest(RequestMethod.POST, "/foo")
this.snippet.expectHttpRequest("post-request-with-charset")
.withContents(httpRequest(RequestMethod.POST, "/foo")
.header("Content-Type", "text/plain;charset=UTF-8")
.header(HttpHeaders.HOST, "localhost")
.header(HttpHeaders.CONTENT_LENGTH, contentBytes.length)
.content(japaneseContent));
new HttpRequestSnippet().document(new OperationBuilder(
"post-request-with-charset", this.snippet.getOutputDirectory())
.request("http://localhost/foo").method("POST")
.header("Content-Type", "text/plain;charset=UTF-8").content(contentBytes)
.build());
new HttpRequestSnippet()
.document(new OperationBuilder("post-request-with-charset",
this.snippet.getOutputDirectory()).request("http://localhost/foo")
.method("POST")
.header("Content-Type", "text/plain;charset=UTF-8")
.content(contentBytes).build());
}
@Test
public void postRequestWithParameter() throws IOException {
this.snippet.expectHttpRequest("post-request-with-parameter").withContents(
httpRequest(RequestMethod.POST, "/foo")
this.snippet.expectHttpRequest("post-request-with-parameter")
.withContents(httpRequest(RequestMethod.POST, "/foo")
.header(HttpHeaders.HOST, "localhost")
.header("Content-Type", "application/x-www-form-urlencoded")
.content("b%26r=baz&a=alpha"));
new HttpRequestSnippet().document(new OperationBuilder(
"post-request-with-parameter", this.snippet.getOutputDirectory())
.request("http://localhost/foo").method("POST").param("b&r", "baz")
.param("a", "alpha").build());
new HttpRequestSnippet()
.document(new OperationBuilder("post-request-with-parameter",
this.snippet.getOutputDirectory()).request("http://localhost/foo")
.method("POST").param("b&r", "baz").param("a", "alpha")
.build());
}
@Test
public void putRequestWithContent() throws IOException {
String content = "Hello, world";
this.snippet.expectHttpRequest("put-request-with-content").withContents(
httpRequest(RequestMethod.PUT, "/foo")
this.snippet.expectHttpRequest("put-request-with-content")
.withContents(httpRequest(RequestMethod.PUT, "/foo")
.header(HttpHeaders.HOST, "localhost").content(content)
.header(HttpHeaders.CONTENT_LENGTH, content.getBytes().length));
new HttpRequestSnippet().document(new OperationBuilder(
"put-request-with-content", this.snippet.getOutputDirectory())
.request("http://localhost/foo").method("PUT").content(content).build());
new HttpRequestSnippet().document(new OperationBuilder("put-request-with-content",
this.snippet.getOutputDirectory()).request("http://localhost/foo")
.method("PUT").content(content).build());
}
@Test
public void putRequestWithParameter() throws IOException {
this.snippet.expectHttpRequest("put-request-with-parameter").withContents(
httpRequest(RequestMethod.PUT, "/foo")
this.snippet.expectHttpRequest("put-request-with-parameter")
.withContents(httpRequest(RequestMethod.PUT, "/foo")
.header(HttpHeaders.HOST, "localhost")
.header("Content-Type", "application/x-www-form-urlencoded")
.content("b%26r=baz&a=alpha"));
new HttpRequestSnippet().document(new OperationBuilder(
"put-request-with-parameter", this.snippet.getOutputDirectory())
.request("http://localhost/foo").method("PUT").param("b&r", "baz")
.param("a", "alpha").build());
new HttpRequestSnippet()
.document(new OperationBuilder("put-request-with-parameter",
this.snippet.getOutputDirectory()).request("http://localhost/foo")
.method("PUT").param("b&r", "baz").param("a", "alpha")
.build());
}
@Test
public void multipartPost() throws IOException {
String expectedContent = createPart(String.format("Content-Disposition: "
+ "form-data; " + "name=image%n%n<< data >>"));
this.snippet.expectHttpRequest("multipart-post").withContents(
httpRequest(RequestMethod.POST, "/upload")
String expectedContent = createPart(String.format(
"Content-Disposition: " + "form-data; " + "name=image%n%n<< data >>"));
this.snippet
.expectHttpRequest(
"multipart-post")
.withContents(httpRequest(RequestMethod.POST, "/upload")
.header("Content-Type",
"multipart/form-data; boundary=" + BOUNDARY)
.header(HttpHeaders.HOST, "localhost").content(expectedContent));
new HttpRequestSnippet().document(new OperationBuilder("multipart-post",
this.snippet.getOutputDirectory()).request("http://localhost/upload")
.method("POST")
.header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE)
.part("image", "<< data >>".getBytes()).build());
new HttpRequestSnippet().document(
new OperationBuilder("multipart-post", this.snippet.getOutputDirectory())
.request("http://localhost/upload").method("POST")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.MULTIPART_FORM_DATA_VALUE)
.part("image", "<< data >>".getBytes()).build());
}
@Test
public void multipartPostWithParameters() throws IOException {
String param1Part = createPart(String.format("Content-Disposition: form-data; "
+ "name=a%n%napple"), false);
String param2Part = createPart(String.format("Content-Disposition: form-data; "
+ "name=a%n%navocado"), false);
String param3Part = createPart(String.format("Content-Disposition: form-data; "
+ "name=b%n%nbanana"), false);
String filePart = createPart(String.format("Content-Disposition: form-data; "
+ "name=image%n%n<< data >>"));
String param1Part = createPart(
String.format("Content-Disposition: form-data; " + "name=a%n%napple"),
false);
String param2Part = createPart(
String.format("Content-Disposition: form-data; " + "name=a%n%navocado"),
false);
String param3Part = createPart(
String.format("Content-Disposition: form-data; " + "name=b%n%nbanana"),
false);
String filePart = createPart(String
.format("Content-Disposition: form-data; " + "name=image%n%n<< data >>"));
String expectedContent = param1Part + param2Part + param3Part + filePart;
this.snippet.expectHttpRequest("multipart-post-with-parameters").withContents(
httpRequest(RequestMethod.POST, "/upload")
this.snippet
.expectHttpRequest(
"multipart-post-with-parameters")
.withContents(httpRequest(RequestMethod.POST, "/upload")
.header("Content-Type",
"multipart/form-data; boundary=" + BOUNDARY)
.header(HttpHeaders.HOST, "localhost").content(expectedContent));
new HttpRequestSnippet().document(new OperationBuilder(
"multipart-post-with-parameters", this.snippet.getOutputDirectory())
.request("http://localhost/upload").method("POST")
.header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE)
.param("a", "apple", "avocado").param("b", "banana")
.part("image", "<< data >>".getBytes()).build());
new HttpRequestSnippet()
.document(new OperationBuilder("multipart-post-with-parameters",
this.snippet.getOutputDirectory())
.request("http://localhost/upload").method("POST")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.MULTIPART_FORM_DATA_VALUE)
.param("a", "apple", "avocado").param("b", "banana")
.part("image", "<< data >>".getBytes()).build());
}
@Test
public void multipartPostWithContentType() throws IOException {
String expectedContent = createPart(String
.format("Content-Disposition: form-data; name=image%nContent-Type: "
String expectedContent = createPart(
String.format("Content-Disposition: form-data; name=image%nContent-Type: "
+ "image/png%n%n<< data >>"));
this.snippet.expectHttpRequest("multipart-post-with-content-type").withContents(
httpRequest(RequestMethod.POST, "/upload")
this.snippet
.expectHttpRequest(
"multipart-post-with-content-type")
.withContents(httpRequest(RequestMethod.POST, "/upload")
.header("Content-Type",
"multipart/form-data; boundary=" + BOUNDARY)
.header(HttpHeaders.HOST, "localhost").content(expectedContent));
new HttpRequestSnippet().document(new OperationBuilder(
"multipart-post-with-content-type", this.snippet.getOutputDirectory())
.request("http://localhost/upload").method("POST")
.header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE)
.part("image", "<< data >>".getBytes())
.header(HttpHeaders.CONTENT_TYPE, MediaType.IMAGE_PNG_VALUE).build());
new HttpRequestSnippet()
.document(new OperationBuilder("multipart-post-with-content-type",
this.snippet.getOutputDirectory())
.request("http://localhost/upload").method("POST")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.MULTIPART_FORM_DATA_VALUE)
.part("image", "<< data >>".getBytes())
.header(HttpHeaders.CONTENT_TYPE, MediaType.IMAGE_PNG_VALUE)
.build());
}
@Test
public void getRequestWithCustomHost() throws IOException {
this.snippet.expectHttpRequest("get-request-custom-host").withContents(
httpRequest(RequestMethod.GET, "/foo").header(HttpHeaders.HOST,
"api.example.com"));
this.snippet.expectHttpRequest("get-request-custom-host")
.withContents(httpRequest(RequestMethod.GET, "/foo")
.header(HttpHeaders.HOST, "api.example.com"));
new HttpRequestSnippet().document(new OperationBuilder("get-request-custom-host",
this.snippet.getOutputDirectory()).request("http://localhost/foo")
.header(HttpHeaders.HOST, "api.example.com").build());
.header(HttpHeaders.HOST, "api.example.com").build());
}
@Test
public void requestWithCustomSnippetAttributes() throws IOException {
this.snippet.expectHttpRequest("request-with-snippet-attributes").withContents(
containsString("Title for the request"));
this.snippet.expectHttpRequest("request-with-snippet-attributes")
.withContents(containsString("Title for the request"));
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("http-request"))
.willReturn(
new FileSystemResource(
"src/test/resources/custom-snippet-templates/http-request-with-title.snippet"));
.willReturn(new FileSystemResource(
"src/test/resources/custom-snippet-templates/http-request-with-title.snippet"));
new HttpRequestSnippet(attributes(key("title").value("Title for the request")))
.document(new OperationBuilder("request-with-snippet-attributes",
this.snippet.getOutputDirectory())
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver))
.request("http://localhost/foo").build());
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver))
.request("http://localhost/foo").build());
}
private String createPart(String content) {

View File

@@ -20,6 +20,7 @@ import java.io.IOException;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
@@ -50,38 +51,41 @@ public class HttpResponseSnippetTests {
@Test
public void basicResponse() throws IOException {
this.snippet.expectHttpResponse("basic-response").withContents(
httpResponse(HttpStatus.OK));
new HttpResponseSnippet().document(new OperationBuilder("basic-response",
this.snippet.getOutputDirectory()).build());
this.snippet.expectHttpResponse("basic-response")
.withContents(httpResponse(HttpStatus.OK));
new HttpResponseSnippet().document(
new OperationBuilder("basic-response", this.snippet.getOutputDirectory())
.build());
}
@Test
public void nonOkResponse() throws IOException {
this.snippet.expectHttpResponse("non-ok-response").withContents(
httpResponse(HttpStatus.BAD_REQUEST));
new HttpResponseSnippet().document(new OperationBuilder("non-ok-response",
this.snippet.getOutputDirectory()).response()
.status(HttpStatus.BAD_REQUEST.value()).build());
this.snippet.expectHttpResponse("non-ok-response")
.withContents(httpResponse(HttpStatus.BAD_REQUEST));
new HttpResponseSnippet().document(
new OperationBuilder("non-ok-response", this.snippet.getOutputDirectory())
.response().status(HttpStatus.BAD_REQUEST.value()).build());
}
@Test
public void responseWithHeaders() throws IOException {
this.snippet.expectHttpResponse("response-with-headers").withContents(
httpResponse(HttpStatus.OK).header("Content-Type", "application/json")
.header("a", "alpha"));
this.snippet.expectHttpResponse("response-with-headers")
.withContents(httpResponse(HttpStatus.OK)
.header("Content-Type", "application/json").header("a", "alpha"));
new HttpResponseSnippet().document(new OperationBuilder("response-with-headers",
this.snippet.getOutputDirectory()).response()
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header("a", "alpha").build());
this.snippet.getOutputDirectory())
.response()
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_JSON_VALUE)
.header("a", "alpha").build());
}
@Test
public void responseWithContent() throws IOException {
String content = "content";
this.snippet.expectHttpResponse("response-with-content").withContents(
httpResponse(HttpStatus.OK).content(content).header(
HttpHeaders.CONTENT_LENGTH, content.getBytes().length));
this.snippet.expectHttpResponse("response-with-content")
.withContents(httpResponse(HttpStatus.OK).content(content)
.header(HttpHeaders.CONTENT_LENGTH, content.getBytes().length));
new HttpResponseSnippet().document(new OperationBuilder("response-with-content",
this.snippet.getOutputDirectory()).response().content(content).build());
}
@@ -90,31 +94,31 @@ public class HttpResponseSnippetTests {
public void responseWithCharset() throws IOException {
String japaneseContent = "\u30b3\u30f3\u30c6\u30f3\u30c4";
byte[] contentBytes = japaneseContent.getBytes("UTF-8");
this.snippet.expectHttpResponse("response-with-charset").withContents(
httpResponse(HttpStatus.OK)
this.snippet.expectHttpResponse("response-with-charset")
.withContents(httpResponse(HttpStatus.OK)
.header("Content-Type", "text/plain;charset=UTF-8")
.content(japaneseContent)
.header(HttpHeaders.CONTENT_LENGTH, contentBytes.length));
new HttpResponseSnippet().document(new OperationBuilder("response-with-charset",
this.snippet.getOutputDirectory()).response()
.header("Content-Type", "text/plain;charset=UTF-8").content(contentBytes)
.build());
.header("Content-Type", "text/plain;charset=UTF-8")
.content(contentBytes).build());
}
@Test
public void responseWithCustomSnippetAttributes() throws IOException {
this.snippet.expectHttpResponse("response-with-snippet-attributes").withContents(
containsString("Title for the response"));
this.snippet.expectHttpResponse("response-with-snippet-attributes")
.withContents(containsString("Title for the response"));
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("http-response"))
.willReturn(
new FileSystemResource(
"src/test/resources/custom-snippet-templates/http-response-with-title.snippet"));
.willReturn(new FileSystemResource(
"src/test/resources/custom-snippet-templates/http-response-with-title.snippet"));
new HttpResponseSnippet(attributes(key("title").value("Title for the response")))
.document(new OperationBuilder("response-with-snippet-attributes",
this.snippet.getOutputDirectory()).attribute(
TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver)).build());
this.snippet.getOutputDirectory())
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver))
.build());
}
}

View File

@@ -23,6 +23,7 @@ import java.util.Map;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
@@ -47,8 +48,8 @@ public class ContentTypeLinkExtractorTests {
@Test
public void extractionFailsWithNullContentType() throws IOException {
this.thrown.expect(IllegalStateException.class);
new ContentTypeLinkExtractor().extractLinks(this.responseFactory.create(
HttpStatus.OK, new HttpHeaders(), null));
new ContentTypeLinkExtractor().extractLinks(
this.responseFactory.create(HttpStatus.OK, new HttpHeaders(), null));
}
@Test

View File

@@ -28,6 +28,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.springframework.http.HttpStatus;
import org.springframework.restdocs.operation.OperationResponse;
import org.springframework.restdocs.operation.OperationResponseFactory;
@@ -100,7 +101,8 @@ public class LinkExtractorsPayloadTests {
assertLinks(Collections.<Link>emptyList(), links);
}
private void assertLinks(List<Link> expectedLinks, Map<String, List<Link>> actualLinks) {
private void assertLinks(List<Link> expectedLinks,
Map<String, List<Link>> actualLinks) {
MultiValueMap<String, Link> expectedLinksByRel = new LinkedMultiValueMap<>();
for (Link expectedLink : expectedLinks) {
expectedLinksByRel.add(expectedLink.getRel(), expectedLink);

View File

@@ -23,6 +23,7 @@ import java.util.Collections;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.core.io.FileSystemResource;
import org.springframework.restdocs.operation.OperationResponse;
import org.springframework.restdocs.snippet.SnippetException;
@@ -58,22 +59,25 @@ public class LinksSnippetTests {
@Test
public void undocumentedLink() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(equalTo("Links with the following relations were not"
+ " documented: [foo]"));
this.thrown.expectMessage(equalTo(
"Links with the following relations were not" + " documented: [foo]"));
new LinksSnippet(new StubLinkExtractor().withLinks(new Link("foo", "bar")),
Collections.<LinkDescriptor>emptyList()).document(new OperationBuilder(
"undocumented-link", this.snippet.getOutputDirectory()).build());
Collections.<LinkDescriptor>emptyList())
.document(new OperationBuilder("undocumented-link",
this.snippet.getOutputDirectory()).build());
}
@Test
public void ignoredLink() throws IOException {
this.snippet.expectLinks("ignored-link").withContents(
tableWithHeader("Relation", "Description").row("b", "Link b"));
new LinksSnippet(new StubLinkExtractor().withLinks(new Link("a", "alpha"),
new Link("b", "bravo")), Arrays.asList(new LinkDescriptor("a").ignored(),
new LinkDescriptor("b").description("Link b")))
.document(new OperationBuilder("ignored-link", this.snippet
.getOutputDirectory()).build());
new LinksSnippet(
new StubLinkExtractor().withLinks(new Link("a", "alpha"),
new Link("b", "bravo")),
Arrays.asList(new LinkDescriptor("a").ignored(),
new LinkDescriptor("b").description("Link b")))
.document(new OperationBuilder("ignored-link",
this.snippet.getOutputDirectory()).build());
}
@Test
@@ -81,9 +85,10 @@ public class LinksSnippetTests {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(equalTo("Links with the following relations were not"
+ " found in the response: [foo]"));
new LinksSnippet(new StubLinkExtractor(), Arrays.asList(new LinkDescriptor("foo")
.description("bar"))).document(new OperationBuilder("missing-link",
this.snippet.getOutputDirectory()).build());
new LinksSnippet(new StubLinkExtractor(),
Arrays.asList(new LinkDescriptor("foo").description("bar")))
.document(new OperationBuilder("missing-link",
this.snippet.getOutputDirectory()).build());
}
@Test
@@ -92,17 +97,18 @@ public class LinksSnippetTests {
tableWithHeader("Relation", "Description").row("foo", "bar"));
new LinksSnippet(new StubLinkExtractor().withLinks(new Link("foo", "blah")),
Arrays.asList(new LinkDescriptor("foo").description("bar").optional()))
.document(new OperationBuilder("documented-optional-link", this.snippet
.getOutputDirectory()).build());
.document(new OperationBuilder("documented-optional-link",
this.snippet.getOutputDirectory()).build());
}
@Test
public void missingOptionalLink() throws IOException {
this.snippet.expectLinks("missing-optional-link").withContents(
tableWithHeader("Relation", "Description").row("foo", "bar"));
new LinksSnippet(new StubLinkExtractor(), Arrays.asList(new LinkDescriptor("foo")
.description("bar").optional())).document(new OperationBuilder(
"missing-optional-link", this.snippet.getOutputDirectory()).build());
new LinksSnippet(new StubLinkExtractor(),
Arrays.asList(new LinkDescriptor("foo").description("bar").optional()))
.document(new OperationBuilder("missing-optional-link",
this.snippet.getOutputDirectory()).build());
}
@Test
@@ -112,64 +118,74 @@ public class LinksSnippetTests {
+ " documented: [a]. Links with the following relations were not"
+ " found in the response: [foo]"));
new LinksSnippet(new StubLinkExtractor().withLinks(new Link("a", "alpha")),
Arrays.asList(new LinkDescriptor("foo").description("bar")))
.document(new OperationBuilder("undocumented-link-and-missing-link",
this.snippet.getOutputDirectory()).build());
Arrays.asList(new LinkDescriptor("foo").description("bar"))).document(
new OperationBuilder("undocumented-link-and-missing-link",
this.snippet.getOutputDirectory()).build());
}
@Test
public void documentedLinks() throws IOException {
this.snippet.expectLinks("documented-links").withContents(
tableWithHeader("Relation", "Description").row("a", "one")
this.snippet.expectLinks("documented-links")
.withContents(tableWithHeader("Relation", "Description").row("a", "one")
.row("b", "two"));
new LinksSnippet(new StubLinkExtractor().withLinks(new Link("a", "alpha"),
new Link("b", "bravo")), Arrays.asList(
new LinkDescriptor("a").description("one"),
new LinkDescriptor("b").description("two")))
.document(new OperationBuilder("documented-links", this.snippet
.getOutputDirectory()).build());
new LinksSnippet(
new StubLinkExtractor().withLinks(new Link("a", "alpha"),
new Link("b", "bravo")),
Arrays.asList(new LinkDescriptor("a").description("one"),
new LinkDescriptor("b").description("two")))
.document(new OperationBuilder("documented-links",
this.snippet.getOutputDirectory()).build());
}
@Test
public void linksWithCustomDescriptorAttributes() throws IOException {
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("links"))
.willReturn(
new FileSystemResource(
"src/test/resources/custom-snippet-templates/links-with-extra-column.snippet"));
this.snippet.expectLinks("links-with-custom-descriptor-attributes").withContents(
tableWithHeader("Relation", "Description", "Foo")
.willReturn(new FileSystemResource(
"src/test/resources/custom-snippet-templates/links-with-extra-column.snippet"));
this.snippet.expectLinks("links-with-custom-descriptor-attributes")
.withContents(tableWithHeader("Relation", "Description", "Foo")
.row("a", "one", "alpha").row("b", "two", "bravo"));
new LinksSnippet(new StubLinkExtractor().withLinks(new Link("a", "alpha"),
new Link("b", "bravo")), Arrays.asList(
new LinkDescriptor("a").description("one").attributes(
key("foo").value("alpha")),
new LinkDescriptor("b").description("two").attributes(
key("foo").value("bravo")))).document(new OperationBuilder(
"links-with-custom-descriptor-attributes", this.snippet
.getOutputDirectory()).attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver)).build());
new LinksSnippet(
new StubLinkExtractor().withLinks(new Link("a", "alpha"),
new Link("b", "bravo")),
Arrays.asList(
new LinkDescriptor("a").description("one")
.attributes(key("foo").value("alpha")),
new LinkDescriptor("b").description("two")
.attributes(key("foo").value("bravo"))))
.document(new OperationBuilder(
"links-with-custom-descriptor-attributes",
this.snippet.getOutputDirectory())
.attribute(
TemplateEngine.class
.getName(),
new MustacheTemplateEngine(
resolver))
.build());
}
@Test
public void linksWithCustomAttributes() throws IOException {
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("links"))
.willReturn(
new FileSystemResource(
"src/test/resources/custom-snippet-templates/links-with-title.snippet"));
this.snippet.expectLinks("links-with-custom-attributes").withContents(
startsWith(".Title for the links"));
.willReturn(new FileSystemResource(
"src/test/resources/custom-snippet-templates/links-with-title.snippet"));
this.snippet.expectLinks("links-with-custom-attributes")
.withContents(startsWith(".Title for the links"));
new LinksSnippet(new StubLinkExtractor().withLinks(new Link("a", "alpha"),
new Link("b", "bravo")), Arrays.asList(
new LinkDescriptor("a").description("one"),
new LinkDescriptor("b").description("two")), attributes(key("title")
.value("Title for the links"))).document(new OperationBuilder(
"links-with-custom-attributes", this.snippet.getOutputDirectory())
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver)).build());
new LinksSnippet(
new StubLinkExtractor().withLinks(new Link("a", "alpha"),
new Link("b", "bravo")),
Arrays.asList(new LinkDescriptor("a").description("one"),
new LinkDescriptor("b").description("two")),
attributes(key("title").value("Title for the links")))
.document(new OperationBuilder("links-with-custom-attributes",
this.snippet.getOutputDirectory())
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver))
.build());
}
private static class StubLinkExtractor implements LinkExtractor {

View File

@@ -20,6 +20,7 @@ import java.net.URI;
import java.util.Collections;
import org.junit.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;

View File

@@ -19,6 +19,7 @@ package org.springframework.restdocs.operation.preprocess;
import java.util.Arrays;
import org.junit.Test;
import org.springframework.restdocs.operation.OperationRequest;
import static org.hamcrest.CoreMatchers.is;
@@ -44,14 +45,14 @@ public class DelegatingOperationRequestPreprocessorTests {
OperationRequest preprocessedRequest3 = mock(OperationRequest.class);
given(preprocessor1.preprocess(originalRequest)).willReturn(preprocessedRequest1);
given(preprocessor2.preprocess(preprocessedRequest1)).willReturn(
preprocessedRequest2);
given(preprocessor3.preprocess(preprocessedRequest2)).willReturn(
preprocessedRequest3);
given(preprocessor2.preprocess(preprocessedRequest1))
.willReturn(preprocessedRequest2);
given(preprocessor3.preprocess(preprocessedRequest2))
.willReturn(preprocessedRequest3);
OperationRequest result = new DelegatingOperationRequestPreprocessor(
Arrays.asList(preprocessor1, preprocessor2, preprocessor3))
.preprocess(originalRequest);
.preprocess(originalRequest);
assertThat(result, is(preprocessedRequest3));
}

View File

@@ -19,6 +19,7 @@ package org.springframework.restdocs.operation.preprocess;
import java.util.Arrays;
import org.junit.Test;
import org.springframework.restdocs.operation.OperationResponse;
import static org.hamcrest.CoreMatchers.is;
@@ -43,16 +44,16 @@ public class DelegatingOperationResponsePreprocessorTests {
OperationPreprocessor preprocessor3 = mock(OperationPreprocessor.class);
OperationResponse preprocessedResponse3 = mock(OperationResponse.class);
given(preprocessor1.preprocess(originalResponse)).willReturn(
preprocessedResponse1);
given(preprocessor2.preprocess(preprocessedResponse1)).willReturn(
preprocessedResponse2);
given(preprocessor3.preprocess(preprocessedResponse2)).willReturn(
preprocessedResponse3);
given(preprocessor1.preprocess(originalResponse))
.willReturn(preprocessedResponse1);
given(preprocessor2.preprocess(preprocessedResponse1))
.willReturn(preprocessedResponse2);
given(preprocessor3.preprocess(preprocessedResponse2))
.willReturn(preprocessedResponse3);
OperationResponse result = new DelegatingOperationResponsePreprocessor(
Arrays.asList(preprocessor1, preprocessor2, preprocessor3))
.preprocess(originalResponse);
.preprocess(originalResponse);
assertThat(result, is(preprocessedResponse3));
}

View File

@@ -21,6 +21,7 @@ import java.util.Arrays;
import java.util.Collections;
import org.junit.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;

View File

@@ -22,13 +22,13 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import org.springframework.restdocs.hypermedia.Link;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.junit.Test;
import org.springframework.restdocs.hypermedia.Link;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
@@ -59,8 +59,9 @@ public class LinkMaskingContentModifierTests {
@Test
public void formattedHalLinksAreMasked() throws Exception {
assertThat(this.contentModifier.modifyContent(
formattedHalPayloadWithLinks(this.links), null),
assertThat(
this.contentModifier
.modifyContent(formattedHalPayloadWithLinks(this.links), null),
is(equalTo(formattedHalPayloadWithLinks(this.maskedLinks))));
}
@@ -72,16 +73,17 @@ public class LinkMaskingContentModifierTests {
@Test
public void formattedAtomLinksAreMasked() throws Exception {
assertThat(this.contentModifier.modifyContent(
formattedAtomPayloadWithLinks(this.links), null),
assertThat(
this.contentModifier
.modifyContent(formattedAtomPayloadWithLinks(this.links), null),
is(equalTo(formattedAtomPayloadWithLinks(this.maskedLinks))));
}
@Test
public void maskCanBeCustomized() throws Exception {
assertThat(
new LinkMaskingContentModifier("custom").modifyContent(
formattedAtomPayloadWithLinks(this.links), null),
new LinkMaskingContentModifier("custom")
.modifyContent(formattedAtomPayloadWithLinks(this.links), null),
is(equalTo(formattedAtomPayloadWithLinks(new Link("a", "custom"),
new Link("b", "custom")))));
}

View File

@@ -20,6 +20,7 @@ import java.nio.charset.Charset;
import java.util.regex.Pattern;
import org.junit.Test;
import org.springframework.http.MediaType;
import static org.hamcrest.CoreMatchers.equalTo;
@@ -65,8 +66,9 @@ public class PatternReplacingContentModifierTests {
Pattern pattern = Pattern.compile("[0-9]+");
PatternReplacingContentModifier contentModifier = new PatternReplacingContentModifier(
pattern, "<<number>>");
assertThat(contentModifier.modifyContent((japaneseContent + " 123").getBytes(),
new MediaType("text", "plain", Charset.forName("UTF-8"))),
assertThat(
contentModifier.modifyContent((japaneseContent + " 123").getBytes(),
new MediaType("text", "plain", Charset.forName("UTF-8"))),
is(equalTo((japaneseContent + " <<number>>").getBytes())));
}

View File

@@ -18,6 +18,7 @@ package org.springframework.restdocs.operation.preprocess;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.restdocs.test.OutputCapture;
import static org.hamcrest.CoreMatchers.equalTo;
@@ -37,25 +38,25 @@ public class PrettyPrintingContentModifierTests {
@Test
public void prettyPrintJson() throws Exception {
assertThat(new PrettyPrintingContentModifier().modifyContent(
"{\"a\":5}".getBytes(), null), equalTo(String.format("{%n \"a\" : 5%n}")
.getBytes()));
assertThat(new PrettyPrintingContentModifier()
.modifyContent("{\"a\":5}".getBytes(), null),
equalTo(String.format("{%n \"a\" : 5%n}").getBytes()));
}
@Test
public void prettyPrintXml() throws Exception {
assertThat(new PrettyPrintingContentModifier().modifyContent(
"<one a=\"alpha\"><two b=\"bravo\"/></one>".getBytes(), null),
equalTo(String.format(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>%n"
assertThat(
new PrettyPrintingContentModifier().modifyContent(
"<one a=\"alpha\"><two b=\"bravo\"/></one>".getBytes(), null),
equalTo(String
.format("<?xml version=\"1.0\" encoding=\"UTF-8\"?>%n"
+ "<one a=\"alpha\">%n <two b=\"bravo\"/>%n</one>%n")
.getBytes()));
.getBytes()));
}
@Test
public void empytContentIsHandledGracefully() throws Exception {
assertThat(
new PrettyPrintingContentModifier().modifyContent("".getBytes(), null),
assertThat(new PrettyPrintingContentModifier().modifyContent("".getBytes(), null),
equalTo("".getBytes()));
}

View File

@@ -22,9 +22,8 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
@@ -95,8 +94,8 @@ public class JsonFieldProcessorTests {
Map<String, String> entry1 = createEntry("id:1");
Map<String, String> entry2 = createEntry("id:2");
Map<String, String> entry3 = createEntry("id:3");
List<List<Map<String, String>>> alpha = Arrays.asList(
Arrays.asList(entry1, entry2), Arrays.asList(entry3));
List<List<Map<String, String>>> alpha = Arrays
.asList(Arrays.asList(entry1, entry2), Arrays.asList(entry3));
payload.put("a", alpha);
assertThat(this.fieldProcessor.extract(JsonFieldPath.compile("a[][]"), payload),
equalTo((Object) Arrays.asList(entry1, entry2, entry3)));
@@ -108,8 +107,8 @@ public class JsonFieldProcessorTests {
Map<String, String> entry1 = createEntry("id:1");
Map<String, String> entry2 = createEntry("id:2");
Map<String, String> entry3 = createEntry("id:3");
List<List<Map<String, String>>> alpha = Arrays.asList(
Arrays.asList(entry1, entry2), Arrays.asList(entry3));
List<List<Map<String, String>>> alpha = Arrays
.asList(Arrays.asList(entry1, entry2), Arrays.asList(entry3));
payload.put("a", alpha);
assertThat(
this.fieldProcessor.extract(JsonFieldPath.compile("a[][].id"), payload),
@@ -122,12 +121,13 @@ public class JsonFieldProcessorTests {
Map<String, Object> entry1 = createEntry("ids", Arrays.asList(1, 2));
Map<String, Object> entry2 = createEntry("ids", Arrays.asList(3));
Map<String, Object> entry3 = createEntry("ids", Arrays.asList(4));
List<List<Map<String, Object>>> alpha = Arrays.asList(
Arrays.asList(entry1, entry2), Arrays.asList(entry3));
List<List<Map<String, Object>>> alpha = Arrays
.asList(Arrays.asList(entry1, entry2), Arrays.asList(entry3));
payload.put("a", alpha);
assertThat(this.fieldProcessor.extract(JsonFieldPath.compile("a[][].ids"),
payload), equalTo((Object) Arrays.asList(Arrays.asList(1, 2),
Arrays.asList(3), Arrays.asList(4))));
assertThat(
this.fieldProcessor.extract(JsonFieldPath.compile("a[][].ids"), payload),
equalTo((Object) Arrays.asList(Arrays.asList(1, 2), Arrays.asList(3),
Arrays.asList(4))));
}
@Test(expected = FieldDoesNotExistException.class)
@@ -202,8 +202,8 @@ public class JsonFieldProcessorTests {
@SuppressWarnings("unchecked")
@Test
public void removeItemsInArray() throws IOException {
Map<String, Object> payload = new ObjectMapper().readValue(
"{\"a\": [{\"b\":\"bravo\"},{\"b\":\"bravo\"}]}", Map.class);
Map<String, Object> payload = new ObjectMapper()
.readValue("{\"a\": [{\"b\":\"bravo\"},{\"b\":\"bravo\"}]}", Map.class);
this.fieldProcessor.remove(JsonFieldPath.compile("a[].b"), payload);
assertThat(payload.size(), equalTo(0));
}
@@ -211,8 +211,8 @@ public class JsonFieldProcessorTests {
@SuppressWarnings("unchecked")
@Test
public void removeItemsInNestedArray() throws IOException {
Map<String, Object> payload = new ObjectMapper().readValue(
"{\"a\": [[{\"id\":1},{\"id\":2}], [{\"id\":3}]]}", Map.class);
Map<String, Object> payload = new ObjectMapper()
.readValue("{\"a\": [[{\"id\":1},{\"id\":2}], [{\"id\":3}]]}", Map.class);
this.fieldProcessor.remove(JsonFieldPath.compile("a[][].id"), payload);
assertThat(payload.size(), equalTo(0));
}
@@ -223,8 +223,8 @@ public class JsonFieldProcessorTests {
Map<String, Object> alpha = new HashMap<>();
payload.put("a.key", alpha);
alpha.put("b.key", "bravo");
assertThat(this.fieldProcessor.extract(
JsonFieldPath.compile("['a.key']['b.key']"), payload),
assertThat(this.fieldProcessor
.extract(JsonFieldPath.compile("['a.key']['b.key']"), payload),
equalTo((Object) "bravo"));
}

View File

@@ -19,12 +19,11 @@ package org.springframework.restdocs.payload;
import java.io.IOException;
import java.util.Map;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import com.fasterxml.jackson.databind.ObjectMapper;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
@@ -73,30 +72,33 @@ public class JsonFieldTypeResolverTests {
@Test
public void nestedField() throws IOException {
assertThat(this.fieldTypeResolver.resolveFieldType("a.b.c",
createPayload("{\"a\":{\"b\":{\"c\":{}}}}")),
assertThat(
this.fieldTypeResolver.resolveFieldType("a.b.c",
createPayload("{\"a\":{\"b\":{\"c\":{}}}}")),
equalTo(JsonFieldType.OBJECT));
}
@Test
public void multipleFieldsWithSameType() throws IOException {
assertThat(this.fieldTypeResolver.resolveFieldType("a[].id",
createPayload("{\"a\":[{\"id\":1},{\"id\":2}]}")),
assertThat(
this.fieldTypeResolver.resolveFieldType("a[].id",
createPayload("{\"a\":[{\"id\":1},{\"id\":2}]}")),
equalTo(JsonFieldType.NUMBER));
}
@Test
public void multipleFieldsWithDifferentTypes() throws IOException {
assertThat(this.fieldTypeResolver.resolveFieldType("a[].id",
createPayload("{\"a\":[{\"id\":1},{\"id\":true}]}")),
assertThat(
this.fieldTypeResolver.resolveFieldType("a[].id",
createPayload("{\"a\":[{\"id\":1},{\"id\":true}]}")),
equalTo(JsonFieldType.VARIES));
}
@Test
public void nonExistentFieldProducesIllegalArgumentException() throws IOException {
this.thrownException.expect(FieldDoesNotExistException.class);
this.thrownException
.expectMessage("The payload does not contain a field with the path 'a.b'");
this.thrownException.expectMessage(
"The payload does not contain a field with the path 'a.b'");
this.fieldTypeResolver.resolveFieldType("a.b", createPayload("{\"a\":{}}"));
}

View File

@@ -23,6 +23,7 @@ import java.util.Collections;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
@@ -58,69 +59,74 @@ public class RequestFieldsSnippetTests {
@Test
public void mapRequestWithFields() throws IOException {
this.snippet.expectRequestFields("map-request-with-fields").withContents(
tableWithHeader("Path", "Type", "Description")
this.snippet.expectRequestFields("map-request-with-fields")
.withContents(tableWithHeader("Path", "Type", "Description")
.row("a.b", "Number", "one").row("a.c", "String", "two")
.row("a", "Object", "three"));
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a.b").description("one"),
fieldWithPath("a.c").description("two"),
fieldWithPath("a").description("three"))).document(new OperationBuilder(
"map-request-with-fields", this.snippet.getOutputDirectory())
.request("http://localhost")
.content("{\"a\": {\"b\": 5, \"c\": \"charlie\"}}").build());
fieldWithPath("a").description("three")))
.document(new OperationBuilder("map-request-with-fields",
this.snippet.getOutputDirectory())
.request("http://localhost")
.content(
"{\"a\": {\"b\": 5, \"c\": \"charlie\"}}")
.build());
}
@Test
public void arrayRequestWithFields() throws IOException {
this.snippet.expectRequestFields("array-request-with-fields").withContents(
tableWithHeader("Path", "Type", "Description")
this.snippet.expectRequestFields("array-request-with-fields")
.withContents(tableWithHeader("Path", "Type", "Description")
.row("[]a.b", "Number", "one").row("[]a.c", "String", "two")
.row("[]a", "Object", "three"));
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("[]a.b").description("one"),
fieldWithPath("[]a.c").description("two"), fieldWithPath("[]a")
.description("three"))).document(new OperationBuilder(
"array-request-with-fields", this.snippet.getOutputDirectory())
.request("http://localhost")
.content("[{\"a\": {\"b\": 5}},{\"a\": {\"c\": \"charlie\"}}]").build());
fieldWithPath("[]a.c").description("two"),
fieldWithPath("[]a").description("three")))
.document(new OperationBuilder("array-request-with-fields",
this.snippet.getOutputDirectory())
.request("http://localhost")
.content(
"[{\"a\": {\"b\": 5}},{\"a\": {\"c\": \"charlie\"}}]")
.build());
}
@Test
public void undocumentedRequestField() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown
.expectMessage(startsWith("The following parts of the payload were not"
+ " documented:"));
this.thrown.expectMessage(startsWith(
"The following parts of the payload were not" + " documented:"));
new RequestFieldsSnippet(Collections.<FieldDescriptor>emptyList())
.document(new OperationBuilder("undocumented-request-field", this.snippet
.getOutputDirectory()).request("http://localhost")
.content("{\"a\": 5}").build());
.document(new OperationBuilder("undocumented-request-field",
this.snippet.getOutputDirectory()).request("http://localhost")
.content("{\"a\": 5}").build());
}
@Test
public void ignoredRequestField() throws IOException {
this.snippet.expectRequestFields("ignored-request-field").withContents(
tableWithHeader("Path", "Type", "Description").row("b", "Number",
"Field b"));
this.snippet.expectRequestFields("ignored-request-field")
.withContents(tableWithHeader("Path", "Type", "Description").row("b",
"Number", "Field b"));
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a").ignored(),
fieldWithPath("b").description("Field b")))
.document(new OperationBuilder("ignored-request-field", this.snippet
.getOutputDirectory()).request("http://localhost")
.content("{\"a\": 5, \"b\": 4}").build());
.document(new OperationBuilder("ignored-request-field",
this.snippet.getOutputDirectory())
.request("http://localhost")
.content("{\"a\": 5, \"b\": 4}").build());
}
@Test
public void missingRequestField() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown
.expectMessage(equalTo("Fields with the following paths were not found"
+ " in the payload: [a.b]"));
this.thrown.expectMessage(equalTo("Fields with the following paths were not found"
+ " in the payload: [a.b]"));
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a.b").description("one")))
.document(new OperationBuilder("missing-request-fields", this.snippet
.getOutputDirectory()).request("http://localhost").content("{}")
.build());
.document(new OperationBuilder("missing-request-fields",
this.snippet.getOutputDirectory()).request("http://localhost")
.content("{}").build());
}
@Test
@@ -128,17 +134,16 @@ public class RequestFieldsSnippetTests {
this.thrown.expect(FieldTypeRequiredException.class);
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a.b").description("one")
.optional())).document(new OperationBuilder(
"missing-optional-request-field-with-no-type", this.snippet
.getOutputDirectory()).request("http://localhost").content("{ }")
.build());
"missing-optional-request-field-with-no-type",
this.snippet.getOutputDirectory()).request("http://localhost")
.content("{ }").build());
}
@Test
public void undocumentedRequestFieldAndMissingRequestField() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown
.expectMessage(startsWith("The following parts of the payload were not"
+ " documented:"));
this.thrown.expectMessage(startsWith(
"The following parts of the payload were not" + " documented:"));
this.thrown
.expectMessage(endsWith("Fields with the following paths were not found"
+ " in the payload: [a.b]"));
@@ -146,153 +151,163 @@ public class RequestFieldsSnippetTests {
.document(new OperationBuilder(
"undocumented-request-field-and-missing-request-field",
this.snippet.getOutputDirectory()).request("http://localhost")
.content("{ \"a\": { \"c\": 5 }}").build());
.content("{ \"a\": { \"c\": 5 }}").build());
}
@Test
public void requestFieldsWithCustomDescriptorAttributes() throws IOException {
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("request-fields")).willReturn(
snippetResource("request-fields-with-extra-column"));
this.snippet.expectRequestFields(
"request-fields-with-custom-descriptor-attributes").withContents(
tableWithHeader("Path", "Type", "Description", "Foo")
given(resolver.resolveTemplateResource("request-fields"))
.willReturn(snippetResource("request-fields-with-extra-column"));
this.snippet
.expectRequestFields("request-fields-with-custom-descriptor-attributes")
.withContents(tableWithHeader("Path", "Type", "Description", "Foo")
.row("a.b", "Number", "one", "alpha")
.row("a.c", "String", "two", "bravo")
.row("a", "Object", "three", "charlie"));
new RequestFieldsSnippet(Arrays.asList(
fieldWithPath("a.b").description("one").attributes(
key("foo").value("alpha")),
fieldWithPath("a.c").description("two").attributes(
key("foo").value("bravo")),
fieldWithPath("a").description("three").attributes(
key("foo").value("charlie")))).document(new OperationBuilder(
"request-fields-with-custom-descriptor-attributes", this.snippet
.getOutputDirectory())
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver)).request("http://localhost")
.content("{\"a\": {\"b\": 5, \"c\": \"charlie\"}}").build());
fieldWithPath("a.b").description("one")
.attributes(key("foo").value("alpha")),
fieldWithPath("a.c").description("two")
.attributes(key("foo").value("bravo")),
fieldWithPath("a").description("three")
.attributes(key("foo").value("charlie"))))
.document(new OperationBuilder(
"request-fields-with-custom-descriptor-attributes",
this.snippet.getOutputDirectory())
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(
resolver))
.request("http://localhost")
.content(
"{\"a\": {\"b\": 5, \"c\": \"charlie\"}}")
.build());
}
@Test
public void requestFieldsWithCustomAttributes() throws IOException {
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("request-fields")).willReturn(
snippetResource("request-fields-with-title"));
given(resolver.resolveTemplateResource("request-fields"))
.willReturn(snippetResource("request-fields-with-title"));
this.snippet.expectRequestFields("request-fields-with-custom-attributes")
.withContents(startsWith(".Custom title"));
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one")),
attributes(key("title").value("Custom title")))
.document(new OperationBuilder("request-fields-with-custom-attributes",
this.snippet.getOutputDirectory())
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver))
.request("http://localhost").content("{\"a\": \"foo\"}").build());
attributes(key("title").value("Custom title"))).document(
new OperationBuilder("request-fields-with-custom-attributes",
this.snippet.getOutputDirectory())
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver))
.request("http://localhost")
.content("{\"a\": \"foo\"}").build());
}
@Test
public void requestFieldsWithListDescription() throws IOException {
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("request-fields")).willReturn(
snippetResource("request-fields-with-list-description"));
given(resolver.resolveTemplateResource("request-fields"))
.willReturn(snippetResource("request-fields-with-list-description"));
this.snippet.expectRequestFields("request-fields-with-list-description")
.withContents(
tableWithHeader("Path", "Type", "Description")
//
.row("a", "String", String.format(" - one%n - two"))
.configuration("[cols=\"1,1,1a\"]"));
.withContents(tableWithHeader("Path", "Type", "Description")
//
.row("a", "String", String.format(" - one%n - two"))
.configuration("[cols=\"1,1,1a\"]"));
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a").description(
Arrays.asList("one", "two"))))
.document(new OperationBuilder("request-fields-with-list-description",
this.snippet.getOutputDirectory())
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver))
.request("http://localhost").content("{\"a\": \"foo\"}").build());
new RequestFieldsSnippet(Arrays.asList(
fieldWithPath("a").description(Arrays.asList("one", "two")))).document(
new OperationBuilder("request-fields-with-list-description",
this.snippet.getOutputDirectory())
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver))
.request("http://localhost")
.content("{\"a\": \"foo\"}").build());
}
@Test
public void xmlRequestFields() throws IOException {
this.snippet.expectRequestFields("xml-request").withContents(
tableWithHeader("Path", "Type", "Description").row("a/b", "b", "one")
.row("a/c", "c", "two").row("a", "a", "three"));
this.snippet.expectRequestFields("xml-request")
.withContents(tableWithHeader("Path", "Type", "Description")
.row("a/b", "b", "one").row("a/c", "c", "two")
.row("a", "a", "three"));
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a/b").description("one")
.type("b"), fieldWithPath("a/c").description("two").type("c"),
fieldWithPath("a").description("three").type("a")))
.document(new OperationBuilder("xml-request", this.snippet
.getOutputDirectory())
.request("http://localhost")
.content("<a><b>5</b><c>charlie</c></a>")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE)
.build());
new RequestFieldsSnippet(
Arrays.asList(fieldWithPath("a/b").description("one").type("b"),
fieldWithPath("a/c").description("two").type("c"),
fieldWithPath("a").description("three")
.type("a"))).document(new OperationBuilder("xml-request",
this.snippet.getOutputDirectory())
.request("http://localhost")
.content("<a><b>5</b><c>charlie</c></a>")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_XML_VALUE)
.build());
}
@Test
public void undocumentedXmlRequestField() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown
.expectMessage(startsWith("The following parts of the payload were not"
+ " documented:"));
this.thrown.expectMessage(startsWith(
"The following parts of the payload were not" + " documented:"));
new RequestFieldsSnippet(Collections.<FieldDescriptor>emptyList())
.document(new OperationBuilder("undocumented-xml-request-field",
this.snippet.getOutputDirectory())
.request("http://localhost")
.content("<a><b>5</b></a>")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE)
.build());
this.snippet.getOutputDirectory()).request("http://localhost")
.content("<a><b>5</b></a>")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_XML_VALUE)
.build());
}
@Test
public void xmlRequestFieldWithNoType() throws IOException {
this.thrown.expect(FieldTypeRequiredException.class);
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one")))
.document(new OperationBuilder("missing-xml-request", this.snippet
.getOutputDirectory())
.request("http://localhost")
.content("<a>5</a>")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE)
.build());
.document(new OperationBuilder("missing-xml-request",
this.snippet.getOutputDirectory()).request("http://localhost")
.content("<a>5</a>").header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_XML_VALUE)
.build());
}
@Test
public void missingXmlRequestField() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown
.expectMessage(equalTo("Fields with the following paths were not found"
+ " in the payload: [a/b]"));
this.thrown.expectMessage(equalTo("Fields with the following paths were not found"
+ " in the payload: [a/b]"));
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a/b").description("one"),
fieldWithPath("a").description("one"))).document(new OperationBuilder(
"missing-xml-request-fields", this.snippet.getOutputDirectory())
.request("http://localhost").content("<a></a>")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE)
.build());
fieldWithPath("a").description("one")))
.document(
new OperationBuilder("missing-xml-request-fields",
this.snippet.getOutputDirectory())
.request("http://localhost")
.content("<a></a>")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_XML_VALUE)
.build());
}
@Test
public void undocumentedXmlRequestFieldAndMissingXmlRequestField() throws IOException {
public void undocumentedXmlRequestFieldAndMissingXmlRequestField()
throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown
.expectMessage(startsWith("The following parts of the payload were not"
+ " documented:"));
this.thrown.expectMessage(startsWith(
"The following parts of the payload were not" + " documented:"));
this.thrown
.expectMessage(endsWith("Fields with the following paths were not found"
+ " in the payload: [a/b]"));
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a/b").description("one")))
.document(new OperationBuilder(
"undocumented-xml-request-field-and-missing-xml-request-field",
this.snippet.getOutputDirectory())
.request("http://localhost")
.content("<a><c>5</c></a>")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE)
.build());
this.snippet.getOutputDirectory()).request("http://localhost")
.content("<a><c>5</c></a>")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_XML_VALUE)
.build());
}
private FileSystemResource snippetResource(String name) {
return new FileSystemResource("src/test/resources/custom-snippet-templates/"
+ name + ".snippet");
return new FileSystemResource(
"src/test/resources/custom-snippet-templates/" + name + ".snippet");
}
}

View File

@@ -23,6 +23,7 @@ import java.util.Collections;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
@@ -58,261 +59,282 @@ public class ResponseFieldsSnippetTests {
@Test
public void mapResponseWithFields() throws IOException {
this.snippet.expectResponseFields("map-response-with-fields").withContents(
tableWithHeader("Path", "Type", "Description").row("id", "Number", "one")
.row("date", "String", "two").row("assets", "Array", "three")
.row("assets[]", "Object", "four")
this.snippet.expectResponseFields("map-response-with-fields")
.withContents(tableWithHeader("Path", "Type", "Description")
.row("id", "Number", "one").row("date", "String", "two")
.row("assets", "Array", "three").row("assets[]", "Object", "four")
.row("assets[].id", "Number", "five")
.row("assets[].name", "String", "six"));
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("id").description("one"),
fieldWithPath("date").description("two"), fieldWithPath("assets")
.description("three"),
fieldWithPath("date").description("two"),
fieldWithPath("assets").description("three"),
fieldWithPath("assets[]").description("four"),
fieldWithPath("assets[].id").description("five"),
fieldWithPath("assets[].name").description("six")))
.document(new OperationBuilder("map-response-with-fields", this.snippet
.getOutputDirectory())
.response()
.content(
"{\"id\": 67,\"date\": \"2015-01-20\",\"assets\":"
+ " [{\"id\":356,\"name\": \"sample\"}]}")
.build());
.document(new OperationBuilder("map-response-with-fields",
this.snippet.getOutputDirectory())
.response()
.content(
"{\"id\": 67,\"date\": \"2015-01-20\",\"assets\":"
+ " [{\"id\":356,\"name\": \"sample\"}]}")
.build());
}
@Test
public void arrayResponseWithFields() throws IOException {
this.snippet.expectResponseFields("array-response-with-fields").withContents(
tableWithHeader("Path", "Type", "Description")
this.snippet.expectResponseFields("array-response-with-fields")
.withContents(tableWithHeader("Path", "Type", "Description")
.row("[]a.b", "Number", "one").row("[]a.c", "String", "two")
.row("[]a", "Object", "three"));
new ResponseFieldsSnippet(Arrays.asList(
fieldWithPath("[]a.b").description("one"), fieldWithPath("[]a.c")
.description("two"), fieldWithPath("[]a").description("three")))
.document(new OperationBuilder("array-response-with-fields", this.snippet
.getOutputDirectory()).response()
.content("[{\"a\": {\"b\": 5}},{\"a\": {\"c\": \"charlie\"}}]")
.build());
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("[]a.b").description("one"),
fieldWithPath("[]a.c").description("two"),
fieldWithPath("[]a").description("three")))
.document(new OperationBuilder("array-response-with-fields",
this.snippet.getOutputDirectory())
.response()
.content(
"[{\"a\": {\"b\": 5}},{\"a\": {\"c\": \"charlie\"}}]")
.build());
}
@Test
public void arrayResponse() throws IOException {
this.snippet.expectResponseFields("array-response")
.withContents(
tableWithHeader("Path", "Type", "Description").row("[]",
"String", "one"));
.withContents(tableWithHeader("Path", "Type", "Description").row("[]",
"String", "one"));
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("[]").description("one")))
.document(new OperationBuilder("array-response", this.snippet
.getOutputDirectory()).response()
.content("[\"a\", \"b\", \"c\"]").build());
.document(new OperationBuilder("array-response",
this.snippet.getOutputDirectory()).response()
.content("[\"a\", \"b\", \"c\"]").build());
}
@Test
public void ignoredResponseField() throws IOException {
this.snippet.expectResponseFields("ignored-response-field").withContents(
tableWithHeader("Path", "Type", "Description").row("b", "Number",
"Field b"));
this.snippet.expectResponseFields("ignored-response-field")
.withContents(tableWithHeader("Path", "Type", "Description").row("b",
"Number", "Field b"));
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a").ignored(),
fieldWithPath("b").description("Field b")))
.document(new OperationBuilder("ignored-response-field", this.snippet
.getOutputDirectory()).response().content("{\"a\": 5, \"b\": 4}")
.build());
.document(new OperationBuilder("ignored-response-field",
this.snippet.getOutputDirectory()).response()
.content("{\"a\": 5, \"b\": 4}").build());
}
@Test
public void responseFieldsWithCustomDescriptorAttributes() throws IOException {
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("response-fields")).willReturn(
snippetResource("response-fields-with-extra-column"));
given(resolver.resolveTemplateResource("response-fields"))
.willReturn(snippetResource("response-fields-with-extra-column"));
this.snippet.expectResponseFields("response-fields-with-custom-attributes")
.withContents(
tableWithHeader("Path", "Type", "Description", "Foo")
.row("a.b", "Number", "one", "alpha")
.row("a.c", "String", "two", "bravo")
.row("a", "Object", "three", "charlie"));
.withContents(tableWithHeader("Path", "Type", "Description", "Foo")
.row("a.b", "Number", "one", "alpha")
.row("a.c", "String", "two", "bravo")
.row("a", "Object", "three", "charlie"));
new ResponseFieldsSnippet(Arrays.asList(
fieldWithPath("a.b").description("one").attributes(
key("foo").value("alpha")),
fieldWithPath("a.c").description("two").attributes(
key("foo").value("bravo")),
fieldWithPath("a").description("three").attributes(
key("foo").value("charlie")))).document(new OperationBuilder(
"response-fields-with-custom-attributes", this.snippet
.getOutputDirectory())
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver)).response()
.content("{\"a\": {\"b\": 5, \"c\": \"charlie\"}}").build());
fieldWithPath("a.b").description("one")
.attributes(key("foo").value("alpha")),
fieldWithPath("a.c").description("two")
.attributes(key("foo").value("bravo")),
fieldWithPath("a").description("three")
.attributes(key("foo").value("charlie"))))
.document(new OperationBuilder(
"response-fields-with-custom-attributes",
this.snippet.getOutputDirectory())
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(
resolver))
.response()
.content(
"{\"a\": {\"b\": 5, \"c\": \"charlie\"}}")
.build());
}
@Test
public void responseFieldsWithCustomAttributes() throws IOException {
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("response-fields")).willReturn(
snippetResource("response-fields-with-title"));
given(resolver.resolveTemplateResource("response-fields"))
.willReturn(snippetResource("response-fields-with-title"));
this.snippet.expectResponseFields("response-fields-with-custom-attributes")
.withContents(startsWith(".Custom title"));
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one")),
attributes(key("title").value("Custom title")))
.document(new OperationBuilder("response-fields-with-custom-attributes",
this.snippet.getOutputDirectory())
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver)).response()
.content("{\"a\": \"foo\"}").build());
attributes(key("title").value("Custom title"))).document(
new OperationBuilder("response-fields-with-custom-attributes",
this.snippet.getOutputDirectory())
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver))
.response().content("{\"a\": \"foo\"}").build());
}
@Test
public void xmlResponseFields() throws IOException {
this.snippet.expectResponseFields("xml-response").withContents(
tableWithHeader("Path", "Type", "Description").row("a/b", "b", "one")
.row("a/c", "c", "two").row("a", "a", "three"));
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a/b").description("one")
.type("b"), fieldWithPath("a/c").description("two").type("c"),
fieldWithPath("a").description("three").type("a")))
.document(new OperationBuilder("xml-response", this.snippet
.getOutputDirectory())
.response()
.content("<a><b>5</b><c>charlie</c></a>")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE)
.build());
this.snippet.expectResponseFields("xml-response")
.withContents(tableWithHeader("Path", "Type", "Description")
.row("a/b", "b", "one").row("a/c", "c", "two")
.row("a", "a", "three"));
new ResponseFieldsSnippet(
Arrays.asList(fieldWithPath("a/b").description("one").type("b"),
fieldWithPath("a/c").description("two").type("c"),
fieldWithPath("a").description("three")
.type("a"))).document(new OperationBuilder("xml-response",
this.snippet.getOutputDirectory()).response()
.content("<a><b>5</b><c>charlie</c></a>")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_XML_VALUE)
.build());
}
@Test
public void undocumentedXmlResponseField() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown
.expectMessage(startsWith("The following parts of the payload were not"
+ " documented:"));
new ResponseFieldsSnippet(Collections.<FieldDescriptor>emptyList())
.document(new OperationBuilder("undocumented-xml-response-field",
this.snippet.getOutputDirectory())
.response()
.content("<a><b>5</b></a>")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE)
.build());
this.thrown.expectMessage(startsWith(
"The following parts of the payload were not" + " documented:"));
new ResponseFieldsSnippet(
Collections.<FieldDescriptor>emptyList())
.document(
new OperationBuilder("undocumented-xml-response-field",
this.snippet.getOutputDirectory()).response()
.content("<a><b>5</b></a>")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_XML_VALUE)
.build());
}
@Test
public void xmlAttribute() throws IOException {
this.snippet.expectResponseFields("xml-attribute").withContents(
tableWithHeader("Path", "Type", "Description").row("a", "b", "one").row(
"a/@id", "c", "two"));
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one")
.type("b"), fieldWithPath("a/@id").description("two").type("c")))
.document(new OperationBuilder("xml-attribute", this.snippet
.getOutputDirectory())
.response()
.content("<a id=\"1\">foo</a>")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE)
.build());
this.snippet.expectResponseFields("xml-attribute")
.withContents(tableWithHeader("Path", "Type", "Description")
.row("a", "b", "one").row("a/@id", "c", "two"));
new ResponseFieldsSnippet(
Arrays.asList(fieldWithPath("a").description("one").type("b"),
fieldWithPath("a/@id").description("two").type("c")))
.document(new OperationBuilder("xml-attribute",
this.snippet.getOutputDirectory()).response()
.content("<a id=\"1\">foo</a>")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_XML_VALUE)
.build());
}
@Test
public void missingXmlAttribute() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown
.expectMessage(equalTo("Fields with the following paths were not found"
+ " in the payload: [a/@id]"));
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one")
.type("b"), fieldWithPath("a/@id").description("two").type("c")))
.document(new OperationBuilder("missing-xml-attribute", this.snippet
.getOutputDirectory())
.response()
.content("<a>foo</a>")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE)
.build());
this.thrown.expectMessage(equalTo("Fields with the following paths were not found"
+ " in the payload: [a/@id]"));
new ResponseFieldsSnippet(
Arrays.asList(fieldWithPath("a").description("one").type("b"),
fieldWithPath("a/@id").description("two").type("c")))
.document(new OperationBuilder("missing-xml-attribute",
this.snippet.getOutputDirectory()).response()
.content("<a>foo</a>")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_XML_VALUE)
.build());
}
@Test
public void missingOptionalXmlAttribute() throws IOException {
this.snippet.expectResponseFields("missing-optional-xml-attribute").withContents(
tableWithHeader("Path", "Type", "Description").row("a", "b", "one").row(
"a/@id", "c", "two"));
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one")
.type("b"), fieldWithPath("a/@id").description("two").type("c")
.optional())).document(new OperationBuilder(
"missing-optional-xml-attribute", this.snippet.getOutputDirectory())
.response().content("<a>foo</a>")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE)
.build());
this.snippet.expectResponseFields("missing-optional-xml-attribute")
.withContents(tableWithHeader("Path", "Type", "Description")
.row("a", "b", "one").row("a/@id", "c", "two"));
new ResponseFieldsSnippet(Arrays.asList(
fieldWithPath("a").description("one").type("b"),
fieldWithPath("a/@id").description("two").type("c").optional()))
.document(
new OperationBuilder("missing-optional-xml-attribute",
this.snippet.getOutputDirectory()).response()
.content("<a>foo</a>")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_XML_VALUE)
.build());
}
@Test
public void undocumentedAttributeDoesNotCauseFailure() throws IOException {
this.snippet.expectResponseFields("undocumented-attribute").withContents(
tableWithHeader("Path", "Type", "Description").row("a", "a", "one"));
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one")
.type("a"))).document(new OperationBuilder("undocumented-attribute",
this.snippet.getOutputDirectory()).response()
.content("<a id=\"foo\">bar</a>")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE)
.build());
new ResponseFieldsSnippet(
Arrays.asList(fieldWithPath("a").description("one").type("a")))
.document(
new OperationBuilder("undocumented-attribute",
this.snippet.getOutputDirectory()).response()
.content("<a id=\"foo\">bar</a>")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_XML_VALUE)
.build());
}
@Test
public void documentedXmlAttributesAreRemoved() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(equalTo(String
.format("The following parts of the payload were not documented:"
this.thrown.expectMessage(equalTo(
String.format("The following parts of the payload were not documented:"
+ "%n<a>bar</a>%n")));
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a/@id").description("one")
.type("a"))).document(new OperationBuilder(
"documented-attribute-is-removed", this.snippet.getOutputDirectory())
.response().content("<a id=\"foo\">bar</a>")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE)
.build());
new ResponseFieldsSnippet(
Arrays.asList(
fieldWithPath("a/@id").description("one")
.type("a"))).document(new OperationBuilder(
"documented-attribute-is-removed",
this.snippet.getOutputDirectory()).response()
.content("<a id=\"foo\">bar</a>")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_XML_VALUE)
.build());
}
@Test
public void xmlResponseFieldWithNoType() throws IOException {
this.thrown.expect(FieldTypeRequiredException.class);
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one")))
.document(new OperationBuilder("xml-response-no-field-type", this.snippet
.getOutputDirectory())
.response()
.content("<a>5</a>")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE)
.build());
.document(new OperationBuilder("xml-response-no-field-type",
this.snippet.getOutputDirectory()).response().content("<a>5</a>")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_XML_VALUE)
.build());
}
@Test
public void missingXmlResponseField() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown
.expectMessage(equalTo("Fields with the following paths were not found"
+ " in the payload: [a/b]"));
this.thrown.expectMessage(equalTo("Fields with the following paths were not found"
+ " in the payload: [a/b]"));
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a/b").description("one"),
fieldWithPath("a").description("one"))).document(new OperationBuilder(
"missing-xml-response-field", this.snippet.getOutputDirectory())
.response().content("<a></a>")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE)
.build());
fieldWithPath("a").description("one")))
.document(
new OperationBuilder("missing-xml-response-field",
this.snippet.getOutputDirectory()).response()
.content("<a></a>")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_XML_VALUE)
.build());
}
@Test
public void undocumentedXmlResponseFieldAndMissingXmlResponseField()
throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown
.expectMessage(startsWith("The following parts of the payload were not"
+ " documented:"));
this.thrown.expectMessage(startsWith(
"The following parts of the payload were not" + " documented:"));
this.thrown
.expectMessage(endsWith("Fields with the following paths were not found"
+ " in the payload: [a/b]"));
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a/b").description("one")))
.document(new OperationBuilder(
"undocumented-xml-request-field-and-missing-xml-request-field",
this.snippet.getOutputDirectory())
.response()
.content("<a><c>5</c></a>")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE)
.build());
new ResponseFieldsSnippet(
Arrays.asList(fieldWithPath("a/b").description("one")))
.document(
new OperationBuilder(
"undocumented-xml-request-field-and-missing-xml-request-field",
this.snippet.getOutputDirectory()).response()
.content("<a><c>5</c></a>")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_XML_VALUE)
.build());
}
private FileSystemResource snippetResource(String name) {
return new FileSystemResource("src/test/resources/custom-snippet-templates/"
+ name + ".snippet");
return new FileSystemResource(
"src/test/resources/custom-snippet-templates/" + name + ".snippet");
}
}

View File

@@ -23,6 +23,7 @@ import java.util.Collections;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.core.io.FileSystemResource;
import org.springframework.restdocs.snippet.SnippetException;
import org.springframework.restdocs.templates.TemplateEngine;
@@ -62,8 +63,10 @@ public class PathParametersSnippetTests {
+ " not documented: [a]"));
new PathParametersSnippet(Collections.<ParameterDescriptor>emptyList())
.document(new OperationBuilder("undocumented-path-parameter",
this.snippet.getOutputDirectory()).attribute(
"org.springframework.restdocs.urlTemplate", "/{a}/").build());
this.snippet.getOutputDirectory())
.attribute("org.springframework.restdocs.urlTemplate",
"/{a}/")
.build());
}
@Test
@@ -73,9 +76,10 @@ public class PathParametersSnippetTests {
+ " not found in the request: [a]"));
new PathParametersSnippet(
Arrays.asList(parameterWithName("a").description("one")))
.document(new OperationBuilder("missing-path-parameter", this.snippet
.getOutputDirectory()).attribute(
"org.springframework.restdocs.urlTemplate", "/").build());
.document(new OperationBuilder("missing-path-parameter",
this.snippet.getOutputDirectory()).attribute(
"org.springframework.restdocs.urlTemplate", "/")
.build());
}
@Test
@@ -85,23 +89,26 @@ public class PathParametersSnippetTests {
+ " not documented: [b]. Path parameters with the following"
+ " names were not found in the request: [a]"));
new PathParametersSnippet(
Arrays.asList(parameterWithName("a").description("one")))
.document(new OperationBuilder(
"undocumented-and-missing-path-parameters", this.snippet
.getOutputDirectory()).attribute(
"org.springframework.restdocs.urlTemplate", "/{b}").build());
Arrays.asList(parameterWithName("a").description("one"))).document(
new OperationBuilder("undocumented-and-missing-path-parameters",
this.snippet.getOutputDirectory())
.attribute(
"org.springframework.restdocs.urlTemplate",
"/{b}")
.build());
}
@Test
public void pathParameters() throws IOException {
this.snippet.expectPathParameters("path-parameters").withContents(
tableWithTitleAndHeader("/{a}/{b}", "Parameter", "Description").row("a",
"one").row("b", "two"));
new PathParametersSnippet(Arrays.asList(
parameterWithName("a").description("one"), parameterWithName("b")
.description("two"))).document(new OperationBuilder(
"path-parameters", this.snippet.getOutputDirectory()).attribute(
"org.springframework.restdocs.urlTemplate", "/{a}/{b}").build());
tableWithTitleAndHeader("/{a}/{b}", "Parameter", "Description")
.row("a", "one").row("b", "two"));
new PathParametersSnippet(Arrays.asList(parameterWithName("a").description("one"),
parameterWithName("b").description("two"))).document(new OperationBuilder(
"path-parameters", this.snippet.getOutputDirectory())
.attribute("org.springframework.restdocs.urlTemplate",
"/{a}/{b}")
.build());
}
@Test
@@ -110,10 +117,11 @@ public class PathParametersSnippetTests {
tableWithTitleAndHeader("/{a}/{b}", "Parameter", "Description").row("b",
"two"));
new PathParametersSnippet(Arrays.asList(parameterWithName("a").ignored(),
parameterWithName("b").description("two")))
.document(new OperationBuilder("ignored-path-parameter", this.snippet
.getOutputDirectory()).attribute(
"org.springframework.restdocs.urlTemplate", "/{a}/{b}").build());
parameterWithName("b").description("two"))).document(new OperationBuilder(
"ignored-path-parameter", this.snippet.getOutputDirectory())
.attribute("org.springframework.restdocs.urlTemplate",
"/{a}/{b}")
.build());
}
@Test
@@ -122,59 +130,69 @@ public class PathParametersSnippetTests {
.withContents(
tableWithTitleAndHeader("/{a}/{b}", "Parameter", "Description")
.row("a", "one").row("b", "two"));
new PathParametersSnippet(Arrays.asList(
parameterWithName("a").description("one"), parameterWithName("b")
.description("two")))
.document(new OperationBuilder("path-parameters-with-query-string",
this.snippet.getOutputDirectory()).attribute(
"org.springframework.restdocs.urlTemplate", "/{a}/{b}?foo=bar")
.build());
new PathParametersSnippet(Arrays.asList(parameterWithName("a").description("one"),
parameterWithName("b").description("two"))).document(
new OperationBuilder("path-parameters-with-query-string",
this.snippet.getOutputDirectory())
.attribute(
"org.springframework.restdocs.urlTemplate",
"/{a}/{b}?foo=bar")
.build());
}
@Test
public void pathParametersWithCustomDescriptorAttributes() throws IOException {
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("path-parameters")).willReturn(
snippetResource("path-parameters-with-extra-column"));
this.snippet.expectPathParameters(
"path-parameters-with-custom-descriptor-attributes").withContents(
tableWithHeader("Parameter", "Description", "Foo").row("a", "one",
"alpha").row("b", "two", "bravo"));
given(resolver.resolveTemplateResource("path-parameters"))
.willReturn(snippetResource("path-parameters-with-extra-column"));
this.snippet
.expectPathParameters("path-parameters-with-custom-descriptor-attributes")
.withContents(tableWithHeader("Parameter", "Description", "Foo")
.row("a", "one", "alpha").row("b", "two", "bravo"));
new PathParametersSnippet(Arrays.asList(parameterWithName("a").description("one")
.attributes(key("foo").value("alpha")), parameterWithName("b")
.description("two").attributes(key("foo").value("bravo"))))
.document(new OperationBuilder(
"path-parameters-with-custom-descriptor-attributes", this.snippet
.getOutputDirectory())
.attribute("org.springframework.restdocs.urlTemplate", "/{a}/{b}")
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver)).build());
new PathParametersSnippet(Arrays.asList(
parameterWithName("a").description("one")
.attributes(key("foo").value("alpha")),
parameterWithName("b").description("two").attributes(
key("foo").value("bravo")))).document(new OperationBuilder(
"path-parameters-with-custom-descriptor-attributes",
this.snippet.getOutputDirectory()).attribute(
"org.springframework.restdocs.urlTemplate",
"/{a}/{b}")
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver))
.build());
}
@Test
public void pathParametersWithCustomAttributes() throws IOException {
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("path-parameters")).willReturn(
snippetResource("path-parameters-with-title"));
given(resolver.resolveTemplateResource("path-parameters"))
.willReturn(snippetResource("path-parameters-with-title"));
this.snippet.expectPathParameters("path-parameters-with-custom-attributes")
.withContents(startsWith(".The title"));
new PathParametersSnippet(Arrays.asList(parameterWithName("a").description("one")
.attributes(key("foo").value("alpha")), parameterWithName("b")
.description("two").attributes(key("foo").value("bravo"))),
attributes(key("title").value("The title")))
.document(new OperationBuilder("path-parameters-with-custom-attributes",
this.snippet.getOutputDirectory())
.attribute("org.springframework.restdocs.urlTemplate", "/{a}/{b}")
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver)).build());
new PathParametersSnippet(
Arrays.asList(
parameterWithName("a").description("one")
.attributes(key("foo").value("alpha")),
parameterWithName("b").description("two")
.attributes(key("foo").value("bravo"))),
attributes(key("title").value("The title"))).document(
new OperationBuilder("path-parameters-with-custom-attributes",
this.snippet.getOutputDirectory())
.attribute(
"org.springframework.restdocs.urlTemplate",
"/{a}/{b}")
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver))
.build());
}
private FileSystemResource snippetResource(String name) {
return new FileSystemResource("src/test/resources/custom-snippet-templates/"
+ name + ".snippet");
return new FileSystemResource(
"src/test/resources/custom-snippet-templates/" + name + ".snippet");
}
}

View File

@@ -23,6 +23,7 @@ import java.util.Collections;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.core.io.FileSystemResource;
import org.springframework.restdocs.snippet.SnippetException;
import org.springframework.restdocs.templates.TemplateEngine;
@@ -60,9 +61,9 @@ public class RequestParametersSnippetTests {
.expectMessage(equalTo("Request parameters with the following names were"
+ " not documented: [a]"));
new RequestParametersSnippet(Collections.<ParameterDescriptor>emptyList())
.document(new OperationBuilder("undocumented-parameter", this.snippet
.getOutputDirectory()).request("http://localhost")
.param("a", "alpha").build());
.document(new OperationBuilder("undocumented-parameter",
this.snippet.getOutputDirectory()).request("http://localhost")
.param("a", "alpha").build());
}
@Test
@@ -71,9 +72,11 @@ public class RequestParametersSnippetTests {
this.thrown
.expectMessage(equalTo("Request parameters with the following names were"
+ " not found in the request: [a]"));
new RequestParametersSnippet(Arrays.asList(parameterWithName("a").description(
"one"))).document(new OperationBuilder("missing-parameter", this.snippet
.getOutputDirectory()).request("http://localhost").build());
new RequestParametersSnippet(
Arrays.asList(parameterWithName("a").description("one")))
.document(new OperationBuilder("missing-parameter",
this.snippet.getOutputDirectory())
.request("http://localhost").build());
}
@Test
@@ -83,23 +86,27 @@ public class RequestParametersSnippetTests {
.expectMessage(equalTo("Request parameters with the following names were"
+ " not documented: [b]. Request parameters with the following"
+ " names were not found in the request: [a]"));
new RequestParametersSnippet(Arrays.asList(parameterWithName("a").description(
"one"))).document(new OperationBuilder(
"undocumented-and-missing-parameters", this.snippet.getOutputDirectory())
.request("http://localhost").param("b", "bravo").build());
new RequestParametersSnippet(
Arrays.asList(parameterWithName("a").description("one"))).document(
new OperationBuilder("undocumented-and-missing-parameters",
this.snippet.getOutputDirectory())
.request("http://localhost").param("b", "bravo")
.build());
}
@Test
public void requestParameters() throws IOException {
this.snippet.expectRequestParameters("request-parameters").withContents(
tableWithHeader("Parameter", "Description").row("a", "one").row("b",
"two"));
new RequestParametersSnippet(Arrays.asList(
parameterWithName("a").description("one"), parameterWithName("b")
.description("two"))).document(new OperationBuilder(
"request-parameters", this.snippet.getOutputDirectory())
.request("http://localhost").param("a", "bravo").param("b", "bravo")
.build());
this.snippet.expectRequestParameters("request-parameters")
.withContents(tableWithHeader("Parameter", "Description").row("a", "one")
.row("b", "two"));
new RequestParametersSnippet(
Arrays.asList(parameterWithName("a").description("one"),
parameterWithName("b").description("two")))
.document(new OperationBuilder("request-parameters",
this.snippet.getOutputDirectory())
.request("http://localhost")
.param("a", "bravo").param("b", "bravo")
.build());
}
@Test
@@ -108,57 +115,65 @@ public class RequestParametersSnippetTests {
tableWithHeader("Parameter", "Description").row("b", "two"));
new RequestParametersSnippet(Arrays.asList(parameterWithName("a").ignored(),
parameterWithName("b").description("two")))
.document(new OperationBuilder("ignored-request-parameter", this.snippet
.getOutputDirectory()).request("http://localhost")
.param("a", "bravo").param("b", "bravo").build());
.document(new OperationBuilder("ignored-request-parameter",
this.snippet.getOutputDirectory())
.request("http://localhost").param("a", "bravo")
.param("b", "bravo").build());
}
@Test
public void requestParametersWithCustomDescriptorAttributes() throws IOException {
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("request-parameters")).willReturn(
snippetResource("request-parameters-with-extra-column"));
this.snippet.expectRequestParameters(
"request-parameters-with-custom-descriptor-attributes").withContents(
tableWithHeader("Parameter", "Description", "Foo").row("a", "one",
"alpha").row("b", "two", "bravo"));
given(resolver.resolveTemplateResource("request-parameters"))
.willReturn(snippetResource("request-parameters-with-extra-column"));
this.snippet
.expectRequestParameters(
"request-parameters-with-custom-descriptor-attributes")
.withContents(tableWithHeader("Parameter", "Description", "Foo")
.row("a", "one", "alpha").row("b", "two", "bravo"));
new RequestParametersSnippet(Arrays.asList(
parameterWithName("a").description("one").attributes(
key("foo").value("alpha")),
parameterWithName("b").description("two").attributes(
key("foo").value("bravo")))).document(new OperationBuilder(
"request-parameters-with-custom-descriptor-attributes", this.snippet
.getOutputDirectory())
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver)).request("http://localhost")
.param("a", "alpha").param("b", "bravo").build());
parameterWithName("a").description("one")
.attributes(key("foo").value("alpha")),
parameterWithName("b").description("two")
.attributes(key("foo").value("bravo"))))
.document(new OperationBuilder(
"request-parameters-with-custom-descriptor-attributes",
this.snippet.getOutputDirectory())
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(
resolver))
.request("http://localhost")
.param("a", "alpha").param("b", "bravo")
.build());
}
@Test
public void requestParametersWithCustomAttributes() throws IOException {
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("request-parameters")).willReturn(
snippetResource("request-parameters-with-title"));
given(resolver.resolveTemplateResource("request-parameters"))
.willReturn(snippetResource("request-parameters-with-title"));
this.snippet.expectRequestParameters("request-parameters-with-custom-attributes")
.withContents(startsWith(".The title"));
new RequestParametersSnippet(Arrays.asList(
parameterWithName("a").description("one").attributes(
key("foo").value("alpha")),
parameterWithName("b").description("two").attributes(
key("foo").value("bravo"))), attributes(key("title").value(
"The title"))).document(new OperationBuilder(
"request-parameters-with-custom-attributes", this.snippet
.getOutputDirectory())
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver)).request("http://localhost")
.param("a", "alpha").param("b", "bravo").build());
new RequestParametersSnippet(
Arrays.asList(
parameterWithName("a").description("one")
.attributes(key("foo").value("alpha")),
parameterWithName("b").description("two")
.attributes(key("foo").value("bravo"))),
attributes(key("title").value("The title"))).document(
new OperationBuilder("request-parameters-with-custom-attributes",
this.snippet.getOutputDirectory())
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver))
.request("http://localhost").param("a", "alpha")
.param("b", "bravo").build());
}
private FileSystemResource snippetResource(String name) {
return new FileSystemResource("src/test/resources/custom-snippet-templates/"
+ name + ".snippet");
return new FileSystemResource(
"src/test/resources/custom-snippet-templates/" + name + ".snippet");
}
}

View File

@@ -17,6 +17,7 @@
package org.springframework.restdocs.snippet;
import org.junit.Test;
import org.springframework.restdocs.RestDocumentationContext;
import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
@@ -33,22 +34,19 @@ public class RestDocumentationContextPlaceholderResolverTests {
@Test
public void kebabCaseMethodName() throws Exception {
assertThat(
createResolver("dashSeparatedMethodName").resolvePlaceholder(
"method-name"), equalTo("dash-separated-method-name"));
assertThat(createResolver("dashSeparatedMethodName").resolvePlaceholder(
"method-name"), equalTo("dash-separated-method-name"));
}
@Test
public void snakeCaseMethodName() throws Exception {
assertThat(
createResolver("underscoreSeparatedMethodName").resolvePlaceholder(
"method_name"), equalTo("underscore_separated_method_name"));
assertThat(createResolver("underscoreSeparatedMethodName").resolvePlaceholder(
"method_name"), equalTo("underscore_separated_method_name"));
}
@Test
public void camelCaseMethodName() throws Exception {
assertThat(
createResolver("camelCaseMethodName").resolvePlaceholder("methodName"),
assertThat(createResolver("camelCaseMethodName").resolvePlaceholder("methodName"),
equalTo("camelCaseMethodName"));
}

View File

@@ -19,6 +19,7 @@ package org.springframework.restdocs.snippet;
import java.io.File;
import org.junit.Test;
import org.springframework.restdocs.RestDocumentationContext;
import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
@@ -34,7 +35,8 @@ import static org.mockito.Mockito.mock;
*/
public class StandardWriterResolverTests {
private final PlaceholderResolver placeholderResolver = mock(PlaceholderResolver.class);
private final PlaceholderResolver placeholderResolver = mock(
PlaceholderResolver.class);
private final StandardWriterResolver resolver = new StandardWriterResolver(
this.placeholderResolver);
@@ -48,26 +50,29 @@ public class StandardWriterResolverTests {
@Test
public void absoluteInput() {
String absolutePath = new File("foo").getAbsolutePath();
assertThat(this.resolver.resolveFile(absolutePath, "bar.txt",
new RestDocumentationContext(null, null, null)), is(new File(
absolutePath, "bar.txt")));
assertThat(
this.resolver.resolveFile(absolutePath, "bar.txt",
new RestDocumentationContext(null, null, null)),
is(new File(absolutePath, "bar.txt")));
}
@Test
public void configuredOutputAndRelativeInput() {
File outputDir = new File("foo").getAbsoluteFile();
assertThat(this.resolver.resolveFile("bar", "baz.txt",
new RestDocumentationContext(null, null, outputDir)), is(new File(
outputDir, "bar/baz.txt")));
assertThat(
this.resolver.resolveFile("bar", "baz.txt",
new RestDocumentationContext(null, null, outputDir)),
is(new File(outputDir, "bar/baz.txt")));
}
@Test
public void configuredOutputAndAbsoluteInput() {
File outputDir = new File("foo").getAbsoluteFile();
String absolutePath = new File("bar").getAbsolutePath();
assertThat(this.resolver.resolveFile(absolutePath, "baz.txt",
new RestDocumentationContext(null, null, outputDir)), is(new File(
absolutePath, "baz.txt")));
assertThat(
this.resolver.resolveFile(absolutePath, "baz.txt",
new RestDocumentationContext(null, null, outputDir)),
is(new File(absolutePath, "baz.txt")));
}
}

View File

@@ -21,6 +21,7 @@ import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import org.springframework.restdocs.operation.Operation;
import static org.hamcrest.CoreMatchers.equalTo;
@@ -55,10 +56,8 @@ public class TemplatedSnippetTests {
@Test
public void snippetName() {
assertThat(
new TestTemplatedSnippet(Collections.<String, Object>emptyMap())
.getSnippetName(),
is(equalTo("test")));
assertThat(new TestTemplatedSnippet(Collections.<String, Object>emptyMap())
.getSnippetName(), is(equalTo("test")));
}
private static class TestTemplatedSnippet extends TemplatedSnippet {

View File

@@ -24,6 +24,7 @@ import java.util.concurrent.Callable;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.core.io.Resource;
import static org.hamcrest.CoreMatchers.equalTo;
@@ -47,8 +48,8 @@ public class StandardTemplateResourceResolverTests {
@Test
public void customSnippetResolution() throws Exception {
this.classLoader.addResource(
"org/springframework/restdocs/templates/test.snippet", getClass()
.getResource("test.snippet"));
"org/springframework/restdocs/templates/test.snippet",
getClass().getResource("test.snippet"));
Resource snippet = doWithThreadContextClassLoader(this.classLoader,
new Callable<Resource>() {
@@ -66,8 +67,8 @@ public class StandardTemplateResourceResolverTests {
@Test
public void fallsBackToDefaultSnippet() throws Exception {
this.classLoader.addResource(
"org/springframework/restdocs/templates/default-test.snippet", getClass()
.getResource("test.snippet"));
"org/springframework/restdocs/templates/default-test.snippet",
getClass().getResource("test.snippet"));
Resource snippet = doWithThreadContextClassLoader(this.classLoader,
new Callable<Resource>() {

View File

@@ -23,6 +23,7 @@ import org.hamcrest.Matcher;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.springframework.restdocs.snippet.TemplatedSnippet;
import org.springframework.restdocs.test.SnippetMatchers.SnippetMatcher;
@@ -48,8 +49,8 @@ public class ExpectedSnippet implements TestRule {
@Override
public Statement apply(final Statement base, Description description) {
this.outputDirectory = new File("build/"
+ description.getTestClass().getSimpleName());
this.outputDirectory = new File(
"build/" + description.getTestClass().getSimpleName());
return new ExpectedSnippetStatement(base);
}

View File

@@ -90,9 +90,9 @@ public class OperationBuilder {
this.attributes.put(WriterResolver.class.getName(), new StandardWriterResolver(
new RestDocumentationContextPlaceholderResolver(context)));
return new StandardOperation(this.name,
(this.requestBuilder == null ? new OperationRequestBuilder(
"http://localhost/").buildRequest() : this.requestBuilder
.buildRequest()),
(this.requestBuilder == null
? new OperationRequestBuilder("http://localhost/").buildRequest()
: this.requestBuilder.buildRequest()),
this.responseBuilder.buildResponse(), this.attributes);
}
@@ -182,7 +182,8 @@ public class OperationBuilder {
this.content = content;
}
public OperationRequestPartBuilder submittedFileName(String submittedFileName) {
public OperationRequestPartBuilder submittedFileName(
String submittedFileName) {
this.submittedFileName = submittedFileName;
return this;
}

View File

@@ -29,6 +29,7 @@ import java.util.List;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.springframework.http.HttpStatus;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils;
@@ -71,8 +72,8 @@ public final class SnippetMatchers {
return new AsciidoctorCodeBlockMatcher(language);
}
private static abstract class AbstractSnippetContentMatcher extends
BaseMatcher<String> {
private static abstract class AbstractSnippetContentMatcher
extends BaseMatcher<String> {
private List<String> lines = new ArrayList<>();
@@ -149,8 +150,8 @@ public final class SnippetMatchers {
*
* @param <T> The type of the matcher
*/
public static abstract class HttpMatcher<T extends HttpMatcher<T>> extends
AsciidoctorCodeBlockMatcher<HttpMatcher<T>> {
public static abstract class HttpMatcher<T extends HttpMatcher<T>>
extends AsciidoctorCodeBlockMatcher<HttpMatcher<T>> {
private int headerOffset = 3;
@@ -175,8 +176,8 @@ public final class SnippetMatchers {
/**
* A {@link Matcher} for an HTTP response.
*/
public static final class HttpResponseMatcher extends
HttpMatcher<HttpResponseMatcher> {
public static final class HttpResponseMatcher
extends HttpMatcher<HttpResponseMatcher> {
private HttpResponseMatcher(HttpStatus status) {
this.content("HTTP/1.1 " + status.value() + " " + status.getReasonPhrase());
@@ -200,17 +201,16 @@ public final class SnippetMatchers {
/**
* A {@link Matcher} for an Asciidoctor table.
*/
public static final class AsciidoctorTableMatcher extends
AbstractSnippetContentMatcher {
public static final class AsciidoctorTableMatcher
extends AbstractSnippetContentMatcher {
private AsciidoctorTableMatcher(String title, String... columns) {
if (StringUtils.hasText(title)) {
this.addLine("." + title);
}
this.addLine("|===");
String header = "|"
+ StringUtils
.collectionToDelimitedString(Arrays.asList(columns), "|");
String header = "|" + StringUtils
.collectionToDelimitedString(Arrays.asList(columns), "|");
this.addLine(header);
this.addLine("");
this.addLine("|===");
@@ -258,8 +258,8 @@ public final class SnippetMatchers {
}
private String read(File snippetFile) throws IOException {
return FileCopyUtils.copyToString(new InputStreamReader(new FileInputStream(
snippetFile), "UTF-8"));
return FileCopyUtils.copyToString(
new InputStreamReader(new FileInputStream(snippetFile), "UTF-8"));
}
@Override
@@ -269,12 +269,12 @@ public final class SnippetMatchers {
}
else if (this.expectedContents != null) {
try {
this.expectedContents
.describeMismatch(read((File) item), description);
this.expectedContents.describeMismatch(read((File) item),
description);
}
catch (IOException e) {
description.appendText("The contents of " + item
+ " cound not be read");
description
.appendText("The contents of " + item + " cound not be read");
}
}
}