Skip to content

Commit

Permalink
Refactored and cleaned up DatastreamServiceTest
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Feb 21, 2013
1 parent 731ba8a commit 6f45553
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 32 deletions.
10 changes: 10 additions & 0 deletions fcrepo-kernel/src/main/java/org/fcrepo/Datastream.java
@@ -1,6 +1,11 @@

package org.fcrepo;

import static org.modeshape.jcr.api.JcrConstants.JCR_CONTENT;
import static org.modeshape.jcr.api.JcrConstants.JCR_DATA;

import java.io.InputStream;

import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.RepositoryException;
Expand All @@ -17,6 +22,11 @@ public Datastream(Node n) {
public Node getNode() {
return node;
}

public InputStream getContent() throws ValueFormatException, PathNotFoundException, RepositoryException {
return node.getNode(JCR_CONTENT).getProperty(JCR_DATA).getBinary()
.getStream();
}

public String getMimeType() throws ValueFormatException,
PathNotFoundException, RepositoryException {
Expand Down
Expand Up @@ -112,14 +112,12 @@ public InputStream getDatastreamContentInputStream(final String dsPath)

public static InputStream getDatastreamContentInputStream(final Node node)
throws RepositoryException {
return node.getNode(JCR_CONTENT).getProperty(JCR_DATA).getBinary()
.getStream();
return new Datastream(node).getContent();
}

public static InputStream getDatastreamContentInputStream(
final Datastream ds) throws RepositoryException {
return ds.getNode().getNode(JCR_CONTENT).getProperty(JCR_DATA)
.getBinary().getStream();
return ds.getContent();
}

@PostConstruct
Expand Down
@@ -1,58 +1,66 @@
package org.fcrepo.services;

import org.apache.tika.io.IOUtils;
import org.fcrepo.AbstractTest;
import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;

import javax.inject.Inject;
import javax.jcr.Node;
import javax.jcr.Repository;
import javax.jcr.Session;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
package org.fcrepo.services;

import static org.fcrepo.services.DatastreamService.getDatastreamContentInputStream;
import static org.jgroups.util.Util.assertEquals;
import static org.jgroups.util.Util.assertTrue;
import static org.modeshape.jcr.api.JcrConstants.JCR_CONTENT;
import static org.modeshape.jcr.api.JcrConstants.JCR_DATA;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

@ContextConfiguration({ "/spring-test/repo.xml" })
import javax.inject.Inject;
import javax.jcr.Repository;
import javax.jcr.Session;

import org.apache.tika.io.IOUtils;
import org.fcrepo.AbstractTest;
import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;

@ContextConfiguration({"/spring-test/repo.xml"})
public class DatastreamServiceTest extends AbstractTest {

@Inject
private Repository repository;

@Inject
private DatastreamService datastreamService;

@Test
public void testCreateDatastreamNode() throws Exception {
Session session = repository.login();
InputStream is = new ByteArrayInputStream("asdf".getBytes());

Node n = new DatastreamService().createDatastreamNode(session, "testDatastreamNode", "application/octet-stream", is);

datastreamService.createDatastreamNode(session, "testDatastreamNode",
"application/octet-stream", is);
session.save();

session.logout();
session = repository.login();

assertTrue(session.getRootNode().hasNode("testDatastreamNode"));
assertEquals("asdf", session.getNode("/testDatastreamNode").getNode(JCR_CONTENT).getProperty(JCR_DATA).getString());

assertEquals("asdf", session.getNode("/testDatastreamNode").getNode(
JCR_CONTENT).getProperty(JCR_DATA).getString());
session.logout();
}

@Test
public void testGetDatastreamContentInputStream() throws Exception {
Session session = repository.login();
InputStream is = new ByteArrayInputStream("asdf".getBytes());

Node n = new DatastreamService().createDatastreamNode(session, "testDatastreamNode", "application/octet-stream", is);
datastreamService.createDatastreamNode(session, "testDatastreamNode",
"application/octet-stream", is);

session.save();

session.logout();
session = repository.login();

InputStream contentInputStream = new DatastreamService().getDatastreamContentInputStream(session, "/testDatastreamNode");
InputStream contentInputStream =
getDatastreamContentInputStream(session, "/testDatastreamNode");

assertEquals("asdf", IOUtils.toString(contentInputStream, "UTF-8"));
session.logout();
}
}
3 changes: 2 additions & 1 deletion fcrepo-kernel/src/test/resources/spring-test/repo.xml
Expand Up @@ -21,5 +21,6 @@
</bean>

<bean class="org.fcrepo.services.ObjectService"/>

<bean class="org.fcrepo.services.DatastreamService"/>

</beans>
Expand Up @@ -10,7 +10,6 @@
import static javax.ws.rs.core.Response.ok;
import static org.fcrepo.api.legacy.FedoraObjects.getObjectSize;
import static org.fcrepo.jaxb.responses.DatastreamProfile.DatastreamStates.A;
import static org.fcrepo.services.DatastreamService.getDatastreamContentInputStream;
import static org.fcrepo.services.DatastreamService.getDatastreamNode;
import static org.fcrepo.services.ObjectService.getObjectNode;
import static org.modeshape.jcr.api.JcrConstants.JCR_CONTENT;
Expand Down Expand Up @@ -199,7 +198,7 @@ public Response modifyDatastream(@PathParam("pid")
: MediaType.APPLICATION_OCTET_STREAM_TYPE;
String dspath = "/objects/" + pid + "/" + dsid;

return Response.created(
return created(
addDatastreamNode(pid, dspath, contentType, requestBodyStream,
session)).build();

Expand Down Expand Up @@ -276,8 +275,8 @@ public Response getDatastreamContent(@PathParam("pid")
final String pid, @PathParam("dsid")
final String dsid) throws RepositoryException {

final Datastream ds = DatastreamService.getDatastream(pid,dsid);
return ok(getDatastreamContentInputStream(ds), ds.getMimeType()).build();
final Datastream ds = DatastreamService.getDatastream(pid, dsid);
return ok(ds.getContent(), ds.getMimeType()).build();
}

/**
Expand All @@ -301,9 +300,9 @@ Response getDatastreamHistory(@PathParam("pid")
final String pid, @PathParam("dsid")
final String dsid) throws RepositoryException, IOException {

final Node ds = getObjectNode(pid).getNode(dsid);
final Datastream ds = DatastreamService.getDatastream(pid, dsid);
final DatastreamHistory dsHistory =
new DatastreamHistory(singletonList(getDSProfile(ds)));
new DatastreamHistory(singletonList(getDSProfile(ds.getNode())));
dsHistory.dsID = dsid;
dsHistory.pid = pid;
return ok(dsHistory).build();
Expand Down

0 comments on commit 6f45553

Please sign in to comment.