Skip to content

Commit

Permalink
Finished unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed May 19, 2013
1 parent deb05bf commit 3a398e0
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 4 deletions.
Expand Up @@ -176,6 +176,10 @@ public void writeTo(final Dataset rdf, final Class<?> type,

}

public void setTemplatesMap(final Map<String, Template> templatesMap) {
this.templatesMap = templatesMap;
}

@Override
public boolean isWriteable(final Class<?> type, final Type genericType,
final Annotation[] annotations, final MediaType mediaType) {
Expand Down
Expand Up @@ -2,6 +2,7 @@
package org.fcrepo.responses;

import static com.hp.hpl.jena.graph.Node.createURI;
import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
import static org.fcrepo.http.RDFMediaType.NTRIPLES_TYPE;
import static org.junit.Assert.assertTrue;
import static org.slf4j.LoggerFactory.getLogger;
Expand All @@ -18,7 +19,6 @@
import com.hp.hpl.jena.graph.Graph;
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.mem.GraphMem;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.sparql.core.DatasetImpl;
import com.hp.hpl.jena.sparql.modify.GraphStoreBasic;
import com.hp.hpl.jena.update.GraphStore;
Expand All @@ -32,8 +32,7 @@ public class GraphStreamingOutputTest {
public void testStuff() throws WebApplicationException, IOException,
RepositoryException {
final GraphStore graphStore =
new GraphStoreBasic(new DatasetImpl(ModelFactory
.createDefaultModel()));
new GraphStoreBasic(new DatasetImpl(createDefaultModel()));
final Graph g = new GraphMem();
g.add(new Triple(createURI("test:subject"),
createURI("test:predicate"), createURI("test:object")));
Expand Down
@@ -1,20 +1,55 @@

package org.fcrepo.responses;

import static com.google.common.collect.ImmutableMap.of;
import static com.hp.hpl.jena.graph.Node.createLiteral;
import static com.hp.hpl.jena.graph.Node.createURI;
import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
import static javax.ws.rs.core.MediaType.TEXT_HTML_TYPE;
import static javax.ws.rs.core.MediaType.TEXT_PLAIN_TYPE;
import static org.fcrepo.responses.RdfSerializationUtils.primaryTypePredicate;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.Type;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;

import org.apache.velocity.Template;
import org.apache.velocity.context.Context;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.sparql.core.DatasetImpl;
import com.sun.jersey.core.util.MultivaluedMapImpl;

public class HtmlProviderTest {

final HtmlProvider htmlProvider = new HtmlProvider();

Dataset testData = new DatasetImpl(createDefaultModel());

{
testData.asDatasetGraph().getDefaultGraph().add(
new Triple(createURI("test:subject"),
createURI("test:predicate"),
createLiteral("test:object")));
testData.asDatasetGraph().getDefaultGraph().add(
new Triple(createURI("test:subject"), primaryTypePredicate,
createLiteral("nt:file")));

}

@Test
public void testIsWriteable() {
assertTrue(
Expand All @@ -38,4 +73,28 @@ public void testGetSize() {

}

@SuppressWarnings({"unchecked", "rawtypes"})
@Test
public void testWriteTo() throws WebApplicationException,
IllegalArgumentException, IOException {
final Template mockTemplate = mock(Template.class);
final ByteArrayOutputStream outStream = new ByteArrayOutputStream();

Mockito.doAnswer(new Answer<Object>() {

@Override
public Object answer(final InvocationOnMock invocation) {
outStream.write("abcdefighijk".getBytes(), 0, 10);
return "I am pretending to merge a template for you.";
}
}).when(mockTemplate).merge(Mockito.isA(Context.class),
Mockito.isA(Writer.class));
htmlProvider.setTemplatesMap(of("nt:file", mockTemplate));
htmlProvider.writeTo(testData, Dataset.class, mock(Type.class), null,
MediaType.valueOf("text/html"),
(MultivaluedMap) new MultivaluedMapImpl(), outStream);
final byte[] results = outStream.toByteArray();
assertTrue("Got no output from serialization!", results.length > 0);

}
}
@@ -1,21 +1,47 @@

package org.fcrepo.responses;

import static com.hp.hpl.jena.graph.Node.createLiteral;
import static com.hp.hpl.jena.graph.Node.createURI;
import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
import static javax.ws.rs.core.MediaType.TEXT_HTML_TYPE;
import static org.fcrepo.responses.RdfSerializationUtils.primaryTypePredicate;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.Type;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;

import org.junit.Test;

import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.sparql.core.DatasetImpl;
import com.sun.jersey.core.util.MultivaluedMapImpl;

public class RdfProviderTest {

final RdfProvider rdfProvider = new RdfProvider();

Dataset testData = new DatasetImpl(createDefaultModel());

{
testData.asDatasetGraph().getDefaultGraph().add(
new Triple(createURI("test:subject"),
createURI("test:predicate"),
createLiteral("test:object")));
testData.asDatasetGraph().getDefaultGraph().add(
new Triple(createURI("test:subject"), primaryTypePredicate,
createLiteral("nt:file")));

}

@Test
public void testIsWriteable() {
assertTrue(
Expand All @@ -38,4 +64,19 @@ public void testGetSize() {
.getSize(null, null, null, null, null), -1);

}

@SuppressWarnings({"unchecked", "rawtypes"})
@Test
public void testWriteTo() throws WebApplicationException,
IllegalArgumentException, IOException {
final ByteArrayOutputStream outStream = new ByteArrayOutputStream();

rdfProvider.writeTo(testData, Dataset.class, mock(Type.class), null,
MediaType.valueOf("application/rdf+xml"),
(MultivaluedMap) new MultivaluedMapImpl(), outStream);
final byte[] results = outStream.toByteArray();
assertTrue("Got no output from serialization!", results.length > 0);
assertTrue("Couldn't find test RDF-object mentioned!", new String(
results).contains("test:object"));
}
}

0 comments on commit 3a398e0

Please sign in to comment.