This is an automated email from the ASF dual-hosted git repository. rcordier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 9fe3b6465b24108514be8f59511171f326e9ab14 Author: Benoit Tellier <btell...@linagora.com> AuthorDate: Tue Dec 3 15:52:13 2019 +0700 JAMES-2952 SieveExecutor::handleFailure should position SIEVE_NOTIFICATION attribute This prevents double recomputation... --- .../apache/mailet/base/test/FakeMailContext.java | 3 + .../mailets/jsieve/delivery/SieveExecutor.java | 17 ++++-- .../mailets/delivery/SieveIntegrationTest.java | 1 + .../mailets/jsieve/delivery/SieveExecutorTest.java | 66 ++++++++++++++++++++++ 4 files changed, 83 insertions(+), 4 deletions(-) diff --git a/mailet/test/src/main/java/org/apache/mailet/base/test/FakeMailContext.java b/mailet/test/src/main/java/org/apache/mailet/base/test/FakeMailContext.java index 48090ec..2f59236 100644 --- a/mailet/test/src/main/java/org/apache/mailet/base/test/FakeMailContext.java +++ b/mailet/test/src/main/java/org/apache/mailet/base/test/FakeMailContext.java @@ -251,6 +251,9 @@ public class FakeMailContext implements MailetContext { return subject; } + public Map<AttributeName, Attribute> getAttributes() { + return ImmutableMap.copyOf(attributes); + } public Optional<Delay> getDelay() { return delay; diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/jsieve/delivery/SieveExecutor.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/jsieve/delivery/SieveExecutor.java index cdcc51d..a06999a 100644 --- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/jsieve/delivery/SieveExecutor.java +++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/jsieve/delivery/SieveExecutor.java @@ -26,6 +26,7 @@ import javax.mail.MessagingException; import org.apache.commons.logging.Log; import org.apache.james.core.MailAddress; +import org.apache.james.server.core.MailImpl; import org.apache.james.sieverepository.api.exception.ScriptNotFoundException; import org.apache.james.transport.mailets.jsieve.ActionDispatcher; import org.apache.james.transport.mailets.jsieve.ResourceLocator; @@ -45,8 +46,8 @@ import org.apache.mailet.MailetContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; -import com.google.common.collect.ImmutableList; public class SieveExecutor { private static final Logger LOGGER = LoggerFactory.getLogger(SieveExecutor.class); @@ -154,8 +155,16 @@ public class SieveExecutor { } } - protected void handleFailure(MailAddress recipient, Mail aMail, Exception ex) throws MessagingException, IOException { - aMail.setAttribute(new Attribute(SIEVE_NOTIFICATION, AttributeValue.of(true))); - mailetContext.sendMail(recipient, ImmutableList.of(recipient), SieveFailureMessageComposer.composeMessage(aMail, ex, recipient.toString())); + @VisibleForTesting + void handleFailure(MailAddress recipient, Mail aMail, Exception ex) throws MessagingException, IOException { + MailImpl errorMail = MailImpl.builder() + .name(MailImpl.getId()) + .addAttribute(new Attribute(SIEVE_NOTIFICATION, AttributeValue.of(true))) + .sender(recipient) + .addRecipient(recipient) + .mimeMessage(SieveFailureMessageComposer.composeMessage(aMail, ex, recipient.toString())) + .build(); + + mailetContext.sendMail(errorMail); } } diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/SieveIntegrationTest.java b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/SieveIntegrationTest.java index fc03502..a7a0aea 100644 --- a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/SieveIntegrationTest.java +++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/delivery/SieveIntegrationTest.java @@ -810,6 +810,7 @@ public class SieveIntegrationTest { .recipient(RECEIVER_DOMAIN_COM) .sender(new MailAddress(RECEIVER_DOMAIN_COM)) .fromMailet() + .attribute(new Attribute(SieveExecutor.SIEVE_NOTIFICATION, AttributeValue.of(true))) .build()); // No action taken assertThat(mail.getAttribute(ATTRIBUTE_NAME)).isEmpty(); diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/jsieve/delivery/SieveExecutorTest.java b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/jsieve/delivery/SieveExecutorTest.java new file mode 100644 index 0000000..6ed0d99 --- /dev/null +++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/jsieve/delivery/SieveExecutorTest.java @@ -0,0 +1,66 @@ +/**************************************************************** + * 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.mailets.jsieve.delivery; + +import static org.apache.james.transport.mailets.jsieve.delivery.SieveExecutor.SIEVE_NOTIFICATION; +import static org.apache.mailet.base.MailAddressFixture.RECIPIENT1; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; + +import org.apache.commons.logging.Log; +import org.apache.james.core.builder.MimeMessageBuilder; +import org.apache.james.server.core.MailImpl; +import org.apache.james.transport.mailets.jsieve.ResourceLocator; +import org.apache.mailet.base.test.FakeMailContext; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class SieveExecutorTest { + SieveExecutor testee; + FakeMailContext mailetContext; + + @BeforeEach + void setUp() throws Exception { + mailetContext = FakeMailContext.defaultContext(); + testee = SieveExecutor.builder() + .mailetContext(mailetContext) + .sievePoster(mock(SievePoster.class)) + .resourceLocator(mock(ResourceLocator.class)) + .log(mock(Log.class)) + .build(); + } + + @Test + void handleFailureShouldSendAMailWithSieveNotificationAttribute() throws Exception { + testee.handleFailure(RECIPIENT1, MailImpl.builder() + .name("mymail") + .sender("sender@localhost") + .addRecipient(RECIPIENT1) + .mimeMessage(MimeMessageBuilder + .mimeMessageBuilder() + .setSubject("test") + .setText("this is the content")) + .build(), new Exception()); + + assertThat(mailetContext.getSentMails()) + .hasSize(1) + .allSatisfy(sentMail -> assertThat(sentMail.getAttributes()).containsKey(SIEVE_NOTIFICATION)); + } +} \ 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