From ccf41d14808da3f7c473b16dcd0061335b5de8b3 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Thu, 1 Sep 2022 11:11:57 -0400 Subject: [PATCH] RedisChMessageStore: Add JSON serialization test --- .../store/RedisChannelMessageStoreTests.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/spring-integration-redis/src/test/java/org/springframework/integration/redis/store/RedisChannelMessageStoreTests.java b/spring-integration-redis/src/test/java/org/springframework/integration/redis/store/RedisChannelMessageStoreTests.java index ca6776c7e4..351974b30d 100644 --- a/spring-integration-redis/src/test/java/org/springframework/integration/redis/store/RedisChannelMessageStoreTests.java +++ b/spring-integration-redis/src/test/java/org/springframework/integration/redis/store/RedisChannelMessageStoreTests.java @@ -18,21 +18,33 @@ package org.springframework.integration.redis.store; import static org.assertj.core.api.Assertions.assertThat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.JacksonObjectWriter; import org.springframework.integration.IntegrationMessageHeaderAccessor; +import org.springframework.integration.channel.NullChannel; +import org.springframework.integration.history.MessageHistory; import org.springframework.integration.redis.RedisContainerTest; +import org.springframework.integration.store.MessageGroup; import org.springframework.integration.support.MessageBuilder; import org.springframework.integration.support.MutableMessageBuilder; +import org.springframework.integration.support.json.JacksonJsonUtils; import org.springframework.messaging.Message; import org.springframework.messaging.PollableChannel; import org.springframework.messaging.support.GenericMessage; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; +import com.fasterxml.jackson.databind.ObjectMapper; + /** * @author Gary Russell * @author Artem Bilan @@ -169,4 +181,29 @@ class RedisChannelMessageStoreTests implements RedisContainerTest { assertThat(this.priorityCms.messageGroupSize("priorityCms:testChannel3")).isZero(); } + @Test + void testJsonSerialization() { + RedisChannelMessageStore store = new RedisChannelMessageStore(RedisContainerTest.connectionFactory()); + ObjectMapper mapper = JacksonJsonUtils.messagingAwareMapper(); + GenericJackson2JsonRedisSerializer serializer = + new GenericJackson2JsonRedisSerializer(mapper, + (mapper1, source, type) -> mapper1.readValue(source, Object.class), + JacksonObjectWriter.create()); + store.setValueSerializer(serializer); + + Message genericMessage = new GenericMessage<>(new Date()); + NullChannel testComponent = new NullChannel(); + testComponent.setBeanName("testChannel"); + genericMessage = MessageHistory.write(genericMessage, testComponent); + + String groupId = "jsonMessagesStore"; + + store.addMessageToGroup(groupId, genericMessage); + MessageGroup messageGroup = store.getMessageGroup(groupId); + assertThat(messageGroup.size()).isEqualTo(1); + List> messages = new ArrayList<>(messageGroup.getMessages()); + assertThat(messages.get(0)).isEqualTo(genericMessage); + assertThat(messages.get(0).getHeaders()).containsKeys(MessageHistory.HEADER_NAME); + } + }