use of org.springframework.social.twitter.api.SearchResults in project spring-integration by spring-projects.
the class TwitterSearchOutboundGateway method handleRequestMessage.
@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
Object args;
if (this.searchArgsExpression != null) {
args = this.searchArgsExpression.getValue(this.evaluationContext, requestMessage);
} else {
args = requestMessage.getPayload();
}
Assert.notNull(args, "The twitter search expression cannot evaluate to 'null'.");
SearchParameters searchParameters;
if (args instanceof SearchParameters) {
searchParameters = (SearchParameters) args;
} else if (args instanceof String) {
searchParameters = new SearchParameters((String) args).count(DEFAULT_PAGE_SIZE);
} else if (args instanceof List) {
List<?> list = (List<?>) args;
Assert.isTrue(list.size() > 0 && list.size() < 5, "Between 1 and 4 search arguments are required");
Assert.isInstanceOf(String.class, list.get(0), "The first search argument (query) must be a String");
searchParameters = new SearchParameters((String) list.get(0));
if (list.size() > 1) {
Assert.isInstanceOf(Number.class, list.get(1), "The second search argument (pageSize) must be a Number");
searchParameters.count(((Number) list.get(1)).intValue());
if (list.size() > 2) {
Assert.isInstanceOf(Number.class, list.get(2), "The third search argument (sinceId) must be a Number");
searchParameters.sinceId(((Number) list.get(2)).longValue());
}
if (list.size() > 3) {
Assert.isInstanceOf(Number.class, list.get(3), "The fourth search argument (maxId) must be a Number");
searchParameters.maxId(((Number) list.get(3)).longValue());
}
}
} else {
throw new IllegalArgumentException("Search Expression must evaluate to a 'SearchParameters', 'String' or 'List'.");
}
SearchResults results = this.getTwitter().searchOperations().search(searchParameters);
if (results != null) {
List<Tweet> tweets = (results.getTweets() != null ? results.getTweets() : Collections.<Tweet>emptyList());
return this.getMessageBuilderFactory().withPayload(tweets).setHeader(TwitterHeaders.SEARCH_METADATA, results.getSearchMetadata());
} else {
return null;
}
}
use of org.springframework.social.twitter.api.SearchResults in project spring-integration by spring-projects.
the class TwitterSearchOutboundGatewayTests method testStringQueryCustomExpression.
@Test
public void testStringQueryCustomExpression() {
this.gateway.setSearchArgsExpression(new SpelExpressionParser().parseExpression("{'bar', 1, 2, 3}"));
Tweet tweet = mock(Tweet.class);
SearchMetadata searchMetadata = mock(SearchMetadata.class);
final SearchResults searchResults = new SearchResults(Collections.singletonList(tweet), searchMetadata);
doAnswer(invocation -> {
SearchParameters searchParameters = invocation.getArgument(0);
assertEquals("bar", searchParameters.getQuery());
assertEquals(Integer.valueOf(1), searchParameters.getCount());
assertEquals(Long.valueOf(2), searchParameters.getSinceId());
assertEquals(Long.valueOf(3), searchParameters.getMaxId());
return searchResults;
}).when(this.searchOps).search(any(SearchParameters.class));
this.gateway.handleMessage(new GenericMessage<String>("foo"));
Message<?> reply = this.outputChannel.receive(0);
assertNotNull(reply);
@SuppressWarnings("unchecked") List<Tweet> tweets = (List<Tweet>) reply.getPayload();
assertEquals(1, tweets.size());
assertSame(tweet, tweets.get(0));
assertSame(searchMetadata, reply.getHeaders().get(TwitterHeaders.SEARCH_METADATA));
}
use of org.springframework.social.twitter.api.SearchResults in project spring-integration by spring-projects.
the class SearchReceivingMessageSource method pollForTweets.
@Override
protected List<Tweet> pollForTweets(long sinceId) {
SearchParameters searchParameters = new SearchParameters(this.query).count(this.getPageSize()).sinceId(sinceId);
SearchResults results = this.getTwitter().searchOperations().search(searchParameters);
return (results != null) ? results.getTweets() : Collections.<Tweet>emptyList();
}
use of org.springframework.social.twitter.api.SearchResults in project spring-integration by spring-projects.
the class SearchReceivingMessageSourceTests method testPollForTweetsThreeResults.
/**
* Verify that a polling operation returns in fact 3 results.
*/
@Test
public void testPollForTweetsThreeResults() {
final TwitterTemplate twitterTemplate;
final SearchOperations so = mock(SearchOperations.class);
final List<Tweet> tweets = new ArrayList<Tweet>();
tweets.add(mock(Tweet.class));
tweets.add(mock(Tweet.class));
tweets.add(mock(Tweet.class));
final SearchResults results = new SearchResults(tweets, new SearchMetadata(111, 111));
twitterTemplate = mock(TwitterTemplate.class);
when(twitterTemplate.searchOperations()).thenReturn(so);
SearchParameters params = new SearchParameters(SEARCH_QUERY).count(20).sinceId(0);
when(twitterTemplate.searchOperations().search(params)).thenReturn(results);
final SearchReceivingMessageSource messageSource = new SearchReceivingMessageSource(twitterTemplate, "foo");
messageSource.setQuery(SEARCH_QUERY);
final List<Tweet> tweetSearchResults = messageSource.pollForTweets(0);
assertNotNull(tweetSearchResults);
assertEquals(3, tweetSearchResults.size());
}
use of org.springframework.social.twitter.api.SearchResults in project spring-integration by spring-projects.
the class TwitterSearchOutboundGatewayTests method testStringQuery.
@Test
public void testStringQuery() {
Tweet tweet = mock(Tweet.class);
SearchMetadata searchMetadata = mock(SearchMetadata.class);
final SearchResults searchResults = new SearchResults(Collections.singletonList(tweet), searchMetadata);
doAnswer(invocation -> {
SearchParameters searchParameters = invocation.getArgument(0);
assertEquals("foo", searchParameters.getQuery());
assertEquals(Integer.valueOf(20), searchParameters.getCount());
return searchResults;
}).when(this.searchOps).search(any(SearchParameters.class));
this.gateway.handleMessage(new GenericMessage<String>("foo"));
Message<?> reply = this.outputChannel.receive(0);
assertNotNull(reply);
@SuppressWarnings("unchecked") List<Tweet> tweets = (List<Tweet>) reply.getPayload();
assertEquals(1, tweets.size());
assertSame(tweet, tweets.get(0));
assertSame(searchMetadata, reply.getHeaders().get(TwitterHeaders.SEARCH_METADATA));
}
Aggregations