public boolean isLink(final JsonNode node) { if (representsNull(node) || isArray(node) || node.isValueNode()) { return false; } final LinkRepresentation link = new LinkRepresentation(node); if (link.getHref() == null) { return false; } return true; }
private LinkRepresentation getLink(final String path, final JsonNode node) { if (representsNull(node)) { return null; } if (isArray(node)) { throw new IllegalArgumentException(formatExMsg(path, "is an array that does not represent a link")); } if (node.isValueNode()) { throw new IllegalArgumentException(formatExMsg(path, "is a value that does not represent a link")); } final LinkRepresentation link = new LinkRepresentation(node); if (link.getHref() == null) { throw new IllegalArgumentException(formatExMsg(path, "is a map that does not fully represent a link")); } return link; }
@Test public void equalDependsOnMethodAndHref() throws UnsupportedEncodingException { final LinkRepresentation link = new LinkRepresentation().withHref("http://localhost:8080/objects/ABC:123").withMethod(RestfulHttpMethod.GET); final LinkRepresentation link2 = new LinkRepresentation().withHref("http://localhost:8080/objects/ABC:123").withMethod(RestfulHttpMethod.GET); final LinkRepresentation link3 = new LinkRepresentation().withHref("http://localhost:8080/objects/ABC:123").withMethod(RestfulHttpMethod.PUT); final LinkRepresentation link4 = new LinkRepresentation().withHref("http://localhost:8080/objects/ABC:456").withMethod(RestfulHttpMethod.GET); assertThat(link, is(equalTo(link2))); assertThat(link, is(not(equalTo(link3)))); assertThat(link, is(not(equalTo(link4)))); }
@Test public void equalDependsOnMethodAndHref() throws UnsupportedEncodingException { final LinkRepresentation link = new LinkRepresentation().withHref("http://localhost:8080/objects/ABC:123").withMethod(RestfulHttpMethod.GET); final LinkRepresentation link2 = new LinkRepresentation().withHref("http://localhost:8080/objects/ABC:123").withMethod(RestfulHttpMethod.GET); final LinkRepresentation link3 = new LinkRepresentation().withHref("http://localhost:8080/objects/ABC:123").withMethod(RestfulHttpMethod.PUT); final LinkRepresentation link4 = new LinkRepresentation().withHref("http://localhost:8080/objects/ABC:456").withMethod(RestfulHttpMethod.GET); assertThat(link, is(equalTo(link2))); assertThat(link, is(not(equalTo(link3)))); assertThat(link, is(not(equalTo(link4)))); }
@Test public void test() throws UnsupportedEncodingException { final LinkRepresentation l = new LinkRepresentation().withRel("object").withHref("http://localhost:8080/objects/ABC:123").withMethod(RestfulHttpMethod.GET); final String expectedValue = "%7B%22method%22%3A%22GET%22%2C%22rel%22%3A%22object%22%2C%22href%22%3A%22http%3A%2F%2Flocalhost%3A8080%2Fobjects%2FABC%3A123%22%7D"; @SuppressWarnings("unused") final String decoded = URLDecoder.decode(expectedValue, Charsets.UTF_8.name()); assertThat(l.asUrlEncoded(), is(expectedValue)); } }
@Test public void test() throws UnsupportedEncodingException { final LinkRepresentation l = new LinkRepresentation().withRel("object").withHref("http://localhost:8080/objects/ABC:123").withMethod(RestfulHttpMethod.GET); final String expectedValue = "%7B%22method%22%3A%22GET%22%2C%22rel%22%3A%22object%22%2C%22href%22%3A%22http%3A%2F%2Flocalhost%3A8080%2Fobjects%2FABC%3A123%22%7D"; @SuppressWarnings("unused") final String decoded = URLDecoder.decode(expectedValue, Charsets.UTF_8.name()); assertThat(l.asUrlEncoded(), is(expectedValue)); } }
@Test public void equalDoesNotDependsOnMethodAndHref() throws UnsupportedEncodingException { final LinkRepresentation link = new LinkRepresentation().withHref("http://localhost:8080/objects/ABC:123").withMethod(RestfulHttpMethod.GET).withRel("something"); final LinkRepresentation link2 = new LinkRepresentation().withHref("http://localhost:8080/objects/ABC:123").withMethod(RestfulHttpMethod.GET).withRel("else"); assertThat(link, is(equalTo(link2))); }
@Test public void equalDoesNotDependsOnMethodAndHref() throws UnsupportedEncodingException { final LinkRepresentation link = new LinkRepresentation().withHref("http://localhost:8080/objects/ABC:123").withMethod(RestfulHttpMethod.GET).withRel("something"); final LinkRepresentation link2 = new LinkRepresentation().withHref("http://localhost:8080/objects/ABC:123").withMethod(RestfulHttpMethod.GET).withRel("else"); assertThat(link, is(equalTo(link2))); }
@Before public void setUp() throws Exception { link = new LinkRepresentation().withHref("http://foo/bar").withMethod(RestfulHttpMethod.GET); jsonRepresentation = new JsonRepresentation(readJson("map.json")); }
@Test public void withPredicate() throws IOException { // given link = new LinkRepresentation().withRel(Rel.SELF).withHref("http://foo/bar").withMethod(RestfulHttpMethod.GET); JsonRepresentation linkListRepr = JsonRepresentation.newArray(); linkListRepr.arrayAdd(link); jsonRepresentation = JsonRepresentation.newMap(); jsonRepresentation.mapPut("links", linkListRepr); // when, then assertThat(jsonRepresentation.isLink("links[rel=self]"), is(true)); assertThat(jsonRepresentation.getLink("links[rel=self]"), is(not(nullValue()))); assertThat(jsonRepresentation.isLink("links[rel=other]"), is(false)); assertThat(jsonRepresentation.getLink("links[rel=other]"), is(nullValue())); }
@Test public void withPredicate() throws JsonParseException, JsonMappingException, IOException { // given link = new LinkRepresentation().withRel(Rel.SELF).withHref("http://foo/bar").withMethod(RestfulHttpMethod.GET); JsonRepresentation linkListRepr = JsonRepresentation.newArray(); linkListRepr.arrayAdd(link); jsonRepresentation = JsonRepresentation.newMap(); jsonRepresentation.mapPut("links", linkListRepr); // when, then assertThat(jsonRepresentation.isLink("links[rel=self]"), is(true)); assertThat(jsonRepresentation.getLink("links[rel=self]"), is(not(nullValue()))); assertThat(jsonRepresentation.isLink("links[rel=other]"), is(false)); assertThat(jsonRepresentation.getLink("links[rel=other]"), is(nullValue())); }
@Before public void setUp() throws Exception { link = new LinkRepresentation().withHref("http://foo/bar").withMethod(RestfulHttpMethod.GET); jsonRepresentation = new JsonRepresentation(readJson("map.json")); }