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