Skip to content

Commit

Permalink
unit tests for creating fcr:content
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Jun 14, 2013
1 parent 59cf6f8 commit 410a733
Showing 1 changed file with 56 additions and 0 deletions.
Expand Up @@ -20,13 +20,15 @@
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.ws.rs.core.EntityTag;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.apache.commons.io.IOUtils;
import org.fcrepo.Datastream;
import org.fcrepo.exception.InvalidChecksumException;
import org.fcrepo.identifiers.PidMinter;
import org.fcrepo.services.DatastreamService;
import org.fcrepo.services.NodeService;
import org.fcrepo.test.util.TestHelpers;
Expand Down Expand Up @@ -87,6 +89,60 @@ public void testPutContent() throws RepositoryException, IOException,
verify(mockSession).save();
}

@Test
public void testCreateContent() throws RepositoryException, IOException,
InvalidChecksumException {
final String pid = "FedoraDatastreamsTest1";
final String dsId = "xyz";
final String dsContent = "asdf";
final String dsPath = "/" + pid + "/" + dsId;
final InputStream dsContentStream = IOUtils.toInputStream(dsContent);
final Node mockNode = mock(Node.class);

when(mockNode.isNew()).thenReturn(true);
when(mockNodeService.exists(mockSession, dsPath)).thenReturn(false);
when(
mockDatastreams.createDatastreamNode(any(Session.class),
eq(dsPath), anyString(), any(InputStream.class)))
.thenReturn(mockNode);
when(mockDatastreams.exists(mockSession, dsPath)).thenReturn(true);
final Response actual =
testObj.create(createPathList(pid, dsId), null, null, MediaType.TEXT_PLAIN_TYPE,
dsContentStream);
assertEquals(Status.CREATED.getStatusCode(), actual.getStatus());
verify(mockDatastreams).createDatastreamNode(mockSession,dsPath, "text/plain", dsContentStream, null, null);
verify(mockSession).save();
}

@Test
public void testCreateContentAtMintedPath() throws RepositoryException, IOException,
InvalidChecksumException {
final String pid = "FedoraDatastreamsTest1";
final String dsId = "fcr:new";
final String dsContent = "asdf";
final String dsPath = "/" + pid + "/" + dsId;
final InputStream dsContentStream = IOUtils.toInputStream(dsContent);
final Node mockNode = mock(Node.class);

PidMinter mockMinter = mock(PidMinter.class);
when(mockMinter.mintPid()).thenReturn("xyz");
testObj.setPidMinter(mockMinter);
when(mockNode.isNew()).thenReturn(true);
when(mockNodeService.exists(mockSession, dsPath)).thenReturn(false);
when(
mockDatastreams.createDatastreamNode(any(Session.class),
eq(dsPath), anyString(), any(InputStream.class)))
.thenReturn(mockNode);
when(mockDatastreams.exists(mockSession, dsPath)).thenReturn(true);
final Response actual =
testObj.create(createPathList(pid, dsId), null, null, MediaType.TEXT_PLAIN_TYPE,
dsContentStream);
assertEquals(Status.CREATED.getStatusCode(), actual.getStatus());
verify(mockDatastreams).createDatastreamNode(mockSession,
"/" + pid + "/xyz", "text/plain", dsContentStream, null, null);
verify(mockSession).save();
}

@Test
public void testModifyContent() throws RepositoryException, IOException,
InvalidChecksumException {
Expand Down

0 comments on commit 410a733

Please sign in to comment.