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;
027import static org.mockito.Mockito.when;
028
029import java.io.ByteArrayInputStream;
030import java.io.ByteArrayOutputStream;
031import java.io.IOException;
032import java.util.HashMap;
033import java.util.Map;
034
035import javax.jcr.NamespaceRegistry;
036import javax.jcr.RepositoryException;
037import javax.jcr.Session;
038import javax.jcr.Workspace;
039import javax.ws.rs.WebApplicationException;
040import javax.ws.rs.core.MediaType;
041
042import org.fcrepo.kernel.api.RdfStream;
043import org.fcrepo.kernel.api.rdf.DefaultRdfStream;
044import org.junit.Before;
045import org.junit.Test;
046import org.mockito.Mock;
047import org.apache.jena.graph.Triple;
048import org.apache.jena.rdf.model.Model;
049
050import org.junit.runner.RunWith;
051import org.mockito.runners.MockitoJUnitRunner;
052
053/**
054 * <p>RdfStreamProviderTest class.</p>
055 *
056 * @author ajs6f
057 */
058@RunWith(MockitoJUnitRunner.class)
059public class RdfStreamProviderTest {
060
061    private final RdfStreamProvider testProvider = new RdfStreamProvider();
062
063    @Mock
064    private Session mockSession;
065
066    @Mock
067    private Workspace mockWorkspace;
068
069    @Mock
070    private NamespaceRegistry mockNamespaceRegistry;
071
072    @Before
073    public void setUp() throws RepositoryException {
074        when(mockSession.getWorkspace()).thenReturn(mockWorkspace);
075        when(mockWorkspace.getNamespaceRegistry()).thenReturn(
076                mockNamespaceRegistry);
077        when(mockNamespaceRegistry.getPrefixes()).thenReturn(new String[] {});
078    }
079
080    @Test
081    public void testGetSize() {
082        assertEquals(-1, testProvider.getSize(null, null, null, null, null));
083    }
084
085    @Test
086    public void testIsWriteable() {
087        assertTrue("Should be able to serialize this!", testProvider
088                .isWriteable(RdfNamespacedStream.class, null, null, MediaType
089                        .valueOf("application/rdf+xml")));
090        assertFalse("Should not be able to serialize this!", testProvider
091                .isWriteable(RdfStream.class, null, null, MediaType
092                        .valueOf("application/rdf+xml")));
093        assertFalse("Should not be able to serialize this!", testProvider
094                .isWriteable(RdfStreamProviderTest.class, null, null, MediaType
095                        .valueOf("application/rdf+xml")));
096        assertFalse("Should not be able to serialize this!", testProvider
097                .isWriteable(RdfStream.class, null, null, MediaType
098                        .valueOf("text/html")));
099    }
100
101    @Test
102    public void testWriteTo() throws WebApplicationException, IllegalArgumentException, IOException {
103        final Triple t = create(createURI("info:test"), createURI("property:test"), createURI("info:test"));
104
105        final Map<String, String> namespaces = new HashMap<>();
106        try (final RdfStream rdfStream = new DefaultRdfStream(createURI("info:test"), of(t));
107                final RdfNamespacedStream nsStream = new RdfNamespacedStream(rdfStream, namespaces)) {
108            try (ByteArrayOutputStream entityStream = new ByteArrayOutputStream();) {
109                testProvider.writeTo(nsStream, RdfNamespacedStream.class, null, null,
110                        MediaType.valueOf("application/rdf+xml"), null, entityStream);
111                final byte[] result = entityStream.toByteArray();
112
113                final Model postSerialization = createDefaultModel().read(new ByteArrayInputStream(result), null);
114                assertTrue("Didn't find our triple!", postSerialization.contains(postSerialization.asStatement(t)));
115            }
116        }
117    }
118
119}