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!");
}
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");
});
}
}
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");
}
}
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;
}
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");
}
}
Aggregations