Refactoring: moved FacebookErrorMessage in its own file/class
[facebook-utils] / src / test / java / de / juplo / facebook / client / GraphApiErrorHandlerTest.java
index ddd19a6..c9921fa 100644 (file)
@@ -1,11 +1,15 @@
 package de.juplo.facebook.client;
 
+import de.juplo.facebook.exceptions.AccessTokenRequiredException;
+import de.juplo.facebook.exceptions.CallbackVerificationFailedException;
 import de.juplo.facebook.exceptions.UnsupportedGetRequestException;
 import de.juplo.facebook.exceptions.UnexpectedErrorException;
 import de.juplo.facebook.exceptions.RateExceededException;
 import de.juplo.facebook.exceptions.GraphApiException;
+import de.juplo.facebook.exceptions.GraphApiException.Type;
 import de.juplo.facebook.exceptions.UnknownErrorException;
 import de.juplo.facebook.exceptions.PageMigratedException;
+import de.juplo.facebook.exceptions.UnmappedErrorException;
 import java.util.Date;
 import java.util.Map;
 import java.util.Set;
@@ -79,9 +83,9 @@ public class GraphApiErrorHandlerTest
     {
       log.debug("{}", e.toString());
       assertEquals("invalid_request", e.getOAuth2ErrorCode());
-      assertEquals(1, e.getCode());
+      assertEquals(new Integer(1), e.getCode());
       assertEquals("An unknown error has occurred.", e.getMessage());
-      assertEquals("OAuthException", e.getType());
+      assertEquals(Type.OAuthException, e.getType());
     }
   }
 
@@ -110,9 +114,9 @@ public class GraphApiErrorHandlerTest
     {
       log.debug("{}", e.toString());
       assertEquals("invalid_request", e.getOAuth2ErrorCode());
-      assertEquals(2, e.getCode());
+      assertEquals(new Integer(2), e.getCode());
       assertEquals("An unexpected error has occurred. Please retry your request later.", e.getMessage());
-      assertEquals("OAuthException", e.getType());
+      assertEquals(Type.OAuthException, e.getType());
     }
   }
 
@@ -141,9 +145,9 @@ public class GraphApiErrorHandlerTest
     {
       log.debug("{}", e.toString());
       assertEquals("invalid_request", e.getOAuth2ErrorCode());
-      assertEquals(21, e.getCode());
+      assertEquals(new Integer(21), e.getCode());
       assertEquals("(#21) Page ID 590408587650316 was migrated to page ID 1421620791415603.  Please update your API calls to the new ID", e.getMessage());
-      assertEquals("OAuthException", e.getType());
+      assertEquals(Type.OAuthException, e.getType());
     }
   }
 
@@ -172,9 +176,32 @@ public class GraphApiErrorHandlerTest
     {
       log.debug("{}", e.toString());
       assertEquals("invalid_request", e.getOAuth2ErrorCode());
-      assertEquals(100, e.getCode());
+      assertEquals(new Integer(100), e.getCode());
       assertEquals("Unsupported get request.", e.getMessage());
-      assertEquals("GraphMethodException", e.getType());
+      assertEquals(Type.GraphMethodException, e.getType());
+    }
+  }
+
+  @Test
+  public void testError104()
+  {
+    log.info("testError104");
+
+    requestFactory.setBody("{\"error\":{\"message\":\"An access token is required to request this resource.\",\"type\":\"OAuthException\",\"code\":104,\"fbtrace_id\":\"E2Jjkj5++LL\"}}");
+
+    try
+    {
+      clientTemplate.getForObject("ANY", SOME.class);
+      fail("The expected exception was not thrown");
+    }
+    catch(AccessTokenRequiredException e)
+    {
+      log.debug("{}", e.toString());
+      assertEquals("invalid_request", e.getOAuth2ErrorCode());
+      assertEquals(new Integer(104), e.getCode());
+      assertEquals("An access token is required to request this resource.", e.getMessage());
+      assertEquals(Type.OAuthException, e.getType());
+      assertEquals("E2Jjkj5++LL", e.getTraceId());
     }
   }
 
@@ -203,9 +230,30 @@ public class GraphApiErrorHandlerTest
     {
       log.debug("{}", e.toString());
       assertEquals("invalid_request", e.getOAuth2ErrorCode());
-      assertEquals(613, e.getCode());
+      assertEquals(new Integer(613), e.getCode());
       assertEquals("(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.", e.getMessage());
-      assertEquals("OAuthException", e.getType());
+      assertEquals(Type.OAuthException, e.getType());
+    }
+  }
+
+  @Test
+  public void testError2200()
+  {
+    requestFactory.setBody("{\"error\":{\"message\":\"(#2200) callback verification failed: \",\"type\":\"OAuthException\",\"code\":2200,\"fbtrace_id\":\"ESLjoZKvPXg\"}}");
+    
+    try
+    {
+      clientTemplate.getForObject("ANY", SOME.class);
+      fail("The expected exception was not thrown");
+    }
+    catch(CallbackVerificationFailedException e)
+    {
+      log.debug("{}", e.toString());
+      assertEquals("invalid_request", e.getOAuth2ErrorCode());
+      assertEquals(new Integer(2200), e.getCode());
+      assertEquals("(#2200) callback verification failed: ", e.getMessage());
+      assertEquals(Type.OAuthException, e.getType());
+      assertEquals("ESLjoZKvPXg", e.getTraceId());
     }
   }
 
@@ -220,7 +268,7 @@ public class GraphApiErrorHandlerTest
         "  \"error\":\n" +
         "  {\n" +
         "    \"message\": \"This error does not exist.\",\n" +
-        "    \"type\": \"NonexistentException\",\n" +
+        "    \"type\": \"NonexistentTypeException\",\n" +
         "    \"code\": 999999999\n" +
         "  }\n" +
         "}");
@@ -234,16 +282,22 @@ public class GraphApiErrorHandlerTest
     {
       log.debug("{}", e.toString());
       assertEquals("invalid_request", e.getOAuth2ErrorCode());
-      assertEquals(999999999, e.getCode());
+      assertEquals(new Integer(999999999), e.getCode());
       assertEquals("This error does not exist.", e.getMessage());
-      assertEquals("NonexistentException", e.getType());
+      try
+      {
+        Type type = e.getType();
+        log.error("unknown type: {}", type);
+        fail("unmapped type was resolved by enum: " + type);
+      }
+      catch (IllegalArgumentException ee) {}
     }
   }
 
   @Test
-  public void testInvlalidErrors()
+  public void testUnmappedErrors()
   {
-    log.info("testInvalidErrors");
+    log.info("testUnmappedErrors");
 
 
     requestFactory.setBody(
@@ -251,7 +305,7 @@ public class GraphApiErrorHandlerTest
         "  \"error\":\n" +
         "  {\n" +
         "    \"message\": null,\n" +
-        "    \"type\": \"Whatever\",\n" +
+        "    \"type\": \"WhateverTypeException\",\n" +
         "    \"code\": 999999999\n" +
         "  }\n" +
         "}");
@@ -261,9 +315,22 @@ public class GraphApiErrorHandlerTest
       clientTemplate.getForObject("ANY", SOME.class);
       fail("The expected exception was not thrown");
     }
-    catch(HttpClientErrorException e)
+    catch(UnmappedErrorException e)
     {
       log.debug("{}", e.toString());
+      assertNull(e.getMessage());
+      try
+      {
+        Type type = e.getType();
+        log.error("unknown type: {}", type);
+        fail("unmapped type was resolved by enum: " + type);
+      }
+      catch (IllegalArgumentException ee) {}
+      assertEquals(new Integer(999999999), e.getCode());
+      assertNull(e.getSubCode());
+      assertNull(e.getUserTitle());
+      assertNull(e.getUserMessage());
+      assertNull(e.getTraceId());
     }
     catch(Exception e)
     {
@@ -275,7 +342,7 @@ public class GraphApiErrorHandlerTest
         "{\n" +
         "  \"error\":\n" +
         "  {\n" +
-        "    \"type\": \"Whatever\",\n" +
+        "    \"type\": \"WhateverTypeException\",\n" +
         "    \"code\": 999999999\n" +
         "  }\n" +
         "}");
@@ -285,9 +352,22 @@ public class GraphApiErrorHandlerTest
       clientTemplate.getForObject("ANY", SOME.class);
       fail("The expected exception was not thrown");
     }
-    catch(HttpClientErrorException e)
+    catch(UnmappedErrorException e)
     {
       log.debug("{}", e.toString());
+      assertNull(e.getMessage());
+      try
+      {
+        Type type = e.getType();
+        log.error("unknown type: {}", type);
+        fail("unmapped type was resolved by enum: " + type);
+      }
+      catch (IllegalArgumentException ee) {}
+      assertEquals(new Integer(999999999), e.getCode());
+      assertNull(e.getSubCode());
+      assertNull(e.getUserTitle());
+      assertNull(e.getUserMessage());
+      assertNull(e.getTraceId());
     }
     catch(Exception e)
     {
@@ -299,7 +379,7 @@ public class GraphApiErrorHandlerTest
         "{\n" +
         "  \"error\":\n" +
         "  {\n" +
-        "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
+        "    \"message\": \"An unmapped Graph-API-Exception.\",\n" +
         "    \"type\": null,\n" +
         "    \"code\": 999999999\n" +
         "  }\n" +
@@ -310,9 +390,16 @@ public class GraphApiErrorHandlerTest
       clientTemplate.getForObject("ANY", SOME.class);
       fail("The expected exception was not thrown");
     }
-    catch(HttpClientErrorException e)
+    catch(UnmappedErrorException e)
     {
       log.debug("{}", e.toString());
+      assertEquals("An unmapped Graph-API-Exception.", e.getMessage());
+      assertNull(e.getType());
+      assertEquals(new Integer(999999999), e.getCode());
+      assertNull(e.getSubCode());
+      assertNull(e.getUserTitle());
+      assertNull(e.getUserMessage());
+      assertNull(e.getTraceId());
     }
     catch(Exception e)
     {
@@ -324,7 +411,7 @@ public class GraphApiErrorHandlerTest
         "{\n" +
         "  \"error\":\n" +
         "  {\n" +
-        "    \"message\": \"Not a Graph-Api-Exception.\",\n" +
+        "    \"message\": \"An unmapped Graph-API-Exception.\",\n" +
         "    \"code\": 999999999\n" +
         "  }\n" +
         "}");
@@ -334,14 +421,27 @@ public class GraphApiErrorHandlerTest
       clientTemplate.getForObject("ANY", SOME.class);
       fail("The expected exception was not thrown");
     }
-    catch(HttpClientErrorException e)
+    catch(UnmappedErrorException e)
     {
       log.debug("{}", e.toString());
+      assertEquals("An unmapped Graph-API-Exception.", e.getMessage());
+      assertNull(e.getType());
+      assertEquals(new Integer(999999999), e.getCode());
+      assertNull(e.getSubCode());
+      assertNull(e.getUserTitle());
+      assertNull(e.getUserMessage());
+      assertNull(e.getTraceId());
     }
     catch(Exception e)
     {
       fail("A wrong exception was thrown: " + e.toString());
     }
+  }
+
+  @Test
+  public void testInvlalidErrors()
+  {
+    log.info("testInvalidErrors");
 
 
     requestFactory.setBody(