Search in sources :

Example 51 with ZMessage

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

the class TestFilter method testDateFiltering.

@Test
public void testDateFiltering() throws Exception {
    // Before tomorrow.
    ZTag tagBeforeTomorrow = mMbox.createTag(NAME_PREFIX + " before tomorrow", null);
    List<ZFilterCondition> conditions = new ArrayList<ZFilterCondition>();
    List<ZFilterAction> actions = new ArrayList<ZFilterAction>();
    List<ZFilterRule> rules = new ArrayList<ZFilterRule>();
    conditions.add(new ZDateCondition(DateOp.BEFORE, new Date(System.currentTimeMillis() + Constants.MILLIS_PER_DAY)));
    actions.add(new ZTagAction(tagBeforeTomorrow.getName()));
    rules.add(new ZFilterRule("before tomorrow", true, false, conditions, actions));
    // After yesterday.
    ZTag tagAfterYesterday = mMbox.createTag(NAME_PREFIX + " after yesterday", null);
    conditions = new ArrayList<ZFilterCondition>();
    actions = new ArrayList<ZFilterAction>();
    conditions.add(new ZDateCondition(DateOp.AFTER, new Date(System.currentTimeMillis() - Constants.MILLIS_PER_DAY)));
    actions.add(new ZTagAction(tagAfterYesterday.getName()));
    rules.add(new ZFilterRule("after yesterday", true, false, conditions, actions));
    // Save rules.
    ZFilterRules zRules = new ZFilterRules(rules);
    mMbox.saveIncomingFilterRules(zRules);
    // Old message.
    String[] recipients = new String[] { USER_NAME };
    String subject = NAME_PREFIX + " testDateFiltering old";
    String content = TestUtil.getTestMessage(subject, USER_NAME, USER_NAME, new Date(System.currentTimeMillis() - (2 * Constants.MILLIS_PER_DAY)));
    TestUtil.addMessageLmtp(recipients, USER_NAME, content);
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    ZMessage msg = TestUtil.getMessage(mbox, "in:inbox subject:\"" + subject + "\"");
    Set<String> tagIds = getTagIds(msg);
    assertEquals(1, tagIds.size());
    assertTrue(tagIds.contains(tagBeforeTomorrow.getId()));
    // Current message.
    subject = NAME_PREFIX + " testDateFiltering current";
    content = TestUtil.getTestMessage(subject, USER_NAME, USER_NAME, null);
    TestUtil.addMessageLmtp(recipients, USER_NAME, content);
    msg = TestUtil.getMessage(mbox, "in:inbox subject:\"" + subject + "\"");
    tagIds = getTagIds(msg);
    assertEquals(2, tagIds.size());
    assertTrue(tagIds.contains(tagAfterYesterday.getId()));
    assertTrue(tagIds.contains(tagBeforeTomorrow.getId()));
    // Future message.
    subject = NAME_PREFIX + " testDateFiltering future";
    content = TestUtil.getTestMessage(subject, USER_NAME, USER_NAME, new Date(System.currentTimeMillis() + (2 * Constants.MILLIS_PER_DAY)));
    TestUtil.addMessageLmtp(recipients, USER_NAME, content);
    msg = TestUtil.getMessage(mbox, "in:inbox subject:\"" + subject + "\"");
    tagIds = getTagIds(msg);
    assertEquals(1, tagIds.size());
    assertTrue(tagIds.contains(tagAfterYesterday.getId()));
    // No date header (bug 44079).
    subject = NAME_PREFIX + " testDateFiltering no date header";
    content = removeHeader(TestUtil.getTestMessage(subject, USER_NAME, USER_NAME, null), "Date");
    TestUtil.addMessageLmtp(recipients, USER_NAME, content);
    msg = TestUtil.getMessage(mbox, "in:inbox subject:\"" + subject + "\"");
    tagIds = getTagIds(msg);
    assertEquals(2, tagIds.size());
    assertTrue(tagIds.contains(tagAfterYesterday.getId()));
    assertTrue(tagIds.contains(tagBeforeTomorrow.getId()));
}
Also used : ZMessage(com.zimbra.client.ZMessage) ZFilterCondition(com.zimbra.client.ZFilterCondition) ArrayList(java.util.ArrayList) ZFilterAction(com.zimbra.client.ZFilterAction) ZFilterRule(com.zimbra.client.ZFilterRule) ZTag(com.zimbra.client.ZTag) Date(java.util.Date) ZMailbox(com.zimbra.client.ZMailbox) ZTagAction(com.zimbra.client.ZFilterAction.ZTagAction) ZFilterRules(com.zimbra.client.ZFilterRules) ZDateCondition(com.zimbra.client.ZFilterCondition.ZDateCondition) Test(org.junit.Test)

Example 52 with ZMessage

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

the class TestFilter method testAddressTest.

@Test
public void testAddressTest() throws Exception {
    List<ZFilterRule> rules = new ArrayList<ZFilterRule>();
    List<ZFilterCondition> conditions = new ArrayList<ZFilterCondition>();
    List<ZFilterAction> actions = new ArrayList<ZFilterAction>();
    // first use header test for address matching
    conditions.add(new ZFilterCondition.ZHeaderCondition("From", HeaderOp.IS, "john.doe@example.com"));
    actions.add(new ZMarkAction(MarkOp.FLAGGED));
    rules.add(new ZFilterRule("testAddressTest1", true, false, conditions, actions));
    saveIncomingRules(mMbox, new ZFilterRules(rules));
    String subject = NAME_PREFIX + " testAddressTest1";
    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 + "\"");
    assertFalse("Unexpected message flag state", msg.isFlagged());
    // now use the address test
    conditions.add(new ZFilterCondition.ZAddressCondition("From", Sieve.AddressPart.all, HeaderOp.IS, false, "john.doe@example.com"));
    actions.add(new ZMarkAction(MarkOp.FLAGGED));
    rules.add(new ZFilterRule("testAddressTest2", true, false, conditions, actions));
    saveIncomingRules(mMbox, new ZFilterRules(rules));
    subject = NAME_PREFIX + " testAddressTest2";
    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) ZHeaderCondition(com.zimbra.client.ZFilterCondition.ZHeaderCondition) ZMarkAction(com.zimbra.client.ZFilterAction.ZMarkAction) ZFilterRules(com.zimbra.client.ZFilterRules) Test(org.junit.Test)

Example 53 with ZMessage

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

the class TestFilter method testNotifyActionCopyAllOrigHeaders.

@Test
public void testNotifyActionCopyAllOrigHeaders() throws Exception {
    if (mAvailableRFCCompliantNotify) {
        fail("Unable to test because 'zimbraSieveNotifyActionRFCCompliant' is set to TRUE");
        return;
    }
    List<ZFilterRule> rules = new ArrayList<ZFilterRule>();
    List<ZFilterCondition> conditions = new ArrayList<ZFilterCondition>();
    List<ZFilterAction> actions = new ArrayList<ZFilterAction>();
    conditions.add(new ZFilterCondition.ZTrueCondition());
    // add an action to notify user2
    // copy all headers (From,To,Cc,Subject) from the original message onto the notification message
    actions.add(new ZFilterAction.ZNotifyAction(TestUtil.getAddress(REMOTE_USER_NAME), null, "${BODY}", -1, "*"));
    rules.add(new ZFilterRule("testNotifyActionCopyAllOrigHeaders", true, false, conditions, actions));
    saveIncomingRules(mMbox, new ZFilterRules(rules));
    String subject = NAME_PREFIX + " testNotifyActionCopyAllOrigHeaders";
    String body = "Hi, How r u?";
    String msg = new MessageBuilder().withFrom(REMOTE_USER_NAME).withToRecipient(USER_NAME).withCcRecipient(USER_NAME).withSubject(subject).withBody(body).create();
    // send msg to user1
    TestUtil.addMessageLmtp(new String[] { USER_NAME }, REMOTE_USER_NAME, msg);
    // check notification msg from user1 in user2's mailbox
    ZMailbox zMailbox = TestUtil.getZMailbox(REMOTE_USER_NAME);
    ZMessage zMessage = TestUtil.waitForMessage(zMailbox, "in:inbox subject:\"" + subject + "\"");
    String content = TestUtil.getContent(zMailbox, zMessage.getId());
    assertTrue(content.contains("From: " + REMOTE_USER_NAME));
    assertTrue(content.contains("To: " + USER_NAME));
    assertTrue(content.contains("Cc: " + USER_NAME));
    assertTrue(content.contains("Subject: " + subject));
}
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) ZMailbox(com.zimbra.client.ZMailbox) ZFilterRules(com.zimbra.client.ZFilterRules) Test(org.junit.Test)

Example 54 with ZMessage

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

the class TestFilter method testInvite.

@Test
public void testInvite() throws Exception {
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    // Create tags.
    String prefix = NAME_PREFIX + " testInvite ";
    ZTag tagNoMethod = mbox.createTag(prefix + "no method", null);
    ZTag tagAnyReply = mbox.createTag(prefix + "any reply", null);
    ZTag tagAnyRequest = mbox.createTag(prefix + "any request", null);
    ZTag tagRequestOrCancel = mbox.createTag(prefix + "request or cancel", null);
    ZTag tagReply = mbox.createTag(prefix + "reply", null);
    // Create filter rules that set tags based on conditions.
    List<ZFilterRule> rules = new ArrayList<ZFilterRule>();
    ZFilterCondition condition = new ZInviteCondition(true);
    ZFilterAction action = new ZTagAction(tagNoMethod.getName());
    rules.add(createRule("testInvite - no method", condition, action));
    condition = new ZInviteCondition(true, ZInviteCondition.METHOD_ANYREPLY);
    action = new ZTagAction(tagAnyReply.getName());
    rules.add(createRule("testInvite - any reply", condition, action));
    condition = new ZInviteCondition(true, ZInviteCondition.METHOD_ANYREQUEST);
    action = new ZTagAction(tagAnyRequest.getName());
    rules.add(createRule("testInvite - any request", condition, action));
    condition = new ZInviteCondition(true, Arrays.asList(ZInviteCondition.METHOD_REQUEST, ZInviteCondition.METHOD_CANCEL));
    action = new ZTagAction(tagRequestOrCancel.getName());
    rules.add(createRule("testInvite - request or cancel", condition, action));
    condition = new ZInviteCondition(true, ZInviteCondition.METHOD_REPLY);
    action = new ZTagAction(tagReply.getName());
    rules.add(createRule("testInvite - reply", condition, action));
    ZFilterRules zRules = new ZFilterRules(rules);
    saveIncomingRules(mMbox, zRules);
    // Send an invite from user2 and check tags.
    ZMailbox organizer = TestUtil.getZMailbox(REMOTE_USER_NAME);
    String subject = NAME_PREFIX + " testInvite request 1";
    Date startDate = new Date(System.currentTimeMillis() + Constants.MILLIS_PER_DAY);
    Date endDate = new Date(startDate.getTime() + Constants.MILLIS_PER_HOUR);
    TestUtil.createAppointment(organizer, subject, mbox.getName(), startDate, endDate);
    // Get message and check tags.
    ZMessage msg = TestUtil.waitForMessage(mbox, "in:inbox subject:\"" + subject + "\"");
    Set<String> tagIds = getTagIds(msg);
    assertTrue(tagIds.contains(tagNoMethod.getId()));
    assertFalse(tagIds.contains(tagAnyReply.getId()));
    assertTrue(tagIds.contains(tagAnyRequest.getId()));
    assertTrue(tagIds.contains(tagRequestOrCancel.getId()));
    assertFalse(tagIds.contains(tagReply.getId()));
    // Now test filtering a reply to an invite.  Send an invite to user2,
    // and have user2 accept the appointment.
    organizer = TestUtil.getZMailbox(USER_NAME);
    mbox = TestUtil.getZMailbox(REMOTE_USER_NAME);
    subject = NAME_PREFIX + " testInvite request 2";
    startDate = new Date(startDate.getTime() + Constants.MILLIS_PER_DAY);
    endDate = new Date(endDate.getTime() + Constants.MILLIS_PER_DAY);
    TestUtil.createAppointment(organizer, subject, mbox.getName(), startDate, endDate);
    // Receive the invite and accept the appointment.
    msg = TestUtil.waitForMessage(mbox, "in:inbox subject:\"" + subject + "\"");
    subject = NAME_PREFIX + " testInvite reply";
    TestUtil.sendInviteReply(mbox, msg.getId(), organizer.getName(), subject, ZMailbox.ReplyVerb.ACCEPT);
    msg = TestUtil.waitForMessage(organizer, "in:inbox subject:\"" + subject + "\"");
    // Check tags on the invite reply.
    tagIds = getTagIds(msg);
    assertTrue(tagIds.contains(tagNoMethod.getId()));
    assertTrue(tagIds.contains(tagAnyReply.getId()));
    assertFalse(tagIds.contains(tagAnyRequest.getId()));
    assertFalse(tagIds.contains(tagRequestOrCancel.getId()));
    assertTrue(tagIds.contains(tagReply.getId()));
}
Also used : ZMessage(com.zimbra.client.ZMessage) ZInviteCondition(com.zimbra.client.ZFilterCondition.ZInviteCondition) ZFilterCondition(com.zimbra.client.ZFilterCondition) ArrayList(java.util.ArrayList) ZFilterRule(com.zimbra.client.ZFilterRule) ZFilterAction(com.zimbra.client.ZFilterAction) ZTag(com.zimbra.client.ZTag) Date(java.util.Date) ZMailbox(com.zimbra.client.ZMailbox) ZTagAction(com.zimbra.client.ZFilterAction.ZTagAction) ZFilterRules(com.zimbra.client.ZFilterRules) Test(org.junit.Test)

Example 55 with ZMessage

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

the class TestFolderFilterRules method verifyFolderSize.

/**
     * Verifies message count for the given folder.
     */
private void verifyFolderSize(String folderId, int size) throws Exception {
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    ZFolder f = mbox.getFolderById(folderId);
    List<ZMessage> messages = TestUtil.search(mbox, "in:" + f.getPath());
    assertEquals("Incorrect message count for folder " + f.getPath(), size, messages.size());
}
Also used : ZMessage(com.zimbra.client.ZMessage) ZMailbox(com.zimbra.client.ZMailbox) ZFolder(com.zimbra.client.ZFolder)

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