Removed redundant logging-messages in test-cases
[facebook-errors] / src / test / java / de / juplo / facebook / errors / GraphApiExceptionTest.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.assertNull;
7 import static org.junit.Assert.assertTrue;
8 import static org.junit.Assert.fail;
9 import org.junit.Test;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12 import org.springframework.http.HttpHeaders;
13 import org.springframework.http.HttpStatus;
14
15
16
17 /**
18  *
19  * @author Kai Moritz
20  */
21 public class GraphApiExceptionTest
22 {
23   private static final Logger LOG =
24       LoggerFactory.getLogger(GraphApiExceptionTest.class);
25
26   private HttpStatus status = HttpStatus.I_AM_A_TEAPOT;
27   private HttpHeaders headers = HttpHeaders.EMPTY;
28
29
30   @Test
31   public void testError1()
32   {
33     String str =
34         "{\n" +
35         "  \"error\":\n" +
36         "  {\n" +
37         "    \"message\": \"An unknown error has occurred.\",\n" +
38         "    \"type\": \"OAuthException\",\n" +
39         "    \"code\": 1\n" +
40         "  }\n" +
41         "}";
42     byte[] message = str.getBytes();
43     GraphApiException e = GraphApiException.create (status, headers, message);
44
45     assertTrue(e.getClass().equals(UnknownErrorException.class));
46     LOG.debug("{}", e.toString());
47   }
48
49   @Test
50   public void testError2()
51   {
52     String str =
53         "{\n" +
54         "  \"error\":\n" +
55         "  {\n" +
56         "    \"message\": \"An unexpected error has occurred. Please retry your request later.\",\n" +
57         "    \"type\": \"OAuthException\",\n" +
58         "    \"code\": 2\n" +
59         "  }\n" +
60         "}";
61     byte[] message = str.getBytes();
62     GraphApiException e = GraphApiException.create (status, headers, message);
63
64     assertTrue(e.getClass().equals(UnexpectedErrorException.class));
65     LOG.debug("{}", e.toString());
66     assertEquals(new Integer(2), e.getCode());
67     assertEquals("An unexpected error has occurred. Please retry your request later.", e.getMessage());
68     assertEquals(Type.OAuthException, e.getType());
69   }
70
71   @Test
72   public void testError4()
73   {
74     String str =
75         "{\n" +
76         "    \"error\": {\n" +
77         "        \"code\": 4, \n" +
78         "        \"fbtrace_id\": \"HZRM6BTMu+D\", \n" +
79         "        \"is_transient\": true, \n" +
80         "        \"message\": \"(#4) Application request limit reached\", \n" +
81         "        \"type\": \"OAuthException\"\n" +
82         "    }\n" +
83         "}\n";
84     byte[] message = str.getBytes();
85     GraphApiException e = GraphApiException.create (status, headers, message);
86
87     assertTrue(e.getClass().equals(ApplicationRequestLimitReachedException.class));
88     LOG.debug("{}", e.toString());
89     assertEquals(new Integer(4), e.getCode());
90     assertEquals("(#4) Application request limit reached", e.getMessage());
91     assertEquals(Type.OAuthException, e.getType());
92   }
93
94   @Test
95   public void testError12()
96   {
97     String str =
98         "{\n" +
99         "  \"error\":\n" +
100         "  {\n" +
101         "    \"message\": \"(#12) location field is deprecated for versions v2.5 and higher\",\n" +
102         "    \"type\": \"OAuthException\",\n" +
103         "    \"code\": 12\n," +
104         "    \"fbtrace_id\":\"BoxCYne7GrL\"\n" +
105         "  }\n" +
106         "}";
107     byte[] message = str.getBytes();
108     GraphApiException e = GraphApiException.create (status, headers, message);
109
110     assertTrue(e.getClass().equals(DeprecatedException.class));
111     LOG.debug("{}", e.toString());
112     assertEquals(new Integer(12), e.getCode());
113     assertEquals("(#12) location field is deprecated for versions v2.5 and higher", e.getMessage());
114     assertEquals(Type.OAuthException, e.getType());
115   }
116
117   @Test
118   public void testError21()
119   {
120     String str =
121         "{\n" +
122         "  \"error\":\n" +
123         "  {\n" +
124         "    \"message\": \"(#21) Page ID 590408587650316 was migrated to page ID 1421620791415603.  Please update your API calls to the new ID\",\n" +
125         "    \"type\": \"OAuthException\",\n" +
126         "    \"code\": 21\n" +
127         "  }\n" +
128         "}";
129     byte[] message = str.getBytes();
130     GraphApiException e = GraphApiException.create (status, headers, message);
131
132     assertTrue(e.getClass().equals(PageMigratedException.class));
133     LOG.debug("{}", e.toString());
134     assertEquals(new Integer(21), e.getCode());
135     assertEquals("(#21) Page ID 590408587650316 was migrated to page ID 1421620791415603.  Please update your API calls to the new ID", e.getMessage());
136     assertEquals(Type.OAuthException, e.getType());
137   }
138
139   @Test
140   public void testError100()
141   {
142     String str =
143         "{\n" +
144         "  \"error\":\n" +
145         "  {\n" +
146         "    \"message\": \"Unsupported get request.\",\n" +
147         "    \"type\": \"GraphMethodException\",\n" +
148         "    \"code\": 100\n" +
149         "  }\n" +
150         "}";
151     byte[] message = str.getBytes();
152     GraphApiException e = GraphApiException.create (status, headers, message);
153
154
155     assertTrue(e.getClass().equals(UnsupportedGetRequestException.class));
156     LOG.debug("{}", e.toString());
157     assertEquals(new Integer(100), e.getCode());
158     assertEquals("Unsupported get request.", e.getMessage());
159     assertEquals(Type.GraphMethodException, e.getType());
160   }
161
162   @Test
163   public void testError102()
164   {
165     String str ="{\"error\":{\"message\":\"A user access token is required to request this resource.\",\"type\":\"OAuthException\",\"code\":102,\"fbtrace_id\":\"DhdMyf23Ki7\"}}";
166     byte[] message = str.getBytes();
167     GraphApiException e = GraphApiException.create (status, headers, message);
168
169
170     assertTrue(e.getClass().equals(UserAccessTokenRequiredException.class));
171     LOG.debug("{}", e.toString());
172   }
173
174   @Test
175   public void testError104()
176   {
177     String str ="{\"error\":{\"message\":\"An access token is required to request this resource.\",\"type\":\"OAuthException\",\"code\":104,\"fbtrace_id\":\"E2Jjkj5++LL\"}}";
178     byte[] message = str.getBytes();
179     GraphApiException e = GraphApiException.create (status, headers, message);
180
181     assertTrue(e.getClass().equals(AccessTokenRequiredException.class));
182     LOG.debug("{}", e.toString());
183   }
184
185   @Test
186   public void testError190()
187   {
188     LOG.info("testError190");
189
190     String str ="{\"error\":{\"message\":\"Bad signature\",\"type\":\"OAuthException\",\"code\":190,\"fbtrace_id\":\"Ay2OYQrINbXOCfQpBvoxDIw\"}}";
191     byte[] message = str.getBytes();
192     GraphApiException e = GraphApiException.create (status, headers, message);
193
194     assertTrue(e.getClass().equals(AccessTokenExpiredException.class));
195     LOG.debug("{}", e.toString());
196   }
197
198   @Test
199   public void testError200()
200   {
201     String str ="{\n" +
202         "  \"error\": {\n" +
203         "    \"message\": \"(#200) The user hasn't authorized the application to perform this action\",\n" +
204         "    \"type\": \"OAuthException\",\n" +
205         "    \"code\": 200\n" +
206         "  }\n" +
207         "}";
208     byte[] message = str.getBytes();
209     GraphApiException e = GraphApiException.create (status, headers, message);
210
211     assertTrue(e.getClass().equals(ApplicationNotAuthorizedByUserException.class));
212     LOG.debug("{}", e.toString());
213   }
214
215   @Test
216   public void testError613()
217   {
218     String str =
219         "{\n" +
220         "  \"error\":\n" +
221         "  {\n" +
222         "    \"message\": \"(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.\",\n" +
223         "    \"type\": \"OAuthException\",\n" +
224         "    \"code\": 613\n" +
225         "  }\n" +
226         "}";
227     byte[] message = str.getBytes();
228     GraphApiException e = GraphApiException.create (status, headers, message);
229
230     assertTrue(e.getClass().equals(RateLimitExceededException.class));
231     LOG.debug("{}", e.toString());
232     assertEquals(new Integer(613), e.getCode());
233     assertEquals("(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.", e.getMessage());
234     assertEquals(Type.OAuthException, e.getType());
235   }
236
237   @Test
238   public void testError2200()
239   {
240     String str ="{\"error\":{\"message\":\"(#2200) callback verification failed: \",\"type\":\"OAuthException\",\"code\":2200,\"fbtrace_id\":\"ESLjoZKvPXg\"}}";
241     byte[] message = str.getBytes();
242     GraphApiException e = GraphApiException.create (status, headers, message);
243
244     assertTrue(e.getClass().equals(CallbackVerificationFailedException.class));
245     LOG.debug("{}", e.toString());
246   }
247
248   @Test
249   public void testUnmappedError()
250   {
251     String str =
252         "{\n" +
253         "  \"error\":\n" +
254         "  {\n" +
255         "    \"message\": \"This error does not exist.\",\n" +
256         "    \"type\": \"NonexistentTypeException\",\n" +
257         "    \"code\": 999999999\n" +
258         "  }\n" +
259         "}";
260     byte[] message = str.getBytes();
261     GraphApiException e = GraphApiException.create (status, headers, message);
262
263     assertTrue(e.getClass().equals(UnmappedErrorException.class));
264     LOG.debug("{}", e.toString());
265     assertEquals(new Integer(999999999), e.getCode());
266     assertEquals("This error does not exist.", e.getMessage());
267     try
268     {
269       Type type = e.getType();
270       LOG.error("unknown type: {}", type);
271       fail("unmapped type was resolved by enum: " + type);
272     }
273     catch (IllegalArgumentException ee) {}
274   }
275
276   @Test
277   public void testUnmappedErrors()
278   {
279     String str;
280     byte[] message;
281     GraphApiException e;
282
283
284     str =
285         "{\n" +
286         "  \"error\":\n" +
287         "  {\n" +
288         "    \"message\": null,\n" +
289         "    \"type\": \"WhateverTypeException\",\n" +
290         "    \"code\": 999999999\n" +
291         "  }\n" +
292         "}";
293     message = str.getBytes();
294     e = GraphApiException.create (status, headers, message);
295
296     assertTrue(e.getClass().equals(UnmappedErrorException.class));
297     LOG.debug("{}", e.toString());
298     assertNull(e.getMessage());
299     try
300     {
301       Type type = e.getType();
302       LOG.error("unknown type: {}", type);
303       fail("unmapped type was resolved by enum: " + type);
304     }
305     catch (IllegalArgumentException ee) {}
306     assertEquals(new Integer(999999999), e.getCode());
307     assertNull(e.getSubCode());
308     assertNull(e.getUserTitle());
309     assertNull(e.getUserMessage());
310     assertNull(e.getTraceId());
311
312
313     str =
314         "{\n" +
315         "  \"error\":\n" +
316         "  {\n" +
317         "    \"type\": \"WhateverTypeException\",\n" +
318         "    \"code\": 999999999\n" +
319         "  }\n" +
320         "}";
321     message = str.getBytes();
322     e = GraphApiException.create (status, headers, message);
323
324     assertTrue(e.getClass().equals(UnmappedErrorException.class));
325     LOG.debug("{}", e.toString());
326     assertNull(e.getMessage());
327     try
328     {
329       Type type = e.getType();
330       LOG.error("unknown type: {}", type);
331       fail("unmapped type was resolved by enum: " + type);
332     }
333     catch (IllegalArgumentException ee) {}
334     assertEquals(new Integer(999999999), e.getCode());
335     assertNull(e.getSubCode());
336     assertNull(e.getUserTitle());
337     assertNull(e.getUserMessage());
338     assertNull(e.getTraceId());
339
340
341     str =
342         "{\n" +
343         "  \"error\":\n" +
344         "  {\n" +
345         "    \"message\": \"An unmapped Graph-API-Exception.\",\n" +
346         "    \"type\": null,\n" +
347         "    \"code\": 999999999\n" +
348         "  }\n" +
349         "}";
350     message = str.getBytes();
351     e = GraphApiException.create (status, headers, message);
352
353     assertTrue(e.getClass().equals(UnmappedErrorException.class));
354     LOG.debug("{}", e.toString());
355     assertEquals("An unmapped Graph-API-Exception.", e.getMessage());
356     assertNull(e.getType());
357     assertEquals(new Integer(999999999), e.getCode());
358     assertNull(e.getSubCode());
359     assertNull(e.getUserTitle());
360     assertNull(e.getUserMessage());
361     assertNull(e.getTraceId());
362
363
364     str =
365         "{\n" +
366         "  \"error\":\n" +
367         "  {\n" +
368         "    \"message\": \"An unmapped Graph-API-Exception.\",\n" +
369         "    \"code\": 999999999\n" +
370         "  }\n" +
371         "}";
372     message = str.getBytes();
373     e = GraphApiException.create (status, headers, message);
374
375     assertTrue(e.getClass().equals(UnmappedErrorException.class));
376     LOG.debug("{}", e.toString());
377     assertEquals("An unmapped Graph-API-Exception.", e.getMessage());
378     assertNull(e.getType());
379     assertEquals(new Integer(999999999), e.getCode());
380     assertNull(e.getSubCode());
381     assertNull(e.getUserTitle());
382     assertNull(e.getUserMessage());
383     assertNull(e.getTraceId());
384   }
385
386   @Test
387   public void testInvlalidErrors()
388   {
389     String str;
390     byte[] message;
391     GraphApiException e;
392
393
394     str =
395         "{\n" +
396         "  \"error\":\n" +
397         "  {\n" +
398         "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
399         "    \"type\": \"Whatever\",\n" +
400         "    \"code\": \"some string\"\n" +
401         "  }\n" +
402         "}";
403     message = str.getBytes();
404     e = GraphApiException.create (status, headers, message);
405     assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
406     LOG.debug("{}", e.toString());
407
408
409     str =
410         "{\n" +
411         "  \"error\":\n" +
412         "  {\n" +
413         "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
414         "    \"type\": \"Whatever\",\n" +
415         "    \"code\": 9.9\n" +
416         "  }\n" +
417         "}";
418     message = str.getBytes();
419     e = GraphApiException.create (status, headers, message);
420     assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
421     LOG.debug("{}", e.toString());
422
423
424     str =
425         "{\n" +
426         "  \"error\":\n" +
427         "  {\n" +
428         "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
429         "    \"type\": \"Whatever\",\n" +
430         "    \"code\": null\n" +
431         "  }\n" +
432         "}";
433     message = str.getBytes();
434     e = GraphApiException.create (status, headers, message);
435     assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
436     LOG.debug("{}", e.toString());
437
438
439     str =
440         "{\n" +
441         "  \"error\":\n" +
442         "  {\n" +
443         "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
444         "    \"type\": \"Whatever\"\n" +
445         "  }\n" +
446         "}";
447     message = str.getBytes();
448     e = GraphApiException.create (status, headers, message);
449     assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
450     LOG.debug("{}", e.toString());
451
452
453     str ="{\"error\":{\"message\":null}}";
454     message = str.getBytes();
455     e = GraphApiException.create (status, headers, message);
456     assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
457     LOG.debug("{}", e.toString());
458
459
460     str ="{\"error\":{\"type\":null}}";
461     message = str.getBytes();
462     e = GraphApiException.create (status, headers, message);
463     assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
464     LOG.debug("{}", e.toString());
465
466
467     str ="{\"error\":{\"code\":null}}";
468     message = str.getBytes();
469     e = GraphApiException.create (status, headers, message);
470     assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
471     LOG.debug("{}", e.toString());
472
473
474     str ="{\"error\":{}}";
475     message = str.getBytes();
476     e = GraphApiException.create (status, headers, message);
477     assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
478     LOG.debug("{}", e.toString());
479
480
481     str ="{\"error\":\"some message\"}";
482     message = str.getBytes();
483     e = GraphApiException.create (status, headers, message);
484     assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
485     LOG.debug("{}", e.toString());
486
487
488     str ="{\"error\":null}";
489     message = str.getBytes();
490     e = GraphApiException.create (status, headers, message);
491     assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
492     LOG.debug("{}", e.toString());
493
494
495     str ="{\"some filed\":\"some message\"}";
496     message = str.getBytes();
497     e = GraphApiException.create (status, headers, message);
498     assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
499     LOG.debug("{}", e.toString());
500
501
502     str ="{}";
503     message = str.getBytes();
504     e = GraphApiException.create (status, headers, message);
505     assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
506     LOG.debug("{}", e.toString());
507
508
509     str ="";
510     message = str.getBytes();
511     e = GraphApiException.create (status, headers, message);
512     assertTrue(e.getClass().equals(ErrorResponseParsingErrorException.class));
513     LOG.debug("{}", e.toString());
514   }
515 }