Isolate use of TemplateEngine into existing common base class
This commit is contained in:
@@ -25,7 +25,6 @@ import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.springframework.restdocs.snippet.SnippetWritingResultHandler;
|
||||
import org.springframework.restdocs.templates.TemplateEngine;
|
||||
import org.springframework.restdocs.util.DocumentableHttpServletRequest;
|
||||
import org.springframework.test.web.servlet.MvcResult;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -76,15 +75,10 @@ public abstract class CurlDocumentation {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(MvcResult result, PrintWriter writer) throws IOException {
|
||||
Map<String, Object> context = new HashMap<String, Object>();
|
||||
context.put("arguments", getCurlCommandArguments(result));
|
||||
context.putAll(getAttributes());
|
||||
|
||||
TemplateEngine templateEngine = (TemplateEngine) result.getRequest()
|
||||
.getAttribute(TemplateEngine.class.getName());
|
||||
|
||||
writer.print(templateEngine.compileTemplate("curl-request").render(context));
|
||||
public Map<String, Object> doHandle(MvcResult result) throws IOException {
|
||||
Map<String, Object> model = new HashMap<String, Object>();
|
||||
model.put("arguments", getCurlCommandArguments(result));
|
||||
return model;
|
||||
}
|
||||
|
||||
private String getCurlCommandArguments(MvcResult result) throws IOException {
|
||||
|
||||
@@ -29,7 +29,6 @@ import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.restdocs.snippet.SnippetWritingResultHandler;
|
||||
import org.springframework.restdocs.templates.TemplateEngine;
|
||||
import org.springframework.restdocs.util.DocumentableHttpServletRequest;
|
||||
import org.springframework.test.web.servlet.MvcResult;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -87,20 +86,15 @@ public abstract class HttpDocumentation {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(MvcResult result, PrintWriter writer) throws IOException {
|
||||
public Map<String, Object> doHandle(MvcResult result) throws IOException {
|
||||
DocumentableHttpServletRequest request = new DocumentableHttpServletRequest(
|
||||
result.getRequest());
|
||||
Map<String, Object> context = new HashMap<String, Object>();
|
||||
context.put("method", result.getRequest().getMethod());
|
||||
context.put("path", request.getRequestUriWithQueryString());
|
||||
context.put("headers", getHeaders(request));
|
||||
context.put("requestBody", getRequestBody(request));
|
||||
context.putAll(getAttributes());
|
||||
|
||||
TemplateEngine templateEngine = (TemplateEngine) result.getRequest()
|
||||
.getAttribute(TemplateEngine.class.getName());
|
||||
|
||||
writer.print(templateEngine.compileTemplate("http-request").render(context));
|
||||
Map<String, Object> model = new HashMap<String, Object>();
|
||||
model.put("method", result.getRequest().getMethod());
|
||||
model.put("path", request.getRequestUriWithQueryString());
|
||||
model.put("headers", getHeaders(request));
|
||||
model.put("requestBody", getRequestBody(request));
|
||||
return model;
|
||||
}
|
||||
|
||||
private List<Map<String, String>> getHeaders(
|
||||
@@ -225,33 +219,28 @@ public abstract class HttpDocumentation {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(MvcResult result, PrintWriter writer) throws IOException {
|
||||
public Map<String, Object> doHandle(MvcResult result) throws IOException {
|
||||
HttpStatus status = HttpStatus.valueOf(result.getResponse().getStatus());
|
||||
Map<String, Object> context = new HashMap<String, Object>();
|
||||
context.put(
|
||||
Map<String, Object> model = new HashMap<String, Object>();
|
||||
model.put(
|
||||
"responseBody",
|
||||
StringUtils.hasLength(result.getResponse().getContentAsString()) ? String
|
||||
.format("%n%s", result.getResponse().getContentAsString())
|
||||
: "");
|
||||
context.put("statusCode", status.value());
|
||||
context.put("statusReason", status.getReasonPhrase());
|
||||
model.put("statusCode", status.value());
|
||||
model.put("statusReason", status.getReasonPhrase());
|
||||
model.put("headers", headers(result));
|
||||
return model;
|
||||
}
|
||||
|
||||
private List<Map<String, String>> headers(MvcResult result) {
|
||||
List<Map<String, String>> headers = new ArrayList<>();
|
||||
context.put("headers", headers);
|
||||
|
||||
for (String headerName : result.getResponse().getHeaderNames()) {
|
||||
for (String header : result.getResponse().getHeaders(headerName)) {
|
||||
headers.add(header(headerName, header));
|
||||
}
|
||||
}
|
||||
|
||||
context.putAll(getAttributes());
|
||||
|
||||
TemplateEngine templateEngine = (TemplateEngine) result.getRequest()
|
||||
.getAttribute(TemplateEngine.class.getName());
|
||||
|
||||
writer.print(templateEngine.compileTemplate("http-response").render(context));
|
||||
|
||||
return headers;
|
||||
}
|
||||
|
||||
private Map<String, String> header(String name, String value) {
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package org.springframework.restdocs.hypermedia;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
@@ -29,7 +28,6 @@ import java.util.Set;
|
||||
|
||||
import org.springframework.restdocs.snippet.SnippetGenerationException;
|
||||
import org.springframework.restdocs.snippet.SnippetWritingResultHandler;
|
||||
import org.springframework.restdocs.templates.TemplateEngine;
|
||||
import org.springframework.test.web.servlet.MvcResult;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
@@ -62,9 +60,11 @@ public class LinkSnippetResultHandler extends SnippetWritingResultHandler {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handle(MvcResult result, PrintWriter writer) throws IOException {
|
||||
protected Map<String, Object> doHandle(MvcResult result) throws IOException {
|
||||
validate(extractLinks(result));
|
||||
writeDocumentationSnippet(result, writer);
|
||||
Map<String, Object> model = new HashMap<>();
|
||||
model.put("links", createLinksModel());
|
||||
return model;
|
||||
}
|
||||
|
||||
private Map<String, List<Link>> extractLinks(MvcResult result) throws IOException {
|
||||
@@ -111,16 +111,6 @@ public class LinkSnippetResultHandler extends SnippetWritingResultHandler {
|
||||
}
|
||||
}
|
||||
|
||||
private void writeDocumentationSnippet(MvcResult result, PrintWriter writer)
|
||||
throws IOException {
|
||||
TemplateEngine templateEngine = (TemplateEngine) result.getRequest()
|
||||
.getAttribute(TemplateEngine.class.getName());
|
||||
Map<String, Object> context = new HashMap<>();
|
||||
context.put("links", createLinksModel());
|
||||
context.putAll(getAttributes());
|
||||
writer.print(templateEngine.compileTemplate("links").render(context));
|
||||
}
|
||||
|
||||
private List<Map<String, Object>> createLinksModel() {
|
||||
List<Map<String, Object>> model = new ArrayList<>();
|
||||
for (Entry<String, LinkDescriptor> entry : this.descriptorsByRel.entrySet()) {
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package org.springframework.restdocs.payload;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.Reader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
@@ -27,7 +26,6 @@ import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.springframework.restdocs.snippet.SnippetWritingResultHandler;
|
||||
import org.springframework.restdocs.templates.TemplateEngine;
|
||||
import org.springframework.test.web.servlet.MvcResult;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
@@ -50,14 +48,11 @@ public abstract class FieldSnippetResultHandler extends SnippetWritingResultHand
|
||||
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
private final String templateName;
|
||||
|
||||
private List<FieldDescriptor> fieldDescriptors;
|
||||
|
||||
FieldSnippetResultHandler(String identifier, String type,
|
||||
Map<String, Object> attributes, List<FieldDescriptor> descriptors) {
|
||||
super(identifier, type + "-fields", attributes);
|
||||
this.templateName = type + "-fields";
|
||||
for (FieldDescriptor descriptor : descriptors) {
|
||||
Assert.notNull(descriptor.getPath());
|
||||
Assert.hasText(descriptor.getDescription());
|
||||
@@ -67,15 +62,12 @@ public abstract class FieldSnippetResultHandler extends SnippetWritingResultHand
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handle(MvcResult result, PrintWriter writer) throws IOException {
|
||||
|
||||
protected Map<String, Object> doHandle(MvcResult result) throws IOException {
|
||||
this.fieldValidator.validate(getPayloadReader(result), this.fieldDescriptors);
|
||||
|
||||
Object payload = extractPayload(result);
|
||||
|
||||
Map<String, Object> context = new HashMap<>();
|
||||
Map<String, Object> model = new HashMap<>();
|
||||
List<Map<String, Object>> fields = new ArrayList<>();
|
||||
context.put("fields", fields);
|
||||
model.put("fields", fields);
|
||||
for (Entry<String, FieldDescriptor> entry : this.descriptorsByPath.entrySet()) {
|
||||
FieldDescriptor descriptor = entry.getValue();
|
||||
if (descriptor.getType() == null) {
|
||||
@@ -83,10 +75,7 @@ public abstract class FieldSnippetResultHandler extends SnippetWritingResultHand
|
||||
}
|
||||
fields.add(descriptor.toModel());
|
||||
}
|
||||
context.putAll(getAttributes());
|
||||
TemplateEngine templateEngine = (TemplateEngine) result.getRequest()
|
||||
.getAttribute(TemplateEngine.class.getName());
|
||||
writer.print(templateEngine.compileTemplate(this.templateName).render(context));
|
||||
return model;
|
||||
}
|
||||
|
||||
private FieldType getFieldType(FieldDescriptor descriptor, Object payload) {
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package org.springframework.restdocs.request;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
@@ -29,7 +28,6 @@ import java.util.Set;
|
||||
|
||||
import org.springframework.restdocs.snippet.SnippetGenerationException;
|
||||
import org.springframework.restdocs.snippet.SnippetWritingResultHandler;
|
||||
import org.springframework.restdocs.templates.TemplateEngine;
|
||||
import org.springframework.test.web.servlet.MvcResult;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
@@ -54,9 +52,16 @@ public class QueryParametersSnippetResultHandler extends SnippetWritingResultHan
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handle(MvcResult result, PrintWriter writer) throws IOException {
|
||||
protected Map<String, Object> doHandle(MvcResult result) throws IOException {
|
||||
verifyParameterDescriptors(result);
|
||||
documentParameters(result, writer);
|
||||
|
||||
Map<String, Object> model = new HashMap<>();
|
||||
List<Map<String, Object>> parameters = new ArrayList<>();
|
||||
for (Entry<String, ParameterDescriptor> entry : this.descriptorsByName.entrySet()) {
|
||||
parameters.add(entry.getValue().toModel());
|
||||
}
|
||||
model.put("parameters", parameters);
|
||||
return model;
|
||||
}
|
||||
|
||||
private void verifyParameterDescriptors(MvcResult result) {
|
||||
@@ -88,18 +93,4 @@ public class QueryParametersSnippetResultHandler extends SnippetWritingResultHan
|
||||
Assert.isTrue(actualParameters.equals(expectedParameters));
|
||||
}
|
||||
|
||||
private void documentParameters(MvcResult result, PrintWriter writer)
|
||||
throws IOException {
|
||||
TemplateEngine templateEngine = (TemplateEngine) result.getRequest()
|
||||
.getAttribute(TemplateEngine.class.getName());
|
||||
Map<String, Object> context = new HashMap<>();
|
||||
List<Map<String, Object>> parameters = new ArrayList<>();
|
||||
for (Entry<String, ParameterDescriptor> entry : this.descriptorsByName.entrySet()) {
|
||||
parameters.add(entry.getValue().toModel());
|
||||
}
|
||||
context.put("parameters", parameters);
|
||||
context.putAll(getAttributes());
|
||||
writer.print(templateEngine.compileTemplate("query-parameters").render(context));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -17,11 +17,11 @@
|
||||
package org.springframework.restdocs.snippet;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.Writer;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.restdocs.templates.TemplateEngine;
|
||||
import org.springframework.test.web.servlet.MvcResult;
|
||||
import org.springframework.test.web.servlet.ResultHandler;
|
||||
|
||||
@@ -47,20 +47,19 @@ public abstract class SnippetWritingResultHandler implements ResultHandler {
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract void handle(MvcResult result, PrintWriter writer)
|
||||
throws IOException;
|
||||
|
||||
@Override
|
||||
public void handle(MvcResult result) throws IOException {
|
||||
WriterResolver writerResolver = (WriterResolver) result.getRequest()
|
||||
.getAttribute(WriterResolver.class.getName());
|
||||
try (Writer writer = writerResolver.resolve(this.identifier, this.snippetName)) {
|
||||
handle(result, new PrintWriter(writer));
|
||||
Map<String, Object> model = doHandle(result);
|
||||
model.putAll(this.attributes);
|
||||
TemplateEngine templateEngine = (TemplateEngine) result.getRequest()
|
||||
.getAttribute(TemplateEngine.class.getName());
|
||||
writer.append(templateEngine.compileTemplate(this.snippetName).render(model));
|
||||
}
|
||||
}
|
||||
|
||||
protected Map<String, Object> getAttributes() {
|
||||
return this.attributes;
|
||||
}
|
||||
protected abstract Map<String, Object> doHandle(MvcResult result) throws IOException;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user