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