Search in sources :

Example 1 with SyncResponse

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

the class SyncResponseTest method unmarshallSyncResponse.

@Test
public void unmarshallSyncResponse() throws Exception {
    //expected items structure
    List<Object> expected = new ArrayList<Object>();
    MessageSummary msg = new MessageSummary("" + 1460);
    expected.add(msg);
    msg = new MessageSummary("" + 1463);
    expected.add(msg);
    msg = new MessageSummary("" + 1464);
    expected.add(msg);
    ConversationSummary conv = new ConversationSummary("" + 1461);
    expected.add(conv);
    List<IdsAttr> ids = new ArrayList<IdsAttr>();
    IdsAttr messageIds = new MsgIdsAttr("1462,1459,1458,1456,1446,1438,1435,1432,576,564,560");
    ids.add(messageIds);
    SyncFolder folder = new SyncFolder(ids);
    folder.setId(5);
    expected.add(folder);
    ids = new ArrayList<IdsAttr>();
    IdsAttr convIds = new ConvIdsAttr("1461,1457");
    ids.add(convIds);
    folder = new SyncFolder(ids);
    folder.setId(9);
    expected.add(folder);
    SyncResponse resp = (SyncResponse) unmarshaller.unmarshal(getClass().getResourceAsStream("SyncResponse.xml"));
    Assert.assertEquals("1244", resp.getToken());
    List<Object> items = resp.getItems();
    //verify items
    Assert.assertEquals(expected.size(), items.size());
    for (Iterator<Object> it = expected.iterator(); it.hasNext(); ) {
        Object expect = it.next();
        MessageSummary expectedMsg = null;
        ConversationSummary expectedConv = null;
        SyncFolder expectedSyncFolder = null;
        if (expect instanceof MessageSummary) {
            expectedMsg = (MessageSummary) expect;
        } else if (expect instanceof ConversationSummary) {
            expectedConv = (ConversationSummary) expect;
        } else if (expect instanceof SyncFolder) {
            expectedSyncFolder = (SyncFolder) expect;
        }
        boolean matched = false;
        for (Object item : items) {
            if (expect.getClass() == item.getClass()) {
                if (expectedMsg != null) {
                    msg = (MessageSummary) item;
                    if (expectedMsg.getId().equals(msg.getId())) {
                        it.remove();
                        matched = true;
                        break;
                    }
                } else if (expectedConv != null) {
                    conv = (ConversationSummary) item;
                    if (expectedConv.getId().equals(conv.getId())) {
                        it.remove();
                        matched = true;
                        break;
                    }
                } else if (expectedSyncFolder != null) {
                    folder = (SyncFolder) item;
                    if (expectedSyncFolder.getId().equals(folder.getId())) {
                        List<IdsAttr> expectedItems = expectedSyncFolder.getItemIds();
                        List<IdsAttr> folderItems = folder.getItemIds();
                        for (Iterator<IdsAttr> expectIdsIterator = expectedItems.iterator(); expectIdsIterator.hasNext(); ) {
                            IdsAttr expectedIdsAttr = expectIdsIterator.next();
                            for (IdsAttr encodedIds : folderItems) {
                                if (encodedIds.getClass() == expectedIdsAttr.getClass()) {
                                    if (encodedIds.getIds().equals(expectedIdsAttr.getIds())) {
                                        expectIdsIterator.remove();
                                    }
                                }
                            }
                        }
                        if (expectedItems.isEmpty()) {
                            it.remove();
                            matched = true;
                            break;
                        }
                    }
                }
            }
        }
        Assert.assertTrue("item not found " + expected, matched);
    }
    Assert.assertTrue(expected.isEmpty());
    //typed deletes
    SyncDeletedInfo deleted = resp.getDeleted();
    Assert.assertEquals("1455,578,1448,1443,1451,1441,1454", deleted.getIds());
    List<IdsAttr> deletesByType = deleted.getTypes();
    //should be two
    boolean foundConvs = false;
    boolean foundMsg = false;
    for (IdsAttr deleteIds : deletesByType) {
        if (deleteIds instanceof ConvIdsAttr && deleteIds.getIds().equals("1455,999")) {
            foundConvs = true;
        } else if (deleteIds instanceof MsgIdsAttr && deleteIds.getIds().equals("578,1448,1443,1451,1441,1454")) {
            foundMsg = true;
        }
    }
    Assert.assertTrue(foundConvs);
    Assert.assertTrue(foundMsg);
}
Also used : MsgIdsAttr(com.zimbra.soap.mail.type.MsgIdsAttr) ArrayList(java.util.ArrayList) ConvIdsAttr(com.zimbra.soap.mail.type.ConvIdsAttr) SyncResponse(com.zimbra.soap.mail.message.SyncResponse) IdsAttr(com.zimbra.soap.mail.type.IdsAttr) MsgIdsAttr(com.zimbra.soap.mail.type.MsgIdsAttr) ConvIdsAttr(com.zimbra.soap.mail.type.ConvIdsAttr) SyncDeletedInfo(com.zimbra.soap.mail.type.SyncDeletedInfo) MessageSummary(com.zimbra.soap.mail.type.MessageSummary) ConversationSummary(com.zimbra.soap.mail.type.ConversationSummary) SyncFolder(com.zimbra.soap.mail.type.SyncFolder) Test(org.junit.Test)

Example 2 with SyncResponse

use of com.zimbra.soap.mail.message.SyncResponse 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 SyncResponse

use of com.zimbra.soap.mail.message.SyncResponse 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)

Example 4 with SyncResponse

use of com.zimbra.soap.mail.message.SyncResponse 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)

Aggregations

SyncResponse (com.zimbra.soap.mail.message.SyncResponse)4 SyncDeletedInfo (com.zimbra.soap.mail.type.SyncDeletedInfo)4 Test (org.junit.Test)4 Element (com.zimbra.common.soap.Element)3 Account (com.zimbra.cs.account.Account)3 Mailbox (com.zimbra.cs.mailbox.Mailbox)3 MailboxTest (com.zimbra.cs.mailbox.MailboxTest)3 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)3 SyncRequest (com.zimbra.soap.mail.message.SyncRequest)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)2 SyncToken (com.zimbra.cs.service.util.SyncToken)1 ConvIdsAttr (com.zimbra.soap.mail.type.ConvIdsAttr)1 ConversationSummary (com.zimbra.soap.mail.type.ConversationSummary)1 IdsAttr (com.zimbra.soap.mail.type.IdsAttr)1 MessageSummary (com.zimbra.soap.mail.type.MessageSummary)1 MsgIdsAttr (com.zimbra.soap.mail.type.MsgIdsAttr)1 SyncFolder (com.zimbra.soap.mail.type.SyncFolder)1 ArrayList (java.util.ArrayList)1