Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
refactor a commonly repeated JCR pattern into a static method, begin …
…unit testing for AbstractResource and FedoraResource
  • Loading branch information
barmintor committed May 13, 2013
1 parent 623f181 commit 75cda25
Show file tree
Hide file tree
Showing 18 changed files with 482 additions and 58 deletions.
Expand Up @@ -32,6 +32,7 @@
import org.fcrepo.generator.rdf.Utils;
import org.fcrepo.services.DatastreamService;
import org.fcrepo.services.ObjectService;
import org.fcrepo.utils.NamespaceTools;
import org.openrdf.model.URI;
import org.openrdf.model.ValueFactory;
import org.openrdf.sail.memory.model.MemValueFactory;
Expand Down Expand Up @@ -95,8 +96,7 @@ public String getRdfXml(
private void writeNamespaces(Node node, TripleHandler writer, ExtractionContext context)
throws TripleHandlerException, RepositoryException {
final NamespaceRegistry nReg =
node.getSession().getWorkspace()
.getNamespaceRegistry();
NamespaceTools.getNamespaceRegistry(node);
for (final String prefix : nReg.getPrefixes()) {
final String nsURI = nReg.getURI(prefix);
if (nsURI != null && !nsURI.equals("") &&
Expand Down
Expand Up @@ -13,6 +13,7 @@
import org.apache.any23.writer.RDFXMLWriter;
import org.apache.any23.writer.TripleHandler;
import org.apache.any23.writer.TurtleWriter;
import org.fcrepo.utils.NamespaceTools;

public abstract class Utils {

Expand All @@ -30,7 +31,7 @@ public static TripleHandler selectWriter(final String mimeType,

public static String expandJCRNamespace(Property p) throws RepositoryException {
String name = p.getName();
NamespaceRegistry nReg = p.getSession().getWorkspace().getNamespaceRegistry();
NamespaceRegistry nReg = NamespaceTools.getNamespaceRegistry(p.getSession());
final String predicatePrefix = name.substring(0, name.indexOf(':'));
return name.replaceFirst(predicatePrefix + ":", nReg
.getURI(predicatePrefix));
Expand Down
Expand Up @@ -3,6 +3,7 @@
import static javax.ws.rs.core.MediaType.TEXT_PLAIN;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;
import static org.powermock.api.mockito.PowerMockito.mockStatic;

import java.io.IOException;
import java.lang.reflect.Field;
Expand All @@ -11,7 +12,6 @@
import java.util.Arrays;
import java.util.List;

import javax.jcr.NamespaceRegistry;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
Expand All @@ -29,10 +29,17 @@
import org.fcrepo.services.ObjectService;
import org.fcrepo.test.util.PathSegmentImpl;
import org.fcrepo.test.util.TestHelpers;
import org.fcrepo.utils.NamespaceTools;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.modeshape.jcr.api.NamespaceRegistry;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;


@RunWith(PowerMockRunner.class)
@PrepareForTest({NamespaceTools.class})
public class FedoraRdfGeneratorTest {

private FedoraRdfGenerator testObj;
Expand Down Expand Up @@ -83,12 +90,12 @@ public void testSetDatastreamGenerators() throws IOException, RepositoryExceptio
public void testGetRdfXml() throws IOException, RepositoryException, TripleHandlerException {
List<PathSegment> pathList = PathSegmentImpl.createPathList("objects", "foo");
FedoraObject mockObj = mock(FedoraObject.class);
Workspace mockWS = mock(Workspace.class);
NamespaceRegistry mockNS = mock(NamespaceRegistry.class);
when(mockSession.getWorkspace()).thenReturn(mockWS);
when(mockWS.getNamespaceRegistry()).thenReturn(mockNS);
when(mockNS.getPrefixes()).thenReturn(new String[]{});
Node mockNode = mock(Node.class);
mockStatic(NamespaceTools.class);
when(NamespaceTools.getNamespaceRegistry(mockNode))
.thenReturn(mockNS);
when(mockObj.getNode()).thenReturn(mockNode);
when(mockNode.getSession()).thenReturn(mockSession);
when(mockObjects.getObject(mockSession, "/objects/foo")).thenReturn(mockObj);
Expand Down
Expand Up @@ -2,10 +2,10 @@

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;
import static org.powermock.api.mockito.PowerMockito.mockStatic;

import java.io.OutputStream;

import javax.jcr.NamespaceRegistry;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
Expand All @@ -15,8 +15,15 @@
import org.apache.any23.writer.RDFXMLWriter;
import org.apache.any23.writer.TripleHandler;
import org.apache.any23.writer.TurtleWriter;
import org.fcrepo.utils.NamespaceTools;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.modeshape.jcr.api.NamespaceRegistry;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest({NamespaceTools.class})
public class UtilsTest {

@Test
Expand All @@ -35,13 +42,12 @@ public void testSelectWriter() {
public void testExpandJcrNamespace() throws RepositoryException {
Property mockProp = mock(Property.class);
Session mockSession = mock(Session.class);
Workspace mockWS = mock(Workspace.class);
NamespaceRegistry mockNSR = mock(NamespaceRegistry.class);
when(mockProp.getName()).thenReturn("foo:bar");
when(mockProp.getSession()).thenReturn(mockSession);
when(mockSession.getWorkspace()).thenReturn(mockWS);
when(mockWS.getNamespaceRegistry()).thenReturn(mockNSR);
when(mockNSR.getURI("foo")).thenReturn("http://foo.gov/");
mockStatic(NamespaceTools.class);
when(NamespaceTools.getNamespaceRegistry(mockSession)).thenReturn(mockNSR);
String actual = Utils.expandJCRNamespace(mockProp);
String expected = "http://foo.gov/bar";
assertEquals(expected, actual);
Expand Down
Expand Up @@ -23,6 +23,7 @@
import org.fcrepo.AbstractResource;
import org.fcrepo.jaxb.responses.management.NamespaceListing;
import org.fcrepo.jaxb.responses.management.NamespaceListing.Namespace;
import org.fcrepo.utils.NamespaceTools;
import org.springframework.stereotype.Component;

import com.codahale.metrics.annotation.Timed;
Expand Down Expand Up @@ -59,7 +60,7 @@ public Response registerObjectNamespace(@PathParam("prefix")
final Session session = getAuthenticatedSession();
try {
final NamespaceRegistry r =
session.getWorkspace().getNamespaceRegistry();
NamespaceTools.getNamespaceRegistry(session);
r.registerNamespace(prefix, uri);
} finally {
session.logout();
Expand All @@ -83,7 +84,7 @@ public Response registerObjectNamespaces(final NamespaceListing nses)
final Session session = getAuthenticatedSession();
try {
final NamespaceRegistry r =
session.getWorkspace().getNamespaceRegistry();
NamespaceTools.getNamespaceRegistry(session);
for (final Namespace ns : nses.namespaces) {
r.registerNamespace(ns.prefix, ns.uri.toString());
}
Expand All @@ -109,7 +110,7 @@ public Namespace retrieveObjectNamespace(@PathParam("prefix")

final Session session = getAuthenticatedSession();
final NamespaceRegistry r =
session.getWorkspace().getNamespaceRegistry();
NamespaceTools.getNamespaceRegistry(session);

try {
final Namespace ns =
Expand All @@ -135,7 +136,7 @@ public NamespaceListing getNamespaces() throws RepositoryException,
final Session session = getAuthenticatedSession();
try {
final NamespaceRegistry r =
session.getWorkspace().getNamespaceRegistry();
NamespaceTools.getNamespaceRegistry(session);

for (final String prefix : r.getPrefixes()) {
b.add(new Namespace(prefix, URI.create(r.getURI(prefix))));
Expand Down
Expand Up @@ -6,6 +6,8 @@
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.mockStatic;


import java.io.IOException;
import java.util.HashMap;
Expand All @@ -21,13 +23,23 @@

import org.fcrepo.jaxb.responses.access.DescribeRepository;
import org.fcrepo.services.ObjectService;
import org.fcrepo.services.RepositoryService;
import org.fcrepo.session.SessionFactory;
import org.fcrepo.test.util.TestHelpers;
import org.fcrepo.utils.JcrRdfTools;
import org.fcrepo.utils.NamespaceTools;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.modeshape.jcr.api.NamespaceRegistry;
import org.modeshape.jcr.api.Repository;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest({RepositoryService.class})
public class FedoraRepositoryTest {

FedoraRepository testFedoraRepo;
Expand All @@ -53,7 +65,9 @@ public void setUp() throws LoginException, RepositoryException {
any(HttpServletRequest.class))).thenReturn(mockSession);
testFedoraRepo.setSessionFactory(mockSessions);
when(mockRepo.getDescriptorKeys()).thenReturn(new String[0]);
when(mockObjects.getRepositoryNamespaces(mockSession)).thenReturn(
mockStatic(RepositoryService.class);

when(RepositoryService.getRepositoryNamespaces(mockSession)).thenReturn(
new HashMap<String, String>(0));
final NodeTypeIterator mockNT = mock(NodeTypeIterator.class);
when(mockObjects.getAllNodeTypes(mockSession)).thenReturn(mockNT);
Expand Down
Expand Up @@ -23,6 +23,7 @@
import org.fcrepo.services.ObjectService;
import org.fcrepo.session.AuthenticatedSessionProvider;
import org.fcrepo.session.SessionFactory;
import org.fcrepo.utils.NamespaceTools;
import org.modeshape.jcr.api.JcrTools;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -36,7 +37,11 @@
*/
public abstract class AbstractResource {

private static final Logger logger = getLogger(AbstractResource.class);
private static final Logger LOGGER = getLogger(AbstractResource.class);

public static final String TEST_NS_PREFIX = "test";

public static final String TEST_NS_URI = "info:fedora/test/";

/**
* Useful for constructing URLs
Expand Down Expand Up @@ -88,8 +93,8 @@ public abstract class AbstractResource {
public void initialize() throws RepositoryException {

final Session session = sessions.getSession();
session.getWorkspace().getNamespaceRegistry().registerNamespace("test",
"info:fedora/test");
NamespaceTools.getNamespaceRegistry(session).registerNamespace(TEST_NS_PREFIX,
TEST_NS_URI);
session.save();
session.logout();
}
Expand All @@ -105,7 +110,7 @@ protected AuthenticatedSessionProvider getAuthenticatedSessionProvider() {
protected synchronized Response deleteResource(final Node resource)
throws RepositoryException {

logger.debug("Attempting to delete resource at path: " +
LOGGER.debug("Attempting to delete resource at path: " +
resource.getPath());
final Session session = resource.getSession();

Expand Down
114 changes: 114 additions & 0 deletions fcrepo-http-commons/src/test/java/org/fcrepo/AbstractResourceTest.java
@@ -0,0 +1,114 @@
package org.fcrepo;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.*;
import static org.powermock.api.mockito.PowerMockito.mockStatic;


import java.util.List;

import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Workspace;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.PathSegment;
import javax.ws.rs.core.SecurityContext;
import javax.ws.rs.core.UriInfo;

import org.fcrepo.identifiers.PidMinter;
import org.fcrepo.services.NodeService;
import org.fcrepo.session.SessionFactory;
import org.fcrepo.test.util.PathSegmentImpl;
import org.fcrepo.utils.JcrRdfTools;
import org.fcrepo.utils.NamespaceTools;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.modeshape.jcr.api.NamespaceRegistry;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
//PowerMock needs to ignore some packages to prevent class-cast errors
//@PowerMockIgnore({"org.apache.xerces.*", "javax.xml.*", "org.xml.sax.*", "javax.management.*"})
@PrepareForTest({NamespaceTools.class})
public class AbstractResourceTest {

AbstractResource testObj;

@Before
public void setUp() {
testObj = new AbstractResource() {

};
}

@Test
public void testInitialize() throws RepositoryException {
SessionFactory mockSessions = mock(SessionFactory.class);
Session mockSession = mock(Session.class);
NamespaceRegistry mockNames = mock(NamespaceRegistry.class);
mockStatic(NamespaceTools.class);
when(NamespaceTools.getNamespaceRegistry(any(Session.class))).thenReturn(mockNames);
when(mockSessions.getSession()).thenReturn(mockSession);
testObj.setSessionFactory(mockSessions);
testObj.initialize();
verify(mockNames).registerNamespace(AbstractResource.TEST_NS_PREFIX, AbstractResource.TEST_NS_URI);
}

@Test
public void testSetPidMinter() {
PidMinter mockPids = mock(PidMinter.class);
testObj.setPidMinter(mockPids);
assertEquals(mockPids, testObj.pidMinter);
}

@Test
public void testSessionMachinerySetters() {
HttpServletRequest mockRequest = mock(HttpServletRequest.class);
SessionFactory mockSession = mock(SessionFactory.class);
SecurityContext mockContext = mock(SecurityContext.class);
testObj.setHttpServletRequest(mockRequest);
testObj.setSessionFactory(mockSession);
testObj.setSecurityContext(mockContext);
testObj.getAuthenticatedSession();
verify(mockSession).getSession(mockContext, mockRequest);
}

@Test
public void testSetNodeService() {
NodeService mockNodes = mock(NodeService.class);
testObj.setNodeService(mockNodes);
assertEquals(mockNodes, testObj.nodeService);
}

@Test
public void testSetUriInfo() {
UriInfo mockUris = mock(UriInfo.class);
testObj.setUriInfo(mockUris);
assertEquals(mockUris, testObj.uriInfo);
}

@Test
public void testToPath() {
List<PathSegment> pathList = PathSegmentImpl.createPathList("foo", "", "bar", "baz");
// empty path segments ('//') should be suppressed
String expected = "/foo/bar/baz";
String actual = AbstractResource.toPath(pathList);
assertEquals(expected, actual);
}

@Test
public void testDeleteResource() throws RepositoryException {
Node mockNode = mock(Node.class);
Session mockSession = mock(Session.class);
when(mockNode.getSession()).thenReturn(mockSession);
testObj.deleteResource(mockNode);
verify(mockNode).remove();
verify(mockSession).save();
verify(mockSession).logout();
}
}
Expand Up @@ -18,7 +18,6 @@
import java.util.Map.Entry;

import javax.jcr.LoginException;
import javax.jcr.NamespaceRegistry;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;
Expand Down Expand Up @@ -46,6 +45,7 @@
import org.fcrepo.session.SessionFactory;
import org.fcrepo.utils.ContentDigest;
import org.fcrepo.utils.DatastreamIterator;
import org.modeshape.jcr.api.NamespaceRegistry;
import org.modeshape.jcr.api.Repository;
import org.modeshape.jcr.api.query.QueryManager;

Expand Down

0 comments on commit 75cda25

Please sign in to comment.