Search in sources :

Example 71 with ZMessage

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

the class TestTagFilterRules method testDeleteTag.

/**
     * Verifies that filter rules are disabled when a tag is deleted.
     */
@Test
public void testDeleteTag() throws Exception {
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    mbox.deleteTag(mTag.getId());
    // Deliver message that used to match the tag rule and make sure
    // that the message is not tagged.
    String subject = SUBJECT_PREFIX + " testDeleteTag";
    TestUtil.addMessageLmtp(subject, USER_NAME, USER_NAME);
    ZMessage msg = TestUtil.getMessage(mbox, "in:inbox subject:\"" + subject + "\"");
    assertEquals(mTag2.getId(), msg.getTagIds());
    // Confirm that the first rule was disabled and the second was not.
    ZFilterRule rule1 = TestUtil.getFilterRule(mbox, TAG_NAME);
    assertFalse(rule1.isActive());
    ZFilterRule rule2 = TestUtil.getFilterRule(mbox, TAG2_NAME);
    assertTrue(rule2.isActive());
}
Also used : ZMessage(com.zimbra.client.ZMessage) ZMailbox(com.zimbra.client.ZMailbox) ZFilterRule(com.zimbra.client.ZFilterRule) Test(org.junit.Test)

Example 72 with ZMessage

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

the class TestSmtpClient method sendAndVerify.

private void sendAndVerify(String sender, String[] recipients, String subject, String body, String expectedBody) throws Exception {
    // Fix up user names and subject.
    if (sender.indexOf("@") < 0) {
        sender = TestUtil.getAddress(sender);
    }
    for (int i = 0; i < recipients.length; i++) {
        if (recipients[i].indexOf("@") < 0) {
            recipients[i] = TestUtil.getAddress(recipients[i]);
        }
    }
    if (subject.indexOf(NAME_PREFIX) < 0) {
        subject = NAME_PREFIX + " " + subject;
    }
    // Send.
    String content = new MessageBuilder().withFrom(sender).withToRecipient(recipients[0]).withSubject(subject).withBody(body).create();
    SmtpConfig config = new SmtpConfig(mHost, mPort, "localhost");
    SmtpConnection smtp = new SmtpConnection(config);
    smtp.sendMessage(sender, recipients, content);
    // Verify.
    for (String recipient : recipients) {
        ZMailbox mbox = TestUtil.getZMailbox(recipient);
        ZMessage msg = TestUtil.waitForMessage(mbox, "in:inbox subject:\"" + subject + "\"");
        String currentBody = getBodyContent(msg.getMimeStructure());
        TestUtil.assertMessageContains(currentBody, expectedBody);
    }
}
Also used : ZMessage(com.zimbra.client.ZMessage) SmtpConfig(com.zimbra.cs.mailclient.smtp.SmtpConfig) SmtpConnection(com.zimbra.cs.mailclient.smtp.SmtpConnection) ZMailbox(com.zimbra.client.ZMailbox)

Example 73 with ZMessage

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

the class TestImapImport method compareMessages.

private void compareMessages(List<ZMessage> msgs1, List<ZMessage> msgs2) throws Exception {
    // Keep track of message ID's in first set
    Map<String, ZMessage> msgMap = new HashMap<String, ZMessage>();
    for (ZMessage msg : msgs1) {
        msgMap.put(msg.getMessageIdHeader(), msg);
    }
    // Compare messages in second set
    for (ZMessage msg2 : msgs2) {
        String id = msg2.getMessageIdHeader();
        ZMessage msg1 = msgMap.remove(id);
        assertNotNull("Found message '" + msg2.getSubject() + "' in mbox2 but not in mbox1", msg1);
        assertEquals("Message content", msg1.getContent(), msg2.getContent());
        String f1 = msg1.getFlags() != null ? msg1.getFlags() : "";
        String f2 = msg2.getFlags() != null ? msg2.getFlags() : "";
        assertEquals("Flags for message '" + msg1.getSubject() + "' don't match", f1, f2);
    }
    // Fail if there are any remaining messages
    if (msgMap.size() != 0) {
        List<String> subjects = new ArrayList<String>();
        for (ZMessage msg : msgMap.values()) {
            subjects.add(msg.getSubject());
        }
        fail("Found messages in mbox1 but not in mbox2: " + StringUtil.join(",", subjects));
    }
}
Also used : ZMessage(com.zimbra.client.ZMessage) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList)

Example 74 with ZMessage

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

the class TestImapImport method testImapImport.

public void testImapImport() throws Exception {
    List<ZMessage> msgs;
    ZMessage msg;
    // Remote: add 1 message
    ZimbraLog.test.info("Testing adding message to remote inbox.");
    String remoteQuery = "in:inbox msg1";
    TestUtil.addMessage(mRemoteMbox, NAME_PREFIX + " msg1", Integer.toString(Mailbox.ID_FOLDER_INBOX), "u");
    checkMsgCount(mRemoteMbox, remoteQuery, 1);
    assertNull(mLocalMbox.getFolderByPath(LOCAL_PATH_INBOX));
    msgs = TestUtil.search(mRemoteMbox, remoteQuery);
    assertEquals("Message count in remote inbox", 1, msgs.size());
    msg = msgs.get(0);
    assertTrue("Remote message is read", msg.isUnread());
    importImap();
    String localInboxQuery = "in:" + LOCAL_PATH_INBOX;
    checkMsgCount(mLocalMbox, localInboxQuery, 1);
    msgs = TestUtil.search(mRemoteMbox, remoteQuery);
    msg = msgs.get(0);
    assertTrue("Remote message is read", msg.isUnread());
    compare();
    // Remote: flag message
    ZimbraLog.test.info("Testing flag.");
    msgs = TestUtil.search(mRemoteMbox, remoteQuery);
    assertEquals("Message count in remote inbox", 1, msgs.size());
    msg = msgs.get(0);
    assertTrue("Remote message is read", msg.isUnread());
    String remoteId = msg.getId();
    mRemoteMbox.flagMessage(remoteId, true);
    // Make sure local copy is not flagged or read
    msgs = TestUtil.search(mLocalMbox, localInboxQuery);
    assertEquals("Message count in local inbox", 1, msgs.size());
    msg = msgs.get(0);
    assertFalse("Local message is flagged", msg.isFlagged());
    assertTrue("Local message is read", msg.isUnread());
    importImap();
    // Make sure that local copy is now flagged but still unread
    msgs = TestUtil.search(mLocalMbox, localInboxQuery);
    assertEquals("Message count in local inbox", 1, msgs.size());
    msg = msgs.get(0);
    assertTrue("Local message is flagged", msg.isFlagged());
    assertTrue("Local message is read", msg.isUnread());
    compare();
    // Remote: move to trash
    ZimbraLog.test.info("Testing remote move to trash.");
    mRemoteMbox.trashMessage(remoteId);
    checkMsgCount(mRemoteMbox, "in:trash", 1);
    checkMsgCount(mLocalMbox, "in:trash", 0);
    importImap();
    checkMsgCount(mLocalMbox, "in:" + DS_FOLDER_ROOT + "/Trash", 1);
    compare();
    // Create folders on both sides
    ZimbraLog.test.info("Testing folder creation.");
    TestUtil.createFolder(mRemoteMbox, REMOTE_PATH_F1);
    TestUtil.createFolder(mRemoteMbox, REMOTE_PATH_F2);
    TestUtil.createFolder(mLocalMbox, LOCAL_PATH_F3);
    TestUtil.createFolder(mLocalMbox, LOCAL_PATH_F4);
    importImap();
    // Make sure that new folders got created on both sides
    assertNotNull("Local folder " + LOCAL_PATH_F1, mLocalMbox.getFolderByPath(LOCAL_PATH_F1));
    assertNotNull("Local folder " + LOCAL_PATH_F2, mLocalMbox.getFolderByPath(LOCAL_PATH_F2));
    assertNotNull("Remote folder " + REMOTE_PATH_F3, mRemoteMbox.getFolderByPath(REMOTE_PATH_F3));
    assertNotNull("Remote folder " + REMOTE_PATH_F4, mRemoteMbox.getFolderByPath(REMOTE_PATH_F4));
    compare();
    // Test UIDVALIDITY change
    ZimbraLog.test.info("Testing UIDVALIDITY change.");
    ZFolder localFolder1 = mLocalMbox.getFolderByPath(LOCAL_PATH_F1);
    ZFolder remoteFolder1 = mRemoteMbox.getFolderByPath(REMOTE_PATH_F1);
    // Insert message into folder1 and remember original id
    String subject = NAME_PREFIX + " msg2";
    String originalId = TestUtil.addMessage(mLocalMbox, subject, localFolder1.getId());
    msgs = TestUtil.search(mLocalMbox, subject);
    assertEquals(1, msgs.size());
    assertEquals(originalId, msgs.get(0).getId());
    // Rename remote folder twice to force UIDVALIDITY change and sync.
    mRemoteMbox.renameFolder(remoteFolder1.getId(), NAME_PREFIX + "-renamed");
    mRemoteMbox.renameFolder(remoteFolder1.getId(), NAME_PREFIX + "-f1");
    importImap();
    // Make sure the original message is still there, and was synced to
    // the remote mailbox and back.
    msgs = TestUtil.search(mLocalMbox, subject);
    assertEquals(1, msgs.size());
    assertFalse("Message id did not change: " + originalId, originalId.equals(msgs.get(0).getId()));
    // Add message to remote folder and delete local folder at the same time
    ZimbraLog.test.info("Testing simultaneous message add and folder delete 1.");
    ZFolder remoteFolder2 = mRemoteMbox.getFolderByPath(REMOTE_PATH_F2);
    TestUtil.addMessage(mRemoteMbox, NAME_PREFIX + " msg3", remoteFolder2.getId());
    ZFolder localFolder3 = mLocalMbox.getFolderByPath(LOCAL_PATH_F3);
    mLocalMbox.deleteFolder(localFolder3.getId());
    checkMsgCount(mLocalMbox, "in:" + LOCAL_PATH_F2, 0);
    importImap();
    // Make sure that remote folders got deleted and that the message was added locally
    assertNull("Remote folder 3", mRemoteMbox.getFolderByPath(REMOTE_PATH_F3));
    assertNull("Remote folder 4", mRemoteMbox.getFolderByPath(REMOTE_PATH_F4));
    checkMsgCount(mLocalMbox, "in:" + LOCAL_PATH_F2, 1);
    compare();
    // Add message to a local folder and delete the same folder in remote mailbox
    ZimbraLog.test.info("Testing simultaneous message add and folder delete 2.");
    // preconditions: 1 synced message in each folder
    checkMsgCount(mLocalMbox, "in:" + LOCAL_PATH_F1, 1);
    checkMsgCount(mLocalMbox, "in:" + LOCAL_PATH_F2, 1);
    checkMsgCount(mRemoteMbox, "in:" + REMOTE_PATH_F1, 1);
    checkMsgCount(mRemoteMbox, "in:" + REMOTE_PATH_F2, 1);
    ZFolder localFolder2 = mLocalMbox.getFolderByPath(LOCAL_PATH_F2);
    TestUtil.addMessage(mLocalMbox, NAME_PREFIX + " msg4", localFolder2.getId());
    // one of which is new since last sync
    checkMsgCount(mLocalMbox, "in:" + LOCAL_PATH_F2, 2);
    remoteFolder1 = mRemoteMbox.getFolderByPath(REMOTE_PATH_F1);
    mRemoteMbox.deleteFolder(remoteFolder1.getId());
    importImap();
    // The remotely deleted folders should be resurrected by the sync,
    // F2 because it contains a new message, and F1 because it's the parent of F2.
    // Both should contain only messages added locally since the last sync.
    assertNotNull("Local folder 1", mLocalMbox.getFolderByPath(LOCAL_PATH_F1));
    assertNotNull("Local folder 2", mLocalMbox.getFolderByPath(LOCAL_PATH_F2));
    checkMsgCount(mLocalMbox, "in:" + LOCAL_PATH_F1, 0);
    checkMsgCount(mLocalMbox, "in:" + LOCAL_PATH_F2, 1);
    checkMsgCount(mLocalMbox, "in:" + LOCAL_PATH_F2 + " subject:msg4", 1);
    compare();
    // Add message to local inbox
    ZimbraLog.test.info("Testing sync from local to remote.");
    checkMsgCount(mLocalMbox, "in:" + LOCAL_PATH_INBOX, 0);
    ZFolder localInbox = mLocalMbox.getFolderByPath(LOCAL_PATH_INBOX);
    TestUtil.addMessage(mLocalMbox, NAME_PREFIX + " msg5", localInbox.getId());
    checkMsgCount(mLocalMbox, "in:" + LOCAL_PATH_INBOX, 1);
    checkMsgCount(mRemoteMbox, "in:inbox", 0);
    // Empty local trash
    checkMsgCount(mLocalMbox, "in:" + LOCAL_PATH_TRASH, 1);
    ZFolder localTrash = mLocalMbox.getFolderByPath(LOCAL_PATH_TRASH);
    mLocalMbox.emptyFolder(localTrash.getId());
    checkMsgCount(mLocalMbox, "in:" + LOCAL_PATH_TRASH, 0);
    checkMsgCount(mRemoteMbox, "in:trash", 1);
    importImap();
    // Make sure that local changes got propagated to remote server
    checkMsgCount(mRemoteMbox, "in:inbox msg5", 1);
    checkMsgCount(mRemoteMbox, "in:trash", 0);
    compare();
}
Also used : ZMessage(com.zimbra.client.ZMessage) ZFolder(com.zimbra.client.ZFolder)

Example 75 with ZMessage

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

the class TestImapOneWayImport method compare.

private void compare(ZMailbox mbox1, ZFolder folder1, ZMailbox mbox2, ZFolder folder2) throws Exception {
    assertNotNull(mbox1);
    assertNotNull(folder1);
    assertNotNull(mbox2);
    assertNotNull(folder2);
    // Recursively compare children
    for (ZFolder child1 : folder1.getSubFolders()) {
        if (isMailFolder(child1)) {
            ZFolder child2 = folder2.getSubFolderByPath(child1.getName());
            String msg = String.format("Could not find folder %s/%s for %s", folder2.getPath(), child1.getName(), mbox2.getName());
            assertNotNull(msg, child2);
            compare(mbox1, child1, mbox2, child2);
        }
    }
    assertEquals("Message count doesn't match (folder1 = " + folder1 + ", folder2 = " + folder2 + ")", folder1.getMessageCount(), folder2.getMessageCount());
    // Compare folders as long as neither one is the user root
    if (!(folder1.getPath().equals("/") || folder2.getPath().equals("/"))) {
        List<ZMessage> msgs1 = TestUtil.search(mbox1, "in:" + folder1.getPath());
        List<ZMessage> msgs2 = TestUtil.search(mbox2, "in:" + folder2.getPath());
        compareMessages(msgs1, msgs2);
    }
}
Also used : ZMessage(com.zimbra.client.ZMessage) 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