camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jans...@apache.org
Subject git commit: CAMEL-6662 - camel-yammer - don't share requestor between different endpoint types
Date Thu, 22 Aug 2013 16:06:51 GMT
Updated Branches:
  refs/heads/master d8c482277 -> bb4f2e5b5


CAMEL-6662 - camel-yammer - don't share requestor between different endpoint types


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

Branch: refs/heads/master
Commit: bb4f2e5b5c268fff4bde72d2301c881fe155bae6
Parents: d8c4822
Author: Jonathan Anstey <janstey@gmail.com>
Authored: Thu Aug 22 13:36:26 2013 -0230
Committer: Jonathan Anstey <janstey@gmail.com>
Committed: Thu Aug 22 13:36:33 2013 -0230

----------------------------------------------------------------------
 .../camel/component/yammer/YammerComponent.java | 21 +++--
 .../yammer/YammerComponentTestSupport.java      | 18 ++--
 .../yammer/YammerMessageAndUserRouteTest.java   | 88 ++++++++++++++++++++
 .../YammerMessagesConsumerOptionTest.java       | 15 ++--
 .../YammerRelationshipConsumerOptionTest.java   |  8 +-
 5 files changed, 128 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/bb4f2e5b/components/camel-yammer/src/main/java/org/apache/camel/component/yammer/YammerComponent.java
----------------------------------------------------------------------
diff --git a/components/camel-yammer/src/main/java/org/apache/camel/component/yammer/YammerComponent.java
b/components/camel-yammer/src/main/java/org/apache/camel/component/yammer/YammerComponent.java
index 1f5433d..fce7c94 100644
--- a/components/camel-yammer/src/main/java/org/apache/camel/component/yammer/YammerComponent.java
+++ b/components/camel-yammer/src/main/java/org/apache/camel/component/yammer/YammerComponent.java
@@ -32,16 +32,22 @@ public class YammerComponent extends DefaultComponent {
     private YammerConfiguration config;
     
     protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object>
parameters) throws Exception {
+        // by default use config for each endpoint; use from component level if one has been
explicitly set
+        YammerConfiguration endpointConfig = getConfig();
+        if (endpointConfig == null) {
+            endpointConfig = new YammerConfiguration();            
+        }
+        
         // set options from component
-        getConfig().setConsumerKey(consumerKey);
-        getConfig().setConsumerSecret(consumerSecret);
-        getConfig().setAccessToken(accessToken);
-        getConfig().setFunction(remaining);
+        endpointConfig.setConsumerKey(consumerKey);
+        endpointConfig.setConsumerSecret(consumerSecret);
+        endpointConfig.setAccessToken(accessToken);
+        endpointConfig.setFunction(remaining);
         
         // and then override from parameters
-        setProperties(getConfig(), parameters);
+        setProperties(endpointConfig, parameters);
         
-        Endpoint endpoint = new YammerEndpoint(uri, this, getConfig());
+        Endpoint endpoint = new YammerEndpoint(uri, this, endpointConfig);
         setProperties(endpoint, parameters);
         return endpoint;
     }
@@ -71,9 +77,6 @@ public class YammerComponent extends DefaultComponent {
     }
 
     public YammerConfiguration getConfig() {
-        if (config == null) {
-            config = new YammerConfiguration();
-        }
         return config;
     }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/bb4f2e5b/components/camel-yammer/src/test/java/org/apache/camel/component/yammer/YammerComponentTestSupport.java
----------------------------------------------------------------------
diff --git a/components/camel-yammer/src/test/java/org/apache/camel/component/yammer/YammerComponentTestSupport.java
b/components/camel-yammer/src/test/java/org/apache/camel/component/yammer/YammerComponentTestSupport.java
index 39dce59..522f919 100644
--- a/components/camel-yammer/src/test/java/org/apache/camel/component/yammer/YammerComponentTestSupport.java
+++ b/components/camel-yammer/src/test/java/org/apache/camel/component/yammer/YammerComponentTestSupport.java
@@ -18,7 +18,9 @@
 package org.apache.camel.component.yammer;
 
 import java.io.InputStream;
+import java.util.Collection;
 
+import org.apache.camel.Endpoint;
 import org.apache.camel.test.junit4.CamelTestSupport;
 import org.junit.Before;
 
@@ -34,12 +36,18 @@ public abstract class YammerComponentTestSupport extends CamelTestSupport
{
     @Before
     public void setUp() throws Exception {
         super.setUp();
-    
+        
+        InputStream is = getClass().getResourceAsStream(jsonFile());        
+        String messages = context.getTypeConverter().convertTo(String.class, is);    
+        
         yammerComponent = context.getComponent("yammer", YammerComponent.class);
-        YammerConfiguration config = yammerComponent.getConfig();
-        InputStream is = getClass().getResourceAsStream(jsonFile());
-        String messages = context.getTypeConverter().convertTo(String.class, is);
-        config.setRequestor(new TestApiRequestor(messages));
+        
+        Collection<Endpoint> endpoints = context.getEndpoints();
+        for (Endpoint endpoint : endpoints) {
+            if (endpoint instanceof YammerEndpoint) {
+                ((YammerEndpoint)endpoint).getConfig().setRequestor(new TestApiRequestor(messages));
+            }
+        }
     }
 
     protected String jsonFile() {

http://git-wip-us.apache.org/repos/asf/camel/blob/bb4f2e5b/components/camel-yammer/src/test/java/org/apache/camel/component/yammer/YammerMessageAndUserRouteTest.java
----------------------------------------------------------------------
diff --git a/components/camel-yammer/src/test/java/org/apache/camel/component/yammer/YammerMessageAndUserRouteTest.java
b/components/camel-yammer/src/test/java/org/apache/camel/component/yammer/YammerMessageAndUserRouteTest.java
new file mode 100644
index 0000000..a8e3f0f
--- /dev/null
+++ b/components/camel-yammer/src/test/java/org/apache/camel/component/yammer/YammerMessageAndUserRouteTest.java
@@ -0,0 +1,88 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.yammer;
+
+
+import java.util.List;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.component.yammer.model.Messages;
+import org.apache.camel.component.yammer.model.User;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Before;
+import org.junit.Test;
+
+public class YammerMessageAndUserRouteTest extends CamelTestSupport {
+
+    private static final String YAMMER_CURRENT_USER_CONSUMER = "yammer:current?consumerKey=aConsumerKey&consumerSecret=aConsumerSecretKey&accessToken=aAccessToken";
+    private static final String YAMMER_MESSAGES_CONSUMER = "yammer:messages?consumerKey=aConsumerKey&consumerSecret=aConsumerSecretKey&accessToken=aAccessToken";
+
+    @Before
+    public void setUp() throws Exception { 
+        super.setUp();
+       
+        YammerEndpoint messagesEndpoint = context.getEndpoint(YAMMER_MESSAGES_CONSUMER, YammerEndpoint.class);
+        YammerEndpoint usersEndpoint = context.getEndpoint(YAMMER_CURRENT_USER_CONSUMER,
YammerEndpoint.class);
+
+        String messages = context.getTypeConverter().convertTo(String.class, getClass().getResourceAsStream("/messages.json"));
+        messagesEndpoint.getConfig().setRequestor(new TestApiRequestor(messages));
+        
+        String users = context.getTypeConverter().convertTo(String.class, getClass().getResourceAsStream("/users.json"));
+        usersEndpoint.getConfig().setRequestor(new TestApiRequestor(users));
+    }
+    
+    @Test
+    public void testConsumeAllMessages() throws Exception {
+        MockEndpoint messagesMock = getMockEndpoint("mock:messages");
+        messagesMock.expectedMinimumMessageCount(1);
+        messagesMock.assertIsSatisfied();
+        
+        Exchange exchange = messagesMock.getExchanges().get(0);
+        Messages messages = exchange.getIn().getBody(Messages.class);
+
+        assertEquals(2, messages.getMessages().size());
+        assertEquals("Testing yammer API...", messages.getMessages().get(0).getBody().getPlain());
+        assertEquals("(Principal Software Engineer) has #joined the redhat.com network. Take
a moment to welcome Jonathan.", messages.getMessages().get(1).getBody().getPlain());
+        
+        MockEndpoint userMock = getMockEndpoint("mock:user");
+        userMock.expectedMinimumMessageCount(1);
+        
+        template.sendBody("direct:start", "overwrite me");        
+        
+        userMock.assertIsSatisfied();
+        
+        exchange = userMock.getExchanges().get(0);
+        List<User> users = exchange.getIn().getBody(List.class);
+
+        assertEquals(1, users.size());
+        assertEquals("Joe Camel", users.get(0).getFullName());        
+        assertEquals("jcamel@redhat.com", users.get(0).getContact().getEmailAddresses().get(0).getAddress());
       
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() {
+                // using dummy keys here since we are mocking out calls to yammer.com with
static json; in a real app, please use your own keys!
+                from(YAMMER_MESSAGES_CONSUMER).to("mock:messages");
+                from("direct:start").pollEnrich(YAMMER_CURRENT_USER_CONSUMER).to("mock:user");
+            }
+        };
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/bb4f2e5b/components/camel-yammer/src/test/java/org/apache/camel/component/yammer/YammerMessagesConsumerOptionTest.java
----------------------------------------------------------------------
diff --git a/components/camel-yammer/src/test/java/org/apache/camel/component/yammer/YammerMessagesConsumerOptionTest.java
b/components/camel-yammer/src/test/java/org/apache/camel/component/yammer/YammerMessagesConsumerOptionTest.java
index 6c3270d..b8476ab 100644
--- a/components/camel-yammer/src/test/java/org/apache/camel/component/yammer/YammerMessagesConsumerOptionTest.java
+++ b/components/camel-yammer/src/test/java/org/apache/camel/component/yammer/YammerMessagesConsumerOptionTest.java
@@ -21,21 +21,24 @@ import org.junit.Test;
 
 public class YammerMessagesConsumerOptionTest extends YammerComponentTestSupport {
 
+    private static final String YAMMER_MESSAGES_CONSUMER = "yammer:messages?consumerKey=aConsumerKey&consumerSecret=aConsumerSecretKey&accessToken=aAccessToken&limit=1&threaded=true&olderThan=130&newerThan=127";
+
     @Test
     public void testOptions() throws Exception {
+        YammerEndpoint endpoint = context.getEndpoint(YAMMER_MESSAGES_CONSUMER, YammerEndpoint.class);
+        
         // now check if options got applied
-        assertEquals(1, yammerComponent.getConfig().getLimit());
-        assertEquals("true", yammerComponent.getConfig().getThreaded());
-        assertEquals(130, yammerComponent.getConfig().getOlderThan());
-        assertEquals(127, yammerComponent.getConfig().getNewerThan());
-        //assertEquals(YammerConstants.YAMMER_BASE_API_URL + "messages.json?limit=1&older_than=130&newer_than=127&threaded=true",
yammerComponent.getConfig().getApiUrl());
+        assertEquals(1, endpoint.getConfig().getLimit());
+        assertEquals("true", endpoint.getConfig().getThreaded());
+        assertEquals(130, endpoint.getConfig().getOlderThan());
+        assertEquals(127, endpoint.getConfig().getNewerThan());
     }
 
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
             public void configure() {
-                from("yammer:messages?consumerKey=aConsumerKey&consumerSecret=aConsumerSecretKey&accessToken=aAccessToken&limit=1&threaded=true&olderThan=130&newerThan=127").to("mock:result");
+                from(YAMMER_MESSAGES_CONSUMER).to("mock:result");
             }
         };
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/bb4f2e5b/components/camel-yammer/src/test/java/org/apache/camel/component/yammer/YammerRelationshipConsumerOptionTest.java
----------------------------------------------------------------------
diff --git a/components/camel-yammer/src/test/java/org/apache/camel/component/yammer/YammerRelationshipConsumerOptionTest.java
b/components/camel-yammer/src/test/java/org/apache/camel/component/yammer/YammerRelationshipConsumerOptionTest.java
index 26f0742..8f31f60 100644
--- a/components/camel-yammer/src/test/java/org/apache/camel/component/yammer/YammerRelationshipConsumerOptionTest.java
+++ b/components/camel-yammer/src/test/java/org/apache/camel/component/yammer/YammerRelationshipConsumerOptionTest.java
@@ -21,17 +21,21 @@ import org.junit.Test;
 
 public class YammerRelationshipConsumerOptionTest extends YammerComponentTestSupport {
 
+    private static final String YAMMER_RELATIONSHIPS_CONSUMER = "yammer:relationships?consumerKey=aConsumerKey&consumerSecret=aConsumerSecretKey&accessToken=aAccessToken&userId=jcamel";
+
     @Test
     public void testOptions() throws Exception {
+        YammerEndpoint endpoint = context.getEndpoint(YAMMER_RELATIONSHIPS_CONSUMER, YammerEndpoint.class);
+        
         // now check if options got applied
-        assertEquals("jcamel", yammerComponent.getConfig().getUserId());
+        assertEquals("jcamel", endpoint.getConfig().getUserId());
     }
     
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
             public void configure() {
-                from("yammer:relationships?consumerKey=aConsumerKey&consumerSecret=aConsumerSecretKey&accessToken=aAccessToken&userId=jcamel").to("mock:result");
+                from(YAMMER_RELATIONSHIPS_CONSUMER).to("mock:result");
             }
         };
     }


Mime
View raw message