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
+
1
17
package org .springframework .integration .redis .outbound ;
2
18
19
+ import static org .assertj .core .api .Assertions .assertThat ;
20
+ import static org .assertj .core .api .Assertions .assertThatIllegalArgumentException ;
21
+
22
+ import java .lang .reflect .Method ;
23
+ import java .util .Arrays ;
24
+ import java .util .List ;
25
+
3
26
import org .junit .Test ;
4
27
import org .junit .runner .RunWith ;
5
28
6
- import org .springframework .data .redis .connection .lettuce .LettuceConnectionFactory ;
29
+ import org .springframework .beans .factory .annotation .Autowired ;
30
+ import org .springframework .beans .factory .annotation .Qualifier ;
31
+ import org .springframework .data .redis .connection .ReactiveRedisConnectionFactory ;
32
+ import org .springframework .data .redis .connection .stream .ObjectRecord ;
33
+ import org .springframework .data .redis .connection .stream .StreamOffset ;
34
+ import org .springframework .data .redis .core .ReactiveRedisTemplate ;
35
+ import org .springframework .data .redis .serializer .Jackson2JsonRedisSerializer ;
36
+ import org .springframework .data .redis .serializer .RedisSerializationContext ;
37
+ import org .springframework .data .redis .serializer .RedisSerializer ;
38
+ import org .springframework .data .redis .serializer .StringRedisSerializer ;
39
+ import org .springframework .integration .handler .ReactiveMessageHandlerAdapter ;
7
40
import org .springframework .integration .redis .rules .RedisAvailable ;
8
41
import org .springframework .integration .redis .rules .RedisAvailableTests ;
42
+ import org .springframework .integration .redis .store .RedisMessageStoreTests ;
43
+ import org .springframework .messaging .Message ;
44
+ import org .springframework .messaging .MessageChannel ;
45
+ import org .springframework .messaging .support .GenericMessage ;
9
46
import org .springframework .test .annotation .DirtiesContext ;
47
+ import org .springframework .test .context .ContextConfiguration ;
10
48
import org .springframework .test .context .junit4 .SpringRunner ;
11
49
12
- import static org .assertj .core .api .Assertions .assertThatIllegalArgumentException ;
13
-
14
50
/**
15
- * @author Attoumane AHAMADI
51
+ * @author Attoumane Ahamadi
52
+ *
53
+ * @since 5.4
16
54
*/
55
+ @ ContextConfiguration
17
56
@ RunWith (SpringRunner .class )
18
57
@ DirtiesContext
58
+ @ SuppressWarnings ({"unchecked" , "rawtypes" })
19
59
public class ReactiveRedisStreamMessageHandlerTests extends RedisAvailableTests {
20
60
61
+ @ Autowired
62
+ @ Qualifier ("forRedisStreamChannel" )
63
+ private MessageChannel messageChannel ;
64
+
65
+ @ Autowired
66
+ private ReactiveRedisConnectionFactory redisConnectionFactory ;
67
+
68
+ @ Autowired
69
+ private ReactiveMessageHandlerAdapter handlerAdapter ;
70
+
21
71
@ Test
22
72
@ RedisAvailable
23
73
public void emptyStreamKeyTest () {
@@ -32,4 +82,142 @@ public void nullConnectionFactoryTest() {
32
82
.isThrownBy (() -> new ReactiveRedisStreamMessageHandler ("stream" , null ));
33
83
}
34
84
85
+
86
+ @ Test
87
+ @ RedisAvailable
88
+ public void simpleStringInsertionTest () {
89
+ String streamKey = "myStream" ;
90
+ String messagePayload = "Bonjour à tous les confinés" ;
91
+
92
+ handlerAdapter .handleMessage (new GenericMessage <>(messagePayload ));
93
+
94
+ RedisSerializationContext <String , String > serializationContext = redisStringOrJsonSerializationContext (true , null );
95
+
96
+ ReactiveRedisTemplate <String , String > template = new ReactiveRedisTemplate (redisConnectionFactory , serializationContext );
97
+
98
+ ObjectRecord <String , String > record = template .opsForStream ().read (String .class , StreamOffset .fromStart (streamKey )).blockFirst ();
99
+ assertThat (record .getStream ()).isEqualTo (streamKey );
100
+ assertThat (record .getValue ()).isEqualTo (messagePayload );
101
+ template .delete (streamKey ).block ();
102
+ }
103
+
104
+ @ Test
105
+ @ RedisAvailable
106
+ public void integrationStreamOutboundTest () {
107
+ String streamKey = "myStream" ;
108
+ String messagePayload = "Bonjour à tous les confinés" ;
109
+
110
+ messageChannel .send (new GenericMessage <>(messagePayload ));
111
+
112
+ RedisSerializationContext <String , String > serializationContext = redisStringOrJsonSerializationContext (true , null );
113
+
114
+ ReactiveRedisTemplate <String , String > template = new ReactiveRedisTemplate (redisConnectionFactory , serializationContext );
115
+
116
+ ObjectRecord <String , String > record = template .opsForStream ().read (String .class , StreamOffset .fromStart (streamKey )).blockFirst ();
117
+ assertThat (record .getStream ()).isEqualTo (streamKey );
118
+ assertThat (record .getValue ()).isEqualTo (messagePayload );
119
+ template .delete (streamKey ).block ();
120
+ }
121
+
122
+ //TODO Find why the deserialization fail does not work
123
+ /*@Test
124
+ @RedisAvailable*/
125
+ public void explicitJsonSerializationContextTest () {
126
+ String streamKey = "myStream" ;
127
+ List <String > messagePayload = Arrays .asList ("Bonjour" , "à" , "tous" , "les" , "confinés" );
128
+
129
+ RedisSerializationContext <String , Object > jsonSerializationContext = redisStringOrJsonSerializationContext (false , List .class );
130
+
131
+ ReactiveRedisStreamMessageHandler streamMessageHandler = new ReactiveRedisStreamMessageHandler (streamKey , redisConnectionFactory );
132
+ streamMessageHandler .setSerializationContext (jsonSerializationContext );
133
+ //initializes reactiveRedisStreamOperations
134
+ invokeOnInitMethod (streamMessageHandler );
135
+
136
+ ReactiveMessageHandlerAdapter handlerAdapter = new ReactiveMessageHandlerAdapter (streamMessageHandler );
137
+ handlerAdapter .handleMessage (new GenericMessage <>(messagePayload ));
138
+
139
+ ReactiveRedisTemplate <String , String > template = new ReactiveRedisTemplate (redisConnectionFactory ,
140
+ jsonSerializationContext );
141
+ ObjectRecord <String , List > record = template .opsForStream ().read (List .class , StreamOffset .fromStart (streamKey ))
142
+ .blockFirst ();
143
+
144
+ assertThat (record .getStream ()).isEqualTo (streamKey );
145
+ assertThat (record .getValue ()).isEqualTo ("[\" Bonjour\" , \" à\" , \" tous\" , \" les\" , \" confinés\" ]" );
146
+ template .delete (streamKey ).block ();
147
+ }
148
+
149
+ //TODO Find why the deserialization does not work
150
+ /*@Test
151
+ @RedisAvailable*/
152
+ public void explicitJsonSerializationContextWithModelTest () {
153
+ String streamKey = "myStream" ;
154
+
155
+ RedisMessageStoreTests .Address address = new RedisMessageStoreTests .Address ().withAddress ("Rennes, France" );
156
+ RedisMessageStoreTests .Person person = new RedisMessageStoreTests .Person (address , "Attoumane" );
157
+
158
+ Message message = new GenericMessage (person );
159
+
160
+ RedisSerializationContext <String , Object > jsonSerializationContext = redisStringOrJsonSerializationContext (false , RedisMessageStoreTests .Person .class );
161
+
162
+ ReactiveRedisStreamMessageHandler streamMessageHandler = new ReactiveRedisStreamMessageHandler (streamKey , redisConnectionFactory );
163
+ streamMessageHandler .setSerializationContext (jsonSerializationContext );
164
+ //initializes reactiveRedisStreamOperations
165
+ invokeOnInitMethod (streamMessageHandler );
166
+
167
+ ReactiveMessageHandlerAdapter handlerAdapter = new ReactiveMessageHandlerAdapter (streamMessageHandler );
168
+ handlerAdapter .handleMessage (message );
169
+
170
+ ReactiveRedisTemplate <String , String > template = new ReactiveRedisTemplate (redisConnectionFactory , jsonSerializationContext );
171
+ ObjectRecord <String , RedisMessageStoreTests .Person > record = template .opsForStream ().read (RedisMessageStoreTests .Person .class , StreamOffset .fromStart (streamKey )).blockFirst ();
172
+ assertThat (record .getStream ()).isEqualTo (streamKey );
173
+ assertThat (record .getValue ().getName ()).isEqualTo ("Attoumane" );
174
+ assertThat (record .getValue ().getAddress ().getAddress ()).isEqualTo ("Rennes, France" );
175
+ template .delete (streamKey ).block ();
176
+ }
177
+
178
+
179
+ private RedisSerializationContext redisStringOrJsonSerializationContext (boolean string , Class jsonTargetType ) {
180
+
181
+ RedisSerializationContext redisSerializationContext ;
182
+ RedisSerializer jsonSerializer = null ;
183
+
184
+ if (jsonTargetType != null ) {
185
+ jsonSerializer = new Jackson2JsonRedisSerializer (jsonTargetType );
186
+ }
187
+ RedisSerializer stringSerializer = StringRedisSerializer .UTF_8 ;
188
+ RedisSerializationContext .SerializationPair stringSerializerPair = RedisSerializationContext .SerializationPair
189
+ .fromSerializer (stringSerializer );
190
+
191
+ if (string ) {
192
+ redisSerializationContext = RedisSerializationContext
193
+ .newSerializationContext ()
194
+ .key (stringSerializerPair )
195
+ .value (stringSerializer )
196
+ .hashKey (stringSerializer )
197
+ .hashValue (stringSerializer )
198
+ .build ();
199
+ }
200
+ else {
201
+ redisSerializationContext = RedisSerializationContext
202
+ .newSerializationContext ()
203
+ .key (stringSerializerPair )
204
+ .value (jsonSerializer )
205
+ .hashKey (jsonSerializer )
206
+ .hashValue (jsonSerializer )
207
+ .build ();
208
+ }
209
+
210
+ return redisSerializationContext ;
211
+ }
212
+
213
+ private void invokeOnInitMethod (ReactiveRedisStreamMessageHandler streamMessageHandler ) {
214
+ try {
215
+ Method onInit = ReactiveRedisStreamMessageHandler .class .getDeclaredMethod ("onInit" );
216
+ onInit .setAccessible (true );
217
+ onInit .invoke (streamMessageHandler );
218
+ }
219
+ catch (Exception e ) {
220
+ e .printStackTrace ();
221
+ }
222
+ }
35
223
}
0 commit comments