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.api.responses; 019 020import static org.apache.jena.graph.NodeFactory.createLiteral; 021import static org.apache.jena.graph.NodeFactory.createURI; 022import static org.apache.jena.vocabulary.RDF.type; 023import static java.util.Collections.singletonMap; 024import static java.util.stream.Stream.of; 025import static javax.ws.rs.core.MediaType.TEXT_HTML_TYPE; 026import static javax.ws.rs.core.MediaType.TEXT_PLAIN_TYPE; 027 028import static org.fcrepo.kernel.api.RdfLexicon.FEDORA_BINARY; 029import static org.fcrepo.kernel.api.RdfLexicon.FEDORA_CONTAINER; 030import static org.junit.Assert.assertEquals; 031import static org.junit.Assert.assertFalse; 032import static org.junit.Assert.assertTrue; 033import static org.mockito.ArgumentMatchers.isA; 034import static org.mockito.Mockito.doAnswer; 035import static org.mockito.Mockito.mock; 036import static org.mockito.Mockito.when; 037import static org.springframework.test.util.ReflectionTestUtils.setField; 038 039import java.io.ByteArrayOutputStream; 040import java.io.IOException; 041import java.io.Writer; 042import java.lang.annotation.Annotation; 043import java.lang.reflect.Type; 044import java.net.URI; 045import java.util.stream.Stream; 046 047import javax.ws.rs.WebApplicationException; 048import javax.ws.rs.core.MediaType; 049import javax.ws.rs.core.MultivaluedHashMap; 050import javax.ws.rs.core.UriBuilder; 051import javax.ws.rs.core.UriInfo; 052 053import org.fcrepo.http.commons.responses.HtmlTemplate; 054import org.fcrepo.http.commons.responses.RdfNamespacedStream; 055import org.fcrepo.kernel.api.RdfStream; 056import org.fcrepo.kernel.api.rdf.DefaultRdfStream; 057 058import org.apache.velocity.Template; 059import org.apache.velocity.context.Context; 060import org.junit.Before; 061import org.junit.Ignore; 062import org.junit.Test; 063import org.junit.runner.RunWith; 064import org.mockito.Mock; 065import org.mockito.invocation.InvocationOnMock; 066import org.mockito.junit.MockitoJUnitRunner; 067import org.mockito.stubbing.Answer; 068 069import com.google.common.collect.ImmutableMap; 070import org.apache.jena.graph.Triple; 071 072/** 073 * <p>BaseHtmlProviderTest class.</p> 074 * 075 * @author awoods 076 */ 077@Ignore // TODO fix these tests 078@RunWith(MockitoJUnitRunner.class) 079public class StreamingBaseHtmlProviderTest { 080 081 private final StreamingBaseHtmlProvider testProvider = new StreamingBaseHtmlProvider(); 082 083 private RdfNamespacedStream testData; 084 private RdfNamespacedStream testData2; 085 086 @Mock 087 private UriInfo mockUriInfo; 088 089 @Before 090 public void setup() throws Exception { 091 092 final Stream<Triple> triples = of(new Triple(createURI("test:subject"), createURI("test:predicate"), 093 createLiteral("test:object")), new Triple(createURI("test:subject"), type.asNode(), 094 FEDORA_BINARY.asNode())); 095 final Stream<Triple> triples2 = of(new Triple(createURI("test:subject2"), type.asNode(), 096 FEDORA_CONTAINER.asNode())); 097 @SuppressWarnings("resource") 098 final DefaultRdfStream stream = new DefaultRdfStream(createURI("test:subject"), triples); 099 @SuppressWarnings("resource") 100 final DefaultRdfStream stream2 = new DefaultRdfStream(createURI("test:subject2"), triples2); 101 testData = new RdfNamespacedStream(stream, null); 102 103 testData2 = new RdfNamespacedStream(stream2, null); 104 105 final URI baseUri = URI.create("http://localhost:8080/rest/"); 106 final UriBuilder baseUriBuilder = UriBuilder.fromUri(baseUri); 107 when(mockUriInfo.getBaseUri()).thenReturn(baseUri); 108 when(mockUriInfo.getBaseUriBuilder()).thenReturn(baseUriBuilder); 109 110 setField(testProvider, "uriInfo", mockUriInfo); 111 } 112 113 @Test 114 public void testIsWriteable() { 115 assertTrue( 116 "Gave false response to HtmlProvider.isWriteable() that contained legitimate combination of parameters", 117 testProvider.isWriteable(RdfNamespacedStream.class, RdfNamespacedStream.class, 118 null, TEXT_HTML_TYPE)); 119 assertFalse( 120 "Gave true response to HtmlProvider.isWriteable() with an incorrect combination of parameters", 121 testProvider.isWriteable(RdfStream.class, RdfStream.class, 122 null, TEXT_HTML_TYPE)); 123 assertFalse( 124 "HtmlProvider.isWriteable() should return false if asked to serialize a non-RdfNamespacedStream!", 125 testProvider.isWriteable(StreamingBaseHtmlProvider.class, 126 StreamingBaseHtmlProvider.class, null, TEXT_HTML_TYPE)); 127 assertFalse( 128 "HtmlProvider.isWriteable() should return false to text/plain!", 129 testProvider.isWriteable(RdfNamespacedStream.class, RdfNamespacedStream.class, 130 null, TEXT_PLAIN_TYPE)); 131 } 132 133 @Test 134 public void testGetSize() { 135 assertEquals("Returned wrong size from HtmlProvider!", testProvider 136 .getSize(null, null, null, null, null), -1); 137 138 } 139 140 @Test 141 public void testWriteTo() throws WebApplicationException, 142 IllegalArgumentException, IOException { 143 final Template mockTemplate = mock(Template.class); 144 final ByteArrayOutputStream outStream = new ByteArrayOutputStream(); 145 146 doAnswer(new Answer<Object>() { 147 148 @Override 149 public Object answer(final InvocationOnMock invocation) { 150 outStream.write("abcdefighijk".getBytes(), 0, 10); 151 return "I am pretending to merge a template for you."; 152 } 153 }).when(mockTemplate).merge(isA(Context.class), isA(Writer.class)); 154 setField(testProvider, "templatesMap", singletonMap(FEDORA_BINARY.getURI(), 155 mockTemplate)); 156 testProvider.writeTo(testData, RdfNamespacedStream.class, mock(Type.class), 157 new Annotation[]{}, MediaType.valueOf("text/html"), 158 new MultivaluedHashMap<>(), outStream); 159 final byte[] results = outStream.toByteArray(); 160 assertTrue("Got no output from serialization!", results.length > 0); 161 162 } 163 164 @Test 165 public void testWriteToWithAnnotation() throws WebApplicationException, 166 IllegalArgumentException, IOException { 167 final Template mockTemplate = mock(Template.class); 168 final ByteArrayOutputStream outStream = new ByteArrayOutputStream(); 169 170 doAnswer(new Answer<Object>() { 171 172 @Override 173 public Object answer(final InvocationOnMock invocation) { 174 outStream.write("abcdefighijk".getBytes(), 0, 10); 175 return "I am pretending to merge a template for you."; 176 } 177 }).when(mockTemplate).merge(isA(Context.class), isA(Writer.class)); 178 179 setField(testProvider, "templatesMap", 180 ImmutableMap.of("some:file", mockTemplate)); 181 final HtmlTemplate mockAnnotation = mock(HtmlTemplate.class); 182 when(mockAnnotation.value()).thenReturn("some:file"); 183 testProvider.writeTo(testData, RdfNamespacedStream.class, mock(Type.class), 184 new Annotation[]{mockAnnotation}, MediaType 185 .valueOf("text/html"), 186 new MultivaluedHashMap<>(), outStream); 187 final byte[] results = outStream.toByteArray(); 188 assertTrue("Got no output from serialization!", results.length > 0); 189 190 } 191 192 @Test 193 public void testWriteToWithParentTemplate() throws WebApplicationException, 194 IllegalArgumentException, IOException { 195 final Template mockTemplate = mock(Template.class); 196 final ByteArrayOutputStream outStream = new ByteArrayOutputStream(); 197 198 doAnswer(new Answer<Object>() { 199 200 @Override 201 public Object answer(final InvocationOnMock invocation) { 202 outStream.write("abcdefighijk".getBytes(), 0, 10); 203 return "I am pretending to merge a template for you."; 204 } 205 }).when(mockTemplate).merge(isA(Context.class), isA(Writer.class)); 206 207 setField(testProvider, "templatesMap", 208 ImmutableMap.of(FEDORA_CONTAINER.getURI(), mockTemplate)); 209 testProvider.writeTo(testData2, RdfNamespacedStream.class, mock(Type.class), 210 new Annotation[] {}, MediaType 211 .valueOf("text/html"), 212 new MultivaluedHashMap<>(), outStream); 213 final byte[] results = outStream.toByteArray(); 214 assertTrue("Got no output from serialization!", results.length > 0); 215 } 216}