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.TestUtils.baseUrl; 022import static org.junit.Assert.assertEquals; 023import static org.mockito.ArgumentMatchers.any; 024import static org.mockito.ArgumentMatchers.eq; 025import static org.mockito.Mockito.verify; 026import static org.mockito.Mockito.when; 027import static org.fcrepo.client.FedoraHeaderConstants.MEMENTO_DATETIME; 028import static org.fcrepo.client.HeaderHelpers.UTC_RFC_1123_FORMATTER; 029 030import java.net.URI; 031import java.time.Instant; 032import java.time.LocalDateTime; 033import java.time.ZoneOffset; 034import java.time.format.DateTimeParseException; 035 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 HistoricMementoBuilderTest { 051 052 private final String HISTORIC_DATETIME = 053 UTC_RFC_1123_FORMATTER.format(LocalDateTime.of(2000, 1, 1, 0, 0).atZone(ZoneOffset.UTC)); 054 055 @Mock 056 private FcrepoClient client; 057 058 @Mock 059 private FcrepoResponse fcrepoResponse; 060 061 @Captor 062 private ArgumentCaptor<HttpRequestBase> requestCaptor; 063 064 private HistoricMementoBuilder testBuilder; 065 066 private URI uri; 067 068 @Before 069 public void setUp() throws Exception { 070 when(client.executeRequest(any(URI.class), any(HttpRequestBase.class))) 071 .thenReturn(fcrepoResponse); 072 073 uri = create(baseUrl); 074 } 075 076 @Test 077 public void testCreateWithInstantHeader() throws Exception { 078 final Instant datetime = LocalDateTime.of(2000, 1, 1, 00, 00).atZone(ZoneOffset.UTC).toInstant(); 079 080 testBuilder = new HistoricMementoBuilder(uri, client, datetime); 081 testBuilder.perform(); 082 083 verify(client).executeRequest(eq(uri), requestCaptor.capture()); 084 085 final HttpEntityEnclosingRequestBase request = (HttpEntityEnclosingRequestBase) requestCaptor.getValue(); 086 assertEquals(HISTORIC_DATETIME, request.getFirstHeader(MEMENTO_DATETIME).getValue()); 087 } 088 089 @Test 090 public void testCreateWithStringMementoDatetime() throws Exception { 091 testBuilder = new HistoricMementoBuilder(uri, client, HISTORIC_DATETIME); 092 testBuilder.perform(); 093 094 verify(client).executeRequest(eq(uri), requestCaptor.capture()); 095 096 final HttpEntityEnclosingRequestBase request = (HttpEntityEnclosingRequestBase) requestCaptor.getValue(); 097 assertEquals(HISTORIC_DATETIME, request.getFirstHeader(MEMENTO_DATETIME).getValue()); 098 } 099 100 @Test(expected = NullPointerException.class) 101 public void testCreateWithNoDatetime() throws Exception { 102 testBuilder = new HistoricMementoBuilder(uri, client, (String) null); 103 testBuilder.perform(); 104 } 105 106 @Test(expected = DateTimeParseException.class) 107 public void testCreateWithNonRFC1123() throws Exception { 108 final String mementoDatetime = Instant.now().toString(); 109 testBuilder = new HistoricMementoBuilder(uri, client, mementoDatetime); 110 testBuilder.perform(); 111 } 112}