use of org.apache.commons.rdf.api.Dataset in project trellis by trellis-ldp.
the class TriplestoreResourceServiceTest method testPutDeleteLdpC.
@Test
void testPutDeleteLdpC() {
final TriplestoreResourceService svc = new TriplestoreResourceService();
svc.idService = idService;
svc.rdfConnection = connect(wrap(toJena(rdf.createDataset())));
svc.initialize();
final Dataset dataset = rdf.createDataset();
dataset.add(Trellis.PreferUserManaged, resource, DC.title, rdf.createLiteral("resource title"));
dataset.add(Trellis.PreferUserManaged, resource, DC.description, rdf.createLiteral("resource description"));
dataset.add(Trellis.PreferAudit, rdf.createBlankNode(), type, AS.Create);
final Instant later = meanwhile();
assertDoesNotThrow(() -> allOf(svc.create(builder(resource).interactionModel(LDP.Container).container(root).build(), dataset).toCompletableFuture(), svc.touch(root).toCompletableFuture()).join(), "Unsuccessful create operation!");
allOf(svc.get(resource).thenAccept(checkResource(later, LDP.Container, 2L, 1L, 0L)).toCompletableFuture(), svc.get(root).thenAccept(checkRoot(later, 1L)).toCompletableFuture()).join();
// Now add a child resource
dataset.clear();
dataset.add(Trellis.PreferAudit, rdf.createBlankNode(), type, AS.Create);
dataset.add(Trellis.PreferUserManaged, child, DC.title, rdf.createLiteral("child"));
dataset.add(Trellis.PreferUserManaged, child, DC.description, rdf.createLiteral("nested resource"));
final Instant evenLater = meanwhile();
assertDoesNotThrow(() -> allOf(svc.create(builder(child).interactionModel(LDP.RDFSource).container(resource).build(), dataset).toCompletableFuture(), svc.touch(resource).toCompletableFuture()).join(), "Unsuccessful create operation!");
allOf(svc.get(child).thenAccept(checkChild(evenLater, 2L, 1L)).toCompletableFuture(), svc.get(resource).thenAccept(checkResource(evenLater, LDP.Container, 2L, 1L, 1L)).toCompletableFuture(), svc.get(root).thenAccept(checkRoot(later, 1L)).toCompletableFuture(), svc.get(root).thenAccept(checkPredates(evenLater)).toCompletableFuture()).join();
// Now delete the child resource
final BlankNode bnode = rdf.createBlankNode();
dataset.clear();
dataset.add(Trellis.PreferAudit, bnode, type, AS.Delete);
dataset.add(Trellis.PreferAudit, bnode, type, PROV.Activity);
dataset.add(Trellis.PreferServerManaged, child, type, LDP.Resource);
final Instant preDelete = meanwhile();
assertDoesNotThrow(() -> allOf(svc.delete(builder(child).interactionModel(LDP.RDFSource).container(resource).build()).toCompletableFuture(), svc.touch(resource).toCompletableFuture()).join(), "Unsuccessful delete operation!");
allOf(svc.get(child).thenAccept(res -> assertEquals(DELETED_RESOURCE, res, "Incorrect resource object!")).toCompletableFuture(), svc.get(resource).thenAccept(checkResource(preDelete, LDP.Container, 2L, 1L, 0L)).toCompletableFuture(), svc.get(root).thenAccept(checkRoot(later, 1L)).toCompletableFuture(), svc.get(root).thenAccept(checkPredates(preDelete)).toCompletableFuture()).join();
}
use of org.apache.commons.rdf.api.Dataset in project trellis by trellis-ldp.
the class TriplestoreResourceServiceTest method testPutLdpDc.
@Test
void testPutLdpDc() {
final TriplestoreResourceService svc = new TriplestoreResourceService();
svc.rdfConnection = connect(wrap(toJena(rdf.createDataset())));
svc.idService = idService;
svc.initialize();
final Dataset dataset = rdf.createDataset();
dataset.add(Trellis.PreferUserManaged, resource, DC.title, rdf.createLiteral("direct container"));
dataset.add(Trellis.PreferUserManaged, resource, DC.description, rdf.createLiteral("LDP-DC test"));
dataset.add(Trellis.PreferUserManaged, resource, LDP.membershipResource, members);
dataset.add(Trellis.PreferUserManaged, resource, LDP.hasMemberRelation, DC.relation);
final Instant later = meanwhile();
assertDoesNotThrow(() -> allOf(svc.create(builder(resource).interactionModel(LDP.DirectContainer).container(root).memberRelation(DC.relation).membershipResource(members).build(), dataset).toCompletableFuture(), svc.touch(root).toCompletableFuture()).join(), "Unsuccessful create operation!");
allOf(svc.get(resource).thenAccept(checkResource(later, LDP.DirectContainer, 4L, 0L, 0L)).toCompletableFuture(), svc.get(root).thenAccept(checkRoot(later, 1L)).toCompletableFuture()).join();
// Now add a membership resource
dataset.clear();
dataset.add(Trellis.PreferUserManaged, members, DC.title, rdf.createLiteral("member resource"));
final Instant evenLater = meanwhile();
assertDoesNotThrow(() -> allOf(svc.create(builder(members).interactionModel(LDP.RDFSource).container(root).build(), dataset).toCompletableFuture(), svc.touch(root).toCompletableFuture()).join(), "Unsuccessful create operation!");
allOf(svc.get(members).thenAccept(checkMember(evenLater, 1L, 0L, 0L)).toCompletableFuture(), svc.get(members).thenAccept(res -> assertFalse(res.getBinaryMetadata().isPresent(), "Unexpected binary metadata!")).toCompletableFuture(), svc.get(resource).thenAccept(checkResource(later, LDP.DirectContainer, 4L, 0L, 0L)).toCompletableFuture(), svc.get(resource).thenAccept(checkPredates(evenLater)).toCompletableFuture(), svc.get(root).thenAccept(checkRoot(evenLater, 2L)).toCompletableFuture()).join();
// Now add the child resources to the ldp-dc
dataset.clear();
dataset.add(Trellis.PreferUserManaged, child, DC.title, rdf.createLiteral("ldp-dc child resource"));
final Instant evenLater2 = meanwhile();
assertDoesNotThrow(() -> allOf(svc.create(builder(child).interactionModel(LDP.RDFSource).container(resource).build(), dataset).toCompletableFuture(), svc.touch(members).toCompletableFuture(), svc.touch(resource).toCompletableFuture()).join(), "Unsuccessful create operation!");
final IRI container = rdf.createIRI(resource.getIRIString() + "/");
allOf(svc.get(child).thenAccept(checkChild(evenLater2, 1L, 0L)).toCompletableFuture(), svc.get(resource).thenAccept(checkResource(evenLater2, LDP.DirectContainer, 4L, 0L, 1L)).toCompletableFuture(), svc.get(resource).thenAccept(res -> assertTrue(res.stream(LDP.PreferContainment).anyMatch(isEqual(rdf.createQuad(LDP.PreferContainment, container, LDP.contains, child))), "Missing contains triple!")).toCompletableFuture(), svc.get(members).thenAccept(checkMember(evenLater2, 1L, 0L, 1L)).toCompletableFuture(), svc.get(members).thenAccept(res -> assertTrue(res.stream(LDP.PreferMembership).anyMatch(isEqual(rdf.createQuad(LDP.PreferMembership, members, DC.relation, child))), "Missing membership triple!")).toCompletableFuture(), svc.get(root).thenAccept(checkRoot(evenLater, 2L)).toCompletableFuture(), svc.get(root).thenAccept(checkPredates(evenLater2)).toCompletableFuture()).join();
}
use of org.apache.commons.rdf.api.Dataset in project trellis by trellis-ldp.
the class TriplestoreResourceServiceTest method testPutLdpDcMultipleInverse.
@Test
void testPutLdpDcMultipleInverse() {
final TriplestoreResourceService svc = new TriplestoreResourceService();
svc.rdfConnection = connect(wrap(toJena(rdf.createDataset())));
svc.idService = idService;
svc.initialize();
final IRI container = rdf.createIRI(resource.getIRIString() + "/");
final IRI container2 = rdf.createIRI(resource2.getIRIString() + "/");
final Dataset dataset = rdf.createDataset();
dataset.add(Trellis.PreferUserManaged, resource, DC.title, rdf.createLiteral("direct container inverse"));
dataset.add(Trellis.PreferUserManaged, resource, RDFS.label, rdf.createLiteral("LDP-DC test"));
dataset.add(Trellis.PreferUserManaged, resource, DC.description, rdf.createLiteral("LDP-DC inverse test"));
dataset.add(Trellis.PreferUserManaged, resource, LDP.membershipResource, members);
dataset.add(Trellis.PreferUserManaged, resource, LDP.isMemberOfRelation, DC.relation);
dataset.add(Trellis.PreferAudit, rdf.createBlankNode(), type, AS.Create);
final Instant later = meanwhile();
assertDoesNotThrow(() -> allOf(svc.create(builder(resource).interactionModel(LDP.DirectContainer).container(root).membershipResource(members).memberOfRelation(DC.relation).build(), dataset).toCompletableFuture(), svc.touch(root).toCompletableFuture()).join(), "Unsuccessful create operation!");
allOf(svc.get(resource).thenAccept(checkResource(later, LDP.DirectContainer, 5L, 1L, 0L)).toCompletableFuture(), svc.get(root).thenAccept(checkRoot(later, 1L)).toCompletableFuture()).join();
dataset.clear();
dataset.add(Trellis.PreferUserManaged, resource2, DC.title, rdf.createLiteral("Second LDP-DC"));
dataset.add(Trellis.PreferUserManaged, resource2, LDP.membershipResource, members);
dataset.add(Trellis.PreferUserManaged, resource2, LDP.isMemberOfRelation, DC.subject);
dataset.add(Trellis.PreferAudit, rdf.createBlankNode(), type, AS.Create);
final Instant evenLater = meanwhile();
assertDoesNotThrow(() -> allOf(svc.create(builder(resource2).interactionModel(LDP.DirectContainer).container(root).membershipResource(members).memberOfRelation(DC.subject).build(), dataset).toCompletableFuture(), svc.touch(root).toCompletableFuture()).join(), "Unsuccessful create operation!");
allOf(svc.get(resource2).thenAccept(res -> assertAll("Check resource", () -> checkResource(res, resource2, LDP.DirectContainer, evenLater), () -> checkResourceStream(res, 3L, 0L, 1L, 0L, 0L))).toCompletableFuture(), svc.get(root).thenAccept(checkRoot(evenLater, 2L)).toCompletableFuture()).join();
// Now add a membership resource
dataset.clear();
dataset.add(Trellis.PreferUserManaged, members, DC.title, rdf.createLiteral("Membership resource"));
dataset.add(Trellis.PreferAudit, rdf.createBlankNode(), type, AS.Create);
final Instant evenLater2 = meanwhile();
assertDoesNotThrow(() -> allOf(svc.create(builder(members).interactionModel(LDP.RDFSource).container(root).build(), dataset).toCompletableFuture(), svc.touch(root).toCompletableFuture()).join(), "Unsuccessful membership resource create operation!");
allOf(svc.get(members).thenAccept(checkMember(evenLater2, 1L, 1L, 0L)).toCompletableFuture(), svc.get(resource).thenAccept(res -> assertAll("Check resource", () -> checkResource(later, LDP.DirectContainer, 5L, 1L, 0L).accept(res), () -> checkPredates(evenLater2).accept(res))).toCompletableFuture(), svc.get(resource2).thenAccept(res -> assertAll("Check second resource", () -> checkPredates(evenLater2).accept(res), () -> checkResourceStream(res, 3L, 0L, 1L, 0L, 0L))).toCompletableFuture(), svc.get(root).thenAccept(checkRoot(evenLater2, 3L)).toCompletableFuture()).join();
// Now add the child resources to the ldp-dc
dataset.clear();
dataset.add(Trellis.PreferUserManaged, child, DC.title, rdf.createLiteral("Child resource"));
dataset.add(Trellis.PreferAudit, rdf.createBlankNode(), type, AS.Create);
final Instant evenLater3 = meanwhile();
assertDoesNotThrow(() -> allOf(svc.create(builder(child).interactionModel(LDP.RDFSource).container(resource).build(), dataset).toCompletableFuture(), svc.touch(resource).toCompletableFuture()).join(), "Unsuccessful create operation!");
allOf(svc.get(child).thenAccept(res -> assertAll("Check resource", () -> checkResource(res, child, LDP.RDFSource, evenLater3), () -> checkResourceStream(res, 1L, 0L, 1L, 1L, 0L))).toCompletableFuture(), svc.get(resource).thenAccept(res -> assertAll("Check resource", () -> checkResource(evenLater3, LDP.DirectContainer, 5L, 1L, 1L).accept(res), () -> assertTrue(res.stream(LDP.PreferContainment).anyMatch(isEqual(rdf.createQuad(LDP.PreferContainment, container, LDP.contains, child))), "Missing contains triple!"))).toCompletableFuture(), svc.get(members).thenAccept(res -> assertAll("Check member resource", () -> checkMember(evenLater2, 1L, 1L, 0L).accept(res), () -> checkPredates(evenLater3).accept(res))).toCompletableFuture(), svc.get(root).thenAccept(res -> assertAll("Check root resource", () -> checkRoot(evenLater2, 3L).accept(res), () -> checkPredates(evenLater3).accept(res))).toCompletableFuture()).join();
// Now add a child resources to the other ldp-dc
dataset.clear();
dataset.add(Trellis.PreferAudit, rdf.createBlankNode(), type, AS.Create);
dataset.add(Trellis.PreferUserManaged, child2, DC.title, rdf.createLiteral("Second child resource"));
final Instant evenLater4 = meanwhile();
assertDoesNotThrow(() -> allOf(svc.create(builder(child2).interactionModel(LDP.RDFSource).container(resource2).build(), dataset).toCompletableFuture(), svc.touch(resource2).toCompletableFuture()).join(), "Unsuccessful create operation!");
allOf(svc.get(child2).thenAccept(res -> assertAll("Check child resource", () -> checkResource(res, child2, LDP.RDFSource, evenLater4), () -> checkResourceStream(res, 1L, 0L, 1L, 1L, 0L))).toCompletableFuture(), svc.get(resource2).thenAccept(res -> assertAll("Check resource", () -> checkResource(res, resource2, LDP.DirectContainer, evenLater4), () -> checkResourceStream(res, 3L, 0L, 1L, 0L, 1L), () -> assertTrue(res.stream(LDP.PreferContainment).anyMatch(isEqual(rdf.createQuad(LDP.PreferContainment, container2, LDP.contains, child2))), "Missing contains triple!"))).toCompletableFuture(), svc.get(members).thenAccept(res -> assertAll("Check member resource", () -> checkMember(evenLater2, 1L, 1L, 0L).accept(res), () -> checkPredates(evenLater4).accept(res))).toCompletableFuture(), svc.get(root).thenAccept(res -> assertAll("Check root resource", () -> checkRoot(evenLater2, 3L).accept(res), () -> checkPredates(evenLater4).accept(res))).toCompletableFuture()).join();
}
use of org.apache.commons.rdf.api.Dataset in project trellis by trellis-ldp.
the class TriplestoreResourceService method delete.
@Override
public CompletionStage<Void> delete(final Metadata metadata) {
LOGGER.debug("Deleting: {}", metadata.getIdentifier());
return runAsync(() -> {
try (final Dataset dataset = rdf.createDataset()) {
final Instant eventTime = now();
dataset.add(PreferServerManaged, metadata.getIdentifier(), DC.type, DeletedResource);
dataset.add(PreferServerManaged, metadata.getIdentifier(), type, LDP.Resource);
storeResource(metadata.getIdentifier(), dataset, eventTime, OperationType.DELETE);
} catch (final Exception ex) {
throw new TrellisRuntimeException("Error deleting resource: " + metadata.getIdentifier(), ex);
}
});
}
use of org.apache.commons.rdf.api.Dataset in project trellis by trellis-ldp.
the class TriplestoreResourceService method buildUpdateRequest.
/**
* This is equivalent to the SPARQL below.
*
* <p><pre><code>
* DELETE WHERE { GRAPH IDENTIFIER { ?s ?p ?o } };
* DELETE WHERE { GRAPH IDENTIFIER?ext=acl { ?s ?p ?o } };
* DELETE WHERE { GRAPH trellis:PreferServerManaged {
* IDENTIFIER a ldp:NonRDFSource .
* IDENTIFIER dc:hasPart ?s .
* ?s ?p ?o .
* };
* DELETE WHERE { GRAPH trellis:PreferServerManaged { IDENTIFIER ?p ?o } };
* INSERT DATA {
* GRAPH IDENTIFIER { ... }
* GRAPH IDENTIFIER?ext=acl { ... }
* GRAPH trellis:PreferServerManaged { ... }
* GRAPH IDENTIFIER?ext=audit { ... }
* }
* </code></pre></p>
*/
private UpdateRequest buildUpdateRequest(final IRI identifier, final Literal time, final Dataset dataset, final OperationType operation) {
// Set the time
dataset.add(PreferServerManaged, identifier, DC.modified, time);
final UpdateRequest req = new UpdateRequest();
req.add(new UpdateDeleteWhere(new QuadAcc(singletonList(new Quad(toJena(identifier), SUBJECT, PREDICATE, OBJECT)))));
extensions.forEach((ext, graph) -> req.add(new UpdateDeleteWhere(new QuadAcc(singletonList(new Quad(getExtIRI(identifier, ext), SUBJECT, PREDICATE, OBJECT))))));
req.add(new UpdateDeleteWhere(new QuadAcc(asList(new Quad(toJena(PreferServerManaged), toJena(identifier), toJena(type), toJena(LDP.NonRDFSource)), new Quad(toJena(PreferServerManaged), toJena(identifier), toJena(DC.hasPart), SUBJECT), new Quad(toJena(PreferServerManaged), SUBJECT, PREDICATE, OBJECT)))));
req.add(new UpdateDeleteWhere(new QuadAcc(singletonList(new Quad(toJena(PreferServerManaged), toJena(identifier), PREDICATE, OBJECT)))));
final QuadDataAcc sink = new QuadDataAcc(synchronizedList(new ArrayList<>()));
if (operation == OperationType.DELETE) {
dataset.stream().filter(q -> q.getGraphName().filter(PreferServerManaged::equals).isPresent()).map(JenaCommonsRDF::toJena).forEach(sink::addQuad);
} else {
dataset.stream().filter(q -> q.getGraphName().filter(PreferServerManaged::equals).isPresent()).map(JenaCommonsRDF::toJena).forEach(sink::addQuad);
dataset.getGraph(PreferUserManaged).ifPresent(g -> g.stream().map(t -> new Quad(toJena(identifier), toJena(t))).forEach(sink::addQuad));
dataset.getGraph(PreferAudit).ifPresent(g -> g.stream().map(t -> new Quad(getExtIRI(identifier, "audit"), toJena(t))).forEach(sink::addQuad));
extensions.forEach((ext, graph) -> dataset.getGraph(graph).ifPresent(g -> g.stream().map(t -> new Quad(getExtIRI(identifier, ext), toJena(t))).forEach(sink::addQuad)));
}
req.add(new UpdateDataInsert(sink));
return req;
}
Aggregations