Search in sources :

Example 76 with ZMessage

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

the class TestImapOneWayImport method testImapOneWayImport.

public void testImapOneWayImport() 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 locally
    assertNotNull("Local folder " + LOCAL_PATH_F1, mLocalMbox.getFolderByPath(LOCAL_PATH_F1));
    assertNotNull("Local folder " + LOCAL_PATH_F2, mLocalMbox.getFolderByPath(LOCAL_PATH_F2));
    // Make sure that new folder is NOT created remotely
    assertNull("Remote folder " + REMOTE_PATH_F3, mRemoteMbox.getFolderByPath(REMOTE_PATH_F3));
    assertNull("Remote folder " + REMOTE_PATH_F4, mRemoteMbox.getFolderByPath(REMOTE_PATH_F4));
    // 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 remote folder, 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);
    TestUtil.createFolder(mRemoteMbox, REMOTE_PATH_F3);
    mLocalMbox.deleteFolder(localFolder3.getId());
    checkMsgCount(mLocalMbox, "in:" + LOCAL_PATH_F2, 0);
    importImap();
    // Make sure that remote folders did not get deleted and that the message was added locally
    assertNotNull("Remote folder 3", mRemoteMbox.getFolderByPath(REMOTE_PATH_F3));
    checkMsgCount(mLocalMbox, "in:" + LOCAL_PATH_F2, 1);
    // 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.");
    ZFolder localFolder = mLocalMbox.getFolderByPath(LOCAL_PATH_F2);
    TestUtil.addMessage(mLocalMbox, NAME_PREFIX + " msg4", localFolder.getId());
    remoteFolder1 = mRemoteMbox.getFolderByPath(REMOTE_PATH_F1);
    mRemoteMbox.deleteFolder(remoteFolder1.getId());
    //make sure local folder exists before sync
    assertNotNull("Local folder 1", mLocalMbox.getFolderByPath(LOCAL_PATH_F1));
    // Make sure remote folder was deleted
    assertNull("Remote folder 1", mRemoteMbox.getFolderByPath(REMOTE_PATH_F1));
    importImap();
    // Make sure remote folder is still deleted
    assertNull("Remote folder 1", mRemoteMbox.getFolderByPath(REMOTE_PATH_F1));
    // Make sure local folder was deleted as well
    assertNull("Local folder 1", mLocalMbox.getFolderByPath(LOCAL_PATH_F1));
    //make sure local message did not get added to remote folder
    checkMsgCount(mRemoteMbox, "msg4", 0);
    // 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 did not propagate to remote server
    checkMsgCount(mRemoteMbox, "in:inbox msg5", 0);
    checkMsgCount(mRemoteMbox, "in:trash", 1);
}
Also used : ZMessage(com.zimbra.client.ZMessage) ZFolder(com.zimbra.client.ZFolder)

Example 77 with ZMessage

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

the class TestSendAndReceive method testMalformedContentType.

/**
     * Confirms that we can forward attachments with a malformed content type (bug 42452).
     */
@Test
public void testMalformedContentType() throws Exception {
    TestUtil.createAccount(USER_NAME);
    // Generate original message.
    String subject = NAME_PREFIX + " testMalformedContentType";
    MessageBuilder builder = new MessageBuilder().withFrom(USER_NAME).withToRecipient(USER_NAME).withSubject(subject).withAttachment("This is an attachment", "test.txt", MimeConstants.CT_TEXT_PLAIN);
    // Hack Content-Type so that it's invalid.
    BufferedReader reader = new BufferedReader(new StringReader(builder.create()));
    StringBuilder msgBuf = new StringBuilder();
    String line = reader.readLine();
    boolean replaced = false;
    while (line != null) {
        if (line.matches("Content-Type.*test.txt.*")) {
            line = line.replace("Content-Type: text/plain;", "Content-Type: text/plain;;");
            Assert.assertTrue("Unexpected line: " + line, line.contains(";;"));
            replaced = true;
        }
        msgBuf.append(line).append("\r\n");
        line = reader.readLine();
    }
    Assert.assertTrue("Could not find text/plain attachment.", replaced);
    // Add message to the mailbox.
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    TestUtil.addMessageLmtp(new String[] { USER_NAME }, USER_NAME, msgBuf.toString());
    // Forward the attachment in a new message.
    ZMessage srcMsg = TestUtil.getMessage(mbox, "subject:\"" + subject + "\"");
    ZMimePart srcAttachPart = srcMsg.getMimeStructure().getChildren().get(1);
    Assert.assertEquals("test.txt", srcAttachPart.getFileName());
    ZOutgoingMessage outgoing = new ZOutgoingMessage();
    outgoing.setMessagePart(new MessagePart(MimeConstants.CT_TEXT_PLAIN, "Forwarding attachment."));
    outgoing.setMessagePartsToAttach(Arrays.asList(new AttachedMessagePart(srcMsg.getId(), srcAttachPart.getPartName(), null)));
    String address = TestUtil.getAddress(USER_NAME);
    ZEmailAddress sender = new ZEmailAddress(address, null, null, ZEmailAddress.EMAIL_TYPE_FROM);
    ZEmailAddress recipient = new ZEmailAddress(address, null, null, ZEmailAddress.EMAIL_TYPE_TO);
    outgoing.setAddresses(Arrays.asList(sender, recipient));
    String fwdSubject = NAME_PREFIX + " testMalformedContentType forward";
    outgoing.setSubject(fwdSubject);
    mbox.sendMessage(outgoing, null, false);
    // Make sure the forwarded message arrives.
    ZMessage fwd = TestUtil.waitForMessage(mbox, "in:inbox subject:\"" + fwdSubject + "\"");
    ZMimePart fwdAttachPart = fwd.getMimeStructure().getChildren().get(1);
    Assert.assertEquals("test.txt", fwdAttachPart.getFileName());
}
Also used : ZMessage(com.zimbra.client.ZMessage) ZOutgoingMessage(com.zimbra.client.ZMailbox.ZOutgoingMessage) MessagePart(com.zimbra.client.ZMailbox.ZOutgoingMessage.MessagePart) AttachedMessagePart(com.zimbra.client.ZMailbox.ZOutgoingMessage.AttachedMessagePart) ZMimePart(com.zimbra.client.ZMessage.ZMimePart) ZMailbox(com.zimbra.client.ZMailbox) AttachedMessagePart(com.zimbra.client.ZMailbox.ZOutgoingMessage.AttachedMessagePart) ZEmailAddress(com.zimbra.client.ZEmailAddress) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) Test(org.junit.Test)

Example 78 with ZMessage

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

the class TestSendAndReceive method testSendDraftWithCustomDataNoSave.

@Test
public void testSendDraftWithCustomDataNoSave() throws Exception {
    TestUtil.createAccount(USER_NAME);
    TestUtil.createAccount(REMOTE_USER_NAME);
    MailSender.registerPreSendMailListener(listener);
    //register second time to test that it will be triggered once
    MailSender.registerPreSendMailListener(listener);
    ZMailbox zmbox = TestUtil.getZMailbox(USER_NAME);
    String subj = "Dwalin";
    String body = "To dungeons deep, and caverns old";
    String customHeaderValue = "http://zss.server/file1";
    SoapHttpTransport transport = new SoapHttpTransport(TestUtil.getSoapUrl());
    transport.setAuthToken(zmbox.getAuthToken());
    //create a draft with a custom mime header
    Element request = new Element.JSONElement(MailConstants.SAVE_DRAFT_REQUEST);
    Element el = request.addUniqueElement(MailConstants.E_MSG);
    el.addAttribute(MailConstants.E_SUBJECT, subj);
    el.addUniqueElement(MailConstants.E_MIMEPART).addAttribute(MailConstants.A_CONTENT_TYPE, "text/plain").addAttribute(MailConstants.E_CONTENT, body);
    el.addNonUniqueElement(MailConstants.E_EMAIL).addAttribute(MailConstants.A_ADDRESS_TYPE, EmailType.TO.toString()).addAttribute(MailConstants.A_ADDRESS, TestUtil.addDomainIfNecessary(REMOTE_USER_NAME));
    el.addNonUniqueElement(MailConstants.E_HEADER).addAttribute(MailConstants.A_NAME, MailSender.PRE_SEND_HEADER).setText("custom");
    el.addNonUniqueElement(MailConstants.E_HEADER).addAttribute(MailConstants.A_NAME, PUBLIC_LIST_HEADER).setText(customHeaderValue);
    ZMessage draft = new ZMessage(transport.invoke(request).getElement(MailConstants.E_MSG), zmbox);
    //send the draft but don't save to Sent folder
    request = new Element.JSONElement(MailConstants.SEND_MSG_REQUEST);
    request.addUniqueElement(MailConstants.E_MSG).addAttribute(MailConstants.A_DRAFT_ID, Integer.parseInt(draft.getId())).addAttribute(MailConstants.A_SEND_FROM_DRAFT, true).addAttribute(MailConstants.A_NO_SAVE_TO_SENT, true);
    transport.invoke(request);
    Assert.assertTrue("Listener was not triggered", listener.isTriggered());
    Assert.assertEquals("listener should ahve been triggered only once", 1, listener.getTriggerCounter());
    String[] listenersData = listener.getData();
    Assert.assertNotNull("Listener did not get data from custom headers", listenersData);
    Assert.assertTrue("wrong number of elements in custom header data", listenersData.length == 1);
    Assert.assertTrue(String.format("wrong value in custom header: %s", listenersData[0]), customHeaderValue.equalsIgnoreCase(listenersData[0].replace("\"", "")));
    Assert.assertNotNull("Listener did not get data from custom headers", listenersData);
}
Also used : ZMessage(com.zimbra.client.ZMessage) ZMailbox(com.zimbra.client.ZMailbox) Element(com.zimbra.common.soap.Element) SoapHttpTransport(com.zimbra.common.soap.SoapHttpTransport) Test(org.junit.Test)

Example 79 with ZMessage

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

the class TestSendAndReceive method verifyTextAttachmentLineEnding.

private void verifyTextAttachmentLineEnding(ZMailbox mbox, String content, String contentType) throws Exception {
    // Test simple send.
    String attachId = mbox.uploadAttachment("text.txt", content.getBytes(), contentType, 5000);
    String subject = NAME_PREFIX + " testTextAttachmentLineEnding " + contentType + " 1";
    TestUtil.sendMessage(mbox, USER_NAME, subject, "Testing text attachment", attachId);
    ZMessage msg = TestUtil.waitForMessage(mbox, "in:inbox subject:\"" + subject + "\"");
    InputStream in = mbox.getRESTResource("?id=" + msg.getId() + "&part=2");
    String attachContent = new String(ByteUtil.getContent(in, content.length()));
    Assert.assertEquals(content, attachContent);
    // Test save draft and send.
    attachId = mbox.uploadAttachment("text.txt", content.getBytes(), contentType, 5000);
    subject = NAME_PREFIX + " testTextAttachmentLineEnding " + contentType + " 2";
    TestUtil.saveDraftAndSendMessage(mbox, USER_NAME, subject, "Testing text attachment", attachId);
    msg = TestUtil.waitForMessage(mbox, "in:inbox subject:\"" + subject + "\"");
    in = mbox.getRESTResource("?id=" + msg.getId() + "&part=2");
    attachContent = new String(ByteUtil.getContent(in, content.length()));
    Assert.assertEquals(content, attachContent);
}
Also used : ZMessage(com.zimbra.client.ZMessage) InputStream(java.io.InputStream)

Example 80 with ZMessage

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

the class TestSendAndReceive method testSendDraftWithData.

@Test
public void testSendDraftWithData() throws Exception {
    TestUtil.createAccount(USER_NAME);
    TestUtil.createAccount(REMOTE_USER_NAME);
    MailSender.registerPreSendMailListener(listener);
    ZMailbox zmbox = TestUtil.getZMailbox(USER_NAME);
    String subj = "Thorin";
    String body = "far over the misty mountains cold";
    String customHeaderValue = "https://zss.server/file2";
    SoapHttpTransport transport = new SoapHttpTransport(TestUtil.getSoapUrl());
    transport.setAuthToken(zmbox.getAuthToken());
    //create a draft with a custom mime header
    Element request = new Element.JSONElement(MailConstants.SAVE_DRAFT_REQUEST);
    Element el = request.addUniqueElement(MailConstants.E_MSG);
    el.addAttribute(MailConstants.E_SUBJECT, subj);
    el.addUniqueElement(MailConstants.E_MIMEPART).addAttribute(MailConstants.A_CONTENT_TYPE, "text/plain").addAttribute(MailConstants.E_CONTENT, body);
    el.addNonUniqueElement(MailConstants.E_EMAIL).addAttribute(MailConstants.A_ADDRESS_TYPE, EmailType.TO.toString()).addAttribute(MailConstants.A_ADDRESS, TestUtil.addDomainIfNecessary(REMOTE_USER_NAME));
    el.addNonUniqueElement(MailConstants.E_HEADER).addAttribute(MailConstants.A_NAME, MailSender.PRE_SEND_HEADER).setText("custom");
    el.addNonUniqueElement(MailConstants.E_HEADER).addAttribute(MailConstants.A_NAME, PUBLIC_LIST_HEADER).setText(customHeaderValue);
    ZMessage draft = new ZMessage(transport.invoke(request).getElement(MailConstants.E_MSG), zmbox);
    //Send the draft and save to Sent folder
    request = new Element.JSONElement(MailConstants.SEND_MSG_REQUEST);
    request.addUniqueElement(MailConstants.E_MSG).addAttribute(MailConstants.A_DRAFT_ID, draft.getId()).addAttribute(MailConstants.A_SEND_FROM_DRAFT, true).addAttribute(MailConstants.A_NO_SAVE_TO_SENT, false);
    transport.invoke(request);
    Assert.assertTrue("Listener was not triggered", listener.isTriggered());
    Assert.assertEquals("listener should ahve been triggered only once", 1, listener.getTriggerCounter());
    String[] listenersData = listener.getData();
    Assert.assertNotNull("Listener did not get data from custom headers", listenersData);
    Assert.assertTrue("wrong number of elements in custom header data", listenersData.length == 1);
    Assert.assertTrue(String.format("wrong value in custom header: %s", listenersData[0]), customHeaderValue.equalsIgnoreCase(listenersData[0].replace("\"", "")));
}
Also used : ZMessage(com.zimbra.client.ZMessage) ZMailbox(com.zimbra.client.ZMailbox) Element(com.zimbra.common.soap.Element) SoapHttpTransport(com.zimbra.common.soap.SoapHttpTransport) 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