JacksonJsonHomeParser implements JsonHomeParser { @Override public JsonHome parse(final InputStream stream) { try { final JsonNode jsonNode = OBJECT_MAPPER.readTree(stream); if (jsonNode != null && jsonNode.has("resources")) { final JsonHomeBuilder builder = jsonHomeBuilder(); final JsonNode resourcesNode = jsonNode.get("resources"); final Iterator<String> fieldNames = resourcesNode.getFieldNames(); while (fieldNames.hasNext()) { final String fieldName = fieldNames.next(); final JsonNode resourceNode = resourcesNode.get(fieldName); builder.addResource(resourceLinkFrom(URI.create(fieldName), resourceNode)); } return builder.build(); } } catch (final JsonProcessingException e) { LOG.error("Unable to parse json-home document: {}", e.getMessage()); throw new IllegalArgumentException("Error parsing json-home document: " + e.getMessage(), e); } catch (final IOException e) { LOG.error("Unable to get json-home from stream: {}", e.getMessage()); throw new IllegalStateException("Unable to get json-home document from stream: " + e.getMessage(), e); } throw new IllegalArgumentException("Unable to parse json-home document: no resources defined."); }  @Override JsonHome parse(final InputStream stream); static final ObjectMapper OBJECT_MAPPER; }
@Test public void shouldParseDirectLink() throws Exception { final String jsonHomeDocument = "{\n" + " \"resources\" : {" + "\"http: " \"href\" : \"http: " \"hints\" : {\n" + " \"allow\" : [\n" + " \"GET\"\n," + " \"HEAD\"\n" + " ],\n" + " \"docs\" : \"http: " \"precondition-req\" : [\"etag\"],\n" + " \"status\" : \"deprecated\",\n" + " \"docs\" : \"http: " \"representations\" : [\n" + " \"text/html\",\n" + " \"text/plain\"\n" + " ]\n" + " }\n" + " }}}"; final JsonHome jsonHome = new JacksonJsonHomeParser() .parse(new ByteArrayInputStream(jsonHomeDocument.getBytes())); assertEquals(jsonHome, jsonHome( directLink( create("http: create("http: hintsBuilder() .allowing(EnumSet.of(GET, HEAD)) .representedAs(asList("text/html", "text/plain")) .with(docLink(URI.create("http: .requiring(ETAG) .withStatus(Status.DEPRECATED) .build() ) )); }
@Test public void shouldParseTemplatedLink() throws Exception { final String jsonHomeDocument = "{\n" + " \"resources\" : {" + "\"http: " \"href-template\" : \"http: " \"href-vars\" : {\n" + " \"productId\" : \"http: " },\n" + " \"hints\" : {\n" + " \"allow\" : [\n" + " \"POST\",\n" + " \"PUT\"\n" + " ],\n" + " \"accept-put\" : [\n" + " \"application/example-product\",\n" + " \"application/json\"\n" + " ],\n" + " \"accept-post\" : [\n" + " \"application/json\"\n" + " ],\n" + " \"representations\" : [\n" + " \"application/json\"\n" + " ]\n" + " }\n" + " }\n" + " }}}"; final JsonHome jsonHome = new JacksonJsonHomeParser() .parse(new ByteArrayInputStream(jsonHomeDocument.getBytes())); assertEquals(jsonHome, jsonHome( templatedLink( create("http: "http: asList(hrefVar("productId", create("http: hintsBuilder() .allowing(EnumSet.of(PUT, POST)) .representedAs(asList("application/json")) .acceptingForPut(asList("application/example-product", "application/json")) .acceptingForPost("application/json") .build() ) )); }
@Test public void shouldParseMultiplePreconditions() throws Exception { final String jsonHomeDocument = "{\n" + " \"resources\" : {" + "\"http: " \"href\" : \"http: " \"hints\" : {\n" + " \"precondition-req\" : [\"etag\", \"last-modified\"]\n" + " }\n" + " }}}"; final JsonHome jsonHome = new JacksonJsonHomeParser() .parse(new ByteArrayInputStream(jsonHomeDocument.getBytes())); assertEquals(jsonHome, jsonHome( directLink( create("http: create("http: hintsBuilder() .requiring(ETAG, LAST_MODIFIED) .build() ) )); }
@Test public void shouldParseMultiplePreferences() throws Exception { final String jsonHomeDocument = "{\n" + " \"resources\" : {" + "\"http: " \"href\" : \"http: " \"hints\" : {\n" + " \"prefer\" : [\"return-representation=application/json\", \"return-asynch\"]\n" + " }\n" + " }}}"; final JsonHome jsonHome = new JacksonJsonHomeParser() .parse(new ByteArrayInputStream(jsonHomeDocument.getBytes())); assertEquals(jsonHome, jsonHome( directLink( create("http: create("http: hintsBuilder() .preferring("return-representation=application/json", "return-asynch") .build() ) )); }
@Test public void shouldParseAcceptedRanges() throws Exception { final String jsonHomeDocument = "{\n" + " \"resources\" : {" + "\"http: " \"href\" : \"http: " \"hints\" : {\n" + " \"accept-ranges\" : [\"bytes\"]\n" + " }\n" + " }}}"; final JsonHome jsonHome = new JacksonJsonHomeParser() .parse(new ByteArrayInputStream(jsonHomeDocument.getBytes())); assertEquals(jsonHome, jsonHome( directLink( create("http: create("http: hintsBuilder() .acceptingRanges("bytes") .build() ) )); }
@Test public void shouldParseMultipleRequiredAuth() throws Exception { final String jsonHomeDocument = "{\n" + " \"resources\" : {" + "\"http: " \"href\" : \"http: " \"hints\" : {\n" + " \"auth-req\" : [{\"scheme\" : \"Basic\", \"realms\" : [\"public\", \"private\"]}, {\"scheme\" : \"Digest\"}]\n" + " }\n" + " }}}"; final JsonHome jsonHome = new JacksonJsonHomeParser() .parse(new ByteArrayInputStream(jsonHomeDocument.getBytes())); assertEquals(jsonHome, jsonHome( directLink( create("http: create("http: hintsBuilder() .withAuthRequired(asList( authReq("Basic", asList("public", "private")), authReq("Digest"))) .build() ) )); }
@Test public void shouldParseApplicationJsonWithDescription() throws Exception { final String jsonDocument = "{\n" + " \"resources\" : {" + "\"http: " \"href\" : \"http: " \"hints\" : {\n" + " \"docs\" : \"http: " \"description\" : [\"a short description\"],\n" + " \"detailedDescription\" : \"<p>A detailed description.</p>\"\n" + " }\n" + " }}}"; final JsonHome jsonHome = new JacksonJsonHomeParser() .parse(new ByteArrayInputStream(jsonDocument.getBytes())); assertEquals(jsonHome, jsonHome( directLink( create("http: create("http: hintsBuilder() .with(documentation( asList("a short description"), "<p>A detailed description.</p>", create("http: .build() ) )); }