Skip to content
This repository has been archived by the owner on Jan 3, 2019. It is now read-only.

Commit

Permalink
Cleaner mocking in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Jul 12, 2013
1 parent b71fc84 commit 25ba185
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 49 deletions.
Expand Up @@ -23,12 +23,13 @@
import static com.hp.hpl.jena.rdf.model.ModelFactory.createModelForGraph;
import static java.lang.String.format;
import static javax.xml.transform.TransformerFactory.newInstance;
import static org.modeshape.jcr.api.JcrConstants.JCR_CONTENT;
import static org.modeshape.jcr.api.JcrConstants.JCR_DATA;
import static org.slf4j.LoggerFactory.getLogger;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.URL;
import javax.jcr.RepositoryException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
Expand All @@ -48,6 +49,7 @@
import org.apache.any23.writer.TripleHandlerException;
import org.apache.any23.Any23;
import org.apache.any23.ExtractionReport;
import org.fcrepo.rdf.GraphProperties;
import org.fcrepo.rdf.GraphSubjects;
import org.fcrepo.triplegenerators.tei.xslt.LoggingErrorListener;
import org.slf4j.Logger;
Expand All @@ -65,7 +67,7 @@
* @author ajs6f
* @date Jul 10, 2013
*/
public class TeiTripleGenerator {
public class TeiTripleGenerator implements GraphProperties {

private static Transformer addIdsXform;

Expand Down Expand Up @@ -103,49 +105,6 @@ public TeiTripleGenerator() throws TransformerConfigurationException,
}
}


/**
* @param uri
* @param gs
* @param teiLocation
* @return A {@link Dataset} with extracted triples.
*/
public Dataset getTriples(final javax.jcr.Node uri, final GraphSubjects gs,
final URL teiLocation) {
// TODO redo this brainless way of retrieving the resource
try (final InputStream teiStream = teiLocation.openStream()) {
return getTriples(uri, gs, teiStream);
} catch (final IOException e) {
try {
return exceptionRdf(gs.getGraphSubject(uri).getURI(), e);
} catch (final RepositoryException ee) {
return exceptionRdf("unknown", e);
}
}
}

/**
* @param uri
* @param gs
* @param resource
* @return A {@link Dataset} with extracted triples.
*/
public Dataset getTriples(final javax.jcr.Node uri, final GraphSubjects gs,
final InputStream resource) {
String baseUri = "unknown";
try {
baseUri = gs.getGraphSubject(uri).getURI();
final byte[] rdfXml = createRDFXML(resource);
// TODO when Any23 supports it, use a streaming transfer between
// these two steps
return extractTriples(rdfXml, baseUri);
} catch (
TripleHandlerException | IOException | TransformerException |
ExtractionException | RepositoryException e) {
return exceptionRdf(baseUri, e);
}
}

/**
* @param rdfXml
* @param baseUri
Expand Down Expand Up @@ -225,4 +184,41 @@ protected Dataset exceptionRdf(final String baseUri, final Exception... es) {
sadResults.addNamedModel("problems", createModelForGraph(problems));
return sadResults;
}


@Override
public String getPropertyModelName() {
// TODO Auto-generated method stub
return null;
}


@Override
public Dataset getProperties(final javax.jcr.Node node, final GraphSubjects subjects,
final long offset, final int limit) throws RepositoryException {
return getProperties(node, subjects);
}


@Override
public Dataset getProperties(final javax.jcr.Node node, final GraphSubjects subjects) {
String baseUri = "unknown";
byte[] rdfXml;
try {
baseUri = subjects.getGraphSubject(node).getURI();
try (
final InputStream resource =
node.getNode(JCR_CONTENT).getProperty(JCR_DATA).getBinary()
.getStream()) {
rdfXml = createRDFXML(resource);
}
// TODO when Any23 supports it, use a streaming transfer between
// these two steps
return extractTriples(rdfXml, baseUri);
} catch (
TripleHandlerException | IOException | TransformerException |
ExtractionException | RepositoryException e) {
return exceptionRdf(baseUri, e);
}
}
}
Expand Up @@ -23,14 +23,18 @@
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.modeshape.jcr.api.JcrConstants.JCR_CONTENT;
import static org.modeshape.jcr.api.JcrConstants.JCR_DATA;
import static org.slf4j.LoggerFactory.getLogger;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactoryConfigurationError;

Expand All @@ -51,7 +55,7 @@
public class TestTeiTripleGenerator extends TeiTripleGenerator {

@Mock
private Node mockUriNode;
private Node mockContentNode;

@Mock
private GraphSubjects mockGraphSubjects;
Expand All @@ -62,6 +66,12 @@ public class TestTeiTripleGenerator extends TeiTripleGenerator {
@Mock
private com.hp.hpl.jena.graph.Node mockNode;

@Mock
private Property mockProperty;

@Mock
private Binary mockBinary;

private static final Statement testTriple = new StatementImpl(
createResource("http://fedora"),
createProperty("http://purl.org/dc/terms/publisher"),
Expand All @@ -78,10 +88,15 @@ public TestTeiTripleGenerator() throws TransformerConfigurationException,
@Before
public void setUp() throws Exception {
initMocks(this);
when(mockUriNode.getPath()).thenReturn("/test");
when(mockGraphSubjects.getGraphSubject(mockUriNode)).thenReturn(
when(mockContentNode.getPath()).thenReturn("/test");
when(mockGraphSubjects.getGraphSubject(mockContentNode)).thenReturn(
mockResource);
when(mockResource.getURI()).thenReturn("http://fedora");
when(mockContentNode.getNode(JCR_CONTENT)).thenReturn(mockContentNode);
when(mockContentNode.getProperty(JCR_DATA)).thenReturn(mockProperty);
when(mockProperty.getBinary()).thenReturn(mockBinary);
when(mockBinary.getStream()).thenReturn(
new FileInputStream(new File("target/test-classes/tei.xml")));
}

@Test
Expand All @@ -91,7 +106,7 @@ public void testExtraction() throws Exception {
final InputStream teiStream =
new FileInputStream(new File("target/test-classes/tei.xml"))) {
results =
getTriples(mockUriNode, mockGraphSubjects, teiStream)
getProperties(mockContentNode, mockGraphSubjects)
.getDefaultModel();
}
assertFalse("Got no triples!", results.isEmpty());
Expand Down

0 comments on commit 25ba185

Please sign in to comment.