Skip to content

Commit

Permalink
Nonfunctional code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Feb 25, 2014
1 parent 7e768dd commit 440a2bc
Show file tree
Hide file tree
Showing 14 changed files with 94 additions and 131 deletions.
Expand Up @@ -116,7 +116,7 @@ public RdfStream getNextPid(@PathParam("path")

}

private Function<String, String> absolutize(final String path) {
private static Function<String, String> absolutize(final String path) {
return new Function<String, String>() {

@Override
Expand Down
Expand Up @@ -183,11 +183,10 @@ public RdfStream getVersion(@PathParam("path")
final Node node = nodeTranslator().getNodeFromGraphSubjectForVersionNode(uriInfo.getRequestUri().toString());
if (node == null) {
throw new WebApplicationException(status(NOT_FOUND).build());
} else {
final FedoraResource resource = new FedoraResourceImpl(node);
return resource.getTriples(nodeTranslator()).session(session).topic(
nodeTranslator().getGraphSubject(resource.getNode()).asNode());
}
final FedoraResource resource = new FedoraResourceImpl(node);
return resource.getTriples(nodeTranslator()).session(session).topic(
nodeTranslator().getGraphSubject(resource.getNode()).asNode());
}

/**
Expand Down
Expand Up @@ -84,7 +84,7 @@ public Model createModelForResource(final FedoraResource resource,
return model;
}

private void addContentStatements(final FedoraResource resource, final UriInfo uriInfo,
private static void addContentStatements(final FedoraResource resource, final UriInfo uriInfo,
final Model model, final Resource s) throws RepositoryException {
// fcr:fixity
final Map<String, String> pathMap =
Expand Down
Expand Up @@ -77,44 +77,42 @@ public class VersionAwareHttpGraphSubjects extends HttpGraphSubjects {
* @param uris a UriInfo implementation with information about the request
* URI.
*/
public VersionAwareHttpGraphSubjects(Session session, Session internalSession, Class<?> relativeTo, UriInfo uris) {
public VersionAwareHttpGraphSubjects(final Session session,
final Session internalSession, final Class<?> relativeTo, final UriInfo uris) {
super(session, relativeTo, uris);
this.internalSession = internalSession;
}

@Override
public String getPathFromGraphSubject(@NotNull final String subjectUri) throws RepositoryException {
if (subjectUri.matches("^.*\\Q" + FCR_VERSIONS + "/\\E.+$")) {
Node node = getNodeFromGraphSubjectForVersionNode(subjectUri);
final Node node = getNodeFromGraphSubjectForVersionNode(subjectUri);
if (node == null) {
return null;
} else {
return node.getPath();
}
} else {
return super.getPathFromGraphSubject(subjectUri);
return node.getPath();
}
return super.getPathFromGraphSubject(subjectUri);
}

@Override
public Resource getGraphSubject(final String absPath) throws RepositoryException {
if (absPath.contains("jcr:versionStorage")) {
Node probableFrozenNode = internalSession.getNode(absPath);
final Node probableFrozenNode = internalSession.getNode(absPath);
if (probableFrozenNode.getPrimaryNodeType().getName().equals("nt:frozenNode")) {
URI result = nodesBuilder.buildFromMap(getPathMapForVersionNode(probableFrozenNode));
final URI result = nodesBuilder.buildFromMap(getPathMapForVersionNode(probableFrozenNode));
LOGGER.debug("Translated path {} into RDF subject {}", absPath, result);
return createResource(result.toString());
} else {
LOGGER.debug("{} was not a frozen node... no version-specific translation required", absPath);
}
LOGGER.debug("{} was not a frozen node... no version-specific translation required", absPath);
}
return super.getGraphSubject(absPath);
}

@Override
public Resource getGraphSubject(final Node node) throws RepositoryException {
if (node.getPrimaryNodeType().getName().equals("nt:frozenNode")) {
URI result = nodesBuilder.buildFromMap(getPathMapForVersionNode(node));
final URI result = nodesBuilder.buildFromMap(getPathMapForVersionNode(node));
LOGGER.debug("Translated node {} into RDF subject {}", node, result);
return createResource(result.toString());
}
Expand Down Expand Up @@ -149,7 +147,7 @@ private Map<String,String> getPathMapForVersionNode(final Node frozenNode)

pathWithinVersionable = versionableFrozenNode.getIdentifier()
+ (pathWithinVersionable.length() > 0 ? pathWithinVersionable : "");
String pathToVersionable = versionableNode.getPath();
final String pathToVersionable = versionableNode.getPath();
LOGGER.debug("frozen node found with id {}.", versionableFrozenNode.getIdentifier());
String path = pathToVersionable + "/" + FCR_VERSIONS + "/" + pathWithinVersionable;
if (path.endsWith(JCR_CONTENT)) {
Expand All @@ -158,7 +156,7 @@ private Map<String,String> getPathMapForVersionNode(final Node frozenNode)
return singletonMap("path", path.startsWith("/") ? path.substring(1) : path);
}

private String getRelativePath(Node node, Node ancestor) throws RepositoryException {
private static String getRelativePath(final Node node, final Node ancestor) throws RepositoryException {
return node.getPath().substring(ancestor.getPath().length() + 1);
}

Expand Down Expand Up @@ -198,15 +196,14 @@ public Node getNodeFromGraphSubjectForVersionNode(final String subjectUri)
}
return internalSession.getNode(versionedFrozenNode.getPath() + "/"
+ pathIntoVersionedGraph.replace(FCR_CONTENT, JCR_CONTENT));
} else {
/**
* The subjectUri references a version of a verisonable node identified
* by label (the root of the versioned subgraph).
*/
final String label = labelAndPath;
LOGGER.trace("subjectUri={}, label={}", subjectUri, label);
return getFrozenNodeByLabel(subjectUri, label);
}
/**
* The subjectUri references a version of a verisonable node identified
* by label (the root of the versioned subgraph).
*/
final String label = labelAndPath;
LOGGER.trace("subjectUri={}, label={}", subjectUri, label);
return getFrozenNodeByLabel(subjectUri, label);
}

/**
Expand All @@ -216,7 +213,7 @@ public Node getNodeFromGraphSubjectForVersionNode(final String subjectUri)
* was used for versions created without a label. The current implementation
* uses the JCR UUID for the frozen node as the system-assigned label.
*/
private Node getFrozenNodeByLabel(String subjectUri, String label) throws RepositoryException {
private Node getFrozenNodeByLabel(final String subjectUri, final String label) throws RepositoryException {
final String baseResourcePath
= getPathFromGraphSubject(subjectUri.substring(0, subjectUri.indexOf("/" + FCR_VERSIONS)));
try {
Expand All @@ -239,20 +236,20 @@ private Node getFrozenNodeByLabel(String subjectUri, String label) throws Reposi
* node we were looking for, so fall through and look for a labeled
* node.
*/
} catch (ItemNotFoundException ex) {
} catch (final ItemNotFoundException ex) {
/*
* the label wasn't a uuid of a frozen node but
* instead possibly a version label.
*/
}

VersionHistory hist = internalSession.getWorkspace().getVersionManager().getVersionHistory(baseResourcePath);
final VersionHistory hist =
internalSession.getWorkspace().getVersionManager().getVersionHistory(baseResourcePath);
if (hist.hasVersionLabel(label)) {
LOGGER.debug("Found version for {} by label {}.", subjectUri, label);
return hist.getVersionByLabel(label).getFrozenNode();
} else {
LOGGER.warn("Unknown version {} with label or uuid {}!", subjectUri, label);
return null;
}
LOGGER.warn("Unknown version {} with label or uuid {}!", subjectUri, label);
return null;
}
}
Expand Up @@ -43,14 +43,11 @@

import org.fcrepo.http.commons.api.rdf.HttpGraphSubjects;
import org.fcrepo.http.commons.session.SessionFactory;
import org.fcrepo.http.commons.test.util.TestHelpers;
import org.fcrepo.kernel.FedoraResourceImpl;
import org.fcrepo.kernel.rdf.GraphSubjects;
import org.fcrepo.kernel.services.NodeService;
import org.fcrepo.kernel.services.VersionService;
import org.fcrepo.kernel.utils.iterators.RdfStream;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.Matchers;
import org.mockito.Mock;
Expand Down
Expand Up @@ -35,7 +35,6 @@
import java.util.UUID;

import static org.fcrepo.jcr.FedoraJcrTypes.FCR_CONTENT;
import static org.fcrepo.jcr.FedoraJcrTypes.FCR_VERSIONS;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand All @@ -58,6 +57,7 @@ public void setUpForThisClass() throws RepositoryException {
when(mockWorkspace.getVersionManager()).thenReturn(mockVersionManager);
}

@Override
protected HttpGraphSubjects getTestObj() {
return new VersionAwareHttpGraphSubjects(mockSession, mockSession,
MockNodeController.class,
Expand All @@ -68,44 +68,44 @@ protected HttpGraphSubjects getTestObj() {
public void testGetGraphSubject() throws RepositoryException {
mockVersion(UUID.randomUUID().toString());

String uri = "http://localhost:8080/fcrepo/rest" + mockVersionableNode.getPath()
final String uri = "http://localhost:8080/fcrepo/rest" + mockVersionableNode.getPath()
+ "/fcr:versions/" + mockFrozenNode.getIdentifier();

Resource actual = testObj.getGraphSubject(mockFrozenNode.getPath());
final Resource actual = testObj.getGraphSubject(mockFrozenNode.getPath());
assertEquals(uri, actual.getURI());
}

@Test
public void testGetGraphSubjectChildNode() throws RepositoryException {
mockVersion(UUID.randomUUID().toString());

String uri = "http://localhost:8080/fcrepo/rest" + mockVersionableNode.getPath()
final String uri = "http://localhost:8080/fcrepo/rest" + mockVersionableNode.getPath()
+ "/fcr:versions/" + mockFrozenNode.getIdentifier() + "/fcr:content";

String contentNodeId = UUID.randomUUID().toString();
Node mockVersionChildNode = mock(Node.class);
final String contentNodeId = UUID.randomUUID().toString();
final Node mockVersionChildNode = mock(Node.class);
when(mockSession.getNodeByIdentifier(contentNodeId)).thenReturn(mockVersionChildNode);
when(mockVersionChildNode.isNodeType("mix:versionable")).thenReturn(false);
final String mockVersionedChildNodePath = mockVersionableNode.getPath() + "/" + FCR_CONTENT;
when(mockVersionChildNode.getPath()).thenReturn(mockVersionedChildNodePath);
when(mockVersionChildNode.getParent()).thenReturn(mockVersionableNode);

Node mockFrozenChildNode = mock(Node.class);
final Node mockFrozenChildNode = mock(Node.class);
final String mockFrozenChildNodePath = mockFrozenNode.getPath() + "/" + JCR_CONTENT;
when(mockFrozenChildNode.getPath()).thenReturn(mockFrozenChildNodePath);
NodeType mockFrozenNodeType = mock(NodeType.class);
final NodeType mockFrozenNodeType = mock(NodeType.class);
when(mockFrozenNodeType.getName()).thenReturn("nt:frozenNode");
when(mockFrozenChildNode.getPrimaryNodeType()).thenReturn(mockFrozenNodeType);
when(mockSession.nodeExists(mockFrozenChildNodePath)).thenReturn(true);
when(mockSession.getNode(mockFrozenChildNodePath)).thenReturn(mockFrozenChildNode);
Property mockProperty = mock(Property.class);
final Property mockProperty = mock(Property.class);
when(mockProperty.getString()).thenReturn(contentNodeId);
when(mockFrozenChildNode.getProperty("jcr:frozenUuid")).thenReturn(mockProperty);
when(mockFrozenChildNode.getPath()).thenReturn(mockFrozenChildNodePath);
when(mockWorkspace.getName()).thenReturn("default");
when(mockFrozenChildNode.getParent()).thenReturn(mockFrozenNode);

Resource actual = testObj.getGraphSubject(mockFrozenChildNodePath);
final Resource actual = testObj.getGraphSubject(mockFrozenChildNodePath);
assertEquals(uri, actual.getURI());
}

Expand All @@ -114,7 +114,7 @@ public void testGetGraphSubjectChildNode() throws RepositoryException {
public void testGetGraphSubjectForVersion() throws RepositoryException {
mockVersion(UUID.randomUUID().toString());

Resource actual = testObj.getGraphSubject(mockFrozenNode);
final Resource actual = testObj.getGraphSubject(mockFrozenNode);
assertEquals("http://localhost:8080/fcrepo/rest" + mockVersionableNode.getPath() + "/fcr:versions/"
+ mockFrozenNode.getIdentifier(), actual.getURI());
}
Expand All @@ -125,11 +125,11 @@ public void testGetNodeFromGraphSubjectForVersionByUUID() throws PathNotFoundExc

mockVersion(UUID.randomUUID().toString());

String uri = "http://localhost:8080/fcrepo/rest" + mockVersionableNode.getPath()
final String uri = "http://localhost:8080/fcrepo/rest" + mockVersionableNode.getPath()
+ "/fcr:versions/" + mockFrozenNode.getIdentifier();
mockSubject(uri);

Node actual = testObj.getNodeFromGraphSubject(mockSubject);
final Node actual = testObj.getNodeFromGraphSubject(mockSubject);
assertEquals(mockFrozenNode, actual);

}
Expand All @@ -141,19 +141,19 @@ public void testGetNodeFromGraphSubjectForVersionByLabel() throws PathNotFoundEx

final String label = UUID.randomUUID().toString();

when(mockSession.getNodeByIdentifier(label)).thenThrow(ItemNotFoundException.class);
Version mockVersion = mock(Version.class);
when(mockSession.getNodeByIdentifier(label)).thenThrow(new ItemNotFoundException());
final Version mockVersion = mock(Version.class);
when(mockVersion.getFrozenNode()).thenReturn(mockFrozenNode);
VersionHistory mockVersionHistory = mock(VersionHistory.class);
final VersionHistory mockVersionHistory = mock(VersionHistory.class);
when(mockVersionHistory.hasVersionLabel(label)).thenReturn(true);
when(mockVersionHistory.getVersionByLabel(label)).thenReturn(mockVersion);
when(mockVersionManager.getVersionHistory(mockVersionableNode.getPath())).thenReturn(mockVersionHistory);

String uri = "http://localhost:8080/fcrepo/rest" + mockVersionableNode.getPath()
final String uri = "http://localhost:8080/fcrepo/rest" + mockVersionableNode.getPath()
+ "/fcr:versions/" + label;
mockSubject(uri);

Node actual = testObj.getNodeFromGraphSubject(mockSubject);
final Node actual = testObj.getNodeFromGraphSubject(mockSubject);
assertEquals(mockFrozenNode, actual);
}

Expand All @@ -163,22 +163,22 @@ public void testGetNodeFromGraphSubjectForVersionChildByUUID() throws PathNotFou

mockVersion(UUID.randomUUID().toString());

String uri = "http://localhost:8080/fcrepo/rest" + mockVersionableNode.getPath()
final String uri = "http://localhost:8080/fcrepo/rest" + mockVersionableNode.getPath()
+ "/fcr:versions/" + mockFrozenNode.getIdentifier() + "/fcr:content";
mockSubject(uri);

Node mockVersionChildNode = mock(Node.class);
final Node mockVersionChildNode = mock(Node.class);
final String mockVersionChildNodePath = mockFrozenNode.getPath() + "/" + JCR_CONTENT;
when(mockSession.getNode(mockVersionChildNodePath)).thenReturn(mockVersionChildNode);
when(mockVersionChildNode.getPath()).thenReturn(mockVersionChildNodePath);
when(mockSession.nodeExists(mockVersionChildNodePath)).thenReturn(true);
when(mockSession.getNode(mockVersionChildNodePath)).thenReturn(mockVersionChildNode);

Node actual = testObj.getNodeFromGraphSubject(mockSubject);
final Node actual = testObj.getNodeFromGraphSubject(mockSubject);
assertEquals(mockVersionChildNode, actual);
}

private void mockSubject(String uri) {
private void mockSubject(final String uri) {
when(mockSubject.getURI()).thenReturn(uri);
when(mockSubject.isURIResource()).thenReturn(true);
}
Expand All @@ -188,9 +188,9 @@ private void mockSubject(String uri) {
* versionable node and mockFrozenNode represents a historical version
* of that node.
*/
private void mockVersion(String nodePath) throws RepositoryException {
String frozenNodeUUID = UUID.randomUUID().toString();
String versionedNodeUUID = UUID.randomUUID().toString();
private void mockVersion(final String nodePath) throws RepositoryException {
final String frozenNodeUUID = UUID.randomUUID().toString();
final String versionedNodeUUID = UUID.randomUUID().toString();

final String frozenPath = "/jcr:versionStorage/" + frozenNodeUUID;
final String versionedPath = "/" + nodePath;
Expand All @@ -205,11 +205,11 @@ private void mockVersion(String nodePath) throws RepositoryException {
when(mockFrozenNode.getIdentifier()).thenReturn(frozenNodeUUID);
when(mockVersionableNode.getPath()).thenReturn(versionedPath);

NodeType mockFrozenNodeType = mock(NodeType.class);
final NodeType mockFrozenNodeType = mock(NodeType.class);
when(mockFrozenNodeType.getName()).thenReturn("nt:frozenNode");
when(mockFrozenNode.getPrimaryNodeType()).thenReturn(mockFrozenNodeType);
when(mockFrozenNode.getPath()).thenReturn(frozenPath);
Property mockProperty = mock(Property.class);
final Property mockProperty = mock(Property.class);
when(mockProperty.getString()).thenReturn(versionedNodeUUID);
when(mockFrozenNode.getProperty("jcr:frozenUuid")).thenReturn(mockProperty);

Expand Down
Expand Up @@ -35,9 +35,7 @@
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.junit.Ignore;
import org.junit.Test;

Expand Down

0 comments on commit 440a2bc

Please sign in to comment.