|
| 1 | +/* |
| 2 | + * Copyright 2020 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 | + * https://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.redis.outbound; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | + |
| 21 | +import org.junit.Before; |
| 22 | +import org.junit.Test; |
| 23 | +import org.junit.runner.RunWith; |
| 24 | + |
| 25 | +import org.springframework.beans.factory.annotation.Autowired; |
| 26 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 27 | +import org.springframework.context.annotation.Bean; |
| 28 | +import org.springframework.context.annotation.Configuration; |
| 29 | +import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory; |
| 30 | +import org.springframework.data.redis.connection.stream.ObjectRecord; |
| 31 | +import org.springframework.data.redis.connection.stream.StreamOffset; |
| 32 | +import org.springframework.data.redis.core.ReactiveRedisTemplate; |
| 33 | +import org.springframework.data.redis.serializer.RedisSerializationContext; |
| 34 | +import org.springframework.data.redis.serializer.StringRedisSerializer; |
| 35 | +import org.springframework.integration.channel.DirectChannel; |
| 36 | +import org.springframework.integration.handler.ReactiveMessageHandlerAdapter; |
| 37 | +import org.springframework.integration.redis.rules.RedisAvailable; |
| 38 | +import org.springframework.integration.redis.rules.RedisAvailableRule; |
| 39 | +import org.springframework.integration.redis.rules.RedisAvailableTests; |
| 40 | +import org.springframework.integration.redis.util.Address; |
| 41 | +import org.springframework.integration.redis.util.Person; |
| 42 | +import org.springframework.messaging.Message; |
| 43 | +import org.springframework.messaging.MessageChannel; |
| 44 | +import org.springframework.messaging.support.GenericMessage; |
| 45 | +import org.springframework.test.annotation.DirtiesContext; |
| 46 | +import org.springframework.test.context.junit4.SpringRunner; |
| 47 | + |
| 48 | +/** |
| 49 | + * @author Attoumane Ahamadi |
| 50 | + * @author Artem Bilan |
| 51 | + * |
| 52 | + * @since 5.4 |
| 53 | + */ |
| 54 | +@RunWith(SpringRunner.class) |
| 55 | +@DirtiesContext |
| 56 | +public class ReactiveRedisStreamMessageHandlerTests extends RedisAvailableTests { |
| 57 | + |
| 58 | + private static final String STREAM_KEY = "myStream"; |
| 59 | + |
| 60 | + @Autowired |
| 61 | + @Qualifier("streamChannel") |
| 62 | + private MessageChannel messageChannel; |
| 63 | + |
| 64 | + @Autowired |
| 65 | + private ReactiveRedisConnectionFactory redisConnectionFactory; |
| 66 | + |
| 67 | + @Autowired |
| 68 | + private ReactiveMessageHandlerAdapter handlerAdapter; |
| 69 | + |
| 70 | + @Autowired |
| 71 | + private ReactiveRedisStreamMessageHandler streamMessageHandler; |
| 72 | + |
| 73 | + @Before |
| 74 | + public void deleteStreamKey() { |
| 75 | + ReactiveRedisTemplate<String, String> template = new ReactiveRedisTemplate<>(this.redisConnectionFactory, |
| 76 | + RedisSerializationContext.string()); |
| 77 | + template.delete(STREAM_KEY).block(); |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + @Test |
| 82 | + @RedisAvailable |
| 83 | + public void integrationStreamOutboundTest() { |
| 84 | + String messagePayload = "Hello stream message"; |
| 85 | + |
| 86 | + messageChannel.send(new GenericMessage<>(messagePayload)); |
| 87 | + |
| 88 | + RedisSerializationContext<String, ?> serializationContext = redisSerializationContext(); |
| 89 | + |
| 90 | + ReactiveRedisTemplate<String, ?> template = |
| 91 | + new ReactiveRedisTemplate<>(redisConnectionFactory, serializationContext); |
| 92 | + |
| 93 | + ObjectRecord<String, String> record = |
| 94 | + template.opsForStream() |
| 95 | + .read(String.class, StreamOffset.fromStart(STREAM_KEY)) |
| 96 | + .blockFirst(); |
| 97 | + |
| 98 | + assertThat(record.getStream()).isEqualTo(STREAM_KEY); |
| 99 | + |
| 100 | + assertThat(record.getValue()).isEqualTo(messagePayload); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + @RedisAvailable |
| 105 | + public void explicitSerializationContextWithModelTest() { |
| 106 | + Address address = new Address("Rennes, France"); |
| 107 | + Person person = new Person(address, "Attoumane"); |
| 108 | + |
| 109 | + Message<?> message = new GenericMessage<>(person); |
| 110 | + |
| 111 | + RedisSerializationContext<String, ?> serializationContext = redisSerializationContext(); |
| 112 | + |
| 113 | + streamMessageHandler.setSerializationContext(serializationContext); |
| 114 | + streamMessageHandler.afterPropertiesSet(); |
| 115 | + |
| 116 | + handlerAdapter.handleMessage(message); |
| 117 | + |
| 118 | + ReactiveRedisTemplate<String, ?> template = |
| 119 | + new ReactiveRedisTemplate<>(redisConnectionFactory, serializationContext); |
| 120 | + |
| 121 | + ObjectRecord<String, Person> record = |
| 122 | + template.opsForStream() |
| 123 | + .read(Person.class, StreamOffset.fromStart(STREAM_KEY)) |
| 124 | + .blockFirst(); |
| 125 | + |
| 126 | + assertThat(record.getStream()).isEqualTo(STREAM_KEY); |
| 127 | + assertThat(record.getValue().getName()).isEqualTo("Attoumane"); |
| 128 | + assertThat(record.getValue().getAddress().getAddress()).isEqualTo("Rennes, France"); |
| 129 | + } |
| 130 | + |
| 131 | + |
| 132 | + private RedisSerializationContext<String, ?> redisSerializationContext() { |
| 133 | + return RedisSerializationContext.fromSerializer(StringRedisSerializer.UTF_8); |
| 134 | + } |
| 135 | + |
| 136 | + |
| 137 | + @Configuration |
| 138 | + public static class ReactiveRedisStreamMessageHandlerTestsContext { |
| 139 | + |
| 140 | + @Bean |
| 141 | + public MessageChannel streamChannel(ReactiveMessageHandlerAdapter messageHandlerAdapter) { |
| 142 | + DirectChannel directChannel = new DirectChannel(); |
| 143 | + directChannel.subscribe(messageHandlerAdapter); |
| 144 | + directChannel.setMaxSubscribers(1); |
| 145 | + return directChannel; |
| 146 | + } |
| 147 | + |
| 148 | + |
| 149 | + @Bean |
| 150 | + public ReactiveRedisStreamMessageHandler streamMessageHandler( |
| 151 | + ReactiveRedisConnectionFactory connectionFactory) { |
| 152 | + |
| 153 | + return new ReactiveRedisStreamMessageHandler(connectionFactory, STREAM_KEY); |
| 154 | + } |
| 155 | + |
| 156 | + @Bean |
| 157 | + public ReactiveMessageHandlerAdapter reactiveMessageHandlerAdapter( |
| 158 | + ReactiveRedisStreamMessageHandler streamMessageHandler) { |
| 159 | + |
| 160 | + return new ReactiveMessageHandlerAdapter(streamMessageHandler); |
| 161 | + } |
| 162 | + |
| 163 | + @Bean |
| 164 | + public ReactiveRedisConnectionFactory reactiveRedisConnectionFactory() { |
| 165 | + return RedisAvailableRule.connectionFactory; |
| 166 | + } |
| 167 | + |
| 168 | + } |
| 169 | + |
| 170 | +} |
0 commit comments