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