Make the ordering of extracted links match the ordering in the payload

Closes gh-63
This commit is contained in:
Andy Wilkinson
2015-04-29 10:05:42 +01:00
parent bad5bdabaa
commit 37f9f02580
3 changed files with 16 additions and 32 deletions

View File

@@ -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<String, List<Link>> extractLinks(Map<String, Object> json) {
Map<String, List<Link>> extractedLinks = new HashMap<>();
Map<String, List<Link>> extractedLinks = new LinkedHashMap<>();
Object possibleLinks = json.get("_links");
if (possibleLinks instanceof Map) {
Map<String, Object> links = (Map<String, Object>) possibleLinks;
@@ -152,7 +154,7 @@ public abstract class LinkExtractors {
@Override
public Map<String, List<Link>> extractLinks(Map<String, Object> json) {
Map<String, List<Link>> extractedLinks = new HashMap<>();
MultiValueMap<String, Link> extractedLinks = new LinkedMultiValueMap<>();
Object possibleLinks = json.get("links");
if (possibleLinks instanceof Collection) {
Collection<Object> linksCollection = (Collection<Object>) possibleLinks;
@@ -176,14 +178,9 @@ public abstract class LinkExtractors {
}
private static void maybeStoreLink(Link link,
Map<String, List<Link>> extractedLinks) {
MultiValueMap<String, Link> extractedLinks) {
if (link != null) {
List<Link> linksForRel = extractedLinks.get(link.getRel());
if (linksForRel == null) {
linksForRel = new ArrayList<Link>();
extractedLinks.put(link.getRel(), linksForRel);
}
linksForRel.add(link);
extractedLinks.add(link.getRel(), link);
}
}
}

View File

@@ -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<String, List<Link>> linksByRel = new HashMap<String, List<Link>>();
private MultiValueMap<String, Link> linksByRel = new LinkedMultiValueMap<String, Link>();
@Override
public Map<String, List<Link>> extractLinks(MockHttpServletResponse response)
public MultiValueMap<String, Link> extractLinks(MockHttpServletResponse response)
throws IOException {
return this.linksByRel;
}
private StubLinkExtractor withLinks(Link... links) {
for (Link link : links) {
List<Link> linksWithRel = this.linksByRel.get(link.getRel());
if (linksWithRel == null) {
linksWithRel = new ArrayList<Link>();
this.linksByRel.put(link.getRel(), linksWithRel);
}
linksWithRel.add(link);
this.linksByRel.add(link.getRel(), link);
}
return this;
}

View File

@@ -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<Link> expectedLinks, Map<String, List<Link>> actualLinks) {
Map<String, List<Link>> expectedLinksByRel = new HashMap<>();
MultiValueMap<String, Link> expectedLinksByRel = new LinkedMultiValueMap<>();
for (Link expectedLink : expectedLinks) {
List<Link> 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);
}