/** * 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 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"); } }
/** * Parse a JSON entity into the desired type. * @param entity the entity * @param valueType the type reference * @param <T> the intended return type * @return the entity as the desired type */ public static <T> T readEntityAsJson(final Object entity, final TypeReference<T> valueType) { try { return MAPPER.readValue(readEntityAsString(entity), valueType); } catch (final IOException ex) { throw new UncheckedIOException(ex); } }
/** * Check a response for expected LDP Link headers. * @param res the Response object * @param ldpType the LDP Resource type * @return a Stream of executable assertions */ default Stream<Executable> checkMementoLdpHeaders(final Response res, final IRI ldpType) { return of( () -> assertEquals(SUCCESSFUL, res.getStatusInfo().getFamily(), "Check for a successful response"), () -> assertTrue(getLinks(res).stream().anyMatch(hasType(LDP.Resource)), "Check that the response is an LDP Resource"), () -> assertTrue(getLinks(res).stream().anyMatch(hasType(ldpType)), "Check that the response is an " + ldpType)); }
/** * Test fetch a self-contained direct container. */ @Test @DisplayName("Test fetch a self-contained direct container") default void testSimpleDirectContainer() { final RDF rdf = getInstance(); final String memberContent = getResourceAsString(SIMPLE_RESOURCE); final String child; // POST an LDP-RS try (final Response res = target(getDirectContainerLocation()).request() .post(entity(memberContent, TEXT_TURTLE))) { assumeTrue(SUCCESSFUL.equals(res.getStatusInfo().getFamily()), "Creation of RDFSource appears to be unsupported"); assumeTrue(getLinks(res).stream().anyMatch(hasType(LDP.RDFSource)), "New resource was not of expected RDFSource type"); child = res.getLocation().toString(); } // Fetch the member resource try (final Response res = target(getDirectContainerLocation()).request().get()) { assertAll("Check the member resource", checkRdfResponse(res, LDP.RDFSource, TEXT_TURTLE_TYPE)); final Graph g = readEntityAsGraph(res.getEntity(), getBaseURL(), TURTLE); assertTrue(g.contains(rdf.createIRI(getDirectContainerLocation()), LDP.contains, rdf.createIRI(child)), "Verify an ldp:contains triple"); assertTrue(g.contains(rdf.createIRI(getDirectContainerLocation() + MEMBER_RESOURCE_HASH), LDP.member, rdf.createIRI(child)), "Verify a member triple"); } }
/** * Check the absense of audit triples. */ @Test @DisplayName("Check the absense of audit triples.") default void testNoAuditTriples() { try (final Response res = target(getResourceLocation()).request().get()) { final Graph g = readEntityAsGraph(res.getEntity(), getBaseURL(), TURTLE); assertEquals(2L, g.size(), "Check that the graph has 2 triples"); } }
/** * 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 receiving a replace message. */ @Test @DisplayName("Test receiving a replace message in a LDP-IC") default void testReceiveReplaceMessageIC() { final String resource; final String agent = "http://example.com/parker#i"; final String childContent = getResourceAsString("/childResource.ttl"); // POST an LDP-RS try (final Response res = target(getIndirectContainerLocation()).request() .header(AUTHORIZATION, buildJwt(agent, getJwtSecret())).post(entity(childContent, TEXT_TURTLE))) { assertEquals(SUCCESSFUL, res.getStatusInfo().getFamily(), "Check for a successful POST to an LDP-IC"); resource = res.getLocation().toString(); } assertAll("Check the LDP-IC parent resource", checkResourceParentLdpIC(resource, agent, AS.Create, LDP.RDFSource, LDP.Container)); final String agent2 = "https://hayden.example.com/profile#me"; // Replace the LDP-RS try (final Response res = target(resource).request().header(AUTHORIZATION, buildJwt(agent2, getJwtSecret())) .put(entity(childContent + "\n<> a <http://example.com/Type3> .", TEXT_TURTLE))) { assertEquals(SUCCESSFUL, res.getStatusInfo().getFamily(), "Check for a successful PUT in an LDP-IC"); } await().atMost(15, SECONDS).until(() -> getMessages().stream() .anyMatch(checkEventGraph(resource, agent2, AS.Update, LDP.RDFSource))); await().atMost(15, SECONDS).until(() -> getMessages().stream() .anyMatch(checkEventGraph(getMemberLocation(), agent2, AS.Update, LDP.Container))); }
@DisplayName("Initialize Indirect Container tests") default void beforeAllTests() { final String containerContent = getResourceAsString(BASIC_CONTAINER); assumeTrue(SUCCESSFUL.equals(res.getStatusInfo().getFamily()), "Creation of BasicContainer appears to be unsupported"); assumeTrue(getLinks(res).stream().anyMatch(hasType(LDP.BasicContainer)), "Expected LDP:BasicContainer type not present in response"); final String content = getResourceAsString(INDIRECT_CONTAINER) + membershipResource(getMemberLocation()); assumeTrue(SUCCESSFUL.equals(res.getStatusInfo().getFamily()), "Creation of IndirectContainer appears to be unsupported"); assumeTrue(getLinks(res).stream().anyMatch(hasType(LDP.IndirectContainer)), "Expected LDP:IndirectContainer type not present in response"); assumeTrue(SUCCESSFUL.equals(res.getStatusInfo().getFamily()), "Creation of RDFSource appears to be unsupported"); assumeTrue(getLinks(res).stream().anyMatch(hasType(LDP.RDFSource)), "Expected LDP:RDFSource type not present in response");
final Graph g = readEntityAsGraph(res.getEntity(), getBaseURL(), TURTLE); size = g.size(); descriptionETag = res.getEntityTag(); final Graph g = readEntityAsGraph(res.getEntity(), getBaseURL(), TURTLE); assertTrue(g.size() > size, "Check the graph size is greater than " + size); assertTrue(g.contains(rdf.createIRI(getResourceLocation()), DC.title, rdf.createLiteral("Title")), assertEquals(CONTENT, readEntityAsString(res.getEntity()), "Check for an expected binary content value");
/** * Test receiving a creation event message in an indirect container. */ @Test @DisplayName("Test receiving a JMS creation message from a LDP-IC") default void testReceiveCreateMessageIC() { final String agent = "http://example.com/sam#i"; // POST an LDP-RS try (final Response res = target(getIndirectContainerLocation()).request() .header(AUTHORIZATION, buildJwt(agent, getJwtSecret())) .post(entity(getResourceAsString("/childResource.ttl"), TEXT_TURTLE))) { assertEquals(SUCCESSFUL, res.getStatusInfo().getFamily(), "Check for a successful POST in an LDP-IC"); assertAll("Check the LDP-IC parent", checkResourceParentLdpIC(res.getLocation().toString(), agent, AS.Create, LDP.RDFSource, LDP.Container)); } }
/** * Check an event graph for required properties. * @param resource the resource IRI * @param agent the agent IRI * @param activity the activity IRI * @param ldpType the LDP type of the resource * @return a predicate function */ public static Predicate<Graph> checkEventGraph(final String resource, final String agent, final IRI activity, final IRI ldpType) { return checkEventGraph(resource, getInstance().createIRI(agent), activity, ldpType); }
@Override public String getAuthorizationHeader() { return buildJwt("https://people.apache.org/~acoburn/#i", AbstractApplicationAuthTests.this.getJwtSecret()); } }
/** * Get the describedby Link value, if one exists. * @param url the URL * @return the location of a description resource, or null if none is available */ default String getDescription(final String url) { try (final Response res = target(url).request().head()) { return getLinks(res).stream().filter(link -> "describedby".equals(link.getRel())) .map(Link::getUri).map(URI::toString).findFirst().orElse(null); } }
/** * Read an entity as an RDF Graph. * @param entity the HTTP entity * @param baseURL the base URL * @param syntax the RDF syntax * @return the graph */ public static Graph readEntityAsGraph(final Object entity, final String baseURL, final RDFSyntax syntax) { final Graph g = getInstance().createGraph(); getIOService().read((InputStream) entity, syntax, baseURL).forEach(g::add); return g; }
/** * Test fetch a self-contained direct container. */ @Test @DisplayName("Test fetch a self-contained direct container") default void testSimpleDirectContainer() { final RDF rdf = getInstance(); final String memberContent = getResourceAsString(SIMPLE_RESOURCE); final String child; // POST an LDP-RS try (final Response res = target(getDirectContainerLocation()).request() .post(entity(memberContent, TEXT_TURTLE))) { assumeTrue(SUCCESSFUL.equals(res.getStatusInfo().getFamily()), "Creation of RDFSource appears to be unsupported"); assumeTrue(getLinks(res).stream().anyMatch(hasType(LDP.RDFSource)), "New resource was not of expected RDFSource type"); child = res.getLocation().toString(); } // Fetch the member resource try (final Response res = target(getDirectContainerLocation()).request().get()) { assertAll("Check the member resource", checkRdfResponse(res, LDP.RDFSource, TEXT_TURTLE_TYPE)); final Graph g = readEntityAsGraph(res.getEntity(), getBaseURL(), TURTLE); assertTrue(g.contains(rdf.createIRI(getDirectContainerLocation()), LDP.contains, rdf.createIRI(child)), "Verify an ldp:contains triple"); assertTrue(g.contains(rdf.createIRI(getDirectContainerLocation() + MEMBER_RESOURCE_HASH), LDP.member, rdf.createIRI(child)), "Verify a member triple"); } }
/** * Check a response for expected LDP Link headers. * @param res the Response object * @param ldpType the LDP Resource type * @return a Stream of executable assertions */ default Stream<Executable> checkMementoLdpHeaders(final Response res, final IRI ldpType) { return of( () -> assertEquals(SUCCESSFUL, res.getStatusInfo().getFamily(), "Check for a successful response"), () -> assertTrue(getLinks(res).stream().anyMatch(hasType(LDP.Resource)), "Check that the response is an LDP Resource"), () -> assertTrue(getLinks(res).stream().anyMatch(hasType(ldpType)), "Check that the response is an " + ldpType)); }
/** * Check the absense of audit triples. */ @Test @DisplayName("Check the absense of audit triples.") default void testNoAuditTriples() { try (final Response res = target(getResourceLocation()).request().get()) { final Graph g = readEntityAsGraph(res.getEntity(), getBaseURL(), TURTLE); assertEquals(2L, g.size(), "Check that the graph has 2 triples"); } }
/** * 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 receiving a replace message. */ @Test @DisplayName("Test receiving a replace message in a LDP-IC") default void testReceiveReplaceMessageIC() { final String resource; final String agent = "http://example.com/parker#i"; final String childContent = getResourceAsString("/childResource.ttl"); // POST an LDP-RS try (final Response res = target(getIndirectContainerLocation()).request() .header(AUTHORIZATION, buildJwt(agent, getJwtSecret())).post(entity(childContent, TEXT_TURTLE))) { assertEquals(SUCCESSFUL, res.getStatusInfo().getFamily(), "Check for a successful POST to an LDP-IC"); resource = res.getLocation().toString(); } assertAll("Check the LDP-IC parent resource", checkResourceParentLdpIC(resource, agent, AS.Create, LDP.RDFSource, LDP.Container)); final String agent2 = "https://hayden.example.com/profile#me"; // Replace the LDP-RS try (final Response res = target(resource).request().header(AUTHORIZATION, buildJwt(agent2, getJwtSecret())) .put(entity(childContent + "\n<> a <http://example.com/Type3> .", TEXT_TURTLE))) { assertEquals(SUCCESSFUL, res.getStatusInfo().getFamily(), "Check for a successful PUT in an LDP-IC"); } await().atMost(15, SECONDS).until(() -> getMessages().stream() .anyMatch(checkEventGraph(resource, agent2, AS.Update, LDP.RDFSource))); await().atMost(15, SECONDS).until(() -> getMessages().stream() .anyMatch(checkEventGraph(getMemberLocation(), agent2, AS.Update, LDP.Container))); }