001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.fcrepo.http.commons.responses;
019
020import static java.util.stream.Stream.of;
021import static org.apache.jena.graph.NodeFactory.createURI;
022import static org.apache.jena.graph.Triple.create;
023import static org.apache.jena.rdf.model.ModelFactory.createDefaultModel;
024import static org.junit.Assert.assertEquals;
025import static org.junit.Assert.assertFalse;
026import static org.junit.Assert.assertTrue;
027
028import java.io.ByteArrayInputStream;
029import java.io.ByteArrayOutputStream;
030import java.io.IOException;
031import java.util.HashMap;
032import java.util.Map;
033
034import javax.ws.rs.WebApplicationException;
035import javax.ws.rs.core.MediaType;
036
037import org.fcrepo.kernel.api.RdfStream;
038import org.fcrepo.kernel.api.rdf.DefaultRdfStream;
039import org.junit.Before;
040import org.junit.Test;
041import org.apache.jena.graph.Triple;
042import org.apache.jena.rdf.model.Model;
043
044import org.junit.runner.RunWith;
045import org.mockito.junit.MockitoJUnitRunner;
046
047/**
048 * <p>RdfStreamProviderTest class.</p>
049 *
050 * @author ajs6f
051 */
052@RunWith(MockitoJUnitRunner.Silent.class)
053public class RdfStreamProviderTest {
054
055    private final RdfStreamProvider testProvider = new RdfStreamProvider();
056
057
058    @Before
059    public void setUp() throws Exception {
060    }
061
062    @Test
063    public void testGetSize() {
064        assertEquals(-1, testProvider.getSize(null, null, null, null, null));
065    }
066
067    @Test
068    public void testIsWriteable() {
069        assertTrue("Should be able to serialize this!", testProvider
070                .isWriteable(RdfNamespacedStream.class, null, null, MediaType
071                        .valueOf("application/rdf+xml")));
072        assertFalse("Should not be able to serialize this!", testProvider
073                .isWriteable(RdfStream.class, null, null, MediaType
074                        .valueOf("application/rdf+xml")));
075        assertFalse("Should not be able to serialize this!", testProvider
076                .isWriteable(RdfStreamProviderTest.class, null, null, MediaType
077                        .valueOf("application/rdf+xml")));
078        assertFalse("Should not be able to serialize this!", testProvider
079                .isWriteable(RdfStream.class, null, null, MediaType
080                        .valueOf("text/html")));
081    }
082
083    @Test
084    public void testWriteTo() throws WebApplicationException, IllegalArgumentException, IOException {
085        final Triple t = create(createURI("info:test"), createURI("property:test"), createURI("info:test"));
086
087        final Map<String, String> namespaces = new HashMap<>();
088        try (final RdfStream rdfStream = new DefaultRdfStream(createURI("info:test"), of(t));
089                final RdfNamespacedStream nsStream = new RdfNamespacedStream(rdfStream, namespaces)) {
090            try (final ByteArrayOutputStream entityStream = new ByteArrayOutputStream()) {
091                testProvider.writeTo(nsStream, RdfNamespacedStream.class, null, null,
092                        MediaType.valueOf("application/rdf+xml"), null, entityStream);
093                final byte[] result = entityStream.toByteArray();
094
095                final Model postSerialization = createDefaultModel().read(new ByteArrayInputStream(result), null);
096                assertTrue("Didn't find our triple!", postSerialization.contains(postSerialization.asStatement(t)));
097            }
098        }
099    }
100
101}