Skip to content

Commit

Permalink
RedisChMessageStore: Add JSON serialization test
Browse files Browse the repository at this point in the history
  • Loading branch information
artembilan committed Sep 1, 2022
1 parent 56aaa4a commit ccf41d1
Showing 1 changed file with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,33 @@

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
Expand Down Expand Up @@ -169,4 +181,29 @@ void testPriority() {
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<Message<?>> messages = new ArrayList<>(messageGroup.getMessages());
assertThat(messages.get(0)).isEqualTo(genericMessage);
assertThat(messages.get(0).getHeaders()).containsKeys(MessageHistory.HEADER_NAME);
}

}

0 comments on commit ccf41d1

Please sign in to comment.