This is an automated email from the ASF dual-hosted git repository.

rcordier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 0beafd9f474d513ff20bae1b6eb9b0c809261d68
Author: Benoit Tellier <[email protected]>
AuthorDate: Thu Nov 21 13:43:39 2019 +0700

    [Refactoring] Strong typing for ModSeq
---
 .../mail/CassandraModSeqProviderTest.java          |  2 +-
 .../elasticsearch/json/IndexableMessageTest.java   | 35 ++++++++++++++++++++++
 .../apache/james/event/json/EventSerializer.scala  | 12 ++++++--
 .../james/event/json/dtos/MessageUidTest.java      |  6 ++++
 .../apache/james/event/json/dtos/ModSeqTest.java   | 14 ++++++---
 5 files changed, 62 insertions(+), 7 deletions(-)

diff --git 
a/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/CassandraModSeqProviderTest.java
 
b/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/CassandraModSeqProviderTest.java
index 55a8088..e9996ff 100644
--- 
a/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/CassandraModSeqProviderTest.java
+++ 
b/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/CassandraModSeqProviderTest.java
@@ -66,7 +66,7 @@ class CassandraModSeqProviderTest {
     void highestModSeqShouldRetrieveValueStoredNextModSeq() throws Exception {
         int nbEntries = 100;
         ModSeq result = modSeqProvider.highestModSeq(mailbox);
-        assertThat(result).isEqualTo(0);
+        assertThat(result).isEqualTo(ModSeq.first());
         LongStream.range(0, nbEntries)
             .forEach(Throwing.longConsumer(value -> {
                     ModSeq modSeq = modSeqProvider.nextModSeq(mailbox);
diff --git 
a/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/json/IndexableMessageTest.java
 
b/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/json/IndexableMessageTest.java
index 1d658dd..bcfaaa48 100644
--- 
a/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/json/IndexableMessageTest.java
+++ 
b/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/json/IndexableMessageTest.java
@@ -32,6 +32,7 @@ import java.util.Optional;
 import javax.mail.Flags;
 
 import org.apache.james.mailbox.MessageUid;
+import org.apache.james.mailbox.ModSeq;
 import org.apache.james.mailbox.elasticsearch.IndexAttachments;
 import org.apache.james.mailbox.extractor.ParsedContent;
 import org.apache.james.mailbox.extractor.TextExtractor;
@@ -80,6 +81,8 @@ public class IndexableMessageTest {
             .thenReturn(mailboxId);
         when(mailboxMessage.getMessageId())
             .thenReturn(InMemoryMessageId.of(42));
+        when(mailboxMessage.getModSeq())
+            .thenReturn(ModSeq.first());
         when(mailboxMessage.getFullContent())
             .thenReturn(new ByteArrayInputStream("".getBytes()));
         when(mailboxMessage.createFlags())
@@ -103,6 +106,8 @@ public class IndexableMessageTest {
         TestId mailboxId = TestId.of(1);
         when(mailboxMessage.getMailboxId())
             .thenReturn(mailboxId);
+        when(mailboxMessage.getModSeq())
+            .thenReturn(ModSeq.first());
         when(mailboxMessage.getMessageId())
             .thenReturn(InMemoryMessageId.of(42));
         when(mailboxMessage.getFullContent())
@@ -130,6 +135,8 @@ public class IndexableMessageTest {
         TestId mailboxId = TestId.of(1);
         when(mailboxMessage.getMailboxId())
             .thenReturn(mailboxId);
+        when(mailboxMessage.getModSeq())
+            .thenReturn(ModSeq.first());
         when(mailboxMessage.getMessageId())
             .thenReturn(InMemoryMessageId.of(42));
         when(mailboxMessage.getFullContent())
@@ -165,6 +172,8 @@ public class IndexableMessageTest {
             .thenReturn(new Flags());
         when(mailboxMessage.getUid())
             .thenReturn(MESSAGE_UID);
+        when(mailboxMessage.getModSeq())
+            .thenReturn(ModSeq.first());
 
         IndexableMessage indexableMessage = IndexableMessage.builder()
                 .message(mailboxMessage)
@@ -192,6 +201,8 @@ public class IndexableMessageTest {
             .thenReturn(new ByteArrayInputStream("Bcc: First bcc 
<[email protected]>\nBcc: Second bcc <[email protected]>".getBytes()));
         when(mailboxMessage.createFlags())
             .thenReturn(new Flags());
+        when(mailboxMessage.getModSeq())
+            .thenReturn(ModSeq.first());
 
         IndexableMessage indexableMessage = IndexableMessage.builder()
                 .message(mailboxMessage)
@@ -211,6 +222,8 @@ public class IndexableMessageTest {
         TestId mailboxId = TestId.of(1);
         when(mailboxMessage.getMailboxId())
             .thenReturn(mailboxId);
+        when(mailboxMessage.getModSeq())
+            .thenReturn(ModSeq.first());
         when(mailboxMessage.getMessageId())
             .thenReturn(InMemoryMessageId.of(42));
         when(mailboxMessage.getFullContent())
@@ -236,6 +249,8 @@ public class IndexableMessageTest {
         TestId mailboxId = TestId.of(1);
         when(mailboxMessage.getMailboxId())
             .thenReturn(mailboxId);
+        when(mailboxMessage.getModSeq())
+            .thenReturn(ModSeq.first());
         when(mailboxMessage.getMessageId())
             .thenReturn(InMemoryMessageId.of(42));
         when(mailboxMessage.getFullContent())
@@ -263,6 +278,8 @@ public class IndexableMessageTest {
             .thenReturn(mailboxId);
         when(mailboxMessage.getMessageId())
             .thenReturn(InMemoryMessageId.of(42));
+        when(mailboxMessage.getModSeq())
+            .thenReturn(ModSeq.first());
         when(mailboxMessage.getFullContent())
             
.thenReturn(ClassLoader.getSystemResourceAsStream("eml/mailWithHeaders.eml"));
         when(mailboxMessage.createFlags())
@@ -295,6 +312,8 @@ public class IndexableMessageTest {
         TestId mailboxId = TestId.of(1);
         when(mailboxMessage.getMailboxId())
             .thenReturn(mailboxId);
+        when(mailboxMessage.getModSeq())
+            .thenReturn(ModSeq.first());
         when(mailboxMessage.getMessageId())
             .thenReturn(InMemoryMessageId.of(42));
         when(mailboxMessage.getFullContent())
@@ -332,6 +351,8 @@ public class IndexableMessageTest {
             .thenReturn(new Flags());
         when(mailboxMessage.getUid())
             .thenReturn(MESSAGE_UID);
+        when(mailboxMessage.getModSeq())
+            .thenReturn(ModSeq.first());
         when(mailboxMessage.getProperties())
             .thenReturn(ImmutableList.of(new 
Property(PropertyBuilder.JAMES_INTERNALS, PropertyBuilder.HAS_ATTACHMENT, 
"false")));
 
@@ -354,6 +375,8 @@ public class IndexableMessageTest {
         TestId mailboxId = TestId.of(1);
         when(mailboxMessage.getMailboxId())
             .thenReturn(mailboxId);
+        when(mailboxMessage.getModSeq())
+            .thenReturn(ModSeq.first());
         when(mailboxMessage.getMessageId())
             .thenReturn(InMemoryMessageId.of(42));
         when(mailboxMessage.getFullContent())
@@ -384,6 +407,8 @@ public class IndexableMessageTest {
         TestId mailboxId = TestId.of(1);
         when(mailboxMessage.getMailboxId())
             .thenReturn(mailboxId);
+        when(mailboxMessage.getModSeq())
+            .thenReturn(ModSeq.first());
         when(mailboxMessage.getMessageId())
             .thenReturn(InMemoryMessageId.of(42));
         when(mailboxMessage.getFullContent())
@@ -412,6 +437,8 @@ public class IndexableMessageTest {
         TestId mailboxId = TestId.of(1);
         when(mailboxMessage.getMailboxId())
             .thenReturn(mailboxId);
+        when(mailboxMessage.getModSeq())
+            .thenReturn(ModSeq.first());
         when(mailboxMessage.getMessageId())
             .thenReturn(InMemoryMessageId.of(42));
         when(mailboxMessage.getFullContent())
@@ -440,6 +467,8 @@ public class IndexableMessageTest {
         TestId mailboxId = TestId.of(1);
         when(mailboxMessage.getMailboxId())
             .thenReturn(mailboxId);
+        when(mailboxMessage.getModSeq())
+            .thenReturn(ModSeq.first());
         when(mailboxMessage.getMessageId())
             .thenReturn(InMemoryMessageId.of(42));
         when(mailboxMessage.getFullContent())
@@ -486,6 +515,8 @@ public class IndexableMessageTest {
         TestId mailboxId = TestId.of(1);
         when(mailboxMessage.getMailboxId())
             .thenReturn(mailboxId);
+        when(mailboxMessage.getModSeq())
+            .thenReturn(ModSeq.first());
         when(mailboxMessage.getMessageId())
             .thenReturn(InMemoryMessageId.of(42));
         when(mailboxMessage.getFullContent())
@@ -518,6 +549,8 @@ public class IndexableMessageTest {
         TestId mailboxId = TestId.of(1);
         when(mailboxMessage.getMailboxId())
             .thenReturn(mailboxId);
+        when(mailboxMessage.getModSeq())
+            .thenReturn(ModSeq.first());
         when(mailboxMessage.getMessageId())
             .thenReturn(invalidMessageIdThatReturnNull);
         when(mailboxMessage.getFullContent())
@@ -548,6 +581,8 @@ public class IndexableMessageTest {
             .thenReturn(mailboxId);
         when(mailboxMessage.getMessageId())
             .thenReturn(null);
+        when(mailboxMessage.getModSeq())
+            .thenReturn(ModSeq.first());
         when(mailboxMessage.getFullContent())
             
.thenReturn(ClassLoader.getSystemResourceAsStream("eml/bodyMakeTikaToFail.eml"));
         when(mailboxMessage.createFlags())
diff --git 
a/mailbox/event/json/src/main/scala/org/apache/james/event/json/EventSerializer.scala
 
b/mailbox/event/json/src/main/scala/org/apache/james/event/json/EventSerializer.scala
index d7596e7..4babdbb 100644
--- 
a/mailbox/event/json/src/main/scala/org/apache/james/event/json/EventSerializer.scala
+++ 
b/mailbox/event/json/src/main/scala/org/apache/james/event/json/EventSerializer.scala
@@ -279,11 +279,19 @@ class JsonSerialize(mailboxIdFactory: MailboxId.Factory, 
messageIdFactory: Messa
     case _ => JsError()
   }
   implicit val messageUidReads: Reads[MessageUid] = {
-    case JsNumber(value) => JsSuccess(MessageUid.of(value.toLong))
+    case JsNumber(value) => if (value.isValidLong) {
+        JsSuccess(MessageUid.of(value.toLong))
+      } else {
+        JsError()
+      }
     case _ => JsError()
   }
   implicit val modSeqReads: Reads[ModSeq] = {
-    case JsNumber(value) => JsSuccess(ModSeq.of(value.toLong))
+    case JsNumber(value) => if (value.isValidLong) {
+      JsSuccess(ModSeq.of(value.toLong))
+    } else {
+      JsError()
+    }
     case _ => JsError()
   }
   implicit val userFlagsReads: Reads[UserFlag] = {
diff --git 
a/mailbox/event/json/src/test/java/org/apache/james/event/json/dtos/MessageUidTest.java
 
b/mailbox/event/json/src/test/java/org/apache/james/event/json/dtos/MessageUidTest.java
index 0d59839..b3b2d6c 100644
--- 
a/mailbox/event/json/src/test/java/org/apache/james/event/json/dtos/MessageUidTest.java
+++ 
b/mailbox/event/json/src/test/java/org/apache/james/event/json/dtos/MessageUidTest.java
@@ -45,6 +45,12 @@ class MessageUidTest {
     }
 
     @Test
+    void messageUidShouldReturnErrorWhenFloat() {
+        assertThat(DTO_JSON_SERIALIZE.messageUidReads().reads(new 
JsNumber(BigDecimal.valueOf(18.5))))
+            .isInstanceOf(JsError.class);
+    }
+
+    @Test
     void messageUidShouldReturnErrorWhenString() {
         assertThat(DTO_JSON_SERIALIZE.messageUidReads().reads(new 
JsString("18")))
             .isInstanceOf(JsError.class);
diff --git 
a/mailbox/event/json/src/test/java/org/apache/james/event/json/dtos/ModSeqTest.java
 
b/mailbox/event/json/src/test/java/org/apache/james/event/json/dtos/ModSeqTest.java
index f0044b1..d66dc8d 100644
--- 
a/mailbox/event/json/src/test/java/org/apache/james/event/json/dtos/ModSeqTest.java
+++ 
b/mailbox/event/json/src/test/java/org/apache/james/event/json/dtos/ModSeqTest.java
@@ -33,25 +33,31 @@ import scala.math.BigDecimal;
 
 class ModSeqTest {
     @Test
-    void messageUidShouldBeWellSerialized() {
+    void modSeqShouldBeWellSerialized() {
         assertThat(DTO_JSON_SERIALIZE.modSeqWrites().writes(ModSeq.of(18)))
             .isEqualTo(new JsNumber(BigDecimal.valueOf(18)));
     }
 
     @Test
-    void messageUidShouldBeWellDeSerialized() {
+    void modSeqShouldBeWellDeSerialized() {
         assertThat(DTO_JSON_SERIALIZE.modSeqReads().reads(new 
JsNumber(BigDecimal.valueOf(18))).get())
             .isEqualTo(ModSeq.of(18));
     }
 
     @Test
-    void messageUidShouldReturnErrorWhenString() {
+    void modSeqShouldReturnErrorWhenFloat() {
+        assertThat(DTO_JSON_SERIALIZE.modSeqReads().reads(new 
JsNumber(BigDecimal.valueOf(18.5))))
+            .isInstanceOf(JsError.class);
+    }
+
+    @Test
+    void modSeqShouldReturnErrorWhenString() {
         assertThat(DTO_JSON_SERIALIZE.modSeqReads().reads(new JsString("18")))
             .isInstanceOf(JsError.class);
     }
 
     @Test
-    void messageUidShouldReturnErrorWhenNull() {
+    void modSeqShouldReturnErrorWhenNull() {
         assertThat(DTO_JSON_SERIALIZE.modSeqReads().reads(JsNull$.MODULE$))
             .isInstanceOf(JsError.class);
     }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to