Skip to content

Commit aafe1f2

Browse files
gregturnodrotbohm
authored andcommitted
#566 - Introduced ResourceSupport.getLinks(…) to return all with a given rel.
Related tickets: #542, #318, #319, #157.
1 parent 85682ea commit aafe1f2

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/main/java/org/springframework/hateoas/ResourceSupport.java

+18
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,24 @@ public Link getLink(String rel) {
133133
return null;
134134
}
135135

136+
/**
137+
* Returns all {@link Link}s with the given relation type.
138+
*
139+
* @return the links in a {@link List}
140+
*/
141+
public List<Link> getLinks(String rel) {
142+
143+
List<Link> relatedLinks = new ArrayList<Link>();
144+
145+
for (Link link : links) {
146+
if (link.getRel().equals(rel)) {
147+
relatedLinks.add(link);
148+
}
149+
}
150+
151+
return relatedLinks;
152+
}
153+
136154
/*
137155
* (non-Javadoc)
138156
* @see java.lang.Object#toString()

src/test/java/org/springframework/hateoas/ResourceSupportUnitTest.java

+19
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import java.util.Arrays;
2222

23+
import org.hamcrest.Matchers;
2324
import org.junit.Test;
2425

2526
/**
@@ -36,6 +37,7 @@ public void setsUpWithEmptyLinkList() {
3637
assertThat(support.hasLinks(), is(false));
3738
assertThat(support.hasLink(Link.REL_SELF), is(false));
3839
assertThat(support.getLinks().isEmpty(), is(true));
40+
assertThat(support.getLinks(Link.REL_SELF).isEmpty(), is(true));
3941
}
4042

4143
@Test
@@ -49,6 +51,21 @@ public void addsLinkCorrectly() {
4951
assertThat(support.hasLinks(), is(true));
5052
assertThat(support.hasLink(link.getRel()), is(true));
5153
assertThat(support.getLink(link.getRel()), is(link));
54+
assertThat(support.getLinks(Link.REL_NEXT), contains(link));
55+
}
56+
57+
@Test
58+
public void addsMultipleLinkRelationsCorrectly() {
59+
60+
Link link = new Link("/customers/1", "customers");
61+
Link link2 = new Link("/orders/1/customer", "customers");
62+
ResourceSupport support = new ResourceSupport();
63+
support.add(link, link2);
64+
65+
assertThat(support.getLinks("customers").size(), is(2));
66+
assertThat(support.getLinks("customers"), contains(link, link2));
67+
assertThat(support.getLinks("non-existent").size(), is(0));
68+
assertThat(support.getLinks("non-existent"), is(Matchers.<Link>empty()));
5269
}
5370

5471
@Test
@@ -64,6 +81,8 @@ public void addsLinksCorrectly() {
6481
assertThat(support.hasLinks(), is(true));
6582
assertThat(support.getLinks(), hasItems(first, second));
6683
assertThat(support.getLinks().size(), is(2));
84+
assertThat(support.getLinks(Link.REL_PREVIOUS), contains(first));
85+
assertThat(support.getLinks(Link.REL_NEXT), contains(second));
6786
}
6887

6988
@Test

0 commit comments

Comments
 (0)