Search in sources :

Example 1 with SyncRequest

use of com.zimbra.soap.mail.message.SyncRequest in project zm-mailbox by Zimbra.

the class SyncTest method msgConversationsCutoff.

@Test
public void msgConversationsCutoff() throws Exception {
    Account acct = Provisioning.getInstance().get(Key.AccountBy.name, "test1@zimbra.com");
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(acct);
    mbox.beginTrackingSync();
    acct.setMailThreadingAlgorithm(MailThreadingAlgorithm.subject);
    int currTime = (int) (System.currentTimeMillis() / 1000);
    long before60min = currTime - (60 * Constants.SECONDS_PER_MINUTE);
    long before45min = currTime - (45 * Constants.SECONDS_PER_MINUTE);
    long before30min = currTime - (30 * Constants.SECONDS_PER_MINUTE);
    long before15min = currTime - (15 * Constants.SECONDS_PER_MINUTE);
    Message msg60minbefore = TestUtil.addMessage(mbox, Mailbox.ID_FOLDER_INBOX, "test1 x", before60min * 1000);
    Message msg45minbefore = TestUtil.addMessage(mbox, Mailbox.ID_FOLDER_INBOX, "Re: test1 x", before45min * 1000);
    Message msg30minbefore = TestUtil.addMessage(mbox, Mailbox.ID_FOLDER_INBOX, "test2 x", before30min * 1000);
    Message msg15minbefore = TestUtil.addMessage(mbox, Mailbox.ID_FOLDER_INBOX, "Re: test2 x", before15min * 1000);
    int convId1 = msg60minbefore.getConversationId();
    int convId2 = msg30minbefore.getConversationId();
    List<Integer> expectedConvIds = new ArrayList<Integer>();
    List<Integer> expectedMsgIds = new ArrayList<Integer>();
    // No message no conversation should be returned if msgCutOff is current time.
    SyncRequest request = new SyncRequest();
    request.setMsgCutoff(currTime);
    request.setFolderId("" + Mailbox.ID_FOLDER_ROOT);
    Map<String, Object> context = new HashMap<String, Object>();
    context.put(SoapEngine.ZIMBRA_CONTEXT, new ZimbraSoapContext(AuthProvider.getAuthToken(acct), acct.getId(), SoapProtocol.Soap12, SoapProtocol.Soap12));
    Element response = new Sync().handle(JaxbUtil.jaxbToElement(request), context);
    Pair<Integer, Element> pairFolderIdElement = new Pair<Integer, Element>(Mailbox.ID_FOLDER_CONVERSATIONS, null);
    getFolderElement(response, pairFolderIdElement);
    Assert.assertNotNull(pairFolderIdElement.getSecond());
    Assert.assertNull(pairFolderIdElement.getSecond().getOptionalElement(MailConstants.E_CONV));
    pairFolderIdElement = new Pair<Integer, Element>(Mailbox.ID_FOLDER_INBOX, null);
    getFolderElement(response, pairFolderIdElement);
    Assert.assertNotNull(pairFolderIdElement.getSecond());
    Assert.assertNull(pairFolderIdElement.getSecond().getOptionalElement(MailConstants.E_MSG));
    // 4 messages 2 conversation should be returned if msgCutOff is current time - 61 mins.
    request = new SyncRequest();
    request.setMsgCutoff(currTime - 61 * Constants.SECONDS_PER_MINUTE);
    request.setFolderId("" + Mailbox.ID_FOLDER_ROOT);
    response = new Sync().handle(JaxbUtil.jaxbToElement(request), context);
    expectedConvIds.add(convId1);
    expectedConvIds.add(convId2);
    Assert.assertTrue(verifyItemsPresentInResponse(response, Mailbox.ID_FOLDER_CONVERSATIONS, expectedConvIds, Type.CONVERSATION));
    expectedConvIds.clear();
    expectedMsgIds.add(msg60minbefore.getId());
    expectedMsgIds.add(msg45minbefore.getId());
    expectedMsgIds.add(msg30minbefore.getId());
    expectedMsgIds.add(msg15minbefore.getId());
    Assert.assertTrue(verifyItemsPresentInResponse(response, Mailbox.ID_FOLDER_INBOX, expectedMsgIds, Type.MESSAGE));
    expectedMsgIds.clear();
    // 2 messages 1 conversation should be returned if msgCutOff is current time - 31 mins.
    request = new SyncRequest();
    request.setMsgCutoff(currTime - 31 * Constants.SECONDS_PER_MINUTE);
    request.setFolderId("" + Mailbox.ID_FOLDER_ROOT);
    response = new Sync().handle(JaxbUtil.jaxbToElement(request), context);
    expectedConvIds.add(convId2);
    Assert.assertTrue(verifyItemsPresentInResponse(response, Mailbox.ID_FOLDER_CONVERSATIONS, expectedConvIds, Type.CONVERSATION));
    expectedConvIds.clear();
    expectedMsgIds.add(msg30minbefore.getId());
    expectedMsgIds.add(msg15minbefore.getId());
    Assert.assertTrue(verifyItemsPresentInResponse(response, Mailbox.ID_FOLDER_INBOX, expectedMsgIds, Type.MESSAGE));
    expectedMsgIds.clear();
    // 1 messages 1 conversation should be returned if msgCutOff is current time - 16 mins.
    request = new SyncRequest();
    request.setMsgCutoff(currTime - 16 * Constants.SECONDS_PER_MINUTE);
    request.setFolderId("" + Mailbox.ID_FOLDER_ROOT);
    response = new Sync().handle(JaxbUtil.jaxbToElement(request), context);
    expectedConvIds.add(convId2);
    Assert.assertTrue(verifyItemsPresentInResponse(response, Mailbox.ID_FOLDER_CONVERSATIONS, expectedConvIds, Type.CONVERSATION));
    expectedConvIds.clear();
    expectedMsgIds.add(msg15minbefore.getId());
    Assert.assertTrue(verifyItemsPresentInResponse(response, Mailbox.ID_FOLDER_INBOX, expectedMsgIds, Type.MESSAGE));
    expectedMsgIds.clear();
    // Delete message before 15mins no messages no conversation should be returned if msgCutOff is current time - 16 mins.
    mbox.delete(null, msg15minbefore.getId(), Type.MESSAGE);
    request = new SyncRequest();
    request.setMsgCutoff(currTime - 16 * Constants.SECONDS_PER_MINUTE);
    request.setFolderId("" + Mailbox.ID_FOLDER_ROOT);
    response = new Sync().handle(JaxbUtil.jaxbToElement(request), context);
    pairFolderIdElement = new Pair<Integer, Element>(Mailbox.ID_FOLDER_CONVERSATIONS, null);
    getFolderElement(response, pairFolderIdElement);
    Assert.assertNotNull(pairFolderIdElement.getSecond());
    Assert.assertNull(pairFolderIdElement.getSecond().getOptionalElement(MailConstants.E_CONV));
    pairFolderIdElement = new Pair<Integer, Element>(Mailbox.ID_FOLDER_INBOX, null);
    getFolderElement(response, pairFolderIdElement);
    Assert.assertNotNull(pairFolderIdElement.getSecond());
    Assert.assertNull(pairFolderIdElement.getSecond().getOptionalElement(MailConstants.E_MSG));
}
Also used : Account(com.zimbra.cs.account.Account) Message(com.zimbra.cs.mailbox.Message) HashMap(java.util.HashMap) Element(com.zimbra.common.soap.Element) ArrayList(java.util.ArrayList) Mailbox(com.zimbra.cs.mailbox.Mailbox) SyncRequest(com.zimbra.soap.mail.message.SyncRequest) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) Pair(com.zimbra.common.util.Pair) Test(org.junit.Test) MailboxTest(com.zimbra.cs.mailbox.MailboxTest)

Example 2 with SyncRequest

use of com.zimbra.soap.mail.message.SyncRequest in project zm-mailbox by Zimbra.

the class SyncTest method detaSyncTest.

/**
 * Test Sync should not send duplicate deletes.
 * @throws Exception
 */
@Test
public void detaSyncTest() throws Exception {
    Account acct = Provisioning.getInstance().get(Key.AccountBy.name, "test2@zimbra.com");
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(acct);
    mbox.beginTrackingSync();
    SyncRequest request = new SyncRequest();
    Map<String, Object> context = new HashMap<String, Object>();
    context.put(SoapEngine.ZIMBRA_CONTEXT, new ZimbraSoapContext(AuthProvider.getAuthToken(acct), acct.getId(), SoapProtocol.Soap12, SoapProtocol.Soap12));
    Element response = new Sync().handle(JaxbUtil.jaxbToElement(request), context);
    SyncResponse syncRes = JaxbUtil.elementToJaxb(response);
    String token = syncRes.getToken();
    int msgId1 = mbox.addMessage(null, MailboxTestUtil.generateMessage("test subject"), MailboxTest.STANDARD_DELIVERY_OPTIONS, null).getId();
    int msgId2 = mbox.addMessage(null, MailboxTestUtil.generateMessage("test subject"), MailboxTest.STANDARD_DELIVERY_OPTIONS, null).getId();
    request = new SyncRequest();
    request.setToken(token);
    response = new Sync().handle(JaxbUtil.jaxbToElement(request), context);
    syncRes = JaxbUtil.elementToJaxb(response);
    token = syncRes.getToken();
    // delta set setMaximumChangeCount=2 Add 3 message and delete 2 message.
    int msgId3 = mbox.addMessage(null, MailboxTestUtil.generateMessage("test subject"), MailboxTest.STANDARD_DELIVERY_OPTIONS, null).getId();
    mbox.addMessage(null, MailboxTestUtil.generateMessage("test subject"), MailboxTest.STANDARD_DELIVERY_OPTIONS, null).getId();
    mbox.addMessage(null, MailboxTestUtil.generateMessage("test subject"), MailboxTest.STANDARD_DELIVERY_OPTIONS, null).getId();
    mbox.delete(null, msgId1, Type.MESSAGE);
    mbox.delete(null, msgId2, Type.MESSAGE);
    request = new SyncRequest();
    request.setToken(token);
    request.setChangeLimit(2);
    response = new Sync().handle(JaxbUtil.jaxbToElement(request), context);
    syncRes = JaxbUtil.elementToJaxb(response);
    token = syncRes.getToken();
    // Expected all deletes and 2 added message. and hasMore=true
    SyncDeletedInfo sdi1 = syncRes.getDeleted();
    String[] deletes = sdi1.getIds().split(",");
    Assert.assertEquals(2, syncRes.getItems().size());
    Assert.assertEquals(2, deletes.length);
    mbox.delete(null, msgId3, Type.MESSAGE);
    request = new SyncRequest();
    request.setToken(token);
    request.setChangeLimit(2);
    response = new Sync().handle(JaxbUtil.jaxbToElement(request), context);
    syncRes = JaxbUtil.elementToJaxb(response);
    // It should return 1 modified and 1 deleted.
    SyncDeletedInfo sdi2 = syncRes.getDeleted();
    deletes = sdi2.getIds().split(",");
    Assert.assertEquals(1, syncRes.getItems().size());
    Assert.assertEquals(1, deletes.length);
    token = syncRes.getToken();
    // Add 3 messages 1 with current date and 2 past date.
    int currTime = (int) (System.currentTimeMillis() / 1000);
    long before60min = currTime - (60 * Constants.SECONDS_PER_MINUTE);
    long before45min = currTime - (45 * Constants.SECONDS_PER_MINUTE);
    TestUtil.addMessage(mbox, Mailbox.ID_FOLDER_INBOX, "test1 x", before60min * 1000);
    TestUtil.addMessage(mbox, Mailbox.ID_FOLDER_INBOX, "test2 x", before45min * 1000);
    mbox.addMessage(null, MailboxTestUtil.generateMessage("test3 x"), MailboxTest.STANDARD_DELIVERY_OPTIONS, null);
    // Delta Sync without msgCutoff. should return all 3 added messages.
    request = new SyncRequest();
    request.setToken(token);
    request.setChangeLimit(5);
    response = new Sync().handle(JaxbUtil.jaxbToElement(request), context);
    syncRes = JaxbUtil.elementToJaxb(response);
    // It should return 3 modified.
    Assert.assertEquals(3, syncRes.getItems().size());
    // Delta Sync with msgCutoff before45min -1. should return only 2 of added messages.
    request = new SyncRequest();
    request.setToken(token);
    request.setChangeLimit(5);
    request.setMsgCutoff(currTime - (46 * Constants.SECONDS_PER_MINUTE));
    response = new Sync().handle(JaxbUtil.jaxbToElement(request), context);
    syncRes = JaxbUtil.elementToJaxb(response);
    Assert.assertEquals(2, syncRes.getItems().size());
    // Delta Sync with msgCutoff currenttime -1. should return only 1 of added messages.
    request = new SyncRequest();
    request.setToken(token);
    request.setChangeLimit(5);
    request.setMsgCutoff(currTime - 1);
    response = new Sync().handle(JaxbUtil.jaxbToElement(request), context);
    syncRes = JaxbUtil.elementToJaxb(response);
    Assert.assertEquals(1, syncRes.getItems().size());
}
Also used : Account(com.zimbra.cs.account.Account) HashMap(java.util.HashMap) Element(com.zimbra.common.soap.Element) Mailbox(com.zimbra.cs.mailbox.Mailbox) SyncRequest(com.zimbra.soap.mail.message.SyncRequest) SyncResponse(com.zimbra.soap.mail.message.SyncResponse) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) SyncDeletedInfo(com.zimbra.soap.mail.type.SyncDeletedInfo) Test(org.junit.Test) MailboxTest(com.zimbra.cs.mailbox.MailboxTest)

Example 3 with SyncRequest

use of com.zimbra.soap.mail.message.SyncRequest in project zm-mailbox by Zimbra.

the class Sync method handle.

@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Mailbox mbox = getRequestedMailbox(zsc);
    OperationContext octxt = getOperationContext(zsc, context);
    ItemIdFormatter ifmt = new ItemIdFormatter(zsc);
    SyncRequest syncRequest = zsc.elementToJaxb(request);
    String token = syncRequest.getToken();
    Element response = zsc.createElement(MailConstants.SYNC_RESPONSE);
    response.addAttribute(MailConstants.A_CHANGE_DATE, System.currentTimeMillis() / 1000);
    // the sync token is of the form "last fully synced change id" (e.g. "32425") or
    // last fully synced change id-last item synced in next change id" (e.g. "32425-99213") or
    // last fully synced change id-last item synced in next change id and last fully synced delete change id" (e.g. "32425-99213:d1231232") or
    // last fully synced change id-last item synced in next change id and
    // last fully synced delete id-last item synced in next delete id (e.g. "32425-99213:d12312-82134")
    SyncToken syncToken = null;
    int tokenInt = 0;
    if (!StringUtil.isNullOrEmpty(token)) {
        syncToken = new SyncToken(token);
        tokenInt = syncToken.getChangeId();
    }
    if (syncToken == null) {
        syncToken = new SyncToken(0);
    }
    int deleleLimit = syncRequest.getDeleteLimit();
    int changeLimit = syncRequest.getChangeLimit();
    // server can apply delete pagination through debugconfig/localconfig.
    if (deleleLimit <= 0) {
        deleleLimit = DebugConfig.syncMaximumDeleteCount;
    }
    // client specify more than DebugConfig.syncMaximumChangeCount It will use DebugConfig.syncMaximumChangeCount
    if (changeLimit <= 0 || changeLimit > DebugConfig.syncMaximumChangeCount) {
        changeLimit = DebugConfig.syncMaximumChangeCount;
    }
    boolean initialSync = tokenInt <= 0;
    // permit the caller to restrict initial sync only to calendar items with a recurrence after a given date
    long calendarStart = (syncRequest.getCalendarCutoff() != null) ? syncRequest.getCalendarCutoff() : -1;
    int messageSyncStart = (syncRequest.getMsgCutoff() != null) ? syncRequest.getMsgCutoff() : -1;
    // if the sync is constrained to a folder subset, we need to first figure out what can be seen
    Folder root = null;
    ItemId iidFolder = null;
    try {
        iidFolder = new ItemId(request.getAttribute(MailConstants.A_FOLDER, DEFAULT_FOLDER_ID + ""), zsc);
        OperationContext octxtOwner = new OperationContext(mbox);
        root = mbox.getFolderById(octxtOwner, iidFolder.getId());
    } catch (MailServiceException.NoSuchItemException nsie) {
    }
    Set<Folder> visible = octxt.isDelegatedRequest(mbox) ? mbox.getVisibleFolders(octxt) : null;
    FolderNode rootNode = null;
    if (root == null || iidFolder == null) {
        // resolve grantee names of all ACLs on the mailbox
        rootNode = mbox.getFolderTree(octxt, null, true);
    } else {
        // resolve grantee names of all ACLs on all sub-folders of the requested folder
        rootNode = mbox.getFolderTree(octxt, iidFolder, true);
    }
    OperationContextData.addGranteeNames(octxt, rootNode);
    // actually perform the sync
    mbox.lock.lock();
    try {
        mbox.beginTrackingSync();
        if (initialSync) {
            response.addAttribute(MailConstants.A_TOKEN, mbox.getLastChangeID());
            response.addAttribute(MailConstants.A_SIZE, mbox.getSize());
            boolean anyFolders = folderSync(response, octxt, ifmt, mbox, root, visible, calendarStart, messageSyncStart, SyncPhase.INITIAL);
            // if no folders are visible, add an empty "<folder/>" as a hint
            if (!anyFolders) {
                response.addElement(MailConstants.E_FOLDER);
            }
        } else {
            boolean typedDeletes = request.getAttributeBool(MailConstants.A_TYPED_DELETES, false);
            String newToken = deltaSync(response, octxt, ifmt, mbox, syncToken, deleleLimit, changeLimit, typedDeletes, root, visible, messageSyncStart);
            response.addAttribute(MailConstants.A_TOKEN, newToken);
        }
    } finally {
        mbox.lock.release();
    }
    return response;
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) FolderNode(com.zimbra.cs.mailbox.Mailbox.FolderNode) ItemIdFormatter(com.zimbra.cs.service.util.ItemIdFormatter) Element(com.zimbra.common.soap.Element) Folder(com.zimbra.cs.mailbox.Folder) ItemId(com.zimbra.cs.service.util.ItemId) SyncToken(com.zimbra.cs.service.util.SyncToken) Mailbox(com.zimbra.cs.mailbox.Mailbox) SyncRequest(com.zimbra.soap.mail.message.SyncRequest) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) MailServiceException(com.zimbra.cs.mailbox.MailServiceException)

Example 4 with SyncRequest

use of com.zimbra.soap.mail.message.SyncRequest in project zm-mailbox by Zimbra.

the class SyncTest method detaSyncMovedMessageTest.

/**
 * Test Sync should not send duplicate deletes.
 * @throws Exception
 */
@Test
public void detaSyncMovedMessageTest() throws Exception {
    Account acct = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(acct);
    mbox.beginTrackingSync();
    SyncRequest request = new SyncRequest();
    request.setFolderId("2");
    Map<String, Object> context = new HashMap<String, Object>();
    Set<Integer> deleted = new HashSet<Integer>();
    context.put(SoapEngine.ZIMBRA_CONTEXT, new ZimbraSoapContext(AuthProvider.getAuthToken(acct), acct.getId(), SoapProtocol.Soap12, SoapProtocol.Soap12));
    Element response = new Sync().handle(JaxbUtil.jaxbToElement(request), context);
    SyncResponse syncRes = JaxbUtil.elementToJaxb(response);
    String token = syncRes.getToken();
    int msgId1 = mbox.addMessage(null, MailboxTestUtil.generateMessage("test subject"), MailboxTest.STANDARD_DELIVERY_OPTIONS, null).getId();
    int msgId2 = mbox.addMessage(null, MailboxTestUtil.generateMessage("test subject"), MailboxTest.STANDARD_DELIVERY_OPTIONS, null).getId();
    request = new SyncRequest();
    request.setFolderId("2");
    request.setToken(token);
    response = new Sync().handle(JaxbUtil.jaxbToElement(request), context);
    syncRes = JaxbUtil.elementToJaxb(response);
    token = syncRes.getToken();
    // delta set setMaximumChangeCount=2 Add 3 message and delete 2 message.
    int msgId3 = mbox.addMessage(null, MailboxTestUtil.generateMessage("test subject"), MailboxTest.STANDARD_DELIVERY_OPTIONS, null).getId();
    mbox.addMessage(null, MailboxTestUtil.generateMessage("test subject"), MailboxTest.STANDARD_DELIVERY_OPTIONS, null).getId();
    mbox.addMessage(null, MailboxTestUtil.generateMessage("test subject"), MailboxTest.STANDARD_DELIVERY_OPTIONS, null).getId();
    mbox.move(null, msgId1, Type.MESSAGE, 5);
    mbox.move(null, msgId2, Type.MESSAGE, 5);
    deleted.add(msgId1);
    deleted.add(msgId2);
    request = new SyncRequest();
    request.setChangeLimit(2);
    request.setToken(token);
    request.setFolderId("2");
    response = new Sync().handle(JaxbUtil.jaxbToElement(request), context);
    syncRes = JaxbUtil.elementToJaxb(response);
    token = syncRes.getToken();
    // Expected all deletes and 2 added message. and hasMore=true
    Assert.assertEquals(2, syncRes.getItems().size());
    SyncDeletedInfo sdi1 = syncRes.getDeleted();
    String[] deletes = sdi1.getIds().split(",");
    removeDeleteFromList(deleted, deletes);
    Assert.assertTrue(syncRes.getMore());
    Assert.assertTrue(deleted.isEmpty());
    // Move one more message.
    mbox.move(null, msgId3, Type.MESSAGE, 5);
    request = new SyncRequest();
    request.setFolderId("2");
    request.setToken(token);
    response = new Sync().handle(JaxbUtil.jaxbToElement(request), context);
    syncRes = JaxbUtil.elementToJaxb(response);
    // It should return 1 modified and 1 deleted.
    SyncDeletedInfo sdi2 = syncRes.getDeleted();
    deletes = sdi2.getIds().split(",");
    Assert.assertEquals(1, syncRes.getItems().size());
    Assert.assertEquals(1, deletes.length);
}
Also used : Account(com.zimbra.cs.account.Account) HashMap(java.util.HashMap) Element(com.zimbra.common.soap.Element) Mailbox(com.zimbra.cs.mailbox.Mailbox) SyncRequest(com.zimbra.soap.mail.message.SyncRequest) SyncResponse(com.zimbra.soap.mail.message.SyncResponse) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) SyncDeletedInfo(com.zimbra.soap.mail.type.SyncDeletedInfo) HashSet(java.util.HashSet) Test(org.junit.Test) MailboxTest(com.zimbra.cs.mailbox.MailboxTest)

Example 5 with SyncRequest

use of com.zimbra.soap.mail.message.SyncRequest in project zm-mailbox by Zimbra.

the class SyncTest method testPaginatedSync.

/**
 * Test Paginations.
 * 1. Modified paged , delete paged (del cutoff modseq <  modified cutoff mod Seq)
 * 2. All modified , delete paged.
 * 3. Modified paged , delete paged (del cutoff modseq >  modified cutoff mod Seq)
 * 4. Modified paged , all deletes.
 * 5. Modified and delete unpaged.
 * @throws Exception
 */
@Test
public void testPaginatedSync() throws Exception {
    Account acct = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(acct);
    mbox.beginTrackingSync();
    SyncRequest request = new SyncRequest();
    request.setFolderId("2");
    Map<String, Object> context = new HashMap<String, Object>();
    context.put(SoapEngine.ZIMBRA_CONTEXT, new ZimbraSoapContext(AuthProvider.getAuthToken(acct), acct.getId(), SoapProtocol.Soap12, SoapProtocol.Soap12));
    Element response = new Sync().handle(JaxbUtil.jaxbToElement(request), context);
    SyncResponse syncRes = JaxbUtil.elementToJaxb(response);
    Set<Integer> itemsDeleted = new HashSet<Integer>();
    Set<Integer> itemsAddedOrModified = new HashSet<Integer>();
    String token = syncRes.getToken();
    int msgId1 = mbox.addMessage(null, MailboxTestUtil.generateMessage("test subject"), MailboxTest.STANDARD_DELIVERY_OPTIONS, null).getId();
    int msgId2 = mbox.addMessage(null, MailboxTestUtil.generateMessage("test subject"), MailboxTest.STANDARD_DELIVERY_OPTIONS, null).getId();
    int msgId3 = mbox.addMessage(null, MailboxTestUtil.generateMessage("test subject"), MailboxTest.STANDARD_DELIVERY_OPTIONS, null).getId();
    int msgId4 = mbox.addMessage(null, MailboxTestUtil.generateMessage("test subject"), MailboxTest.STANDARD_DELIVERY_OPTIONS, null).getId();
    int msgId5 = mbox.addMessage(null, MailboxTestUtil.generateMessage("test subject"), MailboxTest.STANDARD_DELIVERY_OPTIONS, null).getId();
    int msgId6 = mbox.addMessage(null, MailboxTestUtil.generateMessage("test subject"), MailboxTest.STANDARD_DELIVERY_OPTIONS, null).getId();
    itemsAddedOrModified.add(msgId1);
    itemsAddedOrModified.add(msgId2);
    itemsAddedOrModified.add(msgId3);
    itemsAddedOrModified.add(msgId4);
    itemsAddedOrModified.add(msgId5);
    itemsAddedOrModified.add(msgId6);
    request = new SyncRequest();
    request.setChangeLimit(100);
    request.setFolderId("2");
    request.setToken(token);
    response = new Sync().handle(JaxbUtil.jaxbToElement(request), context);
    syncRes = JaxbUtil.elementToJaxb(response);
    List<Object> listObj = syncRes.getItems();
    removeItemsFromList(itemsAddedOrModified, listObj);
    Assert.assertTrue(itemsAddedOrModified.isEmpty());
    token = syncRes.getToken();
    // Modified paged , delete paged (del cutoff modseq < modified cutoff mod Seq);
    mbox.move(null, msgId1, Type.MESSAGE, 5);
    mbox.move(null, msgId2, Type.MESSAGE, 5);
    mbox.delete(null, msgId3, Type.MESSAGE);
    mbox.alterTag(null, new int[] { msgId5, msgId6, msgId4 }, MailItem.Type.MESSAGE, Flag.FlagInfo.UNREAD, true, null);
    itemsDeleted.add(msgId1);
    itemsDeleted.add(msgId2);
    itemsDeleted.add(msgId3);
    itemsAddedOrModified.add(msgId4);
    itemsAddedOrModified.add(msgId5);
    itemsAddedOrModified.add(msgId6);
    request = new SyncRequest();
    request.setToken(token);
    request.setFolderId("2");
    request.setChangeLimit(2);
    request.setDeleteLimit(2);
    response = new Sync().handle(JaxbUtil.jaxbToElement(request), context);
    syncRes = JaxbUtil.elementToJaxb(response);
    token = syncRes.getToken();
    // Expected 2 deletes and 2 added message. and hasMore=true
    SyncDeletedInfo sdi1 = syncRes.getDeleted();
    String[] deletes = sdi1.getIds().split(",");
    Assert.assertEquals(2, deletes.length);
    removeDeleteFromList(itemsDeleted, deletes);
    // pending to sync.
    Assert.assertEquals(1, itemsDeleted.size());
    listObj = syncRes.getItems();
    Assert.assertEquals(2, listObj.size());
    removeItemsFromList(itemsAddedOrModified, listObj);
    // pending to sync.
    Assert.assertEquals(1, itemsAddedOrModified.size());
    Assert.assertTrue(syncRes.getMore());
    SyncToken syncToken = new SyncToken(token);
    int lastChange = syncToken.getChangeId();
    int lastDel = syncToken.getDeleteModSeq();
    Assert.assertTrue(lastDel < lastChange);
    // Test3: previous interrupted sync. All modified and delete paged.
    mbox.move(null, msgId5, Type.MESSAGE, 5);
    mbox.delete(null, msgId4, Type.MESSAGE);
    itemsDeleted.add(msgId4);
    itemsDeleted.add(msgId5);
    request = new SyncRequest();
    request.setToken(token);
    request.setFolderId("2");
    request.setDeleteLimit(2);
    request.setChangeLimit(2);
    response = new Sync().handle(JaxbUtil.jaxbToElement(request), context);
    syncRes = JaxbUtil.elementToJaxb(response);
    token = syncRes.getToken();
    // Expected all modified and 2 deleted message. and hasMore=true
    SyncDeletedInfo sdi2 = syncRes.getDeleted();
    deletes = sdi2.getIds().split(",");
    Assert.assertEquals(2, deletes.length);
    removeDeleteFromList(itemsDeleted, deletes);
    // pending to sync.
    Assert.assertEquals(1, itemsDeleted.size());
    listObj = syncRes.getItems();
    Assert.assertEquals(1, listObj.size());
    removeItemsFromList(itemsAddedOrModified, listObj);
    // All synced.
    Assert.assertEquals(0, itemsAddedOrModified.size());
    Assert.assertTrue(syncRes.getMore());
    syncToken = new SyncToken(token);
    lastChange = syncToken.getChangeId();
    lastDel = syncToken.getDeleteModSeq();
    Assert.assertTrue(lastDel < lastChange);
    Assert.assertTrue(syncRes.getMore());
    // Test3: previous interrupted sync. modified and delete paged.(del cutoff modseq > modified cutoff mod Seq)
    int msgId7 = mbox.addMessage(null, MailboxTestUtil.generateMessage("test subject"), MailboxTest.STANDARD_DELIVERY_OPTIONS, null).getId();
    int msgId8 = mbox.addMessage(null, MailboxTestUtil.generateMessage("test subject"), MailboxTest.STANDARD_DELIVERY_OPTIONS, null).getId();
    int msgId9 = mbox.addMessage(null, MailboxTestUtil.generateMessage("test subject"), MailboxTest.STANDARD_DELIVERY_OPTIONS, null).getId();
    mbox.alterTag(null, new int[] { msgId9 }, MailItem.Type.MESSAGE, Flag.FlagInfo.UNREAD, true, null);
    mbox.delete(null, msgId6, Type.MESSAGE);
    itemsAddedOrModified.add(msgId7);
    itemsAddedOrModified.add(msgId8);
    itemsAddedOrModified.add(msgId9);
    itemsDeleted.add(msgId6);
    request = new SyncRequest();
    request.setToken(token);
    request.setFolderId("2");
    request.setChangeLimit(2);
    request.setDeleteLimit(1);
    response = new Sync().handle(JaxbUtil.jaxbToElement(request), context);
    syncRes = JaxbUtil.elementToJaxb(response);
    token = syncRes.getToken();
    // Expected 1 deletes and 2 added message. and hasMore=true
    SyncDeletedInfo sdi3 = syncRes.getDeleted();
    deletes = sdi3.getIds().split(",");
    Assert.assertEquals(1, deletes.length);
    removeDeleteFromList(itemsDeleted, deletes);
    // pending to sync.
    Assert.assertEquals(1, itemsDeleted.size());
    listObj = syncRes.getItems();
    Assert.assertEquals(2, listObj.size());
    removeItemsFromList(itemsAddedOrModified, listObj);
    // pending to sync.
    Assert.assertEquals(1, itemsAddedOrModified.size());
    Assert.assertTrue(syncRes.getMore());
    syncToken = new SyncToken(token);
    lastChange = syncToken.getChangeId();
    lastDel = syncToken.getDeleteModSeq();
    Assert.assertTrue(lastDel > lastChange);
    Assert.assertTrue(syncRes.getMore());
    // Test4: previous interrupted sync. Modified paged and All Deletes.
    int msgId10 = mbox.addMessage(null, MailboxTestUtil.generateMessage("test subject"), MailboxTest.STANDARD_DELIVERY_OPTIONS, null).getId();
    int msgId11 = mbox.addMessage(null, MailboxTestUtil.generateMessage("test subject"), MailboxTest.STANDARD_DELIVERY_OPTIONS, null).getId();
    int msgId12 = mbox.addMessage(null, MailboxTestUtil.generateMessage("test subject"), MailboxTest.STANDARD_DELIVERY_OPTIONS, null).getId();
    mbox.alterTag(null, new int[] { msgId9 }, MailItem.Type.MESSAGE, Flag.FlagInfo.UNREAD, true, null);
    mbox.delete(null, msgId9, Type.MESSAGE);
    itemsAddedOrModified.add(msgId10);
    itemsAddedOrModified.add(msgId11);
    itemsAddedOrModified.add(msgId12);
    itemsAddedOrModified.remove(msgId9);
    itemsDeleted.add(msgId9);
    request = new SyncRequest();
    request.setToken(token);
    request.setFolderId("2");
    request.setDeleteLimit(2);
    request.setChangeLimit(2);
    response = new Sync().handle(JaxbUtil.jaxbToElement(request), context);
    syncRes = JaxbUtil.elementToJaxb(response);
    token = syncRes.getToken();
    // Expected 2 deletes and 2 added message. and hasMore=true.
    // trick: deleted the remaining to sync modified message from last sync.
    SyncDeletedInfo sdi4 = syncRes.getDeleted();
    deletes = sdi4.getIds().split(",");
    Assert.assertEquals(2, deletes.length);
    removeDeleteFromList(itemsDeleted, deletes);
    // All synced.
    Assert.assertEquals(0, itemsDeleted.size());
    listObj = syncRes.getItems();
    Assert.assertEquals(2, listObj.size());
    removeItemsFromList(itemsAddedOrModified, listObj);
    // pending to sync.
    Assert.assertEquals(1, itemsAddedOrModified.size());
    Assert.assertTrue(syncRes.getMore());
    syncToken = new SyncToken(token);
    lastChange = syncToken.getChangeId();
    lastDel = syncToken.getDeleteModSeq();
    Assert.assertTrue(lastDel == mbox.getLastChangeID());
    Assert.assertTrue(syncRes.getMore());
    // Test5: All deletes and all Modified.
    int msgId13 = mbox.addMessage(null, MailboxTestUtil.generateMessage("test subject"), MailboxTest.STANDARD_DELIVERY_OPTIONS, null).getId();
    mbox.alterTag(null, new int[] { msgId11 }, MailItem.Type.MESSAGE, Flag.FlagInfo.UNREAD, true, null);
    mbox.delete(null, msgId10, Type.MESSAGE);
    itemsAddedOrModified.add(msgId13);
    itemsAddedOrModified.add(msgId11);
    itemsDeleted.add(msgId10);
    request = new SyncRequest();
    request.setToken(token);
    request.setFolderId("2");
    request.setDeleteLimit(0);
    request.setChangeLimit(10);
    response = new Sync().handle(JaxbUtil.jaxbToElement(request), context);
    syncRes = JaxbUtil.elementToJaxb(response);
    token = syncRes.getToken();
    // Expected 2 deletes and 2 added message. and hasMore=true.
    // trick: deleted the remaining to sync modified message from last sync.
    SyncDeletedInfo sdi5 = syncRes.getDeleted();
    deletes = sdi5.getIds().split(",");
    Assert.assertEquals(1, deletes.length);
    removeDeleteFromList(itemsDeleted, deletes);
    // All synced.
    Assert.assertEquals(0, itemsDeleted.size());
    listObj = syncRes.getItems();
    Assert.assertEquals(3, listObj.size());
    removeItemsFromList(itemsAddedOrModified, listObj);
    // All synced.
    Assert.assertEquals(0, itemsAddedOrModified.size());
    syncToken = new SyncToken(token);
    lastChange = syncToken.getChangeId();
    lastDel = syncToken.getDeleteModSeq();
    Assert.assertEquals(lastChange, mbox.getLastChangeID());
    Assert.assertTrue(lastDel == -1);
    Assert.assertNull(syncRes.getMore());
}
Also used : Account(com.zimbra.cs.account.Account) HashMap(java.util.HashMap) Element(com.zimbra.common.soap.Element) SyncToken(com.zimbra.cs.service.util.SyncToken) Mailbox(com.zimbra.cs.mailbox.Mailbox) SyncRequest(com.zimbra.soap.mail.message.SyncRequest) SyncResponse(com.zimbra.soap.mail.message.SyncResponse) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) SyncDeletedInfo(com.zimbra.soap.mail.type.SyncDeletedInfo) HashSet(java.util.HashSet) Test(org.junit.Test) MailboxTest(com.zimbra.cs.mailbox.MailboxTest)

Aggregations

Element (com.zimbra.common.soap.Element)5 Mailbox (com.zimbra.cs.mailbox.Mailbox)5 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)5 SyncRequest (com.zimbra.soap.mail.message.SyncRequest)5 Account (com.zimbra.cs.account.Account)4 MailboxTest (com.zimbra.cs.mailbox.MailboxTest)4 HashMap (java.util.HashMap)4 Test (org.junit.Test)4 SyncResponse (com.zimbra.soap.mail.message.SyncResponse)3 SyncDeletedInfo (com.zimbra.soap.mail.type.SyncDeletedInfo)3 SyncToken (com.zimbra.cs.service.util.SyncToken)2 HashSet (java.util.HashSet)2 Pair (com.zimbra.common.util.Pair)1 Folder (com.zimbra.cs.mailbox.Folder)1 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)1 FolderNode (com.zimbra.cs.mailbox.Mailbox.FolderNode)1 Message (com.zimbra.cs.mailbox.Message)1 OperationContext (com.zimbra.cs.mailbox.OperationContext)1 ItemId (com.zimbra.cs.service.util.ItemId)1 ItemIdFormatter (com.zimbra.cs.service.util.ItemIdFormatter)1