Polishing.

Refine assertions usage.

Original pull request: #2985
Closes #2982
This commit is contained in:
Mark Paluch
2024-09-10 10:55:01 +02:00
parent b7f26fa30f
commit 6eac1d1ad0
4 changed files with 89 additions and 78 deletions

View File

@@ -1,3 +1,18 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.connection;
import static org.assertj.core.api.Assertions.*;
@@ -24,7 +39,7 @@ class ReactiveStreamCommandsUnitTests {
PendingRecordsCommand command = PendingRecordsCommand.pending(key, groupName);
assertThatThrownBy(() -> command.range(null, 10L)).isInstanceOf(IllegalArgumentException.class);
assertThatIllegalArgumentException().isThrownBy(() -> command.range(null, 10L));
}
@Test // GH-2982
@@ -36,6 +51,6 @@ class ReactiveStreamCommandsUnitTests {
PendingRecordsCommand command = PendingRecordsCommand.pending(key, groupName);
Range<?> range = Range.closed("0", "10");
assertThatThrownBy(() -> command.range(range, -1L)).isInstanceOf(IllegalArgumentException.class);
assertThatIllegalArgumentException().isThrownBy(() -> command.range(range, -1L));
}
}

View File

@@ -1,3 +1,18 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.connection;
import static org.assertj.core.api.Assertions.*;
@@ -16,14 +31,12 @@ class RedisStreamCommandsUnitTests {
@Test // GH-2982
void xPendingOptionsUnboundedShouldThrowExceptionWhenCountIsNegative() {
assertThatThrownBy(() -> XPendingOptions.unbounded(-1L)).isInstanceOf(IllegalArgumentException.class);
assertThatIllegalArgumentException().isThrownBy(() -> XPendingOptions.unbounded(-1L));
}
@Test // GH-2982
void xPendingOptionsRangeShouldThrowExceptionWhenRangeIsNull() {
assertThatThrownBy(() -> XPendingOptions.range(null, 10L)).isInstanceOf(IllegalArgumentException.class);
assertThatIllegalArgumentException().isThrownBy(() -> XPendingOptions.range(null, 10L));
}
@Test // GH-2982
@@ -31,6 +44,6 @@ class RedisStreamCommandsUnitTests {
Range<?> range = Range.closed("0", "10");
assertThatThrownBy(() -> XPendingOptions.range(range, -1L)).isInstanceOf(IllegalArgumentException.class);
assertThatIllegalArgumentException().isThrownBy(() -> XPendingOptions.range(range, -1L));
}
}

View File

@@ -82,12 +82,6 @@ public class DefaultReactiveStreamOperationsIntegrationTests<K, HK, HV> {
return ReactiveOperationsTestParams.testParams();
}
/**
* @param redisTemplate
* @param keyFactory
* @param valueFactory
* @param label parameterized test label, no further use besides that.
*/
public DefaultReactiveStreamOperationsIntegrationTests(Fixture<K, HV> fixture) {
this.serializer = fixture.getSerializer();
@@ -208,27 +202,25 @@ public class DefaultReactiveStreamOperationsIntegrationTests<K, HK, HV> {
RecordId messageId = streamOperations.add(key, Collections.singletonMap(hashKey, newValue), options).block();
streamOperations.range(key, Range.unbounded()).as(StepVerifier::create)
.consumeNextWith(actual -> {
streamOperations.range(key, Range.unbounded()).as(StepVerifier::create).consumeNextWith(actual -> {
assertThat(actual.getId()).isEqualTo(messageId);
assertThat(actual.getStream()).isEqualTo(key);
assertThat(actual).hasSize(1);
assertThat(actual.getId()).isEqualTo(messageId);
assertThat(actual.getStream()).isEqualTo(key);
assertThat(actual).hasSize(1);
if (!(key instanceof byte[] || value instanceof byte[])) {
assertThat(actual.getValue()).containsEntry(hashKey, newValue);
}
if (!(key instanceof byte[] || value instanceof byte[])) {
assertThat(actual.getValue()).containsEntry(hashKey, newValue);
}
})
.verifyComplete();
}).verifyComplete();
}
@ParameterizedRedisTest // GH-2915
void addMaxLenShouldLimitSimpleMessagesSize() {
assumeTrue(!(serializer instanceof Jackson2JsonRedisSerializer)
&& !(serializer instanceof GenericJackson2JsonRedisSerializer)
&& !(serializer instanceof JdkSerializationRedisSerializer) && !(serializer instanceof OxmSerializer));
&& !(serializer instanceof GenericJackson2JsonRedisSerializer)
&& !(serializer instanceof JdkSerializationRedisSerializer) && !(serializer instanceof OxmSerializer));
K key = keyFactory.instance();
HV value = valueFactory.instance();
@@ -241,31 +233,29 @@ public class DefaultReactiveStreamOperationsIntegrationTests<K, HK, HV> {
RecordId messageId = streamOperations.add(StreamRecords.objectBacked(newValue).withStreamKey(key), options).block();
streamOperations.range((Class<HV>) value.getClass(), key, Range.unbounded()).as(StepVerifier::create)
.consumeNextWith(actual -> {
.consumeNextWith(actual -> {
assertThat(actual.getId()).isEqualTo(messageId);
assertThat(actual.getStream()).isEqualTo(key);
assertThat(actual.getValue()).isEqualTo(newValue);
assertThat(actual.getId()).isEqualTo(messageId);
assertThat(actual.getStream()).isEqualTo(key);
assertThat(actual.getValue()).isEqualTo(newValue);
})
.expectNextCount(0)
.verifyComplete();
}).expectNextCount(0).verifyComplete();
}
@ParameterizedRedisTest // GH-2915
void addMaxLenShouldLimitSimpleMessageWithRawSerializerSize() {
assumeTrue(!(serializer instanceof Jackson2JsonRedisSerializer)
&& !(serializer instanceof GenericJackson2JsonRedisSerializer));
&& !(serializer instanceof GenericJackson2JsonRedisSerializer));
SerializationPair<K> keySerializer = redisTemplate.getSerializationContext().getKeySerializationPair();
RedisSerializationContext<K, String> serializationContext = RedisSerializationContext
.<K, String> newSerializationContext(StringRedisSerializer.UTF_8).key(keySerializer)
.hashValue(SerializationPair.raw()).hashKey(SerializationPair.raw()).build();
.<K, String> newSerializationContext(StringRedisSerializer.UTF_8).key(keySerializer)
.hashValue(SerializationPair.raw()).hashKey(SerializationPair.raw()).build();
ReactiveRedisTemplate<K, String> raw = new ReactiveRedisTemplate<>(redisTemplate.getConnectionFactory(),
serializationContext);
serializationContext);
K key = keyFactory.instance();
Person value = new PersonObjectFactory().instance();
@@ -275,18 +265,17 @@ public class DefaultReactiveStreamOperationsIntegrationTests<K, HK, HV> {
Person newValue = new PersonObjectFactory().instance();
XAddOptions options = XAddOptions.maxlen(1).approximateTrimming(false);
RecordId messageId = raw.opsForStream().add(StreamRecords.objectBacked(newValue).withStreamKey(key), options).block();
RecordId messageId = raw.opsForStream().add(StreamRecords.objectBacked(newValue).withStreamKey(key), options)
.block();
raw.opsForStream().range((Class<HV>) value.getClass(), key, Range.unbounded()).as(StepVerifier::create)
.consumeNextWith(it -> {
.consumeNextWith(it -> {
assertThat(it.getId()).isEqualTo(messageId);
assertThat(it.getStream()).isEqualTo(key);
assertThat(it.getValue()).isEqualTo(newValue);
assertThat(it.getId()).isEqualTo(messageId);
assertThat(it.getStream()).isEqualTo(key);
assertThat(it.getValue()).isEqualTo(newValue);
})
.expectNextCount(0)
.verifyComplete();
}).expectNextCount(0).verifyComplete();
}
@ParameterizedRedisTest // GH-2915
@@ -303,17 +292,13 @@ public class DefaultReactiveStreamOperationsIntegrationTests<K, HK, HV> {
RecordId messageId2 = streamOperations.add(key, Collections.singletonMap(hashKey, value), options).block();
streamOperations.range(key, Range.unbounded()).as(StepVerifier::create)
.consumeNextWith(actual -> {
assertThat(actual.getId()).isEqualTo(messageId1);
assertThat(actual.getStream()).isEqualTo(key);
})
.consumeNextWith(actual -> {
assertThat(actual.getId()).isEqualTo(messageId2);
assertThat(actual.getStream()).isEqualTo(key);
})
.expectNextCount(0)
.verifyComplete();
streamOperations.range(key, Range.unbounded()).as(StepVerifier::create).consumeNextWith(actual -> {
assertThat(actual.getId()).isEqualTo(messageId1);
assertThat(actual.getStream()).isEqualTo(key);
}).consumeNextWith(actual -> {
assertThat(actual.getId()).isEqualTo(messageId2);
assertThat(actual.getStream()).isEqualTo(key);
}).expectNextCount(0).verifyComplete();
}
@ParameterizedRedisTest // GH-2915
@@ -327,13 +312,9 @@ public class DefaultReactiveStreamOperationsIntegrationTests<K, HK, HV> {
streamOperations.add(key, Collections.singletonMap(hashKey, value), options).block();
streamOperations.size(key).as(StepVerifier::create)
.expectNext(0L)
.verifyComplete();
streamOperations.size(key).as(StepVerifier::create).expectNext(0L).verifyComplete();
streamOperations.range(key, Range.unbounded()).as(StepVerifier::create)
.expectNextCount(0L)
.verifyComplete();
streamOperations.range(key, Range.unbounded()).as(StepVerifier::create).expectNextCount(0L).verifyComplete();
}
@ParameterizedRedisTest // GH-2915
@@ -349,13 +330,9 @@ public class DefaultReactiveStreamOperationsIntegrationTests<K, HK, HV> {
streamOperations.add(key, Collections.singletonMap(hashKey, value), options).block();
streamOperations.size(key).as(StepVerifier::create)
.expectNext(2L)
.verifyComplete();
streamOperations.size(key).as(StepVerifier::create).expectNext(2L).verifyComplete();
streamOperations.range(key, Range.unbounded()).as(StepVerifier::create)
.expectNextCount(2L)
.verifyComplete();
streamOperations.range(key, Range.unbounded()).as(StepVerifier::create).expectNextCount(2L).verifyComplete();
}
@ParameterizedRedisTest // DATAREDIS-864
@@ -525,7 +502,6 @@ public class DefaultReactiveStreamOperationsIntegrationTests<K, HK, HV> {
assertThat(pending.get(0).getConsumerName()).isEqualTo("my-consumer");
assertThat(pending.get(0).getTotalDeliveryCount()).isOne();
}).verifyComplete();
}
@ParameterizedRedisTest // GH-2465
@@ -550,6 +526,5 @@ public class DefaultReactiveStreamOperationsIntegrationTests<K, HK, HV> {
assertThat(claimed.getValue()).isEqualTo(content);
assertThat(claimed.getId()).isEqualTo(messageId);
}).verifyComplete();
}
}

View File

@@ -36,7 +36,16 @@ import org.springframework.data.redis.connection.RedisStreamCommands.XAddOptions
import org.springframework.data.redis.connection.jedis.extension.JedisConnectionFactoryExtension;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.extension.LettuceConnectionFactoryExtension;
import org.springframework.data.redis.connection.stream.*;
import org.springframework.data.redis.connection.stream.Consumer;
import org.springframework.data.redis.connection.stream.MapRecord;
import org.springframework.data.redis.connection.stream.ObjectRecord;
import org.springframework.data.redis.connection.stream.PendingMessages;
import org.springframework.data.redis.connection.stream.PendingMessagesSummary;
import org.springframework.data.redis.connection.stream.ReadOffset;
import org.springframework.data.redis.connection.stream.RecordId;
import org.springframework.data.redis.connection.stream.StreamOffset;
import org.springframework.data.redis.connection.stream.StreamReadOptions;
import org.springframework.data.redis.connection.stream.StreamRecords;
import org.springframework.data.redis.test.condition.EnabledOnCommand;
import org.springframework.data.redis.test.condition.EnabledOnRedisDriver;
import org.springframework.data.redis.test.condition.EnabledOnRedisVersion;
@@ -67,7 +76,7 @@ public class DefaultStreamOperationsIntegrationTests<K, HK, HV> {
private final StreamOperations<K, HK, HV> streamOps;
public DefaultStreamOperationsIntegrationTests(RedisTemplate<K, ?> redisTemplate, ObjectFactory<K> keyFactory,
ObjectFactory<?> objectFactory) {
ObjectFactory<?> objectFactory) {
this.redisTemplate = redisTemplate;
this.connectionFactory = redisTemplate.getRequiredConnectionFactory();
@@ -83,7 +92,7 @@ public class DefaultStreamOperationsIntegrationTests<K, HK, HV> {
params.addAll(AbstractOperationsTestParams
.testParams(JedisConnectionFactoryExtension.getConnectionFactory(RedisStanalone.class)));
if(RedisDetector.isClusterAvailable()) {
if (RedisDetector.isClusterAvailable()) {
params.addAll(AbstractOperationsTestParams
.testParams(JedisConnectionFactoryExtension.getConnectionFactory(RedisCluster.class)));
}
@@ -91,7 +100,7 @@ public class DefaultStreamOperationsIntegrationTests<K, HK, HV> {
params.addAll(AbstractOperationsTestParams
.testParams(LettuceConnectionFactoryExtension.getConnectionFactory(RedisStanalone.class)));
if(RedisDetector.isClusterAvailable()) {
if (RedisDetector.isClusterAvailable()) {
params.addAll(AbstractOperationsTestParams
.testParams(LettuceConnectionFactoryExtension.getConnectionFactory(RedisCluster.class)));
}
@@ -456,7 +465,8 @@ public class DefaultStreamOperationsIntegrationTests<K, HK, HV> {
RecordId messageId1 = streamOps.add(StreamRecords.objectBacked(value).withStreamKey(key));
streamOps.add(StreamRecords.objectBacked(value).withStreamKey(key));
List<ObjectRecord<K, HV>> messages = streamOps.read((Class<HV>) value.getClass(), StreamOffset.create(key, ReadOffset.from("0-0")));
List<ObjectRecord<K, HV>> messages = streamOps.read((Class<HV>) value.getClass(),
StreamOffset.create(key, ReadOffset.from("0-0")));
assertThat(messages).hasSize(2);
@@ -535,8 +545,7 @@ public class DefaultStreamOperationsIntegrationTests<K, HK, HV> {
RecordId messageId = streamOps.add(key, Collections.singletonMap(hashKey, value));
streamOps.createGroup(key, ReadOffset.from("0-0"), "my-group");
streamOps.read(Consumer.from("my-group", "my-consumer"),
StreamOffset.create(key, ReadOffset.lastConsumed()));
streamOps.read(Consumer.from("my-group", "my-consumer"), StreamOffset.create(key, ReadOffset.lastConsumed()));
PendingMessagesSummary pending = streamOps.pending(key, "my-group");
@@ -554,8 +563,7 @@ public class DefaultStreamOperationsIntegrationTests<K, HK, HV> {
RecordId messageId = streamOps.add(key, Collections.singletonMap(hashKey, value));
streamOps.createGroup(key, ReadOffset.from("0-0"), "my-group");
streamOps.read(Consumer.from("my-group", "my-consumer"),
StreamOffset.create(key, ReadOffset.lastConsumed()));
streamOps.read(Consumer.from("my-group", "my-consumer"), StreamOffset.create(key, ReadOffset.lastConsumed()));
PendingMessages pending = streamOps.pending(key, "my-group", Range.unbounded(), 10L);