#671 - Polishing.

Introduced Link.hasRel(…) and use that to filter link lookups in ResourceSupport.
This commit is contained in:
Oliver Gierke
2017-11-29 12:00:02 +01:00
parent d40bda2337
commit 79ebf9b5a4
3 changed files with 39 additions and 2 deletions

View File

@@ -168,6 +168,19 @@ public class Link implements Serializable {
return new Link(getUriTemplate().expand(arguments).toString(), getRel());
}
/**
* Returns whether the current {@link Link} has the given link relation.
*
* @param rel must not be {@literal null} or empty.
* @return
*/
public boolean hasRel(String rel) {
Assert.hasText(rel, "Link relation must not be null or empty!");
return this.rel.equals(rel);
}
private UriTemplate getUriTemplate() {
if (template == null) {

View File

@@ -126,7 +126,7 @@ public class ResourceSupport implements Identifiable<Link> {
public Optional<Link> getLink(String rel) {
return links.stream() //
.filter(link -> link.getRel().equals(rel)) //
.filter(link -> link.hasRel(rel)) //
.findFirst();
}
@@ -151,7 +151,7 @@ public class ResourceSupport implements Identifiable<Link> {
public List<Link> getLinks(String rel) {
return links.stream() //
.filter(link -> link.getRel().equals(rel)) //
.filter(link -> link.hasRel(rel)) //
.collect(Collectors.toList());
}

View File

@@ -223,4 +223,28 @@ public class LinkUnitTest {
assertThat(Link.valueOf("<http://localhost>; rel=\"http://acme.com/rels/foo-bar\"").getRel()) //
.isEqualTo("http://acme.com/rels/foo-bar");
}
/**
* @see #671
*/
@Test
public void exposesLinkRelation() {
Link link = new Link("/", "foo");
assertThat(link.hasRel("foo")).isTrue();
assertThat(link.hasRel("bar")).isFalse();
}
/**
* @see #671
*/
@Test
public void rejectsInvalidRelationsOnHasRel() {
Link link = new Link("/");
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> link.hasRel(null));
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> link.hasRel(""));
}
}