public Rule build() { Preconditions.checkState(id.isPresent(), "`id` is mandatory"); Preconditions.checkState(name.isPresent(), "`name` is mandatory"); Preconditions.checkState(condition.isPresent(), "`condition` is mandatory"); Preconditions.checkState(action.isPresent(), "`action` is mandatory"); return new Rule(id.get(), name.get(), condition.get(), action.get()); }
public static JmapRuleDTO from(Rule rule) { return new JmapRuleDTO(rule.getId().asString(), rule.getName(), ConditionDTO.from(rule.getCondition()), ActionDTO.from(rule.getAction())); }
public void defineRulesForRecipient1(List<Rule.Condition> conditions) { AtomicInteger counter = new AtomicInteger(); ImmutableList<Rule> rules = conditions .stream() .map(condition -> Rule.builder() .id(Rule.Id.of(String.valueOf(counter.incrementAndGet()))) .name(String.valueOf(counter.incrementAndGet())) .condition(condition) .action(Rule.Action.of(Rule.Action.AppendInMailboxes.withMailboxIds(testSystem.getRecipient1MailboxId().serialize()))) .build()) .collect(ImmutableList.toImmutableList()); testSystem.getFilteringManagement().defineRulesForUser(User.fromUsername(RECIPIENT_1_USERNAME), rules); }
private void ensureNoMultipleMailboxesRules(ImmutableList<Rule> rules) throws MultipleMailboxIdException { ImmutableList<Rule.Id> idWithMultipleMailboxes = rules.stream() .filter(rule -> rule.getAction().getAppendInMailboxes().getMailboxIds().size() > 1) .map(Rule::getId) .collect(ImmutableList.toImmutableList()); if (!idWithMultipleMailboxes.isEmpty()) { throw new MultipleMailboxIdException(idWithMultipleMailboxes); } }
static MailMatcher from(Rule rule) { Condition ruleCondition = rule.getCondition(); Optional<ContentMatcher> maybeContentMatcher = ContentMatcher.asContentMatcher(ruleCondition.getField(), ruleCondition.getComparator()); Optional<HeaderExtractor> maybeHeaderExtractor = HeaderExtractor.asHeaderExtractor(ruleCondition.getField()); return new HeaderMatcher( maybeContentMatcher.orElseThrow(() -> new RuntimeException("No content matcher associated with field " + ruleCondition.getField())), rule.getCondition().getValue(), maybeHeaderExtractor.orElseThrow(() -> new RuntimeException("No content matcher associated with comparator " + ruleCondition.getComparator()))); }
public Rule toRule() { return Rule.builder() .id(Rule.Id.of(id)) .name(name) .condition(conditionDTO.toCondition()) .name(name) .action(actionDTO.toAction()) .build(); }
@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 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 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 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 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(); }
Rule.builder() .id(Rule.Id.of("1")) .name("rule 1") .action(Rule.Action.of(Rule.Action.AppendInMailboxes.withMailboxIds(mailbox1Id.serialize()))) .build(), Rule.builder() .id(Rule.Id.of("2")) .name("rule 2") .action(Rule.Action.of(Rule.Action.AppendInMailboxes.withMailboxIds(mailbox2Id.serialize()))) .build(), Rule.builder() .id(Rule.Id.of("3")) .name("rule 3")
@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(); }