You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2019/12/06 02:34:14 UTC

[james-project] 04/21: JAMES-2992 Move Emailer factory methods inside Emailer

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 d22b91ea7eb38fe8d92e4be19ef21028fc37c58f
Author: Tran Tien Duc <dt...@linagora.com>
AuthorDate: Mon Dec 2 11:23:29 2019 +0700

    JAMES-2992 Move Emailer factory methods inside Emailer
    
    the factory methods are took from the MessageViewFactory in order to
    simplify the factory
---
 .../org/apache/james/jmap/draft/model/Emailer.java | 42 ++++++++++++++++
 .../apache/james/jmap/draft/model/EmailerTest.java | 58 ++++++++++++++++++++++
 2 files changed, 100 insertions(+)

diff --git a/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/model/Emailer.java b/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/model/Emailer.java
index d895633..9060c76 100644
--- a/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/model/Emailer.java
+++ b/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/model/Emailer.java
@@ -19,12 +19,16 @@
 
 package org.apache.james.jmap.draft.model;
 
+import java.util.List;
 import java.util.Objects;
 import java.util.Optional;
 
 import javax.mail.internet.AddressException;
 
 import org.apache.james.core.MailAddress;
+import org.apache.james.mime4j.dom.address.AddressList;
+import org.apache.james.mime4j.dom.address.Mailbox;
+import org.apache.james.mime4j.dom.address.MailboxList;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -32,15 +36,53 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
 import com.fasterxml.jackson.annotation.JsonProperty;
 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
 import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
+import com.github.steveash.guavate.Guavate;
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.MoreObjects;
 import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
 
 @JsonDeserialize(builder = Emailer.Builder.class)
 public class Emailer {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(Emailer.class);
 
+    public static List<Emailer> fromAddressList(AddressList list) {
+        if (list == null) {
+            return ImmutableList.of();
+        }
+        return list.flatten()
+            .stream()
+            .map(Emailer::fromMailbox)
+            .collect(Guavate.toImmutableList());
+    }
+
+    public static Emailer firstFromMailboxList(MailboxList list) {
+        if (list == null) {
+            return null;
+        }
+        return list.stream()
+            .map(Emailer::fromMailbox)
+            .findFirst()
+            .orElse(null);
+    }
+
+    private static Emailer fromMailbox(Mailbox mailbox) {
+        return Emailer.builder()
+            .name(getNameOrAddress(mailbox))
+            .email(mailbox.getAddress())
+            .allowInvalid()
+            .build();
+    }
+
+    private static String getNameOrAddress(Mailbox mailbox) {
+        if (mailbox.getName() != null) {
+            return mailbox.getName();
+        }
+        return mailbox.getAddress();
+    }
+
+
     public static Builder builder() {
         return new Builder();
     }
diff --git a/server/protocols/jmap-draft/src/test/java/org/apache/james/jmap/draft/model/EmailerTest.java b/server/protocols/jmap-draft/src/test/java/org/apache/james/jmap/draft/model/EmailerTest.java
index a2b570d..f056d0c 100644
--- a/server/protocols/jmap-draft/src/test/java/org/apache/james/jmap/draft/model/EmailerTest.java
+++ b/server/protocols/jmap-draft/src/test/java/org/apache/james/jmap/draft/model/EmailerTest.java
@@ -22,6 +22,9 @@ import static org.assertj.core.api.Assertions.assertThat;
 
 import java.util.Optional;
 
+import org.apache.james.mime4j.dom.address.AddressList;
+import org.apache.james.mime4j.dom.address.Mailbox;
+import org.apache.james.mime4j.dom.address.MailboxList;
 import org.junit.Test;
 
 public class EmailerTest {
@@ -111,4 +114,59 @@ public class EmailerTest {
 
         assertThat(actual).isEqualToComparingFieldByField(expected);
     }
+
+    @Test
+    public void fromAddressListShouldReturnEmptyWhenNullAddress() {
+        assertThat(Emailer.fromAddressList(null))
+            .isEmpty();
+    }
+
+    @Test
+    public void fromAddressListShouldReturnListOfEmailersContainingAddresses() {
+        assertThat(Emailer.fromAddressList(new AddressList(
+                new Mailbox("user1", "james.org"),
+                new Mailbox("user2", "james.org"))))
+            .containsExactly(
+                Emailer.builder()
+                    .name("user1@james.org")
+                    .email("user1@james.org")
+                    .build(),
+                Emailer.builder()
+                    .name("user2@james.org")
+                    .email("user2@james.org")
+                    .build());
+    }
+
+    @Test
+    public void fromAddressListShouldReturnListOfEmailersContainingAddressesWithNames() {
+        assertThat(Emailer.fromAddressList(new AddressList(
+                new Mailbox("myInbox", "user1", "james.org"),
+                new Mailbox("hisInbox", "user2", "james.org"))))
+            .containsExactly(
+                Emailer.builder()
+                    .name("myInbox")
+                    .email("user1@james.org")
+                    .build(),
+                Emailer.builder()
+                    .name("hisInbox")
+                    .email("user2@james.org")
+                    .build());
+    }
+
+    @Test
+    public void firstFromMailboxListShouldReturnNullWhenNullMailboxList() {
+        assertThat(Emailer.firstFromMailboxList(null))
+            .isNull();
+    }
+
+    @Test
+    public void firstFromMailboxListShouldReturnTheFirstAddressInList() {
+        assertThat(Emailer.firstFromMailboxList(new MailboxList(
+                new Mailbox("user1Inbox", "user1", "james.org"),
+                new Mailbox("user2Inbox", "user2", "james.org"))))
+            .isEqualTo(Emailer.builder()
+                .name("user1Inbox")
+                .email("user1@james.org")
+                .build());
+    }
 }


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