|
| 1 | +/* |
| 2 | + * Copyright 2017 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.integration.hazelcast.store; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertNotNull; |
| 21 | +import static org.junit.Assert.assertNotSame; |
| 22 | +import static org.junit.Assert.assertSame; |
| 23 | + |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Properties; |
| 27 | + |
| 28 | +import org.junit.AfterClass; |
| 29 | +import org.junit.Before; |
| 30 | +import org.junit.BeforeClass; |
| 31 | +import org.junit.Test; |
| 32 | + |
| 33 | +import org.springframework.integration.channel.DirectChannel; |
| 34 | +import org.springframework.integration.history.MessageHistory; |
| 35 | +import org.springframework.integration.store.MessageGroup; |
| 36 | +import org.springframework.integration.support.MessageBuilder; |
| 37 | +import org.springframework.integration.test.util.TestUtils; |
| 38 | +import org.springframework.messaging.Message; |
| 39 | +import org.springframework.messaging.support.GenericMessage; |
| 40 | + |
| 41 | +import com.hazelcast.core.Hazelcast; |
| 42 | +import com.hazelcast.core.HazelcastInstance; |
| 43 | +import com.hazelcast.core.IMap; |
| 44 | + |
| 45 | +/** |
| 46 | + * @author Vinicius Carvalho |
| 47 | + * @author Artem Bilan |
| 48 | + */ |
| 49 | +public class HazelcastMessageStoreTests { |
| 50 | + |
| 51 | + private static HazelcastMessageStore store; |
| 52 | + |
| 53 | + private static HazelcastInstance instance; |
| 54 | + |
| 55 | + private static IMap<Object, Object> map; |
| 56 | + |
| 57 | + @BeforeClass |
| 58 | + public static void init() throws Exception { |
| 59 | + instance = Hazelcast.newHazelcastInstance(); |
| 60 | + map = instance.getMap("customTestsMessageStore"); |
| 61 | + store = new HazelcastMessageStore(map); |
| 62 | + } |
| 63 | + |
| 64 | + @AfterClass |
| 65 | + public static void destroy() throws Exception { |
| 66 | + instance.shutdown(); |
| 67 | + } |
| 68 | + |
| 69 | + @Before |
| 70 | + public void clean() throws Exception { |
| 71 | + map.clear(); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testWithMessageHistory() throws Exception { |
| 76 | + |
| 77 | + Message<?> message = new GenericMessage<>("Hello"); |
| 78 | + DirectChannel fooChannel = new DirectChannel(); |
| 79 | + fooChannel.setBeanName("fooChannel"); |
| 80 | + DirectChannel barChannel = new DirectChannel(); |
| 81 | + barChannel.setBeanName("barChannel"); |
| 82 | + |
| 83 | + message = MessageHistory.write(message, fooChannel); |
| 84 | + message = MessageHistory.write(message, barChannel); |
| 85 | + store.addMessage(message); |
| 86 | + message = store.getMessage(message.getHeaders().getId()); |
| 87 | + MessageHistory messageHistory = MessageHistory.read(message); |
| 88 | + assertNotNull(messageHistory); |
| 89 | + assertEquals(2, messageHistory.size()); |
| 90 | + Properties fooChannelHistory = messageHistory.get(0); |
| 91 | + assertEquals("fooChannel", fooChannelHistory.get("name")); |
| 92 | + assertEquals("channel", fooChannelHistory.get("type")); |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void testAddAndRemoveMessagesFromMessageGroup() throws Exception { |
| 98 | + String groupId = "X"; |
| 99 | + List<Message<?>> messages = new ArrayList<>(); |
| 100 | + for (int i = 0; i < 25; i++) { |
| 101 | + Message<String> message = MessageBuilder.withPayload("foo").setCorrelationId(groupId).build(); |
| 102 | + store.addMessagesToGroup(groupId, message); |
| 103 | + messages.add(message); |
| 104 | + } |
| 105 | + MessageGroup group = store.getMessageGroup(groupId); |
| 106 | + assertEquals(25, group.size()); |
| 107 | + store.removeMessagesFromGroup(groupId, messages); |
| 108 | + group = store.getMessageGroup(groupId); |
| 109 | + assertEquals(0, group.size()); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void addAndGetMessage() throws Exception { |
| 114 | + |
| 115 | + Message<?> message = MessageBuilder.withPayload("test").build(); |
| 116 | + store.addMessage(message); |
| 117 | + Message<?> retrieved = store.getMessage(message.getHeaders().getId()); |
| 118 | + assertEquals(message, retrieved); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void customMap() throws Exception { |
| 123 | + assertSame(map, TestUtils.getPropertyValue(store, "map")); |
| 124 | + HazelcastMessageStore store2 = new HazelcastMessageStore(instance); |
| 125 | + assertNotSame(map, TestUtils.getPropertyValue(store2, "map")); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void messageStoreSize() throws Exception { |
| 130 | + Message<?> message1 = MessageBuilder.withPayload("test").build(); |
| 131 | + Message<?> message2 = MessageBuilder.withPayload("test").build(); |
| 132 | + store.addMessage(message1); |
| 133 | + store.addMessage(message2); |
| 134 | + long size = store.getMessageCount(); |
| 135 | + assertEquals(2, size); |
| 136 | + } |
| 137 | + |
| 138 | +} |
0 commit comments