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; 019 020import static org.fcrepo.kernel.api.RdfLexicon.EXTERNAL_CONTENT; 021import static org.junit.Assert.assertEquals; 022import static org.mockito.ArgumentMatchers.anyString; 023import static org.mockito.Mockito.doThrow; 024 025import java.util.Arrays; 026import java.util.List; 027import java.util.stream.Collectors; 028 029import javax.ws.rs.core.Link; 030import org.fcrepo.kernel.api.exception.ExternalMessageBodyException; 031import org.junit.Before; 032import org.junit.Test; 033import org.junit.runner.RunWith; 034import org.mockito.Mock; 035import org.mockito.junit.MockitoJUnitRunner; 036 037/** 038 * @author bbpennel 039 */ 040@RunWith(MockitoJUnitRunner.class) 041public class ExternalContentHandlerFactoryTest { 042 043 @Mock 044 private ExternalContentPathValidator validator; 045 046 private ExternalContentHandlerFactory factory; 047 048 @Before 049 public void init() { 050 factory = new ExternalContentHandlerFactory(); 051 factory.setValidator(validator); 052 } 053 054 @Test 055 public void testValidLinkHeader() { 056 final ExternalContentHandler handler = factory.createFromLinks( 057 makeLinks("http://test.com")); 058 059 assertEquals("http://test.com", handler.getURL()); 060 assertEquals("text/plain", handler.getContentType()); 061 assertEquals("proxy", handler.getHandling()); 062 } 063 064 @Test(expected = ExternalMessageBodyException.class) 065 public void testValidationFailure() { 066 doThrow(new ExternalMessageBodyException("")).when(validator).validate(anyString()); 067 068 factory.createFromLinks(makeLinks("http://test.com")); 069 } 070 071 @Test(expected = ExternalMessageBodyException.class) 072 public void testMultipleExtLinkHeaders() { 073 final List<String> links = makeLinks("http://test.com", "http://test2.com"); 074 075 factory.createFromLinks(links); 076 } 077 078 private List<String> makeLinks(final String... uris) { 079 return Arrays.stream(uris) 080 .map(uri -> Link.fromUri(uri) 081 .rel(EXTERNAL_CONTENT.toString()) 082 .param("handling", "proxy") 083 .type("text/plain") 084 .build() 085 .toString()) 086 .collect(Collectors.toList()); 087 } 088}