/** * 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(); } } }
/** * 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(); } } }
/** * 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)); } }
/** * 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 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 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 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)); } }
/** * 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 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)); } }
/** * 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 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"); } }
/** * 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 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"); } }
/** * 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)); } }
/** * 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)); } }
/** * 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"); } }
/** * 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"); } }
/** * 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"); } }
/** * Test fetching an RDF resource. */ @Test @DisplayName("Test fetching an RDF resource") default void testGetRDF() { final RDF rdf = getInstance(); // Fetch the new resource try (final Response res = target(getResourceLocation()).request().get()) { assertAll("Check an LDP-RS resource", checkRdfResponse(res, LDP.RDFSource, TEXT_TURTLE_TYPE)); final Graph g = readEntityAsGraph(res.getEntity(), getBaseURL(), TURTLE); assertEquals(3L, g.size(), "Check the size of the resulting graph"); final IRI identifier = rdf.createIRI(getResourceLocation()); assertTrue(g.contains(identifier, type, SKOS.Concept), "Check for a rdf:type triple"); assertTrue(g.contains(identifier, SKOS.prefLabel, rdf.createLiteral("Resource Name", "eng")), "Check for a skos:prefLabel triple"); assertTrue(g.contains(identifier, DC.subject, rdf.createIRI("http://example.org/subject/1")), "Check for a dc:subject triple"); assertTrue(res.getEntityTag().isWeak(), "Check that the ETag is weak"); } }