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


The following commit(s) were added to refs/heads/master by this push:
     new 447685feec [ENHANCEMENT] Provide a core RecipientCountExceeds matcher 
(#2534)
447685feec is described below

commit 447685feec7cc6344e34095e31d42bd60a668f67
Author: Benoit TELLIER <[email protected]>
AuthorDate: Sat Nov 30 08:08:42 2024 +0100

    [ENHANCEMENT] Provide a core RecipientCountExceeds matcher (#2534)
---
 .../servers/partials/RecipientCountExceeds.adoc    |  9 ++
 .../servers/partials/configure/matchers.adoc       |  2 +
 .../transport/matchers/RecipientCountExceeds.java  | 59 ++++++++++++++
 .../matchers/RecipientCountExceedsTest.java        | 95 ++++++++++++++++++++++
 4 files changed, 165 insertions(+)

diff --git a/docs/modules/servers/partials/RecipientCountExceeds.adoc 
b/docs/modules/servers/partials/RecipientCountExceeds.adoc
new file mode 100644
index 0000000000..dc603edb1a
--- /dev/null
+++ b/docs/modules/servers/partials/RecipientCountExceeds.adoc
@@ -0,0 +1,9 @@
+=== RecipientCountExceeds
+
+Matches mail with more recipients than the configured limit.
+
+Eg:
+
+....
+<mailet match="RecipientCountExceeds=36"; class="Null"\>
+....
\ No newline at end of file
diff --git a/docs/modules/servers/partials/configure/matchers.adoc 
b/docs/modules/servers/partials/configure/matchers.adoc
index 8d7915949c..3b787e2a40 100644
--- a/docs/modules/servers/partials/configure/matchers.adoc
+++ b/docs/modules/servers/partials/configure/matchers.adoc
@@ -57,6 +57,8 @@ include::partial$IsSMIMESigned.adoc[]
 
 include::partial$IsX509CertificateSubject.adoc[]
 
+include::partial$RecipientCountExceeds.adoc[]
+
 include::partial$RecipientDomainIs.adoc[]
 
 include::partial$RecipientIs.adoc[]
diff --git 
a/mailet/standard/src/main/java/org/apache/james/transport/matchers/RecipientCountExceeds.java
 
b/mailet/standard/src/main/java/org/apache/james/transport/matchers/RecipientCountExceeds.java
new file mode 100644
index 0000000000..f3e21a84cf
--- /dev/null
+++ 
b/mailet/standard/src/main/java/org/apache/james/transport/matchers/RecipientCountExceeds.java
@@ -0,0 +1,59 @@
+/****************************************************************
+ * 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.transport.matchers;
+
+import java.util.Collection;
+
+import jakarta.mail.MessagingException;
+
+import org.apache.james.core.MailAddress;
+import org.apache.mailet.Mail;
+import org.apache.mailet.base.GenericMatcher;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+
+/**
+ * Matches mail with more recipients than the configured limit.
+ *
+ * Eg:
+ *
+ * <mailet match="RecipientCountExceeds=36" class="Null"/>
+ */
+public class RecipientCountExceeds extends GenericMatcher {
+    private static final int DEFAULT_RECIPIENT_COUNT = 50;
+
+    private int recipientCount = DEFAULT_RECIPIENT_COUNT;
+
+    @Override
+    public void init() throws MessagingException {
+        int count = Integer.parseInt(getMatcherConfig().getCondition());
+        Preconditions.checkArgument(count > 0, "Argument must be a strictly 
positive integer");
+        recipientCount = count;
+    }
+
+    @Override
+    public Collection<MailAddress> match(Mail mail) throws MessagingException {
+        if (mail.getRecipients().size() > recipientCount) {
+            return mail.getRecipients();
+        }
+        return ImmutableList.of();
+    }
+}
diff --git 
a/mailet/standard/src/test/java/org/apache/james/transport/matchers/RecipientCountExceedsTest.java
 
b/mailet/standard/src/test/java/org/apache/james/transport/matchers/RecipientCountExceedsTest.java
new file mode 100644
index 0000000000..405d807b08
--- /dev/null
+++ 
b/mailet/standard/src/test/java/org/apache/james/transport/matchers/RecipientCountExceedsTest.java
@@ -0,0 +1,95 @@
+/****************************************************************
+ * 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.transport.matchers;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import org.apache.james.core.MailAddress;
+import org.apache.mailet.base.test.FakeMail;
+import org.apache.mailet.base.test.FakeMatcherConfig;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+class RecipientCountExceedsTest {
+    private RecipientCountExceeds testee;
+    private MailAddress mailAddress1;
+    private MailAddress mailAddress2;
+    private MailAddress mailAddress3;
+
+    @BeforeEach
+    public void setUp() throws Exception {
+        testee = new RecipientCountExceeds();
+        testee.init(FakeMatcherConfig.builder()
+            .matcherName("RecipientCountExceeds")
+            .condition("2")
+            .build());
+        mailAddress1 = new MailAddress("[email protected]");
+        mailAddress2 = new MailAddress("[email protected]");
+        mailAddress3 = new MailAddress("[email protected]");
+    }
+
+    @Test
+    void shouldNotMatchWhenExactlyThreshold() throws Exception {
+        FakeMail mail = FakeMail.builder()
+            .name("mail")
+            .recipients(mailAddress1, mailAddress2)
+            .build();
+
+        assertThat(testee.match(mail)).isEmpty();
+    }
+
+    @Test
+    void shouldMatchWhenThresholdExceeded() throws Exception {
+        FakeMail mail = FakeMail.builder()
+            .name("mail")
+            .recipients(mailAddress1, mailAddress2, mailAddress3)
+            .build();
+
+        assertThat(testee.match(mail)).containsOnly(mailAddress1, 
mailAddress2, mailAddress3);
+    }
+
+    @Test
+    void initShouldThrowWhenZero() throws Exception {
+        assertThatThrownBy(() -> new 
RecipientCountExceeds().init(FakeMatcherConfig.builder()
+            .condition("0")
+            .matcherName("RecipientCountExceeds")
+            .build()))
+            .isInstanceOf(IllegalArgumentException.class);
+    }
+
+    @Test
+    void initShouldThrowWhenInvalid() throws Exception {
+        assertThatThrownBy(() -> new 
RecipientCountExceeds().init(FakeMatcherConfig.builder()
+            .condition("abc")
+            .matcherName("RecipientCountExceeds")
+            .build()))
+            .isInstanceOf(NumberFormatException.class);
+    }
+
+    @Test
+    void initShouldThrowWhenNegativeNumber() throws Exception {
+        assertThatThrownBy(() -> new 
RecipientCountExceeds().init(FakeMatcherConfig.builder()
+            .condition("-1")
+            .matcherName("RecipientCountExceeds")
+            .build()))
+            .isInstanceOf(IllegalArgumentException.class);
+    }
+}
\ No newline at end of file


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

Reply via email to