camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From acosent...@apache.org
Subject [1/3] camel git commit: Fix facebook tests to support newly created tokens
Date Tue, 15 Sep 2015 19:14:56 GMT
Repository: camel
Updated Branches:
  refs/heads/master ea53dce2d -> cf5a33334


Fix facebook tests to support newly created tokens

With new facebook application tokens some API methods are deprecated and
cause the tests to fail. These deprecation errors need to be ignored
in the tests


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/36b7d018
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/36b7d018
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/36b7d018

Branch: refs/heads/master
Commit: 36b7d018d1023e8f34ba76d7d8189edb22e9ecb0
Parents: 8f38e02
Author: Manuel Holzleitner <manuel.holzleitner@gmail.com>
Authored: Tue Sep 15 14:06:11 2015 +0200
Committer: Andrea Cosentino <ancosen@gmail.com>
Committed: Tue Sep 15 21:08:57 2015 +0200

----------------------------------------------------------------------
 .../facebook/FacebookComponentConsumerTest.java | 50 +++++++++---
 .../facebook/FacebookComponentProducerTest.java | 80 +++++++++++++++++---
 2 files changed, 110 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/36b7d018/components/camel-facebook/src/test/java/org/apache/camel/component/facebook/FacebookComponentConsumerTest.java
----------------------------------------------------------------------
diff --git a/components/camel-facebook/src/test/java/org/apache/camel/component/facebook/FacebookComponentConsumerTest.java
b/components/camel-facebook/src/test/java/org/apache/camel/component/facebook/FacebookComponentConsumerTest.java
index 2bb84db..1e78bc2 100644
--- a/components/camel-facebook/src/test/java/org/apache/camel/component/facebook/FacebookComponentConsumerTest.java
+++ b/components/camel-facebook/src/test/java/org/apache/camel/component/facebook/FacebookComponentConsumerTest.java
@@ -18,19 +18,20 @@ package org.apache.camel.component.facebook;
 
 import java.lang.reflect.Method;
 import java.text.SimpleDateFormat;
-import java.util.Arrays;
-import java.util.Date;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
+import java.util.*;
 import java.util.concurrent.TimeUnit;
 
+import facebook4j.FacebookException;
 import facebook4j.api.SearchMethods;
+import org.apache.camel.*;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.DefaultPollingConsumerPollStrategy;
+import org.apache.camel.impl.ScheduledPollConsumer;
 import org.junit.Test;
 
 public class FacebookComponentConsumerTest extends CamelFacebookTestSupport {
+    public static final String APACHE_FOUNDATION_PAGE_ID = "6538157161";
 
     private final Set<String> searchNames = new HashSet<String>();
     private List<String> excludedNames;
@@ -44,7 +45,7 @@ public class FacebookComponentConsumerTest extends CamelFacebookTestSupport
{
             }
         }
 
-        excludedNames = Arrays.asList("places", "users", "search");
+        excludedNames = Arrays.asList("places", "users", "search", "pages", "searchPosts");
     }
 
     @Test
@@ -75,17 +76,44 @@ public class FacebookComponentConsumerTest extends CamelFacebookTestSupport
{
     }
 
     @Test
-    public void testGetPosts() throws Exception {
-        final MockEndpoint mock = getMockEndpoint("mock:testGetPosts");
+    public void testPage() throws Exception {
+        final MockEndpoint mock = getMockEndpoint("mock:testPage");
         mock.expectedMinimumMessageCount(1);
         mock.assertIsSatisfied();
     }
 
     @Override
+    protected void doPostSetup() throws Exception {
+        ignoreDeprecatedApiError();
+    }
+
+    private void ignoreDeprecatedApiError() {
+        for (final Route route : context().getRoutes()) {
+            ((ScheduledPollConsumer)route.getConsumer()).setPollStrategy(new DefaultPollingConsumerPollStrategy()
{
+                @Override
+                public boolean rollback(Consumer consumer, Endpoint endpoint, int retryCounter,
Exception e) throws Exception {
+                    if (e.getCause() instanceof FacebookException) {
+                        FacebookException facebookException = (FacebookException) e.getCause();
+                        if (facebookException.getErrorCode() == 11 || facebookException.getErrorCode()
== 12 || facebookException.getErrorCode() == 1) {
+                            context().stopRoute(route.getId());
+                            String method = ((FacebookEndpoint) route.getEndpoint()).getMethod();
+                            MockEndpoint mock = getMockEndpoint("mock:consumeQueryResult"
+ method);
+                            mock.expectedMinimumMessageCount(0);
+                            MockEndpoint mock2 = getMockEndpoint("mock:consumeResult" + method);
+                            mock2.expectedMinimumMessageCount(0);
+                            log.warn("Ignoring failed Facebook deprecated API call", facebookException);
+                        }
+                    }
+                    return super.rollback(consumer, endpoint, retryCounter, e);
+                }
+            });
+        }
+    }
+
+    @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
             public void configure() throws Exception {
-
                 // start with a 30 day window for the first delayed poll
                 String since = "RAW(" + new SimpleDateFormat(FacebookConstants.FACEBOOK_DATE_FORMAT).format(
                     new Date(System.currentTimeMillis() - TimeUnit.MILLISECONDS.convert(30,
TimeUnit.DAYS))) + ")";
@@ -110,8 +138,8 @@ public class FacebookComponentConsumerTest extends CamelFacebookTestSupport
{
                 // test unix timestamp support
                 long unixSince =  TimeUnit.SECONDS.convert(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
                     - TimeUnit.SECONDS.convert(30, TimeUnit.DAYS);
-                from("facebook://posts?reading.limit=10&reading.since=" + unixSince +
"&" + getOauthParams())
-                        .to("mock:testGetPosts");
+                from("facebook://page?pageId=" + APACHE_FOUNDATION_PAGE_ID + "&reading.limit=10&reading.since="
+ unixSince + "&" + getOauthParams())
+                        .to("mock:testPage");
 
                 // TODO add tests for the rest of the supported methods
             }

http://git-wip-us.apache.org/repos/asf/camel/blob/36b7d018/components/camel-facebook/src/test/java/org/apache/camel/component/facebook/FacebookComponentProducerTest.java
----------------------------------------------------------------------
diff --git a/components/camel-facebook/src/test/java/org/apache/camel/component/facebook/FacebookComponentProducerTest.java
b/components/camel-facebook/src/test/java/org/apache/camel/component/facebook/FacebookComponentProducerTest.java
index 613d814..753f699 100644
--- a/components/camel-facebook/src/test/java/org/apache/camel/component/facebook/FacebookComponentProducerTest.java
+++ b/components/camel-facebook/src/test/java/org/apache/camel/component/facebook/FacebookComponentProducerTest.java
@@ -17,14 +17,23 @@
 package org.apache.camel.component.facebook;
 
 import java.lang.reflect.Method;
-import java.util.Arrays;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
+import java.util.*;
 
 import facebook4j.Facebook;
+import facebook4j.FacebookException;
+import org.apache.camel.Exchange;
+import org.apache.camel.Predicate;
+import org.apache.camel.Processor;
+import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.builder.DefaultErrorHandlerBuilder;
+import org.apache.camel.builder.ErrorHandlerBuilder;
 import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.facebook.data.FacebookMethodsType;
 import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.model.OnExceptionDefinition;
+import org.apache.camel.processor.ErrorHandler;
+import org.apache.camel.processor.exceptionpolicy.ExceptionPolicyStrategy;
+import org.apache.camel.spi.RouteContext;
 import org.junit.Test;
 
 public class FacebookComponentProducerTest extends CamelFacebookTestSupport {
@@ -40,15 +49,29 @@ public class FacebookComponentProducerTest extends CamelFacebookTestSupport
{
             if (clazzName.endsWith("Methods") && !clazzName.equals("GameMethods"))
{
                 for (Method method : clazz.getDeclaredMethods()) {
                     // find all the no-arg methods
-                    if (method.getParameterTypes().length == 0) {
-                        noArgNames.add(getShortName(method.getName()));
+                    if (method.getParameterTypes().length == 0 && FacebookMethodsType.findMethod(method.getName())
!= null) {
+                        String shortName = getShortName(method.getName());
+                        List<String> generalExcludes = Arrays.asList("home", "tabs",
"updates", "blocked", "pageSettings", "pageAdmins",
+                            "milestones", "offers", "pokes", "promotablePosts", "outbox",
"inbox", "notifications");
+                        if (!generalExcludes.contains(shortName)) {
+                            noArgNames.add(shortName);
+                        }
                     }
                 }
             }
         }
 
-        idExcludes = Arrays.asList("me", "home", "searchCheckins");
-        readingExcludes = Arrays.asList("pictureURL", "permissions");
+        idExcludes = new ArrayList<String>();
+        idExcludes.addAll(Arrays.asList("me", "home", "searchCheckins", "taggableFriends"));
+        readingExcludes = new ArrayList<String>();
+        readingExcludes.addAll(Arrays.asList("pictureURL", "permissions", "taggableFriends",
"sSLPictureURL"));
+
+        for (FacebookMethodsType types : FacebookMethodsType.values()) {
+            if(types.getArgNames().contains("pageId")) {
+                idExcludes.add(getShortName(types.getName()));
+                readingExcludes.add(getShortName(types.getName()));
+            }
+        }
     }
 
     @Test
@@ -78,6 +101,13 @@ public class FacebookComponentProducerTest extends CamelFacebookTestSupport
{
                 mock.expectedMinimumMessageCount(1);
                 template().sendBody("direct://testIdReading" + name, null);
             }
+
+            // with user id and reading
+            if (!(idExcludes.contains(name) || readingExcludes.contains(name))) {
+                mock = getMockEndpoint("mock:resultIdReadingHeader" + name);
+                mock.expectedMinimumMessageCount(1);
+                template().sendBody("direct://testIdReadingHeader" + name, null);
+            }
         }
 
         assertMockEndpointsSatisfied();
@@ -94,19 +124,23 @@ public class FacebookComponentProducerTest extends CamelFacebookTestSupport
{
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
             public void configure() {
-
+                // Deprecated exceptions are ignored in the tests since this depends on the
+                // configuration and/or creation date of the Facebook application.
+                onException(FacebookException.class).setHandledPolicy(new IgnoreDeprecatedExceptions());
                 //---------------
                 // producer tests
                 //---------------
                 // generate test routes for all methods with no args
                 for (String name : noArgNames) {
                     from("direct://test" + name)
+                        .setHeader("mock", constant("mock:result" + name))
                         .to("facebook://" + name + "?" + getOauthParams())
                         .to("mock:result" + name);
 
                     // with user id
                     if (!idExcludes.contains(name)) {
                         from("direct://testId" + name)
+                            .setHeader("mock", constant("mock:resultId" + name))
                             .to("facebook://" + name + "?userId=me&" + getOauthParams())
                             .to("mock:resultId" + name);
                     }
@@ -114,6 +148,7 @@ public class FacebookComponentProducerTest extends CamelFacebookTestSupport
{
                     // reading options
                     if (!readingExcludes.contains(name)) {
                         from("direct://testReading" + name)
+                            .setHeader("mock", constant("mock:resultReading" + name))
                             .to("facebook://" + name + "?reading.limit=10&reading.locale=en,US&"
+ getOauthParams())
                             .to("mock:resultReading" + name);
                     }
@@ -121,9 +156,19 @@ public class FacebookComponentProducerTest extends CamelFacebookTestSupport
{
                     // with id and reading options
                     if (!(idExcludes.contains(name) || readingExcludes.contains(name))) {
                         from("direct://testIdReading" + name)
+                            .setHeader("mock", constant("mock:resultIdReading" + name))
                             .to("facebook://" + name + "?userId=me&reading.limit=10&reading.locale=en,US&"
+ getOauthParams())
                             .to("mock:resultIdReading" + name);
                     }
+
+                    // with id and reading options
+                    if (!(idExcludes.contains(name) || readingExcludes.contains(name))) {
+                        from("direct://testIdReadingHeader" + name)
+                            .setHeader("mock", constant("mock:resultIdReadingHeader" + name))
+                            .setHeader("CamelFacebook.reading.limit", constant("10"))
+                            .to("facebook://" + name + "?userId=me&reading.locale=en,US&"
+ getOauthParams())
+                            .to("mock:resultIdReadingHeader" + name);
+                    }
                 }
 
                 from("direct://testJsonStoreEnabled")
@@ -135,4 +180,21 @@ public class FacebookComponentProducerTest extends CamelFacebookTestSupport
{
         };
     }
 
+    private class IgnoreDeprecatedExceptions implements Predicate {
+        @Override
+        public boolean matches(Exchange exchange) {
+            RuntimeCamelException camelException = exchange.getProperty(Exchange.EXCEPTION_CAUGHT,
RuntimeCamelException.class);
+            if (camelException != null
+                && camelException.getCause() != null
+                && camelException.getCause() instanceof FacebookException) {
+                FacebookException facebookException = (FacebookException) camelException.getCause();
+                if (facebookException.getErrorCode() == 11 || facebookException.getErrorCode()
== 12) {
+                    getMockEndpoint(exchange.getIn().getHeader("mock", String.class)).expectedMinimumMessageCount(0);
+
+                    return true;
+                }
+            }
+            return false;
+        }
+    }
 }


Mime
View raw message