diff --git a/spring-restdocs/src/main/java/org/springframework/restdocs/hypermedia/LinkExtractors.java b/spring-restdocs/src/main/java/org/springframework/restdocs/hypermedia/LinkExtractors.java index 806496c5..35c0f778 100644 --- a/spring-restdocs/src/main/java/org/springframework/restdocs/hypermedia/LinkExtractors.java +++ b/spring-restdocs/src/main/java/org/springframework/restdocs/hypermedia/LinkExtractors.java @@ -19,13 +19,15 @@ package org.springframework.restdocs.hypermedia; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; -import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.springframework.http.MediaType; import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; import org.springframework.util.StringUtils; import com.fasterxml.jackson.databind.ObjectMapper; @@ -107,7 +109,7 @@ public abstract class LinkExtractors { @Override public Map> extractLinks(Map json) { - Map> extractedLinks = new HashMap<>(); + Map> extractedLinks = new LinkedHashMap<>(); Object possibleLinks = json.get("_links"); if (possibleLinks instanceof Map) { Map links = (Map) possibleLinks; @@ -152,7 +154,7 @@ public abstract class LinkExtractors { @Override public Map> extractLinks(Map json) { - Map> extractedLinks = new HashMap<>(); + MultiValueMap extractedLinks = new LinkedMultiValueMap<>(); Object possibleLinks = json.get("links"); if (possibleLinks instanceof Collection) { Collection linksCollection = (Collection) possibleLinks; @@ -176,14 +178,9 @@ public abstract class LinkExtractors { } private static void maybeStoreLink(Link link, - Map> extractedLinks) { + MultiValueMap extractedLinks) { if (link != null) { - List linksForRel = extractedLinks.get(link.getRel()); - if (linksForRel == null) { - linksForRel = new ArrayList(); - extractedLinks.put(link.getRel(), linksForRel); - } - linksForRel.add(link); + extractedLinks.add(link.getRel(), link); } } } diff --git a/spring-restdocs/src/test/java/org/springframework/restdocs/hypermedia/HypermediaDocumentationTests.java b/spring-restdocs/src/test/java/org/springframework/restdocs/hypermedia/HypermediaDocumentationTests.java index 15073949..0eb38b2a 100644 --- a/spring-restdocs/src/test/java/org/springframework/restdocs/hypermedia/HypermediaDocumentationTests.java +++ b/spring-restdocs/src/test/java/org/springframework/restdocs/hypermedia/HypermediaDocumentationTests.java @@ -22,10 +22,6 @@ import static org.springframework.restdocs.test.SnippetMatchers.tableWithHeader; import static org.springframework.restdocs.test.StubMvcResult.result; import java.io.IOException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; import org.junit.Rule; import org.junit.Test; @@ -33,6 +29,8 @@ import org.junit.rules.ExpectedException; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.restdocs.RestDocumentationException; import org.springframework.restdocs.test.ExpectedSnippet; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; /** * Tests for {@link HypermediaDocumentation} @@ -92,22 +90,17 @@ public class HypermediaDocumentationTests { private static class StubLinkExtractor implements LinkExtractor { - private Map> linksByRel = new HashMap>(); + private MultiValueMap linksByRel = new LinkedMultiValueMap(); @Override - public Map> extractLinks(MockHttpServletResponse response) + public MultiValueMap extractLinks(MockHttpServletResponse response) throws IOException { return this.linksByRel; } private StubLinkExtractor withLinks(Link... links) { for (Link link : links) { - List linksWithRel = this.linksByRel.get(link.getRel()); - if (linksWithRel == null) { - linksWithRel = new ArrayList(); - this.linksByRel.put(link.getRel(), linksWithRel); - } - linksWithRel.add(link); + this.linksByRel.add(link.getRel(), link); } return this; } diff --git a/spring-restdocs/src/test/java/org/springframework/restdocs/hypermedia/LinkExtractorsPayloadTests.java b/spring-restdocs/src/test/java/org/springframework/restdocs/hypermedia/LinkExtractorsPayloadTests.java index eba107de..036aa1b5 100644 --- a/spring-restdocs/src/test/java/org/springframework/restdocs/hypermedia/LinkExtractorsPayloadTests.java +++ b/spring-restdocs/src/test/java/org/springframework/restdocs/hypermedia/LinkExtractorsPayloadTests.java @@ -21,11 +21,9 @@ import static org.junit.Assert.assertEquals; import java.io.File; import java.io.FileReader; import java.io.IOException; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; -import java.util.HashMap; import java.util.List; import java.util.Map; @@ -35,6 +33,8 @@ import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.util.FileCopyUtils; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; /** * Parameterized tests for {@link LinkExtractors} with various payloads. @@ -97,15 +97,9 @@ public class LinkExtractorsPayloadTests { } private void assertLinks(List expectedLinks, Map> actualLinks) { - Map> expectedLinksByRel = new HashMap<>(); + MultiValueMap expectedLinksByRel = new LinkedMultiValueMap<>(); for (Link expectedLink : expectedLinks) { - List expectedlinksWithRel = expectedLinksByRel.get(expectedLink - .getRel()); - if (expectedlinksWithRel == null) { - expectedlinksWithRel = new ArrayList<>(); - expectedLinksByRel.put(expectedLink.getRel(), expectedlinksWithRel); - } - expectedlinksWithRel.add(expectedLink); + expectedLinksByRel.add(expectedLink.getRel(), expectedLink); } assertEquals(expectedLinksByRel, actualLinks); }