#805 - Added Links.getRequiredLink(…).

This commit is contained in:
Oliver Drotbohm
2019-02-11 10:41:25 +01:00
parent a9bae74dcf
commit 1dea5f61b9
2 changed files with 32 additions and 0 deletions

View File

@@ -71,6 +71,19 @@ public class Links implements Iterable<Link> {
.filter(link -> link.getRel().equals(rel)).findFirst();
}
/**
* Returns the {@link Link} with the given relation.
*
* @param rel the relation type to lookup a link for.
* @return
* @throws IllegalArgumentException if no link with the given relation was present.
* @since 1.0
*/
public Link getRequiredLink(String rel) {
return getLink(rel) //
.orElseThrow(() -> new IllegalArgumentException(String.format("Couldn't find link with rel '%s'!", rel)));
}
/**
* Returns all {@link Links} with the given relation type.
*

View File

@@ -102,4 +102,23 @@ public class LinksUnitTest {
public void parsesLinksWithWhitespace() {
assertThat(Links.valueOf(WITH_WHITESPACE)).isEqualTo(reference);
}
@Test // #805
public void returnsRequiredLink() {
Link reference = new Link("http://localhost", "someRel");
Links links = new Links(reference);
assertThat(links.getRequiredLink("someRel")).isEqualTo(reference);
}
@Test // #805
public void rejectsMissingLinkWithIllegalArgumentException() {
Links links = new Links();
assertThatExceptionOfType(IllegalArgumentException.class) //
.isThrownBy(() -> links.getRequiredLink("self")) //
.withMessageContaining("self");
}
}