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