RecipientIs extends GenericRecipientMatcher { @Override public void init() throws javax.mail.MessagingException { if (Strings.isNullOrEmpty(getCondition())) { throw new MessagingException("RecipientIs should have a condition composed of a list of mail addresses"); } recipients = MailAddressCollectionReader.read(getCondition()); if (recipients.isEmpty()) { throw new MessagingException("RecipientIs should have at least one address passed as a condition"); } }  @Override void init(); @Override boolean matchRecipient(MailAddress recipient);  }
@Test void shouldMatchCorrespondingAddres() throws Exception { matcher.init(FakeMatcherConfig.builder() .matcherName("RecipientIs") .condition(ANY_AT_JAMES.toString()) .build()); FakeMail fakeMail = FakeMail.builder() .name("mail") .recipient(ANY_AT_JAMES) .build(); assertThat(matcher.match(fakeMail)).containsOnly(ANY_AT_JAMES); }
@Test void shouldOnlyMatchCorrespondingAddress() throws Exception { matcher.init(FakeMatcherConfig.builder() .matcherName("RecipientIs") .condition(ANY_AT_JAMES.toString()) .build()); FakeMail fakeMail = FakeMail.builder() .name("mail") .recipients(ANY_AT_JAMES, OTHER_AT_JAMES) .build(); assertThat(matcher.match(fakeMail)).containsOnly(ANY_AT_JAMES); }
@Test void shouldNotMatchUnrelatedAddresses() throws Exception { matcher.init(FakeMatcherConfig.builder() .matcherName("RecipientIs") .condition(ANY_AT_JAMES.toString()) .build()); FakeMail fakeMail = FakeMail.builder() .name("mail") .recipients(OTHER_AT_JAMES, ANY_AT_JAMES2) .build(); assertThat(matcher.match(fakeMail)).isEmpty(); }
@Test void initShouldThrowOnMissingCondition() { assertThatThrownBy(() -> matcher.init(FakeMatcherConfig.builder() .matcherName("RecipientIs") .build())) .isInstanceOf(MessagingException.class); }
@Test void initShouldThrowOnEmptyCondition() { assertThatThrownBy(() -> matcher.init(FakeMatcherConfig.builder() .matcherName("RecipientIs") .build())) .isInstanceOf(MessagingException.class); }
@Test void shouldBeAbleToMatchSeveralAddresses() throws Exception { matcher.init(FakeMatcherConfig.builder() .matcherName("RecipientIs") .condition(ANY_AT_JAMES + ", " + ANY_AT_JAMES2) .build()); FakeMail fakeMail = FakeMail.builder() .name("mail") .recipients(ANY_AT_JAMES, OTHER_AT_JAMES, ANY_AT_JAMES2) .build(); assertThat(matcher.match(fakeMail)).containsOnly(ANY_AT_JAMES, ANY_AT_JAMES2); }