001/*
002 * The contents of this file are subject to the license and copyright
003 * detailed in the LICENSE and NOTICE files at the root of the source
004 * tree.
005 */
006package org.fcrepo.http.commons.responses;
007
008import static java.util.stream.Stream.of;
009import static org.apache.jena.graph.NodeFactory.createURI;
010import static org.apache.jena.graph.Triple.create;
011import static org.apache.jena.rdf.model.ModelFactory.createDefaultModel;
012import static org.junit.Assert.assertEquals;
013import static org.junit.Assert.assertFalse;
014import static org.junit.Assert.assertTrue;
015
016import java.io.ByteArrayInputStream;
017import java.io.ByteArrayOutputStream;
018import java.io.IOException;
019import java.util.HashMap;
020import java.util.Map;
021
022import javax.ws.rs.WebApplicationException;
023import javax.ws.rs.core.MediaType;
024
025import org.fcrepo.kernel.api.RdfStream;
026import org.fcrepo.kernel.api.rdf.DefaultRdfStream;
027import org.junit.Before;
028import org.junit.Test;
029import org.apache.jena.graph.Triple;
030import org.apache.jena.rdf.model.Model;
031
032import org.junit.runner.RunWith;
033import org.mockito.junit.MockitoJUnitRunner;
034
035/**
036 * <p>RdfStreamProviderTest class.</p>
037 *
038 * @author ajs6f
039 */
040@RunWith(MockitoJUnitRunner.Silent.class)
041public class RdfStreamProviderTest {
042
043    private final RdfStreamProvider testProvider = new RdfStreamProvider();
044
045
046    @Before
047    public void setUp() throws Exception {
048    }
049
050    @Test
051    public void testGetSize() {
052        assertEquals(-1, testProvider.getSize(null, null, null, null, null));
053    }
054
055    @Test
056    public void testIsWriteable() {
057        assertTrue("Should be able to serialize this!", testProvider
058                .isWriteable(RdfNamespacedStream.class, null, null, MediaType
059                        .valueOf("application/rdf+xml")));
060        assertFalse("Should not be able to serialize this!", testProvider
061                .isWriteable(RdfStream.class, null, null, MediaType
062                        .valueOf("application/rdf+xml")));
063        assertFalse("Should not be able to serialize this!", testProvider
064                .isWriteable(RdfStreamProviderTest.class, null, null, MediaType
065                        .valueOf("application/rdf+xml")));
066        assertFalse("Should not be able to serialize this!", testProvider
067                .isWriteable(RdfStream.class, null, null, MediaType
068                        .valueOf("text/html")));
069    }
070
071    @Test
072    public void testWriteTo() throws WebApplicationException, IllegalArgumentException, IOException {
073        final Triple t = create(createURI("info:test"), createURI("property:test"), createURI("info:test"));
074
075        final Map<String, String> namespaces = new HashMap<>();
076        try (final RdfStream rdfStream = new DefaultRdfStream(createURI("info:test"), of(t));
077                final RdfNamespacedStream nsStream = new RdfNamespacedStream(rdfStream, namespaces)) {
078            try (final ByteArrayOutputStream entityStream = new ByteArrayOutputStream()) {
079                testProvider.writeTo(nsStream, RdfNamespacedStream.class, null, null,
080                        MediaType.valueOf("application/rdf+xml"), null, entityStream);
081                final byte[] result = entityStream.toByteArray();
082
083                final Model postSerialization = createDefaultModel().read(new ByteArrayInputStream(result), null);
084                assertTrue("Didn't find our triple!", postSerialization.contains(postSerialization.asStatement(t)));
085            }
086        }
087    }
088
089}