001/*
002 * The contents of this file are subject to the license and copyright
003 * detailed in the LICENSE and NOTICE files at the root of the source
004 * tree.
005 */
006package org.fcrepo.client.integration;
007
008import static javax.ws.rs.core.Response.Status.CREATED;
009import static org.apache.jena.rdf.model.ResourceFactory.createProperty;
010import static org.fcrepo.client.FedoraTypes.MEMENTO_TYPE;
011import static org.fcrepo.client.LinkHeaderConstants.MEMENTO_TIME_MAP_REL;
012import static org.fcrepo.client.TestUtils.rdfTtl;
013import static org.junit.Assert.assertEquals;
014import static org.junit.Assert.assertTrue;
015
016import java.io.ByteArrayInputStream;
017import java.io.IOException;
018import java.net.URI;
019import java.time.Instant;
020
021import org.apache.jena.rdf.model.Model;
022import org.apache.jena.rdf.model.Property;
023import org.fcrepo.client.FcrepoClient;
024import org.fcrepo.client.FcrepoResponse;
025import org.junit.AfterClass;
026import org.junit.BeforeClass;
027import org.junit.Test;
028
029/**
030 * @author bbpennel
031 */
032public class VersioningIT extends AbstractResourceIT {
033
034    private static FcrepoClient client;
035
036    private static final Property DC_TITLE = createProperty("http://purl.org/dc/elements/1.1/title");
037
038    @BeforeClass
039    public static void beforeClass() {
040        client = FcrepoClient.client()
041                .credentials("fedoraAdmin", "fedoraAdmin")
042                .authScope("localhost")
043                .build();
044    }
045
046    @AfterClass
047    public static void afterClass() throws IOException {
048        client.close();
049    }
050
051    @Test
052    public void testCreateMementoFromOriginal() throws Exception {
053        final URI timemapURI;
054        final URI mementoURI;
055
056        // Create original resource with custom property
057        try (final FcrepoResponse createOriginalResp = client.post(new URI(SERVER_ADDRESS))
058                .body(new ByteArrayInputStream(rdfTtl.getBytes()), "text/turtle")
059                .perform()) {
060            timemapURI = createOriginalResp.getLinkHeaders(MEMENTO_TIME_MAP_REL).get(0);
061        }
062
063        try (final FcrepoResponse mementoResp = client.createMemento(timemapURI)
064                .perform()) {
065            assertEquals(CREATED.getStatusCode(), mementoResp.getStatusCode());
066            mementoURI = mementoResp.getLocation();
067        }
068
069        try (final FcrepoResponse getResp = client.get(mementoURI).perform()) {
070            assertTrue("Retrieved object must be a memento", getResp.hasType(MEMENTO_TYPE));
071            final Model respModel = getResponseModel(getResp);
072            assertTrue(respModel.contains(null, DC_TITLE, "Test Object"));
073        }
074    }
075
076    @Test
077    public void testDatetimeNegotiation() throws Exception {
078        final URI location;
079        final URI timemapURI;
080
081        try (final FcrepoResponse createOriginalResp = client.post(new URI(SERVER_ADDRESS))
082                .perform()) {
083            location = createOriginalResp.getLocation();
084            timemapURI = createOriginalResp.getLinkHeaders(MEMENTO_TIME_MAP_REL).get(0);
085        }
086
087        // create version
088        try (final FcrepoResponse mementoResp = client.createMemento(timemapURI).perform()) {
089        }
090
091        // Negotiate for the most recent memento
092        try (final FcrepoResponse negotiateResp = client.get(location)
093                .acceptDatetime(Instant.now())
094                .perform()) {
095            assertTrue(negotiateResp.hasType(MEMENTO_TYPE));
096        }
097    }
098
099}