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.ACCEPT_DATETIME; 023import static org.fcrepo.client.FedoraHeaderConstants.CACHE_CONTROL; 024import static org.fcrepo.client.FedoraHeaderConstants.IF_MODIFIED_SINCE; 025import static org.fcrepo.client.FedoraHeaderConstants.IF_NONE_MATCH; 026import static org.fcrepo.client.FedoraHeaderConstants.LINK; 027import static org.fcrepo.client.FedoraHeaderConstants.PREFER; 028import static org.fcrepo.client.FedoraHeaderConstants.RANGE; 029import static org.fcrepo.client.FedoraHeaderConstants.WANT_DIGEST; 030import static org.fcrepo.client.HeaderHelpers.UTC_RFC_1123_FORMATTER; 031import static org.fcrepo.client.TestUtils.baseUrl; 032import static org.junit.Assert.assertEquals; 033import static org.junit.Assert.assertFalse; 034import static org.mockito.ArgumentMatchers.any; 035import static org.mockito.ArgumentMatchers.eq; 036import static org.mockito.Mockito.verify; 037import static org.mockito.Mockito.when; 038 039import java.net.URI; 040import java.time.LocalDateTime; 041import java.time.ZoneOffset; 042import java.util.Arrays; 043import java.util.List; 044 045import org.apache.http.client.methods.HttpRequestBase; 046import org.junit.Before; 047import org.junit.Test; 048import org.junit.runner.RunWith; 049import org.mockito.ArgumentCaptor; 050import org.mockito.Mock; 051import org.mockito.junit.MockitoJUnitRunner; 052 053/** 054 * @author bbpennel 055 */ 056@RunWith(MockitoJUnitRunner.class) 057public class GetBuilderTest { 058 059 private final String HISTORIC_DATETIME = 060 UTC_RFC_1123_FORMATTER.format(LocalDateTime.of(2000, 1, 1, 0, 0).atZone(ZoneOffset.UTC)); 061 062 @Mock 063 private FcrepoClient client; 064 065 @Mock 066 private FcrepoResponse fcrepoResponse; 067 068 private GetBuilder testBuilder; 069 070 private URI uri; 071 072 @Before 073 public void setUp() throws Exception { 074 when(client.executeRequest(any(URI.class), any(HttpRequestBase.class))) 075 .thenReturn(fcrepoResponse); 076 077 uri = create(baseUrl); 078 testBuilder = new GetBuilder(uri, client); 079 } 080 081 @Test 082 public void testGet() throws Exception { 083 testBuilder.perform(); 084 085 final ArgumentCaptor<HttpRequestBase> requestCaptor = ArgumentCaptor.forClass(HttpRequestBase.class); 086 verify(client).executeRequest(eq(uri), requestCaptor.capture()); 087 088 final HttpRequestBase request = getRequest(); 089 assertEquals(0, request.getAllHeaders().length); 090 } 091 092 @Test 093 public void testPreferMinimal() throws Exception { 094 testBuilder.preferRepresentation(Arrays.asList( 095 URI.create("http://www.w3.org/ns/ldp#PreferMinimalContainer")), null) 096 .perform(); 097 098 final HttpRequestBase request = getRequest(); 099 assertEquals("return=representation; include=\"http://www.w3.org/ns/ldp#PreferMinimalContainer\"", 100 request.getFirstHeader(PREFER).getValue()); 101 } 102 103 @Test 104 public void testPreferRepresentation() throws Exception { 105 testBuilder.preferRepresentation().perform(); 106 107 final HttpRequestBase request = getRequest(); 108 assertEquals("return=representation", request.getFirstHeader(PREFER).getValue()); 109 } 110 111 @Test 112 public void testPreferInclude() throws Exception { 113 final List<URI> includes = Arrays.asList( 114 new URI("http://fedora.info/definitions/v4/repository#InboundReferences"), 115 new URI("http://www.w3.org/ns/ldp#PreferMembership")); 116 testBuilder.preferRepresentation(includes, null).perform(); 117 118 final HttpRequestBase request = getRequest(); 119 assertEquals("return=representation; include=\"" + 120 "http://fedora.info/definitions/v4/repository#InboundReferences" + 121 " http://www.w3.org/ns/ldp#PreferMembership\"", 122 request.getFirstHeader(PREFER).getValue()); 123 } 124 125 @Test 126 public void testModificationHeaders() throws Exception { 127 final String etag = "123456"; 128 final String lastModified = "Mon, 19 May 2014 19:44:59 GMT"; 129 testBuilder.ifNoneMatch(etag).ifModifiedSince(lastModified).perform(); 130 131 final HttpRequestBase request = getRequest(); 132 assertEquals(etag, request.getFirstHeader(IF_NONE_MATCH).getValue()); 133 assertEquals(lastModified, request.getFirstHeader(IF_MODIFIED_SINCE).getValue()); 134 } 135 136 @Test 137 public void testRange() throws Exception { 138 testBuilder.range(5L, 100L).perform(); 139 140 final HttpRequestBase request = getRequest(); 141 assertEquals("bytes=5-100", request.getFirstHeader(RANGE).getValue()); 142 } 143 144 @Test 145 public void testStartRange() throws Exception { 146 testBuilder.range(5L, null).perform(); 147 148 final HttpRequestBase request = getRequest(); 149 assertEquals("bytes=5-", request.getFirstHeader(RANGE).getValue()); 150 } 151 152 @Test 153 public void testEndRange() throws Exception { 154 testBuilder.range(null, 100L).perform(); 155 156 final HttpRequestBase request = getRequest(); 157 assertEquals("bytes=-100", request.getFirstHeader(RANGE).getValue()); 158 } 159 160 @Test 161 public void testAccept() throws Exception { 162 testBuilder.accept("text/turtle").perform(); 163 164 final HttpRequestBase request = getRequest(); 165 assertEquals("text/turtle", request.getFirstHeader(ACCEPT).getValue()); 166 } 167 168 @Test 169 public void testDisableRedirects() throws Exception { 170 testBuilder.disableRedirects(); 171 assertFalse(testBuilder.request.getConfig().isRedirectsEnabled()); 172 } 173 174 @Test 175 public void testWantDigest() throws Exception { 176 testBuilder.wantDigest("md5").perform(); 177 178 final HttpRequestBase request = getRequest(); 179 assertEquals("md5", request.getFirstHeader(WANT_DIGEST).getValue()); 180 } 181 182 @Test 183 public void testNoCache() throws Exception { 184 testBuilder.noCache().perform(); 185 186 final HttpRequestBase request = getRequest(); 187 assertEquals("no-cache", request.getFirstHeader(CACHE_CONTROL).getValue()); 188 } 189 190 @Test 191 public void testAcceptDatetime() throws Exception { 192 testBuilder.acceptDatetime(HISTORIC_DATETIME).perform(); 193 194 final HttpRequestBase request = getRequest(); 195 assertEquals(HISTORIC_DATETIME, request.getFirstHeader(ACCEPT_DATETIME).getValue()); 196 } 197 198 @Test 199 public void testAddHeader() throws Exception { 200 testBuilder.addHeader("my-header", "head-val").perform(); 201 202 final HttpRequestBase request = getRequest(); 203 assertEquals("head-val", request.getFirstHeader("my-header").getValue()); 204 } 205 206 @Test 207 public void testAddLinkHeader() throws Exception { 208 final FcrepoLink link = FcrepoLink.fromUri("http://example.com/link").type("foo").build(); 209 testBuilder.addLinkHeader(link).perform(); 210 211 final HttpRequestBase request = getRequest(); 212 assertEquals(link.toString(), request.getFirstHeader(LINK).getValue()); 213 } 214 215 private HttpRequestBase getRequest() throws FcrepoOperationFailedException { 216 final ArgumentCaptor<HttpRequestBase> requestCaptor = ArgumentCaptor.forClass(HttpRequestBase.class); 217 verify(client).executeRequest(eq(uri), requestCaptor.capture()); 218 219 return requestCaptor.getValue(); 220 } 221}