SpecialAddressesUtils { public List<MailAddress> replaceSpecialAddresses(Mail mailWithReplacementAddresses, List<MailAddress> mailAddresses) { ImmutableList.Builder<MailAddress> builder = ImmutableList.builder(); for (MailAddress mailAddress : mailAddresses) { builder.addAll(getCorrespondingAddress(mailWithReplacementAddresses, mailAddress)); } return builder.build(); } SpecialAddressesUtils(RedirectNotify genericMailet); static SpecialAddressesUtils from(RedirectNotify mailet); List<MailAddress> replaceSpecialAddresses(Mail mailWithReplacementAddresses, List<MailAddress> mailAddresses); List<MailAddress> replaceInternetAddresses(Mail mailWithReplacementAddresses, List<InternetAddress> internetAddresses); Optional<MailAddress> getFirstSpecialAddressIfMatchingOrGivenAddress(Optional<String> givenAddress, List<String> allowedSpecials);  }
@Test void replaceMailAddressesShouldReturnEmptyWhenEmptyList() throws Exception { FakeMail mail = FakeMail.builder() .name("name") .build(); Collection<MailAddress> addresses = testee.replaceSpecialAddresses(mail, ImmutableList.<MailAddress>of()); assertThat(addresses).isEmpty(); }
@Test void replaceMailAddressesShouldReturnSameContentWhenAddressesDoesntMatchAddressMarkerDomain() throws Exception { FakeMail mail = FakeMail.builder() .name("name") .build(); MailAddress mailAddress = new MailAddress("user", "addres.marker"); MailAddress mailAddress2 = new MailAddress("user2", "address.mar"); ImmutableList<MailAddress> list = ImmutableList.of(mailAddress, mailAddress2); Collection<MailAddress> addresses = testee.replaceSpecialAddresses(mail, list); assertThat(addresses).containsOnly(mailAddress, mailAddress2); }
@Test void replaceMailAddressesShouldReturnSenderWhenAddressesMatchSender() throws Exception { MailAddress sender = MailAddressFixture.ANY_AT_JAMES; FakeMail mail = FakeMail.builder() .name("name") .sender(sender) .build(); Collection<MailAddress> addresses = testee.replaceSpecialAddresses(mail, ImmutableList.of(SpecialAddress.SENDER)); assertThat(addresses).containsOnly(sender); }
@Test void replaceMailAddressesShouldReturnSenderWhenAddressesMatchFrom() throws Exception { MailAddress sender = MailAddressFixture.ANY_AT_JAMES; FakeMail mail = FakeMail.builder() .name("name") .sender(sender) .build(); Collection<MailAddress> addresses = testee.replaceSpecialAddresses(mail, ImmutableList.of(SpecialAddress.FROM)); assertThat(addresses).containsOnly(sender); }
@Test void replaceMailAddressesShouldReturnEmptyWhenAddressesMatchSenderAndSenderIsNull() throws Exception { FakeMail mail = FakeMail.builder() .name("name") .build(); Collection<MailAddress> addresses = testee.replaceSpecialAddresses(mail, ImmutableList.of(SpecialAddress.SENDER)); assertThat(addresses).isEmpty(); }
@Test void replaceMailAddressesShouldReturnSenderWhenAddressesMatchReplyToAndNoReplyTo() throws Exception { MailAddress sender = MailAddressFixture.ANY_AT_JAMES; FakeMail mail = FakeMail.builder() .name("name") .sender(sender) .mimeMessage(MimeMessageBuilder.mimeMessageBuilder()) .build(); Collection<MailAddress> addresses = testee.replaceSpecialAddresses(mail, ImmutableList.of(SpecialAddress.REPLY_TO)); assertThat(addresses).containsOnly(sender); }
@Test void replaceMailAddressesShouldReturnSenderWhenAddressesMatchReversePath() throws Exception { MailAddress sender = MailAddressFixture.ANY_AT_JAMES; FakeMail mail = FakeMail.builder() .name("name") .sender(sender) .build(); Collection<MailAddress> addresses = testee.replaceSpecialAddresses(mail, ImmutableList.of(SpecialAddress.REVERSE_PATH)); assertThat(addresses).containsOnly(sender); }
@Test void replaceMailAddressesShouldReturnEmptyWhenAddressesMatchReversePathAndNoSender() throws Exception { FakeMail mail = FakeMail.builder() .name("name") .build(); Collection<MailAddress> addresses = testee.replaceSpecialAddresses(mail, ImmutableList.of(SpecialAddress.REVERSE_PATH)); assertThat(addresses).isEmpty(); }
@Test void replaceMailAddressesShouldReturnRecipientsWhenAddressesMatchRecipients() throws Exception { MailAddress recipient = MailAddressFixture.ANY_AT_JAMES; MailAddress recipient2 = MailAddressFixture.OTHER_AT_JAMES; FakeMail mail = FakeMail.builder() .name("name") .recipients(recipient, recipient2) .build(); Collection<MailAddress> addresses = testee.replaceSpecialAddresses(mail, ImmutableList.of(SpecialAddress.RECIPIENTS)); assertThat(addresses).containsOnly(recipient, recipient2); }
@Test void replaceMailAddressesShouldReturnRecipientsWhenAddressesMatchTo() throws Exception { MailAddress recipient = MailAddressFixture.ANY_AT_JAMES; MailAddress recipient2 = MailAddressFixture.OTHER_AT_JAMES; FakeMail mail = FakeMail.builder() .name("name") .recipients(recipient, recipient2) .build(); Collection<MailAddress> addresses = testee.replaceSpecialAddresses(mail, ImmutableList.of(SpecialAddress.TO)); assertThat(addresses).containsOnly(recipient, recipient2); }
@Test void replaceMailAddressesShouldReturnEmptyWhenAddressesMatchUnaltered() throws Exception { FakeMail mail = FakeMail.builder() .name("name") .build(); Collection<MailAddress> addresses = testee.replaceSpecialAddresses(mail, ImmutableList.of(SpecialAddress.UNALTERED)); assertThat(addresses).isEmpty(); }
@Test void replaceMailAddressesShouldReturnEmptyWhenAddressesMatchNull() throws Exception { FakeMail mail = FakeMail.builder() .name("name") .build(); Collection<MailAddress> addresses = testee.replaceSpecialAddresses(mail, ImmutableList.of(SpecialAddress.NULL)); assertThat(addresses).isEmpty(); }
@Test void replaceMailAddressesShouldReturnSameAddressWhenAddressesDoesntMatch() throws Exception { FakeMail mail = FakeMail.builder() .name("name") .build(); MailAddress address = new MailAddress("user", "address.marker"); MailAddress address2 = new MailAddress("user2", "address.marker"); Collection<MailAddress> addresses = testee.replaceSpecialAddresses(mail, ImmutableList.of(address, address2)); assertThat(addresses).containsOnly(address, address2); }
@Test void replaceMailAddressesShouldReturnSameListWhenAddressesMatchDelete() throws Exception { FakeMail mail = FakeMail.builder() .name("name") .build(); Collection<MailAddress> addresses = testee.replaceSpecialAddresses(mail, ImmutableList.of(SpecialAddress.DELETE)); MailAddress expected = new MailAddress("delete", "address.marker"); assertThat(addresses).containsOnly(expected); }