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