From 0efd494337d0b3a45b79a6cd65da79e585042e27 Mon Sep 17 00:00:00 2001 From: erenavsarogullari Date: Sun, 24 Jan 2016 23:02:52 +0000 Subject: [PATCH] INTEXT-158: JavaConfig tests and upgrades JIRA: https://jira.spring.io/browse/INTEXT-158 * Provide the test cases to demonstrate the Spring Integration Hazelcast adapters usage from the JavaConfig * Upgrade to Hazelcast-3.6, SI-4.2.4, Gradle-2.5 * Fix Hazelcast-3.6 compatibility * Fix failing tests --- spring-integration-hazelcast/README.md | 170 +++- spring-integration-hazelcast/build.gradle | 37 +- .../gradle/wrapper/gradle-wrapper.jar | Bin 52266 -> 52271 bytes .../gradle/wrapper/gradle-wrapper.properties | 4 +- ...zelcastIntegrationDefinitionValidator.java | 4 +- ...ributedSQLInboundChannelAdapterParser.java | 4 +- .../AbstractHazelcastTestSupport.java | 56 -- ...rationDefinitionValidatorTests-context.xml | 20 + ...stIntegrationDefinitionValidatorTests.java | 119 +++ ...tributedMapInboundChannelAdapterTests.java | 258 +++--- ...sterMonitorInboundChannelAdapterTests.java | 256 +++--- ...EventDrivenInboundChannelAdapterTests.java | 103 +-- ...EventDrivenInboundChannelAdapterTests.java | 221 +++-- ...EventDrivenInboundChannelAdapterTests.java | 116 +-- ...tributedSQLInboundChannelAdapterTests.java | 129 +-- ...EventDrivenInboundChannelAdapterTests.java | 116 +-- ...EventDrivenInboundChannelAdapterTests.java | 32 +- ...EventDrivenInboundChannelAdapterTests.java | 167 ++-- ...EventDrivenInboundChannelAdapterTests.java | 239 ++--- ...edMapInboundChannelAdapterConfigTests.java | 87 ++ ...nitorInboundChannelAdapterConfigTests.java | 65 ++ ...edSQLInboundChannelAdapterConfigTests.java | 99 +++ ...rivenInboundChannelAdapterConfigTests.java | 156 ++++ ...stIntegrationInboundTestConfiguration.java | 379 ++++++++ ...zelcastInboundChannelAdapterTestUtils.java | 404 +++++++++ .../HazelcastOutboundChannelAdapterTests.java | 821 ++++++++---------- ...tIntegrationOutboundTestConfiguration.java | 300 +++++++ ...castOutboundChannelAdapterConfigTests.java | 199 +++++ ...elcastOutboundChannelAdapterTestUtils.java | 247 ++++++ 29 files changed, 3361 insertions(+), 1447 deletions(-) delete mode 100644 spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/AbstractHazelcastTestSupport.java create mode 100644 spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/HazelcastIntegrationDefinitionValidatorTests-context.xml create mode 100644 spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/HazelcastIntegrationDefinitionValidatorTests.java create mode 100644 spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/config/HazelcastCQDistributedMapInboundChannelAdapterConfigTests.java create mode 100644 spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/config/HazelcastClusterMonitorInboundChannelAdapterConfigTests.java create mode 100644 spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/config/HazelcastDistributedSQLInboundChannelAdapterConfigTests.java create mode 100644 spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/config/HazelcastEventDrivenInboundChannelAdapterConfigTests.java create mode 100644 spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/config/HazelcastIntegrationInboundTestConfiguration.java create mode 100644 spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/util/HazelcastInboundChannelAdapterTestUtils.java create mode 100644 spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/outbound/config/HazelcastIntegrationOutboundTestConfiguration.java create mode 100644 spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/outbound/config/HazelcastOutboundChannelAdapterConfigTests.java create mode 100644 spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/outbound/util/HazelcastOutboundChannelAdapterTestUtils.java diff --git a/spring-integration-hazelcast/README.md b/spring-integration-hazelcast/README.md index d746979..c01d6cc 100644 --- a/spring-integration-hazelcast/README.md +++ b/spring-integration-hazelcast/README.md @@ -19,7 +19,9 @@ It also provides event listeners in order to listen to the modifications perform * com.hazelcast.core.ItemListener * com.hazelcast.core.MessageListener -Hazelcast Event-Driven Inbound Channel Adapter listens related cache events and sends event messages to defined channel. Its basic definition is as follows : +Hazelcast Event-Driven Inbound Channel Adapter listens related cache events and sends event messages to defined channel. It supports both XML and JavaConfig driven configurations. + +#### XML Driven Configuration : ``` ``` -**Reference :** http://docs.hazelcast.org/docs/3.4/manual/html-single/hazelcast-documentation.html#distributed-data-structures + +#### JavaConfig Driven Configuration : + +The following sample shows Distributed Map configuration. Same configuration can be used for other distributed data structures(IMap, MultiMap, ReplicatedMap, IList, ISet, IQueue and ITopic). +``` +@Bean +public PollableChannel distributedMapChannel() { + return new QueueChannel(); +} + +@Bean +public IMap distributedMap() { + return hazelcastInstance().getMap("Distributed_Map"); +} + +@Bean +public HazelcastInstance hazelcastInstance() { + return Hazelcast.newHazelcastInstance(); +} + +@Bean +public HazelcastEventDrivenMessageProducer hazelcastEventDrivenMessageProducer() { + final HazelcastEventDrivenMessageProducer producer = new HazelcastEventDrivenMessageProducer(distributedMap()); + producer.setOutputChannel(distributedMapChannel()); + producer.setCacheEventTypes("ADDED,REMOVED,UPDATED,CLEAR_ALL"); + producer.setCacheListeningPolicy(CacheListeningPolicyType.SINGLE); + + return producer; +} + +``` + +**Reference :** http://docs.hazelcast.org/docs/latest/manual/html/distributed-data-structures.html -## HAZELCAST CONTINUOUS QUERY INBOUND CHANNEL ADAPTER +## HAZELCAST CONTINUOUS QUERY INBOUND CHANNEL ADAPTER -Hazelcast Continuous Query enables to listen to the modifications performed on specific map entries. Hazelcast Continuous Query Inbound Channel Adapter is an event-driven channel adapter and listens to related distributed map events in the light of defined predicate. Its basic definition is as follows : +Hazelcast Continuous Query enables to listen to the modifications performed on specific map entries. Hazelcast Continuous Query Inbound Channel Adapter is an event-driven channel adapter and listens to related distributed map events in the light of defined predicate. It supports both XML and JavaConfig driven configurations. + +#### XML Driven Configuration : ``` ``` -**Reference :** http://docs.hazelcast.org/docs/3.4/manual/html-single/hazelcast-documentation.html#continuous-query +#### JavaConfig Driven Configuration : +``` +@Bean +public PollableChannel cqDistributedMapChannel() { + return new QueueChannel(); +} + +@Bean +public IMap cqDistributedMap() { + return hazelcastInstance().getMap("CQ_Distributed_Map"); +} + +@Bean +public HazelcastInstance hazelcastInstance() { + return Hazelcast.newHazelcastInstance(); +} + +@Bean +public HazelcastContinuousQueryMessageProducer hazelcastContinuousQueryMessageProducer() { + final HazelcastContinuousQueryMessageProducer producer = + new HazelcastContinuousQueryMessageProducer(cqDistributedMap(), "surname=TestSurname"); + producer.setOutputChannel(cqDistributedMapChannel()); + producer.setCacheEventTypes("UPDATED"); + producer.setIncludeValue(false); + + return producer; +} + +``` +**Reference :** http://docs.hazelcast.org/docs/latest/manual/html/continuousquery.html ## HAZELCAST CLUSTER MONITOR INBOUND CHANNEL ADAPTER -Hazelcast Cluster Monitor enables to listen to the modifications performed on cluster. Hazelcast Cluster Monitor Inbound Channel Adapter is an event-driven channel adapter and listens to related Membership, Distributed Object, Migration, Lifecycle and Client events. Its basic definition is as follows : +Hazelcast Cluster Monitor enables to listen to the modifications performed on cluster. Hazelcast Cluster Monitor Inbound Channel Adapter is an event-driven channel adapter and listens to related Membership, Distributed Object, Migration, Lifecycle and Client events. It supports both XML and JavaConfig driven configurations. + +#### XML Driven Configuration : ``` ``` + +#### JavaConfig Driven Configuration : +``` +@Bean +public PollableChannel cmonChannel() { + return new QueueChannel(); +} + +@Bean +public HazelcastInstance hazelcastInstance() { + return Hazelcast.newHazelcastInstance(); +} + +@Bean +public HazelcastClusterMonitorMessageProducer hazelcastClusterMonitorMessageProducer() { + final HazelcastClusterMonitorMessageProducer producer = new HazelcastClusterMonitorMessageProducer(hazelcastInstance()); + producer.setOutputChannel(cmonChannel()); + producer.setMonitorEventTypes("DISTRIBUTED_OBJECT"); + + return producer; +} +``` **Reference :** http://docs.hazelcast.org/docs/latest/manual/html/distributedevents.html ## HAZELCAST DISTRIBUTED-SQL INBOUND CHANNEL ADAPTER -Hazelcast allows to run distributed queries on the distributed map. Hazelcast Distributed SQL Inbound Channel Adapter is a poller-driven inbound channel adapter. It runs defined distributed-sql and returns results in the light of iteration type. Its basic definition is as follows : +Hazelcast allows to run distributed queries on the distributed map. Hazelcast Distributed SQL Inbound Channel Adapter is a poller-driven inbound channel adapter. It runs defined distributed-sql and returns results in the light of iteration type. It supports both XML and JavaConfig driven configurations. + +#### XML Driven Configuration : ``` ``` -**Reference :** http://docs.hazelcast.org/docs/3.4/manual/html-single/hazelcast-documentation.html#query-overview + +#### JavaConfig Driven Configuration : +``` +@Bean +public PollableChannel dsDistributedMapChannel() { + return new QueueChannel(); +} + +@Bean +public IMap dsDistributedMap() { + return hazelcastInstance().getMap("DS_Distributed_Map"); +} + +@Bean +public HazelcastInstance hazelcastInstance() { + return Hazelcast.newHazelcastInstance(); +} + +@Bean +@InboundChannelAdapter(value = "dsDistributedMapChannel", poller = @Poller(maxMessagesPerPoll = "1")) +public HazelcastDistributedSQLMessageSource hazelcastDistributedSQLMessageSource() { + final HazelcastDistributedSQLMessageSource messageSource = + new HazelcastDistributedSQLMessageSource(dsDistributedMap(), + "name='TestName' AND surname='TestSurname'"); + messageSource.setIterationType(DistributedSQLIterationType.ENTRY); + + return messageSource; +} +``` +**Reference :** http://docs.hazelcast.org/docs/latest/manual/html/distributedquery.html ## HAZELCAST OUTBOUND CHANNEL ADAPTER -Hazelcast Outbound Channel Adapter listens its defined channel and writes incoming messages to related distributed cache. It expects one of cache, cache-expression or HazelcastHeaders.CACHE_NAME for distributed object definition. Supported Distributed Objects : IMap, MultiMap, ReplicatedMap, IList, ISet, IQueue and ITopic. Its sample definition is as follows : +Hazelcast Outbound Channel Adapter listens its defined channel and writes incoming messages to related distributed cache. It expects one of cache, cache-expression or HazelcastHeaders.CACHE_NAME for distributed object definition. Supported Distributed Objects : IMap, MultiMap, ReplicatedMap, IList, ISet, IQueue and ITopic. It supports both XML and JavaConfig driven configurations. + +#### XML Driven Configuration : ``` ``` @@ -306,3 +428,33 @@ By setting distributed object name in the header, messages can be written to dif **OR** If **cache** or **cache-expression** attributes are not defined, HazelcastHeaders.CACHE_NAME has to be set in Message. + +#### JavaConfig Driven Configuration : +``` +@Bean +public MessageChannel distributedMapChannel() { + return new DirectChannel(); +} + +@Bean +public IMap distributedMap() { + return hzInstance().getMap("Distributed_Map"); +} + +@Bean +public HazelcastInstance hzInstance() { + return Hazelcast.newHazelcastInstance(); +} + +@Bean +@ServiceActivator(inputChannel = "distributedMapChannel") +public HazelcastCacheWritingMessageHandler hazelcastCacheWritingMessageHandler() { + final HazelcastCacheWritingMessageHandler handler = new HazelcastCacheWritingMessageHandler(); + handler.setDistributedObject(distributedMap()); + handler.setKeyExpression(new SpelExpressionParser().parseExpression("payload.id")); + handler.setExtractPayload(true); + + return handler; +} + +``` diff --git a/spring-integration-hazelcast/build.gradle b/spring-integration-hazelcast/build.gradle index e504eb2..f242144 100644 --- a/spring-integration-hazelcast/build.gradle +++ b/spring-integration-hazelcast/build.gradle @@ -4,6 +4,7 @@ apply plugin: 'java' apply from: "${rootProject.projectDir}/publish-maven.gradle" apply plugin: 'eclipse' apply plugin: 'idea' +apply plugin: 'jacoco' group = 'org.springframework.integration' @@ -38,10 +39,9 @@ if (project.hasProperty('platformVersion')) { sourceCompatibility = targetCompatibility = 1.7 ext { - hazelcastVersion = '3.5' - jacocoVersion = '0.7.5.201505241946' - slf4jVersion = '1.7.12' - springIntegrationVersion = '4.1.5.RELEASE' + hazelcastVersion = '3.6' + slf4jVersion = '1.7.13' + springIntegrationVersion = '4.2.4.RELEASE' idPrefix = 'hazelcast' @@ -63,8 +63,8 @@ sourceSets { } } -configurations { - jacoco //Configuration Group used by Sonar to provide Code Coverage using JaCoCo +jacoco { + toolVersion = "0.7.5.201505241946" } dependencies { @@ -76,8 +76,6 @@ dependencies { testCompile "org.springframework.integration:spring-integration-test:$springIntegrationVersion" testRuntime "org.slf4j:slf4j-log4j12:$slf4jVersion" - - jacoco "org.jacoco:org.jacoco.agent:$jacocoVersion:runtime" } // enable all compiler warnings; individual projects may customize further @@ -86,8 +84,21 @@ dependencies { test { // suppress all console output during testing unless running `gradle -i` logging.captureStandardOutput(LogLevel.INFO) - jvmArgs "-javaagent:${configurations.jacoco.asPath}=destfile=${buildDir}/jacoco.exec,includes=*", - "-Dhazelcast.logging.type=slf4j" + jvmArgs "-Dhazelcast.logging.type=slf4j" + + maxHeapSize = "1024m" + jacoco { + append = false + destinationFile = file("$buildDir/jacoco.exec") + } +} + +jacocoTestReport { + reports { + xml.enabled false + csv.enabled false + html.destination "${buildDir}/reports/jacoco/html" + } } task sourcesJar(type: Jar) { @@ -244,9 +255,3 @@ task dist(dependsOn: assemble) { group = 'Distribution' description = 'Builds -dist, -docs and -schema distribution archives.' } - -task wrapper(type: Wrapper) { - description = 'Generates gradlew[.bat] scripts' - gradleVersion = '2.4' - distributionUrl = "http://services.gradle.org/distributions/gradle-${gradleVersion}-all.zip" -} diff --git a/spring-integration-hazelcast/gradle/wrapper/gradle-wrapper.jar b/spring-integration-hazelcast/gradle/wrapper/gradle-wrapper.jar index b5166dad4d90021f6a0b45268c0755719f1d5cd4..30d399d8d2bf522ff5de94bf434a7cc43a9a74b5 100644 GIT binary patch delta 1828 zcmZuxdrXs86u=}&0Yh0A9D^~V27JID zgX;{{3W{tH4OpPEg#wnASHbGkF|GkgT*h-m3h+qI01o6# zGT1JlKBj;;;P5*T`Md@o+P(Omj=;#~HL=X7yPO9M-}?)eIpOn;6EJ^?HqHklZn+!l zvpKZf1(-?YG;7?9OPm!_nH>U8NX;S`ZrNdq|u1;OMX@g2VC9|pJ`XssDpje?- zgryYh5zJG|74p#AGU@P@cvWyOlAwRUQ%7SO}*5%Gts05`m${Z zp+z_#O}y8*iz{}&JN>Tz8Qy*|Pt_S)bz)u465)xEA2^bU^^a$CJ4%e&lRs)7<{kef z$5wlM*#-B%gYIh;4<{dF@ib+N1i3j4pPNhlzSmtZs_@QeP|8G6N7tqDkHl+|qpRzO z>h6C~84_6(enWX=>NI+d+oyE)&p;hB2;+LgkwEuxj5?60^vzVJeAZmTY*{3q$D zN7qMZYKNZ~4-BR&f_;TYS0yGleT5RvEp)Nm@;FuYG+VzX_c<@Rz*tl;wksxmn8Sh#g5m-O4N+Iom$3_O;82Z+9m=RmX%WZhwnww;&*SFzb5J{Y;XMO-i{ zH4&^aTzH)a%&C$mtn+`q+bDf!F zY1KEL5T`xaoCSpq%`M}8Lg%%4Ss+|fUg zUbw(4WPS}?Wu`f6akKd?I8B*p>T)`6)h)qti>DuxXk(P~>rF&u+7Y4Eb0P>C_H|}} zW4T2Fv3d(lb!7oHWsw3!_t8|-PTadQ1kd!l0aZ*WWj1(q_DP}4#d+SXHj?2c$*|Hc zLnh|*M?+YAKTYk|Kv<7Ph|@>hpg8|xYiczQA*}$R0J~6AAxXO!Pxbpiyz2l>9V?kF z&JmB5EMPR?>LYT6OL1hQKh_U;Kt&aiVIO`*2J6a2XqBC89-bVCgRo_TG_~d+DCQ5Y z0O~ZM?j0tSKb{z5m0mmzghK3;YVd-gC}<+)>ic)AN&h<1h`mo`-E75fxPHh3oQnc} zPR?{7e{8M@Mc4`MuE#BAUwm&!4#oIcX-e8i+6sO#dqM@Dog0G5>JQyY9Ae#kNw?Xg zhCn+>G3E@1gWBq0n%dU_q1`R?(xikJ4~bb4j3Ttmz-EZVn|2cYZTMDO1Qrgm!xoIt yRE+_*8bashpHHYiJBYLt%fET;6=`_yMDlI&71>Fbb>iAiG4{IT?&xe}ZvF+r(~xTb delta 1820 zcmZvb3rv$&6vr>$*V4k;6;Kqcj+e?yp(3(JMMN?(XnDv>6p*Q7Wf?DP*i4iWQMa)v z2v;V{a7qzzD6jxylNKqDQeHDtr%MI|$qi#9Ksj z;(b511TwN^ng#CMExogFDVS}P;^E9(pejY8}J20i`JY5zuG+L&t)!ft-_YcM-cs;iR zp~T+(x?r%MSlgQ+WQVYXROnrKk)RrLC$9Jz+o8>3q-n@Y55oo!OO8{ek`) z+2DRd=5<3=YRQ9RvqdYSu1=0g9L@}jbyLBqPLirgnR@i0#aP*-^7a->$6HS}#9Q8f z_D|>!t9I~zclj;vc+IjuUJ4LPp9mJto2(W)C9Y$MHbf?V%5VW5*gX!wnEVVtL(krE z8hn?6&0tEQau7n0p6S3sP^gyE;K%G+d>|EvW@z&HoZA8*2an&NMES{OYB-aB$p)q> z%N|qm<+-~wIIL;n!PKTImO9I;AJHkVnk4E|P#Z{dZr1-qNo8XU&6#b&tOa+N;*FzK zZ{pJCfPh8U!zJi{u1A8&r7&Zbvh!~T+i8909}kYvzQJ8rT%`2|?a=yx&T6-S`s>0$ z3v`*B7Po}KFtQ~c^m5B?P|-CyR$WVgvEf=Ms7>p3(05xEppC6GPoj^8af&_`v_(&S z#clgwe4tHfG5}<4-zVb;qYM7L!xdWAT?z9kBHuYmzTZ{kkJ~UwI&^&%E5IxZdltzYkRw-W~q3rD+CtileVKNlq`%# z78Yz$wi$;TW5N1(W#e*{4MJTWi>)W}Ne|BWo)Ml6o-s<9k}_P_=Y|8i?O_24-BKq1 z++3&TxV+nru8ZI_4LKoud_lANg;T<3b+$Uvl_TkDok>?~aA1!dEOL9KjA!+{v!TZs z@C3oH)DXN9U)4w?i_RYh_DY$LYN1MBE5&8KbW<)9++L4I>g6x=vfef|Pe6Ln@(}Vd z>7@jF^##H%1~&<({aJ*1EfA8M> Set validateEnumType(final Class enumType, final String types) { - Set typeSet = StringUtils.commaDelimitedListToSet(types); + Set typeSet = StringUtils.commaDelimitedListToSet(StringUtils.trimAllWhitespace(types)); for (String type : typeSet) { Enum.valueOf(enumType, type); } diff --git a/spring-integration-hazelcast/src/main/java/org/springframework/integration/hazelcast/config/xml/HazelcastDistributedSQLInboundChannelAdapterParser.java b/spring-integration-hazelcast/src/main/java/org/springframework/integration/hazelcast/config/xml/HazelcastDistributedSQLInboundChannelAdapterParser.java index 918b5cc..15c30a2 100644 --- a/spring-integration-hazelcast/src/main/java/org/springframework/integration/hazelcast/config/xml/HazelcastDistributedSQLInboundChannelAdapterParser.java +++ b/spring-integration-hazelcast/src/main/java/org/springframework/integration/hazelcast/config/xml/HazelcastDistributedSQLInboundChannelAdapterParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 the original author or authors. + * Copyright 2015-2016 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. @@ -28,7 +28,7 @@ import org.springframework.util.StringUtils; /** * Hazelcast Distributed SQL Inbound Channel Adapter Parser parses - * {@code } configuration. + * {@code } configuration. * * @author Eren Avsarogullari * @since 1.0.0 diff --git a/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/AbstractHazelcastTestSupport.java b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/AbstractHazelcastTestSupport.java deleted file mode 100644 index bac6cc7..0000000 --- a/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/AbstractHazelcastTestSupport.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2015 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.hazelcast; - -import org.junit.Assert; - -import org.springframework.integration.hazelcast.message.EntryEventMessagePayload; -import org.springframework.messaging.Message; - -import com.hazelcast.core.EntryEventType; - -/** - * Base Class for Hazelcast Test Support - * - * @author Eren Avsarogullari - * @since 1.0.0 - */ -public class AbstractHazelcastTestSupport { - - protected void verifyEntryEvent(Message msg, String cacheName, EntryEventType event) { - Assert.assertNotNull(msg); - Assert.assertNotNull(msg.getPayload()); - if (event == EntryEventType.CLEAR_ALL || event == EntryEventType.EVICT_ALL) { - Assert.assertTrue(msg.getPayload() instanceof Integer); - } - else { - Assert.assertTrue(msg.getPayload() instanceof EntryEventMessagePayload); - } - - Assert.assertEquals(cacheName, msg.getHeaders().get(HazelcastHeaders.CACHE_NAME)); - Assert.assertEquals(event.name(), msg.getHeaders().get(HazelcastHeaders.EVENT_TYPE)); - Assert.assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); - } - - protected void verifyItemEvent(Message msg, EntryEventType event) { - Assert.assertNotNull(msg); - Assert.assertNotNull(msg.getPayload()); - Assert.assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); - Assert.assertEquals(event.toString(), msg.getHeaders().get(HazelcastHeaders.EVENT_TYPE).toString()); - } - -} diff --git a/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/HazelcastIntegrationDefinitionValidatorTests-context.xml b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/HazelcastIntegrationDefinitionValidatorTests-context.xml new file mode 100644 index 0000000..cfcc065 --- /dev/null +++ b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/HazelcastIntegrationDefinitionValidatorTests-context.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + diff --git a/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/HazelcastIntegrationDefinitionValidatorTests.java b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/HazelcastIntegrationDefinitionValidatorTests.java new file mode 100644 index 0000000..5d7e23b --- /dev/null +++ b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/HazelcastIntegrationDefinitionValidatorTests.java @@ -0,0 +1,119 @@ +/* + * Copyright 2015-2016 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.hazelcast; + +import static org.junit.Assert.assertTrue; + +import java.util.HashSet; +import java.util.Set; + +import javax.annotation.Resource; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.hazelcast.core.DistributedObject; +import com.hazelcast.core.IList; + +/** + * Hazelcast Integration Definition Validator Test Class + * @author Eren Avsarogullari + * @since 1.0.0 + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration +@DirtiesContext +public class HazelcastIntegrationDefinitionValidatorTests { + + @Resource + private IList distList; + + @Test + public void testValidateEnumType() { + final String cacheEventTypes = + " ADDED, REMOVED, UPDATED, EVICTED, EVICT_ALL, CLEAR_ALL "; + final Set typeSet = HazelcastIntegrationDefinitionValidator + .validateEnumType(CacheEventType.class, cacheEventTypes); + assertTrue(typeSet.size() == 6); + for (String type : typeSet) { + Enum.valueOf(CacheEventType.class, type); + } + } + + @Test(expected = IllegalArgumentException.class) + public void testValidateEnumTypeWithInvalidValue() { + final String cacheEventTypes = "Invalid_Enum_Type"; + HazelcastIntegrationDefinitionValidator + .validateEnumType(CacheEventType.class, cacheEventTypes); + } + + @Test + public void testValidateCacheEventsByDistributedObject() { + Set cacheEventTypeSet = new HashSet<>(2); + cacheEventTypeSet.add(CacheEventType.ADDED.toString()); + cacheEventTypeSet.add(CacheEventType.REMOVED.toString()); + HazelcastIntegrationDefinitionValidator + .validateCacheEventsByDistributedObject(this.distList, cacheEventTypeSet); + } + + @Test(expected = IllegalArgumentException.class) + public void testValidateCacheEventsByDistributedObjectWithInvalidValue() { + Set cacheEventTypeSet = new HashSet<>(1); + cacheEventTypeSet.add("Invalid_Cache_Event_Type"); + HazelcastIntegrationDefinitionValidator + .validateCacheEventsByDistributedObject(this.distList, cacheEventTypeSet); + } + + @Test + public void testValidateCacheTypeForEventDrivenMessageProducer() { + HazelcastIntegrationDefinitionValidator + .validateCacheTypeForEventDrivenMessageProducer(this.distList); + } + + @Test(expected = IllegalArgumentException.class) + public void testValidateCacheTypeForEventDrivenMessageProducerWithUnexpectedDistObject() { + HazelcastIntegrationDefinitionValidator + .validateCacheTypeForEventDrivenMessageProducer(new DistributedObject() { + + @Override + public String getPartitionKey() { + return null; + } + + @Override + public String getName() { + return null; + } + + @Override + public String getServiceName() { + return null; + } + + @Override + public void destroy() { + + } + + }); + } + +} diff --git a/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/HazelcastCQDistributedMapInboundChannelAdapterTests.java b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/HazelcastCQDistributedMapInboundChannelAdapterTests.java index cd0928b..95cc4a6 100644 --- a/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/HazelcastCQDistributedMapInboundChannelAdapterTests.java +++ b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/HazelcastCQDistributedMapInboundChannelAdapterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 the original author or authors. + * Copyright 2015-2016 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. @@ -16,10 +16,9 @@ package org.springframework.integration.hazelcast.inbound; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertEquals; import javax.annotation.Resource; @@ -27,9 +26,9 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.integration.hazelcast.AbstractHazelcastTestSupport; import org.springframework.integration.hazelcast.HazelcastHeaders; import org.springframework.integration.hazelcast.HazelcastIntegrationTestUser; +import org.springframework.integration.hazelcast.inbound.util.HazelcastInboundChannelAdapterTestUtils; import org.springframework.integration.hazelcast.message.EntryEventMessagePayload; import org.springframework.messaging.Message; import org.springframework.messaging.PollableChannel; @@ -50,166 +49,129 @@ import com.hazelcast.core.IMap; @ContextConfiguration @DirtiesContext @SuppressWarnings("unchecked") -public class HazelcastCQDistributedMapInboundChannelAdapterTests extends AbstractHazelcastTestSupport { +public class HazelcastCQDistributedMapInboundChannelAdapterTests { - @Autowired - private PollableChannel cqMapChannel1; + @Autowired + private PollableChannel cqMapChannel1; - @Autowired - private PollableChannel cqMapChannel2; + @Autowired + private PollableChannel cqMapChannel2; - @Autowired - private PollableChannel cqMapChannel3; + @Autowired + private PollableChannel cqMapChannel3; - @Autowired - private PollableChannel cqMapChannel4; + @Autowired + private PollableChannel cqMapChannel4; - @Autowired - private PollableChannel cqMapChannel5; + @Autowired + private PollableChannel cqMapChannel5; - @Resource - private IMap cqDistributedMap1; + @Resource + private IMap cqDistributedMap1; - @Resource - private IMap cqDistributedMap2; + @Resource + private IMap cqDistributedMap2; - @Resource - private IMap cqDistributedMap3; + @Resource + private IMap cqDistributedMap3; - @Resource - private IMap cqDistributedMap4; + @Resource + private IMap cqDistributedMap4; - @Resource - private IMap cqDistributedMap5; + @Resource + private IMap cqDistributedMap5; - @Test - public void testContinuousQueryForOnlyADDEDEntryEvent() { - cqDistributedMap1.put(1, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1")); - cqDistributedMap1.remove(1); - cqDistributedMap1.put(2, new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2")); - Message msg = cqMapChannel1.receive(2_000); - assertNotNull(msg); - assertNotNull(msg.getPayload()); - assertTrue(msg.getPayload() instanceof EntryEventMessagePayload); - assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); - assertEquals(EntryEventType.ADDED.name(), msg.getHeaders().get(HazelcastHeaders.EVENT_TYPE)); - assertEquals("cqDistributedMap1", msg.getHeaders().get(HazelcastHeaders.CACHE_NAME)); + @Test + public void testContinuousQueryForOnlyADDEDEntryEvent() { + HazelcastInboundChannelAdapterTestUtils + .testEventDrivenForADDEDDistributedMapEntryEvent(cqDistributedMap1, + cqMapChannel1, "cqDistributedMap1"); + } - assertEquals(Integer.valueOf(1), - ((EntryEventMessagePayload) msg - .getPayload()).key); - assertEquals(1, - (((EntryEventMessagePayload) msg - .getPayload()).value).getId()); - assertEquals("TestName1", - (((EntryEventMessagePayload) msg - .getPayload()).value).getName()); - assertEquals("TestSurname1", - (((EntryEventMessagePayload) msg - .getPayload()).value).getSurname()); - } + @Test + public void testContinuousQueryForOnlyREMOVEDEntryEvent() { + cqDistributedMap2 + .put(1, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1")); + cqDistributedMap2 + .put(2, new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2")); + cqDistributedMap2.remove(2); + Message msg = + cqMapChannel2.receive(HazelcastInboundChannelAdapterTestUtils.TIMEOUT); + assertNotNull(msg); + assertNotNull(msg.getPayload()); + assertTrue(msg.getPayload() instanceof EntryEventMessagePayload); + assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); + assertEquals(EntryEventType.REMOVED.name(), + msg.getHeaders().get(HazelcastHeaders.EVENT_TYPE)); + assertEquals("cqDistributedMap2", + msg.getHeaders().get(HazelcastHeaders.CACHE_NAME)); - @Test - public void testContinuousQueryForOnlyREMOVEDEntryEvent() { - cqDistributedMap2.put(1, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1")); - cqDistributedMap2.put(2, new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2")); - cqDistributedMap2.remove(2); - Message msg = cqMapChannel2.receive(2_000); - assertNotNull(msg); - assertNotNull(msg.getPayload()); - assertTrue(msg.getPayload() instanceof EntryEventMessagePayload); - assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); - assertEquals(EntryEventType.REMOVED.name(), msg.getHeaders().get(HazelcastHeaders.EVENT_TYPE)); - assertEquals("cqDistributedMap2", msg.getHeaders().get(HazelcastHeaders.CACHE_NAME)); + assertEquals(Integer.valueOf(2), + ((EntryEventMessagePayload) msg + .getPayload()).key); + assertEquals(2, + (((EntryEventMessagePayload) msg + .getPayload()).oldValue).getId()); + assertEquals("TestName2", + (((EntryEventMessagePayload) msg + .getPayload()).oldValue).getName()); + assertEquals("TestSurname2", + (((EntryEventMessagePayload) msg + .getPayload()).oldValue).getSurname()); + } - assertEquals(Integer.valueOf(2), - ((EntryEventMessagePayload) msg - .getPayload()).key); - assertEquals(2, - (((EntryEventMessagePayload) msg - .getPayload()).oldValue).getId()); - assertEquals("TestName2", - (((EntryEventMessagePayload) msg - .getPayload()).oldValue).getName()); - assertEquals("TestSurname2", - (((EntryEventMessagePayload) msg - .getPayload()).oldValue).getSurname()); - } + @Test + public void testContinuousQueryForALLEntryEvent() { + HazelcastInboundChannelAdapterTestUtils + .testEventDrivenForDistributedMapEntryEvents(cqDistributedMap3, + cqMapChannel3, "cqDistributedMap3"); + } - @Test - public void testContinuousQueryForALLEntryEvent() { - cqDistributedMap3.put(1, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1")); - Message msg = cqMapChannel3.receive(2_000); - verifyEntryEvent(msg, "cqDistributedMap3", EntryEventType.ADDED); + @Test + public void testContinuousQueryForOnlyUPDATEDEntryEvent() { + cqDistributedMap4 + .put(1, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1")); + cqDistributedMap4 + .put(1, new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2")); + Message msg = + cqMapChannel4.receive(HazelcastInboundChannelAdapterTestUtils.TIMEOUT); + assertNotNull(msg); + assertNotNull(msg.getPayload()); + assertTrue(msg.getPayload() instanceof EntryEventMessagePayload); + assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); + assertEquals(EntryEventType.UPDATED.name(), + msg.getHeaders().get(HazelcastHeaders.EVENT_TYPE)); + assertEquals("cqDistributedMap4", + msg.getHeaders().get(HazelcastHeaders.CACHE_NAME)); - cqDistributedMap3.put(1, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurnameUpdated")); - msg = cqMapChannel3.receive(2_000); - verifyEntryEvent(msg, "cqDistributedMap3", EntryEventType.UPDATED); + assertEquals(Integer.valueOf(1), + ((EntryEventMessagePayload) msg + .getPayload()).key); + assertEquals(1, + (((EntryEventMessagePayload) msg + .getPayload()).oldValue).getId()); + assertEquals("TestName1", + (((EntryEventMessagePayload) msg + .getPayload()).oldValue).getName()); + assertEquals("TestSurname1", + (((EntryEventMessagePayload) msg + .getPayload()).oldValue).getSurname()); + assertEquals(2, + (((EntryEventMessagePayload) msg + .getPayload()).value).getId()); + assertEquals("TestName2", + (((EntryEventMessagePayload) msg + .getPayload()).value).getName()); + assertEquals("TestSurname2", + (((EntryEventMessagePayload) msg + .getPayload()).value).getSurname()); + } - cqDistributedMap3.remove(1); - msg = cqMapChannel3.receive(2_000); - verifyEntryEvent(msg, "cqDistributedMap3", EntryEventType.REMOVED); - - cqDistributedMap3.put(2, new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2")); - msg = cqMapChannel3.receive(2_000); - verifyEntryEvent(msg, "cqDistributedMap3", EntryEventType.ADDED); - - cqDistributedMap3.clear(); - msg = cqMapChannel3.receive(2_000); - verifyEntryEvent(msg, "cqDistributedMap3", EntryEventType.CLEAR_ALL); - } - - @Test - public void testContinuousQueryForOnlyUPDATEDEntryEvent() { - cqDistributedMap4.put(1, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1")); - cqDistributedMap4.put(1, new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2")); - Message msg = cqMapChannel4.receive(2_000); - assertNotNull(msg); - assertNotNull(msg.getPayload()); - assertTrue(msg.getPayload() instanceof EntryEventMessagePayload); - assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); - assertEquals(EntryEventType.UPDATED.name(), msg.getHeaders().get(HazelcastHeaders.EVENT_TYPE)); - assertEquals("cqDistributedMap4", msg.getHeaders().get(HazelcastHeaders.CACHE_NAME)); - - assertEquals(Integer.valueOf(1), - ((EntryEventMessagePayload) msg - .getPayload()).key); - assertEquals(1, - (((EntryEventMessagePayload) msg - .getPayload()).oldValue).getId()); - assertEquals("TestName1", - (((EntryEventMessagePayload) msg - .getPayload()).oldValue).getName()); - assertEquals("TestSurname1", - (((EntryEventMessagePayload) msg - .getPayload()).oldValue).getSurname()); - assertEquals(2, - (((EntryEventMessagePayload) msg - .getPayload()).value).getId()); - assertEquals("TestName2", - (((EntryEventMessagePayload) msg - .getPayload()).value).getName()); - assertEquals("TestSurname2", - (((EntryEventMessagePayload) msg - .getPayload()).value).getSurname()); - } - - @Test - public void testContinuousQueryForOnlyUPDATEDEntryEventWhenIncludeValueIsFalse() { - cqDistributedMap5.put(1, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1")); - cqDistributedMap5.put(1, new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2")); - Message msg = cqMapChannel5.receive(2_000); - assertNotNull(msg); - assertNotNull(msg.getPayload()); - assertTrue(msg.getPayload() instanceof EntryEventMessagePayload); - assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); - assertEquals(EntryEventType.UPDATED.name(), msg.getHeaders().get(HazelcastHeaders.EVENT_TYPE)); - assertEquals("cqDistributedMap5", msg.getHeaders().get(HazelcastHeaders.CACHE_NAME)); - - assertEquals(Integer.valueOf(1), - ((EntryEventMessagePayload) msg.getPayload()).key); - assertNull(((EntryEventMessagePayload) msg.getPayload()).oldValue); - assertNull(((EntryEventMessagePayload) msg.getPayload()).value); - } + @Test + public void testContinuousQueryForOnlyUPDATEDEntryEventWhenIncludeValueIsFalse() { + HazelcastInboundChannelAdapterTestUtils + .testContinuousQueryForUPDATEDEntryEventWhenIncludeValueIsFalse( + cqDistributedMap5, cqMapChannel5, "cqDistributedMap5"); + } } diff --git a/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/HazelcastClusterMonitorInboundChannelAdapterTests.java b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/HazelcastClusterMonitorInboundChannelAdapterTests.java index 2a5f3a5..bb4d455 100644 --- a/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/HazelcastClusterMonitorInboundChannelAdapterTests.java +++ b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/HazelcastClusterMonitorInboundChannelAdapterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 the original author or authors. + * Copyright 2015-2016 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. @@ -16,14 +16,15 @@ package org.springframework.integration.hazelcast.inbound; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertEquals; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.integration.hazelcast.inbound.util.HazelcastInboundChannelAdapterTestUtils; import org.springframework.messaging.Message; import org.springframework.messaging.PollableChannel; import org.springframework.test.annotation.DirtiesContext; @@ -35,13 +36,10 @@ import com.hazelcast.client.config.ClientConfig; import com.hazelcast.config.GroupConfig; import com.hazelcast.core.Client; import com.hazelcast.core.ClientType; -import com.hazelcast.core.DistributedObjectEvent; import com.hazelcast.core.HazelcastInstance; import com.hazelcast.core.IMap; import com.hazelcast.core.LifecycleEvent; -import com.hazelcast.core.Member; import com.hazelcast.core.LifecycleEvent.LifecycleState; -import com.hazelcast.core.MembershipEvent; import com.hazelcast.core.MigrationEvent; /** @@ -55,179 +53,135 @@ import com.hazelcast.core.MigrationEvent; @DirtiesContext public class HazelcastClusterMonitorInboundChannelAdapterTests { - private static final String TEST_GROUP_NAME1 = "Test_Group_Name1"; + private static final String TEST_GROUP_NAME1 = "Test_Group_Name1"; - private static final int TIMEOUT = 10_000; + @Autowired + private PollableChannel cmChannel1; - @Autowired - private PollableChannel cmChannel1; + @Autowired + private PollableChannel cmChannel2; - @Autowired - private PollableChannel cmChannel2; + @Autowired + private PollableChannel cmChannel3; - @Autowired - private PollableChannel cmChannel3; + @Autowired + private PollableChannel cmChannel4; - @Autowired - private PollableChannel cmChannel4; + @Autowired + private PollableChannel cmChannel5; - @Autowired - private PollableChannel cmChannel5; + @Autowired + private PollableChannel cmChannel6; - @Autowired - private PollableChannel cmChannel6; + @Autowired + private HazelcastInstance hazelcastInstance; - @Autowired - private HazelcastInstance hazelcastInstance; + @Autowired + private HazelcastInstance hazelcastInstance2; - @Autowired - private HazelcastInstance hazelcastInstance2; + @Autowired + private HazelcastInstance hazelcastInstance3; - @Autowired - private HazelcastInstance hazelcastInstance3; + @Test + public void testMembershipEvent() { + HazelcastInboundChannelAdapterTestUtils + .testMembershipEvent(hazelcastInstance, cmChannel1, "testKey1", "testValue1"); + } - @Test - public void testMembershipEvent() { - testMembershipEvent(hazelcastInstance, cmChannel1, "testKey1", "testValue1"); - } + @Test + public void testDistributedObjectEvent() { + HazelcastInboundChannelAdapterTestUtils + .testDistributedObjectEventByChannelAndHazelcastInstance(cmChannel2, + hazelcastInstance, "Test_Distributed_Map4"); + } - @Test - public void testDistributedObjectEvent() { - testDistributedObjectEventByChannelAndHazelcastInstance(cmChannel2, - hazelcastInstance); - } + @Test + public void testMigrationEvent() { + final IMap distributedMap = + hazelcastInstance3.getMap("Test_Distributed_Map2"); + distributedMap.put(1, "TestValue1"); + distributedMap.put(2, "TestValue2"); - @Test - public void testMigrationEvent() { - final IMap distributedMap = hazelcastInstance3 - .getMap("Test_Distributed_Map2"); - distributedMap.put(1, "TestValue1"); - distributedMap.put(2, "TestValue2"); + hazelcastInstance3.getLifecycleService().terminate(); + final Message msg = + cmChannel3.receive(HazelcastInboundChannelAdapterTestUtils.TIMEOUT); + verifyMigrationEvent(msg); + } - hazelcastInstance3.getLifecycleService().terminate(); - final Message msg = cmChannel3.receive(TIMEOUT); - verifyMigrationEvent(msg); - } + @Test + public void testLifecycleEvent() throws InterruptedException { + hazelcastInstance2.getLifecycleService().terminate(); - @Test - public void testLifecycleEvent() throws InterruptedException { - hazelcastInstance2.getLifecycleService().terminate(); + Message msg = + cmChannel4.receive(HazelcastInboundChannelAdapterTestUtils.TIMEOUT); + verifyLifecycleEvent(msg, LifecycleState.SHUTTING_DOWN); - Message msg = cmChannel4.receive(TIMEOUT); - verifyLifecycleEvent(msg, LifecycleState.SHUTTING_DOWN); + msg = cmChannel4.receive(HazelcastInboundChannelAdapterTestUtils.TIMEOUT); + verifyLifecycleEvent(msg, LifecycleState.SHUTDOWN); + } - msg = cmChannel4.receive(TIMEOUT); - verifyLifecycleEvent(msg, LifecycleState.SHUTDOWN); - } + @Test + public void testClientEvent() { + testClientEventByChannelAndGroupName(cmChannel5, TEST_GROUP_NAME1); + } - @Test - public void testClientEvent() { - testClientEventByChannelAndGroupName(cmChannel5, TEST_GROUP_NAME1); - } + @Test + public void testMultipleMonitorTypes() { + HazelcastInboundChannelAdapterTestUtils + .testDistributedObjectEventByChannelAndHazelcastInstance(cmChannel6, + hazelcastInstance, "Test_Distributed_Map5"); - @Test - public void testMultipleMonitorTypes() { - testDistributedObjectEventByChannelAndHazelcastInstance(cmChannel6, - hazelcastInstance); + HazelcastInboundChannelAdapterTestUtils + .testMembershipEvent(hazelcastInstance, cmChannel6, "testKey2", "testValue2"); + } - testMembershipEvent(hazelcastInstance, cmChannel6, "testKey2", "testValue2"); - } + private void testClientEventByChannelAndGroupName(final PollableChannel channel, + final String groupName) { + final HazelcastInstance client = getHazelcastClientByGroupName(groupName); - private void testMembershipEvent( - final HazelcastInstance instance, final PollableChannel channel, - final String key, final String value) { - Member member = instance.getCluster().getMembers().iterator().next(); - member.setStringAttribute(key, value); + Message msg = channel.receive(HazelcastInboundChannelAdapterTestUtils.TIMEOUT); + verifyClientEvent(msg); - Message msg = channel.receive(TIMEOUT); - verifyMembershipEvent(msg, MembershipEvent.MEMBER_ATTRIBUTE_CHANGED); - } + client.getLifecycleService().terminate(); - private void testClientEventByChannelAndGroupName(final PollableChannel channel, - final String groupName) { - final HazelcastInstance client = getHazelcastClientByGroupName(groupName); + msg = channel.receive(HazelcastInboundChannelAdapterTestUtils.TIMEOUT); + verifyClientEvent(msg); + } - Message msg = channel.receive(TIMEOUT); - verifyClientEvent(msg); + private HazelcastInstance getHazelcastClientByGroupName(final String groupName) { + final GroupConfig groupConfig = new GroupConfig(); + groupConfig.setName(groupName); + groupConfig.setPassword("dev-pass"); + final ClientConfig cfg = new ClientConfig(); + cfg.setGroupConfig(groupConfig); + cfg.getNetworkConfig().addAddress("127.0.0.1:5701"); - client.getLifecycleService().terminate(); + return HazelcastClient.newHazelcastClient(cfg); + } - msg = channel.receive(TIMEOUT); - verifyClientEvent(msg); - } + private void verifyMigrationEvent(final Message msg) { + assertNotNull(msg); + assertNotNull(msg.getPayload()); + assertTrue(msg.getPayload() instanceof MigrationEvent); + assertNotNull(((MigrationEvent) msg.getPayload()).getStatus()); + assertNotNull(((MigrationEvent) msg.getPayload()).getNewOwner()); + assertNotNull(((MigrationEvent) msg.getPayload()).getOldOwner()); + } - private void testDistributedObjectEventByChannelAndHazelcastInstance( - final PollableChannel channel, final HazelcastInstance hazelcastInstance) { - final String distributedObjectName = "Test_Distributed_Map"; - final IMap distributedMap = hazelcastInstance - .getMap(distributedObjectName); + private void verifyLifecycleEvent(final Message msg, + final LifecycleState lifecycleState) { + assertNotNull(msg); + assertNotNull(msg.getPayload()); + assertTrue(msg.getPayload() instanceof LifecycleEvent); + assertEquals(lifecycleState, ((LifecycleEvent) msg.getPayload()).getState()); + } - Message msg = channel.receive(TIMEOUT); - verifyDistributedObjectEvent(msg, DistributedObjectEvent.EventType.CREATED, - distributedObjectName); - - distributedMap.destroy(); - - msg = channel.receive(TIMEOUT); - verifyDistributedObjectEvent(msg, DistributedObjectEvent.EventType.DESTROYED, - distributedObjectName); - } - - private HazelcastInstance getHazelcastClientByGroupName(final String groupName) { - final GroupConfig groupConfig = new GroupConfig(); - groupConfig.setName(groupName); - groupConfig.setPassword("dev-pass"); - final ClientConfig cfg = new ClientConfig(); - cfg.setGroupConfig(groupConfig); - cfg.getNetworkConfig().addAddress("127.0.0.1:5701"); - - return HazelcastClient.newHazelcastClient(cfg); - } - - private void verifyMembershipEvent(final Message msg, final int membershipEvent) { - assertNotNull(msg); - assertNotNull(msg.getPayload()); - assertTrue(msg.getPayload() instanceof MembershipEvent); - assertEquals(membershipEvent, ((MembershipEvent) msg.getPayload()).getEventType()); - assertNotNull(((MembershipEvent) msg.getPayload()).getMember()); - } - - private void verifyDistributedObjectEvent(final Message msg, - final DistributedObjectEvent.EventType eventType, - final String distributedObjectName) { - assertNotNull(msg); - assertNotNull(msg.getPayload()); - assertTrue(msg.getPayload() instanceof DistributedObjectEvent); - assertEquals(eventType, ((DistributedObjectEvent) msg.getPayload()).getEventType()); - assertNotNull( - (((DistributedObjectEvent) msg.getPayload()).getDistributedObject()) - .getName(), - distributedObjectName); - } - - private void verifyMigrationEvent(final Message msg) { - assertNotNull(msg); - assertNotNull(msg.getPayload()); - assertTrue(msg.getPayload() instanceof MigrationEvent); - assertNotNull(((MigrationEvent) msg.getPayload()).getStatus()); - assertNotNull(((MigrationEvent) msg.getPayload()).getNewOwner()); - assertNotNull(((MigrationEvent) msg.getPayload()).getOldOwner()); - } - - private void verifyLifecycleEvent(final Message msg, - final LifecycleState lifecycleState) { - assertNotNull(msg); - assertNotNull(msg.getPayload()); - assertTrue(msg.getPayload() instanceof LifecycleEvent); - assertEquals(lifecycleState, ((LifecycleEvent) msg.getPayload()).getState()); - } - - private void verifyClientEvent(final Message msg) { - assertNotNull(msg); - assertNotNull(msg.getPayload()); - assertTrue(msg.getPayload() instanceof Client); - assertEquals(ClientType.JAVA, ((Client) msg.getPayload()).getClientType()); - assertNotNull(((Client) msg.getPayload()).getSocketAddress()); - } + private void verifyClientEvent(final Message msg) { + assertNotNull(msg); + assertNotNull(msg.getPayload()); + assertTrue(msg.getPayload() instanceof Client); + assertEquals(ClientType.JAVA, ((Client) msg.getPayload()).getClientType()); + assertNotNull(((Client) msg.getPayload()).getSocketAddress()); + } } diff --git a/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/HazelcastDistributedListEventDrivenInboundChannelAdapterTests.java b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/HazelcastDistributedListEventDrivenInboundChannelAdapterTests.java index bb70a48..55cc315 100644 --- a/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/HazelcastDistributedListEventDrivenInboundChannelAdapterTests.java +++ b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/HazelcastDistributedListEventDrivenInboundChannelAdapterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 the original author or authors. + * Copyright 2015-2016 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. @@ -25,9 +25,9 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.integration.hazelcast.AbstractHazelcastTestSupport; import org.springframework.integration.hazelcast.HazelcastHeaders; import org.springframework.integration.hazelcast.HazelcastIntegrationTestUser; +import org.springframework.integration.hazelcast.inbound.util.HazelcastInboundChannelAdapterTestUtils; import org.springframework.messaging.Message; import org.springframework.messaging.PollableChannel; import org.springframework.test.annotation.DirtiesContext; @@ -46,69 +46,58 @@ import com.hazelcast.core.IList; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration @DirtiesContext -public class HazelcastDistributedListEventDrivenInboundChannelAdapterTests extends AbstractHazelcastTestSupport { +public class HazelcastDistributedListEventDrivenInboundChannelAdapterTests { - @Autowired - private PollableChannel edListChannel1; + @Autowired + private PollableChannel edListChannel1; - @Autowired - private PollableChannel edListChannel2; + @Autowired + private PollableChannel edListChannel2; - @Autowired - private PollableChannel edListChannel3; + @Autowired + private PollableChannel edListChannel3; - @Resource - private IList edDistributedList1; + @Resource + private IList edDistributedList1; - @Resource - private IList edDistributedList2; + @Resource + private IList edDistributedList2; - @Resource - private IList edDistributedList3; + @Resource + private IList edDistributedList3; - @Test - public void testEventDrivenForOnlyADDEDEntryEvent() { - edDistributedList1.add(new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1")); - Message msg = edListChannel1.receive(2_000); - assertNotNull(msg); - assertNotNull(msg.getPayload()); - assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); - assertEquals(EntryEventType.ADDED.toString(), msg.getHeaders().get(HazelcastHeaders.EVENT_TYPE).toString()); - assertEquals(1, ((HazelcastIntegrationTestUser) msg.getPayload()).getId()); - assertEquals("TestName1", ((HazelcastIntegrationTestUser) msg.getPayload()).getName()); - assertEquals("TestSurname1", ((HazelcastIntegrationTestUser) msg.getPayload()).getSurname()); - } + @Test + public void testEventDrivenForOnlyADDEDEntryEvent() { + edDistributedList1.add(new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1")); + Message msg = edListChannel1.receive(HazelcastInboundChannelAdapterTestUtils.TIMEOUT); + assertNotNull(msg); + assertNotNull(msg.getPayload()); + assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); + assertEquals(EntryEventType.ADDED.toString(), msg.getHeaders().get(HazelcastHeaders.EVENT_TYPE).toString()); + assertEquals(1, ((HazelcastIntegrationTestUser) msg.getPayload()).getId()); + assertEquals("TestName1", ((HazelcastIntegrationTestUser) msg.getPayload()).getName()); + assertEquals("TestSurname1", ((HazelcastIntegrationTestUser) msg.getPayload()).getSurname()); + } - @Test - public void testEventDrivenForOnlyREMOVEDEntryEvent() { - HazelcastIntegrationTestUser user = new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2"); - edDistributedList2.add(user); - edDistributedList2.remove(user); - Message msg = edListChannel2.receive(2_000); - assertNotNull(msg); - assertNotNull(msg.getPayload()); - assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); - assertEquals(EntryEventType.REMOVED.toString(), msg.getHeaders().get(HazelcastHeaders.EVENT_TYPE).toString()); - assertEquals(2, ((HazelcastIntegrationTestUser) msg.getPayload()).getId()); - assertEquals("TestName2", ((HazelcastIntegrationTestUser) msg.getPayload()).getName()); - assertEquals("TestSurname2", ((HazelcastIntegrationTestUser) msg.getPayload()).getSurname()); - } + @Test + public void testEventDrivenForOnlyREMOVEDEntryEvent() { + HazelcastIntegrationTestUser user = new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2"); + edDistributedList2.add(user); + edDistributedList2.remove(user); + Message msg = edListChannel2.receive(HazelcastInboundChannelAdapterTestUtils.TIMEOUT); + assertNotNull(msg); + assertNotNull(msg.getPayload()); + assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); + assertEquals(EntryEventType.REMOVED.toString(), msg.getHeaders().get(HazelcastHeaders.EVENT_TYPE).toString()); + assertEquals(2, ((HazelcastIntegrationTestUser) msg.getPayload()).getId()); + assertEquals("TestName2", ((HazelcastIntegrationTestUser) msg.getPayload()).getName()); + assertEquals("TestSurname2", ((HazelcastIntegrationTestUser) msg.getPayload()).getSurname()); + } - @Test - public void testEventDrivenForALLEntryEvent() { - HazelcastIntegrationTestUser user = new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1"); - edDistributedList3.add(user); - Message msg = edListChannel3.receive(2_000); - verifyItemEvent(msg, EntryEventType.ADDED); - - edDistributedList3.remove(user); - msg = edListChannel3.receive(2_000); - verifyItemEvent(msg, EntryEventType.REMOVED); - - user = new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2"); - edDistributedList3.add(user); - msg = edListChannel3.receive(2_000); - verifyItemEvent(msg, EntryEventType.ADDED); - } + @Test + public void testEventDrivenForALLEntryEvent() { + HazelcastInboundChannelAdapterTestUtils + .testEventDrivenForDistributedCollectionItemEvents(edDistributedList3, edListChannel3); + } } diff --git a/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/HazelcastDistributedMapEventDrivenInboundChannelAdapterTests.java b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/HazelcastDistributedMapEventDrivenInboundChannelAdapterTests.java index e7077b4..4dd5b60 100644 --- a/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/HazelcastDistributedMapEventDrivenInboundChannelAdapterTests.java +++ b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/HazelcastDistributedMapEventDrivenInboundChannelAdapterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 the original author or authors. + * Copyright 2015-2016 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. @@ -23,9 +23,9 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.integration.hazelcast.AbstractHazelcastTestSupport; import org.springframework.integration.hazelcast.HazelcastHeaders; import org.springframework.integration.hazelcast.HazelcastIntegrationTestUser; +import org.springframework.integration.hazelcast.inbound.util.HazelcastInboundChannelAdapterTestUtils; import org.springframework.integration.hazelcast.message.EntryEventMessagePayload; import org.springframework.messaging.Message; import org.springframework.messaging.PollableChannel; @@ -46,139 +46,116 @@ import com.hazelcast.core.IMap; @ContextConfiguration @DirtiesContext @SuppressWarnings("unchecked") -public class HazelcastDistributedMapEventDrivenInboundChannelAdapterTests extends AbstractHazelcastTestSupport { +public class HazelcastDistributedMapEventDrivenInboundChannelAdapterTests { - @Autowired - private PollableChannel edMapChannel1; + @Autowired + private PollableChannel edMapChannel1; - @Autowired - private PollableChannel edMapChannel2; + @Autowired + private PollableChannel edMapChannel2; - @Autowired - private PollableChannel edMapChannel3; + @Autowired + private PollableChannel edMapChannel3; - @Autowired - private PollableChannel edMapChannel4; + @Autowired + private PollableChannel edMapChannel4; - @Resource - private IMap edDistributedMap1; + @Resource + private IMap edDistributedMap1; - @Resource - private IMap edDistributedMap2; + @Resource + private IMap edDistributedMap2; - @Resource - private IMap edDistributedMap3; + @Resource + private IMap edDistributedMap3; - @Resource - private IMap edDistributedMap4; + @Resource + private IMap edDistributedMap4; - @Test - public void testEventDrivenForOnlyADDEDEntryEvent() { - edDistributedMap1.put(1, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1")); - Message msg = edMapChannel1.receive(2_000); - Assert.assertNotNull(msg); - Assert.assertNotNull(msg.getPayload()); - Assert.assertTrue(msg.getPayload() instanceof EntryEventMessagePayload); - Assert.assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); - Assert.assertEquals(EntryEventType.ADDED.name(), msg.getHeaders().get(HazelcastHeaders.EVENT_TYPE)); - Assert.assertEquals("edDistributedMap1", msg.getHeaders().get(HazelcastHeaders.CACHE_NAME)); + @Test + public void testEventDrivenForOnlyADDEDEntryEvent() { + HazelcastInboundChannelAdapterTestUtils + .testEventDrivenForADDEDDistributedMapEntryEvent(edDistributedMap1, + edMapChannel1, "edDistributedMap1"); + } - Assert.assertEquals(Integer.valueOf(1), - ((EntryEventMessagePayload) msg.getPayload()).key); - Assert.assertEquals(1, - (((EntryEventMessagePayload) msg - .getPayload()).value).getId()); - Assert.assertEquals("TestName1", - (((EntryEventMessagePayload) msg - .getPayload()).value).getName()); - Assert.assertEquals("TestSurname1", - (((EntryEventMessagePayload) msg - .getPayload()).value).getSurname()); - } + @Test + public void testEventDrivenForOnlyUPDATEDEntryEvent() { + edDistributedMap2 + .put(2, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1")); + edDistributedMap2 + .put(2, new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2")); + Message msg = + edMapChannel2.receive(HazelcastInboundChannelAdapterTestUtils.TIMEOUT); + Assert.assertNotNull(msg); + Assert.assertNotNull(msg.getPayload()); + Assert.assertTrue(msg.getPayload() instanceof EntryEventMessagePayload); + Assert.assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); + Assert.assertEquals(EntryEventType.UPDATED.name(), + msg.getHeaders().get(HazelcastHeaders.EVENT_TYPE)); + Assert.assertEquals("edDistributedMap2", + msg.getHeaders().get(HazelcastHeaders.CACHE_NAME)); - @Test - public void testEventDrivenForOnlyUPDATEDEntryEvent() { - edDistributedMap2.put(2, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1")); - edDistributedMap2.put(2, new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2")); - Message msg = edMapChannel2.receive(2_000); - Assert.assertNotNull(msg); - Assert.assertNotNull(msg.getPayload()); - Assert.assertTrue(msg.getPayload() instanceof EntryEventMessagePayload); - Assert.assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); - Assert.assertEquals(EntryEventType.UPDATED.name(), msg.getHeaders().get(HazelcastHeaders.EVENT_TYPE)); - Assert.assertEquals("edDistributedMap2", msg.getHeaders().get(HazelcastHeaders.CACHE_NAME)); + Assert.assertEquals(Integer.valueOf(2), + ((EntryEventMessagePayload) msg + .getPayload()).key); + Assert.assertEquals(1, + (((EntryEventMessagePayload) msg + .getPayload()).oldValue).getId()); + Assert.assertEquals("TestName1", + (((EntryEventMessagePayload) msg + .getPayload()).oldValue).getName()); + Assert.assertEquals("TestSurname1", + (((EntryEventMessagePayload) msg + .getPayload()).oldValue).getSurname()); + Assert.assertEquals(2, + (((EntryEventMessagePayload) msg + .getPayload()).value).getId()); + Assert.assertEquals("TestName2", + (((EntryEventMessagePayload) msg + .getPayload()).value).getName()); + Assert.assertEquals("TestSurname2", + (((EntryEventMessagePayload) msg + .getPayload()).value).getSurname()); + } - Assert.assertEquals(Integer.valueOf(2), - ((EntryEventMessagePayload) msg - .getPayload()).key); - Assert.assertEquals(1, - (((EntryEventMessagePayload) msg - .getPayload()).oldValue).getId()); - Assert.assertEquals("TestName1", - (((EntryEventMessagePayload) msg - .getPayload()).oldValue).getName()); - Assert.assertEquals("TestSurname1", - (((EntryEventMessagePayload) msg - .getPayload()).oldValue).getSurname()); - Assert.assertEquals(2, - (((EntryEventMessagePayload) msg - .getPayload()).value).getId()); - Assert.assertEquals("TestName2", - (((EntryEventMessagePayload) msg - .getPayload()).value).getName()); - Assert.assertEquals("TestSurname2", - (((EntryEventMessagePayload) msg - .getPayload()).value).getSurname()); - } + @Test + public void testEventDrivenForOnlyREMOVEDEntryEvent() { + edDistributedMap3 + .put(1, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1")); + edDistributedMap3 + .put(2, new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2")); + edDistributedMap3.remove(2); + Message msg = + edMapChannel3.receive(HazelcastInboundChannelAdapterTestUtils.TIMEOUT); + Assert.assertNotNull(msg); + Assert.assertNotNull(msg.getPayload()); + Assert.assertTrue(msg.getPayload() instanceof EntryEventMessagePayload); + Assert.assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); + Assert.assertEquals(EntryEventType.REMOVED.name(), + msg.getHeaders().get(HazelcastHeaders.EVENT_TYPE)); + Assert.assertEquals("edDistributedMap3", + msg.getHeaders().get(HazelcastHeaders.CACHE_NAME)); - @Test - public void testEventDrivenForOnlyREMOVEDEntryEvent() { - edDistributedMap3.put(1, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1")); - edDistributedMap3.put(2, new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2")); - edDistributedMap3.remove(2); - Message msg = edMapChannel3.receive(2_000); - Assert.assertNotNull(msg); - Assert.assertNotNull(msg.getPayload()); - Assert.assertTrue(msg.getPayload() instanceof EntryEventMessagePayload); - Assert.assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); - Assert.assertEquals(EntryEventType.REMOVED.name(), msg.getHeaders().get(HazelcastHeaders.EVENT_TYPE)); - Assert.assertEquals("edDistributedMap3", msg.getHeaders().get(HazelcastHeaders.CACHE_NAME)); + Assert.assertEquals(Integer.valueOf(2), + ((EntryEventMessagePayload) msg + .getPayload()).key); + Assert.assertEquals(2, + (((EntryEventMessagePayload) msg + .getPayload()).oldValue).getId()); + Assert.assertEquals("TestName2", + (((EntryEventMessagePayload) msg + .getPayload()).oldValue).getName()); + Assert.assertEquals("TestSurname2", + (((EntryEventMessagePayload) msg + .getPayload()).oldValue).getSurname()); + } - Assert.assertEquals(Integer.valueOf(2), - ((EntryEventMessagePayload) msg - .getPayload()).key); - Assert.assertEquals(2, - (((EntryEventMessagePayload) msg - .getPayload()).oldValue).getId()); - Assert.assertEquals("TestName2", - (((EntryEventMessagePayload) msg - .getPayload()).oldValue).getName()); - Assert.assertEquals("TestSurname2", - (((EntryEventMessagePayload) msg - .getPayload()).oldValue).getSurname()); - } - - @Test - public void testEventDrivenForALLEntryEvent() { - edDistributedMap4.put(1, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1")); - Message msg = edMapChannel4.receive(2_000); - verifyEntryEvent(msg, "edDistributedMap4", EntryEventType.ADDED); - - edDistributedMap4.put(1, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurnameUpdated")); - msg = edMapChannel4.receive(2_000); - verifyEntryEvent(msg, "edDistributedMap4", EntryEventType.UPDATED); - - edDistributedMap4.remove(1); - msg = edMapChannel4.receive(2_000); - verifyEntryEvent(msg, "edDistributedMap4", EntryEventType.REMOVED); - - edDistributedMap4.put(2, new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2")); - msg = edMapChannel4.receive(2_000); - verifyEntryEvent(msg, "edDistributedMap4", EntryEventType.ADDED); - - edDistributedMap4.clear(); - msg = edMapChannel4.receive(2_000); - verifyEntryEvent(msg, "edDistributedMap4", EntryEventType.CLEAR_ALL); - } + @Test + public void testEventDrivenForALLEntryEvent() { + HazelcastInboundChannelAdapterTestUtils + .testEventDrivenForDistributedMapEntryEvents(edDistributedMap4, edMapChannel4, + "edDistributedMap4"); + } } diff --git a/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/HazelcastDistributedQueueEventDrivenInboundChannelAdapterTests.java b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/HazelcastDistributedQueueEventDrivenInboundChannelAdapterTests.java index 351f550..a8cc9d4 100644 --- a/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/HazelcastDistributedQueueEventDrivenInboundChannelAdapterTests.java +++ b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/HazelcastDistributedQueueEventDrivenInboundChannelAdapterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 the original author or authors. + * Copyright 2015-2016 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. @@ -23,9 +23,9 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.integration.hazelcast.AbstractHazelcastTestSupport; import org.springframework.integration.hazelcast.HazelcastHeaders; import org.springframework.integration.hazelcast.HazelcastIntegrationTestUser; +import org.springframework.integration.hazelcast.inbound.util.HazelcastInboundChannelAdapterTestUtils; import org.springframework.messaging.Message; import org.springframework.messaging.PollableChannel; import org.springframework.test.annotation.DirtiesContext; @@ -44,69 +44,71 @@ import com.hazelcast.core.IQueue; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration @DirtiesContext -public class HazelcastDistributedQueueEventDrivenInboundChannelAdapterTests extends AbstractHazelcastTestSupport { +public class HazelcastDistributedQueueEventDrivenInboundChannelAdapterTests { - @Autowired - private PollableChannel edQueueChannel1; + @Autowired + private PollableChannel edQueueChannel1; - @Autowired - private PollableChannel edQueueChannel2; + @Autowired + private PollableChannel edQueueChannel2; - @Autowired - private PollableChannel edQueueChannel3; + @Autowired + private PollableChannel edQueueChannel3; - @Resource - private IQueue edDistributedQueue1; + @Resource + private IQueue edDistributedQueue1; - @Resource - private IQueue edDistributedQueue2; + @Resource + private IQueue edDistributedQueue2; - @Resource - private IQueue edDistributedQueue3; + @Resource + private IQueue edDistributedQueue3; - @Test - public void testEventDrivenForOnlyADDEDEntryEvent() { - edDistributedQueue1.add(new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1")); - Message msg = edQueueChannel1.receive(2_000); - Assert.assertNotNull(msg); - Assert.assertNotNull(msg.getPayload()); - Assert.assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); - Assert.assertEquals(EntryEventType.ADDED.toString(), msg.getHeaders().get(HazelcastHeaders.EVENT_TYPE).toString()); - Assert.assertEquals(1, (((HazelcastIntegrationTestUser) msg.getPayload()).getId())); - Assert.assertEquals("TestName1", (((HazelcastIntegrationTestUser) msg.getPayload()).getName())); - Assert.assertEquals("TestSurname1", (((HazelcastIntegrationTestUser) msg.getPayload()).getSurname())); - } + @Test + public void testEventDrivenForOnlyADDEDEntryEvent() { + edDistributedQueue1 + .add(new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1")); + Message msg = + edQueueChannel1.receive(HazelcastInboundChannelAdapterTestUtils.TIMEOUT); + Assert.assertNotNull(msg); + Assert.assertNotNull(msg.getPayload()); + Assert.assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); + Assert.assertEquals(EntryEventType.ADDED.toString(), + msg.getHeaders().get(HazelcastHeaders.EVENT_TYPE).toString()); + Assert + .assertEquals(1, (((HazelcastIntegrationTestUser) msg.getPayload()).getId())); + Assert.assertEquals("TestName1", + (((HazelcastIntegrationTestUser) msg.getPayload()).getName())); + Assert.assertEquals("TestSurname1", + (((HazelcastIntegrationTestUser) msg.getPayload()).getSurname())); + } - @Test - public void testEventDrivenForOnlyREMOVEDEntryEvent() { - HazelcastIntegrationTestUser user = new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2"); - edDistributedQueue2.add(user); - edDistributedQueue2.remove(user); - Message msg = edQueueChannel2.receive(2_000); - Assert.assertNotNull(msg); - Assert.assertNotNull(msg.getPayload()); - Assert.assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); - Assert.assertEquals(EntryEventType.REMOVED.toString(), msg.getHeaders().get(HazelcastHeaders.EVENT_TYPE).toString()); - Assert.assertEquals(2, (((HazelcastIntegrationTestUser) msg.getPayload()).getId())); - Assert.assertEquals("TestName2", (((HazelcastIntegrationTestUser) msg.getPayload()).getName())); - Assert.assertEquals("TestSurname2", (((HazelcastIntegrationTestUser) msg.getPayload()).getSurname())); - } + @Test + public void testEventDrivenForOnlyREMOVEDEntryEvent() { + HazelcastIntegrationTestUser user = + new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2"); + edDistributedQueue2.add(user); + edDistributedQueue2.remove(user); + Message msg = + edQueueChannel2.receive(HazelcastInboundChannelAdapterTestUtils.TIMEOUT); + Assert.assertNotNull(msg); + Assert.assertNotNull(msg.getPayload()); + Assert.assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); + Assert.assertEquals(EntryEventType.REMOVED.toString(), + msg.getHeaders().get(HazelcastHeaders.EVENT_TYPE).toString()); + Assert + .assertEquals(2, (((HazelcastIntegrationTestUser) msg.getPayload()).getId())); + Assert.assertEquals("TestName2", + (((HazelcastIntegrationTestUser) msg.getPayload()).getName())); + Assert.assertEquals("TestSurname2", + (((HazelcastIntegrationTestUser) msg.getPayload()).getSurname())); + } - @Test - public void testEventDrivenForALLEntryEvent() { - HazelcastIntegrationTestUser user = new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1"); - edDistributedQueue3.add(user); - Message msg = edQueueChannel3.receive(2_000); - verifyItemEvent(msg, EntryEventType.ADDED); - - edDistributedQueue3.remove(user); - msg = edQueueChannel3.receive(2_000); - verifyItemEvent(msg, EntryEventType.REMOVED); - - user = new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2"); - edDistributedQueue3.add(user); - msg = edQueueChannel3.receive(2_000); - verifyItemEvent(msg, EntryEventType.ADDED); - } + @Test + public void testEventDrivenForALLEntryEvent() { + HazelcastInboundChannelAdapterTestUtils + .testEventDrivenForDistributedCollectionItemEvents(edDistributedQueue3, + edQueueChannel3); + } } diff --git a/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/HazelcastDistributedSQLInboundChannelAdapterTests.java b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/HazelcastDistributedSQLInboundChannelAdapterTests.java index 3cc5e05..fa4f405 100644 --- a/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/HazelcastDistributedSQLInboundChannelAdapterTests.java +++ b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/HazelcastDistributedSQLInboundChannelAdapterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 the original author or authors. + * Copyright 2015-2016 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. @@ -16,18 +16,14 @@ package org.springframework.integration.hazelcast.inbound; -import java.util.Collection; -import java.util.Map; - import javax.annotation.Resource; -import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.integration.hazelcast.HazelcastIntegrationTestUser; -import org.springframework.messaging.Message; +import org.springframework.integration.hazelcast.inbound.util.HazelcastInboundChannelAdapterTestUtils; import org.springframework.messaging.PollableChannel; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ContextConfiguration; @@ -46,101 +42,52 @@ import com.hazelcast.core.IMap; @DirtiesContext public class HazelcastDistributedSQLInboundChannelAdapterTests { - @Autowired - private PollableChannel dsMapChannel1; + @Autowired + private PollableChannel dsMapChannel1; - @Autowired - private PollableChannel dsMapChannel2; + @Autowired + private PollableChannel dsMapChannel2; - @Autowired - private PollableChannel dsMapChannel3; + @Autowired + private PollableChannel dsMapChannel3; - @Autowired - private PollableChannel dsMapChannel4; + @Autowired + private PollableChannel dsMapChannel4; - @Resource - private IMap dsDistributedMap1; + @Resource + private IMap dsDistributedMap1; - @Resource - private IMap dsDistributedMap2; + @Resource + private IMap dsDistributedMap2; - @Resource - private IMap dsDistributedMap3; + @Resource + private IMap dsDistributedMap3; - @Resource - private IMap dsDistributedMap4; + @Resource + private IMap dsDistributedMap4; - @Test - public void testDistributedSQLForOnlyENTRYIterationType() throws InterruptedException { - dsDistributedMap1.put(1, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1", 10)); - dsDistributedMap1.put(2, new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2", 20)); - dsDistributedMap1.put(3, new HazelcastIntegrationTestUser(3, "TestName3", "TestSurname3", 30)); - dsDistributedMap1.put(4, new HazelcastIntegrationTestUser(4, "TestName4", "TestSurname4", 40)); - dsDistributedMap1.put(5, new HazelcastIntegrationTestUser(5, "TestName5", "TestSurname5", 50)); + @Test + public void testDistributedSQLForOnlyENTRYIterationType() { + HazelcastInboundChannelAdapterTestUtils + .testDistributedSQLForENTRYIterationType(dsDistributedMap1, dsMapChannel1); + } - Message msg = dsMapChannel1.receive(2_000); + @Test + public void testDistributedSQLForOnlyKEYIterationType() { + HazelcastInboundChannelAdapterTestUtils + .testDistributedSQLForKEYIterationType(dsDistributedMap2, dsMapChannel2); + } - Assert.assertNotNull(msg); - Assert.assertNotNull(msg.getPayload()); - Assert.assertTrue(msg.getPayload() instanceof Collection); - Assert.assertEquals(4, (((Map.Entry) ((Collection) msg.getPayload()).iterator() - .next()).getKey())); - Assert.assertEquals(4, ((HazelcastIntegrationTestUser) ((Map.Entry) ((Collection) msg.getPayload()) - .iterator().next()).getValue()).getId()); - Assert.assertEquals("TestName4", ((HazelcastIntegrationTestUser) ((Map.Entry) ((Collection) msg - .getPayload()).iterator().next()).getValue()).getName()); - Assert.assertEquals("TestSurname4", ((HazelcastIntegrationTestUser) ((Map.Entry) ((Collection) msg - .getPayload()).iterator().next()).getValue()).getSurname()); - } + @Test + public void testDistributedSQLForOnlyLOCAL_KEYIterationType() { + HazelcastInboundChannelAdapterTestUtils + .testDistributedSQLForLOCAL_KEYIterationType(dsDistributedMap3, dsMapChannel3); + } - @Test - public void testDistributedSQLForOnlyKEYIterationType() throws InterruptedException { - dsDistributedMap2.put(1, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1", 10)); - dsDistributedMap2.put(2, new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2", 20)); - dsDistributedMap2.put(3, new HazelcastIntegrationTestUser(3, "TestName3", "TestSurname3", 30)); - - Message msg = dsMapChannel2.receive(2_000); - - Assert.assertNotNull(msg); - Assert.assertNotNull(msg.getPayload()); - Assert.assertTrue(msg.getPayload() instanceof Collection); - Assert.assertEquals(1, ((Collection) msg.getPayload()).iterator().next()); - } - - @Test - public void testDistributedSQLForOnlyLOCAL_KEYIterationType() - throws InterruptedException { - dsDistributedMap3.put(1, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1", 10)); - dsDistributedMap3.put(2, new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2", 20)); - dsDistributedMap3.put(3, new HazelcastIntegrationTestUser(3, "TestName3", "TestSurname3", 30)); - - Message msg = dsMapChannel3.receive(2_000); - - Assert.assertNotNull(msg); - Assert.assertNotNull(msg.getPayload()); - Assert.assertTrue(msg.getPayload() instanceof Collection); - Assert.assertEquals(2, ((Collection) msg.getPayload()).iterator().next()); - } - - @Test - public void testDistributedSQLForOnlyVALUEIterationType() throws InterruptedException { - dsDistributedMap4.put(1, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1", 10)); - dsDistributedMap4.put(2, new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2", 20)); - dsDistributedMap4.put(3, new HazelcastIntegrationTestUser(3, "TestName3", "TestSurname3", 30)); - dsDistributedMap4.put(4, new HazelcastIntegrationTestUser(4, "TestName4", "TestSurname4", 40)); - dsDistributedMap4.put(5, new HazelcastIntegrationTestUser(5, "TestName5", "TestSurname5", 50)); - - Message msg = dsMapChannel4.receive(2_000); - - Assert.assertNotNull(msg); - Assert.assertNotNull(msg.getPayload()); - Assert.assertTrue(msg.getPayload() instanceof Collection); - Assert.assertEquals(3, - ((HazelcastIntegrationTestUser) (((Collection) msg.getPayload()).iterator().next())).getId()); - Assert.assertEquals("TestName3", - ((HazelcastIntegrationTestUser) (((Collection) msg.getPayload()).iterator().next())).getName()); - Assert.assertEquals("TestSurname3", - ((HazelcastIntegrationTestUser) (((Collection) msg.getPayload()).iterator().next())).getSurname()); - } + @Test + public void testDistributedSQLForOnlyVALUEIterationType() { + HazelcastInboundChannelAdapterTestUtils + .testDistributedSQLForVALUEIterationType(dsDistributedMap4, dsMapChannel4); + } } diff --git a/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/HazelcastDistributedSetEventDrivenInboundChannelAdapterTests.java b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/HazelcastDistributedSetEventDrivenInboundChannelAdapterTests.java index a0b18c2..ad9860d 100644 --- a/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/HazelcastDistributedSetEventDrivenInboundChannelAdapterTests.java +++ b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/HazelcastDistributedSetEventDrivenInboundChannelAdapterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 the original author or authors. + * Copyright 2015-2016 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. @@ -23,9 +23,9 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.integration.hazelcast.AbstractHazelcastTestSupport; import org.springframework.integration.hazelcast.HazelcastHeaders; import org.springframework.integration.hazelcast.HazelcastIntegrationTestUser; +import org.springframework.integration.hazelcast.inbound.util.HazelcastInboundChannelAdapterTestUtils; import org.springframework.messaging.Message; import org.springframework.messaging.PollableChannel; import org.springframework.test.annotation.DirtiesContext; @@ -44,69 +44,71 @@ import com.hazelcast.core.ISet; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration @DirtiesContext -public class HazelcastDistributedSetEventDrivenInboundChannelAdapterTests extends AbstractHazelcastTestSupport { +public class HazelcastDistributedSetEventDrivenInboundChannelAdapterTests { - @Autowired - private PollableChannel edSetChannel1; + @Autowired + private PollableChannel edSetChannel1; - @Autowired - private PollableChannel edSetChannel2; + @Autowired + private PollableChannel edSetChannel2; - @Autowired - private PollableChannel edSetChannel3; + @Autowired + private PollableChannel edSetChannel3; - @Resource - private ISet edDistributedSet1; + @Resource + private ISet edDistributedSet1; - @Resource - private ISet edDistributedSet2; + @Resource + private ISet edDistributedSet2; - @Resource - private ISet edDistributedSet3; + @Resource + private ISet edDistributedSet3; - @Test - public void testEventDrivenForOnlyADDEDEntryEvent() { - edDistributedSet1.add(new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1")); - Message msg = edSetChannel1.receive(2_000); - Assert.assertNotNull(msg); - Assert.assertNotNull(msg.getPayload()); - Assert.assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); - Assert.assertEquals(EntryEventType.ADDED.toString(), msg.getHeaders().get(HazelcastHeaders.EVENT_TYPE).toString()); - Assert.assertEquals(1, (((HazelcastIntegrationTestUser) msg.getPayload()).getId())); - Assert.assertEquals("TestName1", (((HazelcastIntegrationTestUser) msg.getPayload()).getName())); - Assert.assertEquals("TestSurname1", (((HazelcastIntegrationTestUser) msg.getPayload()).getSurname())); - } + @Test + public void testEventDrivenForOnlyADDEDEntryEvent() { + edDistributedSet1 + .add(new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1")); + Message msg = + edSetChannel1.receive(HazelcastInboundChannelAdapterTestUtils.TIMEOUT); + Assert.assertNotNull(msg); + Assert.assertNotNull(msg.getPayload()); + Assert.assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); + Assert.assertEquals(EntryEventType.ADDED.toString(), + msg.getHeaders().get(HazelcastHeaders.EVENT_TYPE).toString()); + Assert + .assertEquals(1, (((HazelcastIntegrationTestUser) msg.getPayload()).getId())); + Assert.assertEquals("TestName1", + (((HazelcastIntegrationTestUser) msg.getPayload()).getName())); + Assert.assertEquals("TestSurname1", + (((HazelcastIntegrationTestUser) msg.getPayload()).getSurname())); + } - @Test - public void testEventDrivenForOnlyREMOVEDEntryEvent() { - HazelcastIntegrationTestUser user = new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2"); - edDistributedSet2.add(user); - edDistributedSet2.remove(user); - Message msg = edSetChannel2.receive(2_000); - Assert.assertNotNull(msg); - Assert.assertNotNull(msg.getPayload()); - Assert.assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); - Assert.assertEquals(EntryEventType.REMOVED.toString(), msg.getHeaders().get(HazelcastHeaders.EVENT_TYPE).toString()); - Assert.assertEquals(2, (((HazelcastIntegrationTestUser) msg.getPayload()).getId())); - Assert.assertEquals("TestName2", (((HazelcastIntegrationTestUser) msg.getPayload()).getName())); - Assert.assertEquals("TestSurname2", (((HazelcastIntegrationTestUser) msg.getPayload()).getSurname())); - } + @Test + public void testEventDrivenForOnlyREMOVEDEntryEvent() { + HazelcastIntegrationTestUser user = + new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2"); + edDistributedSet2.add(user); + edDistributedSet2.remove(user); + Message msg = + edSetChannel2.receive(HazelcastInboundChannelAdapterTestUtils.TIMEOUT); + Assert.assertNotNull(msg); + Assert.assertNotNull(msg.getPayload()); + Assert.assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); + Assert.assertEquals(EntryEventType.REMOVED.toString(), + msg.getHeaders().get(HazelcastHeaders.EVENT_TYPE).toString()); + Assert + .assertEquals(2, (((HazelcastIntegrationTestUser) msg.getPayload()).getId())); + Assert.assertEquals("TestName2", + (((HazelcastIntegrationTestUser) msg.getPayload()).getName())); + Assert.assertEquals("TestSurname2", + (((HazelcastIntegrationTestUser) msg.getPayload()).getSurname())); + } - @Test - public void testEventDrivenForALLEntryEvent() { - HazelcastIntegrationTestUser user = new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1"); - edDistributedSet3.add(user); - Message msg = edSetChannel3.receive(2_000); - verifyItemEvent(msg, EntryEventType.ADDED); - - edDistributedSet3.remove(user); - msg = edSetChannel3.receive(2_000); - verifyItemEvent(msg, EntryEventType.REMOVED); - - user = new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2"); - edDistributedSet3.add(user); - msg = edSetChannel3.receive(2_000); - verifyItemEvent(msg, EntryEventType.ADDED); - } + @Test + public void testEventDrivenForALLEntryEvent() { + HazelcastInboundChannelAdapterTestUtils + .testEventDrivenForDistributedCollectionItemEvents(edDistributedSet3, + edSetChannel3); + } } diff --git a/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/HazelcastDistributedTopicEventDrivenInboundChannelAdapterTests.java b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/HazelcastDistributedTopicEventDrivenInboundChannelAdapterTests.java index 3d58346..67a24c0 100644 --- a/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/HazelcastDistributedTopicEventDrivenInboundChannelAdapterTests.java +++ b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/HazelcastDistributedTopicEventDrivenInboundChannelAdapterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 the original author or authors. + * Copyright 2015-2016 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. @@ -18,14 +18,12 @@ package org.springframework.integration.hazelcast.inbound; import javax.annotation.Resource; -import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.integration.hazelcast.HazelcastHeaders; import org.springframework.integration.hazelcast.HazelcastIntegrationTestUser; -import org.springframework.messaging.Message; +import org.springframework.integration.hazelcast.inbound.util.HazelcastInboundChannelAdapterTestUtils; import org.springframework.messaging.PollableChannel; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ContextConfiguration; @@ -44,24 +42,16 @@ import com.hazelcast.core.ITopic; @DirtiesContext public class HazelcastDistributedTopicEventDrivenInboundChannelAdapterTests { - @Autowired - private PollableChannel edTopicChannel1; + @Autowired + private PollableChannel edTopicChannel1; - @Resource - private ITopic edDistributedTopic1; + @Resource + private ITopic edDistributedTopic1; - @Test - public void testEventDrivenForOnlyADDEDEntryEvent() { - edDistributedTopic1.publish(new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1")); - Message msg = edTopicChannel1.receive(2_000); - Assert.assertNotNull(msg); - Assert.assertNotNull(msg.getPayload()); - Assert.assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); - Assert.assertNotNull(msg.getHeaders().get(HazelcastHeaders.PUBLISHING_TIME)); - Assert.assertEquals("edDistributedTopic1", msg.getHeaders().get(HazelcastHeaders.CACHE_NAME)); - Assert.assertEquals(1, ((HazelcastIntegrationTestUser) msg.getPayload()).getId()); - Assert.assertEquals("TestName1", ((HazelcastIntegrationTestUser) msg.getPayload()).getName()); - Assert.assertEquals("TestSurname1", ((HazelcastIntegrationTestUser) msg.getPayload()).getSurname()); - } + @Test + public void testEventDrivenForOnlyADDEDEntryEvent() { + HazelcastInboundChannelAdapterTestUtils + .testEventDrivenForTopicMessageEvent(edDistributedTopic1, edTopicChannel1); + } } diff --git a/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/HazelcastMultiMapEventDrivenInboundChannelAdapterTests.java b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/HazelcastMultiMapEventDrivenInboundChannelAdapterTests.java index 606b459..f4da8f1 100644 --- a/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/HazelcastMultiMapEventDrivenInboundChannelAdapterTests.java +++ b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/HazelcastMultiMapEventDrivenInboundChannelAdapterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 the original author or authors. + * Copyright 2015-2016 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. @@ -23,9 +23,9 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.integration.hazelcast.AbstractHazelcastTestSupport; import org.springframework.integration.hazelcast.HazelcastHeaders; import org.springframework.integration.hazelcast.HazelcastIntegrationTestUser; +import org.springframework.integration.hazelcast.inbound.util.HazelcastInboundChannelAdapterTestUtils; import org.springframework.integration.hazelcast.message.EntryEventMessagePayload; import org.springframework.messaging.Message; import org.springframework.messaging.PollableChannel; @@ -47,102 +47,93 @@ import com.hazelcast.core.MultiMap; @ContextConfiguration @DirtiesContext @SuppressWarnings("unchecked") -public class HazelcastMultiMapEventDrivenInboundChannelAdapterTests extends AbstractHazelcastTestSupport { +public class HazelcastMultiMapEventDrivenInboundChannelAdapterTests { - @Autowired - private PollableChannel edMultiMapChannel1; + @Autowired + private PollableChannel edMultiMapChannel1; - @Autowired - private PollableChannel edMultiMapChannel2; + @Autowired + private PollableChannel edMultiMapChannel2; - @Autowired - private PollableChannel edMultiMapChannel3; + @Autowired + private PollableChannel edMultiMapChannel3; - @Resource - private MultiMap edMultiMap1; + @Resource + private MultiMap edMultiMap1; - @Resource - private MultiMap edMultiMap2; + @Resource + private MultiMap edMultiMap2; - @Resource - private MultiMap edMultiMap3; + @Resource + private MultiMap edMultiMap3; - @Test - public void testEventDrivenForOnlyADDEDEntryEvent() { - edMultiMap1.put(1, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1")); - Message msg = edMultiMapChannel1.receive(2_000); - Assert.assertNotNull(msg); - Assert.assertNotNull(msg.getPayload()); - Assert.assertTrue(msg.getPayload() instanceof EntryEventMessagePayload); - Assert.assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); - Assert.assertEquals(EntryEventType.ADDED.name(), msg.getHeaders().get(HazelcastHeaders.EVENT_TYPE)); - Assert.assertEquals("edMultiMap1", msg.getHeaders().get(HazelcastHeaders.CACHE_NAME)); + @Test + public void testEventDrivenForOnlyADDEDEntryEvent() { + edMultiMap1 + .put(1, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1")); + Message msg = + edMultiMapChannel1.receive(HazelcastInboundChannelAdapterTestUtils.TIMEOUT); + Assert.assertNotNull(msg); + Assert.assertNotNull(msg.getPayload()); + Assert.assertTrue(msg.getPayload() instanceof EntryEventMessagePayload); + Assert.assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); + Assert.assertEquals(EntryEventType.ADDED.name(), + msg.getHeaders().get(HazelcastHeaders.EVENT_TYPE)); + Assert.assertEquals("edMultiMap1", + msg.getHeaders().get(HazelcastHeaders.CACHE_NAME)); - Assert.assertEquals(Integer.valueOf(1), - ((EntryEventMessagePayload) msg - .getPayload()).key); - Assert.assertEquals(1, - (((EntryEventMessagePayload) msg - .getPayload()).value).getId()); - Assert.assertEquals("TestName1", - (((EntryEventMessagePayload) msg - .getPayload()).value).getName()); - Assert.assertEquals("TestSurname1", - (((EntryEventMessagePayload) msg - .getPayload()).value).getSurname()); - } + Assert.assertEquals(Integer.valueOf(1), + ((EntryEventMessagePayload) msg + .getPayload()).key); + Assert.assertEquals(1, + (((EntryEventMessagePayload) msg + .getPayload()).value).getId()); + Assert.assertEquals("TestName1", + (((EntryEventMessagePayload) msg + .getPayload()).value).getName()); + Assert.assertEquals("TestSurname1", + (((EntryEventMessagePayload) msg + .getPayload()).value).getSurname()); + } - @Test - public void testEventDrivenForOnlyREMOVEDEntryEvent() { - edMultiMap2.put(1, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1")); - edMultiMap2.put(2, new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2")); - edMultiMap2.remove(2); - Message msg = edMultiMapChannel2.receive(2_000); - Assert.assertNotNull(msg); - Assert.assertNotNull(msg.getPayload()); - Assert.assertTrue(msg.getPayload() instanceof EntryEventMessagePayload); - Assert.assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); - Assert.assertEquals(EntryEventType.REMOVED.name(), msg.getHeaders().get(HazelcastHeaders.EVENT_TYPE)); - Assert.assertEquals("edMultiMap2", msg.getHeaders().get(HazelcastHeaders.CACHE_NAME)); + @Test + public void testEventDrivenForOnlyREMOVEDEntryEvent() { + edMultiMap2 + .put(1, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1")); + edMultiMap2 + .put(2, new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2")); + edMultiMap2.remove(2); + Message msg = + edMultiMapChannel2.receive(HazelcastInboundChannelAdapterTestUtils.TIMEOUT); + Assert.assertNotNull(msg); + Assert.assertNotNull(msg.getPayload()); + Assert.assertTrue(msg.getPayload() instanceof EntryEventMessagePayload); + Assert.assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); + Assert.assertEquals(EntryEventType.REMOVED.name(), + msg.getHeaders().get(HazelcastHeaders.EVENT_TYPE)); + Assert.assertEquals("edMultiMap2", + msg.getHeaders().get(HazelcastHeaders.CACHE_NAME)); - Assert.assertEquals(Integer.valueOf(2), - ((EntryEventMessagePayload) msg - .getPayload()).key); - Assert.assertNull(((EntryEventMessagePayload) msg.getPayload()).value); - Assert.assertEquals(2, - (((EntryEventMessagePayload) msg - .getPayload()).oldValue).getId()); - Assert.assertEquals("TestName2", - (((EntryEventMessagePayload) msg - .getPayload()).oldValue).getName()); - Assert.assertEquals("TestSurname2", - (((EntryEventMessagePayload) msg - .getPayload()).oldValue).getSurname()); - } + Assert.assertEquals(Integer.valueOf(2), + ((EntryEventMessagePayload) msg + .getPayload()).key); + Assert.assertNull(((EntryEventMessagePayload) msg.getPayload()).value); + Assert.assertEquals(2, + (((EntryEventMessagePayload) msg + .getPayload()).oldValue).getId()); + Assert.assertEquals("TestName2", + (((EntryEventMessagePayload) msg + .getPayload()).oldValue).getName()); + Assert.assertEquals("TestSurname2", + (((EntryEventMessagePayload) msg + .getPayload()).oldValue).getSurname()); + } - @Test - public void testEventDrivenForALLEntryEvent() { - edMultiMap3.put(1, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1")); - Message msg = edMultiMapChannel3.receive(2_000); - verifyEntryEvent(msg, "edMultiMap3", EntryEventType.ADDED); - - edMultiMap3.put(1, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurnameUpdated")); - msg = edMultiMapChannel3.receive(2_000); - verifyEntryEvent(msg, "edMultiMap3", EntryEventType.ADDED); - - edMultiMap3.remove(1); - msg = edMultiMapChannel3.receive(2_000); - verifyEntryEvent(msg, "edMultiMap3", EntryEventType.REMOVED); - msg = edMultiMapChannel3.receive(2_000); - verifyEntryEvent(msg, "edMultiMap3", EntryEventType.REMOVED); - - edMultiMap3.put(2, new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2")); - msg = edMultiMapChannel3.receive(2_000); - verifyEntryEvent(msg, "edMultiMap3", EntryEventType.ADDED); - - edMultiMap3.clear(); - msg = edMultiMapChannel3.receive(2_000); - verifyEntryEvent(msg, "edMultiMap3", EntryEventType.CLEAR_ALL); - } + @Test + public void testEventDrivenForALLEntryEvent() { + HazelcastInboundChannelAdapterTestUtils + .testEventDrivenForMultiMapEntryEvents(edMultiMap3, edMultiMapChannel3, + "edMultiMap3"); + } } diff --git a/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/HazelcastReplicatedMapEventDrivenInboundChannelAdapterTests.java b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/HazelcastReplicatedMapEventDrivenInboundChannelAdapterTests.java index 52725dc..8b4cb7d 100644 --- a/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/HazelcastReplicatedMapEventDrivenInboundChannelAdapterTests.java +++ b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/HazelcastReplicatedMapEventDrivenInboundChannelAdapterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 the original author or authors. + * Copyright 2015-2016 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. @@ -23,9 +23,9 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.integration.hazelcast.AbstractHazelcastTestSupport; import org.springframework.integration.hazelcast.HazelcastHeaders; import org.springframework.integration.hazelcast.HazelcastIntegrationTestUser; +import org.springframework.integration.hazelcast.inbound.util.HazelcastInboundChannelAdapterTestUtils; import org.springframework.integration.hazelcast.message.EntryEventMessagePayload; import org.springframework.messaging.Message; import org.springframework.messaging.PollableChannel; @@ -46,138 +46,139 @@ import com.hazelcast.core.ReplicatedMap; @ContextConfiguration @DirtiesContext @SuppressWarnings("unchecked") -public class HazelcastReplicatedMapEventDrivenInboundChannelAdapterTests extends AbstractHazelcastTestSupport { +public class HazelcastReplicatedMapEventDrivenInboundChannelAdapterTests { - @Autowired - private PollableChannel edReplicatedMapChannel1; + @Autowired + private PollableChannel edReplicatedMapChannel1; - @Autowired - private PollableChannel edReplicatedMapChannel2; + @Autowired + private PollableChannel edReplicatedMapChannel2; - @Autowired - private PollableChannel edReplicatedMapChannel3; + @Autowired + private PollableChannel edReplicatedMapChannel3; - @Autowired - private PollableChannel edReplicatedMapChannel4; + @Autowired + private PollableChannel edReplicatedMapChannel4; - @Resource - private ReplicatedMap edReplicatedMap1; + @Resource + private ReplicatedMap edReplicatedMap1; - @Resource - private ReplicatedMap edReplicatedMap2; + @Resource + private ReplicatedMap edReplicatedMap2; - @Resource - private ReplicatedMap edReplicatedMap3; + @Resource + private ReplicatedMap edReplicatedMap3; - @Resource - private ReplicatedMap edReplicatedMap4; + @Resource + private ReplicatedMap edReplicatedMap4; - @Test - public void testEventDrivenForOnlyADDEDEntryEvent() { - edReplicatedMap1.put(1, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1")); - Message msg = edReplicatedMapChannel1.receive(2_000); - Assert.assertNotNull(msg); - Assert.assertNotNull(msg.getPayload()); - Assert.assertTrue(msg.getPayload() instanceof EntryEventMessagePayload); - Assert.assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); - Assert.assertEquals(EntryEventType.ADDED.name(), msg.getHeaders().get(HazelcastHeaders.EVENT_TYPE)); - Assert.assertEquals("edReplicatedMap1", msg.getHeaders().get(HazelcastHeaders.CACHE_NAME)); + @Test + public void testEventDrivenForOnlyADDEDEntryEvent() { + edReplicatedMap1 + .put(1, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1")); + Message msg = edReplicatedMapChannel1 + .receive(HazelcastInboundChannelAdapterTestUtils.TIMEOUT); + Assert.assertNotNull(msg); + Assert.assertNotNull(msg.getPayload()); + Assert.assertTrue(msg.getPayload() instanceof EntryEventMessagePayload); + Assert.assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); + Assert.assertEquals(EntryEventType.ADDED.name(), + msg.getHeaders().get(HazelcastHeaders.EVENT_TYPE)); + Assert.assertEquals("edReplicatedMap1", + msg.getHeaders().get(HazelcastHeaders.CACHE_NAME)); - Assert.assertEquals(Integer.valueOf(1), - ((EntryEventMessagePayload) msg - .getPayload()).key); - Assert.assertEquals(1, - (((EntryEventMessagePayload) msg - .getPayload()).value).getId()); - Assert.assertEquals("TestName1", - (((EntryEventMessagePayload) msg - .getPayload()).value).getName()); - Assert.assertEquals("TestSurname1", - (((EntryEventMessagePayload) msg - .getPayload()).value).getSurname()); - } + Assert.assertEquals(Integer.valueOf(1), + ((EntryEventMessagePayload) msg + .getPayload()).key); + Assert.assertEquals(1, + (((EntryEventMessagePayload) msg + .getPayload()).value).getId()); + Assert.assertEquals("TestName1", + (((EntryEventMessagePayload) msg + .getPayload()).value).getName()); + Assert.assertEquals("TestSurname1", + (((EntryEventMessagePayload) msg + .getPayload()).value).getSurname()); + } - @Test - public void testEventDrivenForOnlyUPDATEDEntryEvent() { - edReplicatedMap2.put(2, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1")); - edReplicatedMap2.put(2, new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2")); - Message msg = edReplicatedMapChannel2.receive(2_000); - Assert.assertNotNull(msg); - Assert.assertNotNull(msg.getPayload()); + @Test + public void testEventDrivenForOnlyUPDATEDEntryEvent() { + edReplicatedMap2 + .put(2, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1")); + edReplicatedMap2 + .put(2, new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2")); + Message msg = edReplicatedMapChannel2 + .receive(HazelcastInboundChannelAdapterTestUtils.TIMEOUT); + Assert.assertNotNull(msg); + Assert.assertNotNull(msg.getPayload()); - Assert.assertTrue(msg.getPayload() instanceof EntryEventMessagePayload); - Assert.assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); - Assert.assertEquals(EntryEventType.UPDATED.name(), msg.getHeaders().get(HazelcastHeaders.EVENT_TYPE)); - Assert.assertEquals("edReplicatedMap2", msg.getHeaders().get(HazelcastHeaders.CACHE_NAME)); + Assert.assertTrue(msg.getPayload() instanceof EntryEventMessagePayload); + Assert.assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); + Assert.assertEquals(EntryEventType.UPDATED.name(), + msg.getHeaders().get(HazelcastHeaders.EVENT_TYPE)); + Assert.assertEquals("edReplicatedMap2", + msg.getHeaders().get(HazelcastHeaders.CACHE_NAME)); - Assert.assertEquals(Integer.valueOf(2), - ((EntryEventMessagePayload) msg - .getPayload()).key); - Assert.assertEquals(1, - (((EntryEventMessagePayload) msg - .getPayload()).oldValue).getId()); - Assert.assertEquals("TestName1", - (((EntryEventMessagePayload) msg - .getPayload()).oldValue).getName()); - Assert.assertEquals("TestSurname1", - (((EntryEventMessagePayload) msg - .getPayload()).oldValue).getSurname()); - Assert.assertEquals(2, - (((EntryEventMessagePayload) msg - .getPayload()).value).getId()); - Assert.assertEquals("TestName2", - (((EntryEventMessagePayload) msg - .getPayload()).value).getName()); - Assert.assertEquals("TestSurname2", - (((EntryEventMessagePayload) msg - .getPayload()).value).getSurname()); - } + Assert.assertEquals(Integer.valueOf(2), + ((EntryEventMessagePayload) msg + .getPayload()).key); + Assert.assertEquals(1, + (((EntryEventMessagePayload) msg + .getPayload()).oldValue).getId()); + Assert.assertEquals("TestName1", + (((EntryEventMessagePayload) msg + .getPayload()).oldValue).getName()); + Assert.assertEquals("TestSurname1", + (((EntryEventMessagePayload) msg + .getPayload()).oldValue).getSurname()); + Assert.assertEquals(2, + (((EntryEventMessagePayload) msg + .getPayload()).value).getId()); + Assert.assertEquals("TestName2", + (((EntryEventMessagePayload) msg + .getPayload()).value).getName()); + Assert.assertEquals("TestSurname2", + (((EntryEventMessagePayload) msg + .getPayload()).value).getSurname()); + } - @Test - public void testEventDrivenForOnlyREMOVEDEntryEvent() { - edReplicatedMap3.put(1, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1")); - edReplicatedMap3.put(2, new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2")); - edReplicatedMap3.remove(2); - Message msg = edReplicatedMapChannel3.receive(2_000); - Assert.assertNotNull(msg); - Assert.assertNotNull(msg.getPayload()); - Assert.assertTrue(msg.getPayload() instanceof EntryEventMessagePayload); - Assert.assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); - Assert.assertEquals(EntryEventType.REMOVED.name(), msg.getHeaders().get(HazelcastHeaders.EVENT_TYPE)); - Assert.assertEquals("edReplicatedMap3", msg.getHeaders().get(HazelcastHeaders.CACHE_NAME)); + @Test + public void testEventDrivenForOnlyREMOVEDEntryEvent() { + edReplicatedMap3 + .put(1, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1")); + edReplicatedMap3 + .put(2, new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2")); + edReplicatedMap3.remove(2); + Message msg = edReplicatedMapChannel3 + .receive(HazelcastInboundChannelAdapterTestUtils.TIMEOUT); + Assert.assertNotNull(msg); + Assert.assertNotNull(msg.getPayload()); + Assert.assertTrue(msg.getPayload() instanceof EntryEventMessagePayload); + Assert.assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); + Assert.assertEquals(EntryEventType.REMOVED.name(), + msg.getHeaders().get(HazelcastHeaders.EVENT_TYPE)); + Assert.assertEquals("edReplicatedMap3", + msg.getHeaders().get(HazelcastHeaders.CACHE_NAME)); - Assert.assertEquals(Integer.valueOf(2), - ((EntryEventMessagePayload) msg - .getPayload()).key); - Assert.assertEquals(2, - (((EntryEventMessagePayload) msg - .getPayload()).oldValue).getId()); - Assert.assertEquals("TestName2", - (((EntryEventMessagePayload) msg - .getPayload()).oldValue).getName()); - Assert.assertEquals("TestSurname2", - (((EntryEventMessagePayload) msg - .getPayload()).oldValue).getSurname()); - } + Assert.assertEquals(Integer.valueOf(2), + ((EntryEventMessagePayload) msg + .getPayload()).key); + Assert.assertEquals(2, + (((EntryEventMessagePayload) msg + .getPayload()).oldValue).getId()); + Assert.assertEquals("TestName2", + (((EntryEventMessagePayload) msg + .getPayload()).oldValue).getName()); + Assert.assertEquals("TestSurname2", + (((EntryEventMessagePayload) msg + .getPayload()).oldValue).getSurname()); + } - @Test - public void testEventDrivenForALLEntryEvent() { - edReplicatedMap4.put(1, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1")); - Message msg = edReplicatedMapChannel4.receive(2_000); - verifyEntryEvent(msg, "edReplicatedMap4", EntryEventType.ADDED); - - edReplicatedMap4.put(1, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurnameUpdated")); - msg = edReplicatedMapChannel4.receive(2_000); - verifyEntryEvent(msg, "edReplicatedMap4", EntryEventType.UPDATED); - - edReplicatedMap4.remove(1); - msg = edReplicatedMapChannel4.receive(2_000); - verifyEntryEvent(msg, "edReplicatedMap4", EntryEventType.REMOVED); - - edReplicatedMap4.put(2, new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2")); - msg = edReplicatedMapChannel4.receive(2_000); - verifyEntryEvent(msg, "edReplicatedMap4", EntryEventType.ADDED); - - } + @Test + public void testEventDrivenForALLEntryEvent() { + HazelcastInboundChannelAdapterTestUtils + .testEventDrivenForReplicatedMapEntryEvents(edReplicatedMap4, + edReplicatedMapChannel4, "edReplicatedMap4"); + } } diff --git a/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/config/HazelcastCQDistributedMapInboundChannelAdapterConfigTests.java b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/config/HazelcastCQDistributedMapInboundChannelAdapterConfigTests.java new file mode 100644 index 0000000..418bffc --- /dev/null +++ b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/config/HazelcastCQDistributedMapInboundChannelAdapterConfigTests.java @@ -0,0 +1,87 @@ +/* + * Copyright 2015-2016 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.hazelcast.inbound.config; + +import javax.annotation.Resource; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.integration.hazelcast.HazelcastIntegrationTestUser; +import org.springframework.integration.hazelcast.inbound.util.HazelcastInboundChannelAdapterTestUtils; +import org.springframework.messaging.PollableChannel; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import com.hazelcast.core.IMap; + +/** + * Hazelcast Continuous Query Inbound Channel Adapter JavaConfig driven Unit Test Class + * + * @author Eren Avsarogullari + * @since 1.0.0 + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = HazelcastIntegrationInboundTestConfiguration.class, + loader = AnnotationConfigContextLoader.class) +@DirtiesContext +public class HazelcastCQDistributedMapInboundChannelAdapterConfigTests { + + @Autowired + private PollableChannel cqDistributedMapChannel1; + + @Autowired + private PollableChannel cqDistributedMapChannel2; + + @Autowired + private PollableChannel cqDistributedMapChannel3; + + @Resource + private IMap testCQDistributedMap1; + + @Resource + private IMap testCQDistributedMap2; + + @Resource + private IMap testCQDistributedMap3; + + @Test + public void testContinuousQueryForADDEDEntryEvent() { + HazelcastInboundChannelAdapterTestUtils + .testEventDrivenForADDEDDistributedMapEntryEvent(testCQDistributedMap1, + cqDistributedMapChannel1, "Test_CQ_Distributed_Map1"); + } + + @Test + public void testContinuousQueryForALLEntryEvent() { + HazelcastInboundChannelAdapterTestUtils + .testEventDrivenForDistributedMapEntryEvents(testCQDistributedMap2, + cqDistributedMapChannel2, "Test_CQ_Distributed_Map2"); + } + + @Test + public void testContinuousQueryForUPDATEDEntryEventWhenIncludeValueIsFalse() { + HazelcastInboundChannelAdapterTestUtils + .testContinuousQueryForUPDATEDEntryEventWhenIncludeValueIsFalse( + testCQDistributedMap3, cqDistributedMapChannel3, + "Test_CQ_Distributed_Map3"); + } + +} diff --git a/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/config/HazelcastClusterMonitorInboundChannelAdapterConfigTests.java b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/config/HazelcastClusterMonitorInboundChannelAdapterConfigTests.java new file mode 100644 index 0000000..9c1a441 --- /dev/null +++ b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/config/HazelcastClusterMonitorInboundChannelAdapterConfigTests.java @@ -0,0 +1,65 @@ +/* + * Copyright 2015-2016 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.hazelcast.inbound.config; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.integration.hazelcast.inbound.util.HazelcastInboundChannelAdapterTestUtils; +import org.springframework.messaging.PollableChannel; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.hazelcast.core.HazelcastInstance; + +/** + * Hazelcast Cluster Monitor Inbound Channel Adapter JavaConfig driven Unit Test Class + * + * @author Eren Avsarogullari + * @since 1.0.0 + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = HazelcastIntegrationInboundTestConfiguration.class) +@DirtiesContext +public class HazelcastClusterMonitorInboundChannelAdapterConfigTests { + + @Autowired + private PollableChannel cmonChannel; + + @Autowired + private PollableChannel cmonChannel2; + + @Autowired + private HazelcastInstance testHazelcastInstance; + + @Test + public void testConfigDrivenMembershipEvent() { + HazelcastInboundChannelAdapterTestUtils + .testMembershipEvent(testHazelcastInstance, cmonChannel, "testKey1", + "testValue1"); + } + + @Test + public void testConfigDrivenDistributedObjectEvent() { + HazelcastInboundChannelAdapterTestUtils + .testDistributedObjectEventByChannelAndHazelcastInstance(cmonChannel2, + testHazelcastInstance, "Test_Distributed_Map3"); + } + +} diff --git a/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/config/HazelcastDistributedSQLInboundChannelAdapterConfigTests.java b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/config/HazelcastDistributedSQLInboundChannelAdapterConfigTests.java new file mode 100644 index 0000000..cf07e40 --- /dev/null +++ b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/config/HazelcastDistributedSQLInboundChannelAdapterConfigTests.java @@ -0,0 +1,99 @@ +/* + * Copyright 2015-2016 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.hazelcast.inbound.config; + +import javax.annotation.Resource; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.integration.hazelcast.HazelcastIntegrationTestUser; +import org.springframework.integration.hazelcast.inbound.util.HazelcastInboundChannelAdapterTestUtils; +import org.springframework.messaging.PollableChannel; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import com.hazelcast.core.IMap; + +/** + * Hazelcast Distributed SQL Inbound Channel Adapter JavaConfig driven Unit Test Class + * + * @author Eren Avsarogullari + * @since 1.0.0 + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = HazelcastIntegrationInboundTestConfiguration.class, + loader = AnnotationConfigContextLoader.class) +@DirtiesContext +public class HazelcastDistributedSQLInboundChannelAdapterConfigTests { + + @Autowired + private PollableChannel dsDistributedMapChannel; + + @Autowired + private PollableChannel dsDistributedMapChannel2; + + @Autowired + private PollableChannel dsDistributedMapChannel3; + + @Autowired + private PollableChannel dsDistributedMapChannel4; + + @Resource + private IMap testDSDistributedMap; + + @Resource + private IMap testDSDistributedMap2; + + @Resource + private IMap testDSDistributedMap3; + + @Resource + private IMap testDSDistributedMap4; + + @Test + public void testDistributedSQLForENTRYIterationType() { + HazelcastInboundChannelAdapterTestUtils + .testDistributedSQLForENTRYIterationType(testDSDistributedMap, + dsDistributedMapChannel); + } + + @Test + public void testDistributedSQLForKEYIterationType() { + HazelcastInboundChannelAdapterTestUtils + .testDistributedSQLForKEYIterationType(testDSDistributedMap2, + dsDistributedMapChannel2); + } + + @Test + public void testDistributedSQLForLOCAL_KEYIterationType() { + HazelcastInboundChannelAdapterTestUtils + .testDistributedSQLForLOCAL_KEYIterationType(testDSDistributedMap3, + dsDistributedMapChannel3); + } + + @Test + public void testDistributedSQLForVALUEIterationType() { + HazelcastInboundChannelAdapterTestUtils + .testDistributedSQLForVALUEIterationType(testDSDistributedMap4, + dsDistributedMapChannel4); + } + +} diff --git a/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/config/HazelcastEventDrivenInboundChannelAdapterConfigTests.java b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/config/HazelcastEventDrivenInboundChannelAdapterConfigTests.java new file mode 100644 index 0000000..0c3ed1d --- /dev/null +++ b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/config/HazelcastEventDrivenInboundChannelAdapterConfigTests.java @@ -0,0 +1,156 @@ +/* + * Copyright 2015-2016 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.hazelcast.inbound.config; + +import javax.annotation.Resource; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.integration.hazelcast.HazelcastIntegrationTestUser; +import org.springframework.integration.hazelcast.inbound.util.HazelcastInboundChannelAdapterTestUtils; +import org.springframework.messaging.PollableChannel; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import com.hazelcast.core.IList; +import com.hazelcast.core.IMap; +import com.hazelcast.core.IQueue; +import com.hazelcast.core.ISet; +import com.hazelcast.core.ITopic; +import com.hazelcast.core.MultiMap; +import com.hazelcast.core.ReplicatedMap; + +/** + * Hazelcast Event Driven Inbound Channel Adapter JavaConfig driven Unit Test Class + * + * @author Eren Avsarogullari + * @since 1.0.0 + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = HazelcastIntegrationInboundTestConfiguration.class, + loader = AnnotationConfigContextLoader.class) +@DirtiesContext +public class HazelcastEventDrivenInboundChannelAdapterConfigTests { + + @Autowired + private PollableChannel distributedMapChannel; + + @Autowired + private PollableChannel distributedMapChannel2; + + @Autowired + private PollableChannel distributedListChannel; + + @Autowired + private PollableChannel distributedSetChannel; + + @Autowired + private PollableChannel distributedQueueChannel; + + @Autowired + private PollableChannel topicChannel; + + @Autowired + private PollableChannel replicatedMapChannel; + + @Autowired + private PollableChannel multiMapChannel; + + @Resource + private IMap testDistributedMap; + + @Resource + private IMap testDistributedMap2; + + @Resource + private IList testDistributedList; + + @Resource + private ISet testDistributedSet; + + @Resource + private IQueue testDistributedQueue; + + @Resource + private ITopic testTopic; + + @Resource + private ReplicatedMap testReplicatedMap; + + @Resource + private MultiMap testMultiMap; + + @Test + public void testEventDrivenForADDEDEntryEvent() { + HazelcastInboundChannelAdapterTestUtils + .testEventDrivenForADDEDDistributedMapEntryEvent(testDistributedMap, + distributedMapChannel, "Test_Distributed_Map"); + } + + @Test + public void testEventDrivenForEntryEvents() { + HazelcastInboundChannelAdapterTestUtils + .testEventDrivenForDistributedMapEntryEvents(testDistributedMap2, + distributedMapChannel2, "Test_Distributed_Map2"); + } + + @Test + public void testEventDrivenForDistributedListItemEvents() { + HazelcastInboundChannelAdapterTestUtils + .testEventDrivenForDistributedCollectionItemEvents(testDistributedList, + distributedListChannel); + } + + @Test + public void testEventDrivenForDistributedSetItemEvents() { + HazelcastInboundChannelAdapterTestUtils + .testEventDrivenForDistributedCollectionItemEvents(testDistributedSet, + distributedSetChannel); + } + + @Test + public void testEventDrivenForDistributedQueueItemEvents() { + HazelcastInboundChannelAdapterTestUtils + .testEventDrivenForDistributedCollectionItemEvents(testDistributedQueue, + distributedQueueChannel); + } + + @Test + public void testEventDrivenForADDEDMessageEvent() { + HazelcastInboundChannelAdapterTestUtils + .testEventDrivenForTopicMessageEvent(testTopic, topicChannel); + } + + @Test + public void testEventDrivenForReplicatedMapEntryEvents() { + HazelcastInboundChannelAdapterTestUtils + .testEventDrivenForReplicatedMapEntryEvents(testReplicatedMap, + replicatedMapChannel, "Test_Replicated_Map"); + } + + @Test + public void testEventDrivenForMultiMapEntryEvents() { + HazelcastInboundChannelAdapterTestUtils + .testEventDrivenForMultiMapEntryEvents(testMultiMap, multiMapChannel, + "Test_Multi_Map"); + } + +} diff --git a/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/config/HazelcastIntegrationInboundTestConfiguration.java b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/config/HazelcastIntegrationInboundTestConfiguration.java new file mode 100644 index 0000000..6f0b214 --- /dev/null +++ b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/config/HazelcastIntegrationInboundTestConfiguration.java @@ -0,0 +1,379 @@ +/* + * Copyright 2015-2016 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.hazelcast.inbound.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.integration.annotation.InboundChannelAdapter; +import org.springframework.integration.annotation.Poller; +import org.springframework.integration.channel.QueueChannel; +import org.springframework.integration.config.EnableIntegration; +import org.springframework.integration.hazelcast.DistributedSQLIterationType; +import org.springframework.integration.hazelcast.HazelcastIntegrationTestUser; +import org.springframework.integration.hazelcast.inbound.HazelcastClusterMonitorMessageProducer; +import org.springframework.integration.hazelcast.inbound.HazelcastContinuousQueryMessageProducer; +import org.springframework.integration.hazelcast.inbound.HazelcastDistributedSQLMessageSource; +import org.springframework.integration.hazelcast.inbound.HazelcastEventDrivenMessageProducer; +import org.springframework.messaging.PollableChannel; + +import com.hazelcast.core.Hazelcast; +import com.hazelcast.core.HazelcastInstance; +import com.hazelcast.core.IList; +import com.hazelcast.core.IMap; +import com.hazelcast.core.IQueue; +import com.hazelcast.core.ISet; +import com.hazelcast.core.ITopic; +import com.hazelcast.core.MultiMap; +import com.hazelcast.core.ReplicatedMap; + +/** + * Configuration Class for Hazelcast Integration Inbound Test + * + * @author Eren Avsarogullari + * @since 1.0.0 + */ +@Configuration +@EnableIntegration +public class HazelcastIntegrationInboundTestConfiguration { + + @Bean + public PollableChannel distributedMapChannel() { + return new QueueChannel(); + } + + @Bean + public PollableChannel distributedMapChannel2() { + return new QueueChannel(); + } + + @Bean + public PollableChannel distributedListChannel() { + return new QueueChannel(); + } + + @Bean + public PollableChannel distributedSetChannel() { + return new QueueChannel(); + } + + @Bean + public PollableChannel distributedQueueChannel() { + return new QueueChannel(); + } + + @Bean + public PollableChannel topicChannel() { + return new QueueChannel(); + } + + @Bean + public PollableChannel multiMapChannel() { + return new QueueChannel(); + } + + @Bean + public PollableChannel replicatedMapChannel() { + return new QueueChannel(); + } + + @Bean + public PollableChannel cqDistributedMapChannel1() { + return new QueueChannel(); + } + + @Bean + public PollableChannel cqDistributedMapChannel2() { + return new QueueChannel(); + } + + @Bean + public PollableChannel cqDistributedMapChannel3() { + return new QueueChannel(); + } + + @Bean + public PollableChannel dsDistributedMapChannel() { + return new QueueChannel(); + } + + @Bean + public PollableChannel dsDistributedMapChannel2() { + return new QueueChannel(); + } + + @Bean + public PollableChannel dsDistributedMapChannel3() { + return new QueueChannel(); + } + + @Bean + public PollableChannel dsDistributedMapChannel4() { + return new QueueChannel(); + } + + @Bean + public PollableChannel cmonChannel() { + return new QueueChannel(); + } + + @Bean + public PollableChannel cmonChannel2() { + return new QueueChannel(); + } + + @Bean + public IMap testDistributedMap() { + return testHazelcastInstance().getMap("Test_Distributed_Map"); + } + + @Bean + public IMap testDistributedMap2() { + return testHazelcastInstance().getMap("Test_Distributed_Map2"); + } + + @Bean + public IList testDistributedList() { + return testHazelcastInstance().getList("Test_Distributed_List"); + } + + @Bean + public ISet testDistributedSet() { + return testHazelcastInstance().getSet("Test_Distributed_Set"); + } + + @Bean + public IQueue testDistributedQueue() { + return testHazelcastInstance().getQueue("Test_Distributed_Queue"); + } + + @Bean + public ITopic testTopic() { + return testHazelcastInstance().getTopic("Test_Topic"); + } + + @Bean + public MultiMap testMultiMap() { + return testHazelcastInstance().getMultiMap("Test_Multi_Map"); + } + + @Bean + public ReplicatedMap testReplicatedMap() { + return testHazelcastInstance().getReplicatedMap("Test_Replicated_Map"); + } + + @Bean + public IMap testCQDistributedMap1() { + return testHazelcastInstance().getMap("Test_CQ_Distributed_Map1"); + } + + @Bean + public IMap testCQDistributedMap2() { + return testHazelcastInstance().getMap("Test_CQ_Distributed_Map2"); + } + + @Bean + public IMap testCQDistributedMap3() { + return testHazelcastInstance().getMap("Test_CQ_Distributed_Map3"); + } + + @Bean + public IMap testDSDistributedMap() { + return testHazelcastInstance().getMap("Test_DS_Distributed_Map"); + } + + @Bean + public IMap testDSDistributedMap2() { + return testHazelcastInstance().getMap("Test_DS_Distributed_Map2"); + } + + @Bean + public IMap testDSDistributedMap3() { + return testHazelcastInstance().getMap("Test_DS_Distributed_Map3"); + } + + @Bean + public IMap testDSDistributedMap4() { + return testHazelcastInstance().getMap("Test_DS_Distributed_Map4"); + } + + @Bean + public HazelcastInstance testHazelcastInstance() { + return Hazelcast.newHazelcastInstance(); + } + + @Bean + public HazelcastEventDrivenMessageProducer hazelcastEventDrivenMessageProducer() { + final HazelcastEventDrivenMessageProducer producer = + new HazelcastEventDrivenMessageProducer(testDistributedMap()); + producer.setOutputChannel(distributedMapChannel()); + return producer; + } + + @Bean + public HazelcastEventDrivenMessageProducer hazelcastEventDrivenMessageProducer2() { + final HazelcastEventDrivenMessageProducer producer = + new HazelcastEventDrivenMessageProducer(testDistributedMap2()); + producer.setOutputChannel(distributedMapChannel2()); + producer.setCacheEventTypes("ADDED,REMOVED,UPDATED,CLEAR_ALL"); + return producer; + } + + @Bean + public HazelcastEventDrivenMessageProducer hazelcastEventDrivenMessageProducer3() { + final HazelcastEventDrivenMessageProducer producer = + new HazelcastEventDrivenMessageProducer(testDistributedList()); + producer.setOutputChannel(distributedListChannel()); + producer.setCacheEventTypes("ADDED,REMOVED"); + return producer; + } + + @Bean + public HazelcastEventDrivenMessageProducer hazelcastEventDrivenMessageProducer4() { + final HazelcastEventDrivenMessageProducer producer = + new HazelcastEventDrivenMessageProducer(testDistributedSet()); + producer.setOutputChannel(distributedSetChannel()); + producer.setCacheEventTypes("ADDED,REMOVED"); + return producer; + } + + @Bean + public HazelcastEventDrivenMessageProducer hazelcastEventDrivenMessageProducer5() { + final HazelcastEventDrivenMessageProducer producer = + new HazelcastEventDrivenMessageProducer(testDistributedQueue()); + producer.setOutputChannel(distributedQueueChannel()); + producer.setCacheEventTypes("ADDED,REMOVED"); + return producer; + } + + @Bean + public HazelcastEventDrivenMessageProducer hazelcastEventDrivenMessageProducer6() { + final HazelcastEventDrivenMessageProducer producer = + new HazelcastEventDrivenMessageProducer(testTopic()); + producer.setOutputChannel(topicChannel()); + return producer; + } + + @Bean + public HazelcastEventDrivenMessageProducer hazelcastEventDrivenMessageProducer7() { + final HazelcastEventDrivenMessageProducer producer = + new HazelcastEventDrivenMessageProducer(testReplicatedMap()); + producer.setOutputChannel(replicatedMapChannel()); + producer.setCacheEventTypes("ADDED,REMOVED,UPDATED"); + return producer; + } + + @Bean + public HazelcastEventDrivenMessageProducer hazelcastEventDrivenMessageProducer8() { + final HazelcastEventDrivenMessageProducer producer = + new HazelcastEventDrivenMessageProducer(testMultiMap()); + producer.setOutputChannel(multiMapChannel()); + producer.setCacheEventTypes("ADDED,REMOVED,CLEAR_ALL"); + return producer; + } + + @Bean + public HazelcastContinuousQueryMessageProducer hazelcastContinuousQueryMessageProducer() { + final HazelcastContinuousQueryMessageProducer producer = + new HazelcastContinuousQueryMessageProducer(testCQDistributedMap1(), + "name=TestName1"); + producer.setOutputChannel(cqDistributedMapChannel1()); + return producer; + } + + @Bean + public HazelcastContinuousQueryMessageProducer hazelcastContinuousQueryMessageProducer2() { + final HazelcastContinuousQueryMessageProducer producer = + new HazelcastContinuousQueryMessageProducer(testCQDistributedMap2(), + "name=TestName1 OR name=TestName2"); + producer.setOutputChannel(cqDistributedMapChannel2()); + producer.setCacheEventTypes("ADDED,REMOVED,UPDATED,CLEAR_ALL"); + return producer; + } + + @Bean + public HazelcastContinuousQueryMessageProducer hazelcastContinuousQueryMessageProducer3() { + final HazelcastContinuousQueryMessageProducer producer = + new HazelcastContinuousQueryMessageProducer(testCQDistributedMap3(), + "surname=TestSurname2"); + producer.setOutputChannel(cqDistributedMapChannel3()); + producer.setCacheEventTypes("UPDATED"); + producer.setIncludeValue(false); + return producer; + } + + @Bean + public HazelcastClusterMonitorMessageProducer hazelcastClusterMonitorMessageProducer() { + final HazelcastClusterMonitorMessageProducer producer = + new HazelcastClusterMonitorMessageProducer(testHazelcastInstance()); + producer.setOutputChannel(cmonChannel()); + return producer; + } + + @Bean + public HazelcastClusterMonitorMessageProducer hazelcastClusterMonitorMessageProducer2() { + final HazelcastClusterMonitorMessageProducer producer = + new HazelcastClusterMonitorMessageProducer(testHazelcastInstance()); + producer.setOutputChannel(cmonChannel2()); + producer.setMonitorEventTypes("DISTRIBUTED_OBJECT"); + return producer; + } + + @Bean + @InboundChannelAdapter(value = "dsDistributedMapChannel", + poller = @Poller(maxMessagesPerPoll = "1")) + public HazelcastDistributedSQLMessageSource hazelcastDistributedSQLMessageSource() { + final HazelcastDistributedSQLMessageSource messageSource = + new HazelcastDistributedSQLMessageSource(testDSDistributedMap(), + "name='TestName4' AND surname='TestSurname4'"); + messageSource.setIterationType(DistributedSQLIterationType.ENTRY); + return messageSource; + } + + @Bean + @InboundChannelAdapter(value = "dsDistributedMapChannel2", + poller = @Poller(maxMessagesPerPoll = "1")) + public HazelcastDistributedSQLMessageSource hazelcastDistributedSQLMessageSource2() { + final HazelcastDistributedSQLMessageSource messageSource = + new HazelcastDistributedSQLMessageSource(testDSDistributedMap2(), + "name='TestName1' AND surname='TestSurname1'"); + messageSource.setIterationType(DistributedSQLIterationType.KEY); + return messageSource; + } + + @Bean + @InboundChannelAdapter(value = "dsDistributedMapChannel3", + poller = @Poller(maxMessagesPerPoll = "1")) + public HazelcastDistributedSQLMessageSource hazelcastDistributedSQLMessageSource3() { + final HazelcastDistributedSQLMessageSource messageSource = + new HazelcastDistributedSQLMessageSource(testDSDistributedMap3(), + "age > 5"); + messageSource.setIterationType(DistributedSQLIterationType.LOCAL_KEY); + return messageSource; + } + + @Bean + @InboundChannelAdapter(value = "dsDistributedMapChannel4", + poller = @Poller(maxMessagesPerPoll = "1")) + public HazelcastDistributedSQLMessageSource hazelcastDistributedSQLMessageSource4() { + final HazelcastDistributedSQLMessageSource messageSource = + new HazelcastDistributedSQLMessageSource(testDSDistributedMap4(), + "name='TestName3' AND surname='TestSurname3'"); + messageSource.setIterationType(DistributedSQLIterationType.VALUE); + return messageSource; + } + +} diff --git a/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/util/HazelcastInboundChannelAdapterTestUtils.java b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/util/HazelcastInboundChannelAdapterTestUtils.java new file mode 100644 index 0000000..5f5fb90 --- /dev/null +++ b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/inbound/util/HazelcastInboundChannelAdapterTestUtils.java @@ -0,0 +1,404 @@ +/* + * Copyright 2015-2016 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.hazelcast.inbound.util; + +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.assertThat; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.Collection; +import java.util.Map; + +import org.junit.Assert; + +import org.springframework.integration.hazelcast.HazelcastHeaders; +import org.springframework.integration.hazelcast.HazelcastIntegrationTestUser; +import org.springframework.integration.hazelcast.message.EntryEventMessagePayload; +import org.springframework.messaging.Message; +import org.springframework.messaging.PollableChannel; + +import com.hazelcast.core.DistributedObjectEvent; +import com.hazelcast.core.EntryEventType; +import com.hazelcast.core.HazelcastInstance; +import com.hazelcast.core.ICollection; +import com.hazelcast.core.IMap; +import com.hazelcast.core.ITopic; +import com.hazelcast.core.Member; +import com.hazelcast.core.MembershipEvent; +import com.hazelcast.core.MultiMap; +import com.hazelcast.core.ReplicatedMap; +import com.hazelcast.spi.exception.DistributedObjectDestroyedException; + +/** + * Util Class for Hazelcast Inbound Channel Adapters Test Support + * @author Eren Avsarogullari + * @since 1.0.0 + */ +@SuppressWarnings("unchecked") +public class HazelcastInboundChannelAdapterTestUtils { + + public static final int TIMEOUT = 20_000; + + public static void verifyEntryEvent(Message msg, String cacheName, + EntryEventType event) { + Assert.assertNotNull(msg); + Assert.assertNotNull(msg.getPayload()); + if (event == EntryEventType.CLEAR_ALL || event == EntryEventType.EVICT_ALL) { + Assert.assertTrue(msg.getPayload() instanceof Integer); + } + else { + Assert.assertTrue(msg.getPayload() instanceof EntryEventMessagePayload); + } + + Assert.assertEquals(cacheName, msg.getHeaders().get(HazelcastHeaders.CACHE_NAME)); + Assert.assertEquals(event.name(), + msg.getHeaders().get(HazelcastHeaders.EVENT_TYPE)); + Assert.assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); + } + + public static void verifyItemEvent(Message msg, EntryEventType event) { + Assert.assertNotNull(msg); + Assert.assertNotNull(msg.getPayload()); + Assert.assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); + Assert.assertEquals(event.toString(), + msg.getHeaders().get(HazelcastHeaders.EVENT_TYPE).toString()); + } + + public static void testEventDrivenForADDEDDistributedMapEntryEvent( + final IMap distributedMap, + final PollableChannel channel, final String cacheName) { + HazelcastIntegrationTestUser hazelcastIntegrationTestUser = + new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1"); + distributedMap.put(1, hazelcastIntegrationTestUser); + Message msg = channel.receive(TIMEOUT); + Assert.assertNotNull(msg); + Assert.assertNotNull(msg.getPayload()); + Assert.assertTrue(msg.getPayload() instanceof EntryEventMessagePayload); + Assert.assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); + Assert.assertEquals(EntryEventType.ADDED.name(), + msg.getHeaders().get(HazelcastHeaders.EVENT_TYPE)); + Assert.assertEquals(cacheName, msg.getHeaders().get(HazelcastHeaders.CACHE_NAME)); + + Assert.assertEquals(Integer.valueOf(1), + ((EntryEventMessagePayload) msg + .getPayload()).key); + Assert.assertEquals(1, + (((EntryEventMessagePayload) msg + .getPayload()).value).getId()); + Assert.assertEquals("TestName1", + (((EntryEventMessagePayload) msg + .getPayload()).value).getName()); + Assert.assertEquals("TestSurname1", + (((EntryEventMessagePayload) msg + .getPayload()).value).getSurname()); + } + + public static void testEventDrivenForDistributedMapEntryEvents( + final IMap distributedMap, + final PollableChannel channel, final String cacheName) { + distributedMap + .put(1, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1")); + Message msg = channel.receive(TIMEOUT); + verifyEntryEvent(msg, cacheName, EntryEventType.ADDED); + + distributedMap.put(1, + new HazelcastIntegrationTestUser(1, "TestName1", "TestSurnameUpdated")); + msg = channel.receive(TIMEOUT); + verifyEntryEvent(msg, cacheName, EntryEventType.UPDATED); + + distributedMap.remove(1); + msg = channel.receive(TIMEOUT); + verifyEntryEvent(msg, cacheName, EntryEventType.REMOVED); + + distributedMap + .put(2, new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2")); + msg = channel.receive(TIMEOUT); + verifyEntryEvent(msg, cacheName, EntryEventType.ADDED); + + distributedMap.clear(); + msg = channel.receive(TIMEOUT); + verifyEntryEvent(msg, cacheName, EntryEventType.CLEAR_ALL); + } + + public static void testEventDrivenForDistributedCollectionItemEvents( + final ICollection distributedObject, + final PollableChannel channel) { + HazelcastIntegrationTestUser user = + new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1"); + distributedObject.add(user); + Message msg = channel.receive(TIMEOUT); + verifyItemEvent(msg, EntryEventType.ADDED); + + distributedObject.remove(user); + msg = channel.receive(TIMEOUT); + verifyItemEvent(msg, EntryEventType.REMOVED); + + user = new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2"); + distributedObject.add(user); + msg = channel.receive(TIMEOUT); + verifyItemEvent(msg, EntryEventType.ADDED); + } + + public static void testEventDrivenForReplicatedMapEntryEvents( + final ReplicatedMap replicatedMap, + final PollableChannel channel, final String cacheName) { + replicatedMap + .put(1, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1")); + Message msg = channel.receive(TIMEOUT); + verifyEntryEvent(msg, cacheName, EntryEventType.ADDED); + + replicatedMap.put(1, + new HazelcastIntegrationTestUser(1, "TestName1", "TestSurnameUpdated")); + msg = channel.receive(TIMEOUT); + verifyEntryEvent(msg, cacheName, EntryEventType.UPDATED); + + replicatedMap.remove(1); + msg = channel.receive(TIMEOUT); + verifyEntryEvent(msg, cacheName, EntryEventType.REMOVED); + + replicatedMap + .put(2, new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2")); + msg = channel.receive(TIMEOUT); + verifyEntryEvent(msg, cacheName, EntryEventType.ADDED); + } + + public static void testEventDrivenForTopicMessageEvent( + final ITopic topic, final PollableChannel channel) { + topic.publish(new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1")); + Message msg = channel.receive(TIMEOUT); + Assert.assertNotNull(msg); + Assert.assertNotNull(msg.getPayload()); + Assert.assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); + Assert.assertNotNull(msg.getHeaders().get(HazelcastHeaders.PUBLISHING_TIME)); + Assert.assertEquals(topic.getName(), + msg.getHeaders().get(HazelcastHeaders.CACHE_NAME)); + Assert.assertEquals(1, ((HazelcastIntegrationTestUser) msg.getPayload()).getId()); + Assert.assertEquals("TestName1", + ((HazelcastIntegrationTestUser) msg.getPayload()).getName()); + Assert.assertEquals("TestSurname1", + ((HazelcastIntegrationTestUser) msg.getPayload()).getSurname()); + } + + public static void testEventDrivenForMultiMapEntryEvents( + final MultiMap multiMap, + final PollableChannel channel, final String cacheName) { + multiMap.put(1, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1")); + Message msg = channel.receive(TIMEOUT); + verifyEntryEvent(msg, cacheName, EntryEventType.ADDED); + + multiMap.put(1, + new HazelcastIntegrationTestUser(1, "TestName1", "TestSurnameUpdated")); + msg = channel.receive(TIMEOUT); + verifyEntryEvent(msg, cacheName, EntryEventType.ADDED); + + multiMap.remove(1); + msg = channel.receive(TIMEOUT); + verifyEntryEvent(msg, cacheName, EntryEventType.REMOVED); + msg = channel.receive(TIMEOUT); + verifyEntryEvent(msg, cacheName, EntryEventType.REMOVED); + + multiMap.put(2, new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2")); + msg = channel.receive(TIMEOUT); + verifyEntryEvent(msg, cacheName, EntryEventType.ADDED); + + multiMap.clear(); + msg = channel.receive(TIMEOUT); + verifyEntryEvent(msg, cacheName, EntryEventType.CLEAR_ALL); + } + + public static void testContinuousQueryForUPDATEDEntryEventWhenIncludeValueIsFalse( + final IMap cqDistributedMap, + final PollableChannel channel, final String cacheName) { + cqDistributedMap + .put(1, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1")); + cqDistributedMap + .put(1, new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2")); + Message msg = channel.receive(TIMEOUT); + assertNotNull(msg); + assertNotNull(msg.getPayload()); + assertTrue(msg.getPayload() instanceof EntryEventMessagePayload); + assertNotNull(msg.getHeaders().get(HazelcastHeaders.MEMBER)); + assertEquals(EntryEventType.UPDATED.name(), + msg.getHeaders().get(HazelcastHeaders.EVENT_TYPE)); + assertEquals(cacheName, msg.getHeaders().get(HazelcastHeaders.CACHE_NAME)); + + assertEquals(Integer.valueOf(1), + ((EntryEventMessagePayload) msg + .getPayload()).key); + assertNull(((EntryEventMessagePayload) msg + .getPayload()).oldValue); + assertNull(((EntryEventMessagePayload) msg + .getPayload()).value); + } + + public static void testDistributedSQLForENTRYIterationType( + final IMap dsDistributedMap, + final PollableChannel channel) { + dsDistributedMap + .put(1, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1", 10)); + dsDistributedMap + .put(2, new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2", 20)); + dsDistributedMap + .put(3, new HazelcastIntegrationTestUser(3, "TestName3", "TestSurname3", 30)); + dsDistributedMap + .put(4, new HazelcastIntegrationTestUser(4, "TestName4", "TestSurname4", 40)); + dsDistributedMap + .put(5, new HazelcastIntegrationTestUser(5, "TestName5", "TestSurname5", 50)); + + Message msg = channel.receive(TIMEOUT); + + Assert.assertNotNull(msg); + Assert.assertNotNull(msg.getPayload()); + Assert.assertTrue(msg.getPayload() instanceof Collection); + Assert.assertEquals(4, + (((Map.Entry) ((Collection) msg.getPayload()).iterator().next()) + .getKey())); + Assert.assertEquals(4, + ((HazelcastIntegrationTestUser) ((Map.Entry) ((Collection) msg + .getPayload()).iterator().next()).getValue()).getId()); + Assert.assertEquals("TestName4", + ((HazelcastIntegrationTestUser) ((Map.Entry) ((Collection) msg + .getPayload()).iterator().next()).getValue()).getName()); + Assert.assertEquals("TestSurname4", + ((HazelcastIntegrationTestUser) ((Map.Entry) ((Collection) msg + .getPayload()).iterator().next()).getValue()).getSurname()); + } + + public static void testDistributedSQLForKEYIterationType( + final IMap dsDistributedMap, + final PollableChannel channel) { + dsDistributedMap + .put(1, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1", 10)); + dsDistributedMap + .put(2, new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2", 20)); + dsDistributedMap + .put(3, new HazelcastIntegrationTestUser(3, "TestName3", "TestSurname3", 30)); + + Message msg = channel.receive(TIMEOUT); + + Assert.assertNotNull(msg); + Assert.assertNotNull(msg.getPayload()); + Assert.assertTrue(msg.getPayload() instanceof Collection); + Assert.assertEquals(1, ((Collection) msg.getPayload()).iterator().next()); + } + + public static void testDistributedSQLForLOCAL_KEYIterationType( + final IMap dsDistributedMap, + final PollableChannel channel) { + dsDistributedMap + .put(1, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1", 10)); + dsDistributedMap + .put(2, new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2", 20)); + dsDistributedMap + .put(3, new HazelcastIntegrationTestUser(3, "TestName3", "TestSurname3", 30)); + + Message msg = channel.receive(TIMEOUT); + + Assert.assertNotNull(msg); + Assert.assertNotNull(msg.getPayload()); + Assert.assertTrue(msg.getPayload() instanceof Collection); + } + + public static void testDistributedSQLForVALUEIterationType( + final IMap dsDistributedMap, + final PollableChannel channel) { + dsDistributedMap + .put(1, new HazelcastIntegrationTestUser(1, "TestName1", "TestSurname1", 10)); + dsDistributedMap + .put(2, new HazelcastIntegrationTestUser(2, "TestName2", "TestSurname2", 20)); + dsDistributedMap + .put(3, new HazelcastIntegrationTestUser(3, "TestName3", "TestSurname3", 30)); + dsDistributedMap + .put(4, new HazelcastIntegrationTestUser(4, "TestName4", "TestSurname4", 40)); + dsDistributedMap + .put(5, new HazelcastIntegrationTestUser(5, "TestName5", "TestSurname5", 50)); + + Message msg = channel.receive(TIMEOUT); + + Assert.assertNotNull(msg); + Assert.assertNotNull(msg.getPayload()); + Assert.assertTrue(msg.getPayload() instanceof Collection); + Assert.assertEquals(3, + ((HazelcastIntegrationTestUser) (((Collection) msg.getPayload()).iterator() + .next())).getId()); + Assert.assertEquals("TestName3", + ((HazelcastIntegrationTestUser) (((Collection) msg.getPayload()).iterator() + .next())).getName()); + Assert.assertEquals("TestSurname3", + ((HazelcastIntegrationTestUser) (((Collection) msg.getPayload()).iterator() + .next())).getSurname()); + } + + public static void testMembershipEvent(final HazelcastInstance instance, + final PollableChannel channel, final String key, final String value) { + Member member = instance.getCluster().getMembers().iterator().next(); + member.setStringAttribute(key, value); + + Message msg = channel.receive(TIMEOUT); + verifyMembershipEvent(msg, MembershipEvent.MEMBER_ATTRIBUTE_CHANGED); + } + + public static void testDistributedObjectEventByChannelAndHazelcastInstance( + final PollableChannel channel, final HazelcastInstance hazelcastInstance, + final String distributedObjectName) { + final IMap distributedMap = + hazelcastInstance.getMap(distributedObjectName); + + Message msg = channel.receive(TIMEOUT); + verifyDistributedObjectEvent(msg, DistributedObjectEvent.EventType.CREATED, + distributedObjectName); + + distributedMap.destroy(); + + msg = channel.receive(TIMEOUT); + try { + // Since Hazelcast 3.6 we can use DistributedObjectEvent.getDistributedObject() for DESTROYED objects. + verifyDistributedObjectEvent(msg, DistributedObjectEvent.EventType.DESTROYED, distributedObjectName); + fail("DistributedObjectDestroyedException expected"); + } + catch (Exception e) { + assertThat(e, instanceOf(DistributedObjectDestroyedException.class)); + } + } + + private static void verifyMembershipEvent(final Message msg, + final int membershipEvent) { + assertNotNull(msg); + assertNotNull(msg.getPayload()); + assertTrue(msg.getPayload() instanceof MembershipEvent); + assertEquals(membershipEvent, + ((MembershipEvent) msg.getPayload()).getEventType()); + assertNotNull(((MembershipEvent) msg.getPayload()).getMember()); + } + + private static void verifyDistributedObjectEvent(final Message msg, + final DistributedObjectEvent.EventType eventType, + final String distributedObjectName) { + assertNotNull(msg); + assertNotNull(msg.getPayload()); + assertTrue(msg.getPayload() instanceof DistributedObjectEvent); + assertEquals(eventType, + ((DistributedObjectEvent) msg.getPayload()).getEventType()); + assertNotNull((((DistributedObjectEvent) msg.getPayload()).getDistributedObject()) + .getName(), distributedObjectName); + } + +} diff --git a/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/outbound/HazelcastOutboundChannelAdapterTests.java b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/outbound/HazelcastOutboundChannelAdapterTests.java index 8e6d737..8709c6a 100644 --- a/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/outbound/HazelcastOutboundChannelAdapterTests.java +++ b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/outbound/HazelcastOutboundChannelAdapterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 the original author or authors. + * Copyright 2015-2016 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. @@ -21,7 +21,6 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.util.ArrayList; -import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -30,9 +29,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Queue; import java.util.Set; -import java.util.SortedSet; import java.util.TreeMap; -import java.util.TreeSet; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.TimeUnit; @@ -47,6 +44,7 @@ import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.integration.hazelcast.HazelcastHeaders; import org.springframework.integration.hazelcast.HazelcastIntegrationTestUser; import org.springframework.integration.hazelcast.HazelcastTestRequestHandlerAdvice; +import org.springframework.integration.hazelcast.outbound.util.HazelcastOutboundChannelAdapterTestUtils; import org.springframework.integration.support.DefaultMessageBuilderFactory; import org.springframework.integration.support.MessageBuilderFactory; import org.springframework.messaging.Message; @@ -58,7 +56,6 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.hazelcast.core.ITopic; -import com.hazelcast.core.MessageListener; import com.hazelcast.core.MultiMap; import com.hazelcast.core.ReplicatedMap; @@ -72,479 +69,405 @@ import com.hazelcast.core.ReplicatedMap; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration @DirtiesContext -@SuppressWarnings({ "rawtypes", "unchecked" }) +@SuppressWarnings({"rawtypes", "unchecked"}) public class HazelcastOutboundChannelAdapterTests { - private static final int DATA_COUNT = 100; + private static final String DISTRIBUTED_MAP = "distributedMap"; - private static final int DEFAULT_AGE = 5; + private static final String CACHE_HEADER = "CACHE_HEADER"; - private static final String TEST_NAME = "Test_Name"; + private final MessageBuilderFactory messageBuilderFactory = + new DefaultMessageBuilderFactory(); - private static final String TEST_SURNAME = "Test_Surname"; + @Autowired + @Qualifier("firstMapChannel") + private MessageChannel firstMapChannel; - private static final String DISTRIBUTED_MAP = "distributedMap"; + @Autowired + @Qualifier("secondMapChannel") + private MessageChannel secondMapChannel; - private static final String CACHE_HEADER = "CACHE_HEADER"; + @Autowired + @Qualifier("thirdMapChannel") + private MessageChannel thirdMapChannel; - private final MessageBuilderFactory messageBuilderFactory = new DefaultMessageBuilderFactory(); + @Autowired + @Qualifier("fourthMapChannel") + private MessageChannel fourthMapChannel; - @Autowired - @Qualifier("firstMapChannel") - private MessageChannel firstMapChannel; + @Autowired + @Qualifier("fifthMapChannel") + private MessageChannel fifthMapChannel; - @Autowired - @Qualifier("secondMapChannel") - private MessageChannel secondMapChannel; + @Autowired + @Qualifier("sixthMapChannel") + private MessageChannel sixthMapChannel; - @Autowired - @Qualifier("thirdMapChannel") - private MessageChannel thirdMapChannel; + @Autowired + @Qualifier("bulkMapChannel") + private MessageChannel bulkMapChannel; - @Autowired - @Qualifier("fourthMapChannel") - private MessageChannel fourthMapChannel; + @Autowired + @Qualifier("multiMapChannel") + private MessageChannel multiMapChannel; - @Autowired - @Qualifier("fifthMapChannel") - private MessageChannel fifthMapChannel; + @Autowired + @Qualifier("replicatedMapChannel") + private MessageChannel replicatedMapChannel; - @Autowired - @Qualifier("sixthMapChannel") - private MessageChannel sixthMapChannel; + @Autowired + @Qualifier("bulkReplicatedMapChannel") + private MessageChannel bulkReplicatedMapChannel; - @Autowired - @Qualifier("bulkMapChannel") - private MessageChannel bulkMapChannel; + @Autowired + @Qualifier("listChannel") + private MessageChannel listChannel; - @Autowired - @Qualifier("multiMapChannel") - private MessageChannel multiMapChannel; + @Autowired + @Qualifier("bulkListChannel") + private MessageChannel bulkListChannel; - @Autowired - @Qualifier("replicatedMapChannel") - private MessageChannel replicatedMapChannel; + @Autowired + @Qualifier("setChannel") + private MessageChannel setChannel; - @Autowired - @Qualifier("bulkReplicatedMapChannel") - private MessageChannel bulkReplicatedMapChannel; + @Autowired + @Qualifier("bulkSetChannel") + private MessageChannel bulkSetChannel; - @Autowired - @Qualifier("listChannel") - private MessageChannel listChannel; + @Autowired + @Qualifier("queueChannel") + private MessageChannel queueChannel; - @Autowired - @Qualifier("bulkListChannel") - private MessageChannel bulkListChannel; + @Autowired + @Qualifier("bulkQueueChannel") + private MessageChannel bulkQueueChannel; - @Autowired - @Qualifier("setChannel") - private MessageChannel setChannel; + @Autowired + @Qualifier("topicChannel") + private MessageChannel topicChannel; - @Autowired - @Qualifier("bulkSetChannel") - private MessageChannel bulkSetChannel; + @Autowired + @Qualifier("lockChannel") + private MessageChannel lockChannel; - @Autowired - @Qualifier("queueChannel") - private MessageChannel queueChannel; + @Resource + private Map distributedMap; - @Autowired - @Qualifier("bulkQueueChannel") - private MessageChannel bulkQueueChannel; + @Resource + private Map distributedBulkMap; - @Autowired - @Qualifier("topicChannel") - private MessageChannel topicChannel; + @Resource + private MultiMap multiMap; - @Autowired - @Qualifier("lockChannel") - private MessageChannel lockChannel; + @Resource + private ReplicatedMap replicatedMap; - @Resource - private Map distributedMap; + @Resource + private ReplicatedMap bulkReplicatedMap; - @Resource - private Map distributedBulkMap; + @Resource + private List distributedList; - @Resource - private MultiMap multiMap; + @Resource + private List distributedBulkList; - @Resource - private ReplicatedMap replicatedMap; - - @Resource - private ReplicatedMap bulkReplicatedMap; - - @Resource - private List distributedList; - - @Resource - private List distributedBulkList; - - @Resource - private Set distributedSet; - - @Resource - private Set distributedBulkSet; - - @Resource - private Queue distributedQueue; - - @Resource - private Queue distributedBulkQueue; - - @Resource - private ITopic topic; - - @Autowired - @Qualifier("testFirstMapRequestHandlerAdvice") - private HazelcastTestRequestHandlerAdvice testFirstMapRequestHandlerAdvice; - - @Autowired - @Qualifier("testSecondMapRequestHandlerAdvice") - private HazelcastTestRequestHandlerAdvice testSecondMapRequestHandlerAdvice; - - @Autowired - @Qualifier("testThirdMapRequestHandlerAdvice") - private HazelcastTestRequestHandlerAdvice testThirdMapRequestHandlerAdvice; - - @Autowired - @Qualifier("testFourthMapRequestHandlerAdvice") - private HazelcastTestRequestHandlerAdvice testFourthMapRequestHandlerAdvice; - - @Autowired - @Qualifier("testBulkMapRequestHandlerAdvice") - private HazelcastTestRequestHandlerAdvice testBulkMapRequestHandlerAdvice; - - @Autowired - @Qualifier("testMultiMapRequestHandlerAdvice") - private HazelcastTestRequestHandlerAdvice testMultiMapRequestHandlerAdvice; - - @Autowired - @Qualifier("testReplicatedMapRequestHandlerAdvice") - private HazelcastTestRequestHandlerAdvice testReplicatedMapRequestHandlerAdvice; - - @Autowired - @Qualifier("testBulkReplicatedMapRequestHandlerAdvice") - private HazelcastTestRequestHandlerAdvice testBulkReplicatedMapRequestHandlerAdvice; - - @Autowired - @Qualifier("testListRequestHandlerAdvice") - private HazelcastTestRequestHandlerAdvice testListRequestHandlerAdvice; - - @Autowired - @Qualifier("testBulkListRequestHandlerAdvice") - private HazelcastTestRequestHandlerAdvice testBulkListRequestHandlerAdvice; - - @Autowired - @Qualifier("testSetRequestHandlerAdvice") - private HazelcastTestRequestHandlerAdvice testSetRequestHandlerAdvice; - - @Autowired - @Qualifier("testBulkSetRequestHandlerAdvice") - private HazelcastTestRequestHandlerAdvice testBulkSetRequestHandlerAdvice; - - @Autowired - @Qualifier("testQueueRequestHandlerAdvice") - private HazelcastTestRequestHandlerAdvice testQueueRequestHandlerAdvice; - - @Autowired - @Qualifier("testBulkQueueRequestHandlerAdvice") - private HazelcastTestRequestHandlerAdvice testBulkQueueRequestHandlerAdvice; - - @Autowired - @Qualifier("testTopicRequestHandlerAdvice") - private HazelcastTestRequestHandlerAdvice testTopicRequestHandlerAdvice; - - @Before - public void setUp() { - this.distributedMap.clear(); - this.distributedBulkMap.clear(); - this.distributedList.clear(); - this.distributedBulkList.clear(); - this.distributedSet.clear(); - this.distributedBulkSet.clear(); - this.distributedQueue.clear(); - this.distributedBulkQueue.clear(); - this.multiMap.clear(); - this.replicatedMap.clear(); - this.bulkReplicatedMap.clear(); - } - - @Test - public void testWriteToDistributedMap() throws InterruptedException { - sendMessageToChannel(this.firstMapChannel); - assertTrue(this.testFirstMapRequestHandlerAdvice.executeLatch.await(10, - TimeUnit.SECONDS)); - verifyMapForPayload(new TreeMap(this.distributedMap)); - } - - @Test - public void testBulkWriteToDistributedMap() throws InterruptedException { - Map userMap = new HashMap<>(DATA_COUNT); - for (int index = 1; index <= DATA_COUNT; index++) { - userMap.put(index, getTestUser(index)); - } - - this.bulkMapChannel.send(new GenericMessage<>(userMap)); - - assertTrue(this.testBulkMapRequestHandlerAdvice.executeLatch.await(10, - TimeUnit.SECONDS)); - verifyMapForPayload(new TreeMap(this.distributedBulkMap)); - } - - @Test - public void testWriteToDistributedMapWhenCacheExpressionIsSet() - throws InterruptedException { - sendMessageWithCacheHeaderToChannel(this.secondMapChannel, CACHE_HEADER, - DISTRIBUTED_MAP); - assertTrue(this.testSecondMapRequestHandlerAdvice.executeLatch.await(10, - TimeUnit.SECONDS)); - verifyMapForPayload(new TreeMap(this.distributedMap)); - } - - @Test - public void testWriteToDistributedMapWhenHazelcastHeaderIsSet() - throws InterruptedException { - sendMessageWithCacheHeaderToChannel(this.thirdMapChannel, - HazelcastHeaders.CACHE_NAME, DISTRIBUTED_MAP); - assertTrue(this.testThirdMapRequestHandlerAdvice.executeLatch.await(10, - TimeUnit.SECONDS)); - verifyMapForPayload(new TreeMap(this.distributedMap)); - } - - @Test - public void testWriteToDistributedMapWhenExtractPayloadIsFalse() - throws InterruptedException { - sendMessageWithCacheHeaderToChannel(this.fourthMapChannel, - HazelcastHeaders.CACHE_NAME, DISTRIBUTED_MAP); - assertTrue(this.testFourthMapRequestHandlerAdvice.executeLatch.await(10, - TimeUnit.SECONDS)); - verifyMapForMessage(new TreeMap(this.distributedMap)); - } - - @Test - public void testWriteToMultiMap() throws InterruptedException { - sendMessageToChannel(this.multiMapChannel); - assertTrue(this.testMultiMapRequestHandlerAdvice.executeLatch.await(10, - TimeUnit.SECONDS)); - verifyMultiMapForPayload(this.multiMap); - } - - @Test - public void testWriteToReplicatedMap() throws InterruptedException { - sendMessageToChannel(this.replicatedMapChannel); - assertTrue(this.testReplicatedMapRequestHandlerAdvice.executeLatch.await(10, - TimeUnit.SECONDS)); - verifyMapForPayload(new TreeMap(this.replicatedMap)); - } - - @Test - public void testBulkWriteToReplicatedMap() throws InterruptedException { - Map userMap = new HashMap<>(DATA_COUNT); - for (int index = 1; index <= DATA_COUNT; index++) { - userMap.put(index, getTestUser(index)); - } - - this.bulkReplicatedMapChannel.send(new GenericMessage<>(userMap)); - - assertTrue(this.testBulkReplicatedMapRequestHandlerAdvice.executeLatch.await(10, - TimeUnit.SECONDS)); - verifyMapForPayload(new TreeMap(this.bulkReplicatedMap)); - } - - - @Test - public void testWriteToDistributedList() throws InterruptedException { - sendMessageToChannel(this.listChannel); - assertTrue(this.testListRequestHandlerAdvice.executeLatch.await(10, - TimeUnit.SECONDS)); - verifyCollection(this.distributedList, DATA_COUNT); - } - - @Test - public void testBulkWriteToDistributedList() throws InterruptedException { - List userList = new ArrayList<>(DATA_COUNT); - for (int index = 1; index <= DATA_COUNT; index++) { - userList.add(getTestUser(index)); - } - - this.bulkListChannel.send(new GenericMessage<>(userList)); - - assertTrue(this.testBulkListRequestHandlerAdvice.executeLatch.await(10, - TimeUnit.SECONDS)); - verifyCollection(this.distributedBulkList, DATA_COUNT); - } - - @Test - public void testWriteToDistributedSet() throws InterruptedException { - sendMessageToChannel(this.setChannel); - assertTrue(this.testSetRequestHandlerAdvice.executeLatch.await(10, - TimeUnit.SECONDS)); - final List list = new ArrayList(this.distributedSet); - Collections.sort(list); - verifyCollection(list, DATA_COUNT); - } - - @Test - public void testBulkWriteToDistributedSet() throws InterruptedException { - Set userSet = new HashSet<>(DATA_COUNT); - for (int index = 1; index <= DATA_COUNT; index++) { - userSet.add(getTestUser(index)); - } - - this.bulkSetChannel.send(new GenericMessage<>(userSet)); - - assertTrue(this.testBulkSetRequestHandlerAdvice.executeLatch.await(10, - TimeUnit.SECONDS)); - final List list = new ArrayList(this.distributedBulkSet); - Collections.sort(list); - verifyCollection(list, DATA_COUNT); - } - - @Test - public void testWriteToDistributedQueue() throws InterruptedException { - sendMessageToChannel(this.queueChannel); - assertTrue(this.testQueueRequestHandlerAdvice.executeLatch.await(10, - TimeUnit.SECONDS)); - verifyCollection(this.distributedQueue, DATA_COUNT); - } - - @Test - public void testBulkWriteToDistributedQueue() throws InterruptedException { - Queue userQueue = new ArrayBlockingQueue(DATA_COUNT); - for (int index = 1; index <= DATA_COUNT; index++) { - userQueue.add(getTestUser(index)); - } - - this.bulkQueueChannel.send(new GenericMessage<>(userQueue)); - - assertTrue(this.testBulkQueueRequestHandlerAdvice.executeLatch.await(10, - TimeUnit.SECONDS)); - verifyCollection(this.distributedBulkQueue, DATA_COUNT); - } - - @Test - public void testWriteToTopic() throws InterruptedException { - this.topic.addMessageListener(new TestTopicMessageListener()); - sendMessageToChannel(this.topicChannel); - assertTrue(this.testTopicRequestHandlerAdvice.executeLatch.await(10, - TimeUnit.SECONDS)); - } - - @Test(expected = MessageHandlingException.class) - public void testWriteToDistributedMapWhenCacheIsNotSet() { - this.fifthMapChannel.send(new GenericMessage<>(getTestUser(1))); - } - - @Test(expected = MessageHandlingException.class) - public void testWriteToDistributedMapWhenKeyExpressionIsNotSet() { - Message message = this.messageBuilderFactory - .withPayload(getTestUser(1)) - .setHeader(HazelcastHeaders.CACHE_NAME, DISTRIBUTED_MAP).build(); - this.sixthMapChannel.send(message); - } - - @Test(expected = MessageHandlingException.class) - public void testWriteToLock() { - this.lockChannel.send(new GenericMessage<>("foo")); - } - - private void sendMessageToChannel(final MessageChannel channel) { - for (int index = 1; index <= DATA_COUNT; index++) { - channel.send(new GenericMessage<>(getTestUser(index))); - } - } - - private void sendMessageWithCacheHeaderToChannel(final MessageChannel channel, - final String headerName, final String distributedObjectName) { - for (int index = 1; index <= DATA_COUNT; index++) { - Message message = this.messageBuilderFactory - .withPayload(getTestUser(index)) - .setHeader(headerName, distributedObjectName).build(); - channel.send(message); - } - } - - private void verifyMapForPayload(final Map map) { - int index = 1; - assertNotNull(map); - assertEquals(true, map.size() == DATA_COUNT); - for (Entry entry : map.entrySet()) { - assertNotNull(entry); - assertEquals(index, entry.getKey().intValue()); - verifyHazelcastIntegrationTestUser(entry.getValue(), index); - index++; - } - } - - private void verifyMultiMapForPayload( - final MultiMap multiMap) { - int index = 1; - assertNotNull(multiMap); - assertEquals(true, multiMap.size() == DATA_COUNT); - SortedSet keys = new TreeSet<>(multiMap.keySet()); - for (Integer key : keys) { - assertNotNull(key); - assertEquals(index, key.intValue()); - HazelcastIntegrationTestUser user = multiMap.get(key).iterator().next(); - verifyHazelcastIntegrationTestUser(user, index); - index++; - } - } - - private void verifyMapForMessage( - final Map> map) { - int index = 1; - assertNotNull(map); - assertEquals(true, map.size() == DATA_COUNT); - for (Entry> entry : map.entrySet()) { - assertNotNull(entry); - assertEquals(index, entry.getKey().intValue()); - assertTrue(entry.getValue().getHeaders().size() > 0); - verifyHazelcastIntegrationTestUser(entry.getValue().getPayload(), index); - index++; - } - } - - private void verifyCollection(final Collection coll, - final int dataCount) { - int index = 1; - assertNotNull(coll); - assertEquals(true, coll.size() == dataCount); - for (HazelcastIntegrationTestUser user : coll) { - verifyHazelcastIntegrationTestUser(user, index); - index++; - } - } - - private void verifyHazelcastIntegrationTestUser(HazelcastIntegrationTestUser user, - int index) { - assertNotNull(user); - assertEquals(index, user.getId()); - assertEquals(TEST_NAME, user.getName()); - assertEquals(TEST_SURNAME, user.getSurname()); - assertEquals(index + DEFAULT_AGE, user.getAge()); - } - - private HazelcastIntegrationTestUser getTestUser(int index) { - return new HazelcastIntegrationTestUser(index, TEST_NAME, TEST_SURNAME, index - + DEFAULT_AGE); - } - - private class TestTopicMessageListener implements MessageListener { - - private int index = 1; - - @Override - public void onMessage(com.hazelcast.core.Message message) { - HazelcastIntegrationTestUser user = (HazelcastIntegrationTestUser) message - .getMessageObject(); - verifyHazelcastIntegrationTestUser(user, index); - index++; - } - - } + @Resource + private Set distributedSet; + + @Resource + private Set distributedBulkSet; + + @Resource + private Queue distributedQueue; + + @Resource + private Queue distributedBulkQueue; + + @Resource + private ITopic topic; + + @Autowired + @Qualifier("testFirstMapRequestHandlerAdvice") + private HazelcastTestRequestHandlerAdvice testFirstMapRequestHandlerAdvice; + + @Autowired + @Qualifier("testSecondMapRequestHandlerAdvice") + private HazelcastTestRequestHandlerAdvice testSecondMapRequestHandlerAdvice; + + @Autowired + @Qualifier("testThirdMapRequestHandlerAdvice") + private HazelcastTestRequestHandlerAdvice testThirdMapRequestHandlerAdvice; + + @Autowired + @Qualifier("testFourthMapRequestHandlerAdvice") + private HazelcastTestRequestHandlerAdvice testFourthMapRequestHandlerAdvice; + + @Autowired + @Qualifier("testBulkMapRequestHandlerAdvice") + private HazelcastTestRequestHandlerAdvice testBulkMapRequestHandlerAdvice; + + @Autowired + @Qualifier("testMultiMapRequestHandlerAdvice") + private HazelcastTestRequestHandlerAdvice testMultiMapRequestHandlerAdvice; + + @Autowired + @Qualifier("testReplicatedMapRequestHandlerAdvice") + private HazelcastTestRequestHandlerAdvice testReplicatedMapRequestHandlerAdvice; + + @Autowired + @Qualifier("testBulkReplicatedMapRequestHandlerAdvice") + private HazelcastTestRequestHandlerAdvice testBulkReplicatedMapRequestHandlerAdvice; + + @Autowired + @Qualifier("testListRequestHandlerAdvice") + private HazelcastTestRequestHandlerAdvice testListRequestHandlerAdvice; + + @Autowired + @Qualifier("testBulkListRequestHandlerAdvice") + private HazelcastTestRequestHandlerAdvice testBulkListRequestHandlerAdvice; + + @Autowired + @Qualifier("testSetRequestHandlerAdvice") + private HazelcastTestRequestHandlerAdvice testSetRequestHandlerAdvice; + + @Autowired + @Qualifier("testBulkSetRequestHandlerAdvice") + private HazelcastTestRequestHandlerAdvice testBulkSetRequestHandlerAdvice; + + @Autowired + @Qualifier("testQueueRequestHandlerAdvice") + private HazelcastTestRequestHandlerAdvice testQueueRequestHandlerAdvice; + + @Autowired + @Qualifier("testBulkQueueRequestHandlerAdvice") + private HazelcastTestRequestHandlerAdvice testBulkQueueRequestHandlerAdvice; + + @Autowired + @Qualifier("testTopicRequestHandlerAdvice") + private HazelcastTestRequestHandlerAdvice testTopicRequestHandlerAdvice; + + @Before + public void setUp() { + this.distributedMap.clear(); + this.distributedBulkMap.clear(); + this.distributedList.clear(); + this.distributedBulkList.clear(); + this.distributedSet.clear(); + this.distributedBulkSet.clear(); + this.distributedQueue.clear(); + this.distributedBulkQueue.clear(); + this.multiMap.clear(); + this.replicatedMap.clear(); + this.bulkReplicatedMap.clear(); + } + + @Test + public void testWriteToDistributedMap() throws InterruptedException { + HazelcastOutboundChannelAdapterTestUtils + .testWriteToDistributedMap(this.firstMapChannel, this.distributedMap, + this.testFirstMapRequestHandlerAdvice); + } + + @Test + public void testBulkWriteToDistributedMap() { + HazelcastOutboundChannelAdapterTestUtils + .testBulkWriteToDistributedMap(this.bulkMapChannel, this.distributedBulkMap, + this.testBulkMapRequestHandlerAdvice); + } + + @Test + public void testWriteToDistributedMapWhenCacheExpressionIsSet() + throws InterruptedException { + sendMessageWithCacheHeaderToChannel(this.secondMapChannel, CACHE_HEADER, + DISTRIBUTED_MAP); + assertTrue(this.testSecondMapRequestHandlerAdvice.executeLatch + .await(10, TimeUnit.SECONDS)); + HazelcastOutboundChannelAdapterTestUtils + .verifyMapForPayload(new TreeMap(this.distributedMap)); + } + + @Test + public void testWriteToDistributedMapWhenHazelcastHeaderIsSet() + throws InterruptedException { + sendMessageWithCacheHeaderToChannel(this.thirdMapChannel, + HazelcastHeaders.CACHE_NAME, DISTRIBUTED_MAP); + assertTrue(this.testThirdMapRequestHandlerAdvice.executeLatch + .await(10, TimeUnit.SECONDS)); + HazelcastOutboundChannelAdapterTestUtils + .verifyMapForPayload(new TreeMap(this.distributedMap)); + } + + @Test + public void testWriteToDistributedMapWhenExtractPayloadIsFalse() + throws InterruptedException { + sendMessageWithCacheHeaderToChannel(this.fourthMapChannel, + HazelcastHeaders.CACHE_NAME, DISTRIBUTED_MAP); + assertTrue(this.testFourthMapRequestHandlerAdvice.executeLatch + .await(10, TimeUnit.SECONDS)); + verifyMapForMessage(new TreeMap(this.distributedMap)); + } + + @Test + public void testWriteToMultiMap() { + HazelcastOutboundChannelAdapterTestUtils + .testWriteToMultiMap(this.multiMapChannel, this.multiMap, + this.testMultiMapRequestHandlerAdvice); + } + + @Test + public void testWriteToReplicatedMap() { + HazelcastOutboundChannelAdapterTestUtils + .testWriteToReplicatedMap(this.replicatedMapChannel, this.replicatedMap, + this.testReplicatedMapRequestHandlerAdvice); + } + + @Test + public void testBulkWriteToReplicatedMap() throws InterruptedException { + Map userMap = + new HashMap<>(HazelcastOutboundChannelAdapterTestUtils.DATA_COUNT); + for (int index = 1; + index <= HazelcastOutboundChannelAdapterTestUtils.DATA_COUNT; index++) { + userMap + .put(index, HazelcastOutboundChannelAdapterTestUtils.getTestUser(index)); + } + + this.bulkReplicatedMapChannel.send(new GenericMessage<>(userMap)); + + assertTrue(this.testBulkReplicatedMapRequestHandlerAdvice.executeLatch + .await(10, TimeUnit.SECONDS)); + HazelcastOutboundChannelAdapterTestUtils + .verifyMapForPayload(new TreeMap(this.bulkReplicatedMap)); + } + + + @Test + public void testWriteToDistributedList() { + HazelcastOutboundChannelAdapterTestUtils + .testWriteToDistributedList(this.listChannel, this.distributedList, + this.testListRequestHandlerAdvice); + } + + @Test + public void testBulkWriteToDistributedList() throws InterruptedException { + List userList = + new ArrayList<>(HazelcastOutboundChannelAdapterTestUtils.DATA_COUNT); + for (int index = 1; + index <= HazelcastOutboundChannelAdapterTestUtils.DATA_COUNT; index++) { + userList.add(HazelcastOutboundChannelAdapterTestUtils.getTestUser(index)); + } + + this.bulkListChannel.send(new GenericMessage<>(userList)); + + assertTrue(this.testBulkListRequestHandlerAdvice.executeLatch + .await(10, TimeUnit.SECONDS)); + HazelcastOutboundChannelAdapterTestUtils + .verifyCollection(this.distributedBulkList, + HazelcastOutboundChannelAdapterTestUtils.DATA_COUNT); + } + + @Test + public void testWriteToDistributedSet() { + HazelcastOutboundChannelAdapterTestUtils + .testWriteToDistributedSet(this.setChannel, this.distributedSet, + this.testSetRequestHandlerAdvice); + } + + @Test + public void testBulkWriteToDistributedSet() throws InterruptedException { + Set userSet = + new HashSet<>(HazelcastOutboundChannelAdapterTestUtils.DATA_COUNT); + for (int index = 1; + index <= HazelcastOutboundChannelAdapterTestUtils.DATA_COUNT; index++) { + userSet.add(HazelcastOutboundChannelAdapterTestUtils.getTestUser(index)); + } + + this.bulkSetChannel.send(new GenericMessage<>(userSet)); + + assertTrue(this.testBulkSetRequestHandlerAdvice.executeLatch + .await(10, TimeUnit.SECONDS)); + final List list = + new ArrayList(this.distributedBulkSet); + Collections.sort(list); + HazelcastOutboundChannelAdapterTestUtils + .verifyCollection(list, HazelcastOutboundChannelAdapterTestUtils.DATA_COUNT); + } + + @Test + public void testWriteToDistributedQueue() { + HazelcastOutboundChannelAdapterTestUtils + .testWriteToDistributedQueue(this.queueChannel, this.distributedQueue, + this.testQueueRequestHandlerAdvice); + } + + @Test + public void testBulkWriteToDistributedQueue() throws InterruptedException { + Queue userQueue = + new ArrayBlockingQueue(HazelcastOutboundChannelAdapterTestUtils.DATA_COUNT); + for (int index = 1; + index <= HazelcastOutboundChannelAdapterTestUtils.DATA_COUNT; index++) { + userQueue.add(HazelcastOutboundChannelAdapterTestUtils.getTestUser(index)); + } + + this.bulkQueueChannel.send(new GenericMessage<>(userQueue)); + + assertTrue(this.testBulkQueueRequestHandlerAdvice.executeLatch + .await(10, TimeUnit.SECONDS)); + HazelcastOutboundChannelAdapterTestUtils + .verifyCollection(this.distributedBulkQueue, + HazelcastOutboundChannelAdapterTestUtils.DATA_COUNT); + } + + @Test + public void testWriteToTopic() { + HazelcastOutboundChannelAdapterTestUtils + .testWriteToTopic(this.topicChannel, this.topic, + this.testTopicRequestHandlerAdvice); + } + + @Test(expected = MessageHandlingException.class) + public void testWriteToDistributedMapWhenCacheIsNotSet() { + this.fifthMapChannel.send(new GenericMessage<>( + HazelcastOutboundChannelAdapterTestUtils.getTestUser(1))); + } + + @Test(expected = MessageHandlingException.class) + public void testWriteToDistributedMapWhenKeyExpressionIsNotSet() { + Message message = this.messageBuilderFactory + .withPayload(HazelcastOutboundChannelAdapterTestUtils.getTestUser(1)) + .setHeader(HazelcastHeaders.CACHE_NAME, DISTRIBUTED_MAP).build(); + this.sixthMapChannel.send(message); + } + + @Test(expected = MessageHandlingException.class) + public void testWriteToLock() { + this.lockChannel.send(new GenericMessage<>("foo")); + } + + private void sendMessageWithCacheHeaderToChannel(final MessageChannel channel, + final String headerName, final String distributedObjectName) { + for (int index = 1; + index <= HazelcastOutboundChannelAdapterTestUtils.DATA_COUNT; index++) { + Message message = this.messageBuilderFactory + .withPayload(HazelcastOutboundChannelAdapterTestUtils.getTestUser(index)) + .setHeader(headerName, distributedObjectName).build(); + channel.send(message); + } + } + + private void verifyMapForMessage( + final Map> map) { + int index = 1; + assertNotNull(map); + assertEquals(true, map.size() == HazelcastOutboundChannelAdapterTestUtils.DATA_COUNT); + for (Entry> entry : map.entrySet()) { + assertNotNull(entry); + assertEquals(index, entry.getKey().intValue()); + assertTrue(entry.getValue().getHeaders().size() > 0); + HazelcastOutboundChannelAdapterTestUtils + .verifyHazelcastIntegrationTestUser(entry.getValue().getPayload(), index); + index++; + } + } } diff --git a/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/outbound/config/HazelcastIntegrationOutboundTestConfiguration.java b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/outbound/config/HazelcastIntegrationOutboundTestConfiguration.java new file mode 100644 index 0000000..6487998 --- /dev/null +++ b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/outbound/config/HazelcastIntegrationOutboundTestConfiguration.java @@ -0,0 +1,300 @@ +/* + * Copyright 2015-2016 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.hazelcast.outbound.config; + +import java.util.List; +import java.util.Queue; +import java.util.Set; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.expression.spel.standard.SpelExpressionParser; +import org.springframework.integration.annotation.IntegrationComponentScan; +import org.springframework.integration.annotation.ServiceActivator; +import org.springframework.integration.channel.DirectChannel; +import org.springframework.integration.config.EnableIntegration; +import org.springframework.integration.hazelcast.HazelcastIntegrationTestUser; +import org.springframework.integration.hazelcast.HazelcastTestRequestHandlerAdvice; +import org.springframework.integration.hazelcast.outbound.HazelcastCacheWritingMessageHandler; +import org.springframework.integration.hazelcast.outbound.util.HazelcastOutboundChannelAdapterTestUtils; +import org.springframework.messaging.MessageChannel; + +import com.hazelcast.core.Hazelcast; +import com.hazelcast.core.HazelcastInstance; +import com.hazelcast.core.IMap; +import com.hazelcast.core.ITopic; +import com.hazelcast.core.MultiMap; +import com.hazelcast.core.ReplicatedMap; +import com.hazelcast.core.DistributedObject; + +/** + * Configuration Class for Hazelcast Integration Outbound Test + * + * @author Eren Avsarogullari + * @since 1.0.0 + */ +@Configuration +@ComponentScan(basePackages = {"org.springframework.integration.hazelcast.*"}) +@EnableIntegration +@IntegrationComponentScan("org.springframework.integration.hazelcast.outbound") +public class HazelcastIntegrationOutboundTestConfiguration { + + @Bean + public MessageChannel distMapChannel() { + return new DirectChannel(); + } + + @Bean + public MessageChannel distMapBulkChannel() { + return new DirectChannel(); + } + + @Bean + public MessageChannel distListChannel() { + return new DirectChannel(); + } + + @Bean + public MessageChannel distSetChannel() { + return new DirectChannel(); + } + + @Bean + public MessageChannel distQueueChannel() { + return new DirectChannel(); + } + + @Bean + public MessageChannel topicChannel2() { + return new DirectChannel(); + } + + @Bean + public MessageChannel multiMapChannel2() { + return new DirectChannel(); + } + + @Bean + public MessageChannel replicatedMapChannel2() { + return new DirectChannel(); + } + + @Bean + public IMap distMap() { + return testHzInstance().getMap("Distributed_Map1"); + } + + @Bean + public IMap distBulkMap() { + return testHzInstance().getMap("Distributed_Bulk_Map1"); + } + + @Bean + public List distList() { + return testHzInstance().getList("Distributed_List1"); + } + + @Bean + public Set distSet() { + return testHzInstance().getSet("Distributed_Set1"); + } + + @Bean + public Queue distQueue() { + return testHzInstance().getQueue("Distributed_Queue1"); + } + + @Bean + public ITopic topic() { + return testHzInstance().getTopic("Topic1"); + } + + @Bean + public MultiMap multiMap() { + return testHzInstance().getMultiMap("Multi_Map1"); + } + + @Bean + public ReplicatedMap replicatedMap() { + return testHzInstance().getReplicatedMap("Replicated_Map1"); + } + + @Bean + public HazelcastInstance testHzInstance() { + return Hazelcast.newHazelcastInstance(); + } + + @Bean + public HazelcastTestRequestHandlerAdvice distMapRequestHandlerAdvice() { + return new HazelcastTestRequestHandlerAdvice( + HazelcastOutboundChannelAdapterTestUtils.DATA_COUNT); + } + + @Bean + public HazelcastTestRequestHandlerAdvice distBulkMapRequestHandlerAdvice() { + return new HazelcastTestRequestHandlerAdvice(1); + } + + @Bean + public HazelcastTestRequestHandlerAdvice distListRequestHandlerAdvice() { + return new HazelcastTestRequestHandlerAdvice( + HazelcastOutboundChannelAdapterTestUtils.DATA_COUNT); + } + + @Bean + public HazelcastTestRequestHandlerAdvice distSetRequestHandlerAdvice() { + return new HazelcastTestRequestHandlerAdvice( + HazelcastOutboundChannelAdapterTestUtils.DATA_COUNT); + } + + @Bean + public HazelcastTestRequestHandlerAdvice distQueueRequestHandlerAdvice() { + return new HazelcastTestRequestHandlerAdvice( + HazelcastOutboundChannelAdapterTestUtils.DATA_COUNT); + } + + @Bean + public HazelcastTestRequestHandlerAdvice topicRequestHandlerAdvice() { + return new HazelcastTestRequestHandlerAdvice( + HazelcastOutboundChannelAdapterTestUtils.DATA_COUNT); + } + + @Bean + public HazelcastTestRequestHandlerAdvice multiMapRequestHandlerAdvice() { + return new HazelcastTestRequestHandlerAdvice( + HazelcastOutboundChannelAdapterTestUtils.DATA_COUNT); + } + + @Bean + public HazelcastTestRequestHandlerAdvice replicatedMapRequestHandlerAdvice() { + return new HazelcastTestRequestHandlerAdvice( + HazelcastOutboundChannelAdapterTestUtils.DATA_COUNT); + } + + @Bean + @ServiceActivator(inputChannel = "distMapChannel", adviceChain = "distMapRequestHandlerAdvice") + public HazelcastCacheWritingMessageHandler hazelcastCacheWritingMessageHandler() { + final HazelcastCacheWritingMessageHandler handler = + new HazelcastCacheWritingMessageHandler(); + handler.setDistributedObject(distMap()); + handler + .setKeyExpression(new SpelExpressionParser().parseExpression("payload.id")); + handler.setExtractPayload(true); + + return handler; + } + + @Bean + @ServiceActivator(inputChannel = "distMapBulkChannel", + adviceChain = "distBulkMapRequestHandlerAdvice") + public HazelcastCacheWritingMessageHandler hazelcastCacheWritingMessageHandler2() { + final HazelcastCacheWritingMessageHandler handler = + new HazelcastCacheWritingMessageHandler(); + handler.setDistributedObject(distBulkMap()); + handler + .setKeyExpression(new SpelExpressionParser().parseExpression("payload.id")); + handler.setExtractPayload(true); + + return handler; + } + + @Bean + @ServiceActivator(inputChannel = "distListChannel", + adviceChain = "distListRequestHandlerAdvice") + public HazelcastCacheWritingMessageHandler hazelcastCacheWritingMessageHandler3() { + final HazelcastCacheWritingMessageHandler handler = + new HazelcastCacheWritingMessageHandler(); + handler.setDistributedObject((DistributedObject) distList()); + handler + .setKeyExpression(new SpelExpressionParser().parseExpression("payload.id")); + handler.setExtractPayload(true); + + return handler; + } + + @Bean + @ServiceActivator(inputChannel = "distSetChannel", + adviceChain = "distSetRequestHandlerAdvice") + public HazelcastCacheWritingMessageHandler hazelcastCacheWritingMessageHandler4() { + final HazelcastCacheWritingMessageHandler handler = + new HazelcastCacheWritingMessageHandler(); + handler.setDistributedObject((DistributedObject) distSet()); + handler + .setKeyExpression(new SpelExpressionParser().parseExpression("payload.id")); + handler.setExtractPayload(true); + + return handler; + } + + @Bean + @ServiceActivator(inputChannel = "distQueueChannel", + adviceChain = "distQueueRequestHandlerAdvice") + public HazelcastCacheWritingMessageHandler hazelcastCacheWritingMessageHandler5() { + final HazelcastCacheWritingMessageHandler handler = + new HazelcastCacheWritingMessageHandler(); + handler.setDistributedObject((DistributedObject) distQueue()); + handler + .setKeyExpression(new SpelExpressionParser().parseExpression("payload.id")); + handler.setExtractPayload(true); + + return handler; + } + + @Bean + @ServiceActivator(inputChannel = "topicChannel2", + adviceChain = "topicRequestHandlerAdvice") + public HazelcastCacheWritingMessageHandler hazelcastCacheWritingMessageHandler6() { + final HazelcastCacheWritingMessageHandler handler = + new HazelcastCacheWritingMessageHandler(); + handler.setDistributedObject(topic()); + handler + .setKeyExpression(new SpelExpressionParser().parseExpression("payload.id")); + handler.setExtractPayload(true); + + return handler; + } + + @Bean + @ServiceActivator(inputChannel = "multiMapChannel2", + adviceChain = "multiMapRequestHandlerAdvice") + public HazelcastCacheWritingMessageHandler hazelcastCacheWritingMessageHandler7() { + final HazelcastCacheWritingMessageHandler handler = + new HazelcastCacheWritingMessageHandler(); + handler.setDistributedObject(multiMap()); + handler + .setKeyExpression(new SpelExpressionParser().parseExpression("payload.id")); + handler.setExtractPayload(true); + + return handler; + } + + @Bean + @ServiceActivator(inputChannel = "replicatedMapChannel2", + adviceChain = "replicatedMapRequestHandlerAdvice") + public HazelcastCacheWritingMessageHandler hazelcastCacheWritingMessageHandler8() { + final HazelcastCacheWritingMessageHandler handler = + new HazelcastCacheWritingMessageHandler(); + handler.setDistributedObject(replicatedMap()); + handler + .setKeyExpression(new SpelExpressionParser().parseExpression("payload.id")); + handler.setExtractPayload(true); + + return handler; + } + +} diff --git a/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/outbound/config/HazelcastOutboundChannelAdapterConfigTests.java b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/outbound/config/HazelcastOutboundChannelAdapterConfigTests.java new file mode 100644 index 0000000..53bf4a9 --- /dev/null +++ b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/outbound/config/HazelcastOutboundChannelAdapterConfigTests.java @@ -0,0 +1,199 @@ +/* + * Copyright 2015-2016 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.hazelcast.outbound.config; + +import java.util.List; +import java.util.Queue; +import java.util.Set; + +import javax.annotation.Resource; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.integration.hazelcast.HazelcastIntegrationTestUser; +import org.springframework.integration.hazelcast.HazelcastTestRequestHandlerAdvice; +import org.springframework.integration.hazelcast.outbound.util.HazelcastOutboundChannelAdapterTestUtils; +import org.springframework.messaging.MessageChannel; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import com.hazelcast.core.IMap; +import com.hazelcast.core.ITopic; +import com.hazelcast.core.MultiMap; +import com.hazelcast.core.ReplicatedMap; + +/** + * Hazelcast Outbound Channel Adapter JavaConfig driven Unit Test Class + * + * @author Eren Avsarogullari + * @since 1.0.0 + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = HazelcastIntegrationOutboundTestConfiguration.class, + loader = AnnotationConfigContextLoader.class) +@DirtiesContext +public class HazelcastOutboundChannelAdapterConfigTests { + + @Autowired + @Qualifier("distMapChannel") + private MessageChannel distMapChannel; + + @Autowired + @Qualifier("distMapBulkChannel") + private MessageChannel distMapBulkChannel; + + @Autowired + @Qualifier("distListChannel") + private MessageChannel distListChannel; + + @Autowired + @Qualifier("distSetChannel") + private MessageChannel distSetChannel; + + @Autowired + @Qualifier("distQueueChannel") + private MessageChannel distQueueChannel; + + @Autowired + @Qualifier("topicChannel2") + private MessageChannel topicChannel2; + + @Autowired + @Qualifier("multiMapChannel2") + private MessageChannel multiMapChannel2; + + @Autowired + @Qualifier("replicatedMapChannel2") + private MessageChannel replicatedMapChannel2; + + @Resource + private IMap distMap; + + @Resource + private IMap distBulkMap; + + @Resource + private List distList; + + @Resource + private Set distSet; + + @Resource + private Queue distQueue; + + @Resource + private ITopic topic; + + @Resource + private MultiMap multiMap; + + @Resource + private ReplicatedMap replicatedMap; + + @Autowired + @Qualifier("distMapRequestHandlerAdvice") + private HazelcastTestRequestHandlerAdvice distMapRequestHandlerAdvice; + + @Autowired + @Qualifier("distBulkMapRequestHandlerAdvice") + private HazelcastTestRequestHandlerAdvice distBulkMapRequestHandlerAdvice; + + @Autowired + @Qualifier("distListRequestHandlerAdvice") + private HazelcastTestRequestHandlerAdvice distListRequestHandlerAdvice; + + @Autowired + @Qualifier("distSetRequestHandlerAdvice") + private HazelcastTestRequestHandlerAdvice distSetRequestHandlerAdvice; + + @Autowired + @Qualifier("distQueueRequestHandlerAdvice") + private HazelcastTestRequestHandlerAdvice distQueueRequestHandlerAdvice; + + @Autowired + @Qualifier("topicRequestHandlerAdvice") + private HazelcastTestRequestHandlerAdvice topicRequestHandlerAdvice; + + @Autowired + @Qualifier("multiMapRequestHandlerAdvice") + private HazelcastTestRequestHandlerAdvice multiMapRequestHandlerAdvice; + + @Autowired + @Qualifier("replicatedMapRequestHandlerAdvice") + private HazelcastTestRequestHandlerAdvice replicatedMapRequestHandlerAdvice; + + @Test + public void testWriteToDistributedMap() { + HazelcastOutboundChannelAdapterTestUtils + .testWriteToDistributedMap(this.distMapChannel, this.distMap, + this.distMapRequestHandlerAdvice); + } + + @Test + public void testBulkWriteToDistributedMap() { + HazelcastOutboundChannelAdapterTestUtils + .testBulkWriteToDistributedMap(this.distMapBulkChannel, this.distBulkMap, + this.distBulkMapRequestHandlerAdvice); + } + + @Test + public void testWriteToDistributedList() { + HazelcastOutboundChannelAdapterTestUtils + .testWriteToDistributedList(this.distListChannel, this.distList, + this.distListRequestHandlerAdvice); + } + + @Test + public void testWriteToDistributedSet() { + HazelcastOutboundChannelAdapterTestUtils + .testWriteToDistributedSet(this.distSetChannel, this.distSet, + this.distSetRequestHandlerAdvice); + } + + @Test + public void testWriteToDistributedQueue() { + HazelcastOutboundChannelAdapterTestUtils + .testWriteToDistributedQueue(this.distQueueChannel, this.distQueue, + this.distQueueRequestHandlerAdvice); + } + + @Test + public void testWriteToTopic() { + HazelcastOutboundChannelAdapterTestUtils + .testWriteToTopic(this.topicChannel2, this.topic, this.topicRequestHandlerAdvice); + } + + @Test + public void testWriteToMultiMap() { + HazelcastOutboundChannelAdapterTestUtils + .testWriteToMultiMap(this.multiMapChannel2, this.multiMap, + this.multiMapRequestHandlerAdvice); + } + + @Test + public void testWriteToReplicatedMap() { + HazelcastOutboundChannelAdapterTestUtils + .testWriteToReplicatedMap(this.replicatedMapChannel2, this.replicatedMap, + this.replicatedMapRequestHandlerAdvice); + } + +} diff --git a/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/outbound/util/HazelcastOutboundChannelAdapterTestUtils.java b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/outbound/util/HazelcastOutboundChannelAdapterTestUtils.java new file mode 100644 index 0000000..8e28aae --- /dev/null +++ b/spring-integration-hazelcast/src/test/java/org/springframework/integration/hazelcast/outbound/util/HazelcastOutboundChannelAdapterTestUtils.java @@ -0,0 +1,247 @@ +/* + * Copyright 2016 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.hazelcast.outbound.util; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Queue; +import java.util.Set; +import java.util.SortedSet; +import java.util.TreeMap; +import java.util.TreeSet; +import java.util.concurrent.TimeUnit; + +import org.springframework.integration.hazelcast.HazelcastIntegrationTestUser; +import org.springframework.integration.hazelcast.HazelcastTestRequestHandlerAdvice; +import org.springframework.messaging.MessageChannel; +import org.springframework.messaging.support.GenericMessage; + +import com.hazelcast.core.ITopic; +import com.hazelcast.core.MessageListener; +import com.hazelcast.core.MultiMap; +import com.hazelcast.core.ReplicatedMap; + +/** + * Util Class for Hazelcast Outbound Channel Adapter Test Support + * + * @author Eren Avsarogullari + * + * @since 1.0.0 + */ +@SuppressWarnings({"unchecked", "rawtypes"}) +public class HazelcastOutboundChannelAdapterTestUtils { + + public static final int DATA_COUNT = 100; + + public static final int DEFAULT_AGE = 5; + + public static final String TEST_NAME = "Test_Name"; + + public static final String TEST_SURNAME = "Test_Surname"; + + public static void testWriteToDistributedMap(MessageChannel channel, + Map distributedMap, + HazelcastTestRequestHandlerAdvice requestHandlerAdvice) { + testWriteToMap(channel, distributedMap, requestHandlerAdvice); + } + + private static void testWriteToMap(MessageChannel channel, + Map distributedMap, + HazelcastTestRequestHandlerAdvice requestHandlerAdvice) { + try { + sendMessageToChannel(channel); + assertTrue(requestHandlerAdvice.executeLatch.await(10, TimeUnit.SECONDS)); + verifyMapForPayload(new TreeMap(distributedMap)); + } + catch (InterruptedException e) { + fail("Test has been failed due to " + e.getMessage()); + } + } + + public static void testBulkWriteToDistributedMap(MessageChannel channel, + Map distributedMap, + HazelcastTestRequestHandlerAdvice requestHandlerAdvice) { + try { + Map userMap = + new HashMap<>(DATA_COUNT); + for (int index = 1; index <= DATA_COUNT; index++) { + userMap.put(index, getTestUser(index)); + } + + channel.send(new GenericMessage<>(userMap)); + + assertTrue(requestHandlerAdvice.executeLatch.await(10, TimeUnit.SECONDS)); + verifyMapForPayload(new TreeMap(distributedMap)); + } + catch (InterruptedException e) { + fail("Test has been failed due to " + e.getMessage()); + } + } + + public static void testWriteToMultiMap(MessageChannel channel, + MultiMap multiMap, + HazelcastTestRequestHandlerAdvice requestHandlerAdvice) { + try { + sendMessageToChannel(channel); + assertTrue(requestHandlerAdvice.executeLatch.await(10, TimeUnit.SECONDS)); + verifyMultiMapForPayload(multiMap); + } + catch (InterruptedException e) { + fail("Test has been failed due to " + e.getMessage()); + } + } + + public static void testWriteToReplicatedMap(MessageChannel channel, + ReplicatedMap replicatedMap, + HazelcastTestRequestHandlerAdvice requestHandlerAdvice) { + testWriteToMap(channel, replicatedMap, requestHandlerAdvice); + } + + public static void testWriteToDistributedList(MessageChannel channel, + List distributedList, + HazelcastTestRequestHandlerAdvice requestHandlerAdvice) { + testWriteToDistributedCollection(channel, distributedList, requestHandlerAdvice); + } + + private static void testWriteToDistributedCollection(MessageChannel channel, + Collection distributedList, + HazelcastTestRequestHandlerAdvice requestHandlerAdvice) { + try { + sendMessageToChannel(channel); + assertTrue(requestHandlerAdvice.executeLatch.await(10, TimeUnit.SECONDS)); + verifyCollection(distributedList, DATA_COUNT); + } + catch (InterruptedException e) { + fail("Test has been failed due to " + e.getMessage()); + } + } + + public static void testWriteToDistributedSet(MessageChannel channel, + Set distributedSet, + HazelcastTestRequestHandlerAdvice requestHandlerAdvice) { + try { + sendMessageToChannel(channel); + assertTrue(requestHandlerAdvice.executeLatch.await(10, TimeUnit.SECONDS)); + final List list = new ArrayList(distributedSet); + Collections.sort(list); + verifyCollection(list, DATA_COUNT); + } + catch (InterruptedException e) { + fail("Test has been failed due to " + e.getMessage()); + } + } + + public static void testWriteToDistributedQueue(MessageChannel channel, + Queue distributedQueue, + HazelcastTestRequestHandlerAdvice requestHandlerAdvice) { + testWriteToDistributedCollection(channel, distributedQueue, requestHandlerAdvice); + } + + public static void testWriteToTopic(MessageChannel channel, + ITopic topic, + HazelcastTestRequestHandlerAdvice requestHandlerAdvice) { + try { + topic.addMessageListener(new MessageListener() { + + private int index = 1; + + @Override + public void onMessage(com.hazelcast.core.Message message) { + HazelcastIntegrationTestUser user = + (HazelcastIntegrationTestUser) message.getMessageObject(); + verifyHazelcastIntegrationTestUser(user, index); + index++; + } + }); + sendMessageToChannel(channel); + assertTrue(requestHandlerAdvice.executeLatch.await(10, TimeUnit.SECONDS)); + } + catch (InterruptedException e) { + fail("Test has been failed due to " + e.getMessage()); + } + + } + + public static HazelcastIntegrationTestUser getTestUser(int index) { + return new HazelcastIntegrationTestUser(index, TEST_NAME, TEST_SURNAME, + index + DEFAULT_AGE); + } + + public static void verifyMapForPayload( + final Map map) { + int index = 1; + assertNotNull(map); + assertEquals(true, map.size() == DATA_COUNT); + for (Map.Entry entry : map.entrySet()) { + assertNotNull(entry); + assertEquals(index, entry.getKey().intValue()); + verifyHazelcastIntegrationTestUser(entry.getValue(), index); + index++; + } + } + + public static void verifyCollection( + final Collection coll, final int dataCount) { + int index = 1; + assertNotNull(coll); + assertEquals(true, coll.size() == dataCount); + for (HazelcastIntegrationTestUser user : coll) { + verifyHazelcastIntegrationTestUser(user, index); + index++; + } + } + + public static void verifyHazelcastIntegrationTestUser( + HazelcastIntegrationTestUser user, int index) { + assertNotNull(user); + assertEquals(index, user.getId()); + assertEquals(TEST_NAME, user.getName()); + assertEquals(TEST_SURNAME, user.getSurname()); + assertEquals(index + DEFAULT_AGE, user.getAge()); + } + + private static void sendMessageToChannel(final MessageChannel channel) { + for (int index = 1; index <= DATA_COUNT; index++) { + channel.send(new GenericMessage<>(getTestUser(index))); + } + } + + private static void verifyMultiMapForPayload( + final MultiMap multiMap) { + int index = 1; + assertNotNull(multiMap); + assertEquals(true, multiMap.size() == DATA_COUNT); + SortedSet keys = new TreeSet<>(multiMap.keySet()); + for (Integer key : keys) { + assertNotNull(key); + assertEquals(index, key.intValue()); + HazelcastIntegrationTestUser user = multiMap.get(key).iterator().next(); + verifyHazelcastIntegrationTestUser(user, index); + index++; + } + } + +}