SpecialAddressesUtils { public List<MailAddress> replaceInternetAddresses(Mail mailWithReplacementAddresses, List<InternetAddress> internetAddresses) throws MessagingException { ImmutableList.Builder<MailAddress> builder = ImmutableList.builder(); for (InternetAddress internetAddress : internetAddresses) { builder.addAll(getCorrespondingAddress(internetAddress, mailWithReplacementAddresses)); } 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 replaceInternetAddressesShouldReturnEmptyWhenEmptyList() throws Exception { FakeMail mail = FakeMail.builder() .name("name") .build(); List<MailAddress> addresses = testee.replaceInternetAddresses(mail, ImmutableList.<InternetAddress>of()); assertThat(addresses).isEmpty(); }
@Test void replaceInternetAddressesShouldReturnSameContentWhenAddressesDoesntMatchAddressMarkerDomain() throws Exception { FakeMail mail = FakeMail.builder() .name("name") .build(); InternetAddress internetAddress = new InternetAddress("user@addres.marker"); InternetAddress internetAddress2 = new InternetAddress("user2@address.mar"); ImmutableList<InternetAddress> list = ImmutableList.of(internetAddress, internetAddress2); List<MailAddress> addresses = testee.replaceInternetAddresses(mail, list); assertThat(addresses).containsOnly(new MailAddress(internetAddress), new MailAddress(internetAddress2)); }
@Test void replaceInternetAddressesShouldReturnSenderWhenAddressesMatchSender() throws Exception { MailAddress sender = MailAddressFixture.ANY_AT_JAMES; FakeMail mail = FakeMail.builder() .name("name") .sender(sender) .build(); List<MailAddress> addresses = testee.replaceInternetAddresses(mail, ImmutableList.of(SpecialAddress.SENDER.toInternetAddress())); assertThat(addresses).containsOnly(sender); }
@Test void replaceInternetAddressesShouldReturnSenderWhenAddressesMatchFromAndNoFrom() throws Exception { MailAddress sender = MailAddressFixture.ANY_AT_JAMES; FakeMail mail = FakeMail.builder() .name("name") .mimeMessage(MimeMessageBuilder.mimeMessageBuilder()) .sender(sender) .build(); List<MailAddress> addresses = testee.replaceInternetAddresses(mail, ImmutableList.of(SpecialAddress.FROM.toInternetAddress())); assertThat(addresses).containsOnly(sender); }
@Test void replaceInternetAddressesShouldReturnEmptyWhenAddressesMatchSenderAndSenderIsNull() throws Exception { FakeMail mail = FakeMail.builder() .name("name") .build(); List<MailAddress> addresses = testee.replaceInternetAddresses(mail, ImmutableList.of(SpecialAddress.SENDER.toInternetAddress())); assertThat(addresses).isEmpty(); }
@Test void replaceInternetAddressesShouldReturnSenderWhenAddressesMatchReplyToAndNoReplyTo() throws Exception { MailAddress sender = MailAddressFixture.ANY_AT_JAMES; FakeMail mail = FakeMail.builder() .name("name") .sender(sender) .mimeMessage(MimeMessageBuilder.mimeMessageBuilder()) .build(); List<MailAddress> addresses = testee.replaceInternetAddresses(mail, ImmutableList.of(SpecialAddress.REPLY_TO.toInternetAddress())); assertThat(addresses).containsOnly(sender); }
@Test void replaceInternetAddressesShouldReturnSenderWhenAddressesMatchReversePath() throws Exception { MailAddress sender = MailAddressFixture.ANY_AT_JAMES; FakeMail mail = FakeMail.builder() .name("name") .sender(sender) .build(); List<MailAddress> addresses = testee.replaceInternetAddresses(mail, ImmutableList.of(SpecialAddress.REVERSE_PATH.toInternetAddress())); assertThat(addresses).containsOnly(sender); }
@Test void replaceInternetAddressesShouldReturnEmptyWhenAddressesMatchReversePathAndNoSender() throws Exception { FakeMail mail = FakeMail.builder() .name("name") .build(); List<MailAddress> addresses = testee.replaceInternetAddresses(mail, ImmutableList.of(SpecialAddress.REVERSE_PATH.toInternetAddress())); assertThat(addresses).isEmpty(); }
@Test void replaceInternetAddressesShouldReturnEmptyWhenAddressesMatchUnaltered() throws Exception { FakeMail mail = FakeMail.builder() .name("name") .build(); List<MailAddress> addresses = testee.replaceInternetAddresses(mail, ImmutableList.of(SpecialAddress.UNALTERED.toInternetAddress())); assertThat(addresses).isEmpty(); }
@Test void replaceInternetAddressesShouldReturnEmptyWhenAddressesMatchNull() throws Exception { FakeMail mail = FakeMail.builder() .name("name") .build(); List<MailAddress> addresses = testee.replaceInternetAddresses(mail, ImmutableList.of(SpecialAddress.NULL.toInternetAddress())); assertThat(addresses).isEmpty(); }
@Test void replaceInternetAddressesShouldReturnSameAddressWhenAddressesDoesntMatch() throws Exception { FakeMail mail = FakeMail.builder() .name("name") .build(); MailAddress address = new MailAddress("user", "address.marker"); MailAddress address2 = new MailAddress("user2", "address.marker"); List<MailAddress> addresses = testee.replaceInternetAddresses(mail, ImmutableList.of(address.toInternetAddress(), address2.toInternetAddress())); assertThat(addresses).containsOnly(address, address2); }
@Test void replaceInternetAddressesShouldReturnSameListWhenAddressesMatchDelete() throws Exception { FakeMail mail = FakeMail.builder() .name("name") .build(); List<MailAddress> addresses = testee.replaceInternetAddresses(mail, ImmutableList.of(SpecialAddress.DELETE.toInternetAddress())); MailAddress expected = new MailAddress("delete@address.marker"); assertThat(addresses).containsOnly(expected); }