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 7f9a7cb7b701c7da260e1c64c1620d615bfb971c
Author: Benoit Tellier <btell...@linagora.com>
AuthorDate: Tue Jun 18 12:12:25 2019 +0700

    JAMES-2794 Refactor DeleteCondition to implement WithEnqueueId
    
    We need to apply the predicates against EnqueuedItem POJO to gain genericity
---
 .../queue/rabbitmq/view/api/DeleteCondition.java   |  44 ++++----
 .../view/cassandra/CassandraMailQueueView.java     |   2 +-
 .../rabbitmq/view/api/DeleteConditionTest.java     | 111 +++++++++++++++++----
 3 files changed, 118 insertions(+), 39 deletions(-)

diff --git 
a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/view/api/DeleteCondition.java
 
b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/view/api/DeleteCondition.java
index 4b640fa..aa3973a 100644
--- 
a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/view/api/DeleteCondition.java
+++ 
b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/view/api/DeleteCondition.java
@@ -24,17 +24,17 @@ import java.util.Objects;
 import org.apache.commons.lang3.NotImplementedException;
 import org.apache.james.queue.api.ManageableMailQueue;
 import org.apache.james.queue.rabbitmq.EnQueueId;
-import org.apache.mailet.Mail;
+import org.apache.james.queue.rabbitmq.EnqueuedItem;
 
 import com.google.common.base.Preconditions;
 
 public interface DeleteCondition {
-    boolean shouldBeDeleted(Mail mail);
+    boolean shouldBeDeleted(EnqueuedItem enqueuedItem);
 
     class All implements DeleteCondition {
         @Override
-        public boolean shouldBeDeleted(Mail mail) {
-            Preconditions.checkNotNull(mail);
+        public boolean shouldBeDeleted(EnqueuedItem enqueuedItem) {
+            Preconditions.checkNotNull(enqueuedItem);
             return true;
         }
 
@@ -57,9 +57,10 @@ public interface DeleteCondition {
         }
 
         @Override
-        public boolean shouldBeDeleted(Mail mail) {
-            Preconditions.checkNotNull(mail);
-            return mail.getMaybeSender()
+        public boolean shouldBeDeleted(EnqueuedItem enqueuedItem) {
+            Preconditions.checkNotNull(enqueuedItem);
+            return enqueuedItem.getMail()
+                .getMaybeSender()
                 .asString()
                 .equals(senderAsString);
         }
@@ -88,9 +89,11 @@ public interface DeleteCondition {
         }
 
         @Override
-        public boolean shouldBeDeleted(Mail mail) {
-            Preconditions.checkNotNull(mail);
-            return mail.getName().equals(name);
+        public boolean shouldBeDeleted(EnqueuedItem enqueuedItem) {
+            Preconditions.checkNotNull(enqueuedItem);
+            return enqueuedItem.getMail()
+                .getName()
+                .equals(name);
         }
 
         @Override
@@ -112,8 +115,7 @@ public interface DeleteCondition {
     class WithEnqueueId implements DeleteCondition {
         private final EnQueueId enQueueId;
 
-
-        public WithEnqueueId(EnQueueId enQueueId) {
+        WithEnqueueId(EnQueueId enQueueId) {
             this.enQueueId = enQueueId;
         }
 
@@ -122,8 +124,9 @@ public interface DeleteCondition {
         }
 
         @Override
-        public boolean shouldBeDeleted(Mail mail) {
-            throw new NotImplementedException("EnQueueId is not carried as a 
Mail property");
+        public boolean shouldBeDeleted(EnqueuedItem enqueuedItem) {
+            Preconditions.checkNotNull(enqueuedItem);
+            return enqueuedItem.getEnQueueId().equals(enQueueId);
         }
     }
 
@@ -135,9 +138,11 @@ public interface DeleteCondition {
         }
 
         @Override
-        public boolean shouldBeDeleted(Mail mail) {
-            Preconditions.checkNotNull(mail);
-            return mail.getRecipients()
+        public boolean shouldBeDeleted(EnqueuedItem enqueuedItem) {
+            Preconditions.checkNotNull(enqueuedItem);
+            return enqueuedItem
+                .getMail()
+                .getRecipients()
                 .stream()
                 .anyMatch(mailAddress -> 
mailAddress.asString().equals(recipientAsString));
         }
@@ -186,6 +191,11 @@ public interface DeleteCondition {
         return new WithName(value);
     }
 
+    static WithEnqueueId withEnqueueId(EnQueueId value) {
+        Preconditions.checkNotNull(value);
+        return new WithEnqueueId(value);
+    }
+
     static DeleteCondition all() {
         return new All();
     }
diff --git 
a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/view/cassandra/CassandraMailQueueView.java
 
b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/view/cassandra/CassandraMailQueueView.java
index 15fd0fc..a2cabda 100644
--- 
a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/view/cassandra/CassandraMailQueueView.java
+++ 
b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/view/cassandra/CassandraMailQueueView.java
@@ -111,7 +111,7 @@ public class CassandraMailQueueView implements 
MailQueueView {
     private long browseThenDelete(DeleteCondition deleteCondition) {
         return cassandraMailQueueBrowser.browseReferences(mailQueueName)
             .map(EnqueuedItemWithSlicingContext::getEnqueuedItem)
-            .filter(mailReference -> 
deleteCondition.shouldBeDeleted(mailReference.getMail()))
+            .filter(deleteCondition::shouldBeDeleted)
             .flatMap(mailReference -> 
cassandraMailQueueMailDelete.considerDeleted(mailReference.getEnQueueId(), 
mailQueueName))
             .count()
             .doOnNext(ignored -> 
cassandraMailQueueMailDelete.updateBrowseStart(mailQueueName))
diff --git 
a/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/view/api/DeleteConditionTest.java
 
b/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/view/api/DeleteConditionTest.java
index b559aa7..d7d34fa 100644
--- 
a/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/view/api/DeleteConditionTest.java
+++ 
b/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/view/api/DeleteConditionTest.java
@@ -22,8 +22,15 @@ package org.apache.james.queue.rabbitmq.view.api;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
 
+import java.time.Instant;
+
+import org.apache.james.blob.api.HashBlobId;
+import org.apache.james.blob.mail.MimeMessagePartsId;
 import org.apache.james.core.MailAddress;
 import org.apache.james.queue.api.ManageableMailQueue;
+import org.apache.james.queue.rabbitmq.EnQueueId;
+import org.apache.james.queue.rabbitmq.EnqueuedItem;
+import org.apache.james.queue.rabbitmq.MailQueueName;
 import org.apache.mailet.base.test.FakeMail;
 import org.junit.jupiter.api.Nested;
 import org.junit.jupiter.api.Test;
@@ -35,6 +42,14 @@ class DeleteConditionTest {
     private static final String ADDRESS_2 = "a...@toto.com";
     private static final String NAME = "name";
     private static final String VALUE = "value";
+    private static final EnQueueId EN_QUEUE_ID_1 = 
EnQueueId.ofSerialized("110e8400-e29b-11d4-a716-446655440000");
+    private static final EnQueueId EN_QUEUE_ID_2 = 
EnQueueId.ofSerialized("464765a0-e4e7-11e4-aba4-710c1de3782b");
+    private static final MailQueueName OUT_GOING_1 = 
MailQueueName.fromString("OUT_GOING_1");
+    private static final Instant ENQUEUE_TIME = Instant.now();
+    private static final MimeMessagePartsId MESSAGE_PARTS_ID = 
MimeMessagePartsId.builder()
+        .headerBlobId(new HashBlobId.Factory().from("headerBlobId"))
+        .bodyBlobId(new HashBlobId.Factory().from("bodyBlobId"))
+        .build();
 
     @Nested
     class AllTest {
@@ -42,7 +57,7 @@ class DeleteConditionTest {
         void allShouldReturnTrue() throws Exception {
             assertThat(
                 DeleteCondition.all()
-                    .shouldBeDeleted(FakeMail.builder().name("name").build()))
+                    
.shouldBeDeleted(enQueueItemForMail(FakeMail.builder().name("name").build())))
                 .isTrue();
         }
 
@@ -61,6 +76,50 @@ class DeleteConditionTest {
     }
 
     @Nested
+    class WithEnQueueIdTest {
+        @Test
+        void withSenderShouldThrowOnNullCondition() {
+            assertThatThrownBy(() ->
+                DeleteCondition.withEnqueueId(null))
+                .isInstanceOf(NullPointerException.class);
+        }
+
+        @Test
+        void shouldBeDeletedShouldReturnTrueWhenSameId() throws Exception {
+            EnqueuedItem enqueuedItem = EnqueuedItem.builder()
+                .enQueueId(EN_QUEUE_ID_1)
+                .mailQueueName(OUT_GOING_1)
+                .mail(FakeMail.builder()
+                    .name("name")
+                    .sender(ADDRESS)
+                    .build())
+                .enqueuedTime(ENQUEUE_TIME)
+                .mimeMessagePartsId(MESSAGE_PARTS_ID)
+                .build();
+
+            
assertThat(DeleteCondition.withEnqueueId(EN_QUEUE_ID_1).shouldBeDeleted(enqueuedItem))
+                .isTrue();
+        }
+
+        @Test
+        void shouldBeDeletedShouldReturnFalseWhenDifferentId() throws 
Exception {
+            EnqueuedItem enqueuedItem = EnqueuedItem.builder()
+                .enQueueId(EN_QUEUE_ID_2)
+                .mailQueueName(OUT_GOING_1)
+                .mail(FakeMail.builder()
+                    .name("name")
+                    .sender(ADDRESS)
+                    .build())
+                .enqueuedTime(ENQUEUE_TIME)
+                .mimeMessagePartsId(MESSAGE_PARTS_ID)
+                .build();
+
+            
assertThat(DeleteCondition.withEnqueueId(EN_QUEUE_ID_1).shouldBeDeleted(enqueuedItem))
+                .isFalse();
+        }
+    }
+
+    @Nested
     class WithSenderTest {
         @Test
         void withSenderShouldThrowOnNullCondition() {
@@ -81,10 +140,10 @@ class DeleteConditionTest {
         void withSenderShouldReturnTrueWhenSameAddress() throws Exception {
             assertThat(
                 DeleteCondition.withSender(ADDRESS)
-                    .shouldBeDeleted(FakeMail.builder()
+                    .shouldBeDeleted(enQueueItemForMail(FakeMail.builder()
                         .name("name")
                         .sender(ADDRESS)
-                        .build()))
+                        .build())))
                 .isTrue();
         }
 
@@ -92,11 +151,11 @@ class DeleteConditionTest {
         void withSenderShouldReturnFalseWhenDifferentAddress() throws 
Exception {
             assertThat(
                 DeleteCondition.withSender(ADDRESS)
-                    .shouldBeDeleted(FakeMail.builder()
+                    .shouldBeDeleted(enQueueItemForMail(FakeMail.builder()
                         .name("name")
                         .sender(ADDRESS_2)
                         .recipient(ADDRESS)
-                        .build()))
+                        .build())))
                 .isFalse();
         }
 
@@ -104,10 +163,10 @@ class DeleteConditionTest {
         void withSenderShouldNotThrowOnNullSender() throws Exception {
             assertThat(
                 DeleteCondition.withSender(ADDRESS)
-                    .shouldBeDeleted(FakeMail.builder()
+                    .shouldBeDeleted(enQueueItemForMail(FakeMail.builder()
                         .name("name")
                         .sender(MailAddress.nullSender())
-                        .build()))
+                        .build())))
                 .isFalse();
         }
 
@@ -115,10 +174,10 @@ class DeleteConditionTest {
         void withSenderShouldAllowNullSenderMatchingNullSender() throws 
Exception {
             assertThat(
                 DeleteCondition.withSender(MailAddress.NULL_SENDER_AS_STRING)
-                    .shouldBeDeleted(FakeMail.builder()
+                    .shouldBeDeleted(enQueueItemForMail(FakeMail.builder()
                         .name("name")
                         .sender(MailAddress.nullSender())
-                        .build()))
+                        .build())))
                 .isTrue();
         }
 
@@ -149,9 +208,9 @@ class DeleteConditionTest {
         void withNameShouldReturnTrueWhenSameName() throws Exception {
             assertThat(
                 DeleteCondition.withName(NAME)
-                    .shouldBeDeleted(FakeMail.builder()
+                    .shouldBeDeleted(enQueueItemForMail(FakeMail.builder()
                         .name(NAME)
-                        .build()))
+                        .build())))
                 .isTrue();
         }
 
@@ -159,9 +218,9 @@ class DeleteConditionTest {
         void withSenderShouldReturnFalseWhenDifferentAddress() throws 
Exception {
             assertThat(
                 DeleteCondition.withName(NAME)
-                    .shouldBeDeleted(FakeMail.builder()
+                    .shouldBeDeleted(enQueueItemForMail(FakeMail.builder()
                         .name("other")
-                        .build()))
+                        .build())))
                 .isFalse();
         }
 
@@ -192,10 +251,10 @@ class DeleteConditionTest {
         void withRecipientShouldReturnTrueWhenSameAddress() throws Exception {
             assertThat(
                 DeleteCondition.withRecipient(ADDRESS)
-                    .shouldBeDeleted(FakeMail.builder()
+                    .shouldBeDeleted(enQueueItemForMail(FakeMail.builder()
                         .name("name")
                         .recipient(ADDRESS)
-                        .build()))
+                        .build())))
                 .isTrue();
         }
 
@@ -203,10 +262,10 @@ class DeleteConditionTest {
         void withRecipientShouldReturnTrueWhenAtListOneMatches() throws 
Exception {
             assertThat(
                 DeleteCondition.withRecipient(ADDRESS)
-                    .shouldBeDeleted(FakeMail.builder()
+                    .shouldBeDeleted(enQueueItemForMail(FakeMail.builder()
                         .name("name")
                         .recipients(ADDRESS, ADDRESS_2)
-                        .build()))
+                        .build())))
                 .isTrue();
         }
 
@@ -214,11 +273,11 @@ class DeleteConditionTest {
         void withRecipientShouldReturnFalseWhenDifferentAddress() throws 
Exception {
             assertThat(
                 DeleteCondition.withRecipient(ADDRESS)
-                    .shouldBeDeleted(FakeMail.builder()
+                    .shouldBeDeleted(enQueueItemForMail(FakeMail.builder()
                         .name("name")
                         .sender(ADDRESS)
                         .recipient(ADDRESS_2)
-                        .build()))
+                        .build())))
                 .isFalse();
         }
 
@@ -226,9 +285,9 @@ class DeleteConditionTest {
         void withRecipientShouldReturnFalseWhenNoRecipient() throws Exception {
             assertThat(
                 DeleteCondition.withRecipient(ADDRESS)
-                    .shouldBeDeleted(FakeMail.builder()
+                    .shouldBeDeleted(enQueueItemForMail(FakeMail.builder()
                         .name("name")
-                        .build()))
+                        .build())))
                 .isFalse();
         }
 
@@ -258,4 +317,14 @@ class DeleteConditionTest {
                 .isEqualTo(DeleteCondition.withName(VALUE));
         }
     }
+
+    private EnqueuedItem enQueueItemForMail(FakeMail mail) {
+        return EnqueuedItem.builder()
+            .enQueueId(EN_QUEUE_ID_1)
+            .mailQueueName(OUT_GOING_1)
+            .mail(mail)
+            .enqueuedTime(ENQUEUE_TIME)
+            .mimeMessagePartsId(MESSAGE_PARTS_ID)
+            .build();
+    }
 }
\ No newline at end of file


---------------------------------------------------------------------
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