Search in sources :

Example 1 with Dataset

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

the class DBResourceTest method testDirectContainerInverse.

@Test
void testDirectContainerInverse() {
    final IRI dc = rdf.createIRI(TRELLIS_DATA_PREFIX + idService.getSupplier().get());
    final IRI child = rdf.createIRI(dc.getIRIString() + "/" + idService.getSupplier().get());
    final IRI member = rdf.createIRI(TRELLIS_DATA_PREFIX + idService.getSupplier().get());
    final Dataset dcDataset = rdf.createDataset();
    dcDataset.add(Trellis.PreferUserManaged, dc, LDP.isMemberOfRelation, DC.isPartOf);
    dcDataset.add(Trellis.PreferUserManaged, dc, LDP.membershipResource, member);
    assertDoesNotThrow(() -> allOf(svc.create(builder(dc).interactionModel(LDP.DirectContainer).container(root).memberOfRelation(DC.isPartOf).membershipResource(member).build(), dcDataset).toCompletableFuture(), svc.create(builder(member).interactionModel(LDP.Container).container(root).build(), rdf.createDataset()).toCompletableFuture(), svc.create(builder(child).interactionModel(LDP.RDFSource).container(dc).build(), rdf.createDataset()).toCompletableFuture()).join());
    svc.get(child).thenAccept(res -> {
        assertEquals(1L, res.stream(LDP.PreferMembership).count());
        assertEquals(of(rdf.createQuad(LDP.PreferMembership, child, DC.isPartOf, member)), res.stream(LDP.PreferMembership).findFirst());
    }).toCompletableFuture().join();
}
Also used : IRI(org.apache.commons.rdf.api.IRI) Dataset(org.apache.commons.rdf.api.Dataset) Test(org.junit.jupiter.api.Test)

Example 2 with Dataset

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

the class DBUtilsTest method testCloseDatasetError.

@Test
void testCloseDatasetError() throws Exception {
    final Dataset mockDataset = mock(Dataset.class);
    doThrow(IOException.class).when(mockDataset).close();
    assertThrows(TrellisRuntimeException.class, () -> DBUtils.closeDataset(mockDataset));
}
Also used : Dataset(org.apache.commons.rdf.api.Dataset) Test(org.junit.jupiter.api.Test)

Example 3 with Dataset

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

the class DBResourceService method storeResource.

private void storeResource(final Metadata metadata, final Dataset dataset, final Instant time, final OperationType opType) {
    try {
        jdbi.useTransaction(handle -> {
            final int resourceId = updateResource(handle, metadata, dataset, time, opType == OperationType.DELETE);
            updateDescription(handle, resourceId, dataset, batchSize);
            updateAcl(handle, resourceId, dataset, batchSize);
            updateExtra(handle, resourceId, metadata.getIdentifier(), dataset);
            extensions.forEach((ext, graph) -> dataset.getGraph(graph).filter(g -> !"acl".equals(ext)).ifPresent(g -> updateExtension(handle, resourceId, ext, g)));
            if (opType == OperationType.DELETE) {
                // Verify that the container really is empty
                final String query = "SELECT EXISTS(SELECT 1 FROM resource WHERE is_part_of = ?)";
                if (Boolean.TRUE.equals(handle.select(query, metadata.getIdentifier().getIRIString()).map((rs, ctx) -> rs.getBoolean(1)).one())) {
                    throw new StorageConflictException("Cannot delete non-empty containers");
                }
            }
        });
    } catch (final TrellisRuntimeException ex) {
        throw ex;
    } catch (final Exception ex) {
        throw new TrellisRuntimeException("Could not update data for " + metadata.getIdentifier(), ex);
    }
}
Also used : IdentifierService(org.trellisldp.api.IdentifierService) TrellisRuntimeException(org.trellisldp.api.TrellisRuntimeException) PreparedBatch(org.jdbi.v3.core.statement.PreparedBatch) Optional.of(java.util.Optional.of) NTRIPLES(org.apache.jena.riot.Lang.NTRIPLES) Resource(org.trellisldp.api.Resource) PreferAudit(org.trellisldp.vocabulary.Trellis.PreferAudit) Collectors.toMap(java.util.stream.Collectors.toMap) Arrays.asList(java.util.Arrays.asList) JenaCommonsRDF.toJena(org.apache.jena.commonsrdf.JenaCommonsRDF.toJena) Map(java.util.Map) Handle(org.jdbi.v3.core.Handle) Graph(org.apache.commons.rdf.api.Graph) Set(java.util.Set) Instant(java.time.Instant) UncheckedIOException(java.io.UncheckedIOException) CompletionStage(java.util.concurrent.CompletionStage) StorageConflictException(org.trellisldp.api.StorageConflictException) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) ApplicationScoped(javax.enterprise.context.ApplicationScoped) Arrays.stream(java.util.Arrays.stream) Instant.now(java.time.Instant.now) DBUtils.getObjectLang(org.trellisldp.jdbc.DBUtils.getObjectLang) PreferAccessControl(org.trellisldp.vocabulary.Trellis.PreferAccessControl) Supplier(java.util.function.Supplier) RDF(org.apache.commons.rdf.api.RDF) Triple(org.apache.commons.rdf.api.Triple) HashSet(java.util.HashSet) Inject(javax.inject.Inject) Metadata(org.trellisldp.api.Metadata) ResourceService(org.trellisldp.api.ResourceService) DBUtils.getObjectDatatype(org.trellisldp.jdbc.DBUtils.getObjectDatatype) DataSource(javax.sql.DataSource) Collections.singletonMap(java.util.Collections.singletonMap) LDP(org.trellisldp.vocabulary.LDP) Dataset(org.apache.commons.rdf.api.Dataset) DBUtils.getObjectValue(org.trellisldp.jdbc.DBUtils.getObjectValue) Jdbi(org.jdbi.v3.core.Jdbi) BinaryMetadata(org.trellisldp.api.BinaryMetadata) RDFFactory(org.trellisldp.api.RDFFactory) CompletableFuture.runAsync(java.util.concurrent.CompletableFuture.runAsync) Logger(org.slf4j.Logger) StringWriter(java.io.StringWriter) IOException(java.io.IOException) TrellisUtils(org.trellisldp.api.TrellisUtils) IRI(org.apache.commons.rdf.api.IRI) OA(org.trellisldp.vocabulary.OA) Collections.unmodifiableSet(java.util.Collections.unmodifiableSet) RDFDataMgr(org.apache.jena.riot.RDFDataMgr) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) Update(org.jdbi.v3.core.statement.Update) ConfigProperty(org.eclipse.microprofile.config.inject.ConfigProperty) PreferUserManaged(org.trellisldp.vocabulary.Trellis.PreferUserManaged) TrellisRuntimeException(org.trellisldp.api.TrellisRuntimeException) StorageConflictException(org.trellisldp.api.StorageConflictException) TrellisRuntimeException(org.trellisldp.api.TrellisRuntimeException) UncheckedIOException(java.io.UncheckedIOException) StorageConflictException(org.trellisldp.api.StorageConflictException) IOException(java.io.IOException)

Example 4 with Dataset

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

the class DBResourceTest method testDirectContainer.

@Test
void testDirectContainer() {
    final IRI member = rdf.createIRI(TRELLIS_DATA_PREFIX + idService.getSupplier().get());
    final IRI dc = rdf.createIRI(TRELLIS_DATA_PREFIX + idService.getSupplier().get());
    final IRI child = rdf.createIRI(dc.getIRIString() + "/" + idService.getSupplier().get());
    final Dataset dcDataset = rdf.createDataset();
    dcDataset.add(Trellis.PreferUserManaged, dc, LDP.hasMemberRelation, LDP.member);
    dcDataset.add(Trellis.PreferUserManaged, dc, LDP.membershipResource, member);
    assertDoesNotThrow(() -> allOf(svc.create(builder(dc).interactionModel(LDP.DirectContainer).container(root).memberRelation(LDP.member).membershipResource(member).build(), dcDataset).toCompletableFuture(), svc.create(builder(member).interactionModel(LDP.RDFSource).container(root).build(), rdf.createDataset()).toCompletableFuture(), svc.create(builder(child).interactionModel(LDP.RDFSource).container(dc).build(), rdf.createDataset()).toCompletableFuture()).join());
    svc.get(member).thenAccept(res -> {
        assertEquals(1L, res.stream(LDP.PreferMembership).count());
        assertEquals(of(rdf.createQuad(LDP.PreferMembership, member, LDP.member, child)), res.stream(LDP.PreferMembership).findFirst());
    }).toCompletableFuture().join();
}
Also used : IRI(org.apache.commons.rdf.api.IRI) Dataset(org.apache.commons.rdf.api.Dataset) Test(org.junit.jupiter.api.Test)

Example 5 with Dataset

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

the class DBResourceTest method testAddErrorCondition.

@Test
void testAddErrorCondition() {
    final IRI identifier = rdf.createIRI(TRELLIS_DATA_PREFIX + "resource");
    final Dataset dataset = rdf.createDataset();
    dataset.add(Trellis.PreferAudit, rdf.createBlankNode(), type, rdf.createLiteral("Invalid quad"));
    final CompletableFuture<Void> future = svc.add(identifier, dataset).toCompletableFuture();
    assertThrows(CompletionException.class, future::join);
}
Also used : IRI(org.apache.commons.rdf.api.IRI) Dataset(org.apache.commons.rdf.api.Dataset) Test(org.junit.jupiter.api.Test)

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