From 0e8b0e13cac1ff3bc36fbefa25168e404f398795 Mon Sep 17 00:00:00 2001 From: Greg Turnquist Date: Wed, 22 Mar 2017 13:31:29 -0500 Subject: [PATCH] #566 - Return list of links for a given rel Introduces `ResourceSupport.getLinks(String rel)` to allow returning ALL links related to a give rel. Resolves #542,#318,#319,#157 --- .../hateoas/ResourceSupport.java | 364 +++++++++--------- .../hateoas/ResourceSupportUnitTest.java | 19 + 2 files changed, 210 insertions(+), 173 deletions(-) diff --git a/src/main/java/org/springframework/hateoas/ResourceSupport.java b/src/main/java/org/springframework/hateoas/ResourceSupport.java index 64de72076..9806bcaea 100755 --- a/src/main/java/org/springframework/hateoas/ResourceSupport.java +++ b/src/main/java/org/springframework/hateoas/ResourceSupport.java @@ -1,173 +1,191 @@ -/* - * Copyright 2012-2015 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.hateoas; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import javax.xml.bind.annotation.XmlElement; - -import org.springframework.util.Assert; - -import com.fasterxml.jackson.annotation.JsonIgnore; -import com.fasterxml.jackson.annotation.JsonProperty; - -/** - * Base class for DTOs to collect links. - * - * @author Oliver Gierke - */ -public class ResourceSupport implements Identifiable { - - private final List links; - - public ResourceSupport() { - this.links = new ArrayList(); - } - - /** - * Returns the {@link Link} with a rel of {@link Link#REL_SELF}. - */ - @JsonIgnore - public Link getId() { - return getLink(Link.REL_SELF); - } - - /** - * Adds the given link to the resource. - * - * @param link - */ - public void add(Link link) { - Assert.notNull(link, "Link must not be null!"); - this.links.add(link); - } - - /** - * Adds all given {@link Link}s to the resource. - * - * @param links - */ - public void add(Iterable links) { - Assert.notNull(links, "Given links must not be null!"); - for (Link candidate : links) { - add(candidate); - } - } - - /** - * Adds all given {@link Link}s to the resource. - * - * @param links must not be {@literal null}. - */ - public void add(Link... links) { - Assert.notNull(links, "Given links must not be null!"); - add(Arrays.asList(links)); - } - - /** - * Returns whether the resource contains {@link Link}s at all. - * - * @return - */ - public boolean hasLinks() { - return !this.links.isEmpty(); - } - - /** - * Returns whether the resource contains a {@link Link} with the given rel. - * - * @param rel - * @return - */ - public boolean hasLink(String rel) { - return getLink(rel) != null; - } - - /** - * Returns all {@link Link}s contained in this resource. - * - * @return - */ - @XmlElement(name = "link", namespace = Link.ATOM_NAMESPACE) - @JsonProperty("links") - public List getLinks() { - return links; - } - - /** - * Removes all {@link Link}s added to the resource so far. - */ - public void removeLinks() { - this.links.clear(); - } - - /** - * Returns the link with the given rel. - * - * @param rel - * @return the link with the given rel or {@literal null} if none found. - */ - public Link getLink(String rel) { - - for (Link link : links) { - if (link.getRel().equals(rel)) { - return link; - } - } - - return null; - } - - /* - * (non-Javadoc) - * @see java.lang.Object#toString() - */ - @Override - public String toString() { - return String.format("links: %s", links.toString()); - } - - /* - * (non-Javadoc) - * @see java.lang.Object#equals(java.lang.Object) - */ - @Override - public boolean equals(Object obj) { - - if (this == obj) { - return true; - } - - if (obj == null || !obj.getClass().equals(this.getClass())) { - return false; - } - - ResourceSupport that = (ResourceSupport) obj; - - return this.links.equals(that.links); - } - - /* - * (non-Javadoc) - * @see java.lang.Object#hashCode() - */ - @Override - public int hashCode() { - return this.links.hashCode(); - } -} +/* + * Copyright 2012-2015 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.hateoas; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import javax.xml.bind.annotation.XmlElement; + +import org.springframework.util.Assert; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * Base class for DTOs to collect links. + * + * @author Oliver Gierke + */ +public class ResourceSupport implements Identifiable { + + private final List links; + + public ResourceSupport() { + this.links = new ArrayList(); + } + + /** + * Returns the {@link Link} with a rel of {@link Link#REL_SELF}. + */ + @JsonIgnore + public Link getId() { + return getLink(Link.REL_SELF); + } + + /** + * Adds the given link to the resource. + * + * @param link + */ + public void add(Link link) { + Assert.notNull(link, "Link must not be null!"); + this.links.add(link); + } + + /** + * Adds all given {@link Link}s to the resource. + * + * @param links + */ + public void add(Iterable links) { + Assert.notNull(links, "Given links must not be null!"); + for (Link candidate : links) { + add(candidate); + } + } + + /** + * Adds all given {@link Link}s to the resource. + * + * @param links must not be {@literal null}. + */ + public void add(Link... links) { + Assert.notNull(links, "Given links must not be null!"); + add(Arrays.asList(links)); + } + + /** + * Returns whether the resource contains {@link Link}s at all. + * + * @return + */ + public boolean hasLinks() { + return !this.links.isEmpty(); + } + + /** + * Returns whether the resource contains a {@link Link} with the given rel. + * + * @param rel + * @return + */ + public boolean hasLink(String rel) { + return getLink(rel) != null; + } + + /** + * Returns all {@link Link}s contained in this resource. + * + * @return + */ + @XmlElement(name = "link", namespace = Link.ATOM_NAMESPACE) + @JsonProperty("links") + public List getLinks() { + return links; + } + + /** + * Removes all {@link Link}s added to the resource so far. + */ + public void removeLinks() { + this.links.clear(); + } + + /** + * Returns the link with the given rel. + * + * @param rel + * @return the link with the given rel or {@literal null} if none found. + */ + public Link getLink(String rel) { + + for (Link link : links) { + if (link.getRel().equals(rel)) { + return link; + } + } + + return null; + } + + /** + * Returns all {@link Link}s with the given relation type. + * + * @return the links in a {@link List} + */ + public List getLinks(String rel) { + + List relatedLinks = new ArrayList(); + + for (Link link : links) { + if (link.getRel().equals(rel)) { + relatedLinks.add(link); + } + } + + return relatedLinks; + } + + /* + * (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return String.format("links: %s", links.toString()); + } + + /* + * (non-Javadoc) + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + + if (this == obj) { + return true; + } + + if (obj == null || !obj.getClass().equals(this.getClass())) { + return false; + } + + ResourceSupport that = (ResourceSupport) obj; + + return this.links.equals(that.links); + } + + /* + * (non-Javadoc) + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + return this.links.hashCode(); + } +} diff --git a/src/test/java/org/springframework/hateoas/ResourceSupportUnitTest.java b/src/test/java/org/springframework/hateoas/ResourceSupportUnitTest.java index a30cc72af..06877c4c2 100644 --- a/src/test/java/org/springframework/hateoas/ResourceSupportUnitTest.java +++ b/src/test/java/org/springframework/hateoas/ResourceSupportUnitTest.java @@ -20,6 +20,7 @@ import java.util.Arrays; +import org.hamcrest.Matchers; import org.junit.Test; /** @@ -36,6 +37,7 @@ public void setsUpWithEmptyLinkList() { assertThat(support.hasLinks(), is(false)); assertThat(support.hasLink(Link.REL_SELF), is(false)); assertThat(support.getLinks().isEmpty(), is(true)); + assertThat(support.getLinks(Link.REL_SELF).isEmpty(), is(true)); } @Test @@ -49,6 +51,21 @@ public void addsLinkCorrectly() { assertThat(support.hasLinks(), is(true)); assertThat(support.hasLink(link.getRel()), is(true)); assertThat(support.getLink(link.getRel()), is(link)); + assertThat(support.getLinks(Link.REL_NEXT), contains(link)); + } + + @Test + public void addsMultipleLinkRelationsCorrectly() { + + Link link = new Link("/customers/1", "customers"); + Link link2 = new Link("/orders/1/customer", "customers"); + ResourceSupport support = new ResourceSupport(); + support.add(link, link2); + + assertThat(support.getLinks("customers").size(), is(2)); + assertThat(support.getLinks("customers"), contains(link, link2)); + assertThat(support.getLinks("non-existent").size(), is(0)); + assertThat(support.getLinks("non-existent"), is(Matchers.empty())); } @Test @@ -64,6 +81,8 @@ public void addsLinksCorrectly() { assertThat(support.hasLinks(), is(true)); assertThat(support.getLinks(), hasItems(first, second)); assertThat(support.getLinks().size(), is(2)); + assertThat(support.getLinks(Link.REL_PREVIOUS), contains(first)); + assertThat(support.getLinks(Link.REL_NEXT), contains(second)); } @Test