Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Improved DefaultGraphSubjects null-check and unit tests
  • Loading branch information
ajs6f committed Oct 20, 2013
1 parent 22d349a commit 9c5ff8c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
Expand Up @@ -15,7 +15,7 @@
*/
package org.fcrepo.kernel.rdf.impl;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.hp.hpl.jena.rdf.model.ResourceFactory.createResource;
import static org.fcrepo.jcr.FedoraJcrTypes.FCR_CONTENT;
import static org.fcrepo.kernel.RdfLexicon.RESTAPI_NAMESPACE;
Expand Down Expand Up @@ -95,9 +95,7 @@ public Node getNodeFromGraphSubject(final Resource subject)

@Override
public boolean isFedoraGraphSubject(final Resource subject) {
checkArgument(subject != null, "null cannot be a Fedora object!");
assert(subject != null);

checkNotNull(subject, "null cannot be a Fedora object!");
return subject.isURIResource() &&
subject.getURI().startsWith(RESTAPI_NAMESPACE);
}
Expand Down
Expand Up @@ -16,7 +16,10 @@

package org.fcrepo.kernel.rdf.impl;

import static com.hp.hpl.jena.rdf.model.ResourceFactory.createResource;
import static org.fcrepo.kernel.RdfLexicon.RESTAPI_NAMESPACE;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
Expand All @@ -25,7 +28,6 @@
import javax.jcr.RepositoryException;
import javax.jcr.Session;

import org.fcrepo.kernel.RdfLexicon;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
Expand Down Expand Up @@ -54,7 +56,7 @@ public void setUp() {
@Test
public void testGetGraphSubject() throws RepositoryException {
final String testPath = "/foo/bar";
final String expected = RdfLexicon.RESTAPI_NAMESPACE + testPath;
final String expected = RESTAPI_NAMESPACE + testPath;
when(mockNode.getPath()).thenReturn(testPath);
Resource actual = testObj.getGraphSubject(mockNode);
assertEquals(expected, actual.getURI());
Expand All @@ -67,34 +69,34 @@ public void testGetGraphSubject() throws RepositoryException {
public void testGetNodeFromGraphSubject() throws RepositoryException {
final String expected = "/foo/bar";
when(mockSession.nodeExists(expected)).thenReturn(true);
when(mockSession.nodeExists(expected + "/bad")).thenReturn(false);
when(mockSession.getNode(expected)).thenReturn(mockNode);
// test a good subject
when(mockSubject.getURI())
.thenReturn(RdfLexicon.RESTAPI_NAMESPACE + expected);
when(mockSubject.getURI()).thenReturn(RESTAPI_NAMESPACE + expected);
when(mockSubject.isURIResource()).thenReturn(true);
Node actual = testObj.getNodeFromGraphSubject(mockSubject);
assertEquals(mockNode, actual);
// test a bad subject
when(mockSubject.getURI()).thenReturn(
"info:fedora2" + expected + "/bad");
actual = testObj.getNodeFromGraphSubject(mockSubject);
assertEquals(null, actual);
assertEquals("Somehow got a Node from a bad RDF subject!", null, actual);
// test a non-existent path
when(mockSubject.getURI())
.thenReturn("info:fedora" + expected + "/bad");
actual = testObj.getNodeFromGraphSubject(mockSubject);
assertEquals(null, actual);
assertEquals("Somehow got a Node from a non-existent RDF subject!",
null, actual);
// test a fcr:content path
when(mockSubject.getURI()).thenReturn(
RdfLexicon.RESTAPI_NAMESPACE + expected + "/fcr:content");
RESTAPI_NAMESPACE + expected + "/fcr:content");
actual = testObj.getNodeFromGraphSubject(mockSubject);
verify(mockSession).getNode(expected + "/jcr:content");
}

@Test
public void testIsFedoraGraphSubject() {
when(mockSubject.getURI()).thenReturn(
RdfLexicon.RESTAPI_NAMESPACE + "foo");
when(mockSubject.getURI()).thenReturn(RESTAPI_NAMESPACE + "foo");
when(mockSubject.isURIResource()).thenReturn(true);
boolean actual = testObj.isFedoraGraphSubject(mockSubject);
assertEquals(true, actual);
Expand All @@ -103,4 +105,16 @@ public void testIsFedoraGraphSubject() {
assertEquals(false, actual);
}

@Test(expected = NullPointerException.class)
public void testIsFedoraGraphSubjectWithNull() {
testObj.isFedoraGraphSubject(null);
}

@Test
public void testIsFedoraGraphSubjectWithBlankNode() {
final Boolean actual = testObj.isFedoraGraphSubject(createResource());
assertFalse("Misrecognized an RDF blank node as a Fedora resource!",
actual);
}

}

0 comments on commit 9c5ff8c

Please sign in to comment.