INT-1553 Final modifications to align with Spring Social API, added tests for persistent metadatastore, tested every supported twiter function against the real twitter account

This commit is contained in:
Oleg Zhurakousky
2010-11-08 18:12:03 -05:00
parent 8a46cfc0e8
commit 2a8994d7b6
14 changed files with 370 additions and 89 deletions

View File

@@ -25,7 +25,6 @@ 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;

View File

@@ -39,15 +39,15 @@
</twitter:inbound-mention-channel-adapter>
<service-activator input-channel="inbound_mentions" ref="twitterAnnouncer" method="mention"/>
<!-- <twitter:inbound-dm-channel-adapter twitter-template="twitterTemplate" channel="inbound_dm">-->
<!-- <poller fixed-rate="5000" max-messages-per-poll="3"/>-->
<!-- </twitter:inbound-dm-channel-adapter>-->
<!-- <service-activator input-channel="inbound_dm" ref="twitterAnnouncer" method="dm"/>-->
<twitter:inbound-dm-channel-adapter twitter-template="twitterTemplate" channel="inbound_dm">
<poller fixed-rate="5000" max-messages-per-poll="3"/>
</twitter:inbound-dm-channel-adapter>
<service-activator input-channel="inbound_dm" ref="twitterAnnouncer" method="dm"/>
<!-- <twitter:inbound-update-channel-adapter id="twitterInbound" twitter-template="twitterTemplate" channel="inbound_updates">-->
<!-- <poller fixed-rate="5000" max-messages-per-poll="3"/>-->
<!-- </twitter:inbound-update-channel-adapter>-->
<!-- <service-activator input-channel="inbound_updates" ref="twitterAnnouncer" method="updates"/>-->
<twitter:inbound-update-channel-adapter id="twitterInbound" twitter-template="twitterTemplate" channel="inbound_updates">
<poller fixed-rate="5000" max-messages-per-poll="3"/>
</twitter:inbound-update-channel-adapter>
<service-activator input-channel="inbound_updates" ref="twitterAnnouncer" method="updates"/>
<beans:bean id="twitterAnnouncer" class="org.springframework.integration.twitter.ignored.TwitterAnnouncer"/>
</beans:beans>

View File

@@ -21,17 +21,17 @@
location="classpath:twitter.sender.properties"
ignore-unresolvable="true"/>
<twitter:twitter-connection
id="tc"
access-token="${twitter.oauth.accessToken}"
access-token-secret="${twitter.oauth.accessTokenSecret}"
consumer-key="${twitter.oauth.consumerKey}"
consumer-secret="${twitter.oauth.consumerSecret}"/>
<beans:bean id="twitterTemplate" class="org.springframework.integration.twitter.core.Twitter4jTemplate">
<beans:constructor-arg value="${twitter.oauth.consumerKey}"/>
<beans:constructor-arg value="${twitter.oauth.consumerSecret}"/>
<beans:constructor-arg value="${twitter.oauth.accessToken}"/>
<beans:constructor-arg value="${twitter.oauth.accessTokenSecret}"/>
</beans:bean>
<channel id="inputChannel"/>
<twitter:outbound-dm-channel-adapter twitter-connection="tc" channel="inputChannel"/>
<twitter:outbound-dm-channel-adapter twitter-template="twitterTemplate" channel="inputChannel"/>
</beans:beans>

View File

@@ -16,27 +16,23 @@
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">
<context:component-scan
base-package="org.springframework.integration.twitter"/>
<context:property-placeholder
location="file://${user.home}/Desktop/twitter.properties"
ignore-unresolvable="true"/>
<context:property-placeholder
location="classpath:twitter.receiver.properties"
ignore-unresolvable="true"/>
<twitter:twitter-connection
id="tc"
access-token="${twitter.oauth.accessToken}"
access-token-secret="${twitter.oauth.accessTokenSecret}"
consumer-key="${twitter.oauth.consumerKey}"
consumer-secret="${twitter.oauth.consumerSecret}"
/>
<beans:bean id="twitterTemplate" class="org.springframework.integration.twitter.core.Twitter4jTemplate">
<beans:constructor-arg value="${twitter.oauth.consumerKey}"/>
<beans:constructor-arg value="${twitter.oauth.consumerSecret}"/>
<beans:constructor-arg value="${twitter.oauth.accessToken}"/>
<beans:constructor-arg value="${twitter.oauth.accessTokenSecret}"/>
</beans:bean>
<channel id="out"/>
<twitter:outbound-update-channel-adapter twitter-connection="tc" channel="out"/>
<twitter:outbound-update-channel-adapter twitter-template="twitterTemplate" channel="out"/>
</beans:beans>

View File

@@ -16,6 +16,8 @@
package org.springframework.integration.twitter.ignored;
import java.util.Date;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;
@@ -40,9 +42,8 @@ public class TestSendingUpdatesUsingNamespace extends AbstractJUnit4SpringContex
@Test
@Ignore
public void testSendingATweet() throws Throwable {
MessageBuilder<String> mb = MessageBuilder.withPayload("simple test demonstrating the ability to encode location information")
MessageBuilder<String> mb = MessageBuilder.withPayload("Aligning #springintegration Twitter adapter with #SpringSocial API - " + new Date(System.currentTimeMillis()))
.setHeader(TwitterHeaders.IN_REPLY_TO_STATUS_ID, 21927437001L)
//.setHeader(TwitterHeaders.TWITTER_GEOLOCATION, new Twitter4jGeoLocation(-76.226823, 23.642465)) // antarctica
.setHeader(TwitterHeaders.DISPLAY_COORDINATES, true);
Message<String> m = mb.build();
this.messagingTemplate.send(this.channel, m);

View File

@@ -27,7 +27,7 @@ public class TwitterAnnouncer {
}
public void mention(Tweet s) {
System.out.println("A tweet mentioning (or replying) to " + "you was received having text " + s.getText() + " from " + s.getSource());
System.out.println("A tweet mentioning (or replying) to " + "you was received having text " + s.getFromUser() + "-" + s.getText() + " from " + s.getSource());
}
public void updates(Tweet t) {

View File

@@ -19,20 +19,26 @@ package org.springframework.integration.twitter.inbound;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Queue;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.integration.context.IntegrationContextUtils;
import org.springframework.integration.store.PropertiesPersistingMetadataStore;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.integration.twitter.core.Tweet;
import org.springframework.integration.twitter.core.Twitter4jTemplate;
import org.springframework.integration.twitter.core.TwitterOperations;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.util.CollectionUtils;
@@ -42,19 +48,20 @@ import twitter4j.Paging;
import twitter4j.RateLimitStatus;
import twitter4j.ResponseList;
import twitter4j.Twitter;
import twitter4j.TwitterFactory;
/**
* @author Oleg Zhurakousky
*/
public class DirectMessageReceivingMessageSourceTests {
private Tweet firstMessage;
private DirectMessage firstMessage;
private Tweet secondMessage;
private DirectMessage secondMessage;
private Tweet thirdMessage;
private DirectMessage thirdMessage;
private Tweet fourthMessage;
private DirectMessage fourthMessage;
private TwitterOperations twitter;
@@ -63,25 +70,31 @@ public class DirectMessageReceivingMessageSourceTests {
@Before
public void prepare() throws Exception{
twitter = mock(TwitterOperations.class);
firstMessage = mock(Tweet.class);
twitter = new Twitter4jTemplate();
firstMessage = mock(DirectMessage.class);
when(firstMessage.getCreatedAt()).thenReturn(new Date(5555555555L));
when(firstMessage.getId()).thenReturn((long) 200);
secondMessage = mock(Tweet.class);
when(firstMessage.getId()).thenReturn( 200);
secondMessage = mock(DirectMessage.class);
when(secondMessage.getCreatedAt()).thenReturn(new Date(2222222222L));
when(secondMessage.getId()).thenReturn((long) 2000);
when(secondMessage.getId()).thenReturn( 2000);
thirdMessage = mock(Tweet.class);
thirdMessage = mock(DirectMessage.class);
when(thirdMessage.getCreatedAt()).thenReturn(new Date(66666666666L));
when(thirdMessage.getId()).thenReturn((long) 3000);
when(thirdMessage.getId()).thenReturn( 3000);
fourthMessage = mock(Tweet.class);
fourthMessage = mock(DirectMessage.class);
when(fourthMessage.getCreatedAt()).thenReturn(new Date(77777777777L));
when(fourthMessage.getId()).thenReturn((long) 4000);
when(fourthMessage.getId()).thenReturn( 4000);
when(twitter.getProfileId()).thenReturn("kermit");
Twitter tw = mock(Twitter.class);
when(twitter.getUnderlyingTwitter()).thenReturn(tw);
tw = mock(Twitter.class);
Field twField = Twitter4jTemplate.class.getDeclaredField("twitter");
twField.setAccessible(true);
twField.set(twitter, tw);
when(tw.getScreenName()).thenReturn("kermit");
twitter = spy(twitter);
}
@@ -97,6 +110,7 @@ public class DirectMessageReceivingMessageSourceTests {
source.start();
assertEquals("twitter:inbound-dm-channel-adapter.twitterEndpoint.kermit", TestUtils.getPropertyValue(source, "metadataKey"));
assertTrue(source.isRunning());
source.stop();
}
@SuppressWarnings("rawtypes")
@@ -112,24 +126,83 @@ public class DirectMessageReceivingMessageSourceTests {
source.afterPropertiesSet();
source.start();
Thread.sleep(1000);
System.out.println("Tweets: " + TestUtils.getPropertyValue(source, "tweets"));
Queue msg = (Queue) TestUtils.getPropertyValue(source, "tweets");
assertTrue(!CollectionUtils.isEmpty(msg));
assertEquals(1, msg.size()); // because the other message has a older timestamp and is assumed to be read by
Tweet message = (Tweet) msg.poll();
assertEquals(secondMessage, message);
assertEquals(2000, message.getId());
Thread.sleep(1000);
verify(twitter, times(1)).getDirectMessages(2000);
// based on the Mock, the Queue shoud now have 2 mopre messages third and fourth
assertTrue(((Queue)TestUtils.getPropertyValue(source, "tweets")).size() == 2);
source.stop();
}
/**
* This test will validate that last status is initilaized from the metadatastore
* @throws Exception
*/
@SuppressWarnings("rawtypes")
@Test
public void testSuccessfullInitializationWithMessagesWithPersistentMetadata() throws Exception{
String fileName = System.getProperty("java.io.tmpdir") + "/spring-integration/metadata-store.properties";
File file = new File(fileName);
if (file.exists()){
file.delete();
}
this.setUpMockScenarioForMessagePolling();
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
PropertiesPersistingMetadataStore store = new PropertiesPersistingMetadataStore();
store.afterPropertiesSet();
bf.registerSingleton(IntegrationContextUtils.METADATA_STORE_BEAN_NAME, store);
DirectMessageReceivingMessageSource source = new DirectMessageReceivingMessageSource(twitter);
source.setBeanFactory(bf);
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.afterPropertiesSet();
source.setTaskScheduler(scheduler);
source.setBeanName("twitterEndpoint");
source.afterPropertiesSet();
source.start();
Thread.sleep(1000);
Queue msg = (Queue) TestUtils.getPropertyValue(source, "tweets");
assertTrue(!CollectionUtils.isEmpty(msg));
assertEquals(1, msg.size()); // because the other message has a older timestamp and is assumed to be read by
Tweet message = (Tweet) msg.poll();
assertEquals(2000, message.getId());
source.stop();
Thread.sleep(3000);
// Resuming
this.prepare();
this.setUpMockScenarioForMessagePolling();
store.destroy();
bf = new DefaultListableBeanFactory();
store = new PropertiesPersistingMetadataStore();
store.afterPropertiesSet();
bf.registerSingleton(IntegrationContextUtils.METADATA_STORE_BEAN_NAME, store);
source = new DirectMessageReceivingMessageSource(twitter);
source.setBeanFactory(bf);
scheduler = new ThreadPoolTaskScheduler();
scheduler.afterPropertiesSet();
source.setTaskScheduler(scheduler);
source.setBeanName("twitterEndpoint");
source.afterPropertiesSet();
source.start();
Thread.sleep(1000);
msg = (Queue) TestUtils.getPropertyValue(source, "tweets");
assertTrue(!CollectionUtils.isEmpty(msg));
message = (Tweet) msg.poll();
assertEquals(3000, message.getId());
message = (Tweet) msg.poll();
assertEquals(4000, message.getId());
file.delete();
}
@SuppressWarnings("unchecked")
private void setUpMockScenarioForMessagePolling() throws Exception{
RateLimitStatus rateLimitStatus = mock(RateLimitStatus.class);
Twitter tw = mock(Twitter.class);
when(twitter.getUnderlyingTwitter()).thenReturn(tw);
when(tw.getRateLimitStatus()).thenReturn(rateLimitStatus);
when(rateLimitStatus.getSecondsUntilReset()).thenReturn(1000);
when(rateLimitStatus.getRemainingHits()).thenReturn(1000);
@@ -137,12 +210,12 @@ public class DirectMessageReceivingMessageSourceTests {
SampleResoponceList testMessages = new SampleResoponceList();
testMessages.add(firstMessage);
testMessages.add(secondMessage);
when(twitter.getDirectMessages()).thenReturn(testMessages);
when(tw.getDirectMessages()).thenReturn(testMessages);
testMessages = new SampleResoponceList();
testMessages.add(thirdMessage);
testMessages.add(fourthMessage);
when(twitter.getDirectMessages((long)2000)).thenReturn(testMessages);
when(tw.getDirectMessages(Mockito.any(Paging.class))).thenReturn(testMessages);
}
@SuppressWarnings({ "rawtypes", "serial" })

View File

@@ -0,0 +1,234 @@
/*
* 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.inbound;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.Queue;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.integration.context.IntegrationContextUtils;
import org.springframework.integration.store.PropertiesPersistingMetadataStore;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.integration.twitter.core.Tweet;
import org.springframework.integration.twitter.core.Twitter4jTemplate;
import org.springframework.integration.twitter.core.TwitterOperations;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.util.CollectionUtils;
import twitter4j.Paging;
import twitter4j.RateLimitStatus;
import twitter4j.ResponseList;
import twitter4j.Status;
import twitter4j.Twitter;
/**
* @author Oleg Zhurakousky
*/
public class TimelineUpdateReceivingMessageSourceTests {
private Status firstMessage;
private Status secondMessage;
private Status thirdMessage;
private Status fourthMessage;
private TwitterOperations twitter;
Twitter tw;
@Before
public void prepare() throws Exception{
twitter = new Twitter4jTemplate();
firstMessage = mock(Status.class);
when(firstMessage.getCreatedAt()).thenReturn(new Date(5555555555L));
when(firstMessage.getId()).thenReturn( (long) 200);
secondMessage = mock(Status.class);
when(secondMessage.getCreatedAt()).thenReturn(new Date(2222222222L));
when(secondMessage.getId()).thenReturn((long) 2000);
thirdMessage = mock(Status.class);
when(thirdMessage.getCreatedAt()).thenReturn(new Date(66666666666L));
when(thirdMessage.getId()).thenReturn((long) 3000);
fourthMessage = mock(Status.class);
when(fourthMessage.getCreatedAt()).thenReturn(new Date(77777777777L));
when(fourthMessage.getId()).thenReturn( (long)4000);
tw = mock(Twitter.class);
Field twField = Twitter4jTemplate.class.getDeclaredField("twitter");
twField.setAccessible(true);
twField.set(twitter, tw);
when(tw.getScreenName()).thenReturn("kermit");
twitter = spy(twitter);
}
@Test
public void testSuccessfullInitialization() throws Exception{
TimelineUpdateReceivingMessageSource source = new TimelineUpdateReceivingMessageSource(twitter);
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.afterPropertiesSet();
source.setTaskScheduler(scheduler);
source.setBeanName("twitterEndpoint");
source.afterPropertiesSet();
source.start();
assertEquals("twitter:inbound-update-channel-adapter.twitterEndpoint.kermit", TestUtils.getPropertyValue(source, "metadataKey"));
assertTrue(source.isRunning());
source.stop();
}
@SuppressWarnings("rawtypes")
@Test
public void testSuccessfullInitializationWithMessages() throws Exception{
this.setUpMockScenarioForMessagePolling();
TimelineUpdateReceivingMessageSource source = new TimelineUpdateReceivingMessageSource(twitter);
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.afterPropertiesSet();
source.setTaskScheduler(scheduler);
source.setBeanName("twitterEndpoint");
source.afterPropertiesSet();
source.start();
Thread.sleep(1000);
Queue msg = (Queue) TestUtils.getPropertyValue(source, "tweets");
assertTrue(!CollectionUtils.isEmpty(msg));
assertEquals(1, msg.size()); // because the other message has a older timestamp and is assumed to be read by
Tweet message = (Tweet) msg.poll();
assertEquals(2000, message.getId());
Thread.sleep(1000);
verify(twitter, times(1)).getFriendsTimeline(2000);
// based on the Mock, the Queue shoud now have 2 mopre messages third and fourth
assertTrue(((Queue)TestUtils.getPropertyValue(source, "tweets")).size() == 2);
source.stop();
}
/**
* This test will validate that last status is initilaized from the metadatastore
* @throws Exception
*/
@SuppressWarnings("rawtypes")
@Test
public void testSuccessfullInitializationWithMessagesWithPersistentMetadata() throws Exception{
String fileName = System.getProperty("java.io.tmpdir") + "/spring-integration/metadata-store.properties";
File file = new File(fileName);
if (file.exists()){
file.delete();
}
this.setUpMockScenarioForMessagePolling();
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
PropertiesPersistingMetadataStore store = new PropertiesPersistingMetadataStore();
store.afterPropertiesSet();
bf.registerSingleton(IntegrationContextUtils.METADATA_STORE_BEAN_NAME, store);
TimelineUpdateReceivingMessageSource source = new TimelineUpdateReceivingMessageSource(twitter);
source.setBeanFactory(bf);
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.afterPropertiesSet();
source.setTaskScheduler(scheduler);
source.setBeanName("twitterEndpoint");
source.afterPropertiesSet();
source.start();
Thread.sleep(1000);
Queue msg = (Queue) TestUtils.getPropertyValue(source, "tweets");
assertTrue(!CollectionUtils.isEmpty(msg));
assertEquals(1, msg.size()); // because the other message has a older timestamp and is assumed to be read by
Tweet message = (Tweet) msg.poll();
assertEquals(2000, message.getId());
source.stop();
Thread.sleep(3000);
// Resuming
this.prepare();
this.setUpMockScenarioForMessagePolling();
store.destroy();
bf = new DefaultListableBeanFactory();
store = new PropertiesPersistingMetadataStore();
store.afterPropertiesSet();
bf.registerSingleton(IntegrationContextUtils.METADATA_STORE_BEAN_NAME, store);
source = new TimelineUpdateReceivingMessageSource(twitter);
source.setBeanFactory(bf);
scheduler = new ThreadPoolTaskScheduler();
scheduler.afterPropertiesSet();
source.setTaskScheduler(scheduler);
source.setBeanName("twitterEndpoint");
source.afterPropertiesSet();
source.start();
Thread.sleep(1000);
msg = (Queue) TestUtils.getPropertyValue(source, "tweets");
assertTrue(!CollectionUtils.isEmpty(msg));
message = (Tweet) msg.poll();
assertEquals(3000, message.getId());
message = (Tweet) msg.poll();
assertEquals(4000, message.getId());
file.delete();
}
@SuppressWarnings("unchecked")
private void setUpMockScenarioForMessagePolling() throws Exception{
RateLimitStatus rateLimitStatus = mock(RateLimitStatus.class);
when(tw.getRateLimitStatus()).thenReturn(rateLimitStatus);
when(rateLimitStatus.getSecondsUntilReset()).thenReturn(1000);
when(rateLimitStatus.getRemainingHits()).thenReturn(1000);
SampleResoponceList testMessages = new SampleResoponceList();
testMessages.add(firstMessage);
testMessages.add(secondMessage);
when(tw.getFriendsTimeline()).thenReturn(testMessages);
testMessages = new SampleResoponceList();
testMessages.add(thirdMessage);
testMessages.add(fourthMessage);
when(tw.getFriendsTimeline(Mockito.any(Paging.class))).thenReturn(testMessages);
}
@SuppressWarnings({ "rawtypes", "serial" })
public static class SampleResoponceList extends ArrayList implements ResponseList {
@Override
public RateLimitStatus getRateLimitStatus() {
return mock(RateLimitStatus.class);
}
@Override
public RateLimitStatus getFeatureSpecificRateLimitStatus() {
return mock(RateLimitStatus.class);
}
}
}