Search in sources :

Example 56 with ZMessage

use of com.zimbra.client.ZMessage in project zm-mailbox by Zimbra.

the class TestFilter method testAddressTestPart.

@Test
public void testAddressTestPart() throws Exception {
    List<ZFilterRule> rules = new ArrayList<ZFilterRule>();
    List<ZFilterCondition> conditions = new ArrayList<ZFilterCondition>();
    List<ZFilterAction> actions = new ArrayList<ZFilterAction>();
    // use address test for address domain match
    conditions.add(new ZFilterCondition.ZAddressCondition("From", Sieve.AddressPart.domain, HeaderOp.IS, false, "example.com"));
    actions.add(new ZMarkAction(MarkOp.FLAGGED));
    rules.add(new ZFilterRule("testAddressTestPart1", true, false, conditions, actions));
    saveIncomingRules(mMbox, new ZFilterRules(rules));
    String subject = NAME_PREFIX + " testAddressTestPart1";
    String mime = new MessageBuilder().withFrom("John Doe <JOHN.DOE@EXAMPLE.COM>").withSubject(subject).create();
    TestUtil.addMessageLmtp(new String[] { USER_NAME }, USER_NAME, mime);
    ZMessage msg = TestUtil.getMessage(mMbox, "in:inbox subject:\"" + subject + "\"");
    assertTrue("Unexpected message flag state", msg.isFlagged());
    // use address test for address local-part match
    conditions.add(new ZFilterCondition.ZAddressCondition("From", Sieve.AddressPart.localpart, HeaderOp.MATCHES, true, "j*doe"));
    actions.add(new ZMarkAction(MarkOp.FLAGGED));
    rules.add(new ZFilterRule("testAddressTestPart2", true, false, conditions, actions));
    saveIncomingRules(mMbox, new ZFilterRules(rules));
    subject = NAME_PREFIX + " testAddressTestPart2";
    mime = new MessageBuilder().withFrom("John Doe <john.doe@example.com>").withSubject(subject).create();
    TestUtil.addMessageLmtp(new String[] { USER_NAME }, USER_NAME, mime);
    msg = TestUtil.getMessage(mMbox, "in:inbox subject:\"" + subject + "\"");
    assertTrue("Unexpected message flag state", msg.isFlagged());
}
Also used : ZMessage(com.zimbra.client.ZMessage) ZFilterCondition(com.zimbra.client.ZFilterCondition) ArrayList(java.util.ArrayList) ZFilterRule(com.zimbra.client.ZFilterRule) ZFilterAction(com.zimbra.client.ZFilterAction) ZMarkAction(com.zimbra.client.ZFilterAction.ZMarkAction) ZFilterRules(com.zimbra.client.ZFilterRules) Test(org.junit.Test)

Example 57 with ZMessage

use of com.zimbra.client.ZMessage in project zm-mailbox by Zimbra.

the class TestFilterExisting method testBatchSize.

/**
     * Confirms that we're enforcing {@code zimbraFilterBatchSize}.
     */
@Test
public void testBatchSize() throws Exception {
    TestUtil.getAccount(USER_NAME).setFilterBatchSize(1);
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    String subject = NAME_PREFIX + " testBatchSize flag";
    String msg1Id = TestUtil.addMessage(mbox, subject + " 1");
    String msg2Id = TestUtil.addMessage(mbox, subject + " 2");
    try {
        runRules(new String[] { FLAG_RULE_NAME }, null, "subject: \'" + subject + "\'");
        Assert.fail("Batch size was not enforced");
    } catch (SoapFaultException e) {
        String msg = e.getMessage();
        Assert.assertTrue(msg.contains("2 messages"));
        Assert.assertTrue(msg.contains("limit of 1"));
    }
    // Make sure the rule was not executed.
    ZMessage msg = mbox.getMessageById(msg1Id);
    if (msg.hasFlags()) {
        Assert.assertFalse(msg.getFlags().contains("f"));
    }
    msg = mbox.getMessageById(msg2Id);
    if (msg.hasFlags()) {
        Assert.assertFalse(msg.getFlags().contains("f"));
    }
}
Also used : ZMessage(com.zimbra.client.ZMessage) ZMailbox(com.zimbra.client.ZMailbox) SoapFaultException(com.zimbra.common.soap.SoapFaultException) Test(org.junit.Test)

Example 58 with ZMessage

use of com.zimbra.client.ZMessage in project zm-mailbox by Zimbra.

the class TestFilterExisting method testFlag.

@Test
public void testFlag() throws Exception {
    // Add message
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    String subject = NAME_PREFIX + " test flag";
    String id = TestUtil.addMessage(mbox, subject);
    ZMessage msg = mbox.getMessageById(id);
    // Run tag rule and make sure the message is not tagged.
    Set<String> affectedIds = runRules(new String[] { TAG_RULE_NAME }, id, null);
    Assert.assertEquals(0, affectedIds.size());
    Assert.assertFalse(msg.isFlagged());
    // Run flag rule and make sure the message is flagged.
    affectedIds = runRules(new String[] { FLAG_RULE_NAME }, id, null);
    mbox.noOp();
    msg = mbox.getMessageById(id);
    Assert.assertTrue(msg.isFlagged());
}
Also used : ZMessage(com.zimbra.client.ZMessage) ZMailbox(com.zimbra.client.ZMailbox) Test(org.junit.Test)

Example 59 with ZMessage

use of com.zimbra.client.ZMessage in project zm-mailbox by Zimbra.

the class TestFilterExisting method testDiscard.

@Test
public void testDiscard() throws Exception {
    // Add message
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    String subject = NAME_PREFIX + " test keep, fileinto folder1, and discard";
    String id = TestUtil.addMessage(mbox, subject);
    // Run the keep and discard rules, and make sure the message was kept.
    Set<String> affectedIds = runRules(new String[] { KEEP_RULE_NAME, DISCARD_RULE_NAME }, id, null);
    Assert.assertEquals(0, affectedIds.size());
    TestUtil.getMessage(mbox, "in:inbox subject:\"" + subject + "\"");
    // Run the discard and fileinto rules, and make sure the message was filed.
    affectedIds = runRules(new String[] { DISCARD_RULE_NAME, FOLDER1_RULE_NAME }, id, null);
    assertMoved("inbox", FOLDER1_NAME, subject);
    // Run the discard rule by itself and make sure the message was deleted.
    String query = "in:" + FOLDER1_NAME + " subject:\"" + subject + "\"";
    ZMessage msg = TestUtil.getMessage(mbox, query);
    affectedIds = runRules(new String[] { DISCARD_RULE_NAME }, null, query);
    Assert.assertEquals(1, affectedIds.size());
    Assert.assertTrue(affectedIds.contains(msg.getId()));
    List<ZMessage> messages = TestUtil.search(mbox, query);
    Assert.assertEquals(0, messages.size());
}
Also used : ZMessage(com.zimbra.client.ZMessage) ZMailbox(com.zimbra.client.ZMailbox) Test(org.junit.Test)

Example 60 with ZMessage

use of com.zimbra.client.ZMessage in project zm-mailbox by Zimbra.

the class TestFilterExisting method testFileInto.

@Test
public void testFileInto() throws Exception {
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    TestUtil.createFolder(mbox, FOLDER1_PATH);
    TestUtil.createFolder(mbox, FOLDER2_PATH);
    String subject = NAME_PREFIX + " test keep, fileinto folder1 and folder2";
    String id = TestUtil.addMessage(mbox, subject);
    String[] ruleNames = new String[] { FOLDER1_RULE_NAME };
    // Test file into folder1 by id.
    Set<String> affectedIds = runRules(ruleNames, id, null);
    Assert.assertEquals(1, affectedIds.size());
    Assert.assertTrue(affectedIds.contains(id));
    assertMoved("inbox", FOLDER1_NAME, subject);
    // Test file into folder2 by query.
    ZMessage msg = TestUtil.getMessage(mbox, "in:" + FOLDER1_NAME + " subject:\"" + subject + "\"");
    id = msg.getId();
    ruleNames = new String[] { FOLDER2_RULE_NAME };
    affectedIds = runRules(ruleNames, null, "in:" + FOLDER1_NAME);
    Assert.assertEquals(1, affectedIds.size());
    Assert.assertTrue(affectedIds.contains(id));
    assertMoved(FOLDER1_NAME, FOLDER2_NAME, subject);
    // Move message back to inbox.
    msg = TestUtil.getMessage(mbox, "in:" + FOLDER2_NAME + " subject:\"" + subject + "\"");
    id = msg.getId();
    mbox.moveMessage(id, Integer.toString(Mailbox.ID_FOLDER_INBOX));
    // Test keep and file into folder1 and folder2.
    ruleNames = new String[] { KEEP_RULE_NAME, FOLDER1_RULE_NAME, FOLDER2_RULE_NAME };
    affectedIds = runRules(ruleNames, id, null);
    Assert.assertEquals(1, affectedIds.size());
    Assert.assertTrue(affectedIds.contains(id));
    TestUtil.getMessage(mbox, "in:inbox subject:\"" + subject + "\"");
    TestUtil.getMessage(mbox, "in:" + FOLDER1_NAME + " subject:\"" + subject + "\"");
    TestUtil.getMessage(mbox, "in:" + FOLDER2_NAME + " subject:\"" + subject + "\"");
}
Also used : ZMessage(com.zimbra.client.ZMessage) ZMailbox(com.zimbra.client.ZMailbox) Test(org.junit.Test)

Aggregations

ZMessage (com.zimbra.client.ZMessage)101 Test (org.junit.Test)63 ZMailbox (com.zimbra.client.ZMailbox)59 ArrayList (java.util.ArrayList)43 ZFilterRule (com.zimbra.client.ZFilterRule)29 ZFilterAction (com.zimbra.client.ZFilterAction)28 ZFilterCondition (com.zimbra.client.ZFilterCondition)28 ZFilterRules (com.zimbra.client.ZFilterRules)28 ZHeaderCondition (com.zimbra.client.ZFilterCondition.ZHeaderCondition)12 ZOutgoingMessage (com.zimbra.client.ZMailbox.ZOutgoingMessage)12 ZEmailAddress (com.zimbra.client.ZEmailAddress)11 ZTagAction (com.zimbra.client.ZFilterAction.ZTagAction)10 ZFolder (com.zimbra.client.ZFolder)10 ZMarkAction (com.zimbra.client.ZFilterAction.ZMarkAction)8 Account (com.zimbra.cs.account.Account)8 MessagePart (com.zimbra.client.ZMailbox.ZOutgoingMessage.MessagePart)7 Date (java.util.Date)7 ZGetMessageParams (com.zimbra.client.ZGetMessageParams)5 ZMimePart (com.zimbra.client.ZMessage.ZMimePart)5 ZTag (com.zimbra.client.ZTag)5