001/*
002 * The contents of this file are subject to the license and copyright
003 * detailed in the LICENSE and NOTICE files at the root of the source
004 * tree.
005 */
006package org.fcrepo.client;
007
008import static java.net.URI.create;
009import static org.fcrepo.client.ExternalContentHandling.PROXY;
010import static org.fcrepo.client.FedoraHeaderConstants.CONTENT_DISPOSITION;
011import static org.fcrepo.client.FedoraHeaderConstants.CONTENT_TYPE;
012import static org.fcrepo.client.FedoraHeaderConstants.DIGEST;
013import static org.fcrepo.client.FedoraHeaderConstants.LINK;
014import static org.fcrepo.client.FedoraHeaderConstants.SLUG;
015import static org.fcrepo.client.FedoraTypes.LDP_DIRECT_CONTAINER;
016import static org.fcrepo.client.LinkHeaderConstants.ACL_REL;
017import static org.fcrepo.client.LinkHeaderConstants.EXTERNAL_CONTENT_HANDLING;
018import static org.fcrepo.client.LinkHeaderConstants.EXTERNAL_CONTENT_REL;
019import static org.fcrepo.client.LinkHeaderConstants.TYPE_REL;
020import static org.fcrepo.client.TestUtils.baseUrl;
021import static org.junit.Assert.assertEquals;
022import static org.junit.Assert.assertNull;
023import static org.mockito.ArgumentMatchers.any;
024import static org.mockito.ArgumentMatchers.eq;
025import static org.mockito.Mockito.mock;
026import static org.mockito.Mockito.verify;
027import static org.mockito.Mockito.when;
028
029import java.io.InputStream;
030import java.net.URI;
031
032import org.apache.http.HttpEntity;
033import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
034import org.apache.http.client.methods.HttpRequestBase;
035import org.junit.Before;
036import org.junit.Test;
037import org.junit.runner.RunWith;
038import org.mockito.ArgumentCaptor;
039import org.mockito.Captor;
040import org.mockito.Mock;
041import org.mockito.junit.MockitoJUnitRunner;
042
043/**
044 * @author bbpennel
045 */
046@RunWith(MockitoJUnitRunner.class)
047public class PostBuilderTest {
048
049    @Mock
050    private FcrepoClient client;
051
052    @Mock
053    private FcrepoResponse fcrepoResponse;
054
055    @Captor
056    private ArgumentCaptor<HttpRequestBase> requestCaptor;
057
058    private PostBuilder 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 PostBuilder(uri, client);
069    }
070
071    @Test
072    public void testPostNoBody() throws Exception {
073        testBuilder.perform();
074
075        verify(client).executeRequest(eq(uri), requestCaptor.capture());
076
077        final HttpEntityEnclosingRequestBase request = (HttpEntityEnclosingRequestBase) requestCaptor.getValue();
078        assertNull("Request body should not be set", request.getEntity());
079        assertEquals(0, request.getAllHeaders().length);
080    }
081
082    @Test
083    public void testWithBody() throws Exception {
084        final InputStream bodyStream = mock(InputStream.class);
085
086        testBuilder.body(bodyStream, "plain/text")
087                .digestSha1("checksum")
088                .filename("file.txt")
089                .slug("slug_value")
090                .perform();
091
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("sha=checksum", request.getFirstHeader(DIGEST).getValue());
100        assertEquals("slug_value", request.getFirstHeader(SLUG).getValue());
101        assertEquals("attachment; filename=\"file.txt\"", request.getFirstHeader(CONTENT_DISPOSITION).getValue());
102    }
103
104    @Test
105    public void testAttachment() throws Exception {
106        final InputStream bodyStream = mock(InputStream.class);
107        testBuilder.body(bodyStream, "plain/text").filename(null).perform();
108
109        verify(client).executeRequest(eq(uri), requestCaptor.capture());
110
111        final HttpEntityEnclosingRequestBase request = (HttpEntityEnclosingRequestBase) requestCaptor.getValue();
112        final HttpEntity bodyEntity = request.getEntity();
113        assertEquals(bodyStream, bodyEntity.getContent());
114        assertEquals("attachment", request.getFirstHeader(CONTENT_DISPOSITION).getValue());
115    }
116
117    @Test
118    public void testWithBodyMultipleChecksums() throws Exception {
119        final InputStream bodyStream = mock(InputStream.class);
120
121        testBuilder.body(bodyStream, "plain/text")
122                .digestSha1("checksum")
123                .digestSha256("checksum256")
124                .perform();
125
126        verify(client).executeRequest(eq(uri), requestCaptor.capture());
127
128        final HttpEntityEnclosingRequestBase request = (HttpEntityEnclosingRequestBase) requestCaptor.getValue();
129        final HttpEntity bodyEntity = request.getEntity();
130        assertEquals(bodyStream, bodyEntity.getContent());
131
132        assertEquals("plain/text", request.getFirstHeader(CONTENT_TYPE).getValue());
133        assertEquals("sha=checksum, sha256=checksum256", request.getFirstHeader(DIGEST).getValue());
134    }
135
136    @Test
137    public void testBodyNoType() throws Exception {
138        final InputStream bodyStream = mock(InputStream.class);
139
140        testBuilder.body(bodyStream).perform();
141
142        verify(client).executeRequest(eq(uri), requestCaptor.capture());
143
144        final HttpEntityEnclosingRequestBase request = (HttpEntityEnclosingRequestBase) requestCaptor.getValue();
145        final HttpEntity bodyEntity = request.getEntity();
146        assertEquals(bodyStream, bodyEntity.getContent());
147        assertEquals("application/octet-stream", request.getFirstHeader(CONTENT_TYPE).getValue());
148    }
149
150    @Test(expected = FcrepoOperationFailedException.class)
151    public void testPostClientError() throws Exception {
152        when(client.executeRequest(any(URI.class), any(HttpRequestBase.class)))
153                .thenThrow(new FcrepoOperationFailedException(uri, 415, "status"));
154
155        testBuilder.perform();
156    }
157
158    @Test
159    public void testExternalContent() throws Exception {
160        final URI contentURI = URI.create("file:///path/to/file");
161        testBuilder.externalContent(contentURI, "plain/text", PROXY)
162                .perform();
163
164        verify(client).executeRequest(eq(uri), requestCaptor.capture());
165
166        final HttpEntityEnclosingRequestBase request = (HttpEntityEnclosingRequestBase) requestCaptor.getValue();
167
168        final FcrepoLink extLink = new FcrepoLink(request.getFirstHeader(LINK).getValue());
169        assertEquals(EXTERNAL_CONTENT_REL, extLink.getRel());
170        assertEquals(PROXY, extLink.getParams().get(EXTERNAL_CONTENT_HANDLING));
171        assertEquals("plain/text", extLink.getType());
172    }
173
174    @Test
175    public void testExternalContentNoType() throws Exception {
176        final URI contentURI = URI.create("file:///path/to/file");
177        testBuilder.externalContent(contentURI, null, PROXY)
178                .perform();
179
180        verify(client).executeRequest(eq(uri), requestCaptor.capture());
181
182        final HttpEntityEnclosingRequestBase request = (HttpEntityEnclosingRequestBase) requestCaptor.getValue();
183
184        final FcrepoLink extLink = new FcrepoLink(request.getFirstHeader(LINK).getValue());
185        assertEquals(EXTERNAL_CONTENT_REL, extLink.getRel());
186        assertEquals(PROXY, extLink.getParams().get(EXTERNAL_CONTENT_HANDLING));
187        assertNull(extLink.getType());
188    }
189
190    @Test
191    public void testAddInteractionModel() throws Exception {
192        testBuilder.addInteractionModel(LDP_DIRECT_CONTAINER)
193                .perform();
194
195        verify(client).executeRequest(eq(uri), requestCaptor.capture());
196
197        final HttpEntityEnclosingRequestBase request = (HttpEntityEnclosingRequestBase) requestCaptor.getValue();
198
199        final FcrepoLink interLink = new FcrepoLink(request.getFirstHeader(LINK).getValue());
200        assertEquals(TYPE_REL, interLink.getRel());
201        assertEquals(LDP_DIRECT_CONTAINER, interLink.getUri().toString());
202    }
203
204    @Test
205    public void testLinkAcl() throws Exception {
206        testBuilder.linkAcl("http://localhost/acl")
207                .perform();
208
209        verify(client).executeRequest(eq(uri), requestCaptor.capture());
210
211        final HttpEntityEnclosingRequestBase request = (HttpEntityEnclosingRequestBase) requestCaptor.getValue();
212
213        final FcrepoLink aclLink = new FcrepoLink(request.getFirstHeader(LINK).getValue());
214        assertEquals(ACL_REL, aclLink.getRel());
215        assertEquals("http://localhost/acl", aclLink.getUri().toString());
216    }
217
218    @Test
219    public void testAddHeader() throws Exception {
220        testBuilder.addHeader("my-header", "head-val").perform();
221
222        verify(client).executeRequest(eq(uri), requestCaptor.capture());
223        final HttpRequestBase request = requestCaptor.getValue();
224        assertEquals("head-val", request.getFirstHeader("my-header").getValue());
225    }
226
227    @Test
228    public void testAddLinkHeader() throws Exception {
229        final FcrepoLink link = FcrepoLink.fromUri("http://example.com/link").type("foo").build();
230        testBuilder.addLinkHeader(link).perform();
231
232        verify(client).executeRequest(eq(uri), requestCaptor.capture());
233        final HttpRequestBase request = requestCaptor.getValue();
234        assertEquals(link.toString(), request.getFirstHeader(LINK).getValue());
235    }
236}