/** * Ensure that every key present in the provided map matches the criterium. * * <p> * Any keys in the criterium are ignored (these were matched on during the * {@link #follow(String, Object...)} call). */ public boolean matches(final JsonRepresentation jsonRepr) { if (!isFollowing()) { return false; } if(criteriaSpecs == null) { return true; } for (PathNode criteriaSpec : criteriaSpecs) { if(criteriaSpec.matches(jsonRepr)) { return true; } } return false; }
/** * Ensure that every key present in the provided map matches the criterium. * * <p> * Any keys in the criterium are ignored (these were matched on during the * {@link #follow(String, Object...)} call). */ public boolean matches(final JsonRepresentation jsonRepr) { if (!isFollowing()) { return false; } if(criteriaSpecs == null) { return true; } for (PathNode criteriaSpec : criteriaSpecs) { if(criteriaSpec.matches(jsonRepr)) { return true; } } return false; }
@Test public void multiplePaths_withRelFullCriteria() throws Exception { final List<List<String>> links = asListOfLists("links[rel=urn:org.restfulobjects:rels/details;action=foo].x,links[rel=urn:org.restfulobjects:rels/details;action=bar].y"); final LinkFollowSpecs linkFollower = LinkFollowSpecs.create(links); LinkFollowSpecs followRelVersion = linkFollower.follow("links[rel=urn:org.restfulobjects:rels/details;action=foo]"); assertThat(followRelVersion.isFollowing(), is(true)); assertThat(followRelVersion.isTerminated(), is(false)); assertThat(followRelVersion.follow("x").isFollowing(), is(true)); LinkFollowSpecs followRelUser = linkFollower.follow("links[rel=urn:org.restfulobjects:rels/details;action=bar]"); assertThat(followRelUser.isFollowing(), is(true)); assertThat(followRelUser.isTerminated(), is(false)); assertThat(followRelUser.follow("y").isFollowing(), is(true)); }
@Test public void multiplePaths_withCriteria() throws Exception { final List<List<String>> links = asListOfLists("links[rel=urn:org.restfulobjects:rels/version].x,links[rel=urn:org.restfulobjects:rels/user].y"); final LinkFollowSpecs linkFollower = LinkFollowSpecs.create(links); LinkFollowSpecs followRelVersion = linkFollower.follow("links[rel=urn:org.restfulobjects:rels/version]"); assertThat(followRelVersion.isFollowing(), is(true)); assertThat(followRelVersion.isTerminated(), is(false)); assertThat(followRelVersion.follow("x").isFollowing(), is(true)); LinkFollowSpecs followRelUser = linkFollower.follow("links[rel=urn:org.restfulobjects:rels/user]"); assertThat(followRelUser.isFollowing(), is(true)); assertThat(followRelUser.isTerminated(), is(false)); assertThat(followRelUser.follow("y").isFollowing(), is(true)); }
@Test public void multiplePaths_withCriteria() throws Exception { final List<List<String>> links = asListOfLists("links[rel=urn:org.restfulobjects:rels/version].x,links[rel=urn:org.restfulobjects:rels/user].y"); final LinkFollowSpecs linkFollower = LinkFollowSpecs.create(links); LinkFollowSpecs followRelVersion = linkFollower.follow("links[rel=urn:org.restfulobjects:rels/version]"); assertThat(followRelVersion.isFollowing(), is(true)); assertThat(followRelVersion.isTerminated(), is(false)); assertThat(followRelVersion.follow("x").isFollowing(), is(true)); LinkFollowSpecs followRelUser = linkFollower.follow("links[rel=urn:org.restfulobjects:rels/user]"); assertThat(followRelUser.isFollowing(), is(true)); assertThat(followRelUser.isTerminated(), is(false)); assertThat(followRelUser.follow("y").isFollowing(), is(true)); }
@Test public void multiplePaths_withRelFullCriteria() throws Exception { final List<List<String>> links = asListOfLists("links[rel=urn:org.restfulobjects:rels/details;action=foo].x,links[rel=urn:org.restfulobjects:rels/details;action=bar].y"); final LinkFollowSpecs linkFollower = LinkFollowSpecs.create(links); LinkFollowSpecs followRelVersion = linkFollower.follow("links[rel=urn:org.restfulobjects:rels/details;action=foo]"); assertThat(followRelVersion.isFollowing(), is(true)); assertThat(followRelVersion.isTerminated(), is(false)); assertThat(followRelVersion.follow("x").isFollowing(), is(true)); LinkFollowSpecs followRelUser = linkFollower.follow("links[rel=urn:org.restfulobjects:rels/details;action=bar]"); assertThat(followRelUser.isFollowing(), is(true)); assertThat(followRelUser.isTerminated(), is(false)); assertThat(followRelUser.follow("y").isFollowing(), is(true)); }
@Test public void multiplePaths_withRelSimplifiedCriteria() throws Exception { final List<List<String>> links = asListOfLists("links[rel=urn:org.restfulobjects:rels/details;action=foo].x,links[rel=urn:org.restfulobjects:rels/details;action=bar].y"); final LinkFollowSpecs linkFollower = LinkFollowSpecs.create(links); LinkFollowSpecs followRelVersion = linkFollower.follow("links[rel=urn:org.restfulobjects:rels/details]"); assertThat(followRelVersion.isFollowing(), is(true)); assertThat(followRelVersion.isTerminated(), is(false)); assertThat(followRelVersion.follow("x").isFollowing(), is(true)); }
@Test public void multiplePaths_withRelSimplifiedCriteria() throws Exception { final List<List<String>> links = asListOfLists("links[rel=urn:org.restfulobjects:rels/details;action=foo].x,links[rel=urn:org.restfulobjects:rels/details;action=bar].y"); final LinkFollowSpecs linkFollower = LinkFollowSpecs.create(links); LinkFollowSpecs followRelVersion = linkFollower.follow("links[rel=urn:org.restfulobjects:rels/details]"); assertThat(followRelVersion.isFollowing(), is(true)); assertThat(followRelVersion.isTerminated(), is(false)); assertThat(followRelVersion.follow("x").isFollowing(), is(true)); }
@Test public void example_of_eager_loading_of_collection() throws Exception { final List<List<String>> links = asListOfLists("members[children].value"); final LinkFollowSpecs linkFollower = LinkFollowSpecs.create(links); LinkFollowSpecs followMembers = linkFollower.follow("members[children]"); assertThat(followMembers.isFollowing(), is(true)); assertThat(followMembers.isTerminated(), is(false)); assertThat(followMembers.follow("value").isFollowing(), is(true)); assertThat(followMembers.follow("value").isTerminated(), is(false)); }
@Test public void follow_withSingleMapCriteria() throws Exception { final List<List<String>> links = asListOfLists("a[x].b.c"); final LinkFollowSpecs linkFollower = LinkFollowSpecs.create(links); assertThat(linkFollower.follow("x").isFollowing(), is(false)); final LinkFollowSpecs followA = linkFollower.follow("a"); assertThat(followA.isFollowing(), is(true)); assertThat(followA.matches(JsonRepresentation.newMap("x", "y")), is(true)); assertThat(followA.matches(JsonRepresentation.newMap("x", "z")), is(true)); assertThat(followA.matches(JsonRepresentation.newMap()), is(false)); assertThat(followA.matches(JsonRepresentation.newMap("p", "z")), is(false)); }
@Test public void simple_multiplePaths() throws Exception { final List<List<String>> links = asListOfLists("a.b.c,x.y.z"); final LinkFollowSpecs linkFollower = LinkFollowSpecs.create(links); LinkFollowSpecs followA = linkFollower.follow("a"); assertThat(followA.isFollowing(), is(true)); assertThat(followA.isTerminated(), is(false)); LinkFollowSpecs followX = linkFollower.follow("x"); assertThat(followX.isFollowing(), is(true)); assertThat(followX.isTerminated(), is(false)); LinkFollowSpecs followXY = followX.follow("y"); assertThat(followXY.isFollowing(), is(true)); assertThat(followXY.isTerminated(), is(false)); LinkFollowSpecs followXYZ = followXY.follow("z"); assertThat(followXYZ.isFollowing(), is(true)); assertThat(followXYZ.isTerminated(), is(false)); LinkFollowSpecs followXYZQ = followXY.follow("q"); assertThat(followXYZQ.isFollowing(), is(false)); assertThat(followXYZQ.isTerminated(), is(true)); }
@Test public void notMatching() throws Exception { final List<List<String>> links = asListOfLists("a.b.c"); final LinkFollowSpecs linkFollower = LinkFollowSpecs.create(links); assertThat(linkFollower.follow("x").isFollowing(), is(false)); assertThat(linkFollower.follow("x").isTerminated(), is(true)); }
@Test public void follow_withSingleListCriteria() throws Exception { final List<List<String>> links = asListOfLists("a[x=y].b.c"); final LinkFollowSpecs linkFollower = LinkFollowSpecs.create(links); assertThat(linkFollower.follow("x").isFollowing(), is(false)); final LinkFollowSpecs followA = linkFollower.follow("a"); assertThat(followA.isFollowing(), is(true)); assertThat(followA.matches(JsonRepresentation.newMap("x", "y")), is(true)); assertThat(followA.matches(JsonRepresentation.newMap()), is(false)); assertThat(followA.matches(JsonRepresentation.newMap("x", "z")), is(false)); }
@Test public void follow_withSingleListCriteria() throws Exception { final List<List<String>> links = asListOfLists("a[x=y].b.c"); final LinkFollowSpecs linkFollower = LinkFollowSpecs.create(links); assertThat(linkFollower.follow("x").isFollowing(), is(false)); final LinkFollowSpecs followA = linkFollower.follow("a"); assertThat(followA.isFollowing(), is(true)); assertThat(followA.matches(JsonRepresentation.newMap("x", "y")), is(true)); assertThat(followA.matches(JsonRepresentation.newMap()), is(false)); assertThat(followA.matches(JsonRepresentation.newMap("x", "z")), is(false)); }
@Test public void simple() throws Exception { final List<List<String>> links = asListOfLists("a.b.c"); final LinkFollowSpecs linkFollower = LinkFollowSpecs.create(links); assertThat(linkFollower.follow("a").isFollowing(), is(true)); assertThat(linkFollower.follow("a").isTerminated(), is(false)); }
@Test public void notMatching() throws Exception { final List<List<String>> links = asListOfLists("a.b.c"); final LinkFollowSpecs linkFollower = LinkFollowSpecs.create(links); assertThat(linkFollower.follow("x").isFollowing(), is(false)); assertThat(linkFollower.follow("x").isTerminated(), is(true)); }
@Test public void follow_withSingleMapCriteria() throws Exception { final List<List<String>> links = asListOfLists("a[x].b.c"); final LinkFollowSpecs linkFollower = LinkFollowSpecs.create(links); assertThat(linkFollower.follow("x").isFollowing(), is(false)); final LinkFollowSpecs followA = linkFollower.follow("a"); assertThat(followA.isFollowing(), is(true)); assertThat(followA.matches(JsonRepresentation.newMap("x", "y")), is(true)); assertThat(followA.matches(JsonRepresentation.newMap("x", "z")), is(true)); assertThat(followA.matches(JsonRepresentation.newMap()), is(false)); assertThat(followA.matches(JsonRepresentation.newMap("p", "z")), is(false)); }
@Test public void simple() throws Exception { final List<List<String>> links = asListOfLists("a.b.c"); final LinkFollowSpecs linkFollower = LinkFollowSpecs.create(links); assertThat(linkFollower.follow("a").isFollowing(), is(true)); assertThat(linkFollower.follow("a").isTerminated(), is(false)); }
@Test public void follow_withMultipleCriteria() throws Exception { final List<List<String>> links = asListOfLists("a[x=y z=w].b.c"); final LinkFollowSpecs linkFollower = LinkFollowSpecs.create(links); assertThat(linkFollower.follow("x").isFollowing(), is(false)); final LinkFollowSpecs followA = linkFollower.follow("a"); assertThat(followA.isFollowing(), is(true)); assertThat(followA.matches(JsonRepresentation.newMap("x", "y", "z", "w")), is(true)); assertThat(followA.matches(JsonRepresentation.newMap("x", "y", "z", "w", "foo", "bar")), is(true)); assertThat(followA.matches(JsonRepresentation.newMap()), is(false)); assertThat(followA.matches(JsonRepresentation.newMap("x", "y")), is(false)); assertThat(followA.matches(JsonRepresentation.newMap("x", "y", "foo", "bar")), is(false)); assertThat(followA.matches(JsonRepresentation.newMap("x", "bad")), is(false)); assertThat(followA.matches(JsonRepresentation.newMap("x", "y", "z", "bad")), is(false)); }
@Test public void follow_withMultipleCriteria() throws Exception { final List<List<String>> links = asListOfLists("a[x=y z=w].b.c"); final LinkFollowSpecs linkFollower = LinkFollowSpecs.create(links); assertThat(linkFollower.follow("x").isFollowing(), is(false)); final LinkFollowSpecs followA = linkFollower.follow("a"); assertThat(followA.isFollowing(), is(true)); assertThat(followA.matches(JsonRepresentation.newMap("x", "y", "z", "w")), is(true)); assertThat(followA.matches(JsonRepresentation.newMap("x", "y", "z", "w", "foo", "bar")), is(true)); assertThat(followA.matches(JsonRepresentation.newMap()), is(false)); assertThat(followA.matches(JsonRepresentation.newMap("x", "y")), is(false)); assertThat(followA.matches(JsonRepresentation.newMap("x", "y", "foo", "bar")), is(false)); assertThat(followA.matches(JsonRepresentation.newMap("x", "bad")), is(false)); assertThat(followA.matches(JsonRepresentation.newMap("x", "y", "z", "bad")), is(false)); }