Skip to content

Commit ccf41d1

Browse files
committed
RedisChMessageStore: Add JSON serialization test
1 parent 56aaa4a commit ccf41d1

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

spring-integration-redis/src/test/java/org/springframework/integration/redis/store/RedisChannelMessageStoreTests.java

+37
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,33 @@
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
2020

21+
import java.util.ArrayList;
22+
import java.util.Date;
23+
import java.util.List;
24+
2125
import org.junit.jupiter.api.AfterEach;
2226
import org.junit.jupiter.api.BeforeEach;
2327
import org.junit.jupiter.api.Test;
2428

2529
import org.springframework.beans.factory.annotation.Autowired;
30+
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
31+
import org.springframework.data.redis.serializer.JacksonObjectWriter;
2632
import org.springframework.integration.IntegrationMessageHeaderAccessor;
33+
import org.springframework.integration.channel.NullChannel;
34+
import org.springframework.integration.history.MessageHistory;
2735
import org.springframework.integration.redis.RedisContainerTest;
36+
import org.springframework.integration.store.MessageGroup;
2837
import org.springframework.integration.support.MessageBuilder;
2938
import org.springframework.integration.support.MutableMessageBuilder;
39+
import org.springframework.integration.support.json.JacksonJsonUtils;
3040
import org.springframework.messaging.Message;
3141
import org.springframework.messaging.PollableChannel;
3242
import org.springframework.messaging.support.GenericMessage;
3343
import org.springframework.test.annotation.DirtiesContext;
3444
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
3545

46+
import com.fasterxml.jackson.databind.ObjectMapper;
47+
3648
/**
3749
* @author Gary Russell
3850
* @author Artem Bilan
@@ -169,4 +181,29 @@ void testPriority() {
169181
assertThat(this.priorityCms.messageGroupSize("priorityCms:testChannel3")).isZero();
170182
}
171183

184+
@Test
185+
void testJsonSerialization() {
186+
RedisChannelMessageStore store = new RedisChannelMessageStore(RedisContainerTest.connectionFactory());
187+
ObjectMapper mapper = JacksonJsonUtils.messagingAwareMapper();
188+
GenericJackson2JsonRedisSerializer serializer =
189+
new GenericJackson2JsonRedisSerializer(mapper,
190+
(mapper1, source, type) -> mapper1.readValue(source, Object.class),
191+
JacksonObjectWriter.create());
192+
store.setValueSerializer(serializer);
193+
194+
Message<?> genericMessage = new GenericMessage<>(new Date());
195+
NullChannel testComponent = new NullChannel();
196+
testComponent.setBeanName("testChannel");
197+
genericMessage = MessageHistory.write(genericMessage, testComponent);
198+
199+
String groupId = "jsonMessagesStore";
200+
201+
store.addMessageToGroup(groupId, genericMessage);
202+
MessageGroup messageGroup = store.getMessageGroup(groupId);
203+
assertThat(messageGroup.size()).isEqualTo(1);
204+
List<Message<?>> messages = new ArrayList<>(messageGroup.getMessages());
205+
assertThat(messages.get(0)).isEqualTo(genericMessage);
206+
assertThat(messages.get(0).getHeaders()).containsKeys(MessageHistory.HEADER_NAME);
207+
}
208+
172209
}

0 commit comments

Comments
 (0)