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