View Javadoc

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      @Test
21      public void testSomeThrowable() {
22          ThrowableExceptionMapper mapper = new ThrowableExceptionMapper();
23          mapper.request = mock(Request.class);
24          when(mapper.request.selectVariant(Mockito.<List<Variant>>anyObject())).thenReturn(
25                  new Variant(MediaType.TEXT_HTML_TYPE, null, null));
26  
27          RuntimeException e = new RuntimeException("foo");
28          Response res = mapper.toResponse(e);
29          assertEquals(res.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
30          assertEquals("foo", ((UncaughtExceptionEntity) res.getEntity()).getMessage());
31      }
32  
33      @Test
34      public void testWebApplicationException() {
35          ThrowableExceptionMapper mapper = new ThrowableExceptionMapper();
36          WebApplicationException e = new WebApplicationException(444);
37          Response res = mapper.toResponse(e);
38          assertEquals(444, res.getStatus());
39      }
40  
41      @Test
42      public void getResponseAsHtml() {
43          ThrowableExceptionMapper mapper = new ThrowableExceptionMapper();
44          Exception e = new Exception();
45          mapper.request = mock(Request.class);
46          when(mapper.request.selectVariant(Mockito.<List<Variant>>anyObject())).thenReturn(
47                  new Variant(MediaType.TEXT_HTML_TYPE, null, null));
48  
49          Response res = mapper.toResponse(e);
50          assertEquals(Collections.singletonList(MediaType.TEXT_HTML_TYPE), res.getMetadata().get("Content-Type"));
51          assertEquals(500, res.getStatus());
52      }
53  }