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.ACCEPT_DATETIME; 022import static org.fcrepo.client.FedoraHeaderConstants.CACHE_CONTROL; 023import static org.fcrepo.client.FedoraHeaderConstants.LINK; 024import static org.fcrepo.client.FedoraHeaderConstants.WANT_DIGEST; 025import static org.fcrepo.client.TestUtils.baseUrl; 026import static org.fcrepo.client.HeaderHelpers.UTC_RFC_1123_FORMATTER; 027import static org.junit.Assert.assertEquals; 028import static org.junit.Assert.assertFalse; 029import static org.mockito.ArgumentMatchers.any; 030import static org.mockito.ArgumentMatchers.eq; 031import static org.mockito.Mockito.verify; 032import static org.mockito.Mockito.when; 033 034import java.net.URI; 035import java.time.Instant; 036import java.time.LocalDateTime; 037import java.time.ZoneOffset; 038 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.Captor; 045import org.mockito.Mock; 046import org.mockito.junit.MockitoJUnitRunner; 047 048/** 049 * @author escowles 050 */ 051@RunWith(MockitoJUnitRunner.class) 052public class HeadBuilderTest { 053 054 private final String HISTORIC_DATETIME = 055 UTC_RFC_1123_FORMATTER.format(LocalDateTime.of(2000, 1, 1, 0, 0).atZone(ZoneOffset.UTC)); 056 057 @Mock 058 private FcrepoClient client; 059 060 @Mock 061 private FcrepoResponse fcrepoResponse; 062 063 private HeadBuilder testBuilder; 064 065 private URI uri; 066 067 @Captor 068 private ArgumentCaptor<HttpRequestBase> requestCaptor; 069 070 @Before 071 public void setUp() throws Exception { 072 when(client.executeRequest(any(URI.class), any(HttpRequestBase.class))) 073 .thenReturn(fcrepoResponse); 074 075 uri = create(baseUrl); 076 testBuilder = new HeadBuilder(uri, client); 077 } 078 079 @Test 080 public void testHead() throws Exception { 081 testBuilder.perform(); 082 083 verify(client).executeRequest(eq(uri), requestCaptor.capture()); 084 } 085 086 @Test 087 public void testDisableRedirects() throws Exception { 088 testBuilder.disableRedirects(); 089 assertFalse(testBuilder.request.getConfig().isRedirectsEnabled()); 090 } 091 092 @Test 093 public void testWantDigest() throws Exception { 094 testBuilder.wantDigest("md5").perform(); 095 096 verify(client).executeRequest(eq(uri), requestCaptor.capture()); 097 final HttpRequestBase request = requestCaptor.getValue(); 098 assertEquals("md5", request.getFirstHeader(WANT_DIGEST).getValue()); 099 } 100 101 @Test 102 public void testNoCache() throws Exception { 103 testBuilder.noCache().perform(); 104 105 verify(client).executeRequest(eq(uri), requestCaptor.capture()); 106 final HttpRequestBase request = requestCaptor.getValue(); 107 assertEquals("no-cache", request.getFirstHeader(CACHE_CONTROL).getValue()); 108 } 109 110 @Test 111 public void testAcceptDatetime() throws Exception { 112 testBuilder.acceptDatetime(HISTORIC_DATETIME).perform(); 113 114 verify(client).executeRequest(eq(uri), requestCaptor.capture()); 115 final HttpRequestBase request = requestCaptor.getValue(); 116 assertEquals(HISTORIC_DATETIME, request.getFirstHeader(ACCEPT_DATETIME).getValue()); 117 } 118 119 @Test 120 public void testAcceptDatetimeInstant() throws Exception { 121 final Instant datetime = LocalDateTime.of(2000, 1, 1, 00, 00).atZone(ZoneOffset.UTC).toInstant(); 122 testBuilder.acceptDatetime(datetime).perform(); 123 124 verify(client).executeRequest(eq(uri), requestCaptor.capture()); 125 final HttpRequestBase request = requestCaptor.getValue(); 126 assertEquals(HISTORIC_DATETIME, request.getFirstHeader(ACCEPT_DATETIME).getValue()); 127 } 128 129 @Test 130 public void testAddHeader() throws Exception { 131 testBuilder.addHeader("my-header", "head-val").perform(); 132 133 verify(client).executeRequest(eq(uri), requestCaptor.capture()); 134 final HttpRequestBase request = requestCaptor.getValue(); 135 assertEquals("head-val", request.getFirstHeader("my-header").getValue()); 136 } 137 138 @Test 139 public void testAddLinkHeader() throws Exception { 140 final FcrepoLink link = FcrepoLink.fromUri("http://example.com/link").type("foo").build(); 141 testBuilder.addLinkHeader(link).perform(); 142 143 verify(client).executeRequest(eq(uri), requestCaptor.capture()); 144 final HttpRequestBase request = requestCaptor.getValue(); 145 assertEquals(link.toString(), request.getFirstHeader(LINK).getValue()); 146 } 147}