Example usage for org.apache.commons.rdf.api Graph contains

List of usage examples for org.apache.commons.rdf.api Graph contains

Introduction

In this page you can find the example usage for org.apache.commons.rdf.api Graph contains.

Prototype

boolean contains(BlankNodeOrIRI subject, IRI predicate, RDFTerm object);

Source Link

Document

Check if graph contains a pattern of triples.

Usage

From source file:org.trellisldp.test.LdpBasicContainerTests.java

/**
 * Test with ldp:PreferMinimalContainer Prefer header.
 *//* ww  w .  j  a  v  a2  s  .  c  om*/
@Test
@DisplayName("Test with ldp:PreferMinimalContainer Prefer header")
default void testGetInverseEmptyContainer() {
    final RDF rdf = getInstance();
    try (final Response res = target(getContainerLocation()).request()
            .header(PREFER, "return=representation; omit=\"" + LDP.PreferMinimalContainer.getIRIString() + "\"")
            .get()) {
        assertAll("Check a container response", checkRdfResponse(res, LDP.BasicContainer, TEXT_TURTLE_TYPE));
        final Graph g = readEntityAsGraph(res.getEntity(), getBaseURL(), TURTLE);
        final IRI identifier = rdf.createIRI(getContainerLocation());
        assertFalse(g.contains(identifier, DC.description, null), "Check for no dc:description triple");
        assertFalse(g.contains(identifier, SKOS.prefLabel, null), "Check for no skos:prefLabel triple");
        assertTrue(g.contains(identifier, LDP.contains, null), "Check for an ldp:contains triple");
    }
}

From source file:org.trellisldp.test.LdpBasicContainerTests.java

/**
 * Test creating a basic container via POST.
 *///from w w w. j  a  va 2s.  com
@Test
@DisplayName("Test creating a basic container via POST")
default void testCreateContainerViaPost() {
    final RDF rdf = getInstance();
    final String containerContent = getResourceAsString(BASIC_CONTAINER);
    final String child3;

    // First fetch the container headers to get the initial ETag
    final EntityTag initialETag = getETag(getContainerLocation());

    // POST an LDP-BC
    try (final Response res = target(getContainerLocation()).request()
            .header(LINK, fromUri(LDP.BasicContainer.getIRIString()).rel(TYPE).build())
            .post(entity(containerContent, TEXT_TURTLE))) {
        assertAll("Check the LDP-BC response", checkRdfResponse(res, LDP.BasicContainer, null));

        child3 = res.getLocation().toString();
        assertTrue(child3.startsWith(getContainerLocation()), "Check the Location header");
        assertTrue(child3.length() > getContainerLocation().length(), "Check the Location header again");
    }

    await().until(() -> !initialETag.equals(getETag(getContainerLocation())));

    // Now fetch the container
    try (final Response res = target(getContainerLocation()).request().get()) {
        assertAll("Check the LDP-BC again", checkRdfResponse(res, LDP.BasicContainer, TEXT_TURTLE_TYPE));
        final Graph g = readEntityAsGraph(res.getEntity(), getBaseURL(), TURTLE);
        final IRI identifier = rdf.createIRI(getContainerLocation());
        final EntityTag etag = res.getEntityTag();
        assertAll("Check the LDP-BC graph", checkRdfGraph(g, identifier));
        assertTrue(g.contains(identifier, LDP.contains, rdf.createIRI(child3)));
        assertTrue(etag.isWeak(), "Verify that the ETag is weak");
        assertNotEquals(initialETag, etag, "Check that the ETag has been changed");
    }
}

From source file:org.trellisldp.test.LdpBasicContainerTests.java

/**
 * Test creating a child resource via PUT.
 */// w ww  .  j av a2s .  c  o m
@Test
@DisplayName("Test creating a child resource via PUT")
default void testCreateContainerViaPut() {
    final RDF rdf = getInstance();
    final String containerContent = getResourceAsString(BASIC_CONTAINER);
    final String child4 = getContainerLocation() + "/child4";

    // First fetch the container headers to get the initial ETag
    final EntityTag initialETag = getETag(getContainerLocation());

    try (final Response res = target(child4).request()
            .header(LINK, fromUri(LDP.BasicContainer.getIRIString()).rel(TYPE).build())
            .put(entity(containerContent, TEXT_TURTLE))) {
        assertAll("Check PUTting an LDP-BC", checkRdfResponse(res, LDP.BasicContainer, null));
    }

    // Now fetch the resource
    try (final Response res = target(getContainerLocation()).request().get()) {
        assertAll("Check an LDP-BC after PUT", checkRdfResponse(res, LDP.BasicContainer, TEXT_TURTLE_TYPE));
        final Graph g = readEntityAsGraph(res.getEntity(), getBaseURL(), TURTLE);
        final IRI identifier = rdf.createIRI(getContainerLocation());
        final EntityTag etag = res.getEntityTag();
        assertAll("Check the resulting graph", checkRdfGraph(g, identifier));
        assertFalse(g.contains(identifier, LDP.contains, rdf.createIRI(child4)),
                "Check for an ldp:contains triple");
        assertTrue(etag.isWeak(), "Check for a weak ETag");
        assertEquals(initialETag, etag, "Check ETags");
    }
}

From source file:org.trellisldp.test.LdpBasicContainerTests.java

/**
 * Test creating a child resource with a Slug header.
 *///w  w w.  j  a  v  a  2s  .c  o  m
@Test
@DisplayName("Test creating a child resource with a Slug header")
default void testCreateContainerWithSlug() {
    final RDF rdf = getInstance();
    final String containerContent = getResourceAsString(BASIC_CONTAINER);
    final String child5 = getContainerLocation() + "/child5";

    // First fetch the container headers to get the initial ETag
    final EntityTag initialETag = getETag(getContainerLocation());

    // POST an LDP-BC
    try (final Response res = target(getContainerLocation()).request().header("Slug", "child5")
            .header(LINK, fromUri(LDP.BasicContainer.getIRIString()).rel(TYPE).build())
            .post(entity(containerContent, TEXT_TURTLE))) {
        assertAll("Check POSTing an LDP-BC", checkRdfResponse(res, LDP.BasicContainer, null));
        assertEquals(child5, res.getLocation().toString(), "Check the resource location");
    }

    await().until(() -> !initialETag.equals(getETag(getContainerLocation())));

    // Now fetch the resource
    try (final Response res = target(getContainerLocation()).request().get()) {
        assertAll("Check GETting the new resource",
                checkRdfResponse(res, LDP.BasicContainer, TEXT_TURTLE_TYPE));
        final Graph g = readEntityAsGraph(res.getEntity(), getBaseURL(), TURTLE);
        final IRI identifier = rdf.createIRI(getContainerLocation());
        final EntityTag etag = res.getEntityTag();
        assertAll("Check the resulting Graph", checkRdfGraph(g, identifier));
        assertTrue(g.contains(identifier, LDP.contains, rdf.createIRI(child5)),
                "Check for an ldp:contains triple");
        assertTrue(etag.isWeak(), "Check for a weak ETag");
        assertNotEquals(initialETag, etag, "Compare ETags 1 and 4");
    }
}

From source file:org.trellisldp.test.LdpBasicContainerTests.java

/**
 * Test deleting a basic container.//  w  w w.j ava  2  s. c  o m
 */
@Test
@DisplayName("Test deleting a basic container")
default void testDeleteContainer() {
    final RDF rdf = getInstance();
    final String childResource;

    final EntityTag initialETag = getETag(getContainerLocation());

    try (final Response res = target(getContainerLocation()).request().post(entity("", TEXT_TURTLE))) {
        assertAll("Check for an LDP-RS", checkRdfResponse(res, LDP.RDFSource, null));
        childResource = res.getLocation().toString();
    }

    await().until(() -> !initialETag.equals(getETag(getContainerLocation())));

    final EntityTag etag;
    try (final Response res = target(getContainerLocation()).request().get()) {
        assertAll("Check for an LDP-BC", checkRdfResponse(res, LDP.BasicContainer, TEXT_TURTLE_TYPE));
        final IRI identifier = rdf.createIRI(getContainerLocation());
        final Graph g = readEntityAsGraph(res.getEntity(), getBaseURL(), TURTLE);
        assertAll("Verify the resulting graph", checkRdfGraph(g, identifier));
        assertTrue(g.contains(identifier, LDP.contains, rdf.createIRI(childResource)),
                "Check for the presence of an ldp:contains triple");
        etag = res.getEntityTag();
        assertTrue(etag.isWeak(), "Verify that the ETag is weak");
    }

    // Delete one of the child resources
    try (final Response res = target(childResource).request().delete()) {
        assertEquals(SUCCESSFUL, res.getStatusInfo().getFamily(), "Check the response type");
    }

    // Try fetching the deleted resource
    try (final Response res = target(childResource).request().get()) {
        assertEquals(CLIENT_ERROR, res.getStatusInfo().getFamily(), "Check for an expected error");
    }

    try (final Response res = target(getContainerLocation()).request().get()) {
        assertAll("Check the parent container", checkRdfResponse(res, LDP.BasicContainer, TEXT_TURTLE_TYPE));
        final Graph g = readEntityAsGraph(res.getEntity(), getBaseURL(), TURTLE);
        assertFalse(
                g.contains(rdf.createIRI(getContainerLocation()), LDP.contains, rdf.createIRI(childResource)),
                "Check the graph doesn't contain the deleted resource");
        assertTrue(res.getEntityTag().isWeak(), "Check that the ETag is weak");
        assertNotEquals(etag, res.getEntityTag(), "Verify that the ETag value is different");
    }
}

From source file:org.trellisldp.test.LdpBinaryTests.java

/**
 * Test modifying a binary's description via PATCH.
 *///from  ww w.  ja va  2  s  .c  o m
@Test
@DisplayName("Test modifying a binary's description via PATCH")
default void testPatchBinaryDescription() {
    final RDF rdf = getInstance();
    final EntityTag descriptionETag;
    final long size;

    // Discover the location of the description
    final String descriptionLocation = getDescription(getResourceLocation());
    if (isNull(descriptionLocation)) {
        fail("No describedby Link header!");
    }

    // Fetch the description
    try (final Response res = target(descriptionLocation).request().accept("text/turtle").get()) {
        assertAll("Check an LDP-NR description", checkRdfResponse(res, LDP.RDFSource, TEXT_TURTLE_TYPE));
        final Graph g = readEntityAsGraph(res.getEntity(), getBaseURL(), TURTLE);
        size = g.size();
        descriptionETag = res.getEntityTag();
        assertTrue(descriptionETag.isWeak(), "Check for a weak ETag");
    }
    // wait for enough time so that the ETags will surely be different
    await().pollDelay(TWO_SECONDS).until(() -> true);
    // Patch the description
    try (final Response res = target(descriptionLocation).request().method("PATCH", entity(
            "INSERT { <> <http://purl.org/dc/terms/title> \"Title\" } WHERE {}", APPLICATION_SPARQL_UPDATE))) {
        assertAll("Check PATCHing LDP-NR description", checkRdfResponse(res, LDP.RDFSource, null));
    }

    await().until(() -> !descriptionETag.equals(getETag(descriptionLocation)));

    // Fetch the new description
    try (final Response res = target(descriptionLocation).request().accept("text/turtle").get()) {
        assertAll("Check the new LDP-NR description", checkRdfResponse(res, LDP.RDFSource, TEXT_TURTLE_TYPE));
        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")),
                "Check for a dc:title triple");
        assertNotEquals(descriptionETag, res.getEntityTag(), "Check that the ETag values are different");
    }

    // Verify that the binary is still accessible
    try (final Response res = target(getResourceLocation()).request().get()) {
        assertAll("Check the LDP-NR", checkNonRdfResponse(res, TEXT_PLAIN_TYPE));
        assertEquals(CONTENT, readEntityAsString(res.getEntity()),
                "Check for an expected binary content value");
    }
}

From source file:org.trellisldp.test.LdpBinaryTests.java

/**
 * Test that the binary appears in the parent container.
 *///from ww  w . j av a  2  s . c  om
@Test
@DisplayName("Test that the binary appears in the parent container")
default void testBinaryIsInContainer() {
    final RDF rdf = getInstance();
    // Test the root container, verifying that the containment triple exists
    try (final Response res = target().request().get()) {
        assertAll("Check binary in container", checkRdfResponse(res, LDP.BasicContainer, null));
        final Graph g = readEntityAsGraph(res.getEntity(), getBaseURL(), TURTLE);
        assertTrue(g.contains(rdf.createIRI(getBaseURL()), LDP.contains, rdf.createIRI(getResourceLocation())),
                "Check for an ldp:contains triple");
    }
}

From source file:org.trellisldp.test.LdpDirectContainerTests.java

/**
 * Test fetch a self-contained direct container.
 *//*from   w w  w  . j a v  a  2s  . c o  m*/
@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");
    }
}

From source file:org.trellisldp.test.LdpDirectContainerTests.java

/**
 * Test adding resources to the direct container.
 *///  w  w w.j ava  2s.  c  om
@Test
@DisplayName("Test adding resources to the direct container")
default void testAddingMemberResources() {
    final RDF rdf = getInstance();
    final String dcLocation;
    final String child1;
    final String child2;
    final EntityTag etag1;
    final EntityTag etag2;
    final EntityTag etag3;
    final EntityTag etag4;
    final EntityTag etag5;
    final EntityTag etag6;
    final String childContent = getResourceAsString(SIMPLE_RESOURCE);

    final String content = getResourceAsString(DIRECT_CONTAINER) + membershipResource(getMemberLocation());

    // POST an LDP-DC
    try (final Response res = target(getContainerLocation()).request()
            .header(LINK, fromUri(LDP.DirectContainer.getIRIString()).rel(TYPE).build())
            .post(entity(content, TEXT_TURTLE))) {
        assertAll("Check the LDP-DC was created", checkRdfResponse(res, LDP.DirectContainer, null));
        dcLocation = res.getLocation().toString();
    }

    // Fetch the member resource
    try (final Response res = target(getMemberLocation()).request().get()) {
        assertAll("Check the member resource", checkRdfResponse(res, LDP.RDFSource, TEXT_TURTLE_TYPE));
        final Graph g = readEntityAsGraph(res.getEntity(), getBaseURL(), TURTLE);
        assertFalse(g.contains(rdf.createIRI(getMemberLocation()), LDP.member, null),
                "Check that an ldp:contains triple is not present");
        etag1 = res.getEntityTag();
        assertTrue(etag1.isWeak(), "Check for a weak ETag");
    }

    // Fetch the container resource
    try (final Response res = target(dcLocation).request().get()) {
        assertAll("Check the container resource", checkRdfResponse(res, LDP.DirectContainer, TEXT_TURTLE_TYPE));
        final Graph g = readEntityAsGraph(res.getEntity(), getBaseURL(), TURTLE);
        assertFalse(g.contains(rdf.createIRI(dcLocation), LDP.contains, null),
                "Check that the given ldp:contains triple isn't present");
        etag4 = res.getEntityTag();
        assertTrue(etag4.isWeak(), "Verify that the ETag is weak");
    }

    // POST an LDP-RS child
    try (final Response res = target(dcLocation).request().post(entity(childContent, TEXT_TURTLE))) {
        assertAll("Check POSTing a child", checkRdfResponse(res, LDP.RDFSource, null));
        child1 = res.getLocation().toString();
        assertTrue(child1.startsWith(dcLocation), "Check the Location header");
        assertTrue(child1.length() > dcLocation.length(), "Re-check the Location header");
    }

    // POST an LDP-RS child
    try (final Response res = target(dcLocation).request().post(entity(childContent, TEXT_TURTLE))) {
        assertAll("Check POSTing a child", checkRdfResponse(res, LDP.RDFSource, null));
        child2 = res.getLocation().toString();
        assertTrue(child2.startsWith(dcLocation), "Check the Location header of the LDP-DC");
        assertTrue(child2.length() > dcLocation.length(), "Re-check the Location header");
    }

    await().until(() -> !etag4.equals(getETag(dcLocation)));
    await().until(() -> !etag1.equals(getETag(getMemberLocation())));

    // Fetch the member resource
    try (final Response res = target(getMemberLocation()).request().get()) {
        assertAll("Check fetching the member resource", checkRdfResponse(res, LDP.RDFSource, TEXT_TURTLE_TYPE));
        final Graph g = readEntityAsGraph(res.getEntity(), getBaseURL(), TURTLE);
        final IRI identifier = rdf.createIRI(getMemberLocation());
        assertTrue(g.contains(identifier, LDP.member, rdf.createIRI(child1)), "Check for a member property");
        assertTrue(g.contains(identifier, LDP.member, rdf.createIRI(child2)),
                "Check for another member property");
        etag2 = res.getEntityTag();
        assertTrue(etag2.isWeak(), "Check for a weak ETag value");
        assertNotEquals(etag1, etag2, "Establish that the first two ETag values don't match");
    }

    // Fetch the container resource
    try (final Response res = target(dcLocation).request().get()) {
        assertAll("Check the container resource", checkRdfResponse(res, LDP.DirectContainer, TEXT_TURTLE_TYPE));
        final Graph g = readEntityAsGraph(res.getEntity(), getBaseURL(), TURTLE);
        final IRI identifier = rdf.createIRI(dcLocation);
        assertTrue(g.contains(identifier, LDP.contains, rdf.createIRI(child1)),
                "Check for an ldp:contains triple");
        assertTrue(g.contains(identifier, LDP.contains, rdf.createIRI(child2)),
                "Check for an ldp:contains triple");
        etag5 = res.getEntityTag();
        assertTrue(etag5.isWeak(), "Verify that the ETag value is weak");
        assertNotEquals(etag4, etag5, "Check that ETags 4 and 5 are different");
    }

    // Delete one of the child resources
    try (final Response res = target(child1).request().delete()) {
        assertEquals(SUCCESSFUL, res.getStatusInfo().getFamily(), "Check for a successful response");
    }

    await().until(() -> !etag5.equals(getETag(dcLocation)));
    await().until(() -> !etag2.equals(getETag(getMemberLocation())));

    // Try fetching the deleted resource
    try (final Response res = target(child1).request().get()) {
        assertEquals(CLIENT_ERROR, res.getStatusInfo().getFamily(), "Check for an expected 4xx response");
    }

    // Fetch the member resource
    try (final Response res = target(getMemberLocation()).request().get()) {
        assertAll("Check fetching the member resource", checkRdfResponse(res, LDP.RDFSource, TEXT_TURTLE_TYPE));
        final Graph g = readEntityAsGraph(res.getEntity(), getBaseURL(), TURTLE);
        final IRI identifier = rdf.createIRI(getMemberLocation());
        assertFalse(g.contains(identifier, LDP.member, rdf.createIRI(child1)),
                "Check for the absense of a member triple");
        assertTrue(g.contains(identifier, LDP.member, rdf.createIRI(child2)),
                "Check for the presence of a member triple");
        etag3 = res.getEntityTag();
        assertTrue(etag3.isWeak(), "Verify that the third ETag is weak");
        assertNotEquals(etag1, etag3, "Compare ETags 1 and 3");
        assertNotEquals(etag2, etag3, "Compare ETags 2 and 3");
    }

    // Fetch the container resource
    try (final Response res = target(dcLocation).request().get()) {
        assertAll("Check the container resource", checkRdfResponse(res, LDP.DirectContainer, TEXT_TURTLE_TYPE));
        final Graph g = readEntityAsGraph(res.getEntity(), getBaseURL(), TURTLE);
        final IRI identifier = rdf.createIRI(dcLocation);
        assertFalse(g.contains(identifier, LDP.contains, rdf.createIRI(child1)),
                "Check that child1 is no longer contained by the parent");
        assertTrue(g.contains(identifier, LDP.contains, rdf.createIRI(child2)),
                "Verify that the second child is still contained by the parent");
        etag6 = res.getEntityTag();
        assertTrue(etag6.isWeak(), "Confirm that the 6th ETag value is weak");
        assertNotEquals(etag5, etag6, "Compare ETags 5 and 6");
        assertNotEquals(etag4, etag6, "Compare ETags 4 and 6");
    }

    // Now change the membership property
    final String updateContent = "PREFIX dc: <http://purl.org/dc/terms/>\n"
            + "PREFIX ldp: <http://www.w3.org/ns/ldp#>\n\n" + "DELETE WHERE { <> ldp:hasMemberRelation ?o };"
            + "INSERT { <> ldp:hasMemberRelation dc:relation } WHERE {}";

    // Patch the direct container
    try (final Response res = target(dcLocation).request().method("PATCH",
            entity(updateContent, APPLICATION_SPARQL_UPDATE))) {
        assertAll("Check PATCHing the container", checkRdfResponse(res, LDP.DirectContainer, null));
    }

    // Fetch the member resource
    try (final Response res = target(getMemberLocation()).request().get()) {
        assertAll("Check fetching the member resource", checkRdfResponse(res, LDP.RDFSource, TEXT_TURTLE_TYPE));
        final Graph g = readEntityAsGraph(res.getEntity(), getBaseURL(), TURTLE);
        final IRI identifier = rdf.createIRI(getMemberLocation());
        assertTrue(g.contains(identifier, DC.relation, rdf.createIRI(child2)),
                "Confirm that the graph contains a dc:relation member triple");
    }
}

From source file:org.trellisldp.test.LdpDirectContainerTests.java

/**
 * Test fetching inverse member properties on a direct container.
 *///ww w .j a va2 s . c  om
@Test
@DisplayName("Test with inverse direct containment")
default void testDirectContainerWithInverseMembership() {
    final RDF rdf = getInstance();
    final String dcLocation;
    final String rsLocation;
    final String directContainerInverse = getResourceAsString(DIRECT_CONTAINER_INVERSE)
            + membershipResource(getMemberLocation());

    // POST an LDP-DC
    try (final Response res = target(getContainerLocation()).request()
            .header(LINK, fromUri(LDP.DirectContainer.getIRIString()).rel(TYPE).build())
            .post(entity(directContainerInverse, TEXT_TURTLE))) {
        assertAll("Check creating an LDP-DC", checkRdfResponse(res, LDP.DirectContainer, null));

        dcLocation = res.getLocation().toString();
    }

    // Create an LDP-RS
    try (final Response res = target(dcLocation).request().post(entity("", TEXT_TURTLE))) {
        assertAll("Check creating an LDP-RS", checkRdfResponse(res, LDP.RDFSource, null));
        rsLocation = res.getLocation().toString();
    }

    try (final Response res = target(rsLocation).request().get()) {
        assertAll("Check the LDP-RS", checkRdfResponse(res, LDP.RDFSource, TEXT_TURTLE_TYPE));
        final Graph g = readEntityAsGraph(res.getEntity(), getBaseURL(), TURTLE);
        final IRI identifier = rdf.createIRI(getMemberLocation());
        assertTrue(g.contains(rdf.createIRI(rsLocation), DC.isPartOf, identifier),
                "Check that dc:isPartOf is present in the graph");
    }
}