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 2020/07/10 01:49:05 UTC
[james-project] 03/09: JAMES-3295 Add an AtMost matcher
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 4eb9d55bd7b556e236ffbd9f5f91c43f31b26b8b
Author: Rene Cordier <rc...@linagora.com>
AuthorDate: Mon Jul 6 16:19:15 2020 +0700
JAMES-3295 Add an AtMost matcher
---
.../apache/james/transport/matchers/AtMost.java | 73 ++++++++++
.../james/transport/matchers/AtMostTest.java | 158 +++++++++++++++++++++
2 files changed, 231 insertions(+)
diff --git a/mailet/standard/src/main/java/org/apache/james/transport/matchers/AtMost.java b/mailet/standard/src/main/java/org/apache/james/transport/matchers/AtMost.java
new file mode 100644
index 0000000..f3fee35
--- /dev/null
+++ b/mailet/standard/src/main/java/org/apache/james/transport/matchers/AtMost.java
@@ -0,0 +1,73 @@
+/****************************************************************
+ * 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 java.util.Optional;
+
+import javax.mail.MessagingException;
+
+import org.apache.james.core.MailAddress;
+import org.apache.mailet.Attribute;
+import org.apache.mailet.AttributeName;
+import org.apache.mailet.AttributeUtils;
+import org.apache.mailet.AttributeValue;
+import org.apache.mailet.Mail;
+import org.apache.mailet.base.GenericMatcher;
+import org.apache.mailet.base.MailetUtil;
+
+import com.google.common.collect.ImmutableList;
+
+/**
+ * Checks that a mail did at most X executions on a specific operation.
+ *
+ * If no executions have been performed previously, it sets up an attribute `AT_MOST_EXECUTIONS`
+ * in the mail that will be incremented every time the check succeeds.
+ *
+ * The check fails when the defined X limit is reached.
+ *
+ * <p>The example below will match a mail with at most 3 executions on the mailet</p>
+ *
+ * <pre><code>
+ * <mailet match="AtMost=3" class="<any-class>">
+ * </mailet>
+ * </code></pre>
+ */
+public class AtMost extends GenericMatcher {
+ static final AttributeName AT_MOST_EXECUTIONS = AttributeName.of("AT_MOST_EXECUTIONS");
+ private Integer atMostExecutions;
+
+ @Override
+ public void init() throws MessagingException {
+ this.atMostExecutions = MailetUtil.getInitParameterAsStrictlyPositiveInteger(getCondition());
+ }
+
+ @Override
+ public Collection<MailAddress> match(Mail mail) throws MessagingException {
+ return AttributeUtils.getValueAndCastFromMail(mail, AT_MOST_EXECUTIONS, Integer.class)
+ .or(() -> Optional.of(0))
+ .filter(executions -> executions < atMostExecutions)
+ .map(executions -> {
+ mail.setAttribute(new Attribute(AT_MOST_EXECUTIONS, AttributeValue.of(executions + 1)));
+ return mail.getRecipients();
+ })
+ .orElse(ImmutableList.of());
+ }
+}
diff --git a/mailet/standard/src/test/java/org/apache/james/transport/matchers/AtMostTest.java b/mailet/standard/src/test/java/org/apache/james/transport/matchers/AtMostTest.java
new file mode 100644
index 0000000..3be5f12
--- /dev/null
+++ b/mailet/standard/src/test/java/org/apache/james/transport/matchers/AtMostTest.java
@@ -0,0 +1,158 @@
+/****************************************************************
+ * 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.apache.james.transport.matchers.AtMost.AT_MOST_EXECUTIONS;
+import static org.apache.mailet.base.MailAddressFixture.RECIPIENT1;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.util.Collection;
+
+import javax.mail.MessagingException;
+
+import org.apache.james.core.MailAddress;
+import org.apache.mailet.Attribute;
+import org.apache.mailet.AttributeValue;
+import org.apache.mailet.Mail;
+import org.apache.mailet.base.test.FakeMail;
+import org.apache.mailet.base.test.FakeMatcherConfig;
+import org.assertj.core.api.SoftAssertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import com.github.fge.lambdas.Throwing;
+
+class AtMostTest {
+ private static final String CONDITION = "2";
+
+ private AtMost matcher;
+
+ private Mail createMail() throws MessagingException {
+ return FakeMail.builder()
+ .name("test-message")
+ .recipient(RECIPIENT1)
+ .build();
+ }
+
+ @BeforeEach
+ void setup() throws MessagingException {
+ this.matcher = new AtMost();
+ FakeMatcherConfig matcherConfig = FakeMatcherConfig.builder()
+ .matcherName("AtMost")
+ .condition(CONDITION)
+ .build();
+
+ matcher.init(matcherConfig);
+ }
+
+ @Test
+ void shouldMatchWhenAttributeNotSet() throws MessagingException {
+ Mail mail = createMail();
+
+ Collection<MailAddress> actual = matcher.match(mail);
+
+ assertThat(actual).containsOnly(RECIPIENT1);
+ }
+
+ @Test
+ void shouldMatchWhenNoRetries() throws MessagingException {
+ Mail mail = createMail();
+ mail.setAttribute(new Attribute(AT_MOST_EXECUTIONS, AttributeValue.of(0)));
+
+ Collection<MailAddress> actual = matcher.match(mail);
+
+ assertThat(actual).containsOnly(RECIPIENT1);
+ }
+
+ @Test
+ void shouldNotMatchWhenOverAtMost() throws MessagingException {
+ Mail mail = createMail();
+ mail.setAttribute(new Attribute(AT_MOST_EXECUTIONS, AttributeValue.of(3)));
+
+ Collection<MailAddress> actual = matcher.match(mail);
+
+ assertThat(actual).isEmpty();
+ }
+
+ @Test
+ void shouldNotMatchWhenEqualToAtMost() throws MessagingException {
+ Mail mail = createMail();
+ mail.setAttribute(new Attribute(AT_MOST_EXECUTIONS, AttributeValue.of(2)));
+
+ Collection<MailAddress> actual = matcher.match(mail);
+
+ assertThat(actual).isEmpty();
+ }
+
+ @Test
+ void shouldThrowWithEmptyCondition() {
+ FakeMatcherConfig matcherConfig = FakeMatcherConfig.builder()
+ .matcherName("AtMost")
+ .build();
+
+ assertThatThrownBy(() -> matcher.init(matcherConfig))
+ .isInstanceOf(MessagingException.class);
+ }
+
+ @Test
+ void shouldThrowWithInvalidCondition() {
+ FakeMatcherConfig matcherConfig = FakeMatcherConfig.builder()
+ .matcherName("AtMost")
+ .condition("invalid")
+ .build();
+
+ assertThatThrownBy(() -> matcher.init(matcherConfig))
+ .isInstanceOf(MessagingException.class);
+ }
+
+ @Test
+ void shouldThrowWithNegativeCondition() {
+ FakeMatcherConfig matcherConfig = FakeMatcherConfig.builder()
+ .matcherName("AtMost")
+ .condition("-1")
+ .build();
+
+ assertThatThrownBy(() -> matcher.init(matcherConfig))
+ .isInstanceOf(MessagingException.class);
+ }
+
+ @Test
+ void shouldThrowWithConditionToZero() {
+ FakeMatcherConfig matcherConfig = FakeMatcherConfig.builder()
+ .matcherName("AtMost")
+ .condition("0")
+ .build();
+
+ assertThatThrownBy(() -> matcher.init(matcherConfig))
+ .isInstanceOf(MessagingException.class);
+ }
+
+ @Test
+ void shouldMatchUntilOverAtMost() throws MessagingException {
+ Mail mail = createMail();
+
+ SoftAssertions.assertSoftly(Throwing.consumer(softly -> {
+ softly.assertThat(matcher.match(mail)).describedAs("First execution").contains(RECIPIENT1);
+ softly.assertThat(matcher.match(mail)).describedAs("Second execution").contains(RECIPIENT1);
+ softly.assertThat(matcher.match(mail)).describedAs("Third execution").isEmpty();
+ }));
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org