Search in sources :

Example 26 with ZMailbox

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

the class TestFilterExisting method testKeep.

@Test
public void testKeep() throws Exception {
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    String subject = NAME_PREFIX + " test keep";
    String id = TestUtil.addMessage(mbox, subject);
    String query = "in:inbox subject:\"" + subject + "\"";
    String[] ruleNames = new String[] { KEEP_RULE_NAME };
    // Test keep in inbox by id.
    Set<String> affectedIds = runRules(ruleNames, id, null);
    Assert.assertEquals(0, affectedIds.size());
    TestUtil.getMessage(mbox, query);
    // Test keep in inbox by query.
    affectedIds = runRules(ruleNames, null, "in:inbox");
    Assert.assertEquals(0, affectedIds.size());
    TestUtil.getMessage(mbox, query);
    // Move message to folder1.
    ZFolder folder1 = TestUtil.createFolder(mbox, FOLDER1_PATH);
    mbox.moveMessage(id, folder1.getId());
    // Test keep in folder1 by id.
    affectedIds = runRules(ruleNames, id, null);
    Assert.assertEquals(0, affectedIds.size());
    query = "in:" + FOLDER1_NAME + " subject:\"" + subject + "\"";
    TestUtil.getMessage(mbox, query);
    // Test keep in folder1 by query.
    affectedIds = runRules(ruleNames, null, "in:" + FOLDER1_NAME);
    Assert.assertEquals(0, affectedIds.size());
    TestUtil.getMessage(mbox, query);
}
Also used : ZMailbox(com.zimbra.client.ZMailbox) ZFolder(com.zimbra.client.ZFolder) Test(org.junit.Test)

Example 27 with ZMailbox

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

the class TestFilterExisting method testTag.

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

Example 28 with ZMailbox

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

the class TestFilterExisting method setUp.

@Before
public void setUp() throws Exception {
    String prefix = NAME_PREFIX + "-" + testInfo.getMethodName() + "-";
    USER_NAME = prefix + "user";
    USER_NAME2 = prefix + "user2";
    cleanUp();
    TestUtil.createAccount(USER_NAME);
    TestUtil.createAccount(USER_NAME2);
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    ZMailbox mbox2 = TestUtil.getZMailbox(USER_NAME2);
    originalRules = mbox.getIncomingFilterRules();
    originalBatchSize = TestUtil.getAccountAttr(USER_NAME, Provisioning.A_zimbraFilterBatchSize);
    originalSleepInterval = TestUtil.getAccountAttr(USER_NAME, Provisioning.A_zimbraFilterSleepInterval);
    saveNewRules();
    // Speed up the test.
    TestUtil.setAccountAttr(USER_NAME, Provisioning.A_zimbraFilterSleepInterval, "0");
}
Also used : ZMailbox(com.zimbra.client.ZMailbox) Before(org.junit.Before)

Example 29 with ZMailbox

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

the class TestFilterExisting method assertMoved.

private void assertMoved(String sourceFolderName, String destFolderName, String subject) throws Exception {
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    List<ZMessage> messages = TestUtil.search(mbox, "in:" + sourceFolderName + " subject:\"" + subject + "\"");
    Assert.assertEquals(0, messages.size());
    messages = TestUtil.search(mbox, "in:" + destFolderName + " subject:\"" + subject + "\"");
    Assert.assertEquals(1, messages.size());
}
Also used : ZMessage(com.zimbra.client.ZMessage) ZMailbox(com.zimbra.client.ZMailbox)

Example 30 with ZMailbox

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

the class TestFilterExisting method saveNewRules.

private void saveNewRules() throws Exception {
    List<ZFilterRule> rules = new ArrayList<ZFilterRule>();
    // if subject contains "tag", tag
    List<ZFilterCondition> conditions = new ArrayList<ZFilterCondition>();
    List<ZFilterAction> actions = new ArrayList<ZFilterAction>();
    conditions.add(new ZHeaderCondition("subject", HeaderOp.CONTAINS, "tag"));
    actions.add(new ZTagAction(TAG_NAME));
    rules.add(new ZFilterRule(TAG_RULE_NAME, true, false, conditions, actions));
    // if subject contains "flag", flag
    conditions = new ArrayList<ZFilterCondition>();
    actions = new ArrayList<ZFilterAction>();
    conditions.add(new ZHeaderCondition("subject", HeaderOp.CONTAINS, "flag"));
    actions.add(new ZMarkAction(MarkOp.FLAGGED));
    rules.add(new ZFilterRule(FLAG_RULE_NAME, true, false, conditions, actions));
    // if subject contains "mark read", mark read
    conditions = new ArrayList<ZFilterCondition>();
    actions = new ArrayList<ZFilterAction>();
    conditions.add(new ZHeaderCondition("subject", HeaderOp.CONTAINS, "mark read"));
    actions.add(new ZMarkAction(MarkOp.READ));
    rules.add(new ZFilterRule(MARK_READ_RULE_NAME, true, false, conditions, actions));
    // if subject contains "keep", keep
    conditions = new ArrayList<ZFilterCondition>();
    actions = new ArrayList<ZFilterAction>();
    conditions.add(new ZHeaderCondition("subject", HeaderOp.CONTAINS, "keep"));
    actions.add(new ZKeepAction());
    rules.add(new ZFilterRule(KEEP_RULE_NAME, true, false, conditions, actions));
    // if subject contains "folder1", file into folder1
    conditions = new ArrayList<ZFilterCondition>();
    actions = new ArrayList<ZFilterAction>();
    conditions.add(new ZHeaderCondition("subject", HeaderOp.CONTAINS, "folder1"));
    actions.add(new ZFileIntoAction(FOLDER1_PATH));
    rules.add(new ZFilterRule(FOLDER1_RULE_NAME, true, false, conditions, actions));
    // if the subject contains "folder1 and flag", file into folder1 and flag
    conditions = new ArrayList<ZFilterCondition>();
    actions = new ArrayList<ZFilterAction>();
    conditions.add(new ZHeaderCondition("subject", HeaderOp.CONTAINS, "folder1 and flag"));
    actions.add(new ZFileIntoAction(FOLDER1_PATH));
    actions.add(new ZMarkAction(MarkOp.FLAGGED));
    rules.add(new ZFilterRule(FOLDER1_AND_FLAG_RULE_NAME, true, false, conditions, actions));
    // if subject contains "folder2", file into folder2
    conditions = new ArrayList<ZFilterCondition>();
    actions = new ArrayList<ZFilterAction>();
    conditions.add(new ZHeaderCondition("subject", HeaderOp.CONTAINS, "folder2"));
    actions.add(new ZFileIntoAction(FOLDER2_PATH));
    rules.add(new ZFilterRule(FOLDER2_RULE_NAME, true, false, conditions, actions));
    // if subject contains "folder3", file into folder3.  This one uses the
    // folder name without the leading slash.
    conditions = new ArrayList<ZFilterCondition>();
    actions = new ArrayList<ZFilterAction>();
    conditions.add(new ZHeaderCondition("subject", HeaderOp.CONTAINS, "folder3"));
    actions.add(new ZFileIntoAction(FOLDER3_NAME));
    rules.add(new ZFilterRule(FOLDER3_RULE_NAME, true, false, conditions, actions));
    // if subject contains "discard", discard
    conditions = new ArrayList<ZFilterCondition>();
    actions = new ArrayList<ZFilterAction>();
    conditions.add(new ZHeaderCondition("subject", HeaderOp.CONTAINS, "discard"));
    actions.add(new ZDiscardAction());
    rules.add(new ZFilterRule(DISCARD_RULE_NAME, true, false, conditions, actions));
    // If subject contains "redirect", redirect to user2.  This rule should
    // be ignored when applied to existing messages.
    conditions = new ArrayList<ZFilterCondition>();
    actions = new ArrayList<ZFilterAction>();
    conditions.add(new ZHeaderCondition("subject", HeaderOp.CONTAINS, "redirect"));
    actions.add(new ZRedirectAction(TestUtil.getAddress(USER_NAME2)));
    rules.add(new ZFilterRule(REDIRECT_RULE_NAME, true, false, conditions, actions));
    // Save rules
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    mbox.saveIncomingFilterRules(new ZFilterRules(rules));
}
Also used : ZDiscardAction(com.zimbra.client.ZFilterAction.ZDiscardAction) ZHeaderCondition(com.zimbra.client.ZFilterCondition.ZHeaderCondition) ZFilterCondition(com.zimbra.client.ZFilterCondition) ZFilterRule(com.zimbra.client.ZFilterRule) ArrayList(java.util.ArrayList) ZFilterAction(com.zimbra.client.ZFilterAction) ZRedirectAction(com.zimbra.client.ZFilterAction.ZRedirectAction) ZMailbox(com.zimbra.client.ZMailbox) ZFileIntoAction(com.zimbra.client.ZFilterAction.ZFileIntoAction) ZTagAction(com.zimbra.client.ZFilterAction.ZTagAction) ZKeepAction(com.zimbra.client.ZFilterAction.ZKeepAction) ZMarkAction(com.zimbra.client.ZFilterAction.ZMarkAction) ZFilterRules(com.zimbra.client.ZFilterRules)

Aggregations

ZMailbox (com.zimbra.client.ZMailbox)383 Test (org.junit.Test)288 ZFolder (com.zimbra.client.ZFolder)90 ZMessage (com.zimbra.client.ZMessage)82 Mailbox (com.zimbra.cs.mailbox.Mailbox)61 Account (com.zimbra.cs.account.Account)60 ServiceException (com.zimbra.common.service.ServiceException)55 ArrayList (java.util.ArrayList)38 IOException (java.io.IOException)35 MessageData (com.zimbra.cs.mailclient.imap.MessageData)28 ZMountpoint (com.zimbra.client.ZMountpoint)26 Message (com.zimbra.cs.mailbox.Message)24 HashMap (java.util.HashMap)23 ZOutgoingMessage (com.zimbra.client.ZMailbox.ZOutgoingMessage)21 ZTag (com.zimbra.client.ZTag)21 SoapFaultException (com.zimbra.common.soap.SoapFaultException)21 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)21 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)20 HttpClient (org.apache.http.client.HttpClient)19 ZSearchParams (com.zimbra.client.ZSearchParams)18