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.preferMinimal().perform();
095
096        final HttpRequestBase request = getRequest();
097        assertEquals("return=minimal", request.getFirstHeader(PREFER).getValue());
098    }
099
100    @Test
101    public void testPreferRepresentation() throws Exception {
102        testBuilder.preferRepresentation().perform();
103
104        final HttpRequestBase request = getRequest();
105        assertEquals("return=representation", request.getFirstHeader(PREFER).getValue());
106    }
107
108    @Test
109    public void testPreferInclude() throws Exception {
110        final List<URI> includes = Arrays.asList(
111                new URI("http://fedora.info/definitions/v4/repository#InboundReferences"),
112                new URI("http://www.w3.org/ns/ldp#PreferMembership"));
113        testBuilder.preferRepresentation(includes, null).perform();
114
115        final HttpRequestBase request = getRequest();
116        assertEquals("return=representation; include=\"" +
117                "http://fedora.info/definitions/v4/repository#InboundReferences" +
118                " http://www.w3.org/ns/ldp#PreferMembership\"",
119                request.getFirstHeader(PREFER).getValue());
120    }
121
122    @Test
123    public void testModificationHeaders() throws Exception {
124        final String etag = "123456";
125        final String lastModified = "Mon, 19 May 2014 19:44:59 GMT";
126        testBuilder.ifNoneMatch(etag).ifModifiedSince(lastModified).perform();
127
128        final HttpRequestBase request = getRequest();
129        assertEquals(etag, request.getFirstHeader(IF_NONE_MATCH).getValue());
130        assertEquals(lastModified, request.getFirstHeader(IF_MODIFIED_SINCE).getValue());
131    }
132
133    @Test
134    public void testRange() throws Exception {
135        testBuilder.range(5L, 100L).perform();
136
137        final HttpRequestBase request = getRequest();
138        assertEquals("bytes=5-100", request.getFirstHeader(RANGE).getValue());
139    }
140
141    @Test
142    public void testStartRange() throws Exception {
143        testBuilder.range(5L, null).perform();
144
145        final HttpRequestBase request = getRequest();
146        assertEquals("bytes=5-", request.getFirstHeader(RANGE).getValue());
147    }
148
149    @Test
150    public void testEndRange() throws Exception {
151        testBuilder.range(null, 100L).perform();
152
153        final HttpRequestBase request = getRequest();
154        assertEquals("bytes=-100", request.getFirstHeader(RANGE).getValue());
155    }
156
157    @Test
158    public void testAccept() throws Exception {
159        testBuilder.accept("text/turtle").perform();
160
161        final HttpRequestBase request = getRequest();
162        assertEquals("text/turtle", request.getFirstHeader(ACCEPT).getValue());
163    }
164
165    @Test
166    public void testDisableRedirects() throws Exception {
167        testBuilder.disableRedirects();
168        assertFalse(testBuilder.request.getConfig().isRedirectsEnabled());
169    }
170
171    @Test
172    public void testWantDigest() throws Exception {
173        testBuilder.wantDigest("md5").perform();
174
175        final HttpRequestBase request = getRequest();
176        assertEquals("md5", request.getFirstHeader(WANT_DIGEST).getValue());
177    }
178
179    @Test
180    public void testNoCache() throws Exception {
181        testBuilder.noCache().perform();
182
183        final HttpRequestBase request = getRequest();
184        assertEquals("no-cache", request.getFirstHeader(CACHE_CONTROL).getValue());
185    }
186
187    @Test
188    public void testAcceptDatetime() throws Exception {
189        testBuilder.acceptDatetime(HISTORIC_DATETIME).perform();
190
191        final HttpRequestBase request = getRequest();
192        assertEquals(HISTORIC_DATETIME, request.getFirstHeader(ACCEPT_DATETIME).getValue());
193    }
194
195    @Test
196    public void testAddHeader() throws Exception {
197        testBuilder.addHeader("my-header", "head-val").perform();
198
199        final HttpRequestBase request = getRequest();
200        assertEquals("head-val", request.getFirstHeader("my-header").getValue());
201    }
202
203    @Test
204    public void testAddLinkHeader() throws Exception {
205        final FcrepoLink link = FcrepoLink.fromUri("http://example.com/link").type("foo").build();
206        testBuilder.addLinkHeader(link).perform();
207
208        final HttpRequestBase request = getRequest();
209        assertEquals(link.toString(), request.getFirstHeader(LINK).getValue());
210    }
211
212    private HttpRequestBase getRequest() throws FcrepoOperationFailedException {
213        final ArgumentCaptor<HttpRequestBase> requestCaptor = ArgumentCaptor.forClass(HttpRequestBase.class);
214        verify(client).executeRequest(eq(uri), requestCaptor.capture());
215
216        return requestCaptor.getValue();
217    }
218}