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.http.commons.exceptionhandlers;
019
020import org.fcrepo.http.commons.api.rdf.HttpIdentifierConverter;
021import org.fcrepo.kernel.api.exception.TombstoneException;
022import org.fcrepo.kernel.api.identifiers.FedoraId;
023import org.fcrepo.kernel.api.models.FedoraResource;
024import org.junit.Before;
025import org.junit.Test;
026import org.mockito.Mock;
027
028import javax.ws.rs.core.Link;
029import javax.ws.rs.core.Response;
030import javax.ws.rs.core.UriBuilder;
031import javax.ws.rs.ext.ExceptionMapper;
032import java.time.Instant;
033
034import static java.time.ZoneOffset.UTC;
035import static java.time.format.DateTimeFormatter.ISO_INSTANT;
036import static javax.ws.rs.core.HttpHeaders.LINK;
037import static javax.ws.rs.core.Response.Status.GONE;
038import static org.junit.Assert.assertEquals;
039import static org.junit.Assert.assertNull;
040import static org.mockito.Mockito.when;
041import static org.mockito.MockitoAnnotations.initMocks;
042
043/**
044 * @author cabeer
045 */
046public class TombstoneExceptionMapperTest {
047
048    @Mock
049    private FedoraResource mockResource;
050
051    private final FedoraId fedoraId = FedoraId.create("/some:uri");
052
053    private final Instant deleteTime = Instant.now();
054
055    private ExceptionMapper<TombstoneException> testObj;
056
057    private static final String SERVER_URI = "http://localhost:8080/rest(.*)";
058
059    private final HttpIdentifierConverter idConverter = new HttpIdentifierConverter(UriBuilder.fromUri(SERVER_URI));
060
061    @Before
062    public void setUp() {
063        initMocks(this);
064        when(mockResource.getFedoraId()).thenReturn(fedoraId);
065        when(mockResource.getLastModifiedDate()).thenReturn(deleteTime);
066        testObj = new TombstoneExceptionMapper();
067    }
068
069    @Test
070    public void testUrilessException() {
071        final Response response = testObj.toResponse(new TombstoneException(mockResource));
072        assertEquals(GONE.getStatusCode(), response.getStatus());
073        assertTombstone(response, fedoraId.getFullIdPath(), null);
074    }
075
076    @Test
077    public void testExceptionWithUri() {
078        final String tombstone = idConverter.toExternalId(fedoraId.asTombstone().getFullId());
079        final Response response = testObj.toResponse(new TombstoneException(mockResource, tombstone));
080        assertEquals(GONE.getStatusCode(), response.getStatus());
081        assertTombstone(response, fedoraId.getFullIdPath(), tombstone);
082    }
083
084    private void assertTombstone(final Response response, final String tombstoneAt, final String tombstoneUri) {
085        if (tombstoneUri == null) {
086            assertNull(response.getHeaderString(LINK));
087        } else {
088            final Link link = Link.valueOf(response.getHeaderString(LINK));
089            assertEquals(tombstoneUri, link.getUri().toString());
090            assertEquals("hasTombstone", link.getRel());
091        }
092        final String expectedString = "Discovered tombstone resource at " + tombstoneAt + ", departed at: " +
093                ISO_INSTANT.withZone(UTC).format(deleteTime);
094        assertEquals(expectedString, response.getEntity().toString());
095    }
096}