254b51b6d9b37280179196d097b0575b0ecf6f32
[facebook-errors] / src / test / java / de / juplo / facebook / errors / GraphApiErrorResponseErrorHandlerIntegrationTest.java
1 package de.juplo.facebook.errors;
2
3
4 import de.juplo.facebook.errors.GraphApiException.Type;
5 import static org.junit.Assert.assertEquals;
6 import static org.junit.Assert.fail;
7 import org.junit.Before;
8 import org.junit.Test;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11 import org.springframework.http.HttpStatus;
12 import org.springframework.web.client.HttpClientErrorException;
13 import org.springframework.web.client.HttpServerErrorException;
14 import org.springframework.web.client.RestTemplate;
15
16
17
18 /**
19  *
20  * @author Kai Moritz
21  */
22 public class GraphApiErrorResponseErrorHandlerIntegrationTest
23 {
24   private static final Logger LOG =
25       LoggerFactory.getLogger(GraphApiErrorResponseErrorHandlerIntegrationTest.class);
26
27   private RestTemplate clientTemplate;
28   private MockClientHttpRequestFactory requestFactory;
29
30
31   @Test
32   public void testNoError()
33   {
34     requestFactory.setBody("{ \"message\": \"Hello World!\" }");
35
36
37     requestFactory.setStatus(HttpStatus.CONTINUE);
38     try
39     {
40       clientTemplate.getForObject("ANY", SOME.class);
41     }
42     catch(Exception e)
43     {
44       LOG.debug("{}", e.toString());
45       fail("Unexpected error: " + e);
46     }
47
48     requestFactory.setStatus(HttpStatus.OK);
49     try
50     {
51       clientTemplate.getForObject("ANY", SOME.class);
52     }
53     catch(Exception e)
54     {
55       LOG.debug("{}", e.toString());
56       fail("Unexpected error: " + e);
57     }
58
59     requestFactory.setStatus(HttpStatus.TEMPORARY_REDIRECT);
60     try
61     {
62       clientTemplate.getForObject("ANY", SOME.class);
63     }
64     catch(Exception e)
65     {
66       LOG.debug("{}", e.toString());
67       fail("Unexpected error: " + e);
68     }
69
70     requestFactory.setStatus(HttpStatus.BAD_REQUEST);
71     try
72     {
73       clientTemplate.getForObject("ANY", SOME.class);
74       fail("The parent handler should have raised an exception!");
75     }
76     catch(HttpClientErrorException e)
77     {
78       LOG.debug("Expexted error: {}", e.toString());
79     }
80     catch(Exception e)
81     {
82       LOG.debug("{}", e.toString());
83       fail("Unexpected error: " + e);
84     }
85
86     requestFactory.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
87     try
88     {
89       clientTemplate.getForObject("ANY", SOME.class);
90       fail("The parent handler should have raised an exception!");
91     }
92     catch(HttpServerErrorException e)
93     {
94       LOG.debug("Expexted error: {}", e.toString());
95     }
96     catch(Exception e)
97     {
98       LOG.debug("{}", e.toString());
99       fail("Unexpected error: " + e);
100     }
101   }
102
103   @Test
104   public void testValidError()
105   {
106     requestFactory.setBody(
107         "{\n" +
108         "  \"error\":\n" +
109         "  {\n" +
110         "    \"message\": \"(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.\",\n" +
111         "    \"type\": \"OAuthException\",\n" +
112         "    \"code\": 613\n" +
113         "  }\n" +
114         "}");
115
116     try
117     {
118       clientTemplate.getForObject("ANY", SOME.class);
119       fail("The expected exception was not thrown");
120     }
121     catch(RateLimitExceededException e)
122     {
123       LOG.debug("{}", e.toString());
124       assertEquals(new Integer(613), e.getCode());
125       assertEquals("(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.", e.getMessage());
126       assertEquals(Type.OAuthException, e.getType());
127     }
128   }
129
130   @Test
131   public void testUnmappedError()
132   {
133     requestFactory.setBody(
134         "{\n" +
135         "  \"error\":\n" +
136         "  {\n" +
137         "    \"message\": \"This error does not exist.\",\n" +
138         "    \"type\": \"NonexistentTypeException\",\n" +
139         "    \"code\": 999999999\n" +
140         "  }\n" +
141         "}");
142
143     try
144     {
145       clientTemplate.getForObject("ANY", SOME.class);
146       fail("The expected exception was not thrown");
147     }
148     catch(GraphApiException e)
149     {
150       LOG.debug("{}", e.toString());
151       assertEquals(new Integer(999999999), e.getCode());
152       assertEquals("This error does not exist.", e.getMessage());
153       try
154       {
155         Type type = e.getType();
156         LOG.error("unknown type: {}", type);
157         fail("unmapped type was resolved by enum: " + type);
158       }
159       catch (IllegalArgumentException ee) {}
160     }
161   }
162
163   @Test
164   public void testInvlalidError()
165   {
166     requestFactory.setBody(
167         "{\n" +
168         "  \"error\":\n" +
169         "  {\n" +
170         "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
171         "    \"type\": \"Whatever\",\n" +
172         "    \"code\": \"some string\"\n" +
173         "  }\n" +
174         "}");
175
176     try
177     {
178       clientTemplate.getForObject("ANY", SOME.class);
179       fail("The expected exception was not thrown");
180     }
181     catch(HttpClientErrorException e)
182     {
183       LOG.debug("{}", e.toString());
184     }
185     catch(Exception e)
186     {
187       fail("A wrong exception was thrown: " + e.toString());
188     }
189   }
190
191
192   @Before
193   public void setUp()
194   {
195     requestFactory = new MockClientHttpRequestFactory();
196     requestFactory.setStatus(HttpStatus.BAD_REQUEST);
197     requestFactory.addHeader("Content-Type", "application/json");
198
199     clientTemplate = new RestTemplate();
200     clientTemplate.setRequestFactory(requestFactory);
201     clientTemplate.setErrorHandler(
202         new GraphApiErrorResponseErrorHandler(clientTemplate.getErrorHandler())
203         );
204   }
205
206
207   static class SOME
208   {
209   }
210 }