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