Search in sources :

Example 11 with ZContact

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

the class TestContacts method testContactAttachments.

@Test
public void testContactAttachments() throws Exception {
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    // Create a contact with an attachment.
    Map<String, String> attrs = new HashMap<String, String>();
    attrs.put("fullName", NAME_PREFIX + " testAttachments");
    String attachment1Text = "attachment 1";
    int timeout = (int) Constants.MILLIS_PER_MINUTE;
    String folderId = Integer.toString(Mailbox.ID_FOLDER_CONTACTS);
    String attachment1Id = mbox.uploadAttachment("attachment.txt", attachment1Text.getBytes(), "text/plain", timeout);
    Map<String, ZAttachmentInfo> attachments = new HashMap<String, ZAttachmentInfo>();
    ZAttachmentInfo info = new ZAttachmentInfo().setAttachmentId(attachment1Id);
    attachments.put("attachment1", info);
    ZContact contact = mbox.createContact(folderId, null, attrs, attachments);
    // Validate the attachment data.
    assertTrue(contact.getAttachmentNames().contains("attachment1"));
    byte[] data = getAttachmentData(contact, "attachment1");
    assertEquals(attachment1Text, new String(data));
    // Add a second attachment.
    String attachment2Text = "attachment 2";
    String attachment2Id = mbox.uploadAttachment("attachment.txt", attachment2Text.getBytes(), "text/plain", timeout);
    attachments.clear();
    info.setAttachmentId(attachment2Id);
    attachments.put("attachment2", info);
    contact = mbox.modifyContact(contact.getId(), false, null, attachments);
    // Validate second attachment data.
    data = getAttachmentData(contact, "attachment2");
    assertEquals(attachment2Text, new String(data));
    // Replace second attachment.
    String newAttachment2Text = "new attachment 2";
    String newAttachment2Id = mbox.uploadAttachment("attachment.txt", newAttachment2Text.getBytes(), "text/plain", timeout);
    info.setAttachmentId(newAttachment2Id);
    contact = mbox.modifyContact(contact.getId(), false, null, attachments);
    // Confirm that the attachment data was updated.
    data = getAttachmentData(contact, "attachment2");
    assertEquals(newAttachment2Text, new String(data));
    // Create third attachment with data from the second attachment.
    info.setAttachmentId(null);
    info.setPartName(contact.getAttachmentPartName("attachment2"));
    attachments.clear();
    attachments.put("attachment3", info);
    contact = mbox.modifyContact(contact.getId(), false, null, attachments);
    // Verify the attachment data.
    data = getAttachmentData(contact, "attachment2");
    assertEquals(newAttachment2Text, new String(data));
    data = getAttachmentData(contact, "attachment3");
    assertEquals(newAttachment2Text, new String(data));
    // Replace attachments.
    String attachment4Text = "attachment 4";
    String attachment4Id = mbox.uploadAttachment("attachment.txt", attachment4Text.getBytes(), "text/plain", timeout);
    info.setAttachmentId(attachment4Id);
    info.setPartName(null);
    attachments.clear();
    attachments.put("attachment4", info);
    contact = mbox.modifyContact(contact.getId(), true, attrs, attachments);
    // Verify the attachment data.
    Set<String> names = contact.getAttachmentNames();
    assertEquals(1, names.size());
    assertTrue(names.contains("attachment4"));
    data = getAttachmentData(contact, "attachment4");
    assertEquals(attachment4Text, new String(data));
    // Remove all attachments.
    info.setAttachmentId(null);
    contact = mbox.modifyContact(contact.getId(), false, attrs, attachments);
    assertEquals(0, contact.getAttachmentNames().size());
    // Add an attachment to a contact that didn't previously have one.
    attachment4Id = mbox.uploadAttachment("attachment.txt", attachment4Text.getBytes(), "text/plain", timeout);
    info.setAttachmentId(attachment4Id);
    info.setPartName(null);
    attachments.clear();
    attachments.put("attachment4", info);
    contact = mbox.modifyContact(contact.getId(), false, attrs, attachments);
    // Verify the attachment data.
    names = contact.getAttachmentNames();
    assertEquals(1, names.size());
    assertTrue(names.contains("attachment4"));
    data = getAttachmentData(contact, "attachment4");
    assertEquals(attachment4Text, new String(data));
}
Also used : ZMailbox(com.zimbra.client.ZMailbox) HashMap(java.util.HashMap) ZAttachmentInfo(com.zimbra.client.ZMailbox.ZAttachmentInfo) ZContact(com.zimbra.client.ZContact) Test(org.junit.Test)

Example 12 with ZContact

use of com.zimbra.client.ZContact 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

ZContact (com.zimbra.client.ZContact)12 ZMailbox (com.zimbra.client.ZMailbox)11 Test (org.junit.Test)9 Contact (com.zimbra.cs.mailbox.Contact)7 Mailbox (com.zimbra.cs.mailbox.Mailbox)6 HashMap (java.util.HashMap)4 ItemIdentifier (com.zimbra.common.mailbox.ItemIdentifier)3 ZFolder (com.zimbra.client.ZFolder)2 ZAttachmentInfo (com.zimbra.client.ZMailbox.ZAttachmentInfo)2 ZMountpoint (com.zimbra.client.ZMountpoint)2 Element (com.zimbra.common.soap.Element)2 Account (com.zimbra.cs.account.Account)2 ZDataSource (com.zimbra.client.ZDataSource)1 ZSearchGalResult (com.zimbra.client.ZMailbox.ZSearchGalResult)1 ZMessage (com.zimbra.client.ZMessage)1 ZTag (com.zimbra.client.ZTag)1 ZAuthToken (com.zimbra.common.auth.ZAuthToken)1 OpContext (com.zimbra.common.mailbox.OpContext)1 ZimbraMailItem (com.zimbra.common.mailbox.ZimbraMailItem)1 ServiceException (com.zimbra.common.service.ServiceException)1