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