JAMES-2578 Add MessageId serialization

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

Branch: refs/heads/master
Commit: 38b3d7a598cb684e8090e24d3542cda612463d92
Parents: ccd8226
Author: Gautier DI FOLCO <gdifo...@linagora.com>
Authored: Tue Oct 30 17:03:53 2018 +0100
Committer: Benoit Tellier <btell...@linagora.com>
Committed: Thu Nov 1 11:04:22 2018 +0700

----------------------------------------------------------------------
 .../james/mailbox/model/MessageIdDto.java       | 66 ++++++++++++++++++
 .../james/mailbox/model/MessageIdDtoTest.java   | 70 ++++++++++++++++++++
 mailet/api/pom.xml                              | 10 +++
 .../java/org/apache/mailet/AttributeValue.java  | 10 +++
 .../main/java/org/apache/mailet/Serializer.java | 30 +++++++++
 .../org/apache/mailet/AttributeValueTest.java   | 37 +++++++++++
 6 files changed, 223 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/38b3d7a5/mailbox/api/src/main/java/org/apache/james/mailbox/model/MessageIdDto.java
----------------------------------------------------------------------
diff --git 
a/mailbox/api/src/main/java/org/apache/james/mailbox/model/MessageIdDto.java 
b/mailbox/api/src/main/java/org/apache/james/mailbox/model/MessageIdDto.java
new file mode 100644
index 0000000..275a52e
--- /dev/null
+++ b/mailbox/api/src/main/java/org/apache/james/mailbox/model/MessageIdDto.java
@@ -0,0 +1,66 @@
+/****************************************************************
+ * 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.james.mailbox.model;
+
+import java.util.Objects;
+
+import com.google.common.base.MoreObjects;
+
+public class MessageIdDto {
+    private final String messageId;
+
+    public MessageIdDto(String messageId) {
+        this.messageId = messageId;
+    }
+
+    public MessageIdDto(MessageId messageId) {
+        this.messageId = messageId.serialize();
+    }
+
+    public MessageId instantiate(MessageId.Factory factory) {
+        return factory.fromString(messageId);
+    }
+
+    public String asString() {
+        return messageId;
+    }
+
+    @Override
+    public final boolean equals(Object o) {
+        if (o instanceof MessageIdDto) {
+            MessageIdDto that = (MessageIdDto) o;
+
+            return Objects.equals(this.messageId, that.messageId);
+        }
+        return false;
+    }
+
+    @Override
+    public final int hashCode() {
+        return Objects.hash(messageId);
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(this)
+            .add("messageId", messageId)
+            .toString();
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/38b3d7a5/mailbox/api/src/test/java/org/apache/james/mailbox/model/MessageIdDtoTest.java
----------------------------------------------------------------------
diff --git 
a/mailbox/api/src/test/java/org/apache/james/mailbox/model/MessageIdDtoTest.java
 
b/mailbox/api/src/test/java/org/apache/james/mailbox/model/MessageIdDtoTest.java
new file mode 100644
index 0000000..0c7e74d
--- /dev/null
+++ 
b/mailbox/api/src/test/java/org/apache/james/mailbox/model/MessageIdDtoTest.java
@@ -0,0 +1,70 @@
+/*
+ * 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.james.mailbox.model;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import org.junit.Test;
+
+import nl.jqno.equalsverifier.EqualsVerifier;
+
+public class MessageIdDtoTest {
+
+    private static final TestMessageId.Factory factory = new 
TestMessageId.Factory();
+    private static final Long SAMPLE_ID_VALUE = 42L;
+    private static final String SAMPLE_ID_STRING = SAMPLE_ID_VALUE.toString();
+    private static final TestMessageId SAMPLE_ID = 
TestMessageId.of(SAMPLE_ID_VALUE);
+
+    @Test
+    public void shouldRespectJavaBeanContract() {
+        EqualsVerifier.forClass(MessageIdDto.class).verify();
+    }
+
+    @Test
+    public void shouldAcceptStringAndGiveItBack() {
+        assertThat(new MessageIdDto(SAMPLE_ID_STRING).asString())
+            .isEqualTo(SAMPLE_ID_STRING);
+    }
+
+    @Test
+    public void shouldAcceptMessageIdAndGiveTheRightString() {
+        assertThat(new MessageIdDto(SAMPLE_ID).asString())
+            .isEqualTo(SAMPLE_ID_STRING);
+    }
+
+    @Test
+    public void shouldAcceptMessageIdAndGiveItBack() {
+        assertThat(new MessageIdDto(SAMPLE_ID).instantiate(factory))
+            .isEqualTo(SAMPLE_ID);
+    }
+
+    @Test
+    public void shouldAcceptStringAndGiveAnInstantiatedMessageId() {
+        assertThat(new MessageIdDto(SAMPLE_ID_STRING).instantiate(factory))
+            .isEqualTo(SAMPLE_ID);
+    }
+
+    @Test
+    public void shouldThrowAnExceptionOnWronglyFormattedString() {
+        MessageIdDto messageIdDto = new MessageIdDto("Definitively not a 
number");
+        assertThatThrownBy(() -> messageIdDto.instantiate(factory))
+            .isInstanceOf(Exception.class);
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/38b3d7a5/mailet/api/pom.xml
----------------------------------------------------------------------
diff --git a/mailet/api/pom.xml b/mailet/api/pom.xml
index 5b5d599..75be1f1 100644
--- a/mailet/api/pom.xml
+++ b/mailet/api/pom.xml
@@ -38,6 +38,16 @@
     <dependencies>
         <dependency>
             <groupId>${project.groupId}</groupId>
+            <artifactId>apache-james-mailbox-api</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>${project.groupId}</groupId>
+            <artifactId>apache-james-mailbox-api</artifactId>
+            <type>test-jar</type>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>${project.groupId}</groupId>
             <artifactId>james-core</artifactId>
         </dependency>
         <dependency>

http://git-wip-us.apache.org/repos/asf/james-project/blob/38b3d7a5/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 6933bf8..d550d84 100644
--- a/mailet/api/src/main/java/org/apache/mailet/AttributeValue.java
+++ b/mailet/api/src/main/java/org/apache/mailet/AttributeValue.java
@@ -27,6 +27,8 @@ import java.util.Map;
 import java.util.Objects;
 import java.util.Optional;
 
+import org.apache.james.mailbox.model.MessageIdDto;
+
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -76,6 +78,11 @@ public class AttributeValue<T> {
         return new AttributeValue<>(value, Serializer.DOUBLE_SERIALIZER);
     }
 
+    public static AttributeValue<MessageIdDto> of(MessageIdDto value) {
+        Preconditions.checkNotNull(value, "value should not be null");
+        return new AttributeValue<>(value, 
Serializer.MESSAGE_ID_DTO_SERIALIZER);
+    }
+
     public static <T extends ArbitrarySerializable<T>> AttributeValue<T> of(T 
value) {
         Preconditions.checkNotNull(value, "value should not be null");
         return new AttributeValue<>(value, new 
Serializer.ArbitrarySerializableSerializer<>());
@@ -136,6 +143,9 @@ public class AttributeValue<T> {
         if (value instanceof Map<?,?>) {
             return of(((Map<String, AttributeValue<?>>) value));
         }
+        if (value instanceof MessageIdDto) {
+            return of((MessageIdDto) value);
+        }
         if (value instanceof ArbitrarySerializable) {
             return of((ArbitrarySerializable) value);
         }

http://git-wip-us.apache.org/repos/asf/james-project/blob/38b3d7a5/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 016472a..ba73395 100644
--- a/mailet/api/src/main/java/org/apache/mailet/Serializer.java
+++ b/mailet/api/src/main/java/org/apache/mailet/Serializer.java
@@ -32,6 +32,7 @@ import java.util.Optional;
 import java.util.function.Function;
 import java.util.stream.Stream;
 
+import org.apache.james.mailbox.model.MessageIdDto;
 import org.apache.james.util.streams.Iterators;
 import org.nustaq.serialization.FSTConfiguration;
 import org.slf4j.Logger;
@@ -78,6 +79,7 @@ public interface Serializer<T> {
                     LONG_SERIALIZER,
                     FLOAT_SERIALIZER,
                     DOUBLE_SERIALIZER,
+                    MESSAGE_ID_DTO_SERIALIZER,
                     new Serializer.ArbitrarySerializableSerializer(),
                     URL_SERIALIZER,
                     new CollectionSerializer<>(),
@@ -264,6 +266,34 @@ public interface Serializer<T> {
 
     Serializer<Double> DOUBLE_SERIALIZER = new DoubleSerializer();
 
+    class MessageIdDtoSerializer implements Serializer<MessageIdDto> {
+
+        @Override
+        public JsonNode serialize(MessageIdDto serializable) {
+            return STRING_SERIALIZER
+                    .serialize(serializable.asString());
+        }
+
+        @Override
+        public Optional<MessageIdDto> deserialize(JsonNode json) {
+            return STRING_SERIALIZER
+                    .deserialize(json)
+                    .map(MessageIdDto::new);
+        }
+
+        @Override
+        public String getName() {
+            return "MessageIdDtoSerializer";
+        }
+
+        @Override
+        public boolean equals(Object other) {
+            return this.getClass() == other.getClass();
+        }
+    }
+
+    Serializer<MessageIdDto> MESSAGE_ID_DTO_SERIALIZER = new 
MessageIdDtoSerializer();
+
     class ArbitrarySerializableSerializer<T extends ArbitrarySerializable<T>> 
implements Serializer<T> {
         private static final Logger LOGGER = 
LoggerFactory.getLogger(ArbitrarySerializableSerializer.class);
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/38b3d7a5/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 3ce1d3f..c8bbc86 100644
--- a/mailet/api/src/test/java/org/apache/mailet/AttributeValueTest.java
+++ b/mailet/api/src/test/java/org/apache/mailet/AttributeValueTest.java
@@ -30,6 +30,9 @@ import java.util.Map;
 import java.util.Objects;
 import java.util.Optional;
 
+import org.apache.james.mailbox.model.MessageIdDto;
+import org.apache.james.mailbox.model.TestMessageId;
+
 import org.junit.jupiter.api.Nested;
 import org.junit.jupiter.api.Test;
 
@@ -389,6 +392,40 @@ class AttributeValueTest {
     }
 
     @Nested
+    class MessageIdDtoSerialization {
+        @Test
+        void messageIdShouldBeSerializedAndBack() {
+            AttributeValue<MessageIdDto> expected = AttributeValue.of(new 
MessageIdDto(TestMessageId.of(42)));
+
+            JsonNode json = expected.toJson();
+            AttributeValue<?> actual = AttributeValue.fromJson(json);
+
+            assertThat(actual).isEqualTo(expected);
+        }
+
+        @Test
+        void nullMessageIdDtoShouldThrowAnException() {
+            assertThatNullPointerException().
+                isThrownBy(() -> AttributeValue.of((MessageIdDto) null));
+        }
+
+        @Test
+        void fromJsonStringShouldReturnMessageIdAttributeValue() throws 
Exception {
+            AttributeValue<MessageIdDto> expected = AttributeValue.of(new 
MessageIdDto(TestMessageId.of(42)));
+
+            AttributeValue<?> actual = 
AttributeValue.fromJsonString("{\"serializer\":\"MessageIdDtoSerializer\",\"value\":\"42\"}");
+
+            assertThat(actual).isEqualTo(expected);
+        }
+
+        @Test
+        void fromJsonStringShouldThrowOnMalformedFormattedJson() {
+            assertThatIllegalStateException()
+                .isThrownBy(() -> 
AttributeValue.fromJsonString("{\"serializer\":\"MessageIdDtoSerializer\",\"value\":
 {}}"));
+        }
+    }
+
+    @Nested
     class ListSerialization {
         @Test
         void nullStringListShouldThrowAnException() {


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

Reply via email to