INT-1553, Initial overhaul and simplification to the OAuth package, tests

This commit is contained in:
Oleg Zhurakousky
2010-11-05 15:35:02 -04:00
parent da178c1615
commit fae066e48e
24 changed files with 250 additions and 702 deletions

View File

@@ -17,27 +17,29 @@
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd http://www.springframework.org/schema/integration/twitter http://www.springframework.org/schema/integration/twitter/spring-integration-twitter.xsd">
<beans:bean id="tc" class="org.mockito.Mockito" factory-method="mock">
<beans:constructor-arg value="org.springframework.integration.twitter.oauth.OAuthConfiguration"/>
</beans:bean>
<twitter:twitter-connection id="twitter"
consumer-key="consumerKey"
consumer-secret="consumerSecret"
access-token="accessToken"
access-token-secret="accessTokenSecret"/>
<channel id="inbound_mentions"/>
<twitter:inbound-mention-channel-adapter id="mentionAdapter"
twitter-connection="tc"
twitter-connection="twitter"
channel="inbound_mentions"
auto-startup="false">
<poller fixed-rate="5000" max-messages-per-poll="3"/>
</twitter:inbound-mention-channel-adapter>
<twitter:inbound-dm-channel-adapter id="dmAdapter"
twitter-connection="tc"
twitter-connection="twitter"
channel="inbound_mentions"
auto-startup="false">
<poller fixed-rate="5000" max-messages-per-poll="3"/>
</twitter:inbound-dm-channel-adapter>
<twitter:inbound-update-channel-adapter id="updateAdapter"
twitter-connection="tc"
twitter-connection="twitter"
channel="inbound_mentions"
auto-startup="false">
<poller fixed-rate="5000" max-messages-per-poll="3"/>

View File

@@ -14,18 +14,23 @@
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool-3.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd http://www.springframework.org/schema/integration/twitter http://www.springframework.org/schema/integration/twitter/spring-integration-twitter.xsd">
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd
http://www.springframework.org/schema/integration/twitter http://www.springframework.org/schema/integration/twitter/spring-integration-twitter-2.0.xsd">
<beans:bean id="tc" class="org.springframework.integration.twitter.config.TestSendingMessageHandlerParserTests.MockOathConfigurationFactoryBean"/>
<twitter:twitter-connection id="twitter"
consumer-key="consumerKey"
consumer-secret="consumerSecret"
access-token="accessToken"
access-token-secret="accessTokenSecret"/>
<channel id="inbound_mentions"/>
<channel id="inputChannel"/>
<twitter:outbound-dm-channel-adapter twitter-connection="tc" channel="inputChannel"/>
<twitter:outbound-dm-channel-adapter twitter-connection="twitter" channel="inputChannel" />
<twitter:outbound-update-channel-adapter twitter-connection="tc" channel="inputChannel"/>
<twitter:outbound-update-channel-adapter twitter-connection="twitter" channel="inputChannel" />
</beans:beans>

View File

@@ -15,15 +15,8 @@
*/
package org.springframework.integration.twitter.config;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.junit.Test;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.twitter.oauth.OAuthConfiguration;
import twitter4j.Twitter;
/**
* @author Oleg Zhurakousky
@@ -36,22 +29,4 @@ public class TestSendingMessageHandlerParserTests {
new ClassPathXmlApplicationContext("TestSendingMessageHandlerParser-context.xml", this.getClass());
// the fact that no exception was thrown satisfies this test
}
public static class MockOathConfigurationFactoryBean implements FactoryBean<OAuthConfiguration>{
public OAuthConfiguration getObject() throws Exception {
OAuthConfiguration config = mock(OAuthConfiguration.class);
Twitter twitter = mock(Twitter.class);
when(config.getTwitter()).thenReturn(twitter);
return config;
}
public Class<?> getObjectType() {
return OAuthConfiguration.class;
}
public boolean isSingleton() {
return true;
}
}
}

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:twitter="http://www.springframework.org/schema/integration/twitter"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd
http://www.springframework.org/schema/integration/twitter http://www.springframework.org/schema/integration/twitter/spring-integration-twitter-2.0.xsd">
<twitter:twitter-connection id="twitter"
consumer-key="consumerKey"
consumer-secret="consumerSecret"
access-token="accessToken"
access-token-secret="accessTokenSecret"/>
</beans:beans>

View File

@@ -0,0 +1,50 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed 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.springframework.integration.twitter.config;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.integration.twitter.oauth.OAuthTwitterFactoryBean;
import twitter4j.Twitter;
import twitter4j.http.AccessToken;
/**
* @author Oleg Zhurakousky
* @since 2.0
*
*/
public class TwitterConnectionParserTests {
@Test
public void testOAuthTwitterFactoryBean(){
ApplicationContext ac = new ClassPathXmlApplicationContext("TwitterConnectionParserTests-context.xml", this.getClass());
OAuthTwitterFactoryBean twitterFb = ac.getBean("&twitter", OAuthTwitterFactoryBean.class);
assertEquals("consumerKey", TestUtils.getPropertyValue(twitterFb, "consumerKey"));
assertEquals("consumerSecret", TestUtils.getPropertyValue(twitterFb, "consumerSecret"));
AccessToken accessToken = (AccessToken) TestUtils.getPropertyValue(twitterFb, "accessToken");
assertEquals("accessToken", accessToken.getToken());
assertEquals("accessTokenSecret", accessToken.getTokenSecret());
Twitter twitter = ac.getBean("twitter", Twitter.class);
assertTrue(twitter.isOAuthEnabled());
}
}

View File

@@ -16,9 +16,6 @@
package org.springframework.integration.twitter.inbound;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -30,11 +27,6 @@ import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.integration.Message;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.twitter.oauth.OAuthConfiguration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import twitter4j.DirectMessage;
import twitter4j.Paging;
import twitter4j.RateLimitStatus;
@@ -86,24 +78,24 @@ public class InboundDirectMessageStatusEndpointTests {
}
@SuppressWarnings("unchecked")
private OAuthConfiguration getTestConfigurationForDirectMessages() throws Exception{
OAuthConfiguration configuration = mock(OAuthConfiguration.class);
RateLimitStatus rateLimitStatus = mock(RateLimitStatus.class);
when(twitter.getRateLimitStatus()).thenReturn(rateLimitStatus);
when(configuration.getTwitter()).thenReturn(twitter);
when(rateLimitStatus.getSecondsUntilReset()).thenReturn(2464);
when(rateLimitStatus.getRemainingHits()).thenReturn(250);
ResponseList<DirectMessage> responses = mock(ResponseList.class);
List<DirectMessage> testMessages = new ArrayList<DirectMessage>();
testMessages.add(firstMessage);
testMessages.add(secondMessage);
when(responses.iterator()).thenReturn(testMessages.iterator());
when(twitter.getDirectMessages()).thenReturn(responses);
when(twitter.getDirectMessages(Mockito.any(Paging.class))).thenReturn(responses);
return configuration;
}
// @SuppressWarnings("unchecked")
// private OAuthConfiguration getTestConfigurationForDirectMessages() throws Exception{
// OAuthConfiguration configuration = mock(OAuthConfiguration.class);
// RateLimitStatus rateLimitStatus = mock(RateLimitStatus.class);
// when(twitter.getRateLimitStatus()).thenReturn(rateLimitStatus);
// when(configuration.getTwitter()).thenReturn(twitter);
// when(rateLimitStatus.getSecondsUntilReset()).thenReturn(2464);
// when(rateLimitStatus.getRemainingHits()).thenReturn(250);
//
// ResponseList<DirectMessage> responses = mock(ResponseList.class);
// List<DirectMessage> testMessages = new ArrayList<DirectMessage>();
// testMessages.add(firstMessage);
// testMessages.add(secondMessage);
//
// when(responses.iterator()).thenReturn(testMessages.iterator());
// when(twitter.getDirectMessages()).thenReturn(responses);
// when(twitter.getDirectMessages(Mockito.any(Paging.class))).thenReturn(responses);
// return configuration;
// }
}

View File

@@ -1,121 +0,0 @@
/*
* Copyright 2010 the original author or authors
*
* Licensed 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.springframework.integration.twitter.oauth;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.SystemUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.core.io.FileSystemResource;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.http.AccessToken;
import twitter4j.http.RequestToken;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
* Default System.(out|in) based implementation of the {@link org.springframework.integration.twitter.oauth.AccessTokenInitialRequestProcessListener} interface
*
* @author Josh Long
*/
public class ConsoleBasedAccessTokenInitialRequestProcessListener implements AccessTokenInitialRequestProcessListener {
public String openUrlAndReturnPin(String urlToOpen)
throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Open the following URL and grant access to your account:");
System.out.println(urlToOpen);
System.out.print("Enter the PIN(if aviailable) or just hit enter.[PIN]:");
return StringUtils.trim(br.readLine());
}
public void persistReturnedAccessToken(AccessToken accessToken)
throws Exception {
Map<String, String> output = new HashMap<String, String>();
output.put(OAuthConfigurationFactoryBean.WELL_KNOWN_CONSUMER_ACCESS_TOKEN, accessToken.getToken());
output.put(OAuthConfigurationFactoryBean.WELL_KNOWN_CONSUMER_ACCESS_TOKEN_SECRET, accessToken.getTokenSecret());
File accessTokenCreds = new File(SystemUtils.getJavaIoTmpDir(), "twitter-accesstoken.properties");
FileOutputStream fileOutputStream = new FileOutputStream(accessTokenCreds);
Properties props = new Properties();
props.putAll(output);
props.store(fileOutputStream, "oauth-access-token");
IOUtils.closeQuietly(fileOutputStream);
System.out.println("The oauth accesstoken credentials have been written to " + accessTokenCreds.getAbsolutePath());
}
public void failure(Throwable t) {
System.err.println("Exception occurred when trying to retrieve credentials: " + ExceptionUtils.getFullStackTrace(t));
}
public static void main(String[] args) throws Exception {
File twitterProps = new File(SystemUtils.getUserHome(), "Desktop/twitter.properties");
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
propertiesFactoryBean.setLocation(new FileSystemResource(twitterProps));
propertiesFactoryBean.afterPropertiesSet() ;
Properties props = propertiesFactoryBean.getObject();
String key = StringUtils.trim(props.getProperty("twitter.oauth.consumerKey"));
String secret = StringUtils.trim( props.getProperty("twitter.oauth.consumerSecret") ) ;
ConsoleBasedAccessTokenInitialRequestProcessListener consoleBasedAccessTokenInitialRequestProcessListener =
new ConsoleBasedAccessTokenInitialRequestProcessListener();
Twitter twitter = new TwitterFactory().getOAuthAuthorizedInstance( key, secret);
RequestToken requestToken = twitter.getOAuthRequestToken();
AccessToken accessToken = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (null == accessToken) {
String pin = consoleBasedAccessTokenInitialRequestProcessListener.openUrlAndReturnPin(requestToken.getAuthorizationURL());
try {
if (pin.length() > 0) {
accessToken = twitter.getOAuthAccessToken(requestToken, pin);
} else {
accessToken = twitter.getOAuthAccessToken();
}
} catch (TwitterException te) {
if (401 == te.getStatusCode()) {
System.out.println("Unable to get the access token.");
} else {
te.printStackTrace();
}
}
}
consoleBasedAccessTokenInitialRequestProcessListener.persistReturnedAccessToken(accessToken);
}
}

View File

@@ -19,22 +19,22 @@ package org.springframework.integration.twitter.outbound;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.junit.Test;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.twitter.core.TwitterHeaders;
import org.springframework.integration.twitter.oauth.OAuthConfiguration;
import org.springframework.integration.twitter.oauth.OAuthTwitterFactoryBean;
import twitter4j.GeoLocation;
import twitter4j.Twitter;
import twitter4j.http.AccessToken;
/**
* @author Oleg Zhurakousky
*/
public class OutboundDirectMessageMessageHandlerTests {
private Twitter twitter;
private Twitter twitter = mock(Twitter.class);
@Test
public void validateSendDirectMessage() throws Exception{
@@ -43,8 +43,7 @@ public class OutboundDirectMessageMessageHandlerTests {
.setHeader(TwitterHeaders.DISPLAY_COORDINATES, true)
.setHeader(TwitterHeaders.DM_TARGET_USER_ID, "foo");
DirectMessageSendingMessageHandler handler = new DirectMessageSendingMessageHandler();
handler.setConfiguration(this.getTestConfiguration());
DirectMessageSendingMessageHandler handler = new DirectMessageSendingMessageHandler(twitter);
handler.afterPropertiesSet();
handler.handleMessage(mb.build());
@@ -59,12 +58,4 @@ public class OutboundDirectMessageMessageHandlerTests {
verify(twitter, times(1)).sendDirectMessage(123, "hello");
}
private OAuthConfiguration getTestConfiguration() throws Exception {
twitter = mock(Twitter.class);
OAuthConfiguration configuration = mock(OAuthConfiguration.class);
when(configuration.getTwitter()).thenReturn(twitter);
return configuration;
}
}