Skip to content

Commit

Permalink
#566 - Introduced ResourceSupport.getLinks(…) to return all with a gi…
Browse files Browse the repository at this point in the history
…ven rel.

Related tickets: #542, #318, #319, #157.
  • Loading branch information
gregturn authored and odrotbohm committed Mar 26, 2017
1 parent 85682ea commit aafe1f2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/main/java/org/springframework/hateoas/ResourceSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,24 @@ public Link getLink(String rel) {
return null;
}

/**
* Returns all {@link Link}s with the given relation type.
*
* @return the links in a {@link List}
*/
public List<Link> getLinks(String rel) {

List<Link> relatedLinks = new ArrayList<Link>();

for (Link link : links) {
if (link.getRel().equals(rel)) {
relatedLinks.add(link);
}
}

return relatedLinks;
}

/*
* (non-Javadoc)
* @see java.lang.Object#toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.util.Arrays;

import org.hamcrest.Matchers;
import org.junit.Test;

/**
Expand All @@ -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
Expand All @@ -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.<Link>empty()));
}

@Test
Expand All @@ -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
Expand Down

0 comments on commit aafe1f2

Please sign in to comment.