RestDocumentationResultActions->RestDocumentationResultHandler

Previously documentation was handled by using a ResultActions
implementation that had custom methods added to it.

Now documentation is handled using a ResultHandler. This has a few
advantages:

- Fit better with the MockMvc programming model. This is similar to
  andDo(print())
- Support for using alwaysDo
This commit is contained in:
Rob Winch
2014-11-17 12:28:11 -06:00
parent e98658266c
commit ff7119ec54
7 changed files with 297 additions and 333 deletions

View File

@@ -16,36 +16,26 @@
package org.springframework.restdocs.core;
import static org.springframework.restdocs.core.RestDocumentationResultHandlers.documentCurlRequest;
import static org.springframework.restdocs.core.RestDocumentationResultHandlers.documentCurlRequestAndResponse;
import static org.springframework.restdocs.core.RestDocumentationResultHandlers.documentCurlResponse;
import java.util.Map;
import org.springframework.test.web.servlet.ResultActions;
public class RestDocumentation {
public static RestDocumentationResultActions document(String outputDir,
ResultActions resultActions) throws Exception {
return new RestDocumentationResultActions(outputDir, resultActions)
.andDo(documentCurlRequest(outputDir).includeResponseHeaders())
.andDo(documentCurlResponse(outputDir).includeResponseHeaders())
.andDo(documentCurlRequestAndResponse(outputDir).includeResponseHeaders());
}
public static RestDocumentationResultHandler document(String outputDir) throws Exception {
return new RestDocumentationResultHandler(outputDir);
}
public static LinkDescriptor linkWithRel(String rel) {
return new LinkDescriptor(rel);
}
public static LinkDescriptor linkWithRel(String rel) {
return new LinkDescriptor(rel);
}
public static LinkExtractor halLinks() {
return new LinkExtractor() {
public static LinkExtractor halLinks() {
return new LinkExtractor() {
@SuppressWarnings("unchecked")
@Override
public Map<String, Object> extractLinks(Map<String, Object> responseJson) {
return (Map<String, Object>) responseJson.get("_links");
}
};
}
@SuppressWarnings("unchecked")
@Override
public Map<String, Object> extractLinks(Map<String, Object> responseJson) {
return (Map<String, Object>) responseJson.get("_links");
}
};
}
}

View File

@@ -1,63 +0,0 @@
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.restdocs.core;
import java.util.Arrays;
import org.springframework.restdocs.core.RestDocumentationResultHandlers.LinkDocumentingResultHandler;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.ResultHandler;
import org.springframework.test.web.servlet.ResultMatcher;
public class RestDocumentationResultActions implements ResultActions {
private final ResultActions delegate;
private final String outputDir;
public RestDocumentationResultActions(String outputDir, ResultActions delegate) {
this.outputDir = outputDir;
this.delegate = delegate;
}
@Override
public RestDocumentationResultActions andExpect(ResultMatcher matcher)
throws Exception {
this.delegate.andExpect(matcher);
return this;
}
@Override
public RestDocumentationResultActions andDo(ResultHandler handler) throws Exception {
this.delegate.andDo(handler);
return this;
}
@Override
public MvcResult andReturn() {
return this.delegate.andReturn();
}
public RestDocumentationResultActions andDocumentLinks(LinkExtractor linkExtractor, LinkDescriptor... descriptors)
throws Exception {
this.delegate.andDo(new LinkDocumentingResultHandler(this.outputDir, linkExtractor, Arrays
.asList(descriptors)));
return this;
}
}

View File

@@ -0,0 +1,52 @@
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.restdocs.core;
import static org.springframework.restdocs.core.RestDocumentationResultHandlers.documentCurlRequest;
import static org.springframework.restdocs.core.RestDocumentationResultHandlers.documentCurlRequestAndResponse;
import static org.springframework.restdocs.core.RestDocumentationResultHandlers.documentCurlResponse;
import java.util.Arrays;
import org.springframework.restdocs.core.RestDocumentationResultHandlers.LinkDocumentingResultHandler;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultHandler;
public class RestDocumentationResultHandler implements ResultHandler {
private final String outputDir;
private ResultHandler linkDocumentingResultHandler;
public RestDocumentationResultHandler(String outputDir) {
this.outputDir = outputDir;
}
@Override
public void handle(MvcResult result) throws Exception {
documentCurlRequest(outputDir).includeResponseHeaders().handle(result);
documentCurlResponse(outputDir).includeResponseHeaders().handle(result);
documentCurlRequestAndResponse(outputDir).includeResponseHeaders().handle(result);
if(linkDocumentingResultHandler != null) {
linkDocumentingResultHandler.handle(result);
}
}
public RestDocumentationResultHandler withLinks(LinkExtractor linkExtractor, LinkDescriptor... descriptors) {
linkDocumentingResultHandler = new LinkDocumentingResultHandler(outputDir, linkExtractor, Arrays.asList(descriptors));
return this;
}
}