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.CONTENT_TYPE;
021import static org.fcrepo.client.FedoraHeaderConstants.DIGEST;
022import static org.fcrepo.client.FedoraHeaderConstants.IF_MATCH;
023import static org.fcrepo.client.FedoraHeaderConstants.IF_UNMODIFIED_SINCE;
024import static org.fcrepo.client.FedoraHeaderConstants.PREFER;
025import static org.fcrepo.client.TestUtils.baseUrl;
026import static org.junit.Assert.assertEquals;
027import static org.junit.Assert.assertNull;
028import static org.mockito.Matchers.any;
029import static org.mockito.Matchers.eq;
030import static org.mockito.Mockito.mock;
031import static org.mockito.Mockito.verify;
032import static org.mockito.Mockito.when;
033
034import java.io.InputStream;
035import java.net.URI;
036
037import org.apache.http.HttpEntity;
038import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
039import org.apache.http.client.methods.HttpRequestBase;
040import org.junit.Before;
041import org.junit.Test;
042import org.junit.runner.RunWith;
043import org.mockito.ArgumentCaptor;
044import org.mockito.Mock;
045import org.mockito.runners.MockitoJUnitRunner;
046
047/**
048 * @author bbpennel
049 */
050@RunWith(MockitoJUnitRunner.class)
051public class PutBuilderTest {
052
053    @Mock
054    private FcrepoClient client;
055
056    @Mock
057    private FcrepoResponse fcrepoResponse;
058
059    private PutBuilder testBuilder;
060
061    private URI uri;
062
063    @Before
064    public void setUp() throws Exception {
065        when(client.executeRequest(any(URI.class), any(HttpRequestBase.class)))
066                .thenReturn(fcrepoResponse);
067
068        uri = create(baseUrl);
069        testBuilder = new PutBuilder(uri, client);
070    }
071
072    @Test
073    public void testPutNoBody() throws Exception {
074        testBuilder.perform();
075
076        final ArgumentCaptor<HttpRequestBase> requestCaptor = ArgumentCaptor.forClass(HttpRequestBase.class);
077        verify(client).executeRequest(eq(uri), requestCaptor.capture());
078
079        final HttpEntityEnclosingRequestBase request = (HttpEntityEnclosingRequestBase) requestCaptor.getValue();
080        assertNull("Request body should not be set", request.getEntity());
081        assertEquals(0, request.getAllHeaders().length);
082    }
083
084    @Test
085    public void testWithBody() throws Exception {
086        final InputStream bodyStream = mock(InputStream.class);
087
088        testBuilder.body(bodyStream, "plain/text")
089                .digest("checksum")
090                .perform();
091
092        final ArgumentCaptor<HttpRequestBase> requestCaptor = ArgumentCaptor.forClass(HttpRequestBase.class);
093        verify(client).executeRequest(eq(uri), requestCaptor.capture());
094
095        final HttpEntityEnclosingRequestBase request = (HttpEntityEnclosingRequestBase) requestCaptor.getValue();
096        final HttpEntity bodyEntity = request.getEntity();
097        assertEquals(bodyStream, bodyEntity.getContent());
098
099        assertEquals("plain/text", request.getFirstHeader(CONTENT_TYPE).getValue());
100        assertEquals("sha1=checksum", request.getFirstHeader(DIGEST).getValue());
101    }
102
103    @Test(expected = FcrepoOperationFailedException.class)
104    public void testPostClientError() throws Exception {
105        when(client.executeRequest(any(URI.class), any(HttpRequestBase.class)))
106                .thenThrow(new FcrepoOperationFailedException(uri, 415, "status"));
107
108        testBuilder.perform();
109    }
110
111    @Test
112    public void testWithModificationHeaders() throws Exception {
113        final InputStream bodyStream = mock(InputStream.class);
114
115        final String etag = "123456";
116        final String lastModified = "Mon, 19 May 2014 19:44:59 GMT";
117        testBuilder.body(bodyStream, "plain/text")
118                .ifMatch(etag)
119                .ifUnmodifiedSince(lastModified)
120                .perform();
121
122        final ArgumentCaptor<HttpRequestBase> requestCaptor = ArgumentCaptor.forClass(HttpRequestBase.class);
123        verify(client).executeRequest(eq(uri), requestCaptor.capture());
124
125        final HttpEntityEnclosingRequestBase request = (HttpEntityEnclosingRequestBase) requestCaptor.getValue();
126        final HttpEntity bodyEntity = request.getEntity();
127        assertEquals(bodyStream, bodyEntity.getContent());
128
129        assertEquals("plain/text", request.getFirstHeader(CONTENT_TYPE).getValue());
130        assertEquals(etag, request.getFirstHeader(IF_MATCH).getValue());
131        assertEquals(lastModified, request.getFirstHeader(IF_UNMODIFIED_SINCE).getValue());
132    }
133
134    @Test
135    public void testPreferLenient() throws Exception {
136        testBuilder.preferLenient().perform();
137
138        final ArgumentCaptor<HttpRequestBase> requestCaptor = ArgumentCaptor.forClass(HttpRequestBase.class);
139        verify(client).executeRequest(eq(uri), requestCaptor.capture());
140
141        final HttpEntityEnclosingRequestBase request = (HttpEntityEnclosingRequestBase) requestCaptor.getValue();
142        assertEquals("handling=lenient; received=\"minimal\"", request.getFirstHeader(PREFER).getValue());
143    }
144}