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.domain; 019 020import static org.junit.Assert.assertEquals; 021import static org.mockito.Mockito.when; 022import static org.mockito.MockitoAnnotations.initMocks; 023 024import java.io.InputStream; 025import java.net.URI; 026 027import org.fcrepo.kernel.api.services.ExternalContentService; 028import org.junit.Before; 029import org.junit.Test; 030import org.mockito.Mock; 031 032import javax.ws.rs.core.MultivaluedHashMap; 033 034/** 035 * @author cabeer 036 */ 037public class ContentLocationMessageBodyReaderTest { 038 039 040 private ContentLocationMessageBodyReader testObj; 041 042 @Mock 043 private InputStream mockInputStream; 044 045 @Mock 046 private ExternalContentService mockContentService; 047 048 @Before 049 public void setUp() { 050 initMocks(this); 051 testObj = new ContentLocationMessageBodyReader(); 052 testObj.setContentService(mockContentService); 053 } 054 055 @Test 056 public void testReadFromURI() throws Exception { 057 final MultivaluedHashMap<String, String> headers = new MultivaluedHashMap<>(); 058 headers.putSingle("Content-Location", "http://localhost:8080/xyz"); 059 when(mockContentService.retrieveExternalContent(new URI("http://localhost:8080/xyz"))) 060 .thenReturn(mockInputStream); 061 try (final InputStream actual = testObj.readFrom(InputStream.class, null, null, null, headers, null)) { 062 assertEquals(mockInputStream, actual); 063 } 064 } 065 066 @Test 067 public void testReadFromRequestBody() throws Exception { 068 final MultivaluedHashMap<String, String> headers = new MultivaluedHashMap<>(); 069 try (final InputStream actual = 070 testObj.readFrom(InputStream.class, null, null, null, headers, mockInputStream)) { 071 assertEquals(mockInputStream, actual); 072 } 073 074 } 075}