Introduce constants for common attribute names

Closes gh-193
This commit is contained in:
Andy Wilkinson
2016-02-05 20:57:46 +00:00
parent 6df01419cd
commit 83b72f7962
14 changed files with 89 additions and 57 deletions

View File

@@ -22,6 +22,7 @@ import java.util.Map;
import org.springframework.restdocs.RestDocumentationContext;
import org.springframework.restdocs.curl.CurlDocumentation;
import org.springframework.restdocs.generate.RestDocumentationGenerator;
import org.springframework.restdocs.http.HttpDocumentation;
import org.springframework.restdocs.snippet.Snippet;
import org.springframework.restdocs.templates.TemplateFormat;
@@ -36,11 +37,6 @@ import org.springframework.restdocs.templates.TemplateFormats;
*/
public abstract class SnippetConfigurer<P, T> extends AbstractNestedConfigurer<P> {
/**
* The name of the attribute that is used to hold the default snippets.
*/
public static final String ATTRIBUTE_DEFAULT_SNIPPETS = "org.springframework.restdocs.defaultSnippets";
private List<Snippet> defaultSnippets = Arrays.asList(
CurlDocumentation.curlRequest(), HttpDocumentation.httpRequest(),
HttpDocumentation.httpResponse());
@@ -77,7 +73,8 @@ public abstract class SnippetConfigurer<P, T> extends AbstractNestedConfigurer<P
public void apply(Map<String, Object> configuration, RestDocumentationContext context) {
configuration.put(SnippetConfiguration.class.getName(), new SnippetConfiguration(
this.snippetEncoding, this.templateFormat));
configuration.put(ATTRIBUTE_DEFAULT_SNIPPETS, this.defaultSnippets);
configuration.put(RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_SNIPPETS,
this.defaultSnippets);
}
/**

View File

@@ -23,7 +23,6 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.restdocs.config.SnippetConfigurer;
import org.springframework.restdocs.operation.Operation;
import org.springframework.restdocs.operation.OperationRequest;
import org.springframework.restdocs.operation.OperationResponse;
@@ -45,6 +44,16 @@ import org.springframework.util.Assert;
*/
public final class RestDocumentationGenerator<REQ, RESP> {
/**
* Name of the operation attribute used to hold the request's URL template.
*/
public static final String ATTRIBUTE_NAME_URL_TEMPLATE = "org.springframework.restdocs.urlTemplate";
/**
* Name of the operation attribute used to hold the {@link List} of default snippets.
*/
public static final String ATTRIBUTE_NAME_DEFAULT_SNIPPETS = "org.springframework.restdocs.defaultSnippets";
private final String identifier;
private final OperationRequestPreprocessor requestPreprocessor;
@@ -58,8 +67,8 @@ public final class RestDocumentationGenerator<REQ, RESP> {
private final ResponseConverter<RESP> responseConverter;
/**
* Creates a new {@code RestDocumentationGenerator} for the operation identified by the
* given {@code identifier}. The given {@code requestConverter} and
* Creates a new {@code RestDocumentationGenerator} for the operation identified by
* the given {@code identifier}. The given {@code requestConverter} and
* {@code responseConverter} are used to convert the operation's request and response
* into generic {@code OperationRequest} and {@code OperationResponse} instances that
* can then be documented. The given documentation {@code snippets} will be produced.
@@ -78,8 +87,8 @@ public final class RestDocumentationGenerator<REQ, RESP> {
}
/**
* Creates a new {@code RestDocumentationGenerator} for the operation identified by the
* given {@code identifier}. The given {@code requestConverter} and
* Creates a new {@code RestDocumentationGenerator} for the operation identified by
* the given {@code identifier}. The given {@code requestConverter} and
* {@code responseConverter} are used to convert the operation's request and response
* into generic {@code OperationRequest} and {@code OperationResponse} instances that
* can then be documented. The given {@code requestPreprocessor} is applied to the
@@ -101,8 +110,8 @@ public final class RestDocumentationGenerator<REQ, RESP> {
}
/**
* Creates a new {@code RestDocumentationGenerator} for the operation identified by the
* given {@code identifier}. The given {@code requestConverter} and
* Creates a new {@code RestDocumentationGenerator} for the operation identified by
* the given {@code identifier}. The given {@code requestConverter} and
* {@code responseConverter} are used to convert the operation's request and response
* into generic {@code OperationRequest} and {@code OperationResponse} instances that
* can then be documented. The given {@code responsePreprocessor} is applied to the
@@ -125,8 +134,8 @@ public final class RestDocumentationGenerator<REQ, RESP> {
}
/**
* Creates a new {@code RestDocumentationGenerator} for the operation identified by the
* given {@code identifier}. The given {@code requestConverter} and
* Creates a new {@code RestDocumentationGenerator} for the operation identified by
* the given {@code identifier}. The given {@code requestConverter} and
* {@code responseConverter} are used to convert the operation's request and response
* into generic {@code OperationRequest} and {@code OperationResponse} instances that
* can then be documented. The given {@code requestPreprocessor} and
@@ -202,7 +211,7 @@ public final class RestDocumentationGenerator<REQ, RESP> {
private List<Snippet> getSnippets(Map<String, Object> configuration) {
List<Snippet> combinedSnippets = new ArrayList<>(this.snippets);
List<Snippet> defaultSnippets = (List<Snippet>) configuration
.get(SnippetConfigurer.ATTRIBUTE_DEFAULT_SNIPPETS);
.get(ATTRIBUTE_NAME_DEFAULT_SNIPPETS);
if (defaultSnippets != null) {
combinedSnippets.addAll(defaultSnippets);
}

View File

@@ -23,6 +23,7 @@ import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.restdocs.generate.RestDocumentationGenerator;
import org.springframework.restdocs.operation.Operation;
import org.springframework.restdocs.snippet.Snippet;
import org.springframework.restdocs.snippet.SnippetException;
@@ -91,7 +92,7 @@ public class PathParametersSnippet extends AbstractParametersSnippet {
private String extractUrlTemplate(Operation operation) {
String urlTemplate = (String) operation.getAttributes().get(
"org.springframework.restdocs.urlTemplate");
RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE);
Assert.notNull(
urlTemplate,
"urlTemplate not found. If you are using MockMvc, did you use RestDocumentationRequestBuilders to "

View File

@@ -25,7 +25,6 @@ import java.util.Map;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.http.HttpHeaders;
import org.springframework.restdocs.config.SnippetConfigurer;
import org.springframework.restdocs.generate.RestDocumentationGenerator;
import org.springframework.restdocs.operation.Operation;
import org.springframework.restdocs.operation.OperationRequest;
@@ -91,7 +90,7 @@ public class RestDocumentationGeneratorTests {
HashMap<String, Object> configuration = new HashMap<>();
Snippet defaultSnippet1 = mock(Snippet.class);
Snippet defaultSnippet2 = mock(Snippet.class);
configuration.put(SnippetConfigurer.ATTRIBUTE_DEFAULT_SNIPPETS,
configuration.put(RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_SNIPPETS,
Arrays.asList(defaultSnippet1, defaultSnippet2));
new RestDocumentationGenerator<>("id", this.requestConverter,
this.responseConverter, this.snippet).handle(this.request, this.response,

View File

@@ -25,6 +25,7 @@ import org.junit.Test;
import org.springframework.restdocs.RestDocumentationContext;
import org.springframework.restdocs.curl.CurlDocumentation;
import org.springframework.restdocs.curl.CurlRequestSnippet;
import org.springframework.restdocs.generate.RestDocumentationGenerator;
import org.springframework.restdocs.http.HttpRequestSnippet;
import org.springframework.restdocs.http.HttpResponseSnippet;
import org.springframework.restdocs.snippet.Snippet;
@@ -67,10 +68,11 @@ public class RestDocumentationConfigurerTests {
instanceOf(StandardWriterResolver.class)));
assertThat(
configuration,
hasEntry(equalTo("org.springframework.restdocs.defaultSnippets"),
hasEntry(
equalTo(RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_SNIPPETS),
instanceOf(List.class)));
List<Snippet> defaultSnippets = (List<Snippet>) configuration
.get("org.springframework.restdocs.defaultSnippets");
.get(RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_SNIPPETS);
assertThat(
defaultSnippets,
contains(instanceOf(CurlRequestSnippet.class),
@@ -115,11 +117,12 @@ public class RestDocumentationConfigurerTests {
.apply(configuration, context);
assertThat(
configuration,
hasEntry(equalTo("org.springframework.restdocs.defaultSnippets"),
hasEntry(
equalTo(RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_SNIPPETS),
instanceOf(List.class)));
@SuppressWarnings("unchecked")
List<Snippet> defaultSnippets = (List<Snippet>) configuration
.get("org.springframework.restdocs.defaultSnippets");
.get(RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_SNIPPETS);
assertThat(defaultSnippets, contains(instanceOf(CurlRequestSnippet.class)));
}

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.restdocs.generate.RestDocumentationGenerator;
import org.springframework.restdocs.snippet.SnippetException;
import org.springframework.restdocs.templates.TemplateFormats;
import org.springframework.restdocs.test.ExpectedSnippet;
@@ -53,7 +54,8 @@ public class PathParametersSnippetFailureTests {
new PathParametersSnippet(Collections.<ParameterDescriptor>emptyList())
.document(new OperationBuilder("undocumented-path-parameter",
this.snippet.getOutputDirectory()).attribute(
"org.springframework.restdocs.urlTemplate", "/{a}/").build());
RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE, "/{a}/")
.build());
}
@Test
@@ -65,7 +67,8 @@ public class PathParametersSnippetFailureTests {
Arrays.asList(parameterWithName("a").description("one")))
.document(new OperationBuilder("missing-path-parameter", this.snippet
.getOutputDirectory()).attribute(
"org.springframework.restdocs.urlTemplate", "/").build());
RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE, "/")
.build());
}
@Test
@@ -79,7 +82,8 @@ public class PathParametersSnippetFailureTests {
.document(new OperationBuilder(
"undocumented-and-missing-path-parameters", this.snippet
.getOutputDirectory()).attribute(
"org.springframework.restdocs.urlTemplate", "/{b}").build());
RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE, "/{b}")
.build());
}
}

View File

@@ -21,6 +21,7 @@ import java.util.Arrays;
import org.junit.Test;
import org.springframework.restdocs.AbstractSnippetTests;
import org.springframework.restdocs.generate.RestDocumentationGenerator;
import org.springframework.restdocs.templates.TemplateEngine;
import org.springframework.restdocs.templates.TemplateFormat;
import org.springframework.restdocs.templates.TemplateResourceResolver;
@@ -51,9 +52,10 @@ public class PathParametersSnippetTests extends AbstractSnippetTests {
"one").row("b", "two"));
new PathParametersSnippet(Arrays.asList(
parameterWithName("a").description("one"), parameterWithName("b")
.description("two"))).document(operationBuilder("path-parameters")
.attribute("org.springframework.restdocs.urlTemplate", "/{a}/{b}")
.build());
.description("two")))
.document(operationBuilder("path-parameters").attribute(
RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE,
"/{a}/{b}").build());
}
@Test
@@ -64,7 +66,8 @@ public class PathParametersSnippetTests extends AbstractSnippetTests {
new PathParametersSnippet(Arrays.asList(parameterWithName("a").ignored(),
parameterWithName("b").description("two"))).document(operationBuilder(
"ignored-path-parameter").attribute(
"org.springframework.restdocs.urlTemplate", "/{a}/{b}").build());
RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE, "/{a}/{b}")
.build());
}
@Test
@@ -77,7 +80,8 @@ public class PathParametersSnippetTests extends AbstractSnippetTests {
parameterWithName("a").description("one"), parameterWithName("b")
.description("two"))).document(operationBuilder(
"path-parameters-with-query-string").attribute(
"org.springframework.restdocs.urlTemplate", "/{a}/{b}?foo=bar").build());
RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE,
"/{a}/{b}?foo=bar").build());
}
@Test
@@ -93,7 +97,8 @@ public class PathParametersSnippetTests extends AbstractSnippetTests {
.description("two").attributes(key("foo").value("bravo"))),
attributes(key("title").value("The title"))).document(operationBuilder(
"path-parameters-with-custom-attributes")
.attribute("org.springframework.restdocs.urlTemplate", "/{a}/{b}")
.attribute(RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE,
"/{a}/{b}")
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver)).build());
@@ -112,8 +117,11 @@ public class PathParametersSnippetTests extends AbstractSnippetTests {
new PathParametersSnippet(Arrays.asList(parameterWithName("a").description("one")
.attributes(key("foo").value("alpha")), parameterWithName("b")
.description("two").attributes(key("foo").value("bravo"))))
.document(operationBuilder("path-parameters-with-custom-descriptor-attributes")
.attribute("org.springframework.restdocs.urlTemplate", "/{a}/{b}")
.document(operationBuilder(
"path-parameters-with-custom-descriptor-attributes")
.attribute(
RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE,
"/{a}/{b}")
.attribute(TemplateEngine.class.getName(),
new MustacheTemplateEngine(resolver)).build());
}