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