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.CONTENT_TYPE;
022import static org.fcrepo.client.FedoraHeaderConstants.IF_STATE_TOKEN;
023import static org.fcrepo.client.FedoraHeaderConstants.LINK;
024import static org.fcrepo.client.TestUtils.baseUrl;
025import static org.junit.Assert.assertEquals;
026import static org.mockito.ArgumentMatchers.any;
027import static org.mockito.ArgumentMatchers.eq;
028import static org.mockito.Mockito.mock;
029import static org.mockito.Mockito.verify;
030import static org.mockito.Mockito.when;
031
032import java.io.InputStream;
033import java.net.URI;
034
035import org.apache.http.HttpEntity;
036import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
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.Captor;
043import org.mockito.Mock;
044import org.mockito.junit.MockitoJUnitRunner;
045
046/**
047 * @author bbpennel
048 */
049@RunWith(MockitoJUnitRunner.class)
050public class PatchBuilderTest {
051
052    @Mock
053    private FcrepoClient client;
054
055    @Mock
056    private FcrepoResponse fcrepoResponse;
057
058    @Captor
059    private ArgumentCaptor<HttpRequestBase> requestCaptor;
060
061    private PatchBuilder testBuilder;
062
063    private URI uri;
064
065    @Before
066    public void setUp() throws Exception {
067        when(client.executeRequest(any(URI.class), any(HttpRequestBase.class)))
068                .thenReturn(fcrepoResponse);
069
070        uri = create(baseUrl);
071        testBuilder = new PatchBuilder(uri, client);
072    }
073
074    @Test
075    public void testWithBodyDefaultType() throws Exception {
076        final InputStream bodyStream = mock(InputStream.class);
077
078        testBuilder.body(bodyStream)
079                .perform();
080
081        verify(client).executeRequest(eq(uri), requestCaptor.capture());
082
083        final HttpEntityEnclosingRequestBase request = (HttpEntityEnclosingRequestBase) requestCaptor.getValue();
084        final HttpEntity bodyEntity = request.getEntity();
085        assertEquals(bodyStream, bodyEntity.getContent());
086
087        assertEquals("application/sparql-update", request.getFirstHeader(CONTENT_TYPE).getValue());
088    }
089
090    @Test
091    public void testWithBodyCustomType() throws Exception {
092        final InputStream bodyStream = mock(InputStream.class);
093
094        testBuilder.body(bodyStream, "text/plain")
095                .perform();
096
097        verify(client).executeRequest(eq(uri), requestCaptor.capture());
098
099        final HttpEntityEnclosingRequestBase request = (HttpEntityEnclosingRequestBase) requestCaptor.getValue();
100        final HttpEntity bodyEntity = request.getEntity();
101        assertEquals(bodyStream, bodyEntity.getContent());
102
103        assertEquals("text/plain", request.getFirstHeader(CONTENT_TYPE).getValue());
104    }
105
106    @Test
107    public void testStateToken() throws Exception {
108        final InputStream bodyStream = mock(InputStream.class);
109        final String token = "state";
110
111        testBuilder.body(bodyStream)
112                .ifStateToken(token)
113                .perform();
114
115        verify(client).executeRequest(eq(uri), requestCaptor.capture());
116
117        final HttpEntityEnclosingRequestBase request = (HttpEntityEnclosingRequestBase) requestCaptor.getValue();
118        assertEquals(token, request.getFirstHeader(IF_STATE_TOKEN).getValue());
119    }
120
121    @Test
122    public void testAddHeader() throws Exception {
123        testBuilder.addHeader("my-header", "head-val").perform();
124
125        verify(client).executeRequest(eq(uri), requestCaptor.capture());
126        final HttpRequestBase request = requestCaptor.getValue();
127        assertEquals("head-val", request.getFirstHeader("my-header").getValue());
128    }
129
130    @Test
131    public void testAddLinkHeader() throws Exception {
132        final FcrepoLink link = FcrepoLink.fromUri("http://example.com/link").type("foo").build();
133        testBuilder.addLinkHeader(link).perform();
134
135        verify(client).executeRequest(eq(uri), requestCaptor.capture());
136        final HttpRequestBase request = requestCaptor.getValue();
137        assertEquals(link.toString(), request.getFirstHeader(LINK).getValue());
138    }
139}