Configure checkstyle and update code to comply

This commit configures Checkstyle and updates the build to comply with
that configuration. The Eclipse JDT metadata has also been updated
so that the output of Eclipse’s code formatting and clean-up is
compliant with Checkstyle.

The use of the latest version (6.10.1) of Checkstyle has required an
upgrade to Gradle 2.7. Gradle hardcode’s the name of the Checkstyle’s
main class which has changed between version 5 (Gradle’s default) and
version 6.

Closes gh-119
This commit is contained in:
Andy Wilkinson
2015-09-23 11:36:01 +01:00
parent ccb5f3d191
commit 130a230105
150 changed files with 1456 additions and 1514 deletions

View File

@@ -16,10 +16,6 @@
package org.springframework.restdocs.constraints;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertThat;
import java.math.BigDecimal;
import java.util.Date;
@@ -39,8 +35,12 @@ import javax.validation.constraints.Size;
import org.junit.Test;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertThat;
/**
* Tests for {@link ConstraintDescriptions}
* Tests for {@link ConstraintDescriptions}.
*
* @author Andy Wilkinson
*/

View File

@@ -16,10 +16,6 @@
package org.springframework.restdocs.constraints;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
@@ -43,8 +39,12 @@ import javax.validation.constraints.Size;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
/**
* Tests for {@link ResourceBundleConstraintDescriptionResolver}
* Tests for {@link ResourceBundleConstraintDescriptionResolver}.
*
* @author Andy Wilkinson
*/
@@ -55,14 +55,14 @@ public class ResourceBundleConstraintDescriptionResolverTests {
@Test
public void defaultMessageAssertFalse() {
String description = this.resolver.resolveDescription(new Constraint(
AssertFalse.class.getName(), Collections.<String, Object> emptyMap()));
AssertFalse.class.getName(), Collections.<String, Object>emptyMap()));
assertThat(description, is(equalTo("Must be false")));
}
@Test
public void defaultMessageAssertTrue() {
String description = this.resolver.resolveDescription(new Constraint(
AssertTrue.class.getName(), Collections.<String, Object> emptyMap()));
AssertTrue.class.getName(), Collections.<String, Object>emptyMap()));
assertThat(description, is(equalTo("Must be true")));
}
@@ -98,7 +98,7 @@ public class ResourceBundleConstraintDescriptionResolverTests {
@Test
public void defaultMessageFuture() {
String description = this.resolver.resolveDescription(new Constraint(Future.class
.getName(), Collections.<String, Object> emptyMap()));
.getName(), Collections.<String, Object>emptyMap()));
assertThat(description, is(equalTo("Must be in the future")));
}
@@ -123,21 +123,21 @@ public class ResourceBundleConstraintDescriptionResolverTests {
@Test
public void defaultMessageNotNull() {
String description = this.resolver.resolveDescription(new Constraint(
NotNull.class.getName(), Collections.<String, Object> emptyMap()));
NotNull.class.getName(), Collections.<String, Object>emptyMap()));
assertThat(description, is(equalTo("Must not be null")));
}
@Test
public void defaultMessageNull() {
String description = this.resolver.resolveDescription(new Constraint(Null.class
.getName(), Collections.<String, Object> emptyMap()));
.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()));
.getName(), Collections.<String, Object>emptyMap()));
assertThat(description, is(equalTo("Must be in the past")));
}
@@ -179,7 +179,7 @@ public class ResourceBundleConstraintDescriptionResolverTests {
try {
String description = new ResourceBundleConstraintDescriptionResolver()
.resolveDescription(new Constraint(NotNull.class.getName(),
Collections.<String, Object> emptyMap()));
Collections.<String, Object>emptyMap()));
assertThat(description, is(equalTo("Should not be null")));
}
@@ -201,7 +201,7 @@ public class ResourceBundleConstraintDescriptionResolverTests {
};
String description = new ResourceBundleConstraintDescriptionResolver(bundle)
.resolveDescription(new Constraint(NotNull.class.getName(), Collections
.<String, Object> emptyMap()));
.<String, Object>emptyMap()));
assertThat(description, is(equalTo("Not null")));
}

View File

@@ -16,11 +16,6 @@
package org.springframework.restdocs.constraints;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertThat;
import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
@@ -43,8 +38,13 @@ import org.hibernate.validator.constraints.ConstraintComposition;
import org.hibernate.validator.constraints.NotBlank;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertThat;
/**
* Tests for {@link ValidatorConstraintResolver}
* Tests for {@link ValidatorConstraintResolver}.
*
* @author Andy Wilkinson
*/
@@ -86,6 +86,10 @@ public class ValidatorConstraintResolverTests {
assertThat(constraints, hasSize(1));
}
private ConstraintMatcher constraint(final Class<? extends Annotation> annotation) {
return new ConstraintMatcher(annotation);
}
private static class ConstrainedFields {
@NotNull
@@ -108,7 +112,7 @@ public class ValidatorConstraintResolverTests {
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@javax.validation.Constraint(validatedBy = {})
public @interface CompositeConstraint {
private @interface CompositeConstraint {
String message() default "Must be null or not blank";
@@ -118,11 +122,7 @@ public class ValidatorConstraintResolverTests {
}
private ConstraintMatcher constraint(final Class<? extends Annotation> annotation) {
return new ConstraintMatcher(annotation);
}
private static class ConstraintMatcher extends BaseMatcher<Constraint> {
private static final class ConstraintMatcher extends BaseMatcher<Constraint> {
private final Class<?> annotation;

View File

@@ -16,13 +16,6 @@
package org.springframework.restdocs.curl;
import static org.hamcrest.CoreMatchers.containsString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.springframework.restdocs.snippet.Attributes.attributes;
import static org.springframework.restdocs.snippet.Attributes.key;
import static org.springframework.restdocs.test.SnippetMatchers.codeBlock;
import java.io.IOException;
import org.junit.Rule;
@@ -38,8 +31,15 @@ import org.springframework.restdocs.test.ExpectedSnippet;
import org.springframework.restdocs.test.OperationBuilder;
import org.springframework.util.Base64Utils;
import static org.hamcrest.CoreMatchers.containsString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.springframework.restdocs.snippet.Attributes.attributes;
import static org.springframework.restdocs.snippet.Attributes.key;
import static org.springframework.restdocs.test.SnippetMatchers.codeBlock;
/**
* Tests for {@link CurlRequestSnippet}
* Tests for {@link CurlRequestSnippet}.
*
* @author Andy Wilkinson
* @author Yann Le Guern
@@ -247,8 +247,8 @@ public class CurlRequestSnippetTests {
this.snippet.expectCurlRequest("custom-attributes").withContents(
containsString("curl request title"));
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
when(resolver.resolveTemplateResource("curl-request"))
.thenReturn(
given(resolver.resolveTemplateResource("curl-request"))
.willReturn(
new FileSystemResource(
"src/test/resources/custom-snippet-templates/curl-request-with-title.snippet"));
new CurlRequestSnippet(attributes(key("title").value("curl request title")))

View File

@@ -16,16 +16,6 @@
package org.springframework.restdocs.http;
import static org.hamcrest.CoreMatchers.containsString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.springframework.restdocs.snippet.Attributes.attributes;
import static org.springframework.restdocs.snippet.Attributes.key;
import static org.springframework.restdocs.test.SnippetMatchers.httpRequest;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;
import static org.springframework.web.bind.annotation.RequestMethod.PUT;
import java.io.IOException;
import org.junit.Rule;
@@ -38,10 +28,18 @@ import org.springframework.restdocs.templates.TemplateResourceResolver;
import org.springframework.restdocs.templates.mustache.MustacheTemplateEngine;
import org.springframework.restdocs.test.ExpectedSnippet;
import org.springframework.restdocs.test.OperationBuilder;
import org.springframework.web.bind.annotation.RequestMethod;
import static org.hamcrest.CoreMatchers.containsString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.springframework.restdocs.snippet.Attributes.attributes;
import static org.springframework.restdocs.snippet.Attributes.key;
import static org.springframework.restdocs.test.SnippetMatchers.httpRequest;
/**
* Tests for {@link HttpRequestSnippet}
*
* Tests for {@link HttpRequestSnippet}.
*
* @author Andy Wilkinson
* @author Jonathan Pearlin
*
@@ -56,8 +54,8 @@ public class HttpRequestSnippetTests {
@Test
public void getRequest() throws IOException {
this.snippet.expectHttpRequest("get-request").withContents(
httpRequest(GET, "/foo").header(HttpHeaders.HOST, "localhost").header(
"Alpha", "a"));
httpRequest(RequestMethod.GET, "/foo").header(HttpHeaders.HOST,
"localhost").header("Alpha", "a"));
new HttpRequestSnippet().document(new OperationBuilder("get-request",
this.snippet.getOutputDirectory()).request("http://localhost/foo")
@@ -67,7 +65,8 @@ public class HttpRequestSnippetTests {
@Test
public void getRequestWithQueryString() throws IOException {
this.snippet.expectHttpRequest("get-request-with-query-string").withContents(
httpRequest(GET, "/foo?bar=baz").header(HttpHeaders.HOST, "localhost"));
httpRequest(RequestMethod.GET, "/foo?bar=baz").header(HttpHeaders.HOST,
"localhost"));
new HttpRequestSnippet().document(new OperationBuilder(
"get-request-with-query-string", this.snippet.getOutputDirectory())
@@ -77,8 +76,8 @@ public class HttpRequestSnippetTests {
@Test
public void postRequestWithContent() throws IOException {
this.snippet.expectHttpRequest("post-request-with-content").withContents(
httpRequest(POST, "/foo").header(HttpHeaders.HOST, "localhost").content(
"Hello, world"));
httpRequest(RequestMethod.POST, "/foo").header(HttpHeaders.HOST,
"localhost").content("Hello, world"));
new HttpRequestSnippet().document(new OperationBuilder(
"post-request-with-content", this.snippet.getOutputDirectory())
@@ -89,7 +88,8 @@ public class HttpRequestSnippetTests {
@Test
public void postRequestWithParameter() throws IOException {
this.snippet.expectHttpRequest("post-request-with-parameter").withContents(
httpRequest(POST, "/foo").header(HttpHeaders.HOST, "localhost")
httpRequest(RequestMethod.POST, "/foo")
.header(HttpHeaders.HOST, "localhost")
.header("Content-Type", "application/x-www-form-urlencoded")
.content("b%26r=baz&a=alpha"));
@@ -102,8 +102,8 @@ public class HttpRequestSnippetTests {
@Test
public void putRequestWithContent() throws IOException {
this.snippet.expectHttpRequest("put-request-with-content").withContents(
httpRequest(PUT, "/foo").header(HttpHeaders.HOST, "localhost").content(
"Hello, world"));
httpRequest(RequestMethod.PUT, "/foo").header(HttpHeaders.HOST,
"localhost").content("Hello, world"));
new HttpRequestSnippet().document(new OperationBuilder(
"put-request-with-content", this.snippet.getOutputDirectory())
@@ -114,7 +114,8 @@ public class HttpRequestSnippetTests {
@Test
public void putRequestWithParameter() throws IOException {
this.snippet.expectHttpRequest("put-request-with-parameter").withContents(
httpRequest(PUT, "/foo").header(HttpHeaders.HOST, "localhost")
httpRequest(RequestMethod.PUT, "/foo")
.header(HttpHeaders.HOST, "localhost")
.header("Content-Type", "application/x-www-form-urlencoded")
.content("b%26r=baz&a=alpha"));
@@ -129,7 +130,7 @@ public class HttpRequestSnippetTests {
String expectedContent = createPart(String.format("Content-Disposition: "
+ "form-data; " + "name=image%n%n<< data >>"));
this.snippet.expectHttpRequest("multipart-post").withContents(
httpRequest(POST, "/upload")
httpRequest(RequestMethod.POST, "/upload")
.header(HttpHeaders.HOST, "localhost")
.header("Content-Type",
"multipart/form-data; boundary=" + BOUNDARY)
@@ -153,7 +154,7 @@ public class HttpRequestSnippetTests {
+ "name=image%n%n<< data >>"));
String expectedContent = param1Part + param2Part + param3Part + filePart;
this.snippet.expectHttpRequest("multipart-post-with-parameters").withContents(
httpRequest(POST, "/upload")
httpRequest(RequestMethod.POST, "/upload")
.header(HttpHeaders.HOST, "localhost")
.header("Content-Type",
"multipart/form-data; boundary=" + BOUNDARY)
@@ -172,7 +173,7 @@ public class HttpRequestSnippetTests {
.format("Content-Disposition: form-data; name=image%nContent-Type: "
+ "image/png%n%n<< data >>"));
this.snippet.expectHttpRequest("multipart-post-with-content-type").withContents(
httpRequest(POST, "/upload")
httpRequest(RequestMethod.POST, "/upload")
.header(HttpHeaders.HOST, "localhost")
.header("Content-Type",
"multipart/form-data; boundary=" + BOUNDARY)
@@ -188,7 +189,8 @@ public class HttpRequestSnippetTests {
@Test
public void getRequestWithCustomHost() throws IOException {
this.snippet.expectHttpRequest("get-request-custom-host").withContents(
httpRequest(GET, "/foo").header(HttpHeaders.HOST, "api.example.com"));
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());
@@ -199,8 +201,8 @@ public class HttpRequestSnippetTests {
this.snippet.expectHttpRequest("request-with-snippet-attributes").withContents(
containsString("Title for the request"));
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
when(resolver.resolveTemplateResource("http-request"))
.thenReturn(
given(resolver.resolveTemplateResource("http-request"))
.willReturn(
new FileSystemResource(
"src/test/resources/custom-snippet-templates/http-request-with-title.snippet"));
new HttpRequestSnippet(attributes(key("title").value("Title for the request")))

View File

@@ -16,21 +16,13 @@
package org.springframework.restdocs.http;
import static org.hamcrest.CoreMatchers.containsString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.OK;
import static org.springframework.restdocs.snippet.Attributes.attributes;
import static org.springframework.restdocs.snippet.Attributes.key;
import static org.springframework.restdocs.test.SnippetMatchers.httpResponse;
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;
import org.springframework.http.MediaType;
import org.springframework.restdocs.templates.TemplateEngine;
import org.springframework.restdocs.templates.TemplateResourceResolver;
@@ -38,8 +30,15 @@ import org.springframework.restdocs.templates.mustache.MustacheTemplateEngine;
import org.springframework.restdocs.test.ExpectedSnippet;
import org.springframework.restdocs.test.OperationBuilder;
import static org.hamcrest.CoreMatchers.containsString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.springframework.restdocs.snippet.Attributes.attributes;
import static org.springframework.restdocs.snippet.Attributes.key;
import static org.springframework.restdocs.test.SnippetMatchers.httpResponse;
/**
* Tests for {@link HttpResponseSnippet}
* Tests for {@link HttpResponseSnippet}.
*
* @author Andy Wilkinson
* @author Jonathan Pearlin
@@ -51,7 +50,8 @@ public class HttpResponseSnippetTests {
@Test
public void basicResponse() throws IOException {
this.snippet.expectHttpResponse("basic-response").withContents(httpResponse(OK));
this.snippet.expectHttpResponse("basic-response").withContents(
httpResponse(HttpStatus.OK));
new HttpResponseSnippet().document(new OperationBuilder("basic-response",
this.snippet.getOutputDirectory()).build());
}
@@ -59,16 +59,16 @@ public class HttpResponseSnippetTests {
@Test
public void nonOkResponse() throws IOException {
this.snippet.expectHttpResponse("non-ok-response").withContents(
httpResponse(BAD_REQUEST));
httpResponse(HttpStatus.BAD_REQUEST));
new HttpResponseSnippet().document(new OperationBuilder("non-ok-response",
this.snippet.getOutputDirectory()).response().status(BAD_REQUEST.value())
.build());
this.snippet.getOutputDirectory()).response()
.status(HttpStatus.BAD_REQUEST.value()).build());
}
@Test
public void responseWithHeaders() throws IOException {
this.snippet.expectHttpResponse("response-with-headers").withContents(
httpResponse(OK) //
httpResponse(HttpStatus.OK) //
.header("Content-Type", "application/json") //
.header("a", "alpha"));
new HttpResponseSnippet().document(new OperationBuilder("response-with-headers",
@@ -80,7 +80,7 @@ public class HttpResponseSnippetTests {
@Test
public void responseWithContent() throws IOException {
this.snippet.expectHttpResponse("response-with-content").withContents(
httpResponse(OK).content("content"));
httpResponse(HttpStatus.OK).content("content"));
new HttpResponseSnippet().document(new OperationBuilder("response-with-content",
this.snippet.getOutputDirectory()).response().content("content").build());
}
@@ -90,8 +90,8 @@ public class HttpResponseSnippetTests {
this.snippet.expectHttpResponse("response-with-snippet-attributes").withContents(
containsString("Title for the response"));
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
when(resolver.resolveTemplateResource("http-response"))
.thenReturn(
given(resolver.resolveTemplateResource("http-response"))
.willReturn(
new FileSystemResource(
"src/test/resources/custom-snippet-templates/http-response-with-title.snippet"));
new HttpResponseSnippet(attributes(key("title").value("Title for the response")))

View File

@@ -16,9 +16,6 @@
package org.springframework.restdocs.hypermedia;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@@ -31,9 +28,12 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.restdocs.operation.StandardOperationResponse;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/**
* Tests for {@link ContentTypeLinkExtractor}.
*
*
* @author Andy Wilkinson
*/
public class ContentTypeLinkExtractorTests {
@@ -73,5 +73,4 @@ public class ContentTypeLinkExtractorTests {
new ContentTypeLinkExtractor(extractors).extractLinks(response);
verify(extractor).extractLinks(response);
}
}

View File

@@ -16,8 +16,6 @@
package org.springframework.restdocs.hypermedia;
import static org.junit.Assert.assertEquals;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
@@ -37,6 +35,8 @@ import org.springframework.util.FileCopyUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import static org.junit.Assert.assertEquals;
/**
* Parameterized tests for {@link HalLinkExtractor} and {@link AtomLinkExtractor} with
* various payloads.
@@ -88,14 +88,14 @@ public class LinkExtractorsPayloadTests {
public void noLinks() throws IOException {
Map<String, List<Link>> links = this.linkExtractor
.extractLinks(createResponse("no-links"));
assertLinks(Collections.<Link> emptyList(), links);
assertLinks(Collections.<Link>emptyList(), links);
}
@Test
public void linksInTheWrongFormat() throws IOException {
Map<String, List<Link>> links = this.linkExtractor
.extractLinks(createResponse("wrong-format"));
assertLinks(Collections.<Link> emptyList(), links);
assertLinks(Collections.<Link>emptyList(), links);
}
private void assertLinks(List<Link> expectedLinks, Map<String, List<Link>> actualLinks) {

View File

@@ -16,14 +16,6 @@
package org.springframework.restdocs.hypermedia;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.springframework.restdocs.snippet.Attributes.attributes;
import static org.springframework.restdocs.snippet.Attributes.key;
import static org.springframework.restdocs.test.SnippetMatchers.tableWithHeader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
@@ -42,8 +34,16 @@ import org.springframework.restdocs.test.OperationBuilder;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.springframework.restdocs.snippet.Attributes.attributes;
import static org.springframework.restdocs.snippet.Attributes.key;
import static org.springframework.restdocs.test.SnippetMatchers.tableWithHeader;
/**
* Tests for {@link LinksSnippet}
* Tests for {@link LinksSnippet}.
*
* @author Andy Wilkinson
*/
@@ -61,7 +61,7 @@ public class LinksSnippetTests {
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(
Collections.<LinkDescriptor>emptyList()).document(new OperationBuilder(
"undocumented-link", this.snippet.getOutputDirectory()).build());
}
@@ -77,9 +77,8 @@ public class LinksSnippetTests {
@Test
public void documentedOptionalLink() throws IOException {
this.snippet.expectLinks("documented-optional-link").withContents( //
tableWithHeader("Relation", "Description") //
.row("foo", "bar"));
this.snippet.expectLinks("documented-optional-link").withContents(
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
@@ -88,9 +87,8 @@ public class LinksSnippetTests {
@Test
public void missingOptionalLink() throws IOException {
this.snippet.expectLinks("missing-optional-link").withContents( //
tableWithHeader("Relation", "Description") //
.row("foo", "bar"));
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());
@@ -110,9 +108,8 @@ public class LinksSnippetTests {
@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(
@@ -124,15 +121,15 @@ public class LinksSnippetTests {
@Test
public void linksWithCustomDescriptorAttributes() throws IOException {
this.snippet.expectLinks("links-with-custom-descriptor-attributes").withContents( //
tableWithHeader("Relation", "Description", "Foo") //
.row("a", "one", "alpha") //
.row("b", "two", "bravo"));
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
when(resolver.resolveTemplateResource("links"))
.thenReturn(
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")
.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(
@@ -146,21 +143,21 @@ public class LinksSnippetTests {
@Test
public void linksWithCustomAttributes() throws IOException {
this.snippet.expectLinks("links-with-custom-attributes").withContents(
startsWith(".Title for the links"));
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
when(resolver.resolveTemplateResource("links"))
.thenReturn(
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"));
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 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());
}

View File

@@ -16,10 +16,6 @@
package org.springframework.restdocs.operation.preprocess;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.net.URI;
import java.util.Collections;
@@ -34,8 +30,12 @@ import org.springframework.restdocs.operation.Parameters;
import org.springframework.restdocs.operation.StandardOperationRequest;
import org.springframework.restdocs.operation.StandardOperationResponse;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
/**
* Tests for {@link ContentModifyingOperationPreprocessor}
* Tests for {@link ContentModifyingOperationPreprocessor}.
*
* @author Andy Wilkinson
*
@@ -57,7 +57,7 @@ public class ContentModifyingOperationPreprocessorTests {
StandardOperationRequest request = new StandardOperationRequest(
URI.create("http://localhost"), HttpMethod.GET, "content".getBytes(),
new HttpHeaders(), new Parameters(),
Collections.<OperationRequestPart> emptyList());
Collections.<OperationRequestPart>emptyList());
OperationRequest preprocessed = this.preprocessor.preprocess(request);
assertThat(preprocessed.getContent(), is(equalTo("modified".getBytes())));
}
@@ -75,7 +75,7 @@ public class ContentModifyingOperationPreprocessorTests {
StandardOperationRequest request = new StandardOperationRequest(
URI.create("http://localhost"), HttpMethod.GET, "content".getBytes(),
new HttpHeaders(), new Parameters(),
Collections.<OperationRequestPart> emptyList());
Collections.<OperationRequestPart>emptyList());
OperationRequest preprocessed = this.preprocessor.preprocess(request);
assertThat(preprocessed.getHeaders().getContentLength(), is(equalTo(-1L)));
}
@@ -87,7 +87,7 @@ public class ContentModifyingOperationPreprocessorTests {
StandardOperationRequest request = new StandardOperationRequest(
URI.create("http://localhost"), HttpMethod.GET, "content".getBytes(),
httpHeaders, new Parameters(),
Collections.<OperationRequestPart> emptyList());
Collections.<OperationRequestPart>emptyList());
OperationRequest preprocessed = this.preprocessor.preprocess(request);
assertThat(preprocessed.getHeaders().getContentLength(), is(equalTo(8L)));
}

View File

@@ -16,19 +16,19 @@
package org.springframework.restdocs.operation.preprocess;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import org.junit.Test;
import org.springframework.restdocs.operation.OperationRequest;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link DelegatingOperationRequestPreprocessor}
*
* Tests for {@link DelegatingOperationRequestPreprocessor}.
*
* @author Andy Wilkinson
*/
public class DelegatingOperationRequestPreprocessorTests {
@@ -36,19 +36,17 @@ public class DelegatingOperationRequestPreprocessorTests {
@Test
public void delegationOccurs() {
OperationRequest originalRequest = mock(OperationRequest.class);
OperationPreprocessor preprocessor1 = mock(OperationPreprocessor.class);
OperationRequest preprocessedRequest1 = mock(OperationRequest.class);
when(preprocessor1.preprocess(originalRequest)).thenReturn(preprocessedRequest1);
OperationPreprocessor preprocessor2 = mock(OperationPreprocessor.class);
OperationRequest preprocessedRequest2 = mock(OperationRequest.class);
when(preprocessor2.preprocess(preprocessedRequest1)).thenReturn(
preprocessedRequest2);
OperationPreprocessor preprocessor3 = mock(OperationPreprocessor.class);
OperationRequest preprocessedRequest3 = mock(OperationRequest.class);
when(preprocessor3.preprocess(preprocessedRequest2)).thenReturn(
given(preprocessor1.preprocess(originalRequest)).willReturn(preprocessedRequest1);
given(preprocessor2.preprocess(preprocessedRequest1)).willReturn(
preprocessedRequest2);
given(preprocessor3.preprocess(preprocessedRequest2)).willReturn(
preprocessedRequest3);
OperationRequest result = new DelegatingOperationRequestPreprocessor(
@@ -57,4 +55,5 @@ public class DelegatingOperationRequestPreprocessorTests {
assertThat(result, is(preprocessedRequest3));
}
}

View File

@@ -16,19 +16,19 @@
package org.springframework.restdocs.operation.preprocess;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import org.junit.Test;
import org.springframework.restdocs.operation.OperationResponse;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link DelegatingOperationResponsePreprocessor}
*
* Tests for {@link DelegatingOperationResponsePreprocessor}.
*
* @author Andy Wilkinson
*/
public class DelegatingOperationResponsePreprocessorTests {
@@ -36,20 +36,18 @@ public class DelegatingOperationResponsePreprocessorTests {
@Test
public void delegationOccurs() {
OperationResponse originalResponse = mock(OperationResponse.class);
OperationPreprocessor preprocessor1 = mock(OperationPreprocessor.class);
OperationResponse preprocessedResponse1 = mock(OperationResponse.class);
when(preprocessor1.preprocess(originalResponse))
.thenReturn(preprocessedResponse1);
OperationPreprocessor preprocessor2 = mock(OperationPreprocessor.class);
OperationResponse preprocessedResponse2 = mock(OperationResponse.class);
when(preprocessor2.preprocess(preprocessedResponse1)).thenReturn(
preprocessedResponse2);
OperationPreprocessor preprocessor3 = mock(OperationPreprocessor.class);
OperationResponse preprocessedResponse3 = mock(OperationResponse.class);
when(preprocessor3.preprocess(preprocessedResponse2)).thenReturn(
given(preprocessor1.preprocess(originalResponse)).willReturn(
preprocessedResponse1);
given(preprocessor2.preprocess(preprocessedResponse1)).willReturn(
preprocessedResponse2);
given(preprocessor3.preprocess(preprocessedResponse2)).willReturn(
preprocessedResponse3);
OperationResponse result = new DelegatingOperationResponsePreprocessor(

View File

@@ -16,11 +16,6 @@
package org.springframework.restdocs.operation.preprocess;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.hasEntry;
import static org.junit.Assert.assertThat;
import java.net.URI;
import java.util.Arrays;
import java.util.Collections;
@@ -36,8 +31,13 @@ import org.springframework.restdocs.operation.Parameters;
import org.springframework.restdocs.operation.StandardOperationRequest;
import org.springframework.restdocs.operation.StandardOperationResponse;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.hasEntry;
import static org.junit.Assert.assertThat;
/**
* Tests for {@link HeaderRemovingOperationPreprocessorTests}
* Tests for {@link HeaderRemovingOperationPreprocessorTests}.
*
* @author Andy Wilkinson
*
@@ -52,7 +52,7 @@ public class HeaderRemovingOperationPreprocessorTests {
StandardOperationRequest request = new StandardOperationRequest(
URI.create("http://localhost"), HttpMethod.GET, new byte[0],
getHttpHeaders(), new Parameters(),
Collections.<OperationRequestPart> emptyList());
Collections.<OperationRequestPart>emptyList());
OperationRequest preprocessed = this.preprocessor.preprocess(request);
assertThat(preprocessed.getHeaders().size(), is(equalTo(1)));
assertThat(preprocessed.getHeaders(), hasEntry("a", Arrays.asList("alpha")));

View File

@@ -16,10 +16,6 @@
package org.springframework.restdocs.operation.preprocess;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
@@ -34,8 +30,12 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
/**
* Tests for {@link LinkMaskingContentModifier}
* Tests for {@link LinkMaskingContentModifier}.
*
* @author Andy Wilkinson
*
@@ -125,21 +125,22 @@ public class LinkMaskingContentModifierTests {
return payload;
}
static final class AtomPayload {
private static final class AtomPayload {
private List<Link> links;
@SuppressWarnings("unused")
public List<Link> getLinks() {
return this.links;
}
public void setLinks(List<Link> links) {
this.links = links;
}
public List<Link> getLinks() {
return this.links;
}
}
static final class HalPayload {
private static final class HalPayload {
private Map<String, Object> links;

View File

@@ -16,16 +16,16 @@
package org.springframework.restdocs.operation.preprocess;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.util.regex.Pattern;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
/**
* Tests for {@link PatternReplacingContentModifier}
* Tests for {@link PatternReplacingContentModifier}.
*
* @author Andy Wilkinson
*

View File

@@ -16,14 +16,14 @@
package org.springframework.restdocs.operation.preprocess;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import org.junit.Test;
/**
* Tests for {@link PrettyPrintingContentModifier}
*
* Tests for {@link PrettyPrintingContentModifier}.
*
* @author Andy Wilkinson
*
*/

View File

@@ -16,16 +16,16 @@
package org.springframework.restdocs.payload;
import org.junit.Test;
import static org.hamcrest.Matchers.contains;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
/**
* Tests for {@link JsonFieldPath}
*
* Tests for {@link JsonFieldPath}.
*
* @author Andy Wilkinson
* @author Jeremy Rickard
*/
@@ -113,17 +113,20 @@ public class JsonFieldPathTests {
@Test
public void compilationOfMultipleElementPathWithBrackets() {
assertThat(JsonFieldPath.compile("['a']['b']['c']").getSegments(), contains("a", "b", "c"));
assertThat(JsonFieldPath.compile("['a']['b']['c']").getSegments(),
contains("a", "b", "c"));
}
@Test
public void compilationOfMultipleElementPathWithAndWithoutBrackets() {
assertThat(JsonFieldPath.compile("['a'][].b['c']").getSegments(), contains("a", "[]", "b", "c"));
assertThat(JsonFieldPath.compile("['a'][].b['c']").getSegments(),
contains("a", "[]", "b", "c"));
}
@Test
public void compilationOfMultipleElementPathWithAndWithoutBracketsAndEmbeddedDots() {
assertThat(JsonFieldPath.compile("['a.key'][].b['c']").getSegments(), contains("a.key", "[]", "b", "c"));
assertThat(JsonFieldPath.compile("['a.key'][].b['c']").getSegments(),
contains("a.key", "[]", "b", "c"));
}
}

View File

@@ -16,9 +16,6 @@
package org.springframework.restdocs.payload;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
@@ -29,9 +26,12 @@ import org.junit.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
/**
* Tests for {@link JsonFieldProcessor}
*
* Tests for {@link JsonFieldProcessor}.
*
* @author Andy Wilkinson
*/
public class JsonFieldProcessorTests {

View File

@@ -16,9 +16,6 @@
package org.springframework.restdocs.payload;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import java.io.IOException;
import java.util.Map;
@@ -28,9 +25,12 @@ import org.junit.rules.ExpectedException;
import com.fasterxml.jackson.databind.ObjectMapper;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
/**
* Tests for {@link JsonFieldTypeResolver}
*
* Tests for {@link JsonFieldTypeResolver}.
*
* @author Andy Wilkinson
*
*/
@@ -74,7 +74,8 @@ public class JsonFieldTypeResolverTests {
@Test
public void nestedField() throws IOException {
assertThat(this.fieldTypeResolver.resolveFieldType("a.b.c",
createPayload("{\"a\":{\"b\":{\"c\":{}}}}")), equalTo(JsonFieldType.OBJECT));
createPayload("{\"a\":{\"b\":{\"c\":{}}}}")),
equalTo(JsonFieldType.OBJECT));
}
@Test

View File

@@ -16,16 +16,6 @@
package org.springframework.restdocs.payload;
import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.snippet.Attributes.attributes;
import static org.springframework.restdocs.snippet.Attributes.key;
import static org.springframework.restdocs.test.SnippetMatchers.tableWithHeader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
@@ -43,9 +33,19 @@ import org.springframework.restdocs.templates.mustache.MustacheTemplateEngine;
import org.springframework.restdocs.test.ExpectedSnippet;
import org.springframework.restdocs.test.OperationBuilder;
import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.snippet.Attributes.attributes;
import static org.springframework.restdocs.snippet.Attributes.key;
import static org.springframework.restdocs.test.SnippetMatchers.tableWithHeader;
/**
* Tests for {@link RequestFieldsSnippet}
*
* Tests for {@link RequestFieldsSnippet}.
*
* @author Andy Wilkinson
*/
public class RequestFieldsSnippetTests {
@@ -58,10 +58,9 @@ public class RequestFieldsSnippetTests {
@Test
public void mapRequestWithFields() throws IOException {
this.snippet.expectRequestFields("map-request-with-fields").withContents( //
tableWithHeader("Path", "Type", "Description") //
.row("a.b", "Number", "one") //
.row("a.c", "String", "two") //
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"),
@@ -74,10 +73,9 @@ public class RequestFieldsSnippetTests {
@Test
public void arrayRequestWithFields() throws IOException {
this.snippet.expectRequestFields("array-request-with-fields").withContents( //
tableWithHeader("Path", "Type", "Description") //
.row("[]a.b", "Number", "one") //
.row("[]a.c", "String", "two") //
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"),
@@ -94,7 +92,7 @@ public class RequestFieldsSnippetTests {
this.thrown
.expectMessage(startsWith("The following parts of the payload were not"
+ " documented:"));
new RequestFieldsSnippet(Collections.<FieldDescriptor> emptyList())
new RequestFieldsSnippet(Collections.<FieldDescriptor>emptyList())
.document(new OperationBuilder("undocumented-request-field", this.snippet
.getOutputDirectory()).request("http://localhost")
.content("{\"a\": 5}").build());
@@ -140,15 +138,16 @@ public class RequestFieldsSnippetTests {
@Test
public void requestFieldsWithCustomDescriptorAttributes() throws IOException {
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"));
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
when(resolver.resolveTemplateResource("request-fields")).thenReturn(
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")),
@@ -165,11 +164,12 @@ public class RequestFieldsSnippetTests {
@Test
public void requestFieldsWithCustomAttributes() throws IOException {
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("request-fields")).willReturn(
snippetResource("request-fields-with-title"));
this.snippet.expectRequestFields("request-fields-with-custom-attributes")
.withContents(startsWith(".Custom title"));
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
when(resolver.resolveTemplateResource("request-fields")).thenReturn(
snippetResource("request-fields-with-title"));
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one")),
attributes(key("title").value("Custom title")))
.document(new OperationBuilder("request-fields-with-custom-attributes",
@@ -181,15 +181,16 @@ public class RequestFieldsSnippetTests {
@Test
public void requestFieldsWithListDescription() throws IOException {
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
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\"]"));
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
when(resolver.resolveTemplateResource("request-fields")).thenReturn(
snippetResource("request-fields-with-list-description"));
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a").description(
Arrays.asList("one", "two"))))
.document(new OperationBuilder("request-fields-with-list-description",
@@ -201,11 +202,9 @@ public class RequestFieldsSnippetTests {
@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"),
@@ -224,7 +223,7 @@ public class RequestFieldsSnippetTests {
this.thrown
.expectMessage(startsWith("The following parts of the payload were not"
+ " documented:"));
new RequestFieldsSnippet(Collections.<FieldDescriptor> emptyList())
new RequestFieldsSnippet(Collections.<FieldDescriptor>emptyList())
.document(new OperationBuilder("undocumented-xml-request-field",
this.snippet.getOutputDirectory())
.request("http://localhost")

View File

@@ -13,17 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.restdocs.payload;
import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.snippet.Attributes.attributes;
import static org.springframework.restdocs.snippet.Attributes.key;
import static org.springframework.restdocs.test.SnippetMatchers.tableWithHeader;
package org.springframework.restdocs.payload;
import java.io.IOException;
import java.util.Arrays;
@@ -42,8 +33,18 @@ import org.springframework.restdocs.templates.mustache.MustacheTemplateEngine;
import org.springframework.restdocs.test.ExpectedSnippet;
import org.springframework.restdocs.test.OperationBuilder;
import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.snippet.Attributes.attributes;
import static org.springframework.restdocs.snippet.Attributes.key;
import static org.springframework.restdocs.test.SnippetMatchers.tableWithHeader;
/**
* Tests for {@link ReponseFieldsSnippet}
* Tests for {@link ResponseFieldsSnippet}.
*
* @author Andy Wilkinson
*/
@@ -57,13 +58,11 @@ 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") //
.row("assets[].id", "Number", "five") //
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")
@@ -82,10 +81,9 @@ public class ResponseFieldsSnippetTests {
@Test
public void arrayResponseWithFields() throws IOException {
this.snippet.expectResponseFields("array-response-with-fields").withContents( //
tableWithHeader("Path", "Type", "Description") //
.row("[]a.b", "Number", "one") //
.row("[]a.c", "String", "two") //
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")
@@ -98,9 +96,10 @@ public class ResponseFieldsSnippetTests {
@Test
public void arrayResponse() throws IOException {
this.snippet.expectResponseFields("array-response").withContents( //
tableWithHeader("Path", "Type", "Description") //
.row("[]", "String", "one"));
this.snippet.expectResponseFields("array-response")
.withContents(
tableWithHeader("Path", "Type", "Description").row("[]",
"String", "one"));
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("[]").description("one")))
.document(new OperationBuilder("array-response", this.snippet
.getOutputDirectory()).response()
@@ -109,15 +108,16 @@ public class ResponseFieldsSnippetTests {
@Test
public void responseFieldsWithCustomDescriptorAttributes() throws IOException {
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"));
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
when(resolver.resolveTemplateResource("response-fields")).thenReturn(
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"));
new ResponseFieldsSnippet(Arrays.asList(
fieldWithPath("a.b").description("one").attributes(
key("foo").value("alpha")),
@@ -134,11 +134,12 @@ public class ResponseFieldsSnippetTests {
@Test
public void responseFieldsWithCustomAttributes() throws IOException {
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("response-fields")).willReturn(
snippetResource("response-fields-with-title"));
this.snippet.expectResponseFields("response-fields-with-custom-attributes")
.withContents(startsWith(".Custom title"));
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
when(resolver.resolveTemplateResource("response-fields")).thenReturn(
snippetResource("response-fields-with-title"));
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one")),
attributes(key("title").value("Custom title")))
.document(new OperationBuilder("response-fields-with-custom-attributes",
@@ -150,11 +151,9 @@ public class ResponseFieldsSnippetTests {
@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"));
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")))
@@ -172,7 +171,7 @@ public class ResponseFieldsSnippetTests {
this.thrown
.expectMessage(startsWith("The following parts of the payload were not"
+ " documented:"));
new ResponseFieldsSnippet(Collections.<FieldDescriptor> emptyList())
new ResponseFieldsSnippet(Collections.<FieldDescriptor>emptyList())
.document(new OperationBuilder("undocumented-xml-response-field",
this.snippet.getOutputDirectory())
.response()

View File

@@ -16,16 +16,6 @@
package org.springframework.restdocs.request;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
import static org.springframework.restdocs.snippet.Attributes.attributes;
import static org.springframework.restdocs.snippet.Attributes.key;
import static org.springframework.restdocs.test.SnippetMatchers.tableWithHeader;
import static org.springframework.restdocs.test.SnippetMatchers.tableWithTitleAndHeader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
@@ -41,10 +31,20 @@ import org.springframework.restdocs.templates.mustache.MustacheTemplateEngine;
import org.springframework.restdocs.test.ExpectedSnippet;
import org.springframework.restdocs.test.OperationBuilder;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
import static org.springframework.restdocs.snippet.Attributes.attributes;
import static org.springframework.restdocs.snippet.Attributes.key;
import static org.springframework.restdocs.test.SnippetMatchers.tableWithHeader;
import static org.springframework.restdocs.test.SnippetMatchers.tableWithTitleAndHeader;
/**
* Tests for {@link PathParametersSnippet}
*
* @author awilkinson
* Tests for {@link PathParametersSnippet}.
*
* @author Andy Wilkinson
*
*/
public class PathParametersSnippetTests {
@@ -60,7 +60,7 @@ public class PathParametersSnippetTests {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(equalTo("Path parameters with the following names were"
+ " not documented: [a]"));
new PathParametersSnippet(Collections.<ParameterDescriptor> emptyList())
new PathParametersSnippet(Collections.<ParameterDescriptor>emptyList())
.document(new OperationBuilder("undocumented-path-parameter",
this.snippet.getOutputDirectory()).attribute(
"org.springframework.restdocs.urlTemplate", "/{a}/").build());
@@ -121,13 +121,14 @@ public class PathParametersSnippetTests {
@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"));
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
when(resolver.resolveTemplateResource("path-parameters")).thenReturn(
snippetResource("path-parameters-with-extra-column"));
new PathParametersSnippet(Arrays.asList(parameterWithName("a").description("one")
.attributes(key("foo").value("alpha")), parameterWithName("b")
.description("two").attributes(key("foo").value("bravo"))))
@@ -141,16 +142,15 @@ public class PathParametersSnippetTests {
@Test
public void pathParametersWithCustomAttributes() throws IOException {
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("path-parameters")).willReturn(
snippetResource("path-parameters-with-title"));
this.snippet.expectPathParameters("path-parameters-with-custom-attributes")
.withContents(startsWith(".The title"));
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
when(resolver.resolveTemplateResource("path-parameters")).thenReturn(
snippetResource("path-parameters-with-title"));
new PathParametersSnippet(
Arrays.asList(
parameterWithName("a").description("one").attributes(
key("foo").value("alpha")), parameterWithName("b")
.description("two").attributes(key("foo").value("bravo"))),
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())

View File

@@ -16,15 +16,6 @@
package org.springframework.restdocs.request;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
import static org.springframework.restdocs.snippet.Attributes.attributes;
import static org.springframework.restdocs.snippet.Attributes.key;
import static org.springframework.restdocs.test.SnippetMatchers.tableWithHeader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
@@ -40,8 +31,17 @@ import org.springframework.restdocs.templates.mustache.MustacheTemplateEngine;
import org.springframework.restdocs.test.ExpectedSnippet;
import org.springframework.restdocs.test.OperationBuilder;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
import static org.springframework.restdocs.snippet.Attributes.attributes;
import static org.springframework.restdocs.snippet.Attributes.key;
import static org.springframework.restdocs.test.SnippetMatchers.tableWithHeader;
/**
* Tests for {@link RequestParametersSnippet}
* Tests for {@link RequestParametersSnippet}.
*
* @author Andy Wilkinson
*/
@@ -59,7 +59,7 @@ public class RequestParametersSnippetTests {
this.thrown
.expectMessage(equalTo("Request parameters with the following names were"
+ " not documented: [a]"));
new RequestParametersSnippet(Collections.<ParameterDescriptor> emptyList())
new RequestParametersSnippet(Collections.<ParameterDescriptor>emptyList())
.document(new OperationBuilder("undocumented-parameter", this.snippet
.getOutputDirectory()).request("http://localhost")
.param("a", "alpha").build());
@@ -104,13 +104,14 @@ public class RequestParametersSnippetTests {
@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"));
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
when(resolver.resolveTemplateResource("request-parameters")).thenReturn(
snippetResource("request-parameters-with-extra-column"));
new RequestParametersSnippet(Arrays.asList(
parameterWithName("a").description("one").attributes(
key("foo").value("alpha")),
@@ -125,24 +126,23 @@ public class RequestParametersSnippetTests {
@Test
public void requestParametersWithCustomAttributes() throws IOException {
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
given(resolver.resolveTemplateResource("request-parameters")).willReturn(
snippetResource("request-parameters-with-title"));
this.snippet.expectRequestParameters("request-parameters-with-custom-attributes")
.withContents(startsWith(".The title"));
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);
when(resolver.resolveTemplateResource("request-parameters")).thenReturn(
snippetResource("request-parameters-with-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) {

View File

@@ -16,15 +16,15 @@
package org.springframework.restdocs.snippet;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import org.junit.Test;
import org.springframework.restdocs.RestDocumentationContext;
import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
/**
* Tests for {@link RestDocumentationContextPlaceholderResolver}
* Tests for {@link RestDocumentationContextPlaceholderResolver}.
*
* @author Andy Wilkinson
*

View File

@@ -16,20 +16,20 @@
package org.springframework.restdocs.snippet;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import java.io.File;
import org.junit.Test;
import org.springframework.restdocs.RestDocumentationContext;
import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link StandardWriterResolver}.
*
*
* @author Andy Wilkinson
*/
public class StandardWriterResolverTests {

View File

@@ -13,11 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.restdocs.templates;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
package org.springframework.restdocs.templates;
import java.net.URL;
import java.util.HashMap;
@@ -29,9 +26,13 @@ import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.core.io.Resource;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
/**
* Tests for {@link TemplateResourceResolver}.
*
*
* @author Andy Wilkinson
*/
public class StandardTemplateResourceResolverTests {

View File

@@ -16,9 +16,6 @@
package org.springframework.restdocs.test;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.io.File;
import java.io.IOException;
@@ -29,10 +26,13 @@ import org.junit.runners.model.Statement;
import org.springframework.restdocs.snippet.TemplatedSnippet;
import org.springframework.restdocs.test.SnippetMatchers.SnippetMatcher;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
/**
* The {@code ExpectedSnippet} rule is used to verify that a {@link TemplatedSnippet} has
* generated the expected snippet.
*
*
* @author Andy Wilkinson
*/
public class ExpectedSnippet implements TestRule {
@@ -52,22 +52,6 @@ public class ExpectedSnippet implements TestRule {
return new ExpectedSnippetStatement(base);
}
private final class ExpectedSnippetStatement extends Statement {
private final Statement delegate;
public ExpectedSnippetStatement(Statement delegate) {
this.delegate = delegate;
}
@Override
public void evaluate() throws Throwable {
this.delegate.evaluate();
verifySnippet();
}
}
private void verifySnippet() throws IOException {
if (this.outputDirectory != null && this.expectedName != null) {
File snippetDir = new File(this.outputDirectory, this.expectedName);
@@ -130,4 +114,20 @@ public class ExpectedSnippet implements TestRule {
return this.outputDirectory;
}
private final class ExpectedSnippetStatement extends Statement {
private final Statement delegate;
private ExpectedSnippetStatement(Statement delegate) {
this.delegate = delegate;
}
@Override
public void evaluate() throws Throwable {
this.delegate.evaluate();
verifySnippet();
}
}
}

View File

@@ -43,6 +43,11 @@ import org.springframework.restdocs.templates.StandardTemplateResourceResolver;
import org.springframework.restdocs.templates.TemplateEngine;
import org.springframework.restdocs.templates.mustache.MustacheTemplateEngine;
/**
* Basic builder API for creating an {@link Operation}.
*
* @author Andy Wilkinson
*/
public class OperationBuilder {
private final Map<String, Object> attributes = new HashMap<>();
@@ -91,7 +96,10 @@ public class OperationBuilder {
this.responseBuilder.buildResponse(), this.attributes);
}
public class OperationRequestBuilder {
/**
* Basic builder API for creating an {@link OperationRequest}.
*/
public final class OperationRequestBuilder {
private URI requestUri = URI.create("http://localhost/");
@@ -105,7 +113,7 @@ public class OperationBuilder {
private List<OperationRequestPartBuilder> partBuilders = new ArrayList<>();
public OperationRequestBuilder(String uri) {
private OperationRequestBuilder(String uri) {
this.requestUri = URI.create(uri);
}
@@ -151,7 +159,10 @@ public class OperationBuilder {
return partBuilder;
}
public class OperationRequestPartBuilder {
/**
* Basic builder API for creating an {@link OperationRequestPart}.
*/
public final class OperationRequestPartBuilder {
private final String name;
@@ -191,7 +202,10 @@ public class OperationBuilder {
}
}
public class OperationResponseBuilder {
/**
* Basic builder API for creating an {@link OperationResponse}.
*/
public final class OperationResponseBuilder {
private HttpStatus status = HttpStatus.OK;

View File

@@ -35,10 +35,14 @@ import org.springframework.web.bind.annotation.RequestMethod;
/**
* {@link Matcher Matchers} for verify the contents of generated documentation snippets.
*
*
* @author Andy Wilkinson
*/
public class SnippetMatchers {
public final class SnippetMatchers {
private SnippetMatchers() {
}
public static SnippetMatcher snippet() {
return new SnippetMatcher();
@@ -117,6 +121,11 @@ public class SnippetMatchers {
}
}
/**
* A {@link Matcher} for an Asciidoctor code block.
*
* @param <T> The type of the matcher
*/
public static class AsciidoctorCodeBlockMatcher<T extends AsciidoctorCodeBlockMatcher<T>>
extends AbstractSnippetContentMatcher {
@@ -134,6 +143,11 @@ public class SnippetMatchers {
}
/**
* A {@link Matcher} for an HTTP request or response.
*
* @param <T> The type of the matcher
*/
public static abstract class HttpMatcher<T extends HttpMatcher<T>> extends
AsciidoctorCodeBlockMatcher<HttpMatcher<T>> {
@@ -151,25 +165,36 @@ public class SnippetMatchers {
}
public static class HttpResponseMatcher extends HttpMatcher<HttpResponseMatcher> {
/**
* A {@link Matcher} for an HTTP response.
*/
public static final class HttpResponseMatcher extends
HttpMatcher<HttpResponseMatcher> {
public HttpResponseMatcher(HttpStatus status) {
private HttpResponseMatcher(HttpStatus status) {
this.content("HTTP/1.1 " + status.value() + " " + status.getReasonPhrase());
this.content("");
}
}
public static class HttpRequestMatcher extends HttpMatcher<HttpRequestMatcher> {
/**
* A {@link Matcher} for an HTTP request.
*/
public static final class HttpRequestMatcher extends HttpMatcher<HttpRequestMatcher> {
public HttpRequestMatcher(RequestMethod requestMethod, String uri) {
private HttpRequestMatcher(RequestMethod requestMethod, String uri) {
this.content(requestMethod.name() + " " + uri + " HTTP/1.1");
this.content("");
}
}
public static class AsciidoctorTableMatcher extends AbstractSnippetContentMatcher {
/**
* A {@link Matcher} for an Asciidoctor table.
*/
public static final class AsciidoctorTableMatcher extends
AbstractSnippetContentMatcher {
private AsciidoctorTableMatcher(String title, String... columns) {
if (StringUtils.hasText(title)) {
@@ -198,6 +223,9 @@ public class SnippetMatchers {
}
}
/**
* A {@link Matcher} for a snippet file.
*/
public static class SnippetMatcher extends BaseMatcher<File> {
private Matcher<String> expectedContents;