You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by rc...@apache.org on 2021/01/05 04:56:00 UTC

[james-project] 21/24: JAMES-3471 Should be able to serialize UpdatedFlags with and without a message id

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 a8e7a33d134e9a32bef64f65304b822a2662d945
Author: Rene Cordier <rc...@linagora.com>
AuthorDate: Thu Dec 31 16:54:19 2020 +0700

    JAMES-3471 Should be able to serialize UpdatedFlags with and without a message id
---
 .../james/mailbox/events/MailboxListener.java      | 18 +++++-
 .../apache/james/mailbox/model/UpdatedFlags.java   | 37 ++++++++---
 .../scala/org/apache/james/event/json/DTOs.scala   |  5 +-
 .../event/json/FlagsUpdatedSerializationTest.java  | 72 ++++++++++++++++++++++
 4 files changed, 121 insertions(+), 11 deletions(-)

diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/events/MailboxListener.java b/mailbox/api/src/main/java/org/apache/james/mailbox/events/MailboxListener.java
index d305d82..3517801 100644
--- a/mailbox/api/src/main/java/org/apache/james/mailbox/events/MailboxListener.java
+++ b/mailbox/api/src/main/java/org/apache/james/mailbox/events/MailboxListener.java
@@ -24,6 +24,7 @@ import java.util.Collection;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
+import java.util.Optional;
 import java.util.SortedMap;
 
 import org.apache.james.core.Username;
@@ -587,15 +588,21 @@ public interface MailboxListener {
      */
     class FlagsUpdated extends MessageEvent {
         private final List<MessageUid> uids;
+        private final List<MessageId> messageIds;
         private final List<UpdatedFlags> updatedFlags;
 
-        public FlagsUpdated(MailboxSession.SessionId sessionId, Username username, MailboxPath path, MailboxId mailboxId, List<UpdatedFlags> updatedFlags,
-                            EventId eventId) {
+        public FlagsUpdated(MailboxSession.SessionId sessionId, Username username, MailboxPath path,
+                            MailboxId mailboxId, List<UpdatedFlags> updatedFlags, EventId eventId) {
             super(sessionId, username, path, mailboxId, eventId);
             this.updatedFlags = ImmutableList.copyOf(updatedFlags);
             this.uids = updatedFlags.stream()
                 .map(UpdatedFlags::getUid)
                 .collect(Guavate.toImmutableList());
+            this.messageIds = updatedFlags.stream()
+                .map(UpdatedFlags::getMessageId)
+                .filter(Optional::isPresent)
+                .map(Optional::get)
+                .collect(Guavate.toImmutableList());
         }
 
         @Override
@@ -603,6 +610,10 @@ public interface MailboxListener {
             return uids;
         }
 
+        public Collection<MessageId> getMessageIds() {
+            return messageIds;
+        }
+
         public List<UpdatedFlags> getUpdatedFlags() {
             return updatedFlags;
         }
@@ -623,6 +634,7 @@ public interface MailboxListener {
                     && Objects.equals(this.path, that.path)
                     && Objects.equals(this.mailboxId, that.mailboxId)
                     && Objects.equals(this.uids, that.uids)
+                    && Objects.equals(this.messageIds, that.messageIds)
                     && Objects.equals(this.updatedFlags, that.updatedFlags);
             }
             return false;
@@ -630,7 +642,7 @@ public interface MailboxListener {
 
         @Override
         public final int hashCode() {
-            return Objects.hash(eventId, sessionId, username, path, mailboxId, uids, updatedFlags);
+            return Objects.hash(eventId, sessionId, username, path, mailboxId, uids, messageIds, updatedFlags);
         }
     }
 
diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/model/UpdatedFlags.java b/mailbox/api/src/main/java/org/apache/james/mailbox/model/UpdatedFlags.java
index 1611a63..0649c58 100644
--- a/mailbox/api/src/main/java/org/apache/james/mailbox/model/UpdatedFlags.java
+++ b/mailbox/api/src/main/java/org/apache/james/mailbox/model/UpdatedFlags.java
@@ -43,6 +43,7 @@ public class UpdatedFlags {
 
     public static class Builder {
         private MessageUid uid;
+        private Optional<MessageId> messageId = Optional.empty();
         private Flags oldFlags;
         private Flags newFlags;
         private Optional<ModSeq> modSeq = Optional.empty();
@@ -55,6 +56,16 @@ public class UpdatedFlags {
             return this;
         }
 
+        public Builder messageId(MessageId messageId) {
+            this.messageId = Optional.of(messageId);
+            return this;
+        }
+
+        public Builder messageId(Optional<MessageId> messageId) {
+            this.messageId = messageId;
+            return this;
+        }
+
         public Builder oldFlags(Flags oldFlags) {
             this.oldFlags = oldFlags;
             return this;
@@ -71,22 +82,27 @@ public class UpdatedFlags {
         }
 
         public UpdatedFlags build() {
-            Preconditions.checkState(uid != null);
-            Preconditions.checkState(newFlags != null);
-            Preconditions.checkState(oldFlags != null);
+            Preconditions.checkNotNull(uid);
+            Preconditions.checkNotNull(newFlags);
+            Preconditions.checkNotNull(oldFlags);
             Preconditions.checkState(modSeq.isPresent());
-            return new UpdatedFlags(uid, modSeq.get(), oldFlags, newFlags);
+            return new UpdatedFlags(uid, messageId, modSeq.get(), oldFlags, newFlags);
         }
     }
 
     private final MessageUid uid;
+    /**
+     * The usage of Optional here is for backward compatibility (to be able to still dequeue older events)
+     */
+    private final Optional<MessageId> messageId;
     private final Flags oldFlags;
     private final Flags newFlags;
     private final Flags modifiedFlags;
     private final ModSeq modSeq;
 
-    private UpdatedFlags(MessageUid uid, ModSeq modSeq, Flags oldFlags, Flags newFlags) {
+    private UpdatedFlags(MessageUid uid, Optional<MessageId> messageId, ModSeq modSeq, Flags oldFlags, Flags newFlags) {
        this.uid = uid;
+       this.messageId = messageId;
        this.modSeq = modSeq;
        this.oldFlags = oldFlags;
        this.newFlags = newFlags;
@@ -177,7 +193,13 @@ public class UpdatedFlags {
     public MessageUid getUid() {
         return uid;
     }
-   
+
+    /**
+     * Return the uid for the message whichs {@link Flags} was updated
+     */
+    public Optional<MessageId> getMessageId() {
+        return messageId;
+    }
 
     /**
      * Gets an iterator for the system flags changed.
@@ -238,6 +260,7 @@ public class UpdatedFlags {
         UpdatedFlags that = (UpdatedFlags) other;
 
         return Objects.equals(uid, that.uid) &&
+                Objects.equals(messageId, that.messageId) &&
                 Objects.equals(oldFlags, that.oldFlags) &&
                 Objects.equals(newFlags, that.newFlags) &&
                 Objects.equals(modSeq, that.modSeq);
@@ -245,7 +268,7 @@ public class UpdatedFlags {
 
     @Override
     public final int hashCode() {
-        return Objects.hash(uid, oldFlags, newFlags, modSeq);
+        return Objects.hash(uid, messageId, oldFlags, newFlags, modSeq);
     }
     
     @Override
diff --git a/mailbox/event/json/src/main/scala/org/apache/james/event/json/DTOs.scala b/mailbox/event/json/src/main/scala/org/apache/james/event/json/DTOs.scala
index 08ab9e3..f51f100 100644
--- a/mailbox/event/json/src/main/scala/org/apache/james/event/json/DTOs.scala
+++ b/mailbox/event/json/src/main/scala/org/apache/james/event/json/DTOs.scala
@@ -32,6 +32,7 @@ import org.apache.james.mailbox.model.{MessageId, MailboxACL => JavaMailboxACL,
 import org.apache.james.mailbox.{FlagsBuilder, MessageUid, ModSeq}
 
 import scala.jdk.CollectionConverters._
+import scala.jdk.OptionConverters._
 
 object DTOs {
 
@@ -142,14 +143,16 @@ object DTOs {
   object UpdatedFlags {
     def toUpdatedFlags(javaUpdatedFlags: JavaUpdatedFlags): UpdatedFlags = UpdatedFlags(
       javaUpdatedFlags.getUid,
+      javaUpdatedFlags.getMessageId.toScala,
       javaUpdatedFlags.getModSeq,
       Flags.fromJavaFlags(javaUpdatedFlags.getOldFlags),
       Flags.fromJavaFlags(javaUpdatedFlags.getNewFlags))
   }
 
-  case class UpdatedFlags(uid: MessageUid, modSeq: ModSeq, oldFlags: Flags, newFlags: Flags) {
+  case class UpdatedFlags(uid: MessageUid, messageId: Option[MessageId], modSeq: ModSeq, oldFlags: Flags, newFlags: Flags) {
     def toJava: JavaUpdatedFlags = JavaUpdatedFlags.builder()
       .uid(uid)
+      .messageId(messageId.toJava)
       .modSeq(modSeq)
       .oldFlags(Flags.toJavaFlags(oldFlags))
       .newFlags(Flags.toJavaFlags(newFlags))
diff --git a/mailbox/event/json/src/test/java/org/apache/james/event/json/FlagsUpdatedSerializationTest.java b/mailbox/event/json/src/test/java/org/apache/james/event/json/FlagsUpdatedSerializationTest.java
index 8a2347e..6cb82f3 100644
--- a/mailbox/event/json/src/test/java/org/apache/james/event/json/FlagsUpdatedSerializationTest.java
+++ b/mailbox/event/json/src/test/java/org/apache/james/event/json/FlagsUpdatedSerializationTest.java
@@ -39,7 +39,9 @@ import org.apache.james.mailbox.events.MailboxListener;
 import org.apache.james.mailbox.model.MailboxConstants;
 import org.apache.james.mailbox.model.MailboxId;
 import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.model.MessageId;
 import org.apache.james.mailbox.model.TestId;
+import org.apache.james.mailbox.model.TestMessageId;
 import org.apache.james.mailbox.model.UpdatedFlags;
 import org.junit.jupiter.api.Nested;
 import org.junit.jupiter.api.Test;
@@ -173,6 +175,76 @@ class FlagsUpdatedSerializationTest {
     }
 
     @Nested
+    class WithMessageId {
+        private final MessageId messageId1 = TestMessageId.of(23456);
+        private final MessageId messageId2 = TestMessageId.of(78901);
+
+        private final UpdatedFlags updatedFlagsWithMessageId1 = UpdatedFlags.builder()
+            .uid(MESSAGE_UID_1)
+            .messageId(messageId1)
+            .modSeq(MOD_SEQ_1)
+            .oldFlags(OLD_FLAGS_1)
+            .newFlags(NEW_FLAGS_1)
+            .build();
+        private final UpdatedFlags updatedFlagsWithMessageId2 = UpdatedFlags.builder()
+            .uid(MESSAGE_UID_2)
+            .messageId(messageId2)
+            .modSeq(MOD_SEQ_2)
+            .oldFlags(OLD_FLAGS_2)
+            .newFlags(NEW_FLAGS_2)
+            .build();
+
+        private final List<UpdatedFlags> updatedFlagsListWithMessageIds = ImmutableList.of(updatedFlagsWithMessageId1, updatedFlagsWithMessageId2);
+
+        private final MailboxListener.FlagsUpdated eventWithMessageIds = new MailboxListener.FlagsUpdated(SESSION_ID, USERNAME,
+            MAILBOX_PATH, MAILBOX_ID, updatedFlagsListWithMessageIds, EVENT_ID);
+
+        private static final String EVENT_WITH_MESSAGE_IDS_JSON =
+            "{" +
+                "  \"FlagsUpdated\": {" +
+                "    \"eventId\":\"6e0dd59d-660e-4d9b-b22f-0354479f47b4\"," +
+                "    \"path\": {" +
+                "      \"namespace\": \"#private\"," +
+                "      \"user\": \"user\"," +
+                "      \"name\": \"mailboxName\"" +
+                "    }," +
+                "    \"mailboxId\": \"18\"," +
+                "    \"sessionId\": 42," +
+                "    \"updatedFlags\": [" +
+                "      {" +
+                "        \"uid\": 123456," +
+                "        \"messageId\": \"23456\"," +
+                "        \"modSeq\": 35," +
+                "        \"oldFlags\": {\"systemFlags\":[\"Deleted\",\"Seen\"],\"userFlags\":[\"Old Flag 1\"]}," +
+                "        \"newFlags\": {\"systemFlags\":[\"Answered\",\"Draft\"],\"userFlags\":[\"New Flag 1\"]}" +
+                "      }," +
+                "      {" +
+                "        \"uid\": 654321," +
+                "        \"messageId\": \"78901\"," +
+                "        \"modSeq\": 36," +
+                "        \"oldFlags\": {\"systemFlags\":[\"Flagged\",\"Recent\"],\"userFlags\":[\"Old Flag 2\"]}," +
+                "        \"newFlags\": {\"systemFlags\":[\"Answered\",\"Seen\"],\"userFlags\":[\"New Flag 2\"]}" +
+                "      }" +
+                "    ]," +
+                "    \"user\": \"user\"" +
+                "  }" +
+                "}";
+
+        @Test
+        void flagsUpdatedShouldBeWellSerialized() {
+            assertThatJson(EVENT_SERIALIZER.toJson(eventWithMessageIds))
+                .when(Option.IGNORING_ARRAY_ORDER)
+                .isEqualTo(EVENT_WITH_MESSAGE_IDS_JSON);
+        }
+
+        @Test
+        void flagsUpdatedShouldBeWellDeSerialized() {
+            assertThat(EVENT_SERIALIZER.fromJson(EVENT_WITH_MESSAGE_IDS_JSON).get())
+                .isEqualTo(eventWithMessageIds);
+        }
+    }
+
+    @Nested
     class DeserializationError {
         @Test
         void flagsUpdatedShouldThrowWhenMissingSessionId() {


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