Search in sources :

Example 51 with Contact

use of com.zimbra.cs.mailbox.Contact in project zm-mailbox by Zimbra.

the class TestZClient method testCreateAndModifyZContact.

@Test
public void testCreateAndModifyZContact() throws ServiceException {
    Mailbox mbox = TestUtil.getMailbox(USER_NAME);
    ZMailbox zmbox = TestUtil.getZMailbox(USER_NAME);
    Map<String, String> attrs = Maps.newHashMapWithExpectedSize(3);
    attrs.put(ContactConstants.A_email, "fun@example.com");
    attrs.put(ContactConstants.A_fullName, "Barney A. Rubble");
    ZContact zcontact = zmbox.createContact(ZFolder.ID_CONTACTS, null, /* tags */
    attrs);
    assertNotNull("Newly created zcontact should not be null", zcontact);
    assertEquals("Expected=ZContact ID Actual=ZContact IMAP UID", zcontact.getIdInMailbox(), zcontact.getImapUid());
    Contact contact = mbox.getContactById(null, zcontact.getIdInMailbox());
    assertNotNull("Contact object gotten by ID on newly created zcontact should not be null", zcontact);
    assertEquals("IMAP UID Expected=Contact Actual=ZContact", contact.getImapUid(), zcontact.getImapUid());
    assertEquals("ModifiedSequence Expected=Contact Actual=ZContact", contact.getModifiedSequence(), zcontact.getModifiedSequence());
    int origModSeq = zcontact.getModifiedSequence();
    attrs.put(ContactConstants.A_company, "Acme Inc.");
    zcontact = zmbox.modifyContact(zcontact.getId(), true, /* i.e replace all */
    attrs);
    assertNotNull("Contact object from zmbox.modifyContact() should not be null", zcontact);
    assertEquals("After Modify:Expected=ZContact ID Actual=ZContact IMAP UID", zcontact.getIdInMailbox(), zcontact.getImapUid());
    contact = mbox.getContactById(null, zcontact.getIdInMailbox());
    assertNotNull("After Modify:Contact object gotten by ID on newly modified zcontact should not be null", contact);
    assertEquals("After Modify:IMAP UID:Expected=Contact Actual=ZContact", contact.getImapUid(), zcontact.getImapUid());
    assertEquals("After Modify:ModifiedSequence Expected=Contact Actual=ZContact", contact.getModifiedSequence(), zcontact.getModifiedSequence());
    assertTrue(String.format("After Contact modify, new modSeq=%s should be greater than old modSeq=%s", zcontact.getModifiedSequence(), origModSeq), zcontact.getModifiedSequence() > origModSeq);
    List<ZContact> allZContacts = zmbox.getAllContacts(ZFolder.ID_CONTACTS, (ContactSortBy) null, true, /* sync */
    null);
    assertNotNull("zmbox.getAllContacts result should not be null", allZContacts);
    assertEquals("zmbox.getAllContacts result should have 1 entry", 1, allZContacts.size());
    zcontact = allZContacts.get(0);
    assertEquals("GetAllContacts: IMAP UID Expected=Contact Actual=ZContact", contact.getImapUid(), zcontact.getImapUid());
    assertEquals("GetAllContacts ModifiedSequence:Expected=Contact Actual=ZContact", contact.getModifiedSequence(), zcontact.getModifiedSequence());
}
Also used : ZMailbox(com.zimbra.client.ZMailbox) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMailbox(com.zimbra.client.ZMailbox) ZMountpoint(com.zimbra.client.ZMountpoint) ZContact(com.zimbra.client.ZContact) ZContact(com.zimbra.client.ZContact) Contact(com.zimbra.cs.mailbox.Contact) Test(org.junit.Test)

Example 52 with Contact

use of com.zimbra.cs.mailbox.Contact in project zm-mailbox by Zimbra.

the class TestContacts method testMoveContact.

@Test
public void testMoveContact() throws Exception {
    ZMailbox zmbx = TestUtil.getZMailbox(USER_NAME);
    Account acct = Provisioning.getInstance().get(Key.AccountBy.name, TestUtil.getAddress(USER_NAME));
    // Create a contact with an attachment.
    Map<String, String> attrs = new HashMap<String, String>();
    attrs.put("fullName", NAME_PREFIX + " testMoveContact");
    String attachment1Text = "attachment 1";
    int timeout = (int) Constants.MILLIS_PER_MINUTE;
    String folderId = Integer.toString(Mailbox.ID_FOLDER_CONTACTS);
    String attachment1Id = zmbx.uploadAttachment("file1.txt", attachment1Text.getBytes(), "text/plain", timeout);
    Map<String, ZAttachmentInfo> attachments = new HashMap<String, ZAttachmentInfo>();
    ZAttachmentInfo info = new ZAttachmentInfo().setAttachmentId(attachment1Id);
    attachments.put("file1", info);
    ZContact contact = zmbx.createContact(folderId, null, attrs, attachments);
    // bug 80659 add an attribute after initial save so rev changes
    attrs = new HashMap<String, String>();
    attrs.put("phone", NAME_PREFIX + " testMoveContact");
    zmbx.modifyContact(contact.getId(), false, attrs);
    Account acct2 = Provisioning.getInstance().get(Key.AccountBy.name, TestUtil.getAddress(USER2_NAME));
    Mailbox remoteMbox = MailboxManager.getInstance().getMailboxByAccount(acct2);
    Mailbox mbox1 = MailboxManager.getInstance().getMailboxByAccount(acct);
    remoteMbox.grantAccess(null, Mailbox.ID_FOLDER_CONTACTS, acct.getId(), ACL.GRANTEE_USER, (short) (ACL.RIGHT_READ | ACL.RIGHT_WRITE | ACL.RIGHT_INSERT), null);
    mbox1.grantAccess(null, Mailbox.ID_FOLDER_CONTACTS, acct2.getId(), ACL.GRANTEE_USER, (short) (ACL.RIGHT_READ | ACL.RIGHT_WRITE | ACL.RIGHT_INSERT), null);
    // move the contact to user2
    zmbx.moveContact(contact.getId(), acct2.getId() + ":" + Mailbox.ID_FOLDER_CONTACTS);
    ZMailbox remoteZmbx = TestUtil.getZMailbox(USER2_NAME);
    String idStr = TestUtil.search(remoteZmbx, "in:Contacts testMoveContact", ZSearchParams.TYPE_CONTACT).get(0);
    Contact ct = remoteMbox.getContactById(null, Integer.parseInt(idStr));
    // make sure contact has attachment
    List<Attachment> list = ct.getAttachments();
    Assert.assertFalse(list.isEmpty());
    Attachment att = list.get(0);
    Assert.assertEquals("file1.txt", att.getFilename());
    Assert.assertEquals("text/plain", att.getContentType());
    Assert.assertEquals("attachment 1", new String(att.getContent()));
    // move the contact back to user1
    remoteZmbx.moveContact(String.valueOf(ct.getId()), acct.getId() + ":" + Mailbox.ID_FOLDER_CONTACTS);
    // reset the access
    remoteMbox.revokeAccess(null, Mailbox.ID_FOLDER_CONTACTS, acct.getId());
    mbox1.revokeAccess(null, Mailbox.ID_FOLDER_CONTACTS, acct2.getId());
    idStr = TestUtil.search(zmbx, "in:Contacts testMoveContact", ZSearchParams.TYPE_CONTACT).get(0);
    ct = mbox1.getContactById(null, Integer.parseInt(idStr));
    // make sure contact has attachment
    list = ct.getAttachments();
    Assert.assertFalse(list.isEmpty());
    att = list.get(0);
    Assert.assertEquals("file1.txt", att.getFilename());
    Assert.assertEquals("text/plain", att.getContentType());
    Assert.assertEquals("attachment 1", new String(att.getContent()));
}
Also used : Account(com.zimbra.cs.account.Account) HashMap(java.util.HashMap) Attachment(com.zimbra.cs.mailbox.Contact.Attachment) ZContact(com.zimbra.client.ZContact) Contact(com.zimbra.cs.mailbox.Contact) ZMailbox(com.zimbra.client.ZMailbox) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMailbox(com.zimbra.client.ZMailbox) ZAttachmentInfo(com.zimbra.client.ZMailbox.ZAttachmentInfo) ZContact(com.zimbra.client.ZContact) Test(org.junit.Test)

Aggregations

Contact (com.zimbra.cs.mailbox.Contact)52 Mailbox (com.zimbra.cs.mailbox.Mailbox)32 ParsedContact (com.zimbra.cs.mime.ParsedContact)26 Test (org.junit.Test)23 OperationContext (com.zimbra.cs.mailbox.OperationContext)13 HashMap (java.util.HashMap)13 ZMailbox (com.zimbra.client.ZMailbox)10 ContactGroup (com.zimbra.cs.mailbox.ContactGroup)10 MailItem (com.zimbra.cs.mailbox.MailItem)10 ItemId (com.zimbra.cs.service.util.ItemId)10 ServiceException (com.zimbra.common.service.ServiceException)9 ArrayList (java.util.ArrayList)9 ZContact (com.zimbra.client.ZContact)8 Folder (com.zimbra.cs.mailbox.Folder)7 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)7 Term (org.apache.lucene.index.Term)7 Element (com.zimbra.common.soap.Element)6 Account (com.zimbra.cs.account.Account)6 Message (com.zimbra.cs.mailbox.Message)6 List (java.util.List)6