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());
}
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()));
}
Aggregations