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