@ParameterizedTest(name = "NOT-CONTAINS should match for field {1}: {0}") @MethodSource("notContainsTestSuite") void matchingNotContainsTest(String testDescription, Rule.Condition.Field fieldToMatch, MimeMessageBuilder mimeMessageBuilder, String valueToMatch, JMAPFilteringTestSystem testSystem) throws Exception { testSystem.defineRulesForRecipient1(Rule.Condition.of(fieldToMatch, NOT_CONTAINS, valueToMatch)); FakeMail mail = testSystem.asMail(mimeMessageBuilder); testSystem.getJmapFiltering().service(mail); assertThat(mail.getAttribute(DELIVERY_PATH_PREFIX + RECIPIENT_1_USERNAME)) .isEqualTo(RECIPIENT_1_MAILBOX_1); }
@ParameterizedTest(name = "NOT_EXACTLY_EQUALS should match for field {1}: {0}") @MethodSource("notEqualsTestSuite") void notEqualsMatchingTest(String testDescription, Rule.Condition.Field fieldToMatch, MimeMessageBuilder mimeMessageBuilder, String valueToMatch, JMAPFilteringTestSystem testSystem) throws Exception { testSystem.defineRulesForRecipient1(Rule.Condition.of(fieldToMatch, NOT_EXACTLY_EQUALS, valueToMatch)); FakeMail mail = testSystem.asMail(mimeMessageBuilder); testSystem.getJmapFiltering().service(mail); assertThat(mail.getAttribute(DELIVERY_PATH_PREFIX + RECIPIENT_1_USERNAME)) .isEqualTo(RECIPIENT_1_MAILBOX_1); }
@ParameterizedTest(name = "EXACTLY-EQUALS should match for field {1}: {0}") @MethodSource("exactlyEqualsTestSuite") void equalsMatchingTest(String testDescription, Rule.Condition.Field fieldToMatch, MimeMessageBuilder mimeMessageBuilder, String valueToMatch, JMAPFilteringTestSystem testSystem) throws Exception { testSystem.defineRulesForRecipient1(Rule.Condition.of(fieldToMatch, EXACTLY_EQUALS, valueToMatch)); FakeMail mail = testSystem.asMail(mimeMessageBuilder); testSystem.getJmapFiltering().service(mail); assertThat(mail.getAttribute(DELIVERY_PATH_PREFIX + RECIPIENT_1_USERNAME)) .isEqualTo(RECIPIENT_1_MAILBOX_1); }
.id(Rule.Id.of("1")) .name("rule 1") .condition(Rule.Condition.of(SUBJECT, CONTAINS, UNSCRAMBLED_SUBJECT)) .action(Rule.Action.of(Rule.Action.AppendInMailboxes.withMailboxIds(mailbox1Id.serialize()))) .build(), .id(Rule.Id.of("2")) .name("rule 2") .condition(Rule.Condition.of(FROM, NOT_CONTAINS, USER_1_USERNAME)) .action(Rule.Action.of(Rule.Action.AppendInMailboxes.withMailboxIds(mailbox2Id.serialize()))) .build(), .id(Rule.Id.of("3")) .name("rule 3") .condition(Rule.Condition.of(TO, EXACTLY_EQUALS, USER_3_ADDRESS)) .action(Rule.Action.of(Rule.Action.AppendInMailboxes.withMailboxIds(mailbox3Id.serialize()))) .build());
@Test void rulesWithInvalidMailboxIdsShouldBeSkept(JMAPFilteringTestSystem testSystem) throws Exception { String unknownMailboxId = "4242"; testSystem.getFilteringManagement().defineRulesForUser(User.fromUsername(RECIPIENT_1_USERNAME), Rule.builder() .id(Rule.Id.of("1")) .name("rule 1") .condition(Rule.Condition.of(FROM, CONTAINS, FRED_MARTIN_FULLNAME)) .action(Rule.Action.of(Rule.Action.AppendInMailboxes.withMailboxIds(unknownMailboxId))) .build(), Rule.builder() .id(Rule.Id.of("2")) .name("rule 2") .condition(Rule.Condition.of(FROM, CONTAINS, FRED_MARTIN_FULLNAME)) .action(Rule.Action.of(Rule.Action.AppendInMailboxes.withMailboxIds( testSystem.getRecipient1MailboxId().serialize()))) .build()); FakeMail mail = testSystem.asMail(mimeMessageBuilder() .addFrom(FRED_MARTIN_FULL_SCRAMBLED_ADDRESS)); testSystem.getJmapFiltering().service(mail); assertThat(mail.getAttribute(DELIVERY_PATH_PREFIX + RECIPIENT_1_USERNAME)) .isEqualTo(RECIPIENT_1_MAILBOX_1); }
@Test void mailDirectiveShouldSetLastMatchedMailboxWhenMultipleMailboxes(JMAPFilteringTestSystem testSystem) throws Exception { MailboxId mailbox1Id = testSystem.createMailbox(RECIPIENT_1_USERNAME, "RECIPIENT_1_MAILBOX_1"); MailboxId mailbox2Id = testSystem.createMailbox(RECIPIENT_1_USERNAME, "RECIPIENT_1_MAILBOX_2"); MailboxId mailbox3Id = testSystem.createMailbox(RECIPIENT_1_USERNAME, "RECIPIENT_1_MAILBOX_3"); testSystem.getFilteringManagement().defineRulesForUser(User.fromUsername(RECIPIENT_1_USERNAME), Rule.builder() .id(Rule.Id.of("1")) .name("rule 1") .condition(Rule.Condition.of(SUBJECT, CONTAINS, UNSCRAMBLED_SUBJECT)) .action(Rule.Action.of(Rule.Action.AppendInMailboxes.withMailboxIds(ImmutableList.of( mailbox3Id.serialize(), mailbox2Id.serialize(), mailbox1Id.serialize())))) .build()); FakeMail mail = testSystem.asMail(mimeMessageBuilder() .setSubject(UNSCRAMBLED_SUBJECT)); testSystem.getJmapFiltering().service(mail); assertThat(mail.getAttribute(DELIVERY_PATH_PREFIX + RECIPIENT_1_USERNAME)) .isEqualTo("RECIPIENT_1_MAILBOX_1"); }
@Test void rulesWithEmptyMailboxIdsShouldBeSkept(JMAPFilteringTestSystem testSystem) throws Exception { MailboxId mailbox1Id = testSystem.createMailbox(RECIPIENT_1_USERNAME, "RECIPIENT_1_MAILBOX_1"); testSystem.getFilteringManagement().defineRulesForUser(User.fromUsername(RECIPIENT_1_USERNAME), Rule.builder() .id(Rule.Id.of("1")) .name("rule 1") .condition(Rule.Condition.of(SUBJECT, CONTAINS, UNSCRAMBLED_SUBJECT)) .action(Rule.Action.of(Rule.Action.AppendInMailboxes.withMailboxIds(ImmutableList.of()))) .build(), Rule.builder() .id(Rule.Id.of("2")) .name("rule 2") .condition(Rule.Condition.of(SUBJECT, CONTAINS, UNSCRAMBLED_SUBJECT)) .action(Rule.Action.of(Rule.Action.AppendInMailboxes.withMailboxIds(ImmutableList.of( mailbox1Id.serialize())))) .build()); FakeMail mail = testSystem.asMail(mimeMessageBuilder() .setSubject(UNSCRAMBLED_SUBJECT)); testSystem.getJmapFiltering().service(mail); assertThat(mail.getAttribute(DELIVERY_PATH_PREFIX + RECIPIENT_1_USERNAME)) .isEqualTo("RECIPIENT_1_MAILBOX_1"); }
@Test void rulesWithMultipleMailboxIdsShouldFallbackWhenInvalidFirstMailboxId(JMAPFilteringTestSystem testSystem) throws Exception { String unknownMailboxId = "4242"; testSystem.getFilteringManagement().defineRulesForUser(User.fromUsername(RECIPIENT_1_USERNAME), Rule.builder() .id(Rule.Id.of("1")) .name("rule 1") .condition(Rule.Condition.of(FROM, CONTAINS, FRED_MARTIN_FULLNAME)) .action(Rule.Action.of(Rule.Action.AppendInMailboxes.withMailboxIds( unknownMailboxId, testSystem.getRecipient1MailboxId().serialize()))) .build()); FakeMail mail = testSystem.asMail(mimeMessageBuilder() .addFrom(FRED_MARTIN_FULL_SCRAMBLED_ADDRESS)); testSystem.getJmapFiltering().service(mail); assertThat(mail.getAttribute(DELIVERY_PATH_PREFIX + RECIPIENT_1_USERNAME)) .isEqualTo(RECIPIENT_1_MAILBOX_1); } }
@Test void mailDirectiveShouldNotBeSetWhenAllDoNotExactlyEqualsRuleValue(JMAPFilteringTestSystem testSystem) throws Exception { testSystem.defineRulesForRecipient1( Rule.Condition.of(FROM, CONTAINS, USER_1_FULL_ADDRESS), Rule.Condition.of(FROM, EXACTLY_EQUALS, USER_1_FULL_ADDRESS), Rule.Condition.of(TO, CONTAINS, USER_1_FULL_ADDRESS), Rule.Condition.of(TO, EXACTLY_EQUALS, USER_1_FULL_ADDRESS), Rule.Condition.of(CC, CONTAINS, USER_1_FULL_ADDRESS), Rule.Condition.of(CC, EXACTLY_EQUALS, USER_1_FULL_ADDRESS), Rule.Condition.of(RECIPIENT, EXACTLY_EQUALS, USER_1_FULL_ADDRESS), Rule.Condition.of(RECIPIENT, EXACTLY_EQUALS, USER_1_FULL_ADDRESS), Rule.Condition.of(SUBJECT, CONTAINS, USER_1_FULL_ADDRESS), Rule.Condition.of(SUBJECT, EXACTLY_EQUALS, USER_1_FULL_ADDRESS)); FakeMail mail = testSystem.asMail(mimeMessageBuilder()); testSystem.getJmapFiltering().service(mail); assertThat(mail.getAttribute(DELIVERY_PATH_PREFIX + RECIPIENT_1_USERNAME)) .isNull(); }
@Test void mailDirectiveShouldNotBeSetWhenUnknownMailboxId(JMAPFilteringTestSystem testSystem) throws Exception { String unknownMailboxId = "4242"; testSystem.getFilteringManagement().defineRulesForUser(User.fromUsername(RECIPIENT_1_USERNAME), Rule.builder() .id(Rule.Id.of("1")) .name("rule 1") .condition(Rule.Condition.of(FROM, CONTAINS, FRED_MARTIN_FULLNAME)) .action(Rule.Action.of(Rule.Action.AppendInMailboxes.withMailboxIds(unknownMailboxId))) .build()); FakeMail mail = testSystem.asMail(mimeMessageBuilder() .addFrom(FRED_MARTIN_FULL_SCRAMBLED_ADDRESS)); testSystem.getJmapFiltering().service(mail); assertThat(mail.getAttribute(DELIVERY_PATH_PREFIX + RECIPIENT_1_USERNAME)) .isNull(); }
@Test void mailDirectiveShouldNotBeSetWhenNoneRulesValueIsContained(JMAPFilteringTestSystem testSystem) throws Exception { testSystem.defineRulesForRecipient1( Rule.Condition.of(FROM, CONTAINS, SHOULD_NOT_MATCH), Rule.Condition.of(TO, CONTAINS, SHOULD_NOT_MATCH), Rule.Condition.of(CC, CONTAINS, SHOULD_NOT_MATCH)); FakeMail mail = testSystem.asMail(mimeMessageBuilder() .addFrom(USER_1_FULL_ADDRESS) .addToRecipient(USER_2_FULL_ADDRESS) .addCcRecipient(USER_3_FULL_ADDRESS)); testSystem.getJmapFiltering().service(mail); assertThat(mail.getAttribute(DELIVERY_PATH_PREFIX + RECIPIENT_1_USERNAME)) .isNull(); } }
@Test void serviceShouldNotThrowWhenUnknownMailboxId(JMAPFilteringTestSystem testSystem) throws Exception { String unknownMailboxId = "4242"; testSystem.getFilteringManagement().defineRulesForUser(User.fromUsername(RECIPIENT_1_USERNAME), Rule.builder() .id(Rule.Id.of("1")) .name("rule 1") .condition(Rule.Condition.of(FROM, CONTAINS, FRED_MARTIN_FULLNAME)) .action(Rule.Action.of(Rule.Action.AppendInMailboxes.withMailboxIds(unknownMailboxId))) .build()); FakeMail mail = testSystem.asMail(mimeMessageBuilder() .addFrom(FRED_MARTIN_FULL_SCRAMBLED_ADDRESS)); assertThatCode(() -> testSystem.getJmapFiltering().service(mail)) .doesNotThrowAnyException(); }
@ParameterizedTest(name = "NOT-CONTAINS should not match for field {1}: {0}") @MethodSource("containsTestSuite") void notContainsNotMatchingTest(String testDescription, Rule.Condition.Field fieldToMatch, MimeMessageBuilder mimeMessageBuilder, String valueToMatch, JMAPFilteringTestSystem testSystem) throws Exception { testSystem.defineRulesForRecipient1(Rule.Condition.of(fieldToMatch, NOT_CONTAINS, valueToMatch)); FakeMail mail = testSystem.asMail(mimeMessageBuilder); testSystem.getJmapFiltering().service(mail); assertThat(mail.getAttribute(DELIVERY_PATH_PREFIX + RECIPIENT_1_USERNAME)) .isNull(); }
@ParameterizedTest(name = "CONTAINS should match for field {1}: {0}") @MethodSource("containsTestSuite") void matchingContainsTest(String testDescription, Rule.Condition.Field fieldToMatch, MimeMessageBuilder mimeMessageBuilder, String valueToMatch, JMAPFilteringTestSystem testSystem) throws Exception { testSystem.defineRulesForRecipient1(Rule.Condition.of(fieldToMatch, CONTAINS, valueToMatch)); FakeMail mail = testSystem.asMail(mimeMessageBuilder); testSystem.getJmapFiltering().service(mail); assertThat(mail.getAttribute(DELIVERY_PATH_PREFIX + RECIPIENT_1_USERNAME)) .isEqualTo(RECIPIENT_1_MAILBOX_1); }
@ParameterizedTest(name = "NOT_EXACTLY_EQUALS should not match for field {1}: {0}") @MethodSource("exactlyEqualsTestSuite") void notMatchingNotEqualsTests(String testDescription, Rule.Condition.Field fieldToMatch, MimeMessageBuilder mimeMessageBuilder, String valueToMatch, JMAPFilteringTestSystem testSystem) throws Exception { testSystem.defineRulesForRecipient1(Rule.Condition.of(fieldToMatch, NOT_EXACTLY_EQUALS, valueToMatch)); FakeMail mail = testSystem.asMail(mimeMessageBuilder); testSystem.getJmapFiltering().service(mail); assertThat(mail.getAttribute(DELIVERY_PATH_PREFIX + RECIPIENT_1_USERNAME)) .isNull(); }
@ParameterizedTest(name = "CONTAINS should not match for field {1}: {0}") @MethodSource("notContainsTestSuite") void notMatchingContainsTest(String testDescription, Rule.Condition.Field fieldToMatch, MimeMessageBuilder mimeMessageBuilder, String valueToMatch, JMAPFilteringTestSystem testSystem) throws Exception { testSystem.defineRulesForRecipient1(Rule.Condition.of(fieldToMatch, CONTAINS, valueToMatch)); FakeMail mail = testSystem.asMail(mimeMessageBuilder); testSystem.getJmapFiltering().service(mail); assertThat(mail.getAttribute(DELIVERY_PATH_PREFIX + RECIPIENT_1_USERNAME)) .isNull(); }
public Rule.Condition toCondition() { return Rule.Condition.of( Rule.Condition.Field.of(field), Rule.Condition.Comparator.of(comparator), value); } }
@ParameterizedTest(name = "EXACTLY-EQUALS should not match for field {1}: {0}") @MethodSource("notEqualsTestSuite") void equalsNotMatchingTest(String testDescription, Rule.Condition.Field fieldToMatch, MimeMessageBuilder mimeMessageBuilder, String valueToMatch, JMAPFilteringTestSystem testSystem) throws Exception { testSystem.defineRulesForRecipient1(Rule.Condition.of(fieldToMatch, EXACTLY_EQUALS, valueToMatch)); FakeMail mail = testSystem.asMail(mimeMessageBuilder); testSystem.getJmapFiltering().service(mail); assertThat(mail.getAttribute(DELIVERY_PATH_PREFIX + RECIPIENT_1_USERNAME)) .isNull(); }