diff --git a/rest-notes-spring-data-rest/src/documentation/java/com/example/notes/ApiDocumentation.java b/rest-notes-spring-data-rest/src/documentation/java/com/example/notes/ApiDocumentation.java index 106d5859..2c99a395 100644 --- a/rest-notes-spring-data-rest/src/documentation/java/com/example/notes/ApiDocumentation.java +++ b/rest-notes-spring-data-rest/src/documentation/java/com/example/notes/ApiDocumentation.java @@ -20,6 +20,7 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; import static org.springframework.restdocs.core.RestDocumentation.document; import static org.springframework.restdocs.core.RestDocumentation.linkWithRel; +import static org.springframework.restdocs.core.RestDocumentation.halLinks; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; @@ -95,7 +96,8 @@ public class ApiDocumentation { public void indexExample() throws Exception { document("index-example", this.mockMvc.perform(get("/")).andExpect(status().isOk())) - .andDocumentHalLinks( + .andDocumentLinks( + halLinks(), linkWithRel("notes").description( "The <>"), linkWithRel("tags").description( @@ -173,7 +175,8 @@ public class ApiDocumentation { .andExpect(jsonPath("body", is(note.get("body")))) .andExpect(jsonPath("_links.self.href", is(noteLocation))) .andExpect(jsonPath("_links.tags", is(notNullValue()))) - .andDocumentHalLinks( + .andDocumentLinks( + halLinks(), linkWithRel("self").description("This <>"), linkWithRel("tags").description( "This note's <>")); @@ -261,7 +264,8 @@ public class ApiDocumentation { document("tag-get-example", this.mockMvc.perform(get(tagLocation))) .andExpect(status().isOk()) .andExpect(jsonPath("name", is(tag.get("name")))) - .andDocumentHalLinks( + .andDocumentLinks( + halLinks(), linkWithRel("self").description("This <>"), linkWithRel("notes") .description( diff --git a/rest-notes-spring-hateoas/src/documentation/java/com/example/notes/ApiDocumentation.java b/rest-notes-spring-hateoas/src/documentation/java/com/example/notes/ApiDocumentation.java index 2be15399..dfa12050 100644 --- a/rest-notes-spring-hateoas/src/documentation/java/com/example/notes/ApiDocumentation.java +++ b/rest-notes-spring-hateoas/src/documentation/java/com/example/notes/ApiDocumentation.java @@ -19,6 +19,7 @@ package com.example.notes; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; import static org.springframework.restdocs.core.RestDocumentation.document; +import static org.springframework.restdocs.core.RestDocumentation.halLinks; import static org.springframework.restdocs.core.RestDocumentation.linkWithRel; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; @@ -94,8 +95,9 @@ public class ApiDocumentation { @Test public void indexExample() throws Exception { document("index-example", - this.mockMvc.perform(get("/")).andExpect(status().isOk())) - .andDocumentHalLinks( + this.mockMvc.perform(get("/")) + .andExpect(status().isOk())) + .andDocumentLinks(halLinks(), linkWithRel("notes").description( "The <>"), linkWithRel("tags").description( @@ -171,7 +173,7 @@ public class ApiDocumentation { .andExpect(jsonPath("body", is(note.get("body")))) .andExpect(jsonPath("_links.self.href", is(noteLocation))) .andExpect(jsonPath("_links.note-tags", is(notNullValue()))) - .andDocumentHalLinks( + .andDocumentLinks(halLinks(), linkWithRel("self").description("This <>"), linkWithRel("note-tags").description( "This note's <>")); @@ -259,7 +261,7 @@ public class ApiDocumentation { document("tag-get-example", this.mockMvc.perform(get(tagLocation))) .andExpect(status().isOk()) .andExpect(jsonPath("name", is(tag.get("name")))) - .andDocumentHalLinks( + .andDocumentLinks(halLinks(), linkWithRel("self").description("This <>"), linkWithRel("tagged-notes") .description( diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/core/LinkExtractor.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/core/LinkExtractor.java new file mode 100644 index 00000000..3df9e7b8 --- /dev/null +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/core/LinkExtractor.java @@ -0,0 +1,25 @@ +/* + * 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.Map; + +public interface LinkExtractor { + + Map extractLinks(Map responseJson); + +} diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/core/RestDocumentation.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/core/RestDocumentation.java index 950e79bf..effbf6a0 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/core/RestDocumentation.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/core/RestDocumentation.java @@ -20,6 +20,8 @@ import static org.springframework.restdocs.core.RestDocumentationResultHandlers. 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 { @@ -35,4 +37,15 @@ public class RestDocumentation { public static LinkDescriptor linkWithRel(String rel) { return new LinkDescriptor(rel); } + + public static LinkExtractor halLinks() { + return new LinkExtractor() { + + @SuppressWarnings("unchecked") + @Override + public Map extractLinks(Map responseJson) { + return (Map) responseJson.get("_links"); + } + }; + } } diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/core/RestDocumentationResultActions.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/core/RestDocumentationResultActions.java index 621490fa..670a32e7 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/core/RestDocumentationResultActions.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/core/RestDocumentationResultActions.java @@ -53,9 +53,9 @@ public class RestDocumentationResultActions implements ResultActions { return this.delegate.andReturn(); } - public RestDocumentationResultActions andDocumentHalLinks(LinkDescriptor... descriptors) + public RestDocumentationResultActions andDocumentLinks(LinkExtractor linkExtractor, LinkDescriptor... descriptors) throws Exception { - this.delegate.andDo(new LinkDocumentingResultHandler(this.outputDir, Arrays + this.delegate.andDo(new LinkDocumentingResultHandler(this.outputDir, linkExtractor, Arrays .asList(descriptors))); return this; } diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/core/RestDocumentationResultHandlers.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/core/RestDocumentationResultHandlers.java index d5920c5f..2745029a 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/core/RestDocumentationResultHandlers.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/core/RestDocumentationResultHandlers.java @@ -240,8 +240,11 @@ public abstract class RestDocumentationResultHandlers { private final Map descriptorsByRel = new HashMap(); - public LinkDocumentingResultHandler(String outputDir, List descriptors) { + private final LinkExtractor extractor; + + public LinkDocumentingResultHandler(String outputDir, LinkExtractor linkExtractor, List descriptors) { super(outputDir, "links"); + this.extractor = linkExtractor; for (LinkDescriptor descriptor: descriptors) { Assert.hasText(descriptor.getRel()); Assert.hasText(descriptor.getDescription()); @@ -253,7 +256,7 @@ public abstract class RestDocumentationResultHandlers { @Override void handle(MvcResult result, DocumentationWriter writer) throws Exception { Map json = this.objectMapper.readValue(result.getResponse().getContentAsString(), Map.class); - Map links = (Map) json.get("_links"); + Map links = this.extractor.extractLinks(json); Set actualRels = links.keySet(); Set expectedRels = this.descriptorsByRel.keySet();