You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by bt...@apache.org on 2021/02/23 03:45:14 UTC
[james-project] 01/14: JAMES-3431 Allow the use of zoneDateTime in
mail attributes
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
commit 983278b1e585eab3ed5fead0677bdeeaee816535
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Wed Feb 17 11:38:09 2021 +0700
JAMES-3431 Allow the use of zoneDateTime in mail attributes
---
.../java/org/apache/mailet/AttributeValue.java | 9 +++++
.../main/java/org/apache/mailet/Serializer.java | 34 ++++++++++++++++++
.../java/org/apache/mailet/AttributeValueTest.java | 41 ++++++++++++++++++++++
3 files changed, 84 insertions(+)
diff --git a/mailet/api/src/main/java/org/apache/mailet/AttributeValue.java b/mailet/api/src/main/java/org/apache/mailet/AttributeValue.java
index 4b71eb8..cd6b66e 100644
--- a/mailet/api/src/main/java/org/apache/mailet/AttributeValue.java
+++ b/mailet/api/src/main/java/org/apache/mailet/AttributeValue.java
@@ -22,6 +22,7 @@ package org.apache.mailet;
import java.io.IOException;
import java.io.Serializable;
import java.net.URL;
+import java.time.ZonedDateTime;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
@@ -73,6 +74,11 @@ public class AttributeValue<T> {
return new AttributeValue<>(value, Serializer.FLOAT_SERIALIZER);
}
+ public static AttributeValue<ZonedDateTime> of(ZonedDateTime value) {
+ Preconditions.checkNotNull(value, "value should not be null");
+ return new AttributeValue<>(value, Serializer.DATE_SERIALIZER);
+ }
+
public static AttributeValue<Double> of(Double value) {
Preconditions.checkNotNull(value, "value should not be null");
return new AttributeValue<>(value, Serializer.DOUBLE_SERIALIZER);
@@ -136,6 +142,9 @@ public class AttributeValue<T> {
if (value instanceof Double) {
return of((Double) value);
}
+ if (value instanceof ZonedDateTime) {
+ return of((ZonedDateTime) value);
+ }
if (value instanceof Collection<?>) {
return of(((Collection<AttributeValue<?>>) value));
}
diff --git a/mailet/api/src/main/java/org/apache/mailet/Serializer.java b/mailet/api/src/main/java/org/apache/mailet/Serializer.java
index 7d8203b..d5757a0 100644
--- a/mailet/api/src/main/java/org/apache/mailet/Serializer.java
+++ b/mailet/api/src/main/java/org/apache/mailet/Serializer.java
@@ -19,11 +19,14 @@
package org.apache.mailet;
+import static java.time.format.DateTimeFormatter.ISO_DATE_TIME;
+
import java.io.IOException;
import java.io.Serializable;
import java.io.UncheckedIOException;
import java.net.MalformedURLException;
import java.net.URL;
+import java.time.ZonedDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@@ -80,6 +83,7 @@ public interface Serializer<T> {
LONG_SERIALIZER,
FLOAT_SERIALIZER,
DOUBLE_SERIALIZER,
+ DATE_SERIALIZER,
MESSAGE_ID_DTO_SERIALIZER,
new Serializer.ArbitrarySerializableSerializer<>(),
URL_SERIALIZER,
@@ -267,6 +271,36 @@ public interface Serializer<T> {
Serializer<Double> DOUBLE_SERIALIZER = new DoubleSerializer();
+ class DateSerializer implements Serializer<ZonedDateTime> {
+ @Override
+ public JsonNode serialize(ZonedDateTime object) {
+ String serialized = object.format(ISO_DATE_TIME);
+ return TextNode.valueOf(serialized);
+ }
+
+ @Override
+ public Optional<ZonedDateTime> deserialize(JsonNode json) {
+ if (json instanceof TextNode) {
+ String serialized = json.asText();
+ return Optional.of(ZonedDateTime.parse(serialized, ISO_DATE_TIME));
+ } else {
+ return Optional.empty();
+ }
+ }
+
+ @Override
+ public String getName() {
+ return "DateSerializer";
+ }
+
+ @Override
+ public boolean equals(Object other) {
+ return this.getClass() == other.getClass();
+ }
+ }
+
+ Serializer<ZonedDateTime> DATE_SERIALIZER = new DateSerializer();
+
class MessageIdDtoSerializer implements Serializer<MessageIdDto> {
@Override
diff --git a/mailet/api/src/test/java/org/apache/mailet/AttributeValueTest.java b/mailet/api/src/test/java/org/apache/mailet/AttributeValueTest.java
index 28b627c..7ce8b42 100644
--- a/mailet/api/src/test/java/org/apache/mailet/AttributeValueTest.java
+++ b/mailet/api/src/test/java/org/apache/mailet/AttributeValueTest.java
@@ -25,6 +25,7 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.net.MalformedURLException;
import java.net.URL;
+import java.time.ZonedDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@@ -330,6 +331,46 @@ class AttributeValueTest {
}
@Nested
+ class DateSerialization {
+ @Test
+ void dateShouldBeSerializedAndBack() {
+ AttributeValue<ZonedDateTime> expected = AttributeValue.of(ZonedDateTime.parse("2015-10-30T16:12:00Z"));
+
+ JsonNode json = expected.toJson();
+ AttributeValue<?> actual = AttributeValue.fromJson(json);
+
+ assertThat(actual).isEqualTo(expected);
+ }
+
+ @Test
+ void nullDoubleShouldThrowAnException() {
+ assertThatNullPointerException()
+ .isThrownBy(() -> AttributeValue.of((Double) null));
+ }
+
+ @Test
+ void fromJsonStringShouldReturnDoubleAttributeValueWhenDouble() throws Exception {
+ AttributeValue<ZonedDateTime> expected = AttributeValue.of(ZonedDateTime.parse("2015-10-30T16:12:00Z"));
+
+ AttributeValue<?> actual = AttributeValue.fromJsonString("{\"serializer\":\"DateSerializer\",\"value\":\"2015-10-30T16:12:00Z\"}");
+
+ assertThat(actual).isEqualTo(expected);
+ }
+
+ @Test
+ void fromJsonStringShouldThrowOnMalformedFormattedJson() {
+ assertThatIllegalStateException()
+ .isThrownBy(() -> AttributeValue.fromJsonString("{\"serializer\":\"DateSerializer\",\"value\": []}"));
+ }
+
+ @Test
+ void fromJsonStringShouldThrowOnIntNode() {
+ assertThatIllegalStateException()
+ .isThrownBy(() -> AttributeValue.fromJsonString("{\"serializer\":\"DateSerializer\",\"value\": 1}"));
+ }
+ }
+
+ @Nested
class QueueSerializableTest {
@Test
void queueSerializableShouldBeSerializedAndBack() {
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org