1 package com.atlassian.plugins.rest.common.error.jersey;
2
3 import java.util.Arrays;
4 import java.util.Collections;
5
6 import javax.ws.rs.WebApplicationException;
7 import javax.ws.rs.core.HttpHeaders;
8 import javax.ws.rs.core.MediaType;
9 import javax.ws.rs.core.Response;
10
11 import com.atlassian.plugins.rest.common.Status;
12
13 import org.junit.Test;
14
15 import static org.junit.Assert.assertEquals;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.when;
18
19 public class ThrowableExceptionMapperTest
20 {
21 @Test
22 public void testSomeThrowable()
23 {
24 ThrowableExceptionMapper mapper = new ThrowableExceptionMapper();
25 RuntimeException e = new RuntimeException("foo");
26 Response res = mapper.toResponse(e);
27 assertEquals(res.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
28 assertEquals("foo", ((Status)res.getEntity()).getMessage());
29 }
30
31 @Test
32 public void testWebApplicationException()
33 {
34 ThrowableExceptionMapper mapper = new ThrowableExceptionMapper();
35 WebApplicationException e = new WebApplicationException(444);
36 Response res = mapper.toResponse(e);
37 assertEquals(444, res.getStatus());
38 }
39
40 @Test
41 public void getResponseAsHtml()
42 {
43 ThrowableExceptionMapper mapper = new ThrowableExceptionMapper();
44 Exception e = new Exception();
45 mapper.headers = mock(HttpHeaders.class);
46 when(mapper.headers.getAcceptableMediaTypes()).thenReturn(Arrays.asList(MediaType.TEXT_HTML_TYPE));
47
48 Response res = mapper.toResponse(e);
49 assertEquals(Collections.singletonList(MediaType.TEXT_HTML_TYPE), res.getMetadata().get("Content-Type"));
50 assertEquals(500, res.getStatus());
51 }
52 }