Polishing for SearchReceivingMSWithRedisTests

* Add `Log4jLevelAdjuster` for future diagnostics

Rebase and fix unused imports
This commit is contained in:
Artem Bilan
2017-10-16 17:06:57 -04:00
committed by Gary Russell
parent c65584a007
commit 7eea071b4e
2 changed files with 60 additions and 41 deletions

View File

@@ -1,12 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-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
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-twitter="http://www.springframework.org/schema/integration/twitter"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/integration/twitter http://www.springframework.org/schema/integration/twitter/spring-integration-twitter.xsd">
http://www.springframework.org/schema/integration/twitter http://www.springframework.org/schema/integration/twitter/spring-integration-twitter.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<bean class="org.springframework.integration.twitter.inbound.SearchReceivingMessageSourceWithRedisTests$SearchReceivingMessageSourceWithRedisTestsConfig"/>
<int:channel id="inbound_twitter">
<int:queue/>
@@ -18,7 +22,7 @@
channel="inbound_twitter"
metadata-store="redisMetadataStore"
auto-startup="false">
<int:poller fixed-rate="100" max-messages-per-poll="3"/>
<int:poller fixed-delay="100" max-messages-per-poll="3"/>
</int-twitter:search-inbound-channel-adapter>
<bean id="redisMetadataStore" class="org.springframework.integration.redis.metadata.RedisMetadataStore">

View File

@@ -16,10 +16,12 @@
package org.springframework.integration.twitter.inbound;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
@@ -30,19 +32,23 @@ import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;
import org.junit.Before;
import org.apache.log4j.Level;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.integration.endpoint.SourcePollingChannelAdapter;
import org.springframework.integration.metadata.MetadataStore;
import org.springframework.integration.redis.metadata.RedisMetadataStore;
import org.springframework.integration.redis.rules.RedisAvailable;
import org.springframework.integration.redis.rules.RedisAvailableTests;
import org.springframework.integration.test.rule.Log4jLevelAdjuster;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.messaging.Message;
import org.springframework.messaging.PollableChannel;
import org.springframework.social.twitter.api.SearchMetadata;
import org.springframework.social.twitter.api.SearchOperations;
@@ -51,53 +57,48 @@ import org.springframework.social.twitter.api.SearchResults;
import org.springframework.social.twitter.api.Tweet;
import org.springframework.social.twitter.api.UserOperations;
import org.springframework.social.twitter.api.impl.TwitterTemplate;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
/**
* @author Gunnar Hillert
* @author Artem Bilan
* @author Gary Russell
*
* @since 3.0
*/
@ContextConfiguration("SearchReceivingMessageSourceWithRedisTests-context.xml")
@RunWith(SpringRunner.class)
@DirtiesContext
public class SearchReceivingMessageSourceWithRedisTests extends RedisAvailableTests {
@Rule
public Log4jLevelAdjuster adjuster = new Log4jLevelAdjuster(Level.TRACE, "org.springframework.integration");
@Autowired
private SourcePollingChannelAdapter twitterSearchAdapter;
@Autowired
private AbstractTwitterMessageSource<?> twitterMessageSource;
@Autowired
private MetadataStore metadataStore;
private String metadataKey;
@Autowired
@Qualifier("inbound_twitter")
private PollableChannel tweets;
@Before
public void setup() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(SearchReceivingMessageSourceWithRedisTestsConfig.class);
context.registerShutdownHook();
context.refresh();
this.twitterSearchAdapter = context.getBean(SourcePollingChannelAdapter.class);
this.twitterMessageSource = context.getBean(AbstractTwitterMessageSource.class);
this.metadataStore = context.getBean(MetadataStore.class);
this.tweets = context.getBean("inbound_twitter", PollableChannel.class);
this.metadataKey = TestUtils.getPropertyValue(twitterSearchAdapter, "source.metadataKey", String.class);
@Test
@RedisAvailable
public void testPollForTweetsThreeResultsWithRedisMetadataStore() throws Exception {
String metadataKey = TestUtils.getPropertyValue(twitterSearchAdapter, "source.metadataKey", String.class);
// There is need to set a value, not 'remove' and re-init 'twitterMessageSource'
this.metadataStore.put(metadataKey, "-1");
this.twitterMessageSource.afterPropertiesSet();
context.close();
}
/**
* Verify that a polling operation returns in fact 3 results.
* @throws Exception
*/
@Test
@RedisAvailable
public void testPollForTweetsThreeResultsWithRedisMetadataStore() throws Exception {
MetadataStore metadataStore = TestUtils.getPropertyValue(this.twitterSearchAdapter, "source.metadataStore",
MetadataStore.class);
assertTrue("Expected metadataStore to be an instance of RedisMetadataStore",
@@ -108,9 +109,14 @@ public class SearchReceivingMessageSourceWithRedisTests extends RedisAvailableTe
this.twitterSearchAdapter.start();
assertNotNull(this.tweets.receive(10000));
assertNotNull(this.tweets.receive(10000));
assertNotNull(this.tweets.receive(10000));
Message<?> receive = this.tweets.receive(10000);
assertNotNull(receive);
receive = this.tweets.receive(10000);
assertNotNull(receive);
receive = this.tweets.receive(10000);
assertNotNull(receive);
/* We received 3 messages so far. When invoking receive() again the search
* will return again the 3 test Tweets but as we already processed them
@@ -129,8 +135,15 @@ public class SearchReceivingMessageSourceWithRedisTests extends RedisAvailableTe
this.twitterSearchAdapter.start();
assertNotNull(this.tweets.receive(10000));
assertNotNull(this.tweets.receive(10000));
receive = this.tweets.receive(10000);
assertNotNull(receive);
assertThat(receive.getPayload(), instanceOf(Tweet.class));
assertEquals(((Tweet) receive.getPayload()).getId(), "2");
receive = this.tweets.receive(10000);
assertNotNull(receive);
assertThat(receive.getPayload(), instanceOf(Tweet.class));
assertEquals(((Tweet) receive.getPayload()).getId(), "3");
assertNull(this.tweets.receive(0));
@@ -140,8 +153,7 @@ public class SearchReceivingMessageSourceWithRedisTests extends RedisAvailableTe
}
@Configuration
@ImportResource("org/springframework/integration/twitter/inbound/SearchReceivingMessageSourceWithRedisTests-context.xml")
static class SearchReceivingMessageSourceWithRedisTestsConfig {
public static class SearchReceivingMessageSourceWithRedisTestsConfig {
@Bean(name = "twitterTemplate")
public TwitterTemplate twitterTemplate() {
@@ -152,14 +164,17 @@ public class SearchReceivingMessageSourceWithRedisTests extends RedisAvailableTe
Tweet tweet3 = mock(Tweet.class);
given(tweet3.getId()).willReturn("3");
given(tweet3.getCreatedAt()).willReturn(new GregorianCalendar(2013, 2, 20).getTime());
given(tweet3.toString()).will(invocation -> "Mock for Tweet: " + tweet3.getId());
Tweet tweet1 = mock(Tweet.class);
given(tweet1.getId()).willReturn("1");
given(tweet1.getCreatedAt()).willReturn(new GregorianCalendar(2013, 0, 20).getTime());
given(tweet1.toString()).will(invocation -> "Mock for Tweet: " + tweet1.getId());
final Tweet tweet2 = mock(Tweet.class);
given(tweet2.getId()).willReturn("2");
given(tweet2.getCreatedAt()).willReturn(new GregorianCalendar(2013, 1, 20).getTime());
given(tweet2.toString()).will(invocation -> "Mock for Tweet: " + tweet2.getId());
final List<Tweet> tweets = new ArrayList<Tweet>();