Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added tests for Location header correctness in responses, cleaned up …
…unused imports
  • Loading branch information
ajs6f committed Apr 2, 2013
1 parent cfc712a commit a9f7eae
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 29 deletions.
Expand Up @@ -15,7 +15,6 @@

import javax.inject.Inject;
import javax.jcr.LoginException;
import javax.jcr.NamespaceRegistry;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.nodetype.NodeType;
Expand All @@ -31,7 +30,6 @@
import org.fcrepo.jaxb.responses.access.DescribeRepository;
import org.fcrepo.provider.VelocityViewer;
import org.fcrepo.services.ObjectService;
import org.modeshape.jcr.api.nodetype.NodeTypeManager;
import org.slf4j.Logger;

import com.google.common.collect.ImmutableMap.Builder;
Expand Down
Expand Up @@ -33,7 +33,6 @@
import org.jboss.resteasy.specimpl.UriInfoImpl;
import org.modeshape.jcr.api.Repository;
import org.modeshape.jcr.api.nodetype.NodeTypeManager;
import org.modeshape.jcr.query.QueryResults;

public abstract class TestHelpers {

Expand Down Expand Up @@ -110,6 +109,7 @@ public static Session getQuerySessionMock() {
return mock;
}

@SuppressWarnings("unchecked")
public static Repository createMockRepo() throws RepositoryException {
Repository mockRepo = mock(Repository.class);
Session mockSession = mock(Session.class);
Expand Down
Expand Up @@ -4,6 +4,7 @@
import static java.util.regex.Pattern.DOTALL;
import static java.util.regex.Pattern.compile;
import static junit.framework.TestCase.assertFalse;
import static org.fcrepo.services.PathService.OBJECT_PATH;
import static org.fcrepo.utils.FixityResult.FixityState.BAD_CHECKSUM;
import static org.fcrepo.utils.FixityResult.FixityState.BAD_SIZE;
import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -48,16 +49,14 @@ public void testGetDatastreams() throws Exception {
"objects/FedoraDatastreamsTest1/datastreams");
assertEquals(200, getStatus(method));



final HttpResponse response = execute(method);

String content = IOUtils.toString(response.getEntity().getContent());

logger.info(content);

assertTrue("Found the wrong XML tag", compile(
"<datastream ", DOTALL).matcher(content).find());
assertTrue("Found the wrong XML tag", compile("<datastream ", DOTALL)
.matcher(content).find());
}

@Test
Expand All @@ -66,7 +65,13 @@ public void testAddDatastream() throws Exception {
assertEquals(201, getStatus(objMethod));
final HttpPost method =
postDSMethod("FedoraDatastreamsTest2", "zxc", "foo");
assertEquals(201, getStatus(method));
final HttpResponse response = client.execute(method);
final String location = response.getFirstHeader("Location").getValue();
assertEquals(201, response.getStatusLine().getStatusCode());
assertEquals(
"Got wrong URI in Location header for datastream creation!",
serverAddress + OBJECT_PATH.replace("/", "") +
"/FedoraDatastreamsTest2/datastreams/zxc", location);
}

@Test
Expand All @@ -84,9 +89,15 @@ public void testMutateDatastream() throws Exception {
final HttpPut mutateDataStreamMethod =
putDSMethod("FedoraDatastreamsTest3", "ds1");
mutateDataStreamMethod.setEntity(new StringEntity(faulkner1, "UTF-8"));
assertEquals("Couldn't mutate a datastream!", 201,
getStatus(mutateDataStreamMethod));

final HttpResponse response = client.execute(mutateDataStreamMethod);
final String location = response.getFirstHeader("Location").getValue();
assertEquals("Couldn't mutate a datastream!", 201, response
.getStatusLine().getStatusCode());
assertEquals(
"Got wrong URI in Location header for datastream creation!",
serverAddress + OBJECT_PATH.replace("/", "") +
"/FedoraDatastreamsTest3/datastreams/ds1", location);

final HttpGet retrieveMutatedDataStreamMethod =
new HttpGet(serverAddress +
"objects/FedoraDatastreamsTest3/datastreams/ds1/content");
Expand Down
Expand Up @@ -2,6 +2,7 @@
package org.fcrepo.integration.api;

import static java.util.regex.Pattern.compile;
import static org.fcrepo.services.PathService.OBJECT_PATH;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

Expand All @@ -23,6 +24,9 @@ public void testIngest() throws Exception {
final String content = EntityUtils.toString(response.getEntity());
assertTrue("Response wasn't a PID", compile("[a-z]+").matcher(content)
.find());
final String location = response.getFirstHeader("Location").getValue();
assertEquals("Got wrong Location header for ingest!", serverAddress +
OBJECT_PATH.replace("/", "") + "/FedoraObjectsTest1", location);
}

@Test
Expand Down Expand Up @@ -60,7 +64,8 @@ public void testDeleteObject() throws Exception {

@Test
public void testIngestWithLabel() throws Exception {
final HttpPost method = postObjMethod("FedoraObjectsTest4", "label=Awesome_Object");
final HttpPost method =
postObjMethod("FedoraObjectsTest4", "label=Awesome_Object");
final HttpResponse response = client.execute(method);
assertEquals(201, response.getStatusLine().getStatusCode());
final String content = EntityUtils.toString(response.getEntity());
Expand Down
21 changes: 11 additions & 10 deletions fcrepo-kernel/src/main/java/org/fcrepo/Datastream.java
Expand Up @@ -53,11 +53,11 @@ public class Datastream extends JcrTools implements FedoraJcrTypes {
Datastream.class, "content-size"));

Node node;

LowLevelStorageService llStore;

public Datastream(Node n) {
super(false); // turn off debug logging
super(false); // turn off debug logging
this.node = n;
}

Expand All @@ -68,7 +68,7 @@ public Datastream(final Session session, String pid, String dsId)

public Datastream(final Session session, final String dsPath)
throws RepositoryException {
super(false);
super(false);
this.node = findOrCreateNode(session, dsPath, NT_FILE);
if (this.node.isNew()) {
this.node.addMixin(FEDORA_DATASTREAM);
Expand Down Expand Up @@ -98,8 +98,9 @@ public Node getNode() {
* @throws RepositoryException
*/
public InputStream getContent() throws RepositoryException {
return node.getNode(JCR_CONTENT).getProperty(JCR_DATA).getBinary()
.getStream();
final Node contentNode = node.getNode(JCR_CONTENT);
logger.trace("Retrieved datastream content node.");
return contentNode.getProperty(JCR_DATA).getBinary().getStream();
}

/**
Expand Down Expand Up @@ -147,11 +148,11 @@ public void setContent(InputStream content, String checksumType,
Property dataProperty = contentNode.setProperty(JCR_DATA, binary);

String dsChecksum = binary.getHexHash();
if (checksum != null && !checksum.equals("")
&& !checksum.equals(binary.getHexHash())) {
logger.debug("Failed checksum test");
throw new InvalidChecksumException("Checksum Mismatch of " +
dsChecksum + " and " + checksum);
if (checksum != null && !checksum.equals("") &&
!checksum.equals(binary.getHexHash())) {
logger.debug("Failed checksum test");
throw new InvalidChecksumException("Checksum Mismatch of " +
dsChecksum + " and " + checksum);
}

contentSizeHistogram.update(dataProperty.getLength());
Expand Down
@@ -1,6 +1,5 @@
package org.fcrepo.services;

import static com.google.common.collect.ImmutableMap.builder;
import static javax.jcr.query.Query.JCR_SQL2;

import java.io.PrintStream;
Expand Down Expand Up @@ -30,7 +29,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.ImmutableMap.Builder;
import com.yammer.metrics.MetricRegistry;

public class RepositoryService extends JcrTools implements FedoraJcrTypes {
Expand Down
Expand Up @@ -8,9 +8,10 @@
import java.net.URISyntaxException;
import java.util.Map;

import com.google.common.collect.ImmutableMap;
import org.slf4j.Logger;

import com.google.common.collect.ImmutableMap;

public class ContentDigest {

private static final Logger logger = getLogger(ContentDigest.class);
Expand Down
Expand Up @@ -13,7 +13,6 @@
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.UnsupportedRepositoryOperationException;
import javax.jcr.Value;
import javax.jcr.ValueFactory;
import javax.jcr.nodetype.NodeType;
Expand Down
@@ -1,15 +1,15 @@

package org.fcrepo.utils;

import com.google.common.primitives.Longs;

import java.net.URI;
import java.util.EnumSet;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import com.google.common.primitives.Longs;

@XmlRootElement(name = "DatastreamFixityStatus")
public class FixityResult {

Expand Down
1 change: 0 additions & 1 deletion fcrepo-kernel/src/test/java/org/fcrepo/DatastreamTest.java
Expand Up @@ -35,7 +35,6 @@
import org.fcrepo.exception.InvalidChecksumException;
import org.fcrepo.utils.FedoraJcrTypes;
import org.fcrepo.utils.FedoraTypesUtils;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down

0 comments on commit a9f7eae

Please sign in to comment.