Search in sources :

Example 26 with Dataset

use of org.apache.commons.rdf.api.Dataset in project trellis by trellis-ldp.

the class TriplestoreResourceTest method testPartialResource.

@Test
void testPartialResource() {
    final Dataset dataset = rdf.createDataset();
    dataset.add(Trellis.PreferServerManaged, identifier, DC.modified, rdf.createLiteral(time, XSD.dateTime));
    final TriplestoreResource res = new TriplestoreResource(connect(wrap(toJena(dataset))), identifier, extensions, false);
    res.fetchData();
    assertFalse(res.exists(), "Unexpected resource!");
}
Also used : Dataset(org.apache.commons.rdf.api.Dataset) Test(org.junit.jupiter.api.Test)

Example 27 with Dataset

use of org.apache.commons.rdf.api.Dataset in project trellis by trellis-ldp.

the class MementoResourceTests method testMementoContent.

/**
 * Test the content of memento resources.
 * @throws Exception if the RDF resources did not exit cleanly
 */
default void testMementoContent() throws Exception {
    final RDF rdf = RDFFactory.getInstance();
    try (final Dataset dataset = rdf.createDataset()) {
        final Map<String, String> mementos = getMementos();
        mementos.forEach((memento, date) -> {
            try (final Response res = target(memento).request().get()) {
                assertEquals(SUCCESSFUL, res.getStatusInfo().getFamily(), "Check for a successful request");
                readEntityAsGraph(res.getEntity(), getBaseURL(), TURTLE).stream().forEach(triple -> dataset.add(rdf.createIRI(memento), triple.getSubject(), triple.getPredicate(), triple.getObject()));
            }
        });
        final IRI subject = rdf.createIRI(getResourceLocation());
        final List<IRI> urls = mementos.keySet().stream().sorted().map(rdf::createIRI).collect(toList());
        assertEquals(2L, urls.size(), "Check that two mementos were found");
        assertTrue(dataset.getGraph(urls.get(0)).isPresent(), "Check that the first graph is present");
        dataset.getGraph(urls.get(0)).ifPresent(g -> {
            assertTrue(g.contains(subject, type, SKOS.Concept), "Check for a skos:Concept type");
            assertTrue(g.contains(subject, SKOS.prefLabel, rdf.createLiteral("Resource Name", "eng")), "Check for a skos:prefLabel property");
            assertTrue(g.contains(subject, DC.subject, rdf.createIRI("http://example.org/subject/1")), "Check for a dc:subject property");
            assertEquals(2L, g.stream().map(Triple::getPredicate).filter(isEqual(type).negate()).count(), "Check for two non-type triples");
        });
        assertTrue(dataset.getGraph(urls.get(1)).isPresent(), "Check that the last graph is present");
        dataset.getGraph(urls.get(1)).ifPresent(g -> {
            assertTrue(g.contains(subject, type, SKOS.Concept), "Check for a skos:Concept type");
            assertTrue(g.contains(subject, SKOS.prefLabel, rdf.createLiteral("Resource Name", "eng")), "Check for a skos:prefLabel property");
            assertTrue(g.contains(subject, DC.subject, rdf.createIRI("http://example.org/subject/1")), "Check for a dc:subject property");
            assertTrue(g.contains(subject, DC.title, rdf.createLiteral("Title")), "Check for a dc:title property");
            assertTrue(g.contains(subject, DC.alternative, rdf.createLiteral("Alternative Title")), "Check for a dc:alternative property");
            assertEquals(4L, g.stream().map(Triple::getPredicate).filter(isEqual(type).negate()).count(), "Check for four non-type triples");
        });
    }
}
Also used : Response(javax.ws.rs.core.Response) IRI(org.apache.commons.rdf.api.IRI) RDF(org.apache.commons.rdf.api.RDF) Dataset(org.apache.commons.rdf.api.Dataset)

Example 28 with Dataset

use of org.apache.commons.rdf.api.Dataset in project trellis by trellis-ldp.

the class ResourceServiceTests method testReplaceResource.

/**
 * Test replacing a resource.
 * @throws Exception if the RDF resources did not exit cleanly
 */
default void testReplaceResource() throws Exception {
    final RDF rdf = RDFFactory.getInstance();
    final IRI identifier = rdf.createIRI(getIdentifierPrefix() + getResourceService().generateIdentifier());
    try (final Dataset dataset = buildDataset(identifier, "Replacement Test", SUBJECT2)) {
        assertEquals(MISSING_RESOURCE, getResourceService().get(identifier).toCompletableFuture().join(), "Check for no pre-existing LDP-RS");
        assertDoesNotThrow(() -> getResourceService().create(Metadata.builder(identifier).interactionModel(LDP.RDFSource).container(rdf.createIRI(getIdentifierPrefix())).build(), dataset).toCompletableFuture().join(), "Check that the LDP-RS was successfully created");
        dataset.clear();
        dataset.add(Trellis.PreferUserManaged, identifier, SKOS.prefLabel, rdf.createLiteral("preferred label"));
        dataset.add(Trellis.PreferUserManaged, identifier, SKOS.altLabel, rdf.createLiteral("alternate label"));
        dataset.add(Trellis.PreferUserManaged, identifier, DC.type, SKOS.Concept);
        assertDoesNotThrow(() -> getResourceService().replace(Metadata.builder(identifier).interactionModel(LDP.RDFSource).container(rdf.createIRI(getIdentifierPrefix())).build(), dataset).toCompletableFuture().join(), "Check that the LDP-RS was successfully replaced");
        final Resource res = getResourceService().get(identifier).toCompletableFuture().join();
        assertAll("Check the replaced LDP-RS stream", res.stream(Trellis.PreferUserManaged).filter(q -> !q.getPredicate().equals(type)).map(q -> () -> assertTrue(dataset.contains(q), "Check that the quad comes from the dataset: " + q)));
        assertEquals(3L, res.stream(Trellis.PreferUserManaged).filter(q -> !q.getPredicate().equals(type)).count(), "Check the total user-managed triple count");
    }
}
Also used : Instant.now(java.time.Instant.now) Stream.generate(java.util.stream.Stream.generate) XSD(org.trellisldp.vocabulary.XSD) Optional.of(java.util.Optional.of) RDF.type(org.trellisldp.vocabulary.RDF.type) RDF(org.apache.commons.rdf.api.RDF) Resource(org.trellisldp.api.Resource) SKOS(org.trellisldp.vocabulary.SKOS) TrellisUtils.normalizeIdentifier(org.trellisldp.api.TrellisUtils.normalizeIdentifier) AS(org.trellisldp.vocabulary.AS) Metadata(org.trellisldp.api.Metadata) Arrays.asList(java.util.Arrays.asList) ResourceService(org.trellisldp.api.ResourceService) Graph(org.apache.commons.rdf.api.Graph) LDP(org.trellisldp.vocabulary.LDP) Dataset(org.apache.commons.rdf.api.Dataset) TRELLIS_BNODE_PREFIX(org.trellisldp.api.TrellisUtils.TRELLIS_BNODE_PREFIX) Collectors.toSet(java.util.stream.Collectors.toSet) DELETED_RESOURCE(org.trellisldp.api.Resource.SpecialResources.DELETED_RESOURCE) Trellis(org.trellisldp.vocabulary.Trellis) BinaryMetadata(org.trellisldp.api.BinaryMetadata) RDFFactory(org.trellisldp.api.RDFFactory) DC(org.trellisldp.vocabulary.DC) Predicate.isEqual(java.util.function.Predicate.isEqual) Set(java.util.Set) Instant(java.time.Instant) IRI(org.apache.commons.rdf.api.IRI) Stream(java.util.stream.Stream) Quad(org.apache.commons.rdf.api.Quad) PROV(org.trellisldp.vocabulary.PROV) Executable(org.junit.jupiter.api.function.Executable) Assertions(org.junit.jupiter.api.Assertions) TRELLIS_DATA_PREFIX(org.trellisldp.api.TrellisUtils.TRELLIS_DATA_PREFIX) FOAF(org.trellisldp.vocabulary.FOAF) MISSING_RESOURCE(org.trellisldp.api.Resource.SpecialResources.MISSING_RESOURCE) IRI(org.apache.commons.rdf.api.IRI) RDF(org.apache.commons.rdf.api.RDF) Dataset(org.apache.commons.rdf.api.Dataset) Resource(org.trellisldp.api.Resource)

Example 29 with Dataset

use of org.apache.commons.rdf.api.Dataset in project trellis by trellis-ldp.

the class ResourceServiceTests method buildDataset.

/**
 * Build a dataset.
 * @param resource the resource IRI
 * @param title a title
 * @param subject a subject
 * @return a new dataset
 */
default Dataset buildDataset(final IRI resource, final String title, final String subject) {
    final RDF rdf = RDFFactory.getInstance();
    final Dataset dataset = rdf.createDataset();
    dataset.add(Trellis.PreferUserManaged, resource, DC.title, rdf.createLiteral(title));
    dataset.add(Trellis.PreferUserManaged, resource, DC.subject, rdf.createIRI(subject));
    dataset.add(Trellis.PreferUserManaged, resource, DC.type, SKOS.Concept);
    return dataset;
}
Also used : RDF(org.apache.commons.rdf.api.RDF) Dataset(org.apache.commons.rdf.api.Dataset)

Example 30 with Dataset

use of org.apache.commons.rdf.api.Dataset in project trellis by trellis-ldp.

the class ResourceServiceTests method testLdpC.

/**
 * Test an LDP:Container.
 * @throws Exception if the RDF resources did not exit cleanly
 */
default void testLdpC() throws Exception {
    final Instant time = now();
    final RDF rdf = RDFFactory.getInstance();
    final String base = getIdentifierPrefix() + getResourceService().generateIdentifier() + SLASH;
    final IRI identifier = rdf.createIRI(base);
    final IRI child1 = rdf.createIRI(base + "child01");
    final IRI child2 = rdf.createIRI(base + "child02");
    final IRI normalized = normalizeIdentifier(identifier);
    try (final Dataset dataset0 = buildDataset(identifier, "Container Test", SUBJECT0);
        final Dataset dataset1 = buildDataset(child1, "Contained Child 1", SUBJECT1);
        final Dataset dataset2 = buildDataset(child2, "Contained Child2", SUBJECT2);
        final Graph graph = rdf.createGraph()) {
        assertEquals(MISSING_RESOURCE, getResourceService().get(normalized).toCompletableFuture().join(), "Check for no pre-existing LDP-C");
        assertDoesNotThrow(() -> getResourceService().create(Metadata.builder(normalized).interactionModel(LDP.Container).container(rdf.createIRI(getIdentifierPrefix())).build(), dataset0).toCompletableFuture().join(), "Check that the LDP-C is created successfully");
        assertEquals(MISSING_RESOURCE, getResourceService().get(child1).toCompletableFuture().join(), "Check for no child1 resource");
        assertDoesNotThrow(() -> getResourceService().create(Metadata.builder(child1).interactionModel(LDP.RDFSource).container(normalized).build(), dataset1).toCompletableFuture().join(), "Check that the first child was successfully created in the LDP-C");
        assertEquals(MISSING_RESOURCE, getResourceService().get(child2).toCompletableFuture().join(), "Check for no child2 resource");
        assertDoesNotThrow(() -> getResourceService().create(Metadata.builder(child2).interactionModel(LDP.RDFSource).container(normalized).build(), dataset2).toCompletableFuture().join(), "Check that the second child was successfully created in the LDP-C");
        final Resource res = getResourceService().get(normalized).toCompletableFuture().join();
        assertAll("Check the LDP-C resource", checkResource(res, normalized, LDP.Container, time, dataset0));
        assertEquals(2L, res.stream(LDP.PreferContainment).count(), "Check the containment triple count");
        res.stream(LDP.PreferContainment).map(Quad::asTriple).forEach(graph::add);
        assertTrue(graph.contains(identifier, LDP.contains, child1), "Check that child1 is contained in the LDP-C");
        assertTrue(graph.contains(identifier, LDP.contains, child2), "Check that child2 is contained in the LDP-C");
        assertEquals(3L, res.stream(Trellis.PreferUserManaged).filter(q -> !q.getPredicate().equals(type)).count(), "Check the user-managed triple count");
    }
}
Also used : IRI(org.apache.commons.rdf.api.IRI) Graph(org.apache.commons.rdf.api.Graph) RDF(org.apache.commons.rdf.api.RDF) Dataset(org.apache.commons.rdf.api.Dataset) Instant(java.time.Instant) Resource(org.trellisldp.api.Resource)

Aggregations

Dataset (org.apache.commons.rdf.api.Dataset)104 IRI (org.apache.commons.rdf.api.IRI)70 Test (org.junit.jupiter.api.Test)59 Resource (org.trellisldp.api.Resource)47 Instant (java.time.Instant)45 RDF (org.apache.commons.rdf.api.RDF)38 LDP (org.trellisldp.vocabulary.LDP)33 Stream (java.util.stream.Stream)30 Map (java.util.Map)28 BinaryMetadata (org.trellisldp.api.BinaryMetadata)28 RDFFactory (org.trellisldp.api.RDFFactory)28 Quad (org.apache.commons.rdf.api.Quad)27 Metadata (org.trellisldp.api.Metadata)27 TRELLIS_DATA_PREFIX (org.trellisldp.api.TrellisUtils.TRELLIS_DATA_PREFIX)27 MISSING_RESOURCE (org.trellisldp.api.Resource.SpecialResources.MISSING_RESOURCE)25 AS (org.trellisldp.vocabulary.AS)25 Trellis (org.trellisldp.vocabulary.Trellis)22 Logger (org.slf4j.Logger)21 LoggerFactory.getLogger (org.slf4j.LoggerFactory.getLogger)21 DELETED_RESOURCE (org.trellisldp.api.Resource.SpecialResources.DELETED_RESOURCE)21