Skip to content

Commit

Permalink
unit test HttpApiResources
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Jun 14, 2013
1 parent 7dae1dd commit 47af7f7
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 42 deletions.
99 changes: 64 additions & 35 deletions fcrepo-http-api/src/main/java/org/fcrepo/url/HttpApiResources.java
Expand Up @@ -27,6 +27,9 @@
import org.fcrepo.api.repository.FedoraRepositoryNamespaces;
import org.fcrepo.rdf.GraphSubjects;
import org.fcrepo.serialization.FedoraObjectSerializer;
import org.fcrepo.serialization.SerializerUtil;
import org.fcrepo.utils.FedoraJcrTypes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hp.hpl.jena.rdf.model.Model;
Expand All @@ -35,58 +38,84 @@
@Component
public class HttpApiResources implements UriAwareResourceModelFactory {

@javax.annotation.Resource
protected Map<String, FedoraObjectSerializer> serializers;

@Autowired
protected SerializerUtil serializers;

@Override
public Model createModelForResource(final FedoraResource resource,
final UriInfo uriInfo, final GraphSubjects graphSubjects)
throws RepositoryException {

final Model model = createDefaultModel();
final Resource s = graphSubjects.getGraphSubject(resource.getNode());

if (resource.getNode().getPrimaryNodeType().isNodeType("mode:root")) {
model.add(s, HAS_SEARCH_SERVICE, model.createResource(uriInfo
.getBaseUriBuilder().path(FedoraFieldSearch.class).build()
.toASCIIString()));
model.add(s, HAS_SITEMAP, model.createResource(uriInfo
.getBaseUriBuilder().path(FedoraSitemap.class).build()
.toASCIIString()));
model.add(s, HAS_TRANSACTION_SERVICE, model.createResource(uriInfo
.getBaseUriBuilder().path(FedoraTransactions.class).build()
.toASCIIString()));
model.add(s, HAS_NAMESPACE_SERVICE, model.createResource(uriInfo
.getBaseUriBuilder().path(FedoraRepositoryNamespaces.class)
.build().toASCIIString()));
final Resource s = graphSubjects.getGraphSubject(resource.getNode());

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

for (final String key : serializers.keySet()) {
final Map<String, String> pathMap =
of("path", resource.getPath().substring(1), "format",
key);
model.add(s, HAS_SERIALIZATION, model.createResource(uriInfo
.getBaseUriBuilder().path(FedoraExport.class)
.buildFromMap(pathMap).toASCIIString()));
}
if (resource.hasContent()) {
addContentStatements(resource, uriInfo, model, s);
}

final Map<String, String> pathMap =
of("path", resource.getPath().substring(1));
model.add(s, HAS_VERSION_HISTORY, model.createResource(uriInfo
.getBaseUriBuilder().path(FedoraVersions.class)
.buildFromMap(pathMap).toASCIIString()));
return model;
}

}
private void addContentStatements(FedoraResource resource, UriInfo uriInfo, Model model, Resource s) throws RepositoryException {
// fcr:fixity
final Map<String, String> pathMap =
of("path", resource.getPath().substring(1));
model.add(s, HAS_FIXITY_SERVICE, model.createResource(uriInfo
.getBaseUriBuilder().path(FedoraFixity.class).buildFromMap(
pathMap).toASCIIString()));
}

if (resource.hasContent()) {
private void addNodeStatements(FedoraResource resource, UriInfo uriInfo, Model model, Resource s) throws RepositoryException {

// fcr:export?format=xyz
for (final String key : serializers.keySet()) {
final Map<String, String> pathMap =
of("path", resource.getPath().substring(1));
model.add(s, HAS_FIXITY_SERVICE, model.createResource(uriInfo
.getBaseUriBuilder().path(FedoraFixity.class).buildFromMap(
pathMap).toASCIIString()));
model.add(s, HAS_SERIALIZATION, model.createResource(uriInfo
.getBaseUriBuilder().path(FedoraExport.class).queryParam("format", key)
.buildFromMap(pathMap).toASCIIString()));
}

return model;
// fcr:versions
final Map<String, String> pathMap =
of("path", resource.getPath().substring(1));
model.add(s, HAS_VERSION_HISTORY, model.createResource(uriInfo
.getBaseUriBuilder().path(FedoraVersions.class)
.buildFromMap(pathMap).toASCIIString()));
}

private void addRepositoryStatements(UriInfo uriInfo, Model model, Resource s) {
// fcr:search
model.add(s, HAS_SEARCH_SERVICE, model.createResource(uriInfo
.getBaseUriBuilder().path(FedoraFieldSearch.class).build()
.toASCIIString()));

// sitemap
model.add(s, HAS_SITEMAP, model.createResource(uriInfo
.getBaseUriBuilder().path(FedoraSitemap.class).build()
.toASCIIString()));

// fcr:tx
model.add(s, HAS_TRANSACTION_SERVICE, model.createResource(uriInfo
.getBaseUriBuilder().path(FedoraTransactions.class).build()
.toASCIIString()));

// fcr:namespaces
model.add(s, HAS_NAMESPACE_SERVICE, model.createResource(uriInfo
.getBaseUriBuilder().path(FedoraRepositoryNamespaces.class)
.build().toASCIIString()));
}

public void setSerializers(final SerializerUtil serializerUtil) {
this.serializers = serializerUtil;
}

}
105 changes: 105 additions & 0 deletions fcrepo-http-api/src/test/java/org/fcrepo/url/HttpApiResourcesTest.java
@@ -0,0 +1,105 @@
package org.fcrepo.url;

import com.google.common.collect.ImmutableSet;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Resource;
import org.fcrepo.FedoraResource;
import org.fcrepo.api.FedoraNodes;
import org.fcrepo.api.rdf.HttpGraphSubjects;
import org.fcrepo.rdf.GraphSubjects;
import org.fcrepo.serialization.SerializerUtil;
import org.fcrepo.test.util.TestHelpers;
import org.fcrepo.utils.FedoraJcrTypes;
import org.junit.Before;
import org.junit.Test;
import org.modeshape.jcr.api.JcrConstants;

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

import java.util.HashSet;

import static org.fcrepo.RdfLexicon.HAS_FIXITY_SERVICE;
import static org.fcrepo.RdfLexicon.HAS_NAMESPACE_SERVICE;
import static org.fcrepo.RdfLexicon.HAS_SEARCH_SERVICE;
import static org.fcrepo.RdfLexicon.HAS_SERIALIZATION;
import static org.fcrepo.RdfLexicon.HAS_SITEMAP;
import static org.fcrepo.RdfLexicon.HAS_TRANSACTION_SERVICE;
import static org.fcrepo.RdfLexicon.HAS_VERSION_HISTORY;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class HttpApiResourcesTest {

private HttpApiResources testObj;
private Node mockNode;
private FedoraResource mockResource;
private UriInfo uriInfo;
private GraphSubjects mockSubjects;
private SerializerUtil mockSerializers;

@Before
public void setUp() {
testObj = new HttpApiResources();
mockNode = mock(Node.class);
mockResource = new FedoraResource(mockNode);

uriInfo = TestHelpers.getUriInfoImpl();
mockSubjects = new HttpGraphSubjects(FedoraNodes.class, uriInfo);

mockSerializers = mock(SerializerUtil.class);
testObj.setSerializers(mockSerializers);
}

@Test
public void shouldDecorateModeRootNodesWithRepositoryWideLinks() throws RepositoryException {

final NodeType mockNodeType = mock(NodeType.class);
when(mockNodeType.isNodeType(FedoraJcrTypes.ROOT)).thenReturn(true);
when(mockNode.getPrimaryNodeType()).thenReturn(mockNodeType);
when(mockNode.getPath()).thenReturn("/");

Resource graphSubject = mockSubjects.getGraphSubject(mockNode);

final Model model = testObj.createModelForResource(mockResource, uriInfo, mockSubjects);

assertTrue(model.contains(graphSubject, HAS_SEARCH_SERVICE));
assertTrue(model.contains(graphSubject, HAS_SITEMAP));
assertTrue(model.contains(graphSubject, HAS_TRANSACTION_SERVICE));
assertTrue(model.contains(graphSubject, HAS_NAMESPACE_SERVICE));
}

@Test
public void shouldDecorateNodesWithLinksToVersionsAndExport() throws RepositoryException {

when(mockNode.getPrimaryNodeType()).thenReturn(mock(NodeType.class));
when(mockNode.getPath()).thenReturn("/some/path/to/object");

when(mockSerializers.keySet()).thenReturn(ImmutableSet.of("a", "b"));
Resource graphSubject = mockSubjects.getGraphSubject(mockNode);


final Model model = testObj.createModelForResource(mockResource, uriInfo, mockSubjects);

assertTrue(model.contains(graphSubject, HAS_VERSION_HISTORY));
assertEquals(2, model.listObjectsOfProperty(graphSubject, HAS_SERIALIZATION).toSet().size());
}

@Test
public void shouldDecorateDatastreamsWithLinksToFixityChecks() throws RepositoryException {
when(mockNode.hasNode(JcrConstants.JCR_CONTENT)).thenReturn(true);
when(mockNode.getPrimaryNodeType()).thenReturn(mock(NodeType.class));
when(mockNode.getPath()).thenReturn("/some/path/to/datastream");
when(mockSerializers.keySet()).thenReturn(new HashSet<String>());
Resource graphSubject = mockSubjects.getGraphSubject(mockNode);

final Model model = testObj.createModelForResource(mockResource, uriInfo, mockSubjects);

assertTrue(model.contains(graphSubject, HAS_FIXITY_SERVICE));
}

}
Expand Up @@ -44,6 +44,8 @@
import org.fcrepo.session.AuthenticatedSessionProvider;
import org.fcrepo.session.SessionFactory;
import org.fcrepo.utils.ContentDigest;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.modeshape.jcr.api.NamespaceRegistry;
import org.modeshape.jcr.api.Repository;
import org.modeshape.jcr.api.query.QueryManager;
Expand All @@ -66,16 +68,26 @@ public abstract class TestHelpers {
public static UriInfo getUriInfoImpl() {
// UriInfo ui = mock(UriInfo.class,withSettings().verboseLogging());
final UriInfo ui = mock(UriInfo.class);
final UriBuilder ub = new UriBuilderImpl();
ub.scheme("http");
ub.host("localhost");
ub.path("/fcrepo");

when(ui.getRequestUri()).thenReturn(
final Answer<UriBuilder> answer = new Answer<UriBuilder>() {
@Override
public UriBuilder answer(InvocationOnMock invocation) throws Throwable {

final UriBuilder ub = new UriBuilderImpl();
ub.scheme("http");
ub.host("localhost");
ub.path("/fcrepo");


return ub;
}
};

when(ui.getRequestUri()).thenReturn(
URI.create("http://localhost/fcrepo"));
when(ui.getBaseUri()).thenReturn(URI.create("http://localhost/"));
when(ui.getBaseUriBuilder()).thenReturn(ub);
when(ui.getAbsolutePathBuilder()).thenReturn(ub);
when(ui.getBaseUriBuilder()).thenAnswer(answer);
when(ui.getAbsolutePathBuilder()).thenAnswer(answer);

return ui;
}
Expand Down
Expand Up @@ -9,6 +9,7 @@
import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import static org.slf4j.LoggerFactory.getLogger;

Expand All @@ -26,6 +27,10 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
this.applicationContext = applicationContext;
}

public Set<String> keySet() {
return getFedoraObjectSerializers().keySet();
}

public FedoraObjectSerializer getSerializer(final String format) {
return getFedoraObjectSerializers().get(format);
}
Expand Down

0 comments on commit 47af7f7

Please sign in to comment.