Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix versioning check when reverting versions of non-RDF resources
  • Loading branch information
escowles authored and Andrew Woods committed Oct 29, 2014
1 parent feb74ec commit 8d5e056
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
Expand Up @@ -18,6 +18,7 @@
import com.google.common.annotations.VisibleForTesting;
import org.fcrepo.http.commons.domain.PATCH;
import org.fcrepo.http.commons.domain.Prefer;
import org.fcrepo.kernel.FedoraBinary;
import org.fcrepo.kernel.FedoraResource;
import org.fcrepo.kernel.utils.iterators.RdfStream;
import org.slf4j.Logger;
Expand Down Expand Up @@ -109,7 +110,7 @@ private void postConstruct() {
public Response revertToVersion() throws RepositoryException {
LOGGER.info("Reverting {} to version {}.", path,
label);
versionService.revertToVersion(session, unversionedResource().getPath(), label);
versionService.revertToVersion(session, unversionedResourcePath(), label);
return noContent().build();
}

Expand All @@ -121,7 +122,7 @@ public Response revertToVersion() throws RepositoryException {
@DELETE
public Response removeVersion() throws RepositoryException {
LOGGER.info("Removing {} version {}.", path, label);
versionService.removeVersion(session, unversionedResource().getPath(), label);
versionService.removeVersion(session, unversionedResourcePath(), label);
return noContent().build();
}

Expand All @@ -146,13 +147,16 @@ public Response getVersion(@HeaderParam("Prefer") final Prefer prefer,
return getContent(prefer, rangeValue, rdfStream);
}

protected FedoraResource unversionedResource() {
protected String unversionedResourcePath() throws RepositoryException {

if (baseResource == null) {
baseResource = getResourceFromPath(externalPath);
if ( baseResource instanceof FedoraBinary ) {
baseResource = ((FedoraBinary)baseResource).getDescription();
}
}

return baseResource;
return baseResource.getPath();
}

@Override
Expand Down
Expand Up @@ -99,29 +99,29 @@ public void setUp() throws Exception {

@Test
public void testRevertToVersion() throws RepositoryException {
doReturn(mockResource).when(testObj).unversionedResource();
doReturn(path).when(testObj).unversionedResourcePath();
final Response response = testObj.revertToVersion();
verify(mockVersions).revertToVersion(mockSession, path, versionLabel);
assertNotNull(response);
}

@Test (expected = PathNotFoundException.class)
public void testRevertToVersionFailure() throws RepositoryException {
doThrow(PathNotFoundException.class).when(testObj).unversionedResource();
doThrow(PathNotFoundException.class).when(testObj).unversionedResourcePath();
testObj.revertToVersion();
}

@Test
public void testRemoveVersion() throws RepositoryException {
doReturn(mockResource).when(testObj).unversionedResource();
doReturn(path).when(testObj).unversionedResourcePath();
final Response response = testObj.removeVersion();
verify(mockVersions).removeVersion(mockSession, path, versionLabel);
assertNotNull(response);
}

@Test (expected = PathNotFoundException.class)
public void testRemoveVersionFailure() throws RepositoryException {
doThrow(PathNotFoundException.class).when(testObj).unversionedResource();
doThrow(PathNotFoundException.class).when(testObj).unversionedResourcePath();
testObj.removeVersion();
}

Expand Down
Expand Up @@ -394,34 +394,47 @@ public void testVersionOperationAddsVersionableMixin() throws Exception {
}

@Test
public void testDatastreamAutoMixin() throws IOException {
public void testDatastreamAutoMixinAndRevert() throws IOException {
final String pid = getRandomUniquePid();
final String dsid = "ds1";
createObject(pid);

createDatastream(pid, "ds1", "This is some datastream content");
final String originalContent = "This is the original content";
final String versionLabel = "ver1";
createDatastream(pid, dsid, originalContent);

// datastream should not have fcr:versions endpoint
assertEquals(404, getStatus(new HttpGet(serverAddress + pid + "/ds1/fcr:versions")));
assertEquals(404, getStatus(new HttpGet(serverAddress + pid + "/" + dsid + "/fcr:versions")));

// datastream should not be versionable
final GraphStore originalObjectProperties = getContent(serverAddress + pid + "/ds1/fcr:metadata");
final GraphStore originalObjectProperties = getContent(serverAddress + pid + "/" + dsid + "/fcr:metadata");
assertFalse("Node must not have versionable mixin.",
originalObjectProperties.contains(ANY, createResource(serverAddress + pid + "/ds1").asNode(),
originalObjectProperties.contains(ANY, createResource(serverAddress + pid + "/" + dsid).asNode(),
createURI(RDF_TYPE), createURI(MIX_NAMESPACE + "versionable")));

// creating a version should succeed
final HttpPost httpPost = new HttpPost(serverAddress + pid + "/ds1/fcr:versions");
httpPost.setHeader("Slug", "label");
final HttpPost httpPost = new HttpPost(serverAddress + pid + "/" + dsid + "/fcr:versions");
httpPost.setHeader("Slug", versionLabel);
assertEquals( 204, getStatus(httpPost));

// datastream should then have versions endpoint
assertEquals( 200, getStatus(new HttpGet(serverAddress + pid + "/ds1/fcr:versions")) );
assertEquals( 200, getStatus(new HttpGet(serverAddress + pid + "/" + dsid + "/fcr:versions")) );

// datastream should then be versionable
final GraphStore updatedDSProperties = getContent(serverAddress + pid + "/ds1/fcr:metadata");
final GraphStore updatedDSProperties = getContent(serverAddress + pid + "/" + dsid + "/fcr:metadata");
assertTrue("Node must have versionable mixin.",
updatedDSProperties.contains(ANY, createResource(serverAddress + pid + "/ds1/fcr:metadata").asNode(),
createURI(RDF_TYPE), createURI(MIX_NAMESPACE + "versionable")));
updatedDSProperties.contains(ANY,
createResource(serverAddress + pid + "/" + dsid + "/fcr:metadata").asNode(),
createURI(RDF_TYPE), createURI(MIX_NAMESPACE + "versionable")));

// update the content
final String updatedContent = "This is the updated content";
execute(putDSMethod(pid,dsid,updatedContent));
assertEquals( updatedContent, EntityUtils.toString(execute(getDSMethod(pid, dsid)).getEntity()) );

// revert to the original content
revertToVersion(pid + "/" + dsid, versionLabel);
assertEquals( originalContent, EntityUtils.toString(execute(getDSMethod(pid, dsid)).getEntity()) );
}


Expand Down

0 comments on commit 8d5e056

Please sign in to comment.