RecipientRewriteTableProcessor { public void processMail(Mail mail) throws MessagingException { RrtExecutionResult executionResults = executeRrtFor(mail); if (!executionResults.recipientWithError.isEmpty()) { MailImpl newMail = MailImpl.builder() .name(mail.getName()) .sender(mail.getMaybeSender()) .addRecipients(executionResults.recipientWithError) .mimeMessage(mail.getMessage()) .state(errorProcessor) .build(); mailetContext.sendMail(newMail); } if (executionResults.newRecipients.isEmpty()) { mail.setState(Mail.GHOST); } mail.setRecipients(executionResults.newRecipients); } RecipientRewriteTableProcessor(RecipientRewriteTable virtualTableStore, DomainList domainList, MailetContext mailetContext, String errorProcessor);  RecipientRewriteTableProcessor(RecipientRewriteTable virtualTableStore, DomainList domainList, MailetContext mailetContext); void processMail(Mail mail);  }
@Test public void processShouldNotRewriteRecipientWhenVirtualTableStoreReturnNullMappings() throws Exception { when(virtualTableStore.getResolvedMappings(any(String.class), any(Domain.class))).thenReturn(null); mail = FakeMail.builder() .name("mail") .mimeMessage(message) .recipients(MailAddressFixture.ANY_AT_JAMES, MailAddressFixture.OTHER_AT_JAMES) .build(); processor.processMail(mail); assertThat(mail.getRecipients()).containsOnly(MailAddressFixture.ANY_AT_JAMES, MailAddressFixture.OTHER_AT_JAMES); }
@Test public void processShouldSendMailToAllErrorRecipientsWhenErrorMappingException() throws Exception { when(virtualTableStore.getResolvedMappings(eq("other"), eq(Domain.of(MailAddressFixture.JAMES_LOCAL)))).thenThrow(ErrorMappingException.class); mail = FakeMail.builder() .name("mail") .sender(MailAddressFixture.ANY_AT_JAMES) .mimeMessage(message) .recipients(MailAddressFixture.OTHER_AT_LOCAL, MailAddressFixture.ANY_AT_LOCAL) .build(); processor.processMail(mail); FakeMailContext.SentMail expected = FakeMailContext.sentMailBuilder() .sender(MailAddressFixture.ANY_AT_JAMES) .recipient(MailAddressFixture.OTHER_AT_LOCAL) .message(message) .fromMailet() .state(Mail.ERROR) .build(); assertThat(mailetContext.getSentMails()).containsOnly(expected); assertThat(mail.getRecipients()).containsOnly(MailAddressFixture.ANY_AT_LOCAL); }
@Test public void processShouldNotDuplicateRewrittenMailAddresses() throws Exception { when(virtualTableStore.getResolvedMappings(eq("other"), eq(Domain.of(MailAddressFixture.JAMES_LOCAL)))) .thenReturn(MappingsImpl.builder() .add(Mapping.alias(MailAddressFixture.ANY_AT_LOCAL.asString())) .add(Mapping.group(MailAddressFixture.ANY_AT_LOCAL.asString())) .build()); mail = FakeMail.builder() .name("mail") .sender(MailAddressFixture.ANY_AT_JAMES) .mimeMessage(message) .recipients(MailAddressFixture.OTHER_AT_LOCAL) .build(); processor.processMail(mail); assertThat(mail.getRecipients()).containsExactly(MailAddressFixture.ANY_AT_LOCAL); }
@Test public void processShouldSendMailToAllErrorRecipientsWhenRecipientRewriteTableException() throws Exception { when(virtualTableStore.getResolvedMappings(eq("other"), eq(Domain.of(MailAddressFixture.JAMES_LOCAL)))).thenThrow(RecipientRewriteTableException.class); mail = FakeMail.builder() .name("mail") .sender(MailAddressFixture.ANY_AT_JAMES) .mimeMessage(message) .recipients(MailAddressFixture.OTHER_AT_LOCAL, MailAddressFixture.ANY_AT_LOCAL) .build(); processor.processMail(mail); FakeMailContext.SentMail expected = FakeMailContext.sentMailBuilder() .sender(MailAddressFixture.ANY_AT_JAMES) .recipient(MailAddressFixture.OTHER_AT_LOCAL) .message(message) .state(Mail.ERROR) .fromMailet() .build(); assertThat(mailetContext.getSentMails()).containsOnly(expected); assertThat(mail.getRecipients()).containsOnly(MailAddressFixture.ANY_AT_LOCAL); }
@Test public void processShouldNotSendMailWhenNoErrorRecipients() throws Exception { when(virtualTableStore.getResolvedMappings(any(String.class), any(Domain.class))).thenReturn(null); mail = FakeMail.builder() .name("mail") .mimeMessage(message) .recipients(MailAddressFixture.ANY_AT_JAMES, nonDomainWithDefaultLocal) .build(); processor.processMail(mail); assertThat(mailetContext.getSentMails()).isEmpty(); }