/** * Fetch the default JSON-LD serialization. */ @Test @DisplayName("Fetch the default JSON-LD serialization") default void testGetJsonLdDefault() { final String location = createAnnotationResource(); try (final Response res = target(location).request().accept("application/ld+json").get()) { assertAll("Check for an LDP-RS as JSONLD", checkRdfResponse(res, LDP.RDFSource, APPLICATION_LD_JSON_TYPE)); final Map<String, Object> obj = readEntityAsJson(res.getEntity(), new TypeReference<Map<String, Object>>(){}); assertTrue(obj.containsKey("@type"), "Check for a @type property"); assertTrue(obj.containsKey("@id"), "Check for an @id property"); assertTrue(obj.keySet().stream().anyMatch(key -> key.endsWith("hasTarget")), "Check for a hasTarget prop"); assertTrue(obj.keySet().stream().anyMatch(key -> key.endsWith("hasBody")), "Check for a hasBody property"); } }
/** * Fetch the compacted JSON-LD serialization. */ @Test @DisplayName("Fetch the compacted JSON-LD serialization") default void testGetJsonLdCompacted() { final String location = createAnnotationResource(); try (final Response res = target(location).request() .accept("application/ld+json; profile=\"http://www.w3.org/ns/json-ld#compacted\"").get()) { assertAll("Check for compact JSONLD", checkRdfResponse(res, LDP.RDFSource, APPLICATION_LD_JSON_TYPE)); final Map<String, Object> obj = readEntityAsJson(res.getEntity(), new TypeReference<Map<String, Object>>(){}); assertTrue(obj.containsKey("@id"), "Check for an @id property"); assertTrue(obj.containsKey("@type"), "Check for a @type property"); assertTrue(obj.keySet().stream().anyMatch(key -> key.endsWith("hasBody")), "Check for a hasBody property"); assertTrue(obj.keySet().stream().anyMatch(key -> key.endsWith("hasTarget")), "Check for a hasTarget prop"); } }
/** * Fetch the default JSON-LD serialization. */ @Test @DisplayName("Fetch the default JSON-LD serialization") default void testGetJsonLdDefault() { final String location = createAnnotationResource(); try (final Response res = target(location).request().accept("application/ld+json").get()) { assertAll("Check for an LDP-RS as JSONLD", checkRdfResponse(res, LDP.RDFSource, APPLICATION_LD_JSON_TYPE)); final Map<String, Object> obj = readEntityAsJson(res.getEntity(), new TypeReference<Map<String, Object>>(){}); assertTrue(obj.containsKey("@type"), "Check for a @type property"); assertTrue(obj.containsKey("@id"), "Check for an @id property"); assertTrue(obj.keySet().stream().anyMatch(key -> key.endsWith("hasTarget")), "Check for a hasTarget prop"); assertTrue(obj.keySet().stream().anyMatch(key -> key.endsWith("hasBody")), "Check for a hasBody property"); } }
/** * Fetch the compacted JSON-LD serialization. */ @Test @DisplayName("Fetch the compacted JSON-LD serialization") default void testGetJsonLdCompacted() { final String location = createAnnotationResource(); try (final Response res = target(location).request() .accept("application/ld+json; profile=\"http://www.w3.org/ns/json-ld#compacted\"").get()) { assertAll("Check for compact JSONLD", checkRdfResponse(res, LDP.RDFSource, APPLICATION_LD_JSON_TYPE)); final Map<String, Object> obj = readEntityAsJson(res.getEntity(), new TypeReference<Map<String, Object>>(){}); assertTrue(obj.containsKey("@id"), "Check for an @id property"); assertTrue(obj.containsKey("@type"), "Check for a @type property"); assertTrue(obj.keySet().stream().anyMatch(key -> key.endsWith("hasBody")), "Check for a hasBody property"); assertTrue(obj.keySet().stream().anyMatch(key -> key.endsWith("hasTarget")), "Check for a hasTarget prop"); } }
/** * Fetch the expanded JSON-LD serialization. */ @Test @DisplayName("Fetch the expanded JSON-LD serialization") default void testGetJsonLdExpanded() { final String location = createAnnotationResource(); try (final Response res = target(location).request() .accept("application/ld+json; profile=\"http://www.w3.org/ns/json-ld#expanded\"").get()) { assertAll("Check for expanded JSONLD", checkRdfResponse(res, LDP.RDFSource, APPLICATION_LD_JSON_TYPE)); final List<Map<String, Object>> obj = readEntityAsJson(res.getEntity(), new TypeReference<List<Map<String, Object>>>(){}); assertEquals(1L, obj.size(), "Check json structure"); assertTrue(obj.get(0).containsKey("@id"), "Check for an @id property"); assertTrue(obj.get(0).containsKey("@type"), "Check for a @type property"); assertTrue(obj.get(0).containsKey("http://www.w3.org/ns/oa#hasBody"), "Check for an oa:hasBody prop"); assertTrue(obj.get(0).containsKey("http://www.w3.org/ns/oa#hasTarget"), "Check for an oa:hasTarget prop"); } }
/** * Fetch the expanded JSON-LD serialization. */ @Test @DisplayName("Fetch the expanded JSON-LD serialization") default void testGetJsonLdExpanded() { final String location = createAnnotationResource(); try (final Response res = target(location).request() .accept("application/ld+json; profile=\"http://www.w3.org/ns/json-ld#expanded\"").get()) { assertAll("Check for expanded JSONLD", checkRdfResponse(res, LDP.RDFSource, APPLICATION_LD_JSON_TYPE)); final List<Map<String, Object>> obj = readEntityAsJson(res.getEntity(), new TypeReference<List<Map<String, Object>>>(){}); assertEquals(1L, obj.size(), "Check json structure"); assertTrue(obj.get(0).containsKey("@id"), "Check for an @id property"); assertTrue(obj.get(0).containsKey("@type"), "Check for a @type property"); assertTrue(obj.get(0).containsKey("http://www.w3.org/ns/oa#hasBody"), "Check for an oa:hasBody prop"); assertTrue(obj.get(0).containsKey("http://www.w3.org/ns/oa#hasTarget"), "Check for an oa:hasTarget prop"); } }
@Test public void testReadEntityAsJsonError() { final InputStream is = new ByteArrayInputStream("{\"invalid JSON\" }".getBytes(UTF_8)); assertThrows(UncheckedIOException.class, () -> TestUtils.readEntityAsJson(is, new TypeReference<Map<String, Object>>(){}), "Check converting malformed JSON"); } }
@Test public void testReadEntityAsJson() { final Map<String, Object> data = singletonMap("foo", "bar"); final InputStream is = new ByteArrayInputStream("{\"foo\" : \"bar\" }".getBytes(UTF_8)); assertEquals(data, TestUtils.readEntityAsJson(is, new TypeReference<Map<String, Object>>(){}), "Check converting an InputStream to JSON"); }
/** * Fetch a JSON-LD serialization with a custom profile. */ @Test @DisplayName("Fetch the JSON-LD serialization with a custom profile") default void testGetJsonLdAnnotationProfile() { assumeTrue(supportedJsonLdProfiles().contains("http://www.w3.org/ns/anno.jsonld"), "Support for the Web Annotation profile is not enabled."); final String location = createAnnotationResource(); try (final Response res = target(location).request() .accept("application/ld+json; profile=\"http://www.w3.org/ns/anno.jsonld\"").get()) { assertAll("Check for custom JSONLD", checkRdfResponse(res, LDP.RDFSource, APPLICATION_LD_JSON_TYPE)); final Map<String, Object> obj = readEntityAsJson(res.getEntity(), new TypeReference<Map<String, Object>>(){}); assertTrue(obj.containsKey("@context"), "Check for a @context property"); assertEquals("http://www.w3.org/ns/anno.jsonld", obj.get("@context"), "Check the @context value"); assertEquals(location, obj.get("id"), "Check the id value"); assertEquals("Annotation", obj.get("type"), "Check the type value"); assertEquals("http://example.org/post1", obj.get("body"), "Check the body value"); assertEquals("http://example.org/page1", obj.get("target"), "Check the target value"); } }
/** * Fetch a JSON-LD serialization with a custom profile. */ @Test @DisplayName("Fetch the JSON-LD serialization with a custom profile") default void testGetJsonLdAnnotationProfile() { assumeTrue(supportedJsonLdProfiles().contains("http://www.w3.org/ns/anno.jsonld"), "Support for the Web Annotation profile is not enabled."); final String location = createAnnotationResource(); try (final Response res = target(location).request() .accept("application/ld+json; profile=\"http://www.w3.org/ns/anno.jsonld\"").get()) { assertAll("Check for custom JSONLD", checkRdfResponse(res, LDP.RDFSource, APPLICATION_LD_JSON_TYPE)); final Map<String, Object> obj = readEntityAsJson(res.getEntity(), new TypeReference<Map<String, Object>>(){}); assertTrue(obj.containsKey("@context"), "Check for a @context property"); assertEquals("http://www.w3.org/ns/anno.jsonld", obj.get("@context"), "Check the @context value"); assertEquals(location, obj.get("id"), "Check the id value"); assertEquals("Annotation", obj.get("type"), "Check the type value"); assertEquals("http://example.org/post1", obj.get("body"), "Check the body value"); assertEquals("http://example.org/page1", obj.get("target"), "Check the target value"); } }