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