1 package it.com.atlassian.rest.error;
2
3 import javax.ws.rs.core.MediaType;
4
5 import com.atlassian.rest.jersey.client.WebResourceFactory;
6
7 import com.sun.jersey.api.client.ClientResponse;
8
9 import org.junit.Test;
10 import org.junit.matchers.JUnitMatchers;
11
12 import net.sf.json.JSONObject;
13
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assert.assertThat;
17
18 public class UncaughtErrorTest {
19 private static final String MESSAGE = "my error detail string";
20
21 @Test
22 public void testUncaughtErrorResponseIsReturnedInXMLWhenXMLIsRequested() {
23 final ClientResponse response = WebResourceFactory.anonymous()
24 .path("errors/uncaughtInternalError")
25 .queryParam("message", MESSAGE)
26 .accept("application/xml")
27 .get(ClientResponse.class);
28 assertEquals(500, response.getStatus());
29 assertEquals("application/xml", response.getType().toString());
30
31 String entity = response.getEntity(String.class);
32
33 assertThat(entity,
34 JUnitMatchers.containsString("<status-code>500</status-code>"));
35 assertThat(entity,
36 JUnitMatchers.containsString("<message>" + MESSAGE + "</message>"));
37 }
38
39 @Test
40 public void testUncaughtErrorResponseIsReturnedInJSONWhenJSONIsRequested() {
41 final ClientResponse response = WebResourceFactory.anonymous()
42 .path("errors/uncaughtInternalError")
43 .queryParam("message", MESSAGE)
44 .accept("application/json")
45 .get(ClientResponse.class);
46 assertEquals(500, response.getStatus());
47 assertEquals("application/json", response.getType().toString());
48
49 JSONObject obj = JSONObject.fromObject(response.getEntity(String.class));
50
51 assertEquals(MESSAGE, obj.getString("message"));
52 assertEquals("500", obj.getString("status-code"));
53
54 assertNotNull(obj.getString("stack-trace"));
55 }
56
57 @Test
58 public void testUncaughtErrorResponseIsReturnedInTextWhenTextIsRequested() {
59 final ClientResponse response = WebResourceFactory.anonymous()
60 .path("errors/uncaughtInternalError")
61 .queryParam("message", MESSAGE)
62 .accept("text/plain")
63 .get(ClientResponse.class);
64 assertEquals(500, response.getStatus());
65
66 assertEquals(response.getType(),
67 MediaType.valueOf("text/plain; charset=utf-8"));
68
69 String entity = response.getEntity(String.class);
70 assertThat(entity,
71 JUnitMatchers.containsString(MESSAGE));
72 assertThat(entity,
73 JUnitMatchers.containsString(IllegalStateException.class.getName()));
74 }
75 }