INT-1553 initial refactoring to introduce Twitter4jTemplate in place of the twitter-connection
This commit is contained in:
@@ -0,0 +1,151 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package org.springframework.integration.twitter.core;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import twitter4j.DirectMessage;
|
||||
import twitter4j.Paging;
|
||||
import twitter4j.RateLimitStatus;
|
||||
import twitter4j.Status;
|
||||
import twitter4j.StatusUpdate;
|
||||
import twitter4j.Twitter;
|
||||
import twitter4j.TwitterFactory;
|
||||
import twitter4j.http.AccessToken;
|
||||
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
* @since 2.0
|
||||
*
|
||||
*/
|
||||
public class Twitter4jTemplate implements TwitterOperations{
|
||||
private final Twitter twitter;
|
||||
|
||||
/**
|
||||
* Used to construct this template to perform Twitter API calls that do not require authorization.
|
||||
* (e.g., search)
|
||||
*/
|
||||
public Twitter4jTemplate(){
|
||||
this.twitter = new TwitterFactory().getInstance();
|
||||
}
|
||||
/**
|
||||
* Used to construct this template with OAuth authentication/authorization to perform Twitter API calls
|
||||
* that do require authorization (e.g., send/receive DirectMessage)
|
||||
*
|
||||
* @param consumerKey
|
||||
* @param consumerSecret
|
||||
* @param accessToken
|
||||
* @param accessTokenSecret
|
||||
*/
|
||||
public Twitter4jTemplate(String consumerKey, String consumerSecret, String accessToken, String accessTokenSecret){
|
||||
Assert.hasText(consumerKey, "'consumerKey' must be provided");
|
||||
Assert.hasText(consumerSecret, "'consumerSecret' must be provided");
|
||||
Assert.hasText(accessToken, "'accessToken' must be provided");
|
||||
Assert.hasText(accessTokenSecret, "'accessTokenSecret' must be provided");
|
||||
AccessToken at = new AccessToken(accessToken, accessTokenSecret);
|
||||
this.twitter = new TwitterFactory().getOAuthAuthorizedInstance(consumerKey, consumerSecret, at);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getProfileId() {
|
||||
try {
|
||||
return twitter.getScreenName();
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new TwitterOperationException("Failed to obtain profile id ", e);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public RateLimitStatus getRateLimitStatus() {
|
||||
try {
|
||||
return twitter.getRateLimitStatus();
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new TwitterOperationException("Failed to obtain Rate Limit status ", e);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public List<DirectMessage> getDirectMessages() {
|
||||
try {
|
||||
return twitter.getDirectMessages();
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new TwitterOperationException("Failed to receive Direct Messages ", e);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public List<DirectMessage> getDirectMessages(Paging paging) {
|
||||
try {
|
||||
return twitter.getDirectMessages(paging);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new TwitterOperationException("Failed to receive Direct Messages ", e);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public List<Status> getMentions() {
|
||||
try {
|
||||
return twitter.getMentions();
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new TwitterOperationException("Failed to receive Mention statuses ", e);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public List<Status> getMentions(Paging paging) {
|
||||
try {
|
||||
return twitter.getMentions(paging);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new TwitterOperationException("Failed to receive Mention statuses ", e);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public List<Status> getFriendsTimeline() {
|
||||
try {
|
||||
return twitter.getFriendsTimeline();
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new TwitterOperationException("Failed to receive Timeline statuses ", e);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public List<Status> getFriendsTimeline(Paging paging) {
|
||||
try {
|
||||
return twitter.getFriendsTimeline(paging);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new TwitterOperationException("Failed to receive Timeline statuses ", e);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void sendDirectMessage(String userName, String text) {
|
||||
try {
|
||||
twitter.sendDirectMessage(userName, text);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new TwitterOperationException("Failed to send Direct Message ", e);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void sendDirectMessage(int userId, String text) {
|
||||
try {
|
||||
twitter.sendDirectMessage(userId, text);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new TwitterOperationException("Failed to send Direct Message ", e);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void updateStatus(StatusUpdate status) {
|
||||
try {
|
||||
twitter.updateStatus(status);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new TwitterOperationException("Failed to send Status update ", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* 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.core;
|
||||
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
* @since 2.0
|
||||
*
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public class TwitterOperationException extends RuntimeException {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public TwitterOperationException() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param description
|
||||
*/
|
||||
public TwitterOperationException(String description) {
|
||||
super(description);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param throwable
|
||||
*/
|
||||
public TwitterOperationException(Throwable throwable) {
|
||||
super(throwable);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param description
|
||||
* @param throwable
|
||||
*/
|
||||
public TwitterOperationException(String description, Throwable throwable) {
|
||||
super(description, throwable);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package org.springframework.integration.twitter.core;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import twitter4j.DirectMessage;
|
||||
import twitter4j.Paging;
|
||||
import twitter4j.RateLimitStatus;
|
||||
import twitter4j.Status;
|
||||
import twitter4j.StatusUpdate;
|
||||
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
* @since 2.0
|
||||
*
|
||||
*/
|
||||
public interface TwitterOperations {
|
||||
|
||||
String getProfileId();
|
||||
|
||||
RateLimitStatus getRateLimitStatus();
|
||||
|
||||
List<DirectMessage> getDirectMessages();
|
||||
|
||||
List<DirectMessage> getDirectMessages(Paging paging);
|
||||
|
||||
List<Status> getMentions();
|
||||
|
||||
List<Status> getMentions(Paging paging);
|
||||
|
||||
List<Status> getFriendsTimeline();
|
||||
|
||||
List<Status> getFriendsTimeline(Paging paging);
|
||||
|
||||
void sendDirectMessage(String userName, String text);
|
||||
|
||||
void sendDirectMessage(int userId, String text);
|
||||
|
||||
void updateStatus(StatusUpdate status);
|
||||
}
|
||||
@@ -33,6 +33,7 @@ import org.springframework.integration.history.TrackableComponent;
|
||||
import org.springframework.integration.store.MetadataStore;
|
||||
import org.springframework.integration.store.SimpleMetadataStore;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.integration.twitter.core.TwitterOperations;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
@@ -67,7 +68,7 @@ public abstract class AbstractTwitterMessageSource<T> extends AbstractEndpoint
|
||||
|
||||
protected volatile long markerId = -1;
|
||||
|
||||
protected final Twitter twitter;
|
||||
protected final TwitterOperations twitter;
|
||||
|
||||
private final Object markerGuard = new Object();
|
||||
|
||||
@@ -75,7 +76,7 @@ public abstract class AbstractTwitterMessageSource<T> extends AbstractEndpoint
|
||||
|
||||
private final HistoryWritingMessagePostProcessor historyWritingPostProcessor = new HistoryWritingMessagePostProcessor();
|
||||
|
||||
public AbstractTwitterMessageSource(Twitter twitter){
|
||||
public AbstractTwitterMessageSource(TwitterOperations twitter){
|
||||
this.twitter = twitter;
|
||||
}
|
||||
|
||||
@@ -120,8 +121,8 @@ public abstract class AbstractTwitterMessageSource<T> extends AbstractEndpoint
|
||||
else if (logger.isWarnEnabled()) {
|
||||
logger.warn(this.getClass().getSimpleName() + " has no name. MetadataStore key might not be unique.");
|
||||
}
|
||||
String accessToken = twitter.getOAuthAccessToken().getToken();
|
||||
metadataKeyBuilder.append(accessToken);
|
||||
String profileId = twitter.getProfileId();
|
||||
metadataKeyBuilder.append(profileId);
|
||||
this.metadataKey = metadataKeyBuilder.toString();
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.integration.MessagingException;
|
||||
import org.springframework.integration.twitter.core.TwitterOperations;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import twitter4j.DirectMessage;
|
||||
@@ -34,7 +35,7 @@ import twitter4j.Twitter;
|
||||
*/
|
||||
public class DirectMessageReceivingMessageSource extends AbstractTwitterMessageSource<DirectMessage> {
|
||||
|
||||
public DirectMessageReceivingMessageSource(Twitter twitter){
|
||||
public DirectMessageReceivingMessageSource(TwitterOperations twitter){
|
||||
super(twitter);
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.integration.twitter.inbound;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.integration.MessagingException;
|
||||
import org.springframework.integration.twitter.core.TwitterOperations;
|
||||
|
||||
import twitter4j.Paging;
|
||||
import twitter4j.Status;
|
||||
@@ -31,7 +32,7 @@ import twitter4j.Twitter;
|
||||
*/
|
||||
public class MentionReceivingMessageSource extends AbstractTwitterMessageSource<Status> {
|
||||
|
||||
public MentionReceivingMessageSource(Twitter twitter){
|
||||
public MentionReceivingMessageSource(TwitterOperations twitter){
|
||||
super(twitter);
|
||||
}
|
||||
@Override
|
||||
|
||||
@@ -19,14 +19,12 @@ import java.util.Date;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.scheduling.SchedulingException;
|
||||
import org.springframework.integration.twitter.core.TwitterOperations;
|
||||
import org.springframework.scheduling.Trigger;
|
||||
import org.springframework.scheduling.TriggerContext;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import twitter4j.RateLimitStatus;
|
||||
import twitter4j.Twitter;
|
||||
import twitter4j.TwitterException;
|
||||
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
@@ -34,9 +32,9 @@ import twitter4j.TwitterException;
|
||||
*/
|
||||
class RateLimitStatusTrigger implements Trigger {
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
private Twitter twitter;
|
||||
private TwitterOperations twitter;
|
||||
|
||||
public RateLimitStatusTrigger(Twitter twitter){
|
||||
public RateLimitStatusTrigger(TwitterOperations twitter){
|
||||
Assert.notNull(twitter, "'twitter' must not be null");
|
||||
this.twitter = twitter;
|
||||
}
|
||||
@@ -48,7 +46,7 @@ class RateLimitStatusTrigger implements Trigger {
|
||||
if (triggerContext.lastCompletionTime() == null){
|
||||
return new Date(System.currentTimeMillis());
|
||||
}
|
||||
try {
|
||||
// try {
|
||||
RateLimitStatus rateLimitStatus = twitter.getRateLimitStatus();
|
||||
int secondsUntilReset = rateLimitStatus.getSecondsUntilReset();
|
||||
int remainingHits = rateLimitStatus.getRemainingHits();
|
||||
@@ -69,8 +67,8 @@ class RateLimitStatusTrigger implements Trigger {
|
||||
" remaining pull this rate period. The period ends in " +
|
||||
secondsUntilReset);
|
||||
return new Date(System.currentTimeMillis() + msUntilWeCanPullAgain);
|
||||
} catch (TwitterException e) {
|
||||
throw new SchedulingException("Failed to schedule the next Twitter update", e);
|
||||
}
|
||||
// } catch (TwitterException e) {
|
||||
// throw new SchedulingException("Failed to schedule the next Twitter update", e);
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package org.springframework.integration.twitter.inbound;
|
||||
|
||||
import org.springframework.integration.MessagingException;
|
||||
import org.springframework.integration.twitter.core.TwitterOperations;
|
||||
|
||||
import twitter4j.Paging;
|
||||
import twitter4j.Status;
|
||||
@@ -32,7 +33,7 @@ import twitter4j.Twitter;
|
||||
*/
|
||||
public class TimelineUpdateReceivingMessageSource extends AbstractTwitterMessageSource<Status> {
|
||||
|
||||
public TimelineUpdateReceivingMessageSource(Twitter twitter){
|
||||
public TimelineUpdateReceivingMessageSource(TwitterOperations twitter){
|
||||
super(twitter);
|
||||
}
|
||||
@Override
|
||||
|
||||
@@ -1,68 +0,0 @@
|
||||
/*
|
||||
* 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.oauth;
|
||||
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import twitter4j.Twitter;
|
||||
import twitter4j.TwitterFactory;
|
||||
import twitter4j.http.AccessToken;
|
||||
|
||||
/**
|
||||
* Will create an OAuth-Authorized instance of Twitter object.
|
||||
*
|
||||
* @author Oleg Zhurakousky
|
||||
* @since 2.0
|
||||
*/
|
||||
public class OAuthTwitterFactoryBean implements FactoryBean<Twitter>, InitializingBean {
|
||||
private final String consumerKey;
|
||||
private final String consumerSecret;
|
||||
private final AccessToken accessToken;
|
||||
|
||||
private volatile Twitter twitter;
|
||||
|
||||
public OAuthTwitterFactoryBean(String consumerKey, String consumerSecret, AccessToken accessToken){
|
||||
Assert.hasText(consumerKey, "'consumerKey' must be provided");
|
||||
Assert.hasText(consumerSecret, "'consumerSecret' must be provided");
|
||||
Assert.notNull(accessToken, "'accessToken' must be provided");
|
||||
this.consumerKey = consumerKey;
|
||||
this.consumerSecret = consumerSecret;
|
||||
this.accessToken = accessToken;
|
||||
}
|
||||
@Override
|
||||
public Twitter getObject() throws Exception {
|
||||
Assert.notNull(this.twitter, "OAuthTwitterFactoryBean must be initialized. Invoke afterPropertiesSet() method");
|
||||
return twitter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getObjectType() {
|
||||
return Twitter.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSingleton() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
this.twitter = new TwitterFactory().getOAuthAuthorizedInstance(consumerKey, consumerSecret, accessToken);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,6 +16,7 @@
|
||||
package org.springframework.integration.twitter.outbound;
|
||||
|
||||
import org.springframework.integration.handler.AbstractMessageHandler;
|
||||
import org.springframework.integration.twitter.core.TwitterOperations;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import twitter4j.Twitter;
|
||||
@@ -28,23 +29,11 @@ import twitter4j.Twitter;
|
||||
* @since 2.0
|
||||
*/
|
||||
public abstract class AbstractOutboundTwitterEndpointSupport extends AbstractMessageHandler {
|
||||
//protected volatile OAuthConfiguration configuration;
|
||||
protected final Twitter twitter;
|
||||
protected final TwitterOperations twitter;
|
||||
protected final OutboundStatusUpdateMessageMapper supportStatusUpdate = new OutboundStatusUpdateMessageMapper();
|
||||
|
||||
public AbstractOutboundTwitterEndpointSupport(Twitter twitter){
|
||||
public AbstractOutboundTwitterEndpointSupport(TwitterOperations twitter){
|
||||
Assert.notNull(twitter, "'twitter' must not be null");
|
||||
this.twitter = twitter;
|
||||
}
|
||||
// public void setConfiguration(OAuthConfiguration configuration) {
|
||||
// this.configuration = configuration;
|
||||
// }
|
||||
|
||||
// @Override
|
||||
// protected void onInit() throws Exception {
|
||||
// Assert.notNull(this.configuration, "'configuration' can't be null");
|
||||
// this.twitter = this.configuration.getTwitter();
|
||||
// Assert.notNull(this.twitter, "'twitter' can't be null");
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
@@ -17,13 +17,10 @@
|
||||
package org.springframework.integration.twitter.outbound;
|
||||
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.MessageHandlingException;
|
||||
import org.springframework.integration.twitter.core.TwitterHeaders;
|
||||
import org.springframework.integration.twitter.core.TwitterOperations;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import twitter4j.Twitter;
|
||||
import twitter4j.TwitterException;
|
||||
|
||||
/**
|
||||
* Simple adapter to support sending outbound direct messages ("DM"s) using twitter
|
||||
*
|
||||
@@ -33,7 +30,7 @@ import twitter4j.TwitterException;
|
||||
*/
|
||||
public class DirectMessageSendingMessageHandler extends AbstractOutboundTwitterEndpointSupport {
|
||||
|
||||
public DirectMessageSendingMessageHandler(Twitter twitter){
|
||||
public DirectMessageSendingMessageHandler(TwitterOperations twitter){
|
||||
super(twitter);
|
||||
}
|
||||
|
||||
@@ -42,7 +39,7 @@ public class DirectMessageSendingMessageHandler extends AbstractOutboundTwitterE
|
||||
if (this.twitter == null) {
|
||||
this.afterPropertiesSet();
|
||||
}
|
||||
try {
|
||||
// try {
|
||||
Assert.isInstanceOf(String.class, message.getPayload(), "Only payload of type String is supported. If your payload " +
|
||||
"is not of type String consider adding a transformer to the message flow in front of this adapter.");
|
||||
Assert.isTrue(message.getHeaders().containsKey(TwitterHeaders.DM_TARGET_USER_ID),
|
||||
@@ -58,10 +55,10 @@ public class DirectMessageSendingMessageHandler extends AbstractOutboundTwitterE
|
||||
else if (toUser instanceof String) {
|
||||
this.twitter.sendDirectMessage((String) toUser, payload);
|
||||
}
|
||||
}
|
||||
catch (TwitterException e) {
|
||||
throw new MessageHandlingException(message, e);
|
||||
}
|
||||
// }
|
||||
// catch (TwitterException e) {
|
||||
// throw new MessageHandlingException(message, e);
|
||||
// }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package org.springframework.integration.twitter.outbound;
|
||||
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.twitter.core.TwitterOperations;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import twitter4j.StatusUpdate;
|
||||
@@ -30,7 +31,7 @@ import twitter4j.Twitter;
|
||||
*/
|
||||
public class TimelineUpdateSendingMessageHandler extends AbstractOutboundTwitterEndpointSupport {
|
||||
|
||||
public TimelineUpdateSendingMessageHandler(Twitter twitter){
|
||||
public TimelineUpdateSendingMessageHandler(TwitterOperations twitter){
|
||||
super(twitter);
|
||||
}
|
||||
|
||||
|
||||
@@ -17,11 +17,7 @@
|
||||
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">
|
||||
|
||||
|
||||
<twitter:twitter-connection id="twitter"
|
||||
consumer-key="consumerKey"
|
||||
consumer-secret="consumerSecret"
|
||||
access-token="accessToken"
|
||||
access-token-secret="accessTokenSecret"/>
|
||||
<beans:bean id="twitter" class="org.springframework.integration.twitter.config.TestReceivingMessageSourceParserTests.TwitterTemplateFactoryBean"/>
|
||||
|
||||
<channel id="inbound_mentions"/>
|
||||
|
||||
|
||||
@@ -16,12 +16,17 @@
|
||||
package org.springframework.integration.twitter.config;
|
||||
|
||||
import static junit.framework.Assert.assertFalse;
|
||||
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.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.integration.endpoint.SourcePollingChannelAdapter;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
import org.springframework.integration.twitter.core.Twitter4jTemplate;
|
||||
import org.springframework.integration.twitter.core.TwitterOperations;
|
||||
import org.springframework.integration.twitter.inbound.AbstractTwitterMessageSource;
|
||||
|
||||
/**
|
||||
@@ -45,4 +50,25 @@ public class TestReceivingMessageSourceParserTests {
|
||||
ms = (AbstractTwitterMessageSource<?>) TestUtils.getPropertyValue(spca, "source");
|
||||
assertFalse(ms.isAutoStartup());
|
||||
}
|
||||
|
||||
public static class TwitterTemplateFactoryBean implements FactoryBean<TwitterOperations>{
|
||||
|
||||
@Override
|
||||
public TwitterOperations getObject() throws Exception {
|
||||
TwitterOperations oper = mock(TwitterOperations.class);
|
||||
when(oper.getProfileId()).thenReturn("kermit");
|
||||
return oper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getObjectType() {
|
||||
return TwitterOperations.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSingleton() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,11 +18,7 @@
|
||||
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:bean id="twitter" class="org.springframework.integration.twitter.core.Twitter4jTemplate"/>
|
||||
|
||||
<channel id="inbound_mentions"/>
|
||||
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
<?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>
|
||||
|
||||
|
||||
@@ -1,50 +0,0 @@
|
||||
/*
|
||||
* 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());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* 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.core;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
*/
|
||||
public class Twitter4jTemplateTests {
|
||||
|
||||
@Test
|
||||
public void testProfileId(){
|
||||
|
||||
}
|
||||
}
|
||||
@@ -23,13 +23,13 @@ import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
import org.springframework.integration.twitter.core.TwitterOperations;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
@@ -37,8 +37,6 @@ import twitter4j.DirectMessage;
|
||||
import twitter4j.Paging;
|
||||
import twitter4j.RateLimitStatus;
|
||||
import twitter4j.ResponseList;
|
||||
import twitter4j.Twitter;
|
||||
import twitter4j.http.AccessToken;
|
||||
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
@@ -49,12 +47,12 @@ public class DirectMessageReceivingMessageSourceTests {
|
||||
|
||||
private DirectMessage secondMessage;
|
||||
|
||||
private Twitter twitter = mock(Twitter.class);
|
||||
private TwitterOperations twitter;
|
||||
|
||||
|
||||
@Before
|
||||
public void prepare() throws Exception{
|
||||
twitter = mock(Twitter.class);
|
||||
twitter = mock(TwitterOperations.class);
|
||||
firstMessage = mock(DirectMessage.class);
|
||||
when(firstMessage.getCreatedAt()).thenReturn(new Date(5555555555L));
|
||||
when(firstMessage.getId()).thenReturn(200);
|
||||
@@ -63,7 +61,7 @@ public class DirectMessageReceivingMessageSourceTests {
|
||||
when(secondMessage.getId()).thenReturn(2000);
|
||||
|
||||
|
||||
when(twitter.getOAuthAccessToken()).thenReturn(new AccessToken("token123", "tokenSecret123"));
|
||||
when(twitter.getProfileId()).thenReturn("kermit");
|
||||
}
|
||||
|
||||
|
||||
@@ -76,7 +74,7 @@ public class DirectMessageReceivingMessageSourceTests {
|
||||
source.setBeanName("twitterEndpoint");
|
||||
source.afterPropertiesSet();
|
||||
source.start();
|
||||
assertEquals("twitter:inbound-dm-channel-adapter.twitterEndpoint.token123", TestUtils.getPropertyValue(source, "metadataKey"));
|
||||
assertEquals("twitter:inbound-dm-channel-adapter.twitterEndpoint.kermit", TestUtils.getPropertyValue(source, "metadataKey"));
|
||||
assertTrue(source.isRunning());
|
||||
}
|
||||
|
||||
|
||||
@@ -22,10 +22,10 @@ import static org.mockito.Mockito.when;
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.integration.twitter.core.TwitterOperations;
|
||||
import org.springframework.scheduling.TriggerContext;
|
||||
|
||||
import twitter4j.RateLimitStatus;
|
||||
import twitter4j.Twitter;
|
||||
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
@@ -35,7 +35,7 @@ public class RateLimitStatusTriggerTests {
|
||||
|
||||
@Test
|
||||
public void testTriggerImediateAndSubsequentExecutionTime() throws Exception{
|
||||
Twitter twitter = mock(Twitter.class);
|
||||
TwitterOperations twitter = mock(TwitterOperations.class);
|
||||
RateLimitStatusTrigger trigger = new RateLimitStatusTrigger(twitter);
|
||||
TriggerContext context = mock(TriggerContext.class);
|
||||
Date currentDate = new Date(System.currentTimeMillis());
|
||||
|
||||
@@ -23,16 +23,16 @@ import static org.mockito.Mockito.verify;
|
||||
import org.junit.Test;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.integration.twitter.core.TwitterHeaders;
|
||||
import org.springframework.integration.twitter.core.TwitterOperations;
|
||||
|
||||
import twitter4j.GeoLocation;
|
||||
import twitter4j.Twitter;
|
||||
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
*/
|
||||
public class OutboundDirectMessageMessageHandlerTests {
|
||||
|
||||
private Twitter twitter = mock(Twitter.class);
|
||||
private TwitterOperations twitter = mock(TwitterOperations.class);
|
||||
|
||||
@Test
|
||||
public void validateSendDirectMessage() throws Exception{
|
||||
|
||||
Reference in New Issue
Block a user