/** * Initialize the RDF tests. */ @BeforeAll @DisplayName("Initialize RDF tests") default void beforeAllTests() { final String content = getResourceAsString(SIMPLE_RESOURCE); // POST an LDP-RS try (final Response res = target().request().header(SLUG, generateRandomValue(getClass().getSimpleName())) .post(entity(content, TEXT_TURTLE))) { setResourceLocation(checkCreateResponseAssumptions(res, LDP.RDFSource)); } }
/** * Verify that the correct containment triples exist. */ @Test @DisplayName("Verify that the correct containment triples exist") default void testRdfContainment() { final RDF rdf = getInstance(); // Test the root container, verifying that the containment triple exists try (final Response res = target().request().get()) { assertAll("Check a container resource", checkRdfResponse(res, LDP.BasicContainer, TEXT_TURTLE_TYPE)); final Graph g = readEntityAsGraph(res.getEntity(), getBaseURL(), TURTLE); assertTrue(g.contains(rdf.createIRI(getBaseURL()), LDP.contains, rdf.createIRI(getResourceLocation())), "Check for an ldp:contains property"); } }
/** * Fetch the default RDF serialization. */ @Test @DisplayName("Fetch the default RDF serialization") default void testGetDefault() { try (final Response res = target(getResourceLocation()).request().get()) { assertAll("Check for an LDP-RS as Turtle", checkRdfResponse(res, LDP.RDFSource, TEXT_TURTLE_TYPE)); } }
/** * Create an Annotation resource. * @return the location of the new resource */ default String createAnnotationResource() { // POST an LDP-RS try (final Response res = target().request().header(SLUG, generateRandomValue(getClass().getSimpleName())) .post(entity(getResourceAsString(ANNOTATION_RESOURCE), TEXT_TURTLE))) { assertAll("Check for an LDP-RS", checkRdfResponse(res, LDP.RDFSource, null)); return res.getLocation().toString(); } } }
/** * 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"); } }
/** * Test POSTing an RDF resource. */ @Test @DisplayName("Test POSTing an RDF resource") default void testPostRDF() { final String content = getResourceAsString(SIMPLE_RESOURCE); // POST an LDP-RS try (final Response res = target().request().header(SLUG, generateRandomValue(getClass().getSimpleName())) .post(entity(content, TEXT_TURTLE))) { assertAll("Check POSTing an RDF resource", checkRdfResponse(res, LDP.RDFSource, null)); final String location = res.getLocation().toString(); assertTrue(location.startsWith(getBaseURL()), "Check the Location header"); assertTrue(location.length() > getBaseURL().length(), "Re-check the Location header"); } }
/** * 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"); } }
/** * Test creating resource with syntactically invalid RDF. */ @Test @DisplayName("Test creating resource with syntactically invalid RDF") default void testInvalidRDF() { final String rdf = "PREFIX skos: <http://www.w3.org/2004/02/skos/core#> \n" + "PREFIX dc: <http://purl.org/dc/terms/> \n\n" + "<> a skos:Concept \n" + " skos:prefLabel \"Resource Name\"@eng \n" + " dc:subject <http://example.org/subject/1> ."; // POST an LDP-RS try (final Response res = target().request().header(SLUG, generateRandomValue(getClass().getSimpleName())) .post(entity(rdf, TEXT_TURTLE))) { assertEquals(CLIENT_ERROR, res.getStatusInfo().getFamily(), "Syntactically invalid RDF produces a 4xx"); } }
/** * 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 RDF serialization. */ @Test @DisplayName("Fetch the default RDF serialization") default void testGetDefault() { try (final Response res = target(getResourceLocation()).request().get()) { assertAll("Check for an LDP-RS as Turtle", checkRdfResponse(res, LDP.RDFSource, TEXT_TURTLE_TYPE)); } }
/** * Test POSTing an RDF resource. */ @Test @DisplayName("Test POSTing an RDF resource") default void testPostRDF() { final String content = getResourceAsString(SIMPLE_RESOURCE); // POST an LDP-RS try (final Response res = target().request().header(SLUG, generateRandomValue(getClass().getSimpleName())) .post(entity(content, TEXT_TURTLE))) { assertAll("Check POSTing an RDF resource", checkRdfResponse(res, LDP.RDFSource, null)); final String location = res.getLocation().toString(); assertTrue(location.startsWith(getBaseURL()), "Check the Location header"); assertTrue(location.length() > getBaseURL().length(), "Re-check the Location header"); } }
/** * 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"); } }
/** * Create an Annotation resource. * @return the location of the new resource */ default String createAnnotationResource() { // POST an LDP-RS try (final Response res = target().request().header(SLUG, generateRandomValue(getClass().getSimpleName())) .post(entity(getResourceAsString(ANNOTATION_RESOURCE), TEXT_TURTLE))) { assertAll("Check for an LDP-RS", checkRdfResponse(res, LDP.RDFSource, null)); return res.getLocation().toString(); } } }
/** * Test creating resource with syntactically invalid RDF. */ @Test @DisplayName("Test creating resource with syntactically invalid RDF") default void testInvalidRDF() { final String rdf = "PREFIX skos: <http://www.w3.org/2004/02/skos/core#> \n" + "PREFIX dc: <http://purl.org/dc/terms/> \n\n" + "<> a skos:Concept \n" + " skos:prefLabel \"Resource Name\"@eng \n" + " dc:subject <http://example.org/subject/1> ."; // POST an LDP-RS try (final Response res = target().request().header(SLUG, generateRandomValue(getClass().getSimpleName())) .post(entity(rdf, TEXT_TURTLE))) { assertEquals(CLIENT_ERROR, res.getStatusInfo().getFamily(), "Syntactically invalid RDF produces a 4xx"); } }
/** * Verify that the correct containment triples exist. */ @Test @DisplayName("Verify that the correct containment triples exist") default void testRdfContainment() { final RDF rdf = getInstance(); // Test the root container, verifying that the containment triple exists try (final Response res = target().request().get()) { assertAll("Check a container resource", checkRdfResponse(res, LDP.BasicContainer, TEXT_TURTLE_TYPE)); final Graph g = readEntityAsGraph(res.getEntity(), getBaseURL(), TURTLE); assertTrue(g.contains(rdf.createIRI(getBaseURL()), LDP.contains, rdf.createIRI(getResourceLocation())), "Check for an ldp:contains property"); } }
/** * 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 N-Triples serialization. */ @Test @DisplayName("Fetch the N-Triples serialization") default void testGetNTriples() { try (final Response res = target(getResourceLocation()).request().accept("application/n-triples").get()) { assertAll("Check for N-Triples", checkRdfResponse(res, LDP.RDFSource, APPLICATION_N_TRIPLES_TYPE)); } }
/** * Initialize the RDF tests. */ @BeforeAll @DisplayName("Initialize RDF tests") default void beforeAllTests() { final String content = getResourceAsString(SIMPLE_RESOURCE); // POST an LDP-RS try (final Response res = target().request().header(SLUG, generateRandomValue(getClass().getSimpleName())) .post(entity(content, TEXT_TURTLE))) { setResourceLocation(checkCreateResponseAssumptions(res, LDP.RDFSource)); } }
/** * Test creating resource with invalid RDF. */ @Test @DisplayName("Test creating resource with invalid RDF") default void testWeirdRDF() { final String rdf = getResourceAsString(SIMPLE_RESOURCE) + "<> a \"skos concept\" ."; // POST an LDP-RS try (final Response res = target().request().header(SLUG, generateRandomValue(getClass().getSimpleName())) .post(entity(rdf, TEXT_TURTLE))) { assertEquals(CLIENT_ERROR, res.getStatusInfo().getFamily(), "Semantically invalid RDF should throw a 4xx error"); assertTrue(getLinks(res).stream().anyMatch(hasConstrainedBy(Trellis.InvalidRange)), "Check for an InvalidRange constraint for weird rdf:type constructs"); } }
/** * Test modifying an RDF document via PATCH. */ @Test @DisplayName("Test modifying an RDF document via PATCH") default void testPatchRDF() { final RDF rdf = getInstance(); final EntityTag initialETag = getETag(getResourceLocation()); // Patch the resource try (final Response res = target(getResourceLocation()).request().method("PATCH", entity("INSERT { <> a <http://www.w3.org/ns/ldp#Container> ; " + "<http://purl.org/dc/terms/title> \"Title\" } WHERE {}", APPLICATION_SPARQL_UPDATE))) { assertAll("Check an LDP-RS resource", checkRdfResponse(res, LDP.RDFSource, null)); } await().until(() -> !initialETag.equals(getETag(getResourceLocation()))); // Fetch the updated resource try (final Response res = target(getResourceLocation()).request().accept("application/n-triples").get()) { assertAll("Check an updated resource", checkRdfResponse(res, LDP.RDFSource, APPLICATION_N_TRIPLES_TYPE)); final Graph g = readEntityAsGraph(res.getEntity(), getBaseURL(), NTRIPLES); assertEquals(4L, g.size(), "Check the graph size"); assertTrue(g.contains(rdf.createIRI(getResourceLocation()), DC.title, rdf.createLiteral("Title")), "Check for a dc:title triple"); assertTrue(res.getEntityTag().isWeak(), "Check that the ETag is weak"); assertNotEquals(initialETag, res.getEntityTag(), "Compare the first and second ETags"); } }