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 com.atlassian.plugins.rest.common.Status;
13  
14  import org.junit.Test;
15  import org.mockito.Mockito;
16  
17  import static org.junit.Assert.assertEquals;
18  import static org.mockito.Mockito.mock;
19  import static org.mockito.Mockito.when;
20  
21  public class ThrowableExceptionMapperTest
22  {
23      @Test
24      public void testSomeThrowable()
25      {
26          ThrowableExceptionMapper mapper = new ThrowableExceptionMapper();
27          mapper.request = mock(Request.class);
28          when(mapper.request.selectVariant(Mockito.<List<Variant>>anyObject())).thenReturn(
29                  new Variant(MediaType.TEXT_HTML_TYPE, null, null));
30  
31          RuntimeException e = new RuntimeException("foo");
32          Response res = mapper.toResponse(e);
33          assertEquals(res.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
34          assertEquals("foo", ((Status)res.getEntity()).getMessage());
35      }
36  
37      @Test
38      public void testWebApplicationException()
39      {
40          ThrowableExceptionMapper mapper = new ThrowableExceptionMapper();
41          WebApplicationException e = new WebApplicationException(444);
42          Response res = mapper.toResponse(e);
43          assertEquals(444, res.getStatus());
44      }
45  
46      @Test
47      public void getResponseAsHtml()
48      {
49          ThrowableExceptionMapper mapper = new ThrowableExceptionMapper();
50          Exception e = new Exception();
51          mapper.request = mock(Request.class);
52          when(mapper.request.selectVariant(Mockito.<List<Variant>>anyObject())).thenReturn(
53                  new Variant(MediaType.TEXT_HTML_TYPE, null, null));
54  
55          Response res = mapper.toResponse(e);
56          assertEquals(Collections.singletonList(MediaType.TEXT_HTML_TYPE), res.getMetadata().get("Content-Type"));
57          assertEquals(500, res.getStatus());
58      }
59  }