Search in sources :

Example 1 with MementoService

use of org.trellisldp.api.MementoService 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 2 with MementoService

use of org.trellisldp.api.MementoService 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 3 with MementoService

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

the class S3MementoServiceTest method testMementoRDFSource.

@Test
void testMementoRDFSource() {
    final IRI identifier = rdf.createIRI(TRELLIS_DATA_PREFIX + "mementos/" + base + "/resource");
    final Resource res = mock(Resource.class);
    final Instant time = now();
    final Quad quad = rdf.createQuad(Trellis.PreferUserManaged, identifier, DC.title, rdf.createLiteral("Title"));
    final Quad acl = rdf.createQuad(Trellis.PreferAccessControl, identifier, ACL.mode, ACL.Read);
    when(res.getModified()).thenReturn(time);
    when(res.getIdentifier()).thenReturn(identifier);
    when(res.getContainer()).thenReturn(of(root));
    when(res.getInteractionModel()).thenReturn(LDP.RDFSource);
    when(res.getBinaryMetadata()).thenReturn(empty());
    when(res.getMembershipResource()).thenReturn(empty());
    when(res.getMemberRelation()).thenReturn(empty());
    when(res.getMemberOfRelation()).thenReturn(empty());
    when(res.getInsertedContentRelation()).thenReturn(empty());
    when(res.stream()).thenAnswer(inv -> Stream.of(quad, acl));
    final MementoService svc = new S3MementoService();
    assertDoesNotThrow(svc.put(res).toCompletableFuture()::join);
    svc.get(identifier, time).thenAccept(r -> {
        assertEquals(identifier, r.getIdentifier());
        assertEquals(LDP.RDFSource, r.getInteractionModel());
        assertEquals(time, r.getModified());
        assertEquals(of(root), r.getContainer());
        assertTrue(r.stream().anyMatch(isEqual(quad)));
        assertFalse(r.getBinaryMetadata().isPresent());
        assertFalse(r.getMembershipResource().isPresent());
        assertFalse(r.getMemberRelation().isPresent());
        assertFalse(r.getMemberOfRelation().isPresent());
        assertFalse(r.getInsertedContentRelation().isPresent());
        assertTrue(r.getMetadataGraphNames().contains(Trellis.PreferAccessControl));
        assertEquals(1L, r.stream(Trellis.PreferServerManaged).count());
    }).toCompletableFuture().join();
    final Resource res2 = mock(Resource.class);
    final Instant time2 = time.plusSeconds(10L);
    final Quad quad2 = rdf.createQuad(Trellis.PreferUserManaged, identifier, DC.title, rdf.createLiteral("Better Title"));
    when(res2.getModified()).thenReturn(time2);
    when(res2.getIdentifier()).thenReturn(identifier);
    when(res2.getContainer()).thenReturn(of(root));
    when(res2.getInteractionModel()).thenReturn(LDP.RDFSource);
    when(res2.getBinaryMetadata()).thenReturn(empty());
    when(res2.getMembershipResource()).thenReturn(empty());
    when(res2.getMemberRelation()).thenReturn(empty());
    when(res2.getMemberOfRelation()).thenReturn(empty());
    when(res2.getInsertedContentRelation()).thenReturn(empty());
    when(res2.stream()).thenAnswer(inv -> Stream.of(quad2, acl));
    assertDoesNotThrow(svc.put(res2).toCompletableFuture()::join);
    svc.mementos(identifier).thenAccept(mementos -> {
        assertTrue(mementos.contains(time.truncatedTo(SECONDS)));
        assertTrue(mementos.contains(time2.truncatedTo(SECONDS)));
    });
    svc.get(identifier, time.plusSeconds(5L)).thenAccept(r -> assertEquals(time, r.getModified()));
    svc.get(identifier, time2.plusSeconds(5L)).thenAccept(r -> assertEquals(time2, r.getModified()));
}
Also used : IRI(org.apache.commons.rdf.api.IRI) Quad(org.apache.commons.rdf.api.Quad) Instant(java.time.Instant) Resource(org.trellisldp.api.Resource) MementoService(org.trellisldp.api.MementoService) Test(org.junit.jupiter.api.Test)

Example 4 with MementoService

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

the class S3MementoServiceTest method testMementoIndirectContainer.

@Test
void testMementoIndirectContainer() {
    final IRI identifier = rdf.createIRI(TRELLIS_DATA_PREFIX + "mementos/" + base + "/container");
    final IRI member = rdf.createIRI(TRELLIS_DATA_PREFIX + "mementos/" + base + "/resource");
    final Resource res = mock(Resource.class);
    final Instant time = now();
    final Quad quad = rdf.createQuad(Trellis.PreferUserManaged, identifier, DC.title, rdf.createLiteral("Title"));
    final Quad acl = rdf.createQuad(Trellis.PreferAccessControl, identifier, ACL.mode, ACL.Write);
    when(res.getModified()).thenReturn(time);
    when(res.getIdentifier()).thenReturn(identifier);
    when(res.getContainer()).thenReturn(of(root));
    when(res.getInteractionModel()).thenReturn(LDP.IndirectContainer);
    when(res.getBinaryMetadata()).thenReturn(empty());
    when(res.getMembershipResource()).thenReturn(of(member));
    when(res.getMemberRelation()).thenReturn(of(LDP.member));
    when(res.getMemberOfRelation()).thenReturn(of(DC.isPartOf));
    when(res.getInsertedContentRelation()).thenReturn(of(LDP.MemberSubject));
    when(res.stream()).thenAnswer(inv -> Stream.of(quad, acl));
    final MementoService svc = new S3MementoService();
    assertDoesNotThrow(svc.put(res).toCompletableFuture()::join);
    svc.get(identifier, time).thenAccept(r -> {
        assertEquals(identifier, r.getIdentifier());
        assertEquals(LDP.IndirectContainer, r.getInteractionModel());
        assertEquals(time, r.getModified());
        assertEquals(of(root), r.getContainer());
        assertTrue(r.stream().anyMatch(isEqual(quad)));
        assertFalse(r.getBinaryMetadata().isPresent());
        assertEquals(of(member), r.getMembershipResource());
        assertEquals(of(LDP.member), r.getMemberRelation());
        assertEquals(of(DC.isPartOf), r.getMemberOfRelation());
        assertEquals(of(LDP.MemberSubject), r.getInsertedContentRelation());
        assertTrue(r.getMetadataGraphNames().contains(Trellis.PreferAccessControl));
        assertFalse(r.getMetadataGraphNames().contains(Trellis.PreferAudit));
    }).toCompletableFuture().join();
    svc.mementos(identifier).thenAccept(mementos -> assertTrue(mementos.contains(time)));
}
Also used : IRI(org.apache.commons.rdf.api.IRI) Quad(org.apache.commons.rdf.api.Quad) Instant(java.time.Instant) Resource(org.trellisldp.api.Resource) MementoService(org.trellisldp.api.MementoService) Test(org.junit.jupiter.api.Test)

Example 5 with MementoService

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

the class S3MementoServiceTest method testMementoError.

@Test
void testMementoError() {
    final Resource res = mock(Resource.class);
    when(res.getInteractionModel()).thenAnswer(inv -> {
        throw new IOException("Expected");
    });
    final MementoService svc = new S3MementoService();
    assertThrows(CompletionException.class, svc.put(res).toCompletableFuture()::join);
}
Also used : Resource(org.trellisldp.api.Resource) MementoService(org.trellisldp.api.MementoService) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)10 MementoService (org.trellisldp.api.MementoService)10 IRI (org.apache.commons.rdf.api.IRI)9 Instant (java.time.Instant)7 Resource (org.trellisldp.api.Resource)7 Quad (org.apache.commons.rdf.api.Quad)3 NoopMementoService (org.trellisldp.api.NoopMementoService)3 Response (javax.ws.rs.core.Response)2 ResourceService (org.trellisldp.api.ResourceService)2 AmazonS3 (com.amazonaws.services.s3.AmazonS3)1 ListObjectsV2Request (com.amazonaws.services.s3.model.ListObjectsV2Request)1 ListObjectsV2Result (com.amazonaws.services.s3.model.ListObjectsV2Result)1 S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)1 IOException (java.io.IOException)1 BinaryMetadata (org.trellisldp.api.BinaryMetadata)1 TrellisRuntimeException (org.trellisldp.api.TrellisRuntimeException)1