Skip to content

Commit

Permalink
Changed FedoraContent to use injected Session
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed May 8, 2013
1 parent 76574a7 commit e78fca8
Show file tree
Hide file tree
Showing 16 changed files with 231 additions and 215 deletions.
Expand Up @@ -46,21 +46,23 @@ public class FedoraRdfGenerator extends AbstractResource {
private static final ValueFactory valFactory = new MemValueFactory();

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

private List<TripleSource<FedoraObject>> objectGenerators;

private List<TripleSource<Datastream>> datastreamGenerators;

@GET
@Produces({TEXT_XML, "text/turtle", TEXT_PLAIN})
public String getRdfXml(
@PathParam("path") final List<PathSegment> pathList,
@HeaderParam("Accept") @DefaultValue(TEXT_XML) final String mimeType
) throws IOException, RepositoryException, TripleHandlerException {
public String getRdf(@PathParam("path")
final List<PathSegment> pathList, @HeaderParam("Accept")
@DefaultValue(TEXT_XML)
final String mimeType) throws IOException, RepositoryException,
TripleHandlerException {

final Session session = getAuthenticatedSession();
final Session session = getAuthenticatedSession();
final String path = toPath(pathList);
final java.net.URI itemUri = uriInfo.getBaseUriBuilder().build("/rest" + path);
final java.net.URI itemUri =
uriInfo.getBaseUriBuilder().build("/rest" + path);
final URI docURI = valFactory.createURI(itemUri.toString());
logger.debug("Using ValueFactory: " + valFactory.toString());
final ExtractionContext context =
Expand All @@ -82,42 +84,41 @@ public String getRdfXml(
writeObjectTriples(path, writer, context);
}


writer.endDocument(docURI);
writer.close();
logger.debug("Generated RDF: {}", out.toString());
return out.toString();
}

}

// add JCR-managed namespaces
private void writeNamespaces(Node node, TripleHandler writer, ExtractionContext context)
throws TripleHandlerException, RepositoryException {
private void writeNamespaces(final Node node, final TripleHandler writer,
final ExtractionContext context) throws TripleHandlerException,
RepositoryException {
final NamespaceRegistry nReg =
node.getSession().getWorkspace()
.getNamespaceRegistry();
node.getSession().getWorkspace().getNamespaceRegistry();
for (final String prefix : nReg.getPrefixes()) {
final String nsURI = nReg.getURI(prefix);
if (nsURI != null && !nsURI.equals("") &&
!prefix.equals("xmlns")) {
if (nsURI != null && !nsURI.equals("") && !prefix.equals("xmlns")) {
writer.receiveNamespace(prefix, nsURI, context);
}
}
}

private void writeDatastreamTriples(String path, TripleHandler writer, ExtractionContext context)

private void writeDatastreamTriples(final String path,
final TripleHandler writer, final ExtractionContext context)
throws TripleHandlerException, RepositoryException {

final Session session = getAuthenticatedSession();
final Session session = getAuthenticatedSession();

final Datastream obj = datastreamService.getDatastream(session, path);
// add namespaces
writeNamespaces(obj.getNode(), writer, context);
// add triples from each TripleSource
for (final TripleSource<Datastream> tripleSource : datastreamGenerators) {
logger.trace("Using TripleSource: {}",
tripleSource.getClass().getName());
logger.trace("Using TripleSource: {}", tripleSource.getClass()
.getName());
for (final Triple t : tripleSource.getTriples(obj, uriInfo)) {
writer.receiveTriple(valFactory.createURI(t.subject),
valFactory.createURI(t.predicate), valFactory
Expand All @@ -126,24 +127,25 @@ private void writeDatastreamTriples(String path, TripleHandler writer, Extractio
}
}

private void writeObjectTriples(String path, TripleHandler writer, ExtractionContext context)
private void writeObjectTriples(final String path,
final TripleHandler writer, final ExtractionContext context)
throws TripleHandlerException, RepositoryException {

final Session session = getAuthenticatedSession();
final Session session = getAuthenticatedSession();

final FedoraObject obj = objectService.getObject(session, path);
// add namespaces
writeNamespaces(obj.getNode(), writer, context);
// add triples from each TripleSource
for (final TripleSource<FedoraObject> tripleSource : objectGenerators) {
logger.trace("Using TripleSource: {}",
tripleSource.getClass().getName());
logger.trace("Using TripleSource: {}", tripleSource.getClass()
.getName());
for (final Triple t : tripleSource.getTriples(obj, uriInfo)) {
writer.receiveTriple(valFactory.createURI(t.subject),
valFactory.createURI(t.predicate), valFactory
.createLiteral(t.object), null, context);
}
}
}
}

public void setObjectGenerators(
Expand All @@ -156,11 +158,11 @@ public void setDatastreamGenerators(
this.datastreamGenerators = dsGenerators;
}

void setObjectService(ObjectService objectService) {
void setObjectService(final ObjectService objectService) {
this.objectService = objectService;
}

void setDatastreamService(DatastreamService datastreamService) {
void setDatastreamService(final DatastreamService datastreamService) {
this.datastreamService = datastreamService;
}

Expand Down
Expand Up @@ -15,6 +15,7 @@

import org.fcrepo.Datastream;
import org.slf4j.Logger;

import com.google.common.base.Function;

public class DefaultDatastreamGenerator implements TripleSource<Datastream> {
Expand Down
@@ -1,8 +1,11 @@

package org.fcrepo.generator;

import static javax.ws.rs.core.MediaType.TEXT_PLAIN;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.lang.reflect.Field;
Expand Down Expand Up @@ -32,82 +35,89 @@
import org.junit.Before;
import org.junit.Test;


public class FedoraRdfGeneratorTest {

private FedoraRdfGenerator testObj;

private ObjectService mockObjects;

private DatastreamService mockDatastreams;

private UriInfo mockURIs;
private Session mockSession;

@Before
public void setUp() throws IllegalArgumentException, UriBuilderException, URISyntaxException, RepositoryException {
private Session mockSession;

@Before
public void setUp() throws IllegalArgumentException, UriBuilderException,
URISyntaxException, RepositoryException {
testObj = new FedoraRdfGenerator();
mockSession = TestHelpers.mockSession(testObj);
mockSession = TestHelpers.mockSession(testObj);
mockObjects = mock(ObjectService.class);
mockDatastreams = mock(DatastreamService.class);
mockURIs = mock(UriInfo.class);
UriBuilder mockBuilder = mock(UriBuilder.class);
final UriBuilder mockBuilder = mock(UriBuilder.class);
when(mockBuilder.build(any())).thenReturn(new URI("http://fcrepo.tv/"));
when(mockURIs.getBaseUriBuilder()).thenReturn(mockBuilder);
testObj.setObjectService(mockObjects);
testObj.setDatastreamService(mockDatastreams);
testObj.setUriInfo(mockURIs);
}

@Test
public void testSetObjectGenerators() throws IOException, RepositoryException, TripleHandlerException {
public void testSetObjectGenerators() throws IOException,
RepositoryException, TripleHandlerException {
@SuppressWarnings("unchecked")
List<TripleSource<FedoraObject>> mockList = mock(List.class);
final List<TripleSource<FedoraObject>> mockList = mock(List.class);
testObj.setObjectGenerators(mockList);
@SuppressWarnings("unchecked")
List<TripleSource<FedoraObject>> actual = (List<TripleSource<FedoraObject>>) getMember("objectGenerators");
final List<TripleSource<FedoraObject>> actual =
(List<TripleSource<FedoraObject>>) getMember("objectGenerators");
assertEquals(mockList, actual);
}

@Test
public void testSetDatastreamGenerators() throws IOException, RepositoryException, TripleHandlerException {
public void testSetDatastreamGenerators() throws IOException,
RepositoryException, TripleHandlerException {
@SuppressWarnings("unchecked")
List<TripleSource<Datastream>> mockList = mock(List.class);
final List<TripleSource<Datastream>> mockList = mock(List.class);
testObj.setDatastreamGenerators(mockList);
@SuppressWarnings("unchecked")
List<TripleSource<Datastream>> actual = (List<TripleSource<Datastream>>) getMember("datastreamGenerators");
final List<TripleSource<Datastream>> actual =
(List<TripleSource<Datastream>>) getMember("datastreamGenerators");
assertEquals(mockList, actual);
}

@Test
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);
public void testGetRdfXml() throws IOException, RepositoryException,
TripleHandlerException {
final List<PathSegment> pathList =
PathSegmentImpl.createPathList("objects", "foo");
final FedoraObject mockObj = mock(FedoraObject.class);
final Workspace mockWS = mock(Workspace.class);
final 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);
when(mockNS.getPrefixes()).thenReturn(new String[] {});
final Node mockNode = mock(Node.class);
when(mockObj.getNode()).thenReturn(mockNode);
when(mockNode.getSession()).thenReturn(mockSession);
when(mockObjects.getObject(mockSession, "/objects/foo")).thenReturn(mockObj);
when(mockObjects.getObject(mockSession, "/objects/foo")).thenReturn(
mockObj);

@SuppressWarnings("unchecked")
TripleSource<FedoraObject>[] gens = new TripleSource[0];
List<TripleSource<FedoraObject>> mockList = Arrays.asList(gens);
final TripleSource<FedoraObject>[] gens = new TripleSource[0];
final List<TripleSource<FedoraObject>> mockList = Arrays.asList(gens);
testObj.setObjectGenerators(mockList);


testObj.getRdfXml(pathList, TEXT_PLAIN);
testObj.getRdf(pathList, TEXT_PLAIN);
}

private Object getMember(String name) {
private Object getMember(final String name) {
try {
Field field = FedoraRdfGenerator.class.getDeclaredField(name);
final Field field = FedoraRdfGenerator.class.getDeclaredField(name);
field.setAccessible(true);
return field.get(testObj);
} catch (Exception e) {
} catch (final Exception e) {
e.printStackTrace();
return null;
}
Expand Down
Expand Up @@ -2,26 +2,13 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.*;
import static com.google.common.collect.ImmutableList.copyOf;
import static com.google.common.collect.Iterables.concat;
import static org.slf4j.LoggerFactory.getLogger;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.ws.rs.core.UriBuilderException;
import javax.ws.rs.core.UriInfo;

import org.fcrepo.Datastream;
import org.slf4j.Logger;
import com.google.common.base.Function;

import java.util.List;

import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.nodetype.NodeType;
Expand Down
@@ -1,3 +1,4 @@

package org.fcrepo.generator.rdf;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -25,33 +26,36 @@
public class PropertiesGeneratorTest {

private PropertiesGenerator testObj;

@Before
public void setUp() {
testObj = new PropertiesGenerator();
}


@SuppressWarnings("unchecked")
@Test
public void testGetTriples() throws RepositoryException {
String path = "/testing/fake/object";
UriInfo mockUris = mock(UriInfo.class);
Node mockNode = mock(Node.class);
final String path = "/testing/fake/object";
final UriInfo mockUris = mock(UriInfo.class);
final Node mockNode = mock(Node.class);
when(mockNode.getPath()).thenReturn(path);
Property mockProp = mock(Property.class);
final Property mockProp = mock(Property.class);
when(mockProp.getParent()).thenReturn(mockNode);
when(mockProp.getString()).thenReturn("mockValue");
PropertyIterator mockProps = mock(PropertyIterator.class);
final PropertyIterator mockProps = mock(PropertyIterator.class);
when(mockProps.hasNext()).thenReturn(true, false);
when(mockProps.next()).thenReturn(mockProp).thenThrow(IndexOutOfBoundsException.class);
when(mockProps.next()).thenReturn(mockProp).thenThrow(
IndexOutOfBoundsException.class);
when(mockNode.getProperties()).thenReturn(mockProps);

// mock the static method on Utils
PowerMockito.mockStatic(Utils.class);
when(Utils.expandJCRNamespace(mockProp)).thenReturn("{http://fcrepo.co.uk/test/}property");
when(Utils.expandJCRNamespace(mockProp)).thenReturn(
"{http://fcrepo.co.uk/test/}property");

List<Triple> triples = testObj.getTriples(mockNode, mockUris);
final List<Triple> triples = testObj.getTriples(mockNode, mockUris);
assertEquals(1, triples.size());
Triple t = triples.get(0);
final Triple t = triples.get(0);
assertEquals("{http://fcrepo.co.uk/test/}property", t.predicate);
assertEquals("mockValue", t.object);
}
Expand Down
@@ -1,7 +1,8 @@
package org.fcrepo.generator.rdf;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.OutputStream;

Expand Down

0 comments on commit e78fca8

Please sign in to comment.