Skip to content

Commit

Permalink
Last bit of unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Nov 1, 2013
1 parent 515e582 commit 05c2320
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
Expand Up @@ -36,7 +36,6 @@
import org.openrdf.rio.RDFFormat;
import org.openrdf.rio.RDFHandlerException;
import org.openrdf.rio.Rio;
import org.openrdf.rio.WriterConfig;
import org.slf4j.Logger;

import com.google.common.base.Function;
Expand Down Expand Up @@ -82,7 +81,7 @@ public void write(final OutputStream output) throws IOException,
WebApplicationException {
LOGGER.debug("Serializing RDF stream in: {}", format);
try {
Rio.write(asStatements(), output, format, new WriterConfig());
Rio.write(asStatements(), output, format);
} catch (final RDFHandlerException e) {
setException(e);
} finally {
Expand Down
Expand Up @@ -13,15 +13,18 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.fcrepo.http.commons.responses;

import static com.google.common.util.concurrent.Futures.addCallback;
import static com.hp.hpl.jena.graph.NodeFactory.createURI;
import static com.hp.hpl.jena.graph.Triple.create;
import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
import static javax.ws.rs.core.MediaType.valueOf;
import static org.fcrepo.http.commons.responses.RdfStreamStreamingOutput.getValueForObject;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.openrdf.model.impl.ValueFactoryImpl.getInstance;
import static org.openrdf.model.util.Literals.createLiteral;
Expand All @@ -30,28 +33,41 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.ws.rs.core.MediaType;

import org.fcrepo.kernel.utils.iterators.RdfStream;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.openrdf.model.Value;
import org.openrdf.model.ValueFactory;
import org.openrdf.rio.RDFHandlerException;

import com.google.common.util.concurrent.FutureCallback;
import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.NodeFactory;
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.rdf.model.Model;


public class RdfStreamStreamingOutputTest {

private RdfStreamStreamingOutput testRdfStreamStreamingOutput;

private static final Triple triple = create(createURI("info:testSubject"),
createURI("info:testPredicate"), createURI("info:testObject"));

@Mock
private Node mockNode;

private RdfStream testRdfStream = new RdfStream(triple);

@Mock
private RdfStream mockRdfStream;

private MediaType testMediaType = valueOf("application/rdf+xml");

private static final ValueFactory vf = getInstance();
Expand Down Expand Up @@ -90,11 +106,43 @@ public void testWrite() throws IOException {
try (
final InputStream resultStream =
new ByteArrayInputStream(output.toByteArray())) {
final Model result = createDefaultModel().read(resultStream, null);
final Model result =
createDefaultModel().read(resultStream, null);
assertTrue("Didn't find our test triple!", result
.contains(result.asStatement(triple)));
}
}
}

@Test
public void testWriteWithException() throws IOException {

final FutureCallback<Void> callback = new FutureCallback<Void>() {

@Override
public void onSuccess(final Void v) {
throw new AssertionError("Should never happen!");
}

@Override
public void onFailure(final Throwable e) {
assertTrue("Got wrong kind of exception!",
e instanceof RDFHandlerException);
}

};
addCallback(testRdfStreamStreamingOutput, callback);
final OutputStream mockOutputStream =
mock(OutputStream.class, new Answer<Object>() {

@Override
public Object answer(final InvocationOnMock invocation)
throws IOException {
throw new IOException("Expected.");
}
});

testRdfStreamStreamingOutput.write(mockOutputStream);
}

}

0 comments on commit 05c2320

Please sign in to comment.