Search in sources :

Example 1 with Resource

use of org.trellisldp.api.Resource in project trellis by trellis-ldp.

the class DBResourceTest method testMetadata.

@Test
void testMetadata() {
    final Resource res = DBResource.findResource(ds, root, extensions, false).toCompletableFuture().join();
    assertFalse(res.hasMetadata(Trellis.PreferAccessControl));
    assertFalse(res.hasMetadata(rdf.createIRI("http://example.com/Extension")));
}
Also used : Resource(org.trellisldp.api.Resource) Test(org.junit.jupiter.api.Test)

Example 2 with Resource

use of org.trellisldp.api.Resource in project trellis by trellis-ldp.

the class DBWrappedMementoServiceTest method testMementoService.

@Test
void testMementoService() {
    final Resource mockResource = mock(Resource.class);
    when(mockMementoService.put(any(Resource.class))).thenAnswer(inv -> completedFuture(null));
    when(mockMementoService.get(any(IRI.class), any(Instant.class))).thenAnswer(inv -> completedFuture(mockResource));
    final MementoService svc = new DBWrappedMementoService(ds, mockMementoService);
    final Instant time = now();
    final IRI identifier = rdf.createIRI("trellis:data/resource");
    when(mockResource.getIdentifier()).thenReturn(identifier);
    when(mockResource.getModified()).thenReturn(time);
    assertDoesNotThrow(svc.put(mockResource).toCompletableFuture()::join);
    assertDoesNotThrow(svc.put(mockResource).toCompletableFuture()::join);
    when(mockResource.getModified()).thenReturn(time.plusSeconds(2L));
    assertDoesNotThrow(svc.put(mockResource).toCompletableFuture()::join);
    when(mockResource.getModified()).thenReturn(time.plusSeconds(4L));
    assertDoesNotThrow(svc.put(mockResource).toCompletableFuture()::join);
    final SortedSet<Instant> mementos = svc.mementos(identifier).toCompletableFuture().join();
    assertTrue(mementos.contains(time.truncatedTo(SECONDS)));
    assertTrue(mementos.contains(time.plusSeconds(2L).truncatedTo(SECONDS)));
    assertTrue(mementos.contains(time.plusSeconds(4L).truncatedTo(SECONDS)));
    when(mockResource.getModified()).thenReturn(time);
    final Resource res = svc.get(identifier, time).toCompletableFuture().join();
    assertEquals(time, res.getModified());
}
Also used : IRI(org.apache.commons.rdf.api.IRI) Instant(java.time.Instant) Resource(org.trellisldp.api.Resource) NoopMementoService(org.trellisldp.api.NoopMementoService) MementoService(org.trellisldp.api.MementoService) Test(org.junit.jupiter.api.Test)

Example 3 with Resource

use of org.trellisldp.api.Resource in project trellis by trellis-ldp.

the class DBWrappedMementoServiceTest method testMementoUtils.

@Test
void testMementoUtils() {
    final Resource mockResource = mock(Resource.class);
    when(mockMementoService.put(any(Resource.class))).thenAnswer(inv -> completedFuture(null));
    when(mockMementoService.get(any(IRI.class), any(Instant.class))).thenAnswer(inv -> completedFuture(mockResource));
    final MementoService svc = new DBWrappedMementoService(ds, mockMementoService);
    final Instant time = now();
    final IRI identifier = rdf.createIRI("trellis:data/resource");
    when(mockResource.getIdentifier()).thenReturn(identifier);
    when(mockResource.getModified()).thenReturn(time);
    assertDoesNotThrow(svc.put(mockResource).toCompletableFuture()::join);
    assertDoesNotThrow(svc.put(mockResource).toCompletableFuture()::join);
    when(mockResource.getModified()).thenReturn(time.plusSeconds(2L));
    assertDoesNotThrow(svc.put(mockResource).toCompletableFuture()::join);
    when(mockResource.getModified()).thenReturn(time.plusSeconds(4L));
    assertDoesNotThrow(svc.put(mockResource).toCompletableFuture()::join);
    final SortedSet<Instant> mementos = svc.mementos(identifier).toCompletableFuture().join();
    assertTrue(mementos.contains(time.truncatedTo(SECONDS)));
    assertTrue(mementos.contains(time.plusSeconds(2L).truncatedTo(SECONDS)));
    assertTrue(mementos.contains(time.plusSeconds(4L).truncatedTo(SECONDS)));
    when(mockResource.getModified()).thenReturn(time);
    final Resource res = svc.get(identifier, time).toCompletableFuture().join();
    assertEquals(time, res.getModified());
}
Also used : IRI(org.apache.commons.rdf.api.IRI) Instant(java.time.Instant) Resource(org.trellisldp.api.Resource) NoopMementoService(org.trellisldp.api.NoopMementoService) MementoService(org.trellisldp.api.MementoService) Test(org.junit.jupiter.api.Test)

Example 4 with Resource

use of org.trellisldp.api.Resource 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 5 with Resource

use of org.trellisldp.api.Resource in project trellis by trellis-ldp.

the class FileMementoServiceTest method testPutBinary.

@Test
void testPutBinary() {
    final File dir = new File(getClass().getResource("/versions").getFile());
    final FileMementoService svc = new FileMementoService();
    svc.directoryPath = dir.getAbsolutePath();
    svc.algorithm = SHA_256;
    svc.includeLdpType = true;
    svc.enabled = true;
    svc.init();
    final IRI identifier = rdf.createIRI("trellis:data/a-binary");
    final IRI binaryId = rdf.createIRI("file:binary");
    final IRI root = rdf.createIRI("trellis:data/");
    final Instant time = now();
    final String mimeType = "text/plain";
    final Resource mockResource = mock(Resource.class);
    when(mockResource.getIdentifier()).thenReturn(identifier);
    when(mockResource.getInteractionModel()).thenReturn(LDP.NonRDFSource);
    when(mockResource.getModified()).thenReturn(time);
    when(mockResource.getContainer()).thenReturn(of(root));
    when(mockResource.stream()).thenAnswer(inv -> Stream.of(rdf.createQuad(Trellis.PreferUserManaged, identifier, DC.title, rdf.createLiteral("Title")), rdf.createQuad(Trellis.PreferServerManaged, identifier, DC.isPartOf, root)));
    when(mockResource.getBinaryMetadata()).thenReturn(of(BinaryMetadata.builder(binaryId).mimeType(mimeType).build()));
    when(mockResource.getMemberOfRelation()).thenReturn(empty());
    when(mockResource.getMemberRelation()).thenReturn(empty());
    when(mockResource.getMembershipResource()).thenReturn(empty());
    when(mockResource.getInsertedContentRelation()).thenReturn(empty());
    svc.put(mockResource).toCompletableFuture().join();
    final Resource res = svc.get(identifier, time).toCompletableFuture().join();
    assertEquals(identifier, res.getIdentifier());
    assertEquals(time, res.getModified());
    assertEquals(LDP.NonRDFSource, res.getInteractionModel());
    assertEquals(of(root), res.getContainer());
    assertTrue(res.getBinaryMetadata().isPresent());
    res.getBinaryMetadata().ifPresent(b -> {
        assertEquals(binaryId, b.getIdentifier());
        assertEquals(of(mimeType), b.getMimeType());
    });
    assertFalse(res.getMemberOfRelation().isPresent());
    assertFalse(res.getMemberRelation().isPresent());
    assertFalse(res.getMembershipResource().isPresent());
    assertFalse(res.getInsertedContentRelation().isPresent());
    assertEquals(1L, res.stream(Trellis.PreferUserManaged).count());
}
Also used : IRI(org.apache.commons.rdf.api.IRI) Instant(java.time.Instant) Resource(org.trellisldp.api.Resource) File(java.io.File) Test(org.junit.jupiter.api.Test)

Aggregations

Resource (org.trellisldp.api.Resource)74 IRI (org.apache.commons.rdf.api.IRI)65 Instant (java.time.Instant)46 Dataset (org.apache.commons.rdf.api.Dataset)46 Test (org.junit.jupiter.api.Test)40 RDF (org.apache.commons.rdf.api.RDF)36 LDP (org.trellisldp.vocabulary.LDP)35 Stream (java.util.stream.Stream)32 BinaryMetadata (org.trellisldp.api.BinaryMetadata)31 Map (java.util.Map)30 RDFFactory (org.trellisldp.api.RDFFactory)29 Quad (org.apache.commons.rdf.api.Quad)27 TRELLIS_DATA_PREFIX (org.trellisldp.api.TrellisUtils.TRELLIS_DATA_PREFIX)27 AS (org.trellisldp.vocabulary.AS)26 MISSING_RESOURCE (org.trellisldp.api.Resource.SpecialResources.MISSING_RESOURCE)25 File (java.io.File)23 Metadata (org.trellisldp.api.Metadata)23 Logger (org.slf4j.Logger)22 DELETED_RESOURCE (org.trellisldp.api.Resource.SpecialResources.DELETED_RESOURCE)22 CompletionStage (java.util.concurrent.CompletionStage)21