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