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_DISPOSITION; 022import static org.fcrepo.client.FedoraHeaderConstants.CONTENT_TYPE; 023import static org.fcrepo.client.FedoraHeaderConstants.DIGEST; 024import static org.fcrepo.client.FedoraHeaderConstants.SLUG; 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 PostBuilderTest { 052 053 @Mock 054 private FcrepoClient client; 055 056 @Mock 057 private FcrepoResponse fcrepoResponse; 058 059 private PostBuilder 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 PostBuilder(uri, client); 070 } 071 072 @Test 073 public void testPostNoBody() 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 .digestSha1("checksum") 090 .filename("file.txt") 091 .slug("slug_value") 092 .perform(); 093 094 final ArgumentCaptor<HttpRequestBase> requestCaptor = ArgumentCaptor.forClass(HttpRequestBase.class); 095 verify(client).executeRequest(eq(uri), requestCaptor.capture()); 096 097 final HttpEntityEnclosingRequestBase request = (HttpEntityEnclosingRequestBase) requestCaptor.getValue(); 098 final HttpEntity bodyEntity = request.getEntity(); 099 assertEquals(bodyStream, bodyEntity.getContent()); 100 101 assertEquals("plain/text", request.getFirstHeader(CONTENT_TYPE).getValue()); 102 assertEquals("sha1=checksum", request.getFirstHeader(DIGEST).getValue()); 103 assertEquals("slug_value", request.getFirstHeader(SLUG).getValue()); 104 assertEquals("attachment; filename=\"file.txt\"", request.getFirstHeader(CONTENT_DISPOSITION).getValue()); 105 } 106 107 @Test 108 public void testAttachment() throws Exception { 109 final InputStream bodyStream = mock(InputStream.class); 110 testBuilder.body(bodyStream, "plain/text").filename(null).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("attachment", request.getFirstHeader(CONTENT_DISPOSITION).getValue()); 119 } 120 121 @Test 122 public void testWithBodyMultipleChecksums() throws Exception { 123 final InputStream bodyStream = mock(InputStream.class); 124 125 testBuilder.body(bodyStream, "plain/text") 126 .digestSha1("checksum") 127 .digestSha256("checksum256") 128 .perform(); 129 130 final ArgumentCaptor<HttpRequestBase> requestCaptor = ArgumentCaptor.forClass(HttpRequestBase.class); 131 verify(client).executeRequest(eq(uri), requestCaptor.capture()); 132 133 final HttpEntityEnclosingRequestBase request = (HttpEntityEnclosingRequestBase) requestCaptor.getValue(); 134 final HttpEntity bodyEntity = request.getEntity(); 135 assertEquals(bodyStream, bodyEntity.getContent()); 136 137 assertEquals("plain/text", request.getFirstHeader(CONTENT_TYPE).getValue()); 138 assertEquals("sha1=checksum, sha256=checksum256", request.getFirstHeader(DIGEST).getValue()); 139 } 140 141 @Test 142 public void testBodyNoType() throws Exception { 143 final InputStream bodyStream = mock(InputStream.class); 144 145 testBuilder.body(bodyStream).perform(); 146 147 final ArgumentCaptor<HttpRequestBase> requestCaptor = ArgumentCaptor.forClass(HttpRequestBase.class); 148 verify(client).executeRequest(eq(uri), requestCaptor.capture()); 149 150 final HttpEntityEnclosingRequestBase request = (HttpEntityEnclosingRequestBase) requestCaptor.getValue(); 151 final HttpEntity bodyEntity = request.getEntity(); 152 assertEquals(bodyStream, bodyEntity.getContent()); 153 assertEquals("application/octet-stream", request.getFirstHeader(CONTENT_TYPE).getValue()); 154 } 155 156 @Test(expected = FcrepoOperationFailedException.class) 157 public void testPostClientError() throws Exception { 158 when(client.executeRequest(any(URI.class), any(HttpRequestBase.class))) 159 .thenThrow(new FcrepoOperationFailedException(uri, 415, "status")); 160 161 testBuilder.perform(); 162 } 163}