Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix reposting error handling for indirect datastream creation via Fed…
  • Loading branch information
escowles authored and Andrew Woods committed Apr 17, 2014
1 parent 8696a72 commit 311b19a
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
Expand Up @@ -101,6 +101,11 @@ public Response create(@PathParam("path")
final String path = toPath(pathList);

if (nodeService.exists(session, path)) {
if ( nodeService.getObject(session, path).hasContent() ) {
return status(SC_CONFLICT)
.entity(path + " is an existing resource!").build();
}

final String pid;

if (slug != null) {
Expand Down
Expand Up @@ -473,7 +473,9 @@ public Response createObject(@PathParam("path")
final HttpIdentifierTranslator idTranslator =
new HttpIdentifierTranslator(session, FedoraNodes.class, uriInfo);

final boolean hasContent;
if (nodeService.exists(session, path)) {
hasContent = nodeService.getObject(session,path).hasContent();
String pid;

if (slug != null) {
Expand All @@ -492,6 +494,7 @@ public Response createObject(@PathParam("path")
newObjectPath = path + "/" + pid;
} else {
newObjectPath = path;
hasContent = false;
}

LOGGER.debug("Attempting to ingest with path: {}", newObjectPath);
Expand Down Expand Up @@ -520,6 +523,10 @@ public Response createObject(@PathParam("path")
if (s.equals(contentTypeSPARQLUpdate) || contentTypeToLang(s) != null) {
objectType = FEDORA_OBJECT;
} else {
if ( hasContent ) {
return status(SC_CONFLICT).entity(
path + " is an existing resource!").build();
}
objectType = FEDORA_DATASTREAM;
}
} else {
Expand Down
Expand Up @@ -54,6 +54,7 @@

import org.apache.commons.io.IOUtils;
import org.fcrepo.kernel.Datastream;
import org.fcrepo.kernel.FedoraResource;
import org.fcrepo.kernel.exception.InvalidChecksumException;
import org.fcrepo.kernel.identifiers.PidMinter;
import org.fcrepo.kernel.services.DatastreamService;
Expand Down Expand Up @@ -185,6 +186,10 @@ public void testCreateContentAtMintedPath() throws RepositoryException, InvalidC
+ pid + "/xyz"), anyString(), eq((String)null), any(InputStream.class),
eq((URI) null))).thenReturn(mockNode);
when(mockDatastreams.exists(mockSession, dsPath)).thenReturn(true);
final FedoraResource mockResource = mock(FedoraResource.class);
when(mockNodeService.getObject(mockSession,dsPath)).thenReturn(mockResource);
when(mockResource.hasContent()).thenReturn(false);

final Response actual =
testObj.create(createPathList(pid), null, null, null, TEXT_PLAIN_TYPE,
dsContentStream);
Expand Down Expand Up @@ -213,6 +218,9 @@ public void testCreateContentWithSlug() throws RepositoryException, InvalidCheck
+ pid + "/slug"), anyString(), eq((String)null), any(InputStream.class),
eq((URI) null))).thenReturn(mockNode);
when(mockDatastreams.exists(mockSession, dsPath)).thenReturn(true);
final FedoraResource mockResource = mock(FedoraResource.class);
when(mockNodeService.getObject(mockSession,dsPath)).thenReturn(mockResource);
when(mockResource.hasContent()).thenReturn(false);
final Response actual =
testObj.create(createPathList(pid), dsid, null, null, TEXT_PLAIN_TYPE,
dsContentStream);
Expand Down
Expand Up @@ -222,6 +222,9 @@ public void testCreateChildObject() throws Exception {
when(mockNode.getPath()).thenReturn(path);
when(mockSession.getValueFactory()).thenReturn(mockValueFactory);
when(mockValueFactory.createValue("a", PATH)).thenReturn(mockValue);
when(mockNodes.getObject(mockSession,"/" + pid)).thenReturn(mockResource);
when(mockResource.hasContent()).thenReturn(false);

final Response actual =
testObj.createObject(createPathList(pid), FEDORA_OBJECT, null, null,
null, null, getUriInfoImpl(), null);
Expand All @@ -244,6 +247,8 @@ public void testCreateChildObjectWithSlug() throws Exception {
when(mockNode.getPath()).thenReturn(path);
when(mockSession.getValueFactory()).thenReturn(mockValueFactory);
when(mockValueFactory.createValue("a", PATH)).thenReturn(mockValue);
when(mockNodes.getObject(mockSession,"/" + pid)).thenReturn(mockResource);
when(mockResource.hasContent()).thenReturn(false);

final Response actual =
testObj.createObject(createPathList(pid), FEDORA_OBJECT, null, null,
Expand Down Expand Up @@ -571,4 +576,4 @@ public void testMoveObjectWithBadDestination() throws RepositoryException, URISy
// BAD GATEWAY
assertEquals(SC_BAD_GATEWAY, response.getStatus());
}
}
}
Expand Up @@ -284,4 +284,32 @@ public void testRangeRequestBadRange() throws Exception {
assertEquals("bytes 50-100/20", response.getFirstHeader("Content-Range").getValue());

}

@Test
public void testPostToExistingDS() throws Exception {
final String pid = randomUUID().toString();
final HttpPost createObjMethod = postObjMethod(pid);
assertEquals(201, getStatus(createObjMethod));

final HttpPost postDSMethod = postDSMethod(pid, "ds1", "foo");
assertEquals("Posting should work!", 201, getStatus(postDSMethod));

final HttpPost repostDSMethod = postDSMethod(pid, "ds1", "bar");
assertEquals("Reposting should error!", 409, getStatus(repostDSMethod));
}

@Test
public void testPostToExistingDSIndirect() throws Exception {
final String pid = randomUUID().toString();

final HttpPost postDSMethod = new HttpPost(serverAddress + pid);
postDSMethod.setEntity(new StringEntity("foo", "UTF-8"));
postDSMethod.addHeader("Content-Type", "application/foo");
assertEquals(201, getStatus(postDSMethod));

final HttpPost repostDSMethod = new HttpPost(serverAddress + pid);
repostDSMethod.setEntity(new StringEntity("bar", "UTF-8"));
repostDSMethod.addHeader("Content-Type", "application/foo");
assertEquals(409, getStatus(repostDSMethod));
}
}

0 comments on commit 311b19a

Please sign in to comment.