This is an automated email from the ASF dual-hosted git repository.
arnold pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/fineract.git
The following commit(s) were added to refs/heads/develop by this push:
new f24c9f808 FINERACT-1724: Enhance external event datetime precision
f24c9f808 is described below
commit f24c9f808495f9df2fa99e5cc458c7f9f31d101e
Author: Adam Saghy <[email protected]>
AuthorDate: Thu May 4 11:20:03 2023 +0200
FINERACT-1724: Enhance external event datetime precision
---
.../infrastructure/core/service/DateUtils.java | 4 +-
.../jobs/SendAsynchronousEventsTasklet.java | 2 +-
.../external/repository/domain/ExternalEvent.java | 2 +-
.../external/service/message/MessageFactory.java | 22 ++++++-
.../service/message/MessageFactoryTest.java | 75 ++++++++++++++++++++++
5 files changed, 98 insertions(+), 7 deletions(-)
diff --git
a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/service/DateUtils.java
b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/service/DateUtils.java
index 611d9e2be..c1f0a0d29 100644
---
a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/service/DateUtils.java
+++
b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/service/DateUtils.java
@@ -66,9 +66,9 @@ public final class DateUtils {
return OffsetDateTime.now(zone).truncatedTo(ChronoUnit.SECONDS);
}
- public static OffsetDateTime getOffsetDateTimeOfTenantWithMilliseconds() {
+ public static OffsetDateTime getOffsetDateTimeOfTenantWithMostPrecision() {
final ZoneId zone = getDateTimeZoneOfTenant();
- return OffsetDateTime.now(zone).truncatedTo(ChronoUnit.MILLIS);
+ return OffsetDateTime.now(zone);
}
public static LocalDateTime getLocalDateTimeOfSystem() {
diff --git
a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/jobs/SendAsynchronousEventsTasklet.java
b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/jobs/SendAsynchronousEventsTasklet.java
index e092bcebf..d89866517 100644
---
a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/jobs/SendAsynchronousEventsTasklet.java
+++
b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/jobs/SendAsynchronousEventsTasklet.java
@@ -98,7 +98,7 @@ public class SendAsynchronousEventsTasklet implements Tasklet
{
}
private void markEventsAsSent(List<Long> eventIds) {
- OffsetDateTime sentAt =
DateUtils.getOffsetDateTimeOfTenantWithMilliseconds();
+ OffsetDateTime sentAt =
DateUtils.getOffsetDateTimeOfTenantWithMostPrecision();
// Partitioning dataset to avoid exception: PreparedStatement can have
at most 65,535 parameters
List<List<Long>> partitions = Lists.partition(eventIds, 5_000);
diff --git
a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/repository/domain/ExternalEvent.java
b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/repository/domain/ExternalEvent.java
index a771fa812..a285ddf21 100644
---
a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/repository/domain/ExternalEvent.java
+++
b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/repository/domain/ExternalEvent.java
@@ -80,7 +80,7 @@ public class ExternalEvent extends AbstractPersistableCustom {
this.data = data;
this.idempotencyKey = idempotencyKey;
this.aggregateRootId = aggregateRootId;
- this.createdAt = DateUtils.getOffsetDateTimeOfTenantWithMilliseconds();
+ this.createdAt =
DateUtils.getOffsetDateTimeOfTenantWithMostPrecision();
this.status = ExternalEventStatus.TO_BE_SENT;
this.businessDate = DateUtils.getBusinessLocalDate();
}
diff --git
a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/service/message/MessageFactory.java
b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/service/message/MessageFactory.java
index 389a1ab63..332349772 100644
---
a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/service/message/MessageFactory.java
+++
b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/service/message/MessageFactory.java
@@ -18,10 +18,14 @@
*/
package org.apache.fineract.infrastructure.event.external.service.message;
+import static java.time.format.DateTimeFormatter.ISO_LOCAL_DATE;
+
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
+import java.time.format.DateTimeFormatterBuilder;
+import java.time.temporal.ChronoField;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@@ -46,9 +50,21 @@ import org.springframework.stereotype.Component;
@RequiredArgsConstructor
public class MessageFactory implements InitializingBean {
- private final ByteBufferConverter byteBufferConverter;
+ public static final DateTimeFormatter CUSTOM_ISO_LOCAL_DATE_TIME_FORMATTER;
private static final String SOURCE_UUID = UUID.randomUUID().toString();
+ private static final DateTimeFormatter CUSTOM_ISO_LOCAL_TIME_FORMATTER;
+
+ static {
+ CUSTOM_ISO_LOCAL_TIME_FORMATTER = new
DateTimeFormatterBuilder().appendValue(ChronoField.HOUR_OF_DAY,
2).appendLiteral(':')
+ .appendValue(ChronoField.MINUTE_OF_HOUR,
2).optionalStart().appendLiteral(':').appendValue(ChronoField.SECOND_OF_MINUTE,
2)
+ .optionalStart().appendFraction(ChronoField.NANO_OF_SECOND, 6,
9, true).toFormatter();
+ CUSTOM_ISO_LOCAL_DATE_TIME_FORMATTER = new
DateTimeFormatterBuilder().parseCaseInsensitive().append(ISO_LOCAL_DATE)
+
.appendLiteral('T').append(CUSTOM_ISO_LOCAL_TIME_FORMATTER).toFormatter();
+ }
+
+ private final ByteBufferConverter byteBufferConverter;
+
public MessageV1 createMessage(MessageId id, MessageSource source,
MessageType type, MessageCategory category,
MessageCreatedAt createdAt, MessageBusinessDate businessDate,
MessageIdempotencyKey idempotencyKey,
MessageDataSchema dataSchema, MessageData data) {
@@ -84,11 +100,11 @@ public class MessageFactory implements InitializingBean {
}
private String getMessageCreatedAt(OffsetDateTime createdAt) {
- return
createdAt.withOffsetSameInstant(ZoneOffset.UTC).toLocalDateTime().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
+ return
createdAt.withOffsetSameInstant(ZoneOffset.UTC).toLocalDateTime().format(CUSTOM_ISO_LOCAL_DATE_TIME_FORMATTER);
}
private String getMessageBusinessDate(LocalDate businessDate) {
- return businessDate.format(DateTimeFormatter.ISO_LOCAL_DATE);
+ return businessDate.format(ISO_LOCAL_DATE);
}
@Override
diff --git
a/fineract-provider/src/test/java/org/apache/fineract/infrastructure/event/external/service/message/MessageFactoryTest.java
b/fineract-provider/src/test/java/org/apache/fineract/infrastructure/event/external/service/message/MessageFactoryTest.java
new file mode 100644
index 000000000..cd47a86c0
--- /dev/null
+++
b/fineract-provider/src/test/java/org/apache/fineract/infrastructure/event/external/service/message/MessageFactoryTest.java
@@ -0,0 +1,75 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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.apache.fineract.infrastructure.event.external.service.message;
+
+import static org.junit.Assert.assertEquals;
+
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import org.junit.Test;
+
+public class MessageFactoryTest {
+
+ private final LocalDateTime localDateTime1 = LocalDateTime.of(2023, 5, 4,
10, 54, 0, 0);
+ private final LocalDateTime localDateTime2 = LocalDateTime.of(2023, 5, 4,
10, 54, 1, 0);
+ private final LocalDateTime localDateTime3 = LocalDateTime.of(2023, 5, 4,
10, 54, 1, 1000);
+ private final LocalDateTime localDateTime4 = LocalDateTime.of(2023, 5, 4,
10, 0, 0, 1000);
+ private final LocalDateTime localDateTime5 = LocalDateTime.of(2023, 5, 4,
10, 0, 0, 1000000);
+ private final LocalDateTime localDateTime6 = LocalDateTime.of(2023, 5, 4,
10, 0, 0, 1234567);
+
+ /**
+ * Test whether the tailing zeros are always part of the formatter date
time.
+ */
+ @Test
+ public void formatterTest() {
+ DateTimeFormatter dateTimeFormatter =
MessageFactory.CUSTOM_ISO_LOCAL_DATE_TIME_FORMATTER;
+
+ assertEquals("2023-05-04T10:54:00.000000",
localDateTime1.format(dateTimeFormatter));
+ assertEquals("2023-05-04T10:54:01.000000",
localDateTime2.format(dateTimeFormatter));
+ // 1 microsecond
+ assertEquals("2023-05-04T10:54:01.000001",
localDateTime3.format(dateTimeFormatter));
+ assertEquals("2023-05-04T10:00:00.000001",
localDateTime4.format(dateTimeFormatter));
+ // 1 millisecond
+ assertEquals("2023-05-04T10:00:00.001000",
localDateTime5.format(dateTimeFormatter));
+ assertEquals("2023-05-04T10:00:00.001234567",
localDateTime6.format(dateTimeFormatter));
+ }
+
+ /**
+ * Test whether the formatted datetime by
CUSTOM_ISO_LOCAL_DATE_TIME_FORMATTER is still parseable by the
+ * (non-custom) ISO_LOCAL_DATE_TIME_FORMATTER (compatibility check)
+ */
+ @Test
+ public void backParsingTest() {
+ DateTimeFormatter customDateTimeFormatter =
MessageFactory.CUSTOM_ISO_LOCAL_DATE_TIME_FORMATTER;
+
+ assertEquals(localDateTime1,
+
LocalDateTime.parse(localDateTime1.format(customDateTimeFormatter),
DateTimeFormatter.ISO_LOCAL_DATE_TIME));
+ assertEquals(localDateTime2,
+
LocalDateTime.parse(localDateTime2.format(customDateTimeFormatter),
DateTimeFormatter.ISO_LOCAL_DATE_TIME));
+ assertEquals(localDateTime3,
+
LocalDateTime.parse(localDateTime3.format(customDateTimeFormatter),
DateTimeFormatter.ISO_LOCAL_DATE_TIME));
+ assertEquals(localDateTime4,
+
LocalDateTime.parse(localDateTime4.format(customDateTimeFormatter),
DateTimeFormatter.ISO_LOCAL_DATE_TIME));
+ assertEquals(localDateTime5,
+
LocalDateTime.parse(localDateTime5.format(customDateTimeFormatter),
DateTimeFormatter.ISO_LOCAL_DATE_TIME));
+ assertEquals(localDateTime6,
+
LocalDateTime.parse(localDateTime6.format(customDateTimeFormatter),
DateTimeFormatter.ISO_LOCAL_DATE_TIME));
+ }
+
+}