001/*
002 * Copyright 2015 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fcrepo.http.commons.exceptionhandlers;
017
018import org.fcrepo.kernel.api.exception.TombstoneException;
019import org.fcrepo.kernel.api.models.Tombstone;
020import org.junit.Before;
021import org.junit.Test;
022import org.mockito.Mock;
023
024import javax.ws.rs.core.Link;
025import javax.ws.rs.core.Response;
026import javax.ws.rs.ext.ExceptionMapper;
027
028import static javax.ws.rs.core.Response.Status.GONE;
029import static org.junit.Assert.assertEquals;
030import static org.mockito.MockitoAnnotations.initMocks;
031
032/**
033 * @author cabeer
034 */
035public class TombstoneExceptionMapperTest {
036
037    private ExceptionMapper<TombstoneException> testObj;
038
039    @Mock
040    public Tombstone mockTombstone;
041
042    @Before
043    public void setUp() {
044        initMocks(this);
045
046        testObj = new TombstoneExceptionMapper();
047    }
048
049    @Test
050    public void testUrilessException() {
051        final Response response = testObj.toResponse(new TombstoneException(mockTombstone));
052        assertEquals(GONE.getStatusCode(), response.getStatus());
053    }
054
055    @Test
056    public void testExceptionWithUri() {
057        final Response response = testObj.toResponse(new TombstoneException(mockTombstone, "some:uri"));
058        assertEquals(GONE.getStatusCode(), response.getStatus());
059        final Link link = Link.valueOf(response.getHeaderString("Link"));
060        assertEquals("some:uri", link.getUri().toString());
061        assertEquals("hasTombstone", link.getRel());
062    }
063
064}