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 static org.junit.Assert.assertEquals;
009import static org.mockito.Mockito.verify;
010import static org.mockito.Mockito.when;
011import static org.mockito.MockitoAnnotations.initMocks;
012
013import javax.ws.rs.core.Response;
014import javax.ws.rs.ext.ExceptionMapper;
015import javax.ws.rs.ext.Providers;
016
017import org.fcrepo.kernel.api.exception.RepositoryException;
018import org.fcrepo.kernel.api.exception.RepositoryRuntimeException;
019import org.junit.Before;
020import org.junit.Test;
021import org.mockito.Mock;
022
023/**
024 * @author cabeer
025 */
026public class RepositoryRuntimeExceptionMapperTest {
027
028    @Mock
029    private Providers mockProviders;
030
031    private RepositoryRuntimeExceptionMapper testObj;
032
033    @Mock
034    private ExceptionMapper<RepositoryException> mockProvider;
035
036    @Before
037    public void setUp() {
038        initMocks(this);
039
040        testObj = new RepositoryRuntimeExceptionMapper(mockProviders);
041    }
042
043    @Test
044    public void testToResponseWithHandledRepositoryException() {
045        when(mockProviders.getExceptionMapper(RepositoryException.class)).thenReturn(mockProvider);
046        final RepositoryException cause = new RepositoryException("xyz");
047        final RepositoryRuntimeException ex = new RepositoryRuntimeException(cause.getMessage(), cause);
048        testObj.toResponse(ex);
049        verify(mockProvider).toResponse(cause);
050    }
051
052    @Test
053    public void testToResponseWithUnhandledRepositoryException() {
054        when(mockProviders.getExceptionMapper(Exception.class)).thenReturn(null);
055        final Exception cause = new Exception("xyz");
056        final RepositoryRuntimeException ex = new RepositoryRuntimeException(cause.getMessage(), cause);
057        final Response response = testObj.toResponse(ex);
058        assertEquals(500, response.getStatus());
059    }
060
061    @Test
062    public void testToResponseWithNoWrappedException() {
063        when(mockProviders.getExceptionMapper(Exception.class)).thenReturn(null);
064        final RepositoryRuntimeException ex = new RepositoryRuntimeException("!");
065        final Response response = testObj.toResponse(ex);
066        assertEquals(500, response.getStatus());
067    }
068}