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.client; 019 020import static java.net.URI.create; 021import static org.fcrepo.client.FedoraHeaderConstants.ACCEPT; 022import static org.fcrepo.client.FedoraHeaderConstants.IF_MODIFIED_SINCE; 023import static org.fcrepo.client.FedoraHeaderConstants.IF_NONE_MATCH; 024import static org.fcrepo.client.FedoraHeaderConstants.PREFER; 025import static org.fcrepo.client.FedoraHeaderConstants.RANGE; 026import static org.fcrepo.client.TestUtils.baseUrl; 027import static org.junit.Assert.assertEquals; 028import static org.junit.Assert.assertFalse; 029import static org.mockito.Matchers.any; 030import static org.mockito.Matchers.eq; 031import static org.mockito.Mockito.verify; 032import static org.mockito.Mockito.when; 033 034import java.net.URI; 035import java.util.Arrays; 036import java.util.List; 037 038import org.apache.http.client.methods.HttpRequestBase; 039import org.junit.Before; 040import org.junit.Test; 041import org.junit.runner.RunWith; 042import org.mockito.ArgumentCaptor; 043import org.mockito.Mock; 044import org.mockito.runners.MockitoJUnitRunner; 045 046/** 047 * @author bbpennel 048 */ 049@RunWith(MockitoJUnitRunner.class) 050public class GetBuilderTest { 051 052 @Mock 053 private FcrepoClient client; 054 055 @Mock 056 private FcrepoResponse fcrepoResponse; 057 058 private GetBuilder testBuilder; 059 060 private URI uri; 061 062 @Before 063 public void setUp() throws Exception { 064 when(client.executeRequest(any(URI.class), any(HttpRequestBase.class))) 065 .thenReturn(fcrepoResponse); 066 067 uri = create(baseUrl); 068 testBuilder = new GetBuilder(uri, client); 069 } 070 071 @Test 072 public void testGet() throws Exception { 073 testBuilder.perform(); 074 075 final ArgumentCaptor<HttpRequestBase> requestCaptor = ArgumentCaptor.forClass(HttpRequestBase.class); 076 verify(client).executeRequest(eq(uri), requestCaptor.capture()); 077 078 final HttpRequestBase request = getRequest(); 079 assertEquals(0, request.getAllHeaders().length); 080 } 081 082 @Test 083 public void testPreferMinimal() throws Exception { 084 testBuilder.preferMinimal().perform(); 085 086 final HttpRequestBase request = getRequest(); 087 assertEquals("return=minimal", request.getFirstHeader(PREFER).getValue()); 088 } 089 090 @Test 091 public void testPreferRepresentation() throws Exception { 092 testBuilder.preferRepresentation().perform(); 093 094 final HttpRequestBase request = getRequest(); 095 assertEquals("return=representation", request.getFirstHeader(PREFER).getValue()); 096 } 097 098 @Test 099 public void testPreferInclude() throws Exception { 100 final List<URI> includes = Arrays.asList( 101 new URI("http://fedora.info/definitions/v4/repository#InboundReferences"), 102 new URI("http://www.w3.org/ns/ldp#PreferMembership")); 103 testBuilder.preferRepresentation(includes, null).perform(); 104 105 final HttpRequestBase request = getRequest(); 106 assertEquals("return=representation; include=\"" + 107 "http://fedora.info/definitions/v4/repository#InboundReferences" + 108 " http://www.w3.org/ns/ldp#PreferMembership\"", 109 request.getFirstHeader(PREFER).getValue()); 110 } 111 112 @Test 113 public void testModificationHeaders() throws Exception { 114 final String etag = "123456"; 115 final String lastModified = "Mon, 19 May 2014 19:44:59 GMT"; 116 testBuilder.ifNoneMatch(etag).ifModifiedSince(lastModified).perform(); 117 118 final HttpRequestBase request = getRequest(); 119 assertEquals(etag, request.getFirstHeader(IF_NONE_MATCH).getValue()); 120 assertEquals(lastModified, request.getFirstHeader(IF_MODIFIED_SINCE).getValue()); 121 } 122 123 @Test 124 public void testRange() throws Exception { 125 testBuilder.range(5L, 100L).perform(); 126 127 final HttpRequestBase request = getRequest(); 128 assertEquals("bytes=5-100", request.getFirstHeader(RANGE).getValue()); 129 } 130 131 @Test 132 public void testStartRange() throws Exception { 133 testBuilder.range(5L, null).perform(); 134 135 final HttpRequestBase request = getRequest(); 136 assertEquals("bytes=5-", request.getFirstHeader(RANGE).getValue()); 137 } 138 139 @Test 140 public void testEndRange() throws Exception { 141 testBuilder.range(null, 100L).perform(); 142 143 final HttpRequestBase request = getRequest(); 144 assertEquals("bytes=-100", request.getFirstHeader(RANGE).getValue()); 145 } 146 147 @Test 148 public void testAccept() throws Exception { 149 testBuilder.accept("text/turtle").perform(); 150 151 final HttpRequestBase request = getRequest(); 152 assertEquals("text/turtle", request.getFirstHeader(ACCEPT).getValue()); 153 } 154 155 @Test 156 public void testDisableRedirects() throws Exception { 157 testBuilder.disableRedirects(); 158 assertFalse(testBuilder.request.getConfig().isRedirectsEnabled()); 159 } 160 161 private HttpRequestBase getRequest() throws FcrepoOperationFailedException { 162 final ArgumentCaptor<HttpRequestBase> requestCaptor = ArgumentCaptor.forClass(HttpRequestBase.class); 163 verify(client).executeRequest(eq(uri), requestCaptor.capture()); 164 165 return requestCaptor.getValue(); 166 } 167}