Refined the test-cases and fixed a bug in GraphApiErrorResponseErrorHandler
[facebook-errors] / src / test / java / de / juplo / facebook / errors / GraphApiErrorResponseErrorHandlerIntegrationTest.java
diff --git a/src/test/java/de/juplo/facebook/errors/GraphApiErrorResponseErrorHandlerIntegrationTest.java b/src/test/java/de/juplo/facebook/errors/GraphApiErrorResponseErrorHandlerIntegrationTest.java
new file mode 100644 (file)
index 0000000..6593375
--- /dev/null
@@ -0,0 +1,205 @@
+package de.juplo.facebook.errors;
+
+
+import de.juplo.facebook.errors.GraphApiException.Type;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+import org.junit.Before;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.http.HttpStatus;
+import org.springframework.web.client.HttpClientErrorException;
+import org.springframework.web.client.RestTemplate;
+
+
+
+/**
+ *
+ * @author Kai Moritz
+ */
+public class GraphApiErrorResponseErrorHandlerIntegrationTest
+{
+  private static final Logger LOG =
+      LoggerFactory.getLogger(GraphApiErrorResponseErrorHandlerIntegrationTest.class);
+
+  private RestTemplate clientTemplate;
+  private MockClientHttpRequestFactory requestFactory;
+
+
+  @Test
+  public void testNoError()
+  {
+    LOG.info("testNoError");
+
+    requestFactory.setBody("{\n" +
+        "  \"error\": {\n" +
+        "    \"message\": \"(#200) The user hasn't authorized the application to perform this action\",\n" +
+        "    \"type\": \"OAuthException\",\n" +
+        "    \"code\": 200\n" +
+        "  }\n" +
+        "}");
+
+
+    requestFactory.setStatus(HttpStatus.CONTINUE);
+    try
+    {
+      clientTemplate.getForObject("ANY", SOME.class);
+    }
+    catch(Exception e)
+    {
+      LOG.debug("{}", e.toString());
+      fail("Unexpected error: " + e);
+    }
+
+    requestFactory.setStatus(HttpStatus.OK);
+    try
+    {
+      clientTemplate.getForObject("ANY", SOME.class);
+    }
+    catch(Exception e)
+    {
+      LOG.debug("{}", e.toString());
+      fail("Unexpected error: " + e);
+    }
+
+    requestFactory.setStatus(HttpStatus.TEMPORARY_REDIRECT);
+    try
+    {
+      clientTemplate.getForObject("ANY", SOME.class);
+    }
+    catch(Exception e)
+    {
+      LOG.debug("{}", e.toString());
+      fail("Unexpected error: " + e);
+    }
+
+    requestFactory.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
+    try
+    {
+      clientTemplate.getForObject("ANY", SOME.class);
+    }
+    catch(Exception e)
+    {
+      LOG.debug("{}", e.toString());
+      fail("Unexpected error: " + e);
+    }
+  }
+
+  @Test
+  public void testValidError()
+  {
+    LOG.info("testValidError");
+
+
+    requestFactory.setBody(
+        "{\n" +
+        "  \"error\":\n" +
+        "  {\n" +
+        "    \"message\": \"(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.\",\n" +
+        "    \"type\": \"OAuthException\",\n" +
+        "    \"code\": 613\n" +
+        "  }\n" +
+        "}");
+
+    try
+    {
+      clientTemplate.getForObject("ANY", SOME.class);
+      fail("The expected exception was not thrown");
+    }
+    catch(RateLimitExceededException e)
+    {
+      LOG.debug("{}", e.toString());
+      assertEquals(new Integer(613), e.getCode());
+      assertEquals("(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.", e.getMessage());
+      assertEquals(Type.OAuthException, e.getType());
+    }
+  }
+
+  @Test
+  public void testUnmappedError()
+  {
+    LOG.info("testUnmappedError");
+
+
+    requestFactory.setBody(
+        "{\n" +
+        "  \"error\":\n" +
+        "  {\n" +
+        "    \"message\": \"This error does not exist.\",\n" +
+        "    \"type\": \"NonexistentTypeException\",\n" +
+        "    \"code\": 999999999\n" +
+        "  }\n" +
+        "}");
+
+    try
+    {
+      clientTemplate.getForObject("ANY", SOME.class);
+      fail("The expected exception was not thrown");
+    }
+    catch(GraphApiException e)
+    {
+      LOG.debug("{}", e.toString());
+      assertEquals(new Integer(999999999), e.getCode());
+      assertEquals("This error does not exist.", e.getMessage());
+      try
+      {
+        Type type = e.getType();
+        LOG.error("unknown type: {}", type);
+        fail("unmapped type was resolved by enum: " + type);
+      }
+      catch (IllegalArgumentException ee) {}
+    }
+  }
+
+  @Test
+  public void testInvlalidError()
+  {
+    LOG.info("testInvalidError");
+
+
+    requestFactory.setBody(
+        "{\n" +
+        "  \"error\":\n" +
+        "  {\n" +
+        "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
+        "    \"type\": \"Whatever\",\n" +
+        "    \"code\": \"some string\"\n" +
+        "  }\n" +
+        "}");
+
+    try
+    {
+      clientTemplate.getForObject("ANY", SOME.class);
+      fail("The expected exception was not thrown");
+    }
+    catch(HttpClientErrorException e)
+    {
+      LOG.debug("{}", e.toString());
+    }
+    catch(Exception e)
+    {
+      fail("A wrong exception was thrown: " + e.toString());
+    }
+  }
+
+
+  @Before
+  public void setUp()
+  {
+    requestFactory = new MockClientHttpRequestFactory();
+    requestFactory.setStatus(HttpStatus.BAD_REQUEST);
+    requestFactory.addHeader("Content-Type", "application/json");
+
+    clientTemplate = new RestTemplate();
+    clientTemplate.setRequestFactory(requestFactory);
+    clientTemplate.setErrorHandler(
+        new GraphApiErrorResponseErrorHandler(clientTemplate.getErrorHandler())
+        );
+  }
+
+
+  static class SOME
+  {
+  }
+}