INT-1471 removed custom thread pools used by inbound adapters in favor of task scheduler with RateLimitStatusTRiger, polishing the code and javadocs. . . more to come
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static junit.framework.Assert.assertNotNull;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.twitter.core.twitter.Twitter4jDirectMessage;
|
||||
import org.springframework.integration.twitter.inbound.InboundDirectMessageEndpoint;
|
||||
import org.springframework.integration.twitter.oauth.OAuthConfiguration;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
|
||||
import twitter4j.DirectMessage;
|
||||
import twitter4j.Paging;
|
||||
import twitter4j.RateLimitStatus;
|
||||
import twitter4j.ResponseList;
|
||||
import twitter4j.Twitter;
|
||||
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
*/
|
||||
public class InboundDirectMessageStatusEndpointTests {
|
||||
private DirectMessage firstMessage;
|
||||
private DirectMessage secondMessage;
|
||||
private Twitter twitter;
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
/*
|
||||
* In order to run this test you need to provide values to the twitter.properties file
|
||||
*/
|
||||
public void testUpdatesWithRealTwitter() throws Exception{
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
new ClassPathXmlApplicationContext("TestRecievingUsingNamespace-context.xml", this.getClass());
|
||||
latch.await(10000, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTwitterMockedUpdates() throws Exception{
|
||||
QueueChannel channel = new QueueChannel();
|
||||
|
||||
InboundDirectMessageEndpoint endpoint = new InboundDirectMessageEndpoint();
|
||||
endpoint.setOutputChannel(channel);
|
||||
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
|
||||
scheduler.afterPropertiesSet();
|
||||
endpoint.setTaskScheduler(scheduler);
|
||||
|
||||
endpoint.setConfiguration(this.getTestConfigurationForDirectMessages());
|
||||
endpoint.afterPropertiesSet();
|
||||
endpoint.start();
|
||||
Message message1 = channel.receive(3000);
|
||||
assertNotNull(message1);
|
||||
assertEquals(secondMessage, ((Twitter4jDirectMessage)message1.getPayload()).getDirectMessage());
|
||||
Message message2 = channel.receive(3000);
|
||||
assertNotNull(message2);
|
||||
assertEquals(firstMessage, ((Twitter4jDirectMessage)message2.getPayload()).getDirectMessage());
|
||||
}
|
||||
|
||||
@Before
|
||||
public void prepare(){
|
||||
twitter = mock(Twitter.class);
|
||||
firstMessage = mock(DirectMessage.class);
|
||||
when(firstMessage.getCreatedAt()).thenReturn(new Date(5555555555L));
|
||||
when(firstMessage.getId()).thenReturn(200);
|
||||
secondMessage = mock(DirectMessage.class);
|
||||
when(secondMessage.getCreatedAt()).thenReturn(new Date(2222222222L));
|
||||
when(secondMessage.getId()).thenReturn(2000);
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package org.springframework.integration.twitter;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.integration.twitter.oauth.OAuthConfiguration;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import twitter4j.Status;
|
||||
import twitter4j.Twitter;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
|
||||
/**
|
||||
* This class is used to simply demonstrating correctly factory-ing a {@link twitter4j.Twitter} instance
|
||||
*
|
||||
* @author Josh Long
|
||||
*/
|
||||
@ContextConfiguration
|
||||
public class SimpleTwitterTestClient {
|
||||
private Twitter twitter;
|
||||
@Autowired
|
||||
private volatile OAuthConfiguration oAuthConfiguration;
|
||||
|
||||
@Before
|
||||
public void begin() throws Exception {
|
||||
this.twitter = oAuthConfiguration.getTwitter();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void testConnectivity() throws Throwable {
|
||||
Collection<Status> responses;
|
||||
Assert.assertNotNull(this.twitter);
|
||||
Assert.assertNotNull(responses = this.twitter.getFriendsTimeline());
|
||||
Assert.assertTrue(responses.size() > 0);
|
||||
}
|
||||
}
|
||||
@@ -39,7 +39,7 @@
|
||||
base-package="org.springframework.integration.twitter"/>
|
||||
|
||||
<context:property-placeholder
|
||||
location="file://${user.home}/Desktop/twitter.properties"
|
||||
location="classpath:twitter.properties"
|
||||
ignore-unresolvable="true"/>
|
||||
|
||||
<channel id="inbound_dm"/>
|
||||
|
||||
@@ -1,3 +1,18 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import org.springframework.integration.twitter.core.DirectMessage;
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* 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.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.scheduling.TriggerContext;
|
||||
|
||||
import twitter4j.RateLimitStatus;
|
||||
import twitter4j.Twitter;
|
||||
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
*/
|
||||
public class RateLimitStatusTriggerTests {
|
||||
|
||||
@Test
|
||||
public void testTriggerImediateAndSubsequentExecutionTime() throws Exception{
|
||||
Twitter twitter = mock(Twitter.class);
|
||||
RateLimitStatusTrigger trigger = new RateLimitStatusTrigger(twitter);
|
||||
TriggerContext context = mock(TriggerContext.class);
|
||||
Date currentDate = new Date(System.currentTimeMillis());
|
||||
Date nextDate = trigger.nextExecutionTime(context);
|
||||
// as long as its within 1 msec we can consider it right away for the purpose of testing
|
||||
assertTrue(nextDate.getTime() - currentDate.getTime() < 100);
|
||||
|
||||
|
||||
RateLimitStatus rateLimitStatis = mock(RateLimitStatus.class);
|
||||
when(twitter.getRateLimitStatus()).thenReturn(rateLimitStatis);
|
||||
when(rateLimitStatis.getRemainingHits()).thenReturn(2000);
|
||||
when(rateLimitStatis.getSecondsUntilReset()).thenReturn(4000);
|
||||
|
||||
when(context.lastCompletionTime()).thenReturn(nextDate);
|
||||
// based on the above values the next execution time should be at least 2000 msec
|
||||
assertTrue(trigger.nextExecutionTime(context).getTime() - nextDate.getTime() > 2000);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
twitter.oauth.consumerKey=
|
||||
twitter.oauth.consumerSecret=
|
||||
twitter.oauth.pin=
|
||||
twitter.oauth.accessToken=
|
||||
twitter.oauth.accessTokenSecret=
|
||||
Reference in New Issue
Block a user