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_DISPOSITION; 021import static org.fcrepo.client.FedoraHeaderConstants.CONTENT_TYPE; 022import static org.fcrepo.client.FedoraHeaderConstants.DIGEST; 023import static org.fcrepo.client.FedoraHeaderConstants.SLUG; 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 PostBuilderTest { 051 052 @Mock 053 private FcrepoClient client; 054 055 @Mock 056 private FcrepoResponse fcrepoResponse; 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 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 .filename("file.txt") 090 .slug("slug_value") 091 .perform(); 092 093 final ArgumentCaptor<HttpRequestBase> requestCaptor = ArgumentCaptor.forClass(HttpRequestBase.class); 094 verify(client).executeRequest(eq(uri), requestCaptor.capture()); 095 096 final HttpEntityEnclosingRequestBase request = (HttpEntityEnclosingRequestBase) requestCaptor.getValue(); 097 final HttpEntity bodyEntity = request.getEntity(); 098 assertEquals(bodyStream, bodyEntity.getContent()); 099 100 assertEquals("plain/text", request.getFirstHeader(CONTENT_TYPE).getValue()); 101 assertEquals("sha1=checksum", request.getFirstHeader(DIGEST).getValue()); 102 assertEquals("slug_value", request.getFirstHeader(SLUG).getValue()); 103 assertEquals("attachment; filename=\"file.txt\"", request.getFirstHeader(CONTENT_DISPOSITION).getValue()); 104 } 105 106 @Test 107 public void testBodyNoType() throws Exception { 108 final InputStream bodyStream = mock(InputStream.class); 109 110 testBuilder.body(bodyStream).perform(); 111 112 final ArgumentCaptor<HttpRequestBase> requestCaptor = ArgumentCaptor.forClass(HttpRequestBase.class); 113 verify(client).executeRequest(eq(uri), requestCaptor.capture()); 114 115 final HttpEntityEnclosingRequestBase request = (HttpEntityEnclosingRequestBase) requestCaptor.getValue(); 116 final HttpEntity bodyEntity = request.getEntity(); 117 assertEquals(bodyStream, bodyEntity.getContent()); 118 assertEquals("application/octet-stream", request.getFirstHeader(CONTENT_TYPE).getValue()); 119 } 120 121 @Test(expected = FcrepoOperationFailedException.class) 122 public void testPostClientError() throws Exception { 123 when(client.executeRequest(any(URI.class), any(HttpRequestBase.class))) 124 .thenThrow(new FcrepoOperationFailedException(uri, 415, "status")); 125 126 testBuilder.perform(); 127 } 128}