This is an automated email from the ASF dual-hosted git repository. btellier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
The following commit(s) were added to refs/heads/master by this push: new 7c95b0c086 JAMES-1717 Notification Registry - handle case Ints.checkedCast throws out of range when expireDate too far (#2051) 7c95b0c086 is described below commit 7c95b0c086f656d1461e90e5b7045db5915149e5 Author: vttran <vtt...@linagora.com> AuthorDate: Thu Feb 29 00:30:00 2024 +0700 JAMES-1717 Notification Registry - handle case Ints.checkedCast throws out of range when expireDate too far (#2051) --- .../vacation/cassandra/CassandraNotificationRegistry.java | 12 +++++++++++- .../cassandra/CassandraNotificationRegistryTest.java | 15 +++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/server/data/data-cassandra/src/main/java/org/apache/james/vacation/cassandra/CassandraNotificationRegistry.java b/server/data/data-cassandra/src/main/java/org/apache/james/vacation/cassandra/CassandraNotificationRegistry.java index 5471061322..5ddc9d06cc 100644 --- a/server/data/data-cassandra/src/main/java/org/apache/james/vacation/cassandra/CassandraNotificationRegistry.java +++ b/server/data/data-cassandra/src/main/java/org/apache/james/vacation/cassandra/CassandraNotificationRegistry.java @@ -51,7 +51,7 @@ public class CassandraNotificationRegistry implements NotificationRegistry { @Override public Mono<Void> register(AccountId accountId, RecipientId recipientId, Optional<ZonedDateTime> expiryDate) { - Optional<Integer> waitDelay = expiryDate.map(expiry -> Ints.checkedCast(zonedDateTimeProvider.get().until(expiry, ChronoUnit.SECONDS))); + Optional<Integer> waitDelay = evaluateWaitDelay(expiryDate); if (isValid(waitDelay)) { return cassandraNotificationRegistryDAO.register(accountId, recipientId, waitDelay); } else { @@ -60,6 +60,16 @@ public class CassandraNotificationRegistry implements NotificationRegistry { } } + private Optional<Integer> evaluateWaitDelay(Optional<ZonedDateTime> expiryDate) { + return expiryDate.map(expiry -> zonedDateTimeProvider.get().until(expiry, ChronoUnit.SECONDS)) + .flatMap(longValue -> { + if (longValue >= Integer.MAX_VALUE || longValue <= Integer.MIN_VALUE) { + return Optional.empty(); + } + return Optional.of(Ints.checkedCast(longValue)); + }); + } + @Override public Mono<Boolean> isRegistered(AccountId accountId, RecipientId recipientId) { return cassandraNotificationRegistryDAO.isRegistered(accountId, recipientId); diff --git a/server/data/data-cassandra/src/test/java/org/apache/james/vacation/cassandra/CassandraNotificationRegistryTest.java b/server/data/data-cassandra/src/test/java/org/apache/james/vacation/cassandra/CassandraNotificationRegistryTest.java index 57c09b7bf6..11f4435bf8 100644 --- a/server/data/data-cassandra/src/test/java/org/apache/james/vacation/cassandra/CassandraNotificationRegistryTest.java +++ b/server/data/data-cassandra/src/test/java/org/apache/james/vacation/cassandra/CassandraNotificationRegistryTest.java @@ -19,6 +19,11 @@ package org.apache.james.vacation.cassandra; +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.mockito.Mockito.when; + +import java.util.Optional; + import org.apache.james.backends.cassandra.CassandraCluster; import org.apache.james.backends.cassandra.CassandraClusterExtension; import org.apache.james.core.MailAddress; @@ -26,6 +31,7 @@ import org.apache.james.vacation.api.NotificationRegistry; import org.apache.james.vacation.api.NotificationRegistryContract; import org.apache.james.vacation.api.RecipientId; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; class CassandraNotificationRegistryTest implements NotificationRegistryContract { @@ -49,4 +55,13 @@ class CassandraNotificationRegistryTest implements NotificationRegistryContract public RecipientId recipientId() { return recipientId; } + + @Test + void registerShouldNotFailedWhenExpiredIsTooFar() { + when(zonedDateTimeProvider.get()).thenReturn(ZONED_DATE_TIME); + + assertThatCode(() -> notificationRegistry().register(ACCOUNT_ID, recipientId(), Optional.of(ZONED_DATE_TIME_PLUS_8_SECONDS)).block()) + .doesNotThrowAnyException(); + } + } --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@james.apache.org For additional commands, e-mail: notifications-h...@james.apache.org