JAMES-2292 Introduce MailQueueItemDTO

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

Branch: refs/heads/master
Commit: 8d77e62043bbac0ac72d2d02c6b0e874828b7997
Parents: 2ecbdea
Author: Antoine Duprat <[email protected]>
Authored: Tue Jan 23 14:54:27 2018 +0100
Committer: benwa <[email protected]>
Committed: Thu Jan 25 11:46:04 2018 +0700

----------------------------------------------------------------------
 .../james/webadmin/dto/MailQueueItemDTO.java    | 125 +++++++++++++++++++
 .../webadmin/dto/MailQueueItemDTOTest.java      |  67 ++++++++++
 2 files changed, 192 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/8d77e620/server/protocols/webadmin/webadmin-mailqueue/src/main/java/org/apache/james/webadmin/dto/MailQueueItemDTO.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/webadmin/webadmin-mailqueue/src/main/java/org/apache/james/webadmin/dto/MailQueueItemDTO.java
 
b/server/protocols/webadmin/webadmin-mailqueue/src/main/java/org/apache/james/webadmin/dto/MailQueueItemDTO.java
new file mode 100644
index 0000000..d7ec0b8
--- /dev/null
+++ 
b/server/protocols/webadmin/webadmin-mailqueue/src/main/java/org/apache/james/webadmin/dto/MailQueueItemDTO.java
@@ -0,0 +1,125 @@
+/****************************************************************
+ * 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.webadmin.dto;
+
+import java.util.Collection;
+import java.util.Date;
+import java.util.List;
+import java.util.Optional;
+
+import org.apache.james.core.MailAddress;
+import org.apache.james.queue.api.MailQueue.MailQueueException;
+import org.apache.james.queue.api.ManageableMailQueue;
+
+import com.github.steveash.guavate.Guavate;
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+
+public class MailQueueItemDTO {
+
+    private static final long NO_NEXT_DELIVERY = -1;
+
+    public static Builder builder() {
+        return new Builder();
+    }
+
+    public static MailQueueItemDTO from(ManageableMailQueue.MailQueueItemView 
mailQueueItemView) throws MailQueueException {
+        return builder()
+                .name(mailQueueItemView.getMail().getName())
+                .sender(mailQueueItemView.getMail().getSender())
+                .recipients(mailQueueItemView.getMail().getRecipients())
+                .nextDelivery(nextDelivery(mailQueueItemView))
+                .build();
+    }
+
+    private static Optional<Date> 
nextDelivery(ManageableMailQueue.MailQueueItemView mailQueueItemView) {
+        long nextDelivery = mailQueueItemView.getNextDelivery();
+        if (nextDelivery == NO_NEXT_DELIVERY) {
+            return Optional.empty();
+        }
+        return Optional.of(new Date(nextDelivery));
+    }
+
+    public static class Builder {
+
+        private String name;
+        private String sender;
+        private List<String> recipients;
+        private Optional<Date> nextDelivery;
+
+        private Builder() {
+        }
+
+        public Builder name(String name) {
+            this.name = name;
+            return this;
+        }
+
+        public Builder sender(MailAddress sender) {
+            this.sender = sender.asString();
+            return this;
+        }
+
+        public Builder recipients(Collection<MailAddress> recipients) {
+            this.recipients = recipients.stream()
+                    .map(MailAddress::asString)
+                    .collect(Guavate.toImmutableList());
+            return this;
+        }
+
+        public Builder nextDelivery(Optional<Date> nextDelivery) {
+            this.nextDelivery = nextDelivery;
+            return this;
+        }
+
+        public MailQueueItemDTO build() {
+            Preconditions.checkArgument(!Strings.isNullOrEmpty(name), "name is 
mandatory");
+            return new MailQueueItemDTO(name, sender, recipients, 
nextDelivery);
+        }
+    }
+
+    private final String name;
+    private final String sender;
+    private final List<String> recipients;
+    private final Optional<Date> nextDelivery;
+
+    public MailQueueItemDTO(String name, String sender, List<String> 
recipients, Optional<Date> nextDelivery) {
+        this.name = name;
+        this.sender = sender;
+        this.recipients = recipients;
+        this.nextDelivery = nextDelivery;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public String getSender() {
+        return sender;
+    }
+
+    public List<String> getRecipients() {
+        return recipients;
+    }
+
+    public Optional<Date> getNextDelivery() {
+        return nextDelivery;
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/8d77e620/server/protocols/webadmin/webadmin-mailqueue/src/test/java/org/apache/james/webadmin/dto/MailQueueItemDTOTest.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/webadmin/webadmin-mailqueue/src/test/java/org/apache/james/webadmin/dto/MailQueueItemDTOTest.java
 
b/server/protocols/webadmin/webadmin-mailqueue/src/test/java/org/apache/james/webadmin/dto/MailQueueItemDTOTest.java
new file mode 100644
index 0000000..72d8ef7
--- /dev/null
+++ 
b/server/protocols/webadmin/webadmin-mailqueue/src/test/java/org/apache/james/webadmin/dto/MailQueueItemDTOTest.java
@@ -0,0 +1,67 @@
+/****************************************************************
+ * 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.webadmin.dto;
+
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.util.Date;
+import java.util.List;
+
+import org.apache.james.core.MailAddress;
+import org.apache.james.queue.api.Mails;
+import org.apache.james.queue.api.ManageableMailQueue.MailQueueItemView;
+import org.apache.mailet.base.test.FakeMail;
+import org.assertj.core.api.JUnitSoftAssertions;
+import org.junit.Rule;
+import org.junit.Test;
+
+import com.github.steveash.guavate.Guavate;
+
+public class MailQueueItemDTOTest {
+
+    @Rule
+    public final JUnitSoftAssertions softly = new JUnitSoftAssertions();
+
+    @Test
+    public void buildShouldThrowWhenNameIsNull() {
+        assertThatThrownBy(() -> MailQueueItemDTO.builder().build())
+            .isInstanceOf(IllegalArgumentException.class);
+    }
+
+    @Test
+    public void buildShouldThrowWhenNameIsEmpty() {
+        assertThatThrownBy(() -> MailQueueItemDTO.builder().name("").build())
+            .isInstanceOf(IllegalArgumentException.class);
+    }
+
+    @Test
+    public void fromShouldCreateTheRightObject() throws Exception {
+        FakeMail mail = Mails.defaultMail().build();
+        MailQueueItemView mailQueueItemView = new MailQueueItemView(mail, 4);
+        MailQueueItemDTO mailQueueItemDTO = 
MailQueueItemDTO.from(mailQueueItemView);
+        List<String> expectedRecipients = mail.getRecipients().stream()
+                .map(MailAddress::asString)
+                .collect(Guavate.toImmutableList());
+
+        
softly.assertThat(mailQueueItemDTO.getName()).isEqualTo(mail.getName());
+        
softly.assertThat(mailQueueItemDTO.getSender()).isEqualTo(mail.getSender().asString());
+        
softly.assertThat(mailQueueItemDTO.getRecipients()).isEqualTo(expectedRecipients);
+        softly.assertThat(mailQueueItemDTO.getNextDelivery()).contains(new 
Date(4));
+    }
+}


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

Reply via email to