james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [11/17] james-project git commit: JAMES-2578 Add an Optional serializer
Date Thu, 01 Nov 2018 04:07:46 GMT
JAMES-2578 Add an Optional serializer


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/d66fa22b
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/d66fa22b
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/d66fa22b

Branch: refs/heads/master
Commit: d66fa22b9af271d0d56e326a8c4f6c31bc399650
Parents: c1f4ba5
Author: Benoit Tellier <btellier@linagora.com>
Authored: Tue Oct 30 09:08:02 2018 +0700
Committer: Benoit Tellier <btellier@linagora.com>
Committed: Thu Nov 1 11:03:27 2018 +0700

----------------------------------------------------------------------
 .../java/org/apache/mailet/AttributeValue.java  |  9 ++++
 .../main/java/org/apache/mailet/Serializer.java | 34 ++++++++++++-
 .../org/apache/mailet/AttributeValueTest.java   | 52 ++++++++++++++++++++
 3 files changed, 94 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/d66fa22b/mailet/api/src/main/java/org/apache/mailet/AttributeValue.java
----------------------------------------------------------------------
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 6bf3a6e..4ae6952 100644
--- a/mailet/api/src/main/java/org/apache/mailet/AttributeValue.java
+++ b/mailet/api/src/main/java/org/apache/mailet/AttributeValue.java
@@ -87,6 +87,12 @@ public class AttributeValue<T> {
     }
 
     @SuppressWarnings({ "unchecked", "rawtypes" })
+    public static <T> AttributeValue<Optional<AttributeValue<T>>>
of(Optional<AttributeValue<T>> value) {
+        Preconditions.checkNotNull(value, "value should not be null");
+        return new AttributeValue<>(value, new Serializer.OptionalSerializer<>());
+    }
+
+    @SuppressWarnings({ "unchecked", "rawtypes" })
     public static AttributeValue<Collection<AttributeValue<?>>> of(Collection<AttributeValue<?>>
value) {
         Preconditions.checkNotNull(value, "value should not be null");
         return new AttributeValue<>(value, new Serializer.CollectionSerializer());
@@ -135,6 +141,9 @@ public class AttributeValue<T> {
         if (value instanceof URL) {
             return of((URL) value);
         }
+        if (value instanceof Optional) {
+            return of((Optional) value);
+        }
         if (value instanceof Serializable) {
             return ofSerializable((Serializable) value);
         }

http://git-wip-us.apache.org/repos/asf/james-project/blob/d66fa22b/mailet/api/src/main/java/org/apache/mailet/Serializer.java
----------------------------------------------------------------------
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 514b464..e3eb47a 100644
--- a/mailet/api/src/main/java/org/apache/mailet/Serializer.java
+++ b/mailet/api/src/main/java/org/apache/mailet/Serializer.java
@@ -47,6 +47,7 @@ import com.fasterxml.jackson.databind.node.FloatNode;
 import com.fasterxml.jackson.databind.node.IntNode;
 import com.fasterxml.jackson.databind.node.JsonNodeFactory;
 import com.fasterxml.jackson.databind.node.LongNode;
+import com.fasterxml.jackson.databind.node.NullNode;
 import com.fasterxml.jackson.databind.node.NumericNode;
 import com.fasterxml.jackson.databind.node.ObjectNode;
 import com.fasterxml.jackson.databind.node.TextNode;
@@ -82,7 +83,8 @@ public interface Serializer<T> {
                     URL_SERIALIZER,
                     new CollectionSerializer<>(),
                     new MapSerializer<>(),
-                    new FSTSerializer())
+                    new FSTSerializer(),
+                    new OptionalSerializer<>())
                 .collect(ImmutableMap.toImmutableMap(Serializer::getName, Function.identity()));
         }
 
@@ -405,6 +407,36 @@ public interface Serializer<T> {
         }
     }
 
+    class OptionalSerializer<U> implements Serializer<Optional<AttributeValue<U>>>
{
+        @Override
+        public JsonNode serialize(Optional<AttributeValue<U>> object) {
+            return object.map(AttributeValue::toJson)
+                .orElse(NullNode.getInstance());
+        }
+
+        @Override
+        public Optional<Optional<AttributeValue<U>>> deserialize(JsonNode
json) {
+            if (json instanceof ObjectNode) {
+                AttributeValue<U> value = (AttributeValue<U>) AttributeValue.fromJson(json);
+                return Optional.of(Optional.of(value));
+            } else if (json instanceof NullNode) {
+                return Optional.of(Optional.empty());
+            } else {
+                return Optional.empty();
+            }
+        }
+
+        @Override
+        public String getName() {
+            return "OptionalSerializer";
+        }
+
+        @Override
+        public boolean equals(Object other) {
+            return this.getClass() == other.getClass();
+        }
+    }
+
     class FSTSerializer implements Serializer<Serializable> {
         static final FSTConfiguration CONFIGURATION = FSTConfiguration.createJsonConfiguration();
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/d66fa22b/mailet/api/src/test/java/org/apache/mailet/AttributeValueTest.java
----------------------------------------------------------------------
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 dd3f43f..95cb0c2 100644
--- a/mailet/api/src/test/java/org/apache/mailet/AttributeValueTest.java
+++ b/mailet/api/src/test/java/org/apache/mailet/AttributeValueTest.java
@@ -446,6 +446,58 @@ class AttributeValueTest {
         }
     }
 
+    @Nested
+    class OptionalSerialization {
+        @Test
+        void nullMapShouldThrowAnException() {
+            assertThatNullPointerException().
+                isThrownBy(() -> AttributeValue.of((Optional) null));
+        }
+
+        @Test
+        void emptyOptionalShouldBeSerializedAndBack() {
+            AttributeValue<?> expected = AttributeValue.of(Optional.empty());
+            JsonNode json = expected.toJson();
+            AttributeValue<?> actual = AttributeValue.fromJson(json);
+
+            assertThat(actual).isEqualTo(expected);
+        }
+
+        @Test
+        void optionalShouldBeSerializedAndBack() {
+            AttributeValue<?> expected = AttributeValue.of(Optional.of(AttributeValue.of(42)));
+            JsonNode json = expected.toJson();
+            AttributeValue<?> actual = AttributeValue.fromJson(json);
+
+            assertThat(actual).isEqualTo(expected);
+        }
+
+        @Test
+        void fromJsonStringShouldReturnEmptyOptionalWhenEmptyOptional() throws Exception
{
+            AttributeValue<?> expected = AttributeValue.of(Optional.empty());
+
+            AttributeValue<?> actual = AttributeValue.fromJsonString("{\"serializer\":\"OptionalSerializer\",\"value\":null}");
+
+            assertThat(actual).isEqualTo(expected);
+        }
+
+        @Test
+        void fromJsonStringShouldReturnOptional() throws Exception {
+            AttributeValue<Optional<AttributeValue<Integer>>> expected
= AttributeValue.of(
+                Optional.of(AttributeValue.of(1)));
+
+            AttributeValue<?> actual = AttributeValue.fromJsonString("{\"serializer\":\"OptionalSerializer\",\"value\":{\"serializer\":\"IntSerializer\",\"value\":1}}");
+
+            assertThat(actual).isEqualTo(expected);
+        }
+
+        @Test
+        void fromJsonStringShouldThrowOnMalformedFormattedJson() {
+            assertThatIllegalStateException()
+                .isThrownBy(() -> AttributeValue.fromJsonString("{\"serializer\":\"OptionalSerializer\",\"value\":
[]}"));
+        }
+    }
+
     @Test
     void fromJsonStringShouldThrowOnUnknownSerializer() {
         assertThatIllegalStateException()


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message