Skip to content

Commit e8a59b0

Browse files
Add validation tests
1 parent 4914182 commit e8a59b0

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/kafka/KafkaAutoConfiguration.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,10 @@ private static void setBackOffPolicy(RetryTopicConfigurationBuilder builder, Top
169169
}
170170

171171
private static void validateRetryTopicInput(Topic retryTopic) {
172-
assertProperty("attempts", retryTopic.getAttempts(), 1);
173-
assertProperty("delay", retryTopic.getDelayMillis(), 0);
174-
assertProperty("multiplier", retryTopic.getMultiplier(), 0);
175-
assertProperty("maxDelayMillis", retryTopic.getMaxDelayMillis(), 0);
172+
assertProperty("attempts", retryTopic.getAttempts(), 1);
173+
assertProperty("delay", retryTopic.getDelayMillis(), 0);
174+
assertProperty("multiplier", retryTopic.getMultiplier(), 0);
175+
assertProperty("maxDelayMillis", retryTopic.getMaxDelayMillis(), 0);
176176
}
177177

178178
private static void assertProperty(String propertyName, Number providedValue, int minValue) {

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/kafka/KafkaAutoConfigurationTests.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,51 @@ void retryTopicConfigurationWithNoBackOff() {
369369
.extracting(DestinationTopic.Properties::delay).containsExactly(0L, 0L, 0L));
370370
}
371371

372+
@Test
373+
void retryTopicConfigurationWithNegativeDelay() {
374+
this.contextRunner.withPropertyValues("spring.application.name=my-test-app",
375+
"spring.kafka.bootstrap-servers=localhost:9092,localhost:9093", "spring.kafka.retry.topic.enabled=true",
376+
"spring.kafka.retry.topic.attempts=4", "spring.kafka.retry.topic.delay=-1")
377+
.run((context) -> assertThat(context.getStartupFailure()).getRootCause()
378+
.isInstanceOf(IllegalArgumentException.class).message()
379+
.isEqualTo("Property spring.kafka.retry.topic.delay"
380+
+ " should be greater than or equal to 0. Provided value was -1."));
381+
}
382+
383+
@Test
384+
void retryTopicConfigurationWithNegativeMultiplier() {
385+
this.contextRunner.withPropertyValues("spring.application.name=my-test-app",
386+
"spring.kafka.bootstrap-servers=localhost:9092,localhost:9093", "spring.kafka.retry.topic.enabled=true",
387+
"spring.kafka.retry.topic.attempts=4", "spring.kafka.retry.topic.multiplier=-1")
388+
.run((context) -> assertThat(context.getStartupFailure()).getRootCause()
389+
.isInstanceOf(IllegalArgumentException.class).message()
390+
.isEqualTo("Property spring.kafka.retry.topic.multiplier"
391+
+ " should be greater than or equal to 0. Provided value was -1.0."));
392+
}
393+
394+
@Test
395+
void retryTopicConfigurationWithNegativeMaxDelay() {
396+
this.contextRunner.withPropertyValues("spring.application.name=my-test-app",
397+
"spring.kafka.bootstrap-servers=localhost:9092,localhost:9093", "spring.kafka.retry.topic.enabled=true",
398+
"spring.kafka.retry.topic.attempts=4", "spring.kafka.retry.topic.maxDelay=-1")
399+
.run((context) -> assertThat(context.getStartupFailure()).getRootCause()
400+
.isInstanceOf(IllegalArgumentException.class).message()
401+
.isEqualTo("Property spring.kafka.retry.topic.maxDelay"
402+
+ " should be greater than or equal to 0. Provided value was -1."));
403+
}
404+
405+
@Test
406+
void retryTopicConfigurationWithZeroAttempts() {
407+
this.contextRunner
408+
.withPropertyValues("spring.application.name=my-test-app",
409+
"spring.kafka.bootstrap-servers=localhost:9092,localhost:9093",
410+
"spring.kafka.retry.topic.enabled=true", "spring.kafka.retry.topic.attempts=0")
411+
.run((context) -> assertThat(context.getStartupFailure()).getRootCause()
412+
.isInstanceOf(IllegalArgumentException.class).message()
413+
.isEqualTo("Property spring.kafka.retry.topic.attempts"
414+
+ " should be greater than or equal to 1. Provided value was 0."));
415+
}
416+
372417
@SuppressWarnings("unchecked")
373418
@Test
374419
void streamsWithSeveralStreamsBuilderFactoryBeans() {

0 commit comments

Comments
 (0)