Skip to content

Commit 6eac1d1

Browse files
committed
Polishing.
Refine assertions usage. Original pull request: #2985 Closes #2982
1 parent b7f26fa commit 6eac1d1

File tree

4 files changed

+89
-78
lines changed

4 files changed

+89
-78
lines changed

src/test/java/org/springframework/data/redis/connection/ReactiveStreamCommandsUnitTests.java

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2024 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+
*/
116
package org.springframework.data.redis.connection;
217

318
import static org.assertj.core.api.Assertions.*;
@@ -24,7 +39,7 @@ void pendingRecordsCommandRangeShouldThrowExceptionWhenRangeIsNull() {
2439

2540
PendingRecordsCommand command = PendingRecordsCommand.pending(key, groupName);
2641

27-
assertThatThrownBy(() -> command.range(null, 10L)).isInstanceOf(IllegalArgumentException.class);
42+
assertThatIllegalArgumentException().isThrownBy(() -> command.range(null, 10L));
2843
}
2944

3045
@Test // GH-2982
@@ -36,6 +51,6 @@ void pendingRecordsCommandRangeShouldThrowExceptionWhenCountIsNegative() {
3651
PendingRecordsCommand command = PendingRecordsCommand.pending(key, groupName);
3752
Range<?> range = Range.closed("0", "10");
3853

39-
assertThatThrownBy(() -> command.range(range, -1L)).isInstanceOf(IllegalArgumentException.class);
54+
assertThatIllegalArgumentException().isThrownBy(() -> command.range(range, -1L));
4055
}
4156
}

src/test/java/org/springframework/data/redis/connection/RedisStreamCommandsUnitTests.java

+18-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2024 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+
*/
116
package org.springframework.data.redis.connection;
217

318
import static org.assertj.core.api.Assertions.*;
@@ -16,21 +31,19 @@ class RedisStreamCommandsUnitTests {
1631

1732
@Test // GH-2982
1833
void xPendingOptionsUnboundedShouldThrowExceptionWhenCountIsNegative() {
19-
20-
assertThatThrownBy(() -> XPendingOptions.unbounded(-1L)).isInstanceOf(IllegalArgumentException.class);
34+
assertThatIllegalArgumentException().isThrownBy(() -> XPendingOptions.unbounded(-1L));
2135
}
2236

2337
@Test // GH-2982
2438
void xPendingOptionsRangeShouldThrowExceptionWhenRangeIsNull() {
25-
26-
assertThatThrownBy(() -> XPendingOptions.range(null, 10L)).isInstanceOf(IllegalArgumentException.class);
39+
assertThatIllegalArgumentException().isThrownBy(() -> XPendingOptions.range(null, 10L));
2740
}
2841

2942
@Test // GH-2982
3043
void xPendingOptionsRangeShouldThrowExceptionWhenCountIsNegative() {
3144

3245
Range<?> range = Range.closed("0", "10");
3346

34-
assertThatThrownBy(() -> XPendingOptions.range(range, -1L)).isInstanceOf(IllegalArgumentException.class);
47+
assertThatIllegalArgumentException().isThrownBy(() -> XPendingOptions.range(range, -1L));
3548
}
3649
}

src/test/java/org/springframework/data/redis/core/DefaultReactiveStreamOperationsIntegrationTests.java

+37-62
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,6 @@ public class DefaultReactiveStreamOperationsIntegrationTests<K, HK, HV> {
8282
return ReactiveOperationsTestParams.testParams();
8383
}
8484

85-
/**
86-
* @param redisTemplate
87-
* @param keyFactory
88-
* @param valueFactory
89-
* @param label parameterized test label, no further use besides that.
90-
*/
9185
public DefaultReactiveStreamOperationsIntegrationTests(Fixture<K, HV> fixture) {
9286

9387
this.serializer = fixture.getSerializer();
@@ -208,27 +202,25 @@ void addMaxLenShouldLimitMessagesSize() {
208202

209203
RecordId messageId = streamOperations.add(key, Collections.singletonMap(hashKey, newValue), options).block();
210204

211-
streamOperations.range(key, Range.unbounded()).as(StepVerifier::create)
212-
.consumeNextWith(actual -> {
205+
streamOperations.range(key, Range.unbounded()).as(StepVerifier::create).consumeNextWith(actual -> {
213206

214-
assertThat(actual.getId()).isEqualTo(messageId);
215-
assertThat(actual.getStream()).isEqualTo(key);
216-
assertThat(actual).hasSize(1);
207+
assertThat(actual.getId()).isEqualTo(messageId);
208+
assertThat(actual.getStream()).isEqualTo(key);
209+
assertThat(actual).hasSize(1);
217210

218-
if (!(key instanceof byte[] || value instanceof byte[])) {
219-
assertThat(actual.getValue()).containsEntry(hashKey, newValue);
220-
}
211+
if (!(key instanceof byte[] || value instanceof byte[])) {
212+
assertThat(actual.getValue()).containsEntry(hashKey, newValue);
213+
}
221214

222-
})
223-
.verifyComplete();
215+
}).verifyComplete();
224216
}
225217

226218
@ParameterizedRedisTest // GH-2915
227219
void addMaxLenShouldLimitSimpleMessagesSize() {
228220

229221
assumeTrue(!(serializer instanceof Jackson2JsonRedisSerializer)
230-
&& !(serializer instanceof GenericJackson2JsonRedisSerializer)
231-
&& !(serializer instanceof JdkSerializationRedisSerializer) && !(serializer instanceof OxmSerializer));
222+
&& !(serializer instanceof GenericJackson2JsonRedisSerializer)
223+
&& !(serializer instanceof JdkSerializationRedisSerializer) && !(serializer instanceof OxmSerializer));
232224

233225
K key = keyFactory.instance();
234226
HV value = valueFactory.instance();
@@ -241,31 +233,29 @@ void addMaxLenShouldLimitSimpleMessagesSize() {
241233
RecordId messageId = streamOperations.add(StreamRecords.objectBacked(newValue).withStreamKey(key), options).block();
242234

243235
streamOperations.range((Class<HV>) value.getClass(), key, Range.unbounded()).as(StepVerifier::create)
244-
.consumeNextWith(actual -> {
236+
.consumeNextWith(actual -> {
245237

246-
assertThat(actual.getId()).isEqualTo(messageId);
247-
assertThat(actual.getStream()).isEqualTo(key);
248-
assertThat(actual.getValue()).isEqualTo(newValue);
238+
assertThat(actual.getId()).isEqualTo(messageId);
239+
assertThat(actual.getStream()).isEqualTo(key);
240+
assertThat(actual.getValue()).isEqualTo(newValue);
249241

250-
})
251-
.expectNextCount(0)
252-
.verifyComplete();
242+
}).expectNextCount(0).verifyComplete();
253243
}
254244

255245
@ParameterizedRedisTest // GH-2915
256246
void addMaxLenShouldLimitSimpleMessageWithRawSerializerSize() {
257247

258248
assumeTrue(!(serializer instanceof Jackson2JsonRedisSerializer)
259-
&& !(serializer instanceof GenericJackson2JsonRedisSerializer));
249+
&& !(serializer instanceof GenericJackson2JsonRedisSerializer));
260250

261251
SerializationPair<K> keySerializer = redisTemplate.getSerializationContext().getKeySerializationPair();
262252

263253
RedisSerializationContext<K, String> serializationContext = RedisSerializationContext
264-
.<K, String> newSerializationContext(StringRedisSerializer.UTF_8).key(keySerializer)
265-
.hashValue(SerializationPair.raw()).hashKey(SerializationPair.raw()).build();
254+
.<K, String> newSerializationContext(StringRedisSerializer.UTF_8).key(keySerializer)
255+
.hashValue(SerializationPair.raw()).hashKey(SerializationPair.raw()).build();
266256

267257
ReactiveRedisTemplate<K, String> raw = new ReactiveRedisTemplate<>(redisTemplate.getConnectionFactory(),
268-
serializationContext);
258+
serializationContext);
269259

270260
K key = keyFactory.instance();
271261
Person value = new PersonObjectFactory().instance();
@@ -275,18 +265,17 @@ void addMaxLenShouldLimitSimpleMessageWithRawSerializerSize() {
275265
Person newValue = new PersonObjectFactory().instance();
276266
XAddOptions options = XAddOptions.maxlen(1).approximateTrimming(false);
277267

278-
RecordId messageId = raw.opsForStream().add(StreamRecords.objectBacked(newValue).withStreamKey(key), options).block();
268+
RecordId messageId = raw.opsForStream().add(StreamRecords.objectBacked(newValue).withStreamKey(key), options)
269+
.block();
279270

280271
raw.opsForStream().range((Class<HV>) value.getClass(), key, Range.unbounded()).as(StepVerifier::create)
281-
.consumeNextWith(it -> {
272+
.consumeNextWith(it -> {
282273

283-
assertThat(it.getId()).isEqualTo(messageId);
284-
assertThat(it.getStream()).isEqualTo(key);
285-
assertThat(it.getValue()).isEqualTo(newValue);
274+
assertThat(it.getId()).isEqualTo(messageId);
275+
assertThat(it.getStream()).isEqualTo(key);
276+
assertThat(it.getValue()).isEqualTo(newValue);
286277

287-
})
288-
.expectNextCount(0)
289-
.verifyComplete();
278+
}).expectNextCount(0).verifyComplete();
290279
}
291280

292281
@ParameterizedRedisTest // GH-2915
@@ -303,17 +292,13 @@ void addMinIdShouldEvictLowerIdMessages() {
303292

304293
RecordId messageId2 = streamOperations.add(key, Collections.singletonMap(hashKey, value), options).block();
305294

306-
streamOperations.range(key, Range.unbounded()).as(StepVerifier::create)
307-
.consumeNextWith(actual -> {
308-
assertThat(actual.getId()).isEqualTo(messageId1);
309-
assertThat(actual.getStream()).isEqualTo(key);
310-
})
311-
.consumeNextWith(actual -> {
312-
assertThat(actual.getId()).isEqualTo(messageId2);
313-
assertThat(actual.getStream()).isEqualTo(key);
314-
})
315-
.expectNextCount(0)
316-
.verifyComplete();
295+
streamOperations.range(key, Range.unbounded()).as(StepVerifier::create).consumeNextWith(actual -> {
296+
assertThat(actual.getId()).isEqualTo(messageId1);
297+
assertThat(actual.getStream()).isEqualTo(key);
298+
}).consumeNextWith(actual -> {
299+
assertThat(actual.getId()).isEqualTo(messageId2);
300+
assertThat(actual.getStream()).isEqualTo(key);
301+
}).expectNextCount(0).verifyComplete();
317302
}
318303

319304
@ParameterizedRedisTest // GH-2915
@@ -327,13 +312,9 @@ void addMakeNoStreamShouldNotCreateStreamWhenNoStreamExists() {
327312

328313
streamOperations.add(key, Collections.singletonMap(hashKey, value), options).block();
329314

330-
streamOperations.size(key).as(StepVerifier::create)
331-
.expectNext(0L)
332-
.verifyComplete();
315+
streamOperations.size(key).as(StepVerifier::create).expectNext(0L).verifyComplete();
333316

334-
streamOperations.range(key, Range.unbounded()).as(StepVerifier::create)
335-
.expectNextCount(0L)
336-
.verifyComplete();
317+
streamOperations.range(key, Range.unbounded()).as(StepVerifier::create).expectNextCount(0L).verifyComplete();
337318
}
338319

339320
@ParameterizedRedisTest // GH-2915
@@ -349,13 +330,9 @@ void addMakeNoStreamShouldCreateStreamWhenStreamExists() {
349330

350331
streamOperations.add(key, Collections.singletonMap(hashKey, value), options).block();
351332

352-
streamOperations.size(key).as(StepVerifier::create)
353-
.expectNext(2L)
354-
.verifyComplete();
333+
streamOperations.size(key).as(StepVerifier::create).expectNext(2L).verifyComplete();
355334

356-
streamOperations.range(key, Range.unbounded()).as(StepVerifier::create)
357-
.expectNextCount(2L)
358-
.verifyComplete();
335+
streamOperations.range(key, Range.unbounded()).as(StepVerifier::create).expectNextCount(2L).verifyComplete();
359336
}
360337

361338
@ParameterizedRedisTest // DATAREDIS-864
@@ -525,7 +502,6 @@ void pendingShouldReadMessageDetails() {
525502
assertThat(pending.get(0).getConsumerName()).isEqualTo("my-consumer");
526503
assertThat(pending.get(0).getTotalDeliveryCount()).isOne();
527504
}).verifyComplete();
528-
529505
}
530506

531507
@ParameterizedRedisTest // GH-2465
@@ -550,6 +526,5 @@ void claimShouldReadMessageDetails() {
550526
assertThat(claimed.getValue()).isEqualTo(content);
551527
assertThat(claimed.getId()).isEqualTo(messageId);
552528
}).verifyComplete();
553-
554529
}
555530
}

src/test/java/org/springframework/data/redis/core/DefaultStreamOperationsIntegrationTests.java

+17-9
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,16 @@
3636
import org.springframework.data.redis.connection.jedis.extension.JedisConnectionFactoryExtension;
3737
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
3838
import org.springframework.data.redis.connection.lettuce.extension.LettuceConnectionFactoryExtension;
39-
import org.springframework.data.redis.connection.stream.*;
39+
import org.springframework.data.redis.connection.stream.Consumer;
40+
import org.springframework.data.redis.connection.stream.MapRecord;
41+
import org.springframework.data.redis.connection.stream.ObjectRecord;
42+
import org.springframework.data.redis.connection.stream.PendingMessages;
43+
import org.springframework.data.redis.connection.stream.PendingMessagesSummary;
44+
import org.springframework.data.redis.connection.stream.ReadOffset;
45+
import org.springframework.data.redis.connection.stream.RecordId;
46+
import org.springframework.data.redis.connection.stream.StreamOffset;
47+
import org.springframework.data.redis.connection.stream.StreamReadOptions;
48+
import org.springframework.data.redis.connection.stream.StreamRecords;
4049
import org.springframework.data.redis.test.condition.EnabledOnCommand;
4150
import org.springframework.data.redis.test.condition.EnabledOnRedisDriver;
4251
import org.springframework.data.redis.test.condition.EnabledOnRedisVersion;
@@ -67,7 +76,7 @@ public class DefaultStreamOperationsIntegrationTests<K, HK, HV> {
6776
private final StreamOperations<K, HK, HV> streamOps;
6877

6978
public DefaultStreamOperationsIntegrationTests(RedisTemplate<K, ?> redisTemplate, ObjectFactory<K> keyFactory,
70-
ObjectFactory<?> objectFactory) {
79+
ObjectFactory<?> objectFactory) {
7180

7281
this.redisTemplate = redisTemplate;
7382
this.connectionFactory = redisTemplate.getRequiredConnectionFactory();
@@ -83,15 +92,15 @@ public static Collection<Object[]> testParams() {
8392
params.addAll(AbstractOperationsTestParams
8493
.testParams(JedisConnectionFactoryExtension.getConnectionFactory(RedisStanalone.class)));
8594

86-
if(RedisDetector.isClusterAvailable()) {
95+
if (RedisDetector.isClusterAvailable()) {
8796
params.addAll(AbstractOperationsTestParams
8897
.testParams(JedisConnectionFactoryExtension.getConnectionFactory(RedisCluster.class)));
8998
}
9099

91100
params.addAll(AbstractOperationsTestParams
92101
.testParams(LettuceConnectionFactoryExtension.getConnectionFactory(RedisStanalone.class)));
93102

94-
if(RedisDetector.isClusterAvailable()) {
103+
if (RedisDetector.isClusterAvailable()) {
95104
params.addAll(AbstractOperationsTestParams
96105
.testParams(LettuceConnectionFactoryExtension.getConnectionFactory(RedisCluster.class)));
97106
}
@@ -456,7 +465,8 @@ void readShouldReadSimpleMessage() {
456465
RecordId messageId1 = streamOps.add(StreamRecords.objectBacked(value).withStreamKey(key));
457466
streamOps.add(StreamRecords.objectBacked(value).withStreamKey(key));
458467

459-
List<ObjectRecord<K, HV>> messages = streamOps.read((Class<HV>) value.getClass(), StreamOffset.create(key, ReadOffset.from("0-0")));
468+
List<ObjectRecord<K, HV>> messages = streamOps.read((Class<HV>) value.getClass(),
469+
StreamOffset.create(key, ReadOffset.from("0-0")));
460470

461471
assertThat(messages).hasSize(2);
462472

@@ -535,8 +545,7 @@ void pendingShouldReadMessageSummary() {
535545
RecordId messageId = streamOps.add(key, Collections.singletonMap(hashKey, value));
536546
streamOps.createGroup(key, ReadOffset.from("0-0"), "my-group");
537547

538-
streamOps.read(Consumer.from("my-group", "my-consumer"),
539-
StreamOffset.create(key, ReadOffset.lastConsumed()));
548+
streamOps.read(Consumer.from("my-group", "my-consumer"), StreamOffset.create(key, ReadOffset.lastConsumed()));
540549

541550
PendingMessagesSummary pending = streamOps.pending(key, "my-group");
542551

@@ -554,8 +563,7 @@ void pendingShouldReadMessageDetails() {
554563
RecordId messageId = streamOps.add(key, Collections.singletonMap(hashKey, value));
555564
streamOps.createGroup(key, ReadOffset.from("0-0"), "my-group");
556565

557-
streamOps.read(Consumer.from("my-group", "my-consumer"),
558-
StreamOffset.create(key, ReadOffset.lastConsumed()));
566+
streamOps.read(Consumer.from("my-group", "my-consumer"), StreamOffset.create(key, ReadOffset.lastConsumed()));
559567

560568
PendingMessages pending = streamOps.pending(key, "my-group", Range.unbounded(), 10L);
561569

0 commit comments

Comments
 (0)