21
21
import java .util .Arrays ;
22
22
import java .util .List ;
23
23
24
+ import org .junit .Before ;
24
25
import org .junit .Test ;
25
26
import org .junit .runner .RunWith ;
26
27
27
28
import org .springframework .beans .factory .annotation .Autowired ;
28
29
import org .springframework .beans .factory .annotation .Qualifier ;
30
+ import org .springframework .context .annotation .Bean ;
31
+ import org .springframework .context .annotation .Configuration ;
29
32
import org .springframework .data .redis .connection .ReactiveRedisConnectionFactory ;
30
33
import org .springframework .data .redis .connection .stream .ObjectRecord ;
31
34
import org .springframework .data .redis .connection .stream .StreamOffset ;
32
35
import org .springframework .data .redis .core .ReactiveRedisTemplate ;
33
- import org .springframework .data .redis .serializer .Jackson2JsonRedisSerializer ;
34
36
import org .springframework .data .redis .serializer .RedisSerializationContext ;
35
37
import org .springframework .data .redis .serializer .RedisSerializer ;
36
38
import org .springframework .data .redis .serializer .StringRedisSerializer ;
39
+ import org .springframework .integration .channel .DirectChannel ;
37
40
import org .springframework .integration .handler .ReactiveMessageHandlerAdapter ;
38
41
import org .springframework .integration .redis .rules .RedisAvailable ;
42
+ import org .springframework .integration .redis .rules .RedisAvailableRule ;
39
43
import org .springframework .integration .redis .rules .RedisAvailableTests ;
40
44
import org .springframework .integration .redis .util .Address ;
41
45
import org .springframework .integration .redis .util .Person ;
42
46
import org .springframework .messaging .Message ;
43
47
import org .springframework .messaging .MessageChannel ;
44
48
import org .springframework .messaging .support .GenericMessage ;
45
49
import org .springframework .test .annotation .DirtiesContext ;
46
- import org .springframework .test .context .ContextConfiguration ;
47
50
import org .springframework .test .context .junit4 .SpringRunner ;
48
51
49
52
/**
50
53
* @author Attoumane Ahamadi
51
54
*
52
55
* @since 5.4
53
56
*/
54
- @ ContextConfiguration (classes = ReactiveRedisStreamMessageHandlerTestsContext .class )
55
57
@ RunWith (SpringRunner .class )
56
58
@ DirtiesContext
57
59
@ SuppressWarnings ({"unchecked" , "rawtypes" })
@@ -70,108 +72,129 @@ public class ReactiveRedisStreamMessageHandlerTests extends RedisAvailableTests
70
72
@ Autowired
71
73
private ReactiveRedisStreamMessageHandler streamMessageHandler ;
72
74
75
+ @ Before
76
+ public void deleteStreamKey () {
77
+ ReactiveRedisTemplate <String , String > template = new ReactiveRedisTemplate <>(this .redisConnectionFactory ,
78
+ RedisSerializationContext .string ());
79
+ template .delete (ReactiveRedisStreamMessageHandlerTestsContext .STREAM_KEY ).block ();
80
+ }
81
+
82
+
73
83
@ Test
74
84
@ RedisAvailable
75
85
public void integrationStreamOutboundTest () {
76
86
String messagePayload = "Hello stream message" ;
77
87
78
88
messageChannel .send (new GenericMessage <>(messagePayload ));
79
89
80
- RedisSerializationContext <String , String > serializationContext = redisStringOrJsonSerializationContext ( null );
90
+ RedisSerializationContext <String , String > serializationContext = redisSerializationContext ( );
81
91
82
92
ReactiveRedisTemplate <String , String > template = new ReactiveRedisTemplate (redisConnectionFactory , serializationContext );
83
93
84
94
ObjectRecord <String , String > record = template .opsForStream ().read (String .class , StreamOffset .fromStart (ReactiveRedisStreamMessageHandlerTestsContext .STREAM_KEY )).blockFirst ();
85
95
86
96
assertThat (record .getStream ()).isEqualTo (ReactiveRedisStreamMessageHandlerTestsContext .STREAM_KEY );
87
- assertThat (record .getValue ()).isEqualTo (messagePayload );
88
97
89
- template . delete ( ReactiveRedisStreamMessageHandlerTestsContext . STREAM_KEY ). block ( );
98
+ assertThat ( record . getValue ()). isEqualTo ( messagePayload );
90
99
}
91
100
92
- //TODO Find why the deserialization fail does not work
101
+
93
102
/*@Test
94
103
@RedisAvailable*/
95
- public void explicitJsonSerializationContextTest () {
104
+ public void explicitSerializationContextTest () {
96
105
List <String > messagePayload = Arrays .asList ("Hello" , "stream" , "message" );
97
106
98
- RedisSerializationContext <String , Object > jsonSerializationContext = redisStringOrJsonSerializationContext ( List . class );
107
+ RedisSerializationContext <String , Object > serializationContext = redisSerializationContext ( );
99
108
100
- streamMessageHandler .setSerializationContext (jsonSerializationContext );
109
+ streamMessageHandler .setSerializationContext (serializationContext );
110
+ streamMessageHandler .afterPropertiesSet ();
101
111
102
112
handlerAdapter .handleMessage (new GenericMessage <>(messagePayload ));
103
113
104
114
ReactiveRedisTemplate <String , List > template = new ReactiveRedisTemplate (redisConnectionFactory ,
105
- jsonSerializationContext );
115
+ serializationContext );
106
116
107
- ObjectRecord <String , List > record = template .opsForStream ().read (List .class , StreamOffset .fromStart (ReactiveRedisStreamMessageHandlerTestsContext .STREAM_KEY ))
117
+ ObjectRecord <String , List > record = template .opsForStream ().read (List .class , StreamOffset
118
+ .fromStart (ReactiveRedisStreamMessageHandlerTestsContext .STREAM_KEY ))
108
119
.blockFirst ();
109
120
110
121
assertThat (record .getStream ()).isEqualTo (ReactiveRedisStreamMessageHandlerTestsContext .STREAM_KEY );
111
- assertThat (record .getValue ()).isEqualTo ("[\" Hello\" , \" stream\" , \" message\" ]" );
112
122
113
- template . delete ( ReactiveRedisStreamMessageHandlerTestsContext . STREAM_KEY ). block ( );
123
+ assertThat ( record . getValue ()). isEqualTo ( "[ \" Hello \" , \" stream \" , \" message \" ]" );
114
124
}
115
125
116
- //TODO Find why the deserialization does not work
117
- /* @Test
118
- @RedisAvailable*/
119
- public void explicitJsonSerializationContextWithModelTest () {
126
+
127
+ @ Test
128
+ @ RedisAvailable
129
+ public void explicitSerializationContextWithModelTest () {
120
130
Address address = new Address ().withAddress ("Rennes, France" );
121
131
Person person = new Person (address , "Attoumane" );
122
132
123
133
Message message = new GenericMessage (person );
124
134
125
- RedisSerializationContext <String , Object > jsonSerializationContext = redisStringOrJsonSerializationContext ( Person . class );
135
+ RedisSerializationContext <String , Object > serializationContext = redisSerializationContext ( );
126
136
127
- streamMessageHandler .setSerializationContext (jsonSerializationContext );
137
+ streamMessageHandler .setSerializationContext (serializationContext );
138
+ streamMessageHandler .afterPropertiesSet ();
128
139
129
140
handlerAdapter .handleMessage (message );
130
141
131
- ReactiveRedisTemplate <String , Person > template = new ReactiveRedisTemplate (redisConnectionFactory , jsonSerializationContext );
142
+ ReactiveRedisTemplate <String , Person > template = new ReactiveRedisTemplate (redisConnectionFactory , serializationContext );
132
143
133
144
ObjectRecord <String , Person > record = template .opsForStream ().read (Person .class , StreamOffset .fromStart (ReactiveRedisStreamMessageHandlerTestsContext .STREAM_KEY )).blockFirst ();
134
145
135
146
assertThat (record .getStream ()).isEqualTo (ReactiveRedisStreamMessageHandlerTestsContext .STREAM_KEY );
136
147
assertThat (record .getValue ().getName ()).isEqualTo ("Attoumane" );
137
148
assertThat (record .getValue ().getAddress ().getAddress ()).isEqualTo ("Rennes, France" );
149
+ }
138
150
139
- template .delete (ReactiveRedisStreamMessageHandlerTestsContext .STREAM_KEY ).block ();
151
+
152
+ private RedisSerializationContext redisSerializationContext () {
153
+
154
+ RedisSerializer stringSerializer = StringRedisSerializer .UTF_8 ;
155
+
156
+ RedisSerializationContext .SerializationPair stringSerializerPair = RedisSerializationContext
157
+ .SerializationPair
158
+ .fromSerializer (stringSerializer );
159
+
160
+ return RedisSerializationContext
161
+ .newSerializationContext ()
162
+ .key (stringSerializerPair )
163
+ .value (stringSerializer )
164
+ .hashKey (stringSerializer )
165
+ .hashValue (stringSerializer )
166
+ .build ();
140
167
}
141
168
142
169
143
- private RedisSerializationContext redisStringOrJsonSerializationContext (Class <?> jsonTargetType ) {
170
+ @ Configuration
171
+ public static class ReactiveRedisStreamMessageHandlerTestsContext {
144
172
145
- RedisSerializationContext redisSerializationContext ;
146
- RedisSerializer jsonSerializer = null ;
173
+ public static final String STREAM_KEY = "myStream" ;
147
174
148
- if (jsonTargetType != null ) {
149
- jsonSerializer = new Jackson2JsonRedisSerializer (jsonTargetType );
175
+ @ Bean
176
+ public MessageChannel streamChannel (ReactiveMessageHandlerAdapter messageHandlerAdapter ) {
177
+ DirectChannel directChannel = new DirectChannel ();
178
+ directChannel .subscribe (messageHandlerAdapter );
179
+ directChannel .setMaxSubscribers (1 );
180
+ return directChannel ;
150
181
}
151
- RedisSerializer stringSerializer = StringRedisSerializer .UTF_8 ;
152
- RedisSerializationContext .SerializationPair stringSerializerPair = RedisSerializationContext .SerializationPair
153
- .fromSerializer (stringSerializer );
154
182
155
- if (jsonTargetType == null ) {
156
- redisSerializationContext = RedisSerializationContext
157
- .newSerializationContext ()
158
- .key (stringSerializerPair )
159
- .value (stringSerializer )
160
- .hashKey (stringSerializer )
161
- .hashValue (stringSerializer )
162
- .build ();
183
+
184
+ @ Bean
185
+ public ReactiveRedisStreamMessageHandler streamMessageHandler (ReactiveRedisConnectionFactory connectionFactory ) {
186
+ return new ReactiveRedisStreamMessageHandler (connectionFactory , STREAM_KEY );
163
187
}
164
- else {
165
- redisSerializationContext = RedisSerializationContext
166
- .newSerializationContext ()
167
- .key (jsonSerializer )
168
- .value (jsonSerializer )
169
- .hashKey (jsonSerializer )
170
- .hashValue (jsonSerializer )
171
- .build ();
188
+
189
+ @ Bean
190
+ public ReactiveMessageHandlerAdapter reactiveMessageHandlerAdapter (ReactiveRedisStreamMessageHandler streamMessageHandler ) {
191
+ return new ReactiveMessageHandlerAdapter (streamMessageHandler );
172
192
}
173
193
174
- return redisSerializationContext ;
194
+ @ Bean
195
+ public ReactiveRedisConnectionFactory reactiveRedisConnectionFactory () {
196
+ return RedisAvailableRule .connectionFactory ;
197
+ }
175
198
}
176
199
177
200
}
0 commit comments