Skip to content

Commit

Permalink
Use methods on FedoraResource instead of getting the JCR Node
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Oct 29, 2014
1 parent feb74ec commit 3da4858
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 90 deletions.
Expand Up @@ -39,15 +39,12 @@
import org.fcrepo.http.api.repository.FedoraRepositoryTransactions;
import org.fcrepo.http.commons.api.rdf.UriAwareResourceModelFactory;
import org.fcrepo.kernel.FedoraResource;
import org.fcrepo.kernel.exception.RepositoryRuntimeException;
import org.fcrepo.kernel.identifiers.IdentifierConverter;
import org.fcrepo.serialization.SerializerUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.jcr.RepositoryException;
import javax.jcr.nodetype.NodeType;
import javax.ws.rs.core.UriInfo;

import java.util.Map;
Expand All @@ -68,21 +65,17 @@ public Model createModelForResource(final FedoraResource resource,
final UriInfo uriInfo, final IdentifierConverter<Resource,FedoraResource> idTranslator) {

final Model model = createDefaultModel();
try {

final Resource s = idTranslator.reverse().convert(resource);
final Resource s = idTranslator.reverse().convert(resource);

if (resource.getNode().getPrimaryNodeType().isNodeType(ROOT)) {
addRepositoryStatements(uriInfo, model, s);
} else {
addNodeStatements(resource, uriInfo, model, s);
}
if (resource.hasType(ROOT)) {
addRepositoryStatements(uriInfo, model, s);
} else {
addNodeStatements(resource, uriInfo, model, s);
}

if (resource.hasContent()) {
addContentStatements(resource, uriInfo, model, s);
}
} catch (final RepositoryException e) {
throw new RepositoryRuntimeException(e);
if (resource.hasContent()) {
addContentStatements(resource, uriInfo, model, s);
}

return model;
Expand All @@ -105,7 +98,7 @@ private void addNodeStatements(final FedoraResource resource, final UriInfo uriI
singletonMap("path", resource.getPath().substring(1));

// fcr:versions
if (resource.hasType(NodeType.MIX_VERSIONABLE)) {
if (resource.isVersioned()) {
model.add(s, HAS_VERSION_HISTORY, createResource(uriInfo
.getBaseUriBuilder().path(FedoraVersioning.class).buildFromMap(
pathMap, false).toASCIIString()));
Expand Down
Expand Up @@ -25,10 +25,8 @@
import static org.jgroups.util.Util.assertFalse;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.modeshape.jcr.api.JcrConstants.JCR_CONTENT;
import static org.springframework.test.util.ReflectionTestUtils.setField;

import com.hp.hpl.jena.rdf.model.Model;
Expand All @@ -41,12 +39,8 @@
import org.junit.Test;
import org.mockito.Mock;

import javax.jcr.Node;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Workspace;
import javax.jcr.nodetype.NodeType;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;

Expand All @@ -61,16 +55,8 @@ public class HttpApiResourcesTest {

private HttpApiResources testObj;

@Mock
private Node mockNode;

private FedoraResourceImpl mockResource;

private UriInfo uriInfo;

@Mock
private NodeType mockNodeType;

private HttpResourceConverter mockSubjects;

@Mock
Expand All @@ -80,31 +66,22 @@ public class HttpApiResourcesTest {
private Session mockSession;

@Mock
private Repository mockRepository;

@Mock
private Workspace mockWorkspace;
private FedoraResourceImpl mockResource;

@Before
public void setUp() {
initMocks(this);
testObj = new HttpApiResources();
mockResource = new FedoraResourceImpl(mockNode);
uriInfo = getUriInfoImpl();
when(mockSession.getRepository()).thenReturn(mockRepository);
when(mockSession.getWorkspace()).thenReturn(mockWorkspace);
when(mockWorkspace.getName()).thenReturn("default");
mockSubjects = new HttpResourceConverter(mockSession, UriBuilder.fromUri("http://localhost/{path: .*}"));
setField(testObj, "serializers", mockSerializers);
}

@Test
public void shouldDecorateModeRootNodesWithRepositoryWideLinks()
throws RepositoryException {
when(mockNodeType.isNodeType(ROOT)).thenReturn(true);
when(mockNodeType.getName()).thenReturn("nt:root");
when(mockNode.getPrimaryNodeType()).thenReturn(mockNodeType);
when(mockNode.getPath()).thenReturn("/");
when(mockResource.hasType(ROOT)).thenReturn(true);
when(mockResource.getPath()).thenReturn("/");

final Resource graphSubject = mockSubjects.reverse().convert(mockResource);

Expand All @@ -118,10 +95,8 @@ public void shouldDecorateModeRootNodesWithRepositoryWideLinks()
public void shouldDecorateNodesWithLinksToVersionsAndExport()
throws RepositoryException {

when(mockNodeType.getName()).thenReturn("nt:folder");
when(mockNode.getPrimaryNodeType()).thenReturn(mockNodeType);
when(mockNode.isNodeType(NodeType.MIX_VERSIONABLE)).thenReturn(true);
when(mockNode.getPath()).thenReturn("/some/path/to/object");
when(mockResource.isVersioned()).thenReturn(true);
when(mockResource.getPath()).thenReturn("/some/path/to/object");

when(mockSerializers.keySet()).thenReturn(of("a", "b"));
final Resource graphSubject = mockSubjects.reverse().convert(mockResource);
Expand All @@ -138,10 +113,8 @@ public void shouldDecorateNodesWithLinksToVersionsAndExport()
public void shouldNotDecorateNodesWithLinksToVersionsUnlessVersionable()
throws RepositoryException {

when(mockNodeType.getName()).thenReturn("nt:folder");
when(mockNode.getPrimaryNodeType()).thenReturn(mockNodeType);
when(mockNode.isNodeType(NodeType.MIX_VERSIONABLE)).thenReturn(false);
when(mockNode.getPath()).thenReturn("/some/path/to/object");
when(mockResource.isVersioned()).thenReturn(false);
when(mockResource.getPath()).thenReturn("/some/path/to/object");

when(mockSerializers.keySet()).thenReturn(of("a", "b"));
final Resource graphSubject = mockSubjects.reverse().convert(mockResource);
Expand All @@ -155,10 +128,8 @@ public void shouldNotDecorateNodesWithLinksToVersionsUnlessVersionable()
@Test
public void shouldDecorateDatastreamsWithLinksToFixityChecks()
throws RepositoryException {
when(mockNode.hasNode(JCR_CONTENT)).thenReturn(true);
when(mockNodeType.getName()).thenReturn("nt:file");
when(mockNode.getPrimaryNodeType()).thenReturn(mockNodeType);
when(mockNode.getPath()).thenReturn("/some/path/to/datastream");
when(mockResource.hasContent()).thenReturn(true);
when(mockResource.getPath()).thenReturn("/some/path/to/datastream");
when(mockSerializers.keySet()).thenReturn(new HashSet<String>());
final Resource graphSubject = mockSubjects.reverse().convert(mockResource);

Expand All @@ -171,12 +142,9 @@ public void shouldDecorateDatastreamsWithLinksToFixityChecks()
@Test
public void shouldDecorateRootNodeWithCorrectResourceURI()
throws RepositoryException {
final NodeType mockNodeType = mock(NodeType.class);
when(mockNodeType.isNodeType(ROOT)).thenReturn(true);
when(mockNode.getPrimaryNodeType()).thenReturn(mockNodeType);

when(mockResource.hasType(ROOT)).thenReturn(true);
when(mockSerializers.keySet()).thenReturn(of("a"));
when(mockNode.getPath()).thenReturn("/");
when(mockResource.getPath()).thenReturn("/");

final Resource graphSubject = mockSubjects.reverse().convert(mockResource);
final Model model =
Expand All @@ -190,13 +158,8 @@ public void shouldDecorateRootNodeWithCorrectResourceURI()
@Test
public void shouldDecorateOtherNodesWithCorrectResourceURI()
throws RepositoryException {
final NodeType mockNodeType = mock(NodeType.class);
when(mockNodeType.isNodeType(ROOT)).thenReturn(false);
when(mockNode.getPrimaryNodeType()).thenReturn(mockNodeType);
when(mockNodeType.getName()).thenReturn("not-frozen");

when(mockSerializers.keySet()).thenReturn(of("a"));
when(mockNode.getPath()).thenReturn("/some/path/to/object");
when(mockResource.getPath()).thenReturn("/some/path/to/object");

final Resource graphSubject = mockSubjects.reverse().convert(mockResource);
final Model model =
Expand Down
Expand Up @@ -74,29 +74,7 @@ public abstract class FedoraTypesUtils implements FedoraJcrTypes {

@Override
public boolean apply(final FedoraResource f) {
try {

if (f.hasType(FROZEN_NODE)) {
return true;
}

final Node node = f.getNode();

if (node != null) {
final String path = node.getPath();

if (path == null) {
return false;
}

if (path.contains(JCR_FROZEN_NODE)) {
return true;
}
}
return false;
} catch (final RepositoryException e) {
throw new RepositoryRuntimeException(e);
}
return f.hasType(FROZEN_NODE) || f.getPath().contains(JCR_FROZEN_NODE);
}
};

Expand Down
Expand Up @@ -63,7 +63,7 @@ public void serialize(final FedoraResource obj,
final Node node = obj.getNode();
// jcr/xml export system view implemented for noRecurse:
// exportSystemView(String absPath, OutputStream out, boolean skipBinary, boolean noRecurse)
node.getSession().exportSystemView(node.getPath(), out, skipBinary, !recurse);
node.getSession().exportSystemView(obj.getPath(), out, skipBinary, !recurse);
}

@Override
Expand Down
Expand Up @@ -58,9 +58,9 @@ public class JcrXmlSerializerTest {
@Before
public void setUp() throws Exception {
initMocks(this);
when(mockObject.getPath()).thenReturn(testPath);
when(mockObject.getNode()).thenReturn(mockNode);
when(mockNode.getSession()).thenReturn(mockSession);
when(mockNode.getPath()).thenReturn(testPath);
}

@Test
Expand Down

0 comments on commit 3da4858

Please sign in to comment.