1 package com.atlassian.plugins.rest.common.error.jersey;
2
3 import java.util.Collections;
4 import java.util.List;
5
6 import javax.ws.rs.WebApplicationException;
7 import javax.ws.rs.core.MediaType;
8 import javax.ws.rs.core.Request;
9 import javax.ws.rs.core.Response;
10 import javax.ws.rs.core.Variant;
11
12 import org.junit.Test;
13 import org.mockito.Mockito;
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 mapper.request = mock(Request.class);
26 when(mapper.request.selectVariant(Mockito.<List<Variant>>anyObject())).thenReturn(
27 new Variant(MediaType.TEXT_HTML_TYPE, null, null));
28
29 RuntimeException e = new RuntimeException("foo");
30 Response res = mapper.toResponse(e);
31 assertEquals(res.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
32 assertEquals("foo", ((UncaughtExceptionEntity)res.getEntity()).getMessage());
33 }
34
35 @Test
36 public void testWebApplicationException()
37 {
38 ThrowableExceptionMapper mapper = new ThrowableExceptionMapper();
39 WebApplicationException e = new WebApplicationException(444);
40 Response res = mapper.toResponse(e);
41 assertEquals(444, res.getStatus());
42 }
43
44 @Test
45 public void getResponseAsHtml()
46 {
47 ThrowableExceptionMapper mapper = new ThrowableExceptionMapper();
48 Exception e = new Exception();
49 mapper.request = mock(Request.class);
50 when(mapper.request.selectVariant(Mockito.<List<Variant>>anyObject())).thenReturn(
51 new Variant(MediaType.TEXT_HTML_TYPE, null, null));
52
53 Response res = mapper.toResponse(e);
54 assertEquals(Collections.singletonList(MediaType.TEXT_HTML_TYPE), res.getMetadata().get("Content-Type"));
55 assertEquals(500, res.getStatus());
56 }
57 }