2121import java .nio .ByteBuffer ;
2222import java .util .Collections ;
2323import java .util .Map ;
24+ import java .util .stream .Stream ;
2425
2526import org .junit .jupiter .api .Test ;
27+ import org .junit .jupiter .params .ParameterizedTest ;
28+ import org .junit .jupiter .params .provider .Arguments ;
29+ import org .junit .jupiter .params .provider .MethodSource ;
2630
2731import org .springframework .data .redis .connection .stream .ByteRecord ;
2832import org .springframework .data .redis .connection .stream .MapRecord ;
3943 *
4044 * @author Christoph Strobl
4145 * @author Romain Beghi
46+ * @author Seo Bo Gyeong
4247 */
4348class StreamRecordsUnitTests {
4449
@@ -99,9 +104,7 @@ void serializeMapRecordStringAsHashValue() {
99104 ByteRecord target = source .serialize (RedisSerializer .string ());
100105
101106 assertThat (target .getId ()).isEqualTo (RECORD_ID );
102- assertThat (target .getStream ()).isEqualTo (SERIALIZED_STRING_STREAM_KEY );
103- assertThat (target .getValue ().keySet ().iterator ().next ()).isEqualTo (SERIALIZED_STRING_MAP_KEY );
104- assertThat (target .getValue ().values ().iterator ().next ()).isEqualTo (SERIALIZED_STRING_VAL );
107+ assertByteRecord (target );
105108 }
106109
107110 @ Test // DATAREDIS-993
@@ -137,14 +140,6 @@ static class StubValueReturningHashMapper<T, K, V> implements HashMapper<T, K, V
137140 final Map <K , V > to ;
138141 final T from ;
139142
140- public StubValueReturningHashMapper (Map <K , V > to ) {
141- this (to , (T ) new Object ());
142- }
143- 144- public StubValueReturningHashMapper (T from ) {
145- this (Collections .emptyMap (), from );
146- }
147- 148143 StubValueReturningHashMapper (Map <K , V > to , T from ) {
149144 this .to = to ;
150145 this .from = from ;
@@ -165,72 +160,51 @@ static HashMapper<Object, String, String> simpleString(String value) {
165160 }
166161 }
167162
168- @ Test // Stream key auto conversion for ofBytes method
169- void ofBytesWithStringStreamKey () {
163+ @ Test // GH-3204
164+ void ofBytesWithNullStreamKey () {
170165
171166 ByteRecord record = StreamRecords .newRecord ()
172- .in (STRING_STREAM_KEY )
173167 .withId (RECORD_ID )
174168 .ofBytes (Collections .singletonMap (SERIALIZED_STRING_MAP_KEY , SERIALIZED_STRING_VAL ));
175169
176170 assertThat (record .getId ()).isEqualTo (RECORD_ID );
177- assertThat (record .getStream ()).isEqualTo (SERIALIZED_STRING_STREAM_KEY );
178- assertThat (record .getValue ().keySet ().iterator ().next ()).isEqualTo (SERIALIZED_STRING_MAP_KEY );
179- assertThat (record .getValue ().values ().iterator ().next ()).isEqualTo (SERIALIZED_STRING_VAL );
171+ assertThat (record .getStream ()).isNull ();
180172 }
181173
182- @ Test // Stream key auto conversion for ofBytes method with byte array
183- void ofBytesWithByteArrayStreamKey () {
184- 185- ByteRecord record = StreamRecords .newRecord ()
186- .in (SERIALIZED_STRING_STREAM_KEY )
187- .withId (RECORD_ID )
188- .ofBytes (Collections .singletonMap (SERIALIZED_STRING_MAP_KEY , SERIALIZED_STRING_VAL ));
174+ @ Test // GH-3204
175+ void ofBytesWithUnsupportedStreamKeyType () {
189176
190- assertThat (record .getId ()).isEqualTo (RECORD_ID );
191- assertThat (record .getStream ()).isEqualTo (SERIALIZED_STRING_STREAM_KEY );
192- assertThat (record .getValue ().keySet ().iterator ().next ()).isEqualTo (SERIALIZED_STRING_MAP_KEY );
193- assertThat (record .getValue ().values ().iterator ().next ()).isEqualTo (SERIALIZED_STRING_VAL );
177+ assertThatIllegalArgumentException ().isThrownBy (() -> StreamRecords .newRecord ().in (123L ) // Unsupported type
178+ .withId (RECORD_ID ).ofBytes (Collections .singletonMap (SERIALIZED_STRING_MAP_KEY , SERIALIZED_STRING_VAL )))
179+ .withMessageContaining ("Stream key '123' cannot be converted to byte array" );
194180 }
195181
196- @ Test // Stream key auto conversion for ofBytes method with null stream key
197- void ofBytesWithNullStreamKey () {
182+ @ ParameterizedTest // GH-3204
183+ @ MethodSource ("ofBytesInStreamArgs" )
184+ void ofBytes (Object streamKey ) {
198185
199- ByteRecord record = StreamRecords .newRecord ()
200- .withId (RECORD_ID )
186+ ByteRecord record = StreamRecords .newRecord ().in (streamKey ).withId (RECORD_ID )
201187 .ofBytes (Collections .singletonMap (SERIALIZED_STRING_MAP_KEY , SERIALIZED_STRING_VAL ));
202188
203189 assertThat (record .getId ()).isEqualTo (RECORD_ID );
204- assertThat (record .getStream ()).isNull ();
205- assertThat (record .getValue ().keySet ().iterator ().next ()).isEqualTo (SERIALIZED_STRING_MAP_KEY );
206- assertThat (record .getValue ().values ().iterator ().next ()).isEqualTo (SERIALIZED_STRING_VAL );
190+ assertByteRecord (record );
207191 }
208192
209- @ Test // Stream key auto conversion for ofBytes method with ByteBuffer stream key
210- void ofBytesWithByteBufferStreamKey () {
211- 212- ByteBuffer streamKeyBuffer = ByteBuffer .wrap (SERIALIZED_STRING_STREAM_KEY );
213- 214- ByteRecord record = StreamRecords .newRecord ()
215- .in (streamKeyBuffer )
216- .withId (RECORD_ID )
217- .ofBytes (Collections .singletonMap (SERIALIZED_STRING_MAP_KEY , SERIALIZED_STRING_VAL ));
218- 219- assertThat (record .getId ()).isEqualTo (RECORD_ID );
220- assertThat (record .getStream ()).isEqualTo (SERIALIZED_STRING_STREAM_KEY );
221- assertThat (record .getValue ().keySet ().iterator ().next ()).isEqualTo (SERIALIZED_STRING_MAP_KEY );
222- assertThat (record .getValue ().values ().iterator ().next ()).isEqualTo (SERIALIZED_STRING_VAL );
193+ static Stream <Arguments > ofBytesInStreamArgs () {
194+ return Stream .of (Arguments .argumentSet ("ByteBuffer" , ByteBuffer .wrap (SERIALIZED_STRING_STREAM_KEY )), //
195+ Arguments .argumentSet ("byte[]" , new Object [] { SERIALIZED_STRING_STREAM_KEY }), //
196+ Arguments .argumentSet ("String" , STRING_STREAM_KEY ));
223197 }
224198
225- @ Test // Stream key auto conversion for ofBytes method with unsupported type
226- void ofBytesWithUnsupportedStreamKeyType () {
199+ private void assertByteRecord (ByteRecord target ) {
227200
228- assertThatThrownBy (() -> StreamRecords .newRecord ()
229- .in (123L ) // Unsupported type
230- .withId (RECORD_ID )
231- .ofBytes (Collections .singletonMap (SERIALIZED_STRING_MAP_KEY , SERIALIZED_STRING_VAL )))
232- .isInstanceOf (IllegalArgumentException .class )
233- .hasMessageContaining ("Stream key 123 cannot be converted to byte array" );
201+ assertThat (target .getStream ()).isEqualTo (SERIALIZED_STRING_STREAM_KEY );
202+ assertThat (target .getValue ()).hasSize (1 );
203+ 204+ target .getValue ().forEach ((k , v ) -> {
205+ assertThat (k ).isEqualTo (SERIALIZED_STRING_MAP_KEY );
206+ assertThat (v ).isEqualTo (SERIALIZED_STRING_VAL );
207+ });
234208 }
235209
236210}
0 commit comments