From 7eea071b4ec084a1caa0d3ebee5a587b06df8d3c Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Mon, 16 Oct 2017 17:06:57 -0400 Subject: [PATCH] Polishing for SearchReceivingMSWithRedisTests * Add `Log4jLevelAdjuster` for future diagnostics Rebase and fix unused imports --- ...ingMessageSourceWithRedisTests-context.xml | 18 ++-- ...hReceivingMessageSourceWithRedisTests.java | 83 +++++++++++-------- 2 files changed, 60 insertions(+), 41 deletions(-) diff --git a/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/inbound/SearchReceivingMessageSourceWithRedisTests-context.xml b/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/inbound/SearchReceivingMessageSourceWithRedisTests-context.xml index b9d4d6b009..1f0cd01112 100644 --- a/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/inbound/SearchReceivingMessageSourceWithRedisTests-context.xml +++ b/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/inbound/SearchReceivingMessageSourceWithRedisTests-context.xml @@ -1,12 +1,16 @@ + 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"> + + + + @@ -18,7 +22,7 @@ channel="inbound_twitter" metadata-store="redisMetadataStore" auto-startup="false"> - + diff --git a/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/inbound/SearchReceivingMessageSourceWithRedisTests.java b/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/inbound/SearchReceivingMessageSourceWithRedisTests.java index b179a6a7fa..85c0145021 100644 --- a/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/inbound/SearchReceivingMessageSourceWithRedisTests.java +++ b/spring-integration-twitter/src/test/java/org/springframework/integration/twitter/inbound/SearchReceivingMessageSourceWithRedisTests.java @@ -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 tweets = new ArrayList();