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