001/*
002 * The contents of this file are subject to the license and copyright
003 * detailed in the LICENSE and NOTICE files at the root of the source
004 * tree.
005 */
006package org.fcrepo.http.commons.exceptionhandlers;
007
008import org.fcrepo.http.commons.api.rdf.HttpIdentifierConverter;
009import org.fcrepo.kernel.api.exception.TombstoneException;
010import org.fcrepo.kernel.api.identifiers.FedoraId;
011import org.fcrepo.kernel.api.models.FedoraResource;
012import org.junit.Before;
013import org.junit.Test;
014import org.mockito.Mock;
015
016import javax.ws.rs.core.Link;
017import javax.ws.rs.core.Response;
018import javax.ws.rs.core.UriBuilder;
019import javax.ws.rs.ext.ExceptionMapper;
020import java.time.Instant;
021
022import static java.time.ZoneOffset.UTC;
023import static java.time.format.DateTimeFormatter.ISO_INSTANT;
024import static javax.ws.rs.core.HttpHeaders.LINK;
025import static javax.ws.rs.core.Response.Status.GONE;
026import static org.junit.Assert.assertEquals;
027import static org.junit.Assert.assertNull;
028import static org.mockito.Mockito.when;
029import static org.mockito.MockitoAnnotations.initMocks;
030
031/**
032 * @author cabeer
033 */
034public class TombstoneExceptionMapperTest {
035
036    @Mock
037    private FedoraResource mockResource;
038
039    private final FedoraId fedoraId = FedoraId.create("/some:uri");
040
041    private final Instant deleteTime = Instant.now();
042
043    private ExceptionMapper<TombstoneException> testObj;
044
045    private static final String SERVER_URI = "http://localhost:8080/rest(.*)";
046
047    private final HttpIdentifierConverter idConverter = new HttpIdentifierConverter(UriBuilder.fromUri(SERVER_URI));
048
049    @Before
050    public void setUp() {
051        initMocks(this);
052        when(mockResource.getFedoraId()).thenReturn(fedoraId);
053        when(mockResource.getLastModifiedDate()).thenReturn(deleteTime);
054        testObj = new TombstoneExceptionMapper();
055    }
056
057    @Test
058    public void testUrilessException() {
059        final Response response = testObj.toResponse(new TombstoneException(mockResource));
060        assertEquals(GONE.getStatusCode(), response.getStatus());
061        assertTombstone(response, fedoraId.getFullIdPath(), null);
062    }
063
064    @Test
065    public void testExceptionWithUri() {
066        final String tombstone = idConverter.toExternalId(fedoraId.asTombstone().getFullId());
067        final Response response = testObj.toResponse(new TombstoneException(mockResource, tombstone));
068        assertEquals(GONE.getStatusCode(), response.getStatus());
069        assertTombstone(response, fedoraId.getFullIdPath(), tombstone);
070    }
071
072    private void assertTombstone(final Response response, final String tombstoneAt, final String tombstoneUri) {
073        if (tombstoneUri == null) {
074            assertNull(response.getHeaderString(LINK));
075        } else {
076            final Link link = Link.valueOf(response.getHeaderString(LINK));
077            assertEquals(tombstoneUri, link.getUri().toString());
078            assertEquals("hasTombstone", link.getRel());
079        }
080        final String expectedString = "Discovered tombstone resource at " + tombstoneAt + ", departed at: " +
081                ISO_INSTANT.withZone(UTC).format(deleteTime);
082        assertEquals(expectedString, response.getEntity().toString());
083    }
084}