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 static org.junit.Assert.assertEquals; 021import static org.mockito.Mockito.verify; 022import static org.mockito.Mockito.when; 023import static org.mockito.MockitoAnnotations.initMocks; 024 025import javax.ws.rs.core.Response; 026import javax.ws.rs.ext.ExceptionMapper; 027import javax.ws.rs.ext.Providers; 028 029import org.fcrepo.kernel.api.exception.RepositoryException; 030import org.fcrepo.kernel.api.exception.RepositoryRuntimeException; 031import org.junit.Before; 032import org.junit.Test; 033import org.mockito.Mock; 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.getMessage(), 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.getMessage(), cause); 069 final Response response = testObj.toResponse(ex); 070 assertEquals(500, response.getStatus()); 071 } 072 073 @Test 074 public void testToResponseWithNoWrappedException() { 075 when(mockProviders.getExceptionMapper(Exception.class)).thenReturn(null); 076 final RepositoryRuntimeException ex = new RepositoryRuntimeException("!"); 077 final Response response = testObj.toResponse(ex); 078 assertEquals(500, response.getStatus()); 079 } 080}