Search in sources :

Example 1 with ZTag

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

the class TestImap method testStoreTagsDirty.

@Test
public void testStoreTagsDirty() throws Exception {
    ZMailbox mbox = TestUtil.getZMailbox(USER);
    List<ZTag> tags = mbox.getAllTags();
    assertTrue(tags == null || tags.size() == 0);
    String tagName = "T1";
    final String tagName2 = "T2";
    ZTag tag = mbox.getTag(tagName);
    if (tag == null) {
        tag = mbox.createTag(tagName, Color.blue);
    }
    tags = mbox.getAllTags();
    assertTrue(tags != null && tags.size() == 1);
    assertEquals("T1", tags.get(0).getName());
    String folderName = "newfolder1";
    ZFolder folder = mbox.createFolder(Mailbox.ID_FOLDER_USER_ROOT + "", folderName, ZFolder.View.message, ZFolder.Color.DEFAULTCOLOR, null, null);
    mbox.addMessage(Mailbox.ID_FOLDER_INBOX + "", "u", tag.getId(), System.currentTimeMillis(), simpleMessage("foo1"), true);
    MailboxInfo info = connection.select("INBOX");
    assertTrue("INBOX does not contain expected flag " + tagName, info.getFlags().isSet(tagName));
    assertFalse("INBOX contain unexpected flag " + tagName2, info.getFlags().isSet(tagName2));
    Map<Long, MessageData> data = connection.fetch("1:*", "FLAGS");
    assertEquals(1, data.size());
    Iterator<Long> it = data.keySet().iterator();
    Long seq = it.next();
    assertTrue("flag not set on first message", data.get(seq).getFlags().isSet(tagName));
    ImapRequest req = connection.newRequest("STORE", seq + "", "+FLAGS", tagName2);
    req.setResponseHandler(new ResponseHandler() {

        @Override
        public void handleResponse(ImapResponse res) throws Exception {
            if (res.isUntagged() && res.getCCode() == CAtom.FLAGS) {
                Flags flags = (Flags) res.getData();
                assertTrue(flags.isSet(tagName2));
            }
        }
    });
    req.sendCheckStatus();
}
Also used : ResponseHandler(com.zimbra.cs.mailclient.imap.ResponseHandler) MessageData(com.zimbra.cs.mailclient.imap.MessageData) ImapRequest(com.zimbra.cs.mailclient.imap.ImapRequest) ZTag(com.zimbra.client.ZTag) ImapResponse(com.zimbra.cs.mailclient.imap.ImapResponse) Flags(com.zimbra.cs.mailclient.imap.Flags) ServiceException(com.zimbra.common.service.ServiceException) CommandFailedException(com.zimbra.cs.mailclient.CommandFailedException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ZMailbox(com.zimbra.client.ZMailbox) ZFolder(com.zimbra.client.ZFolder) MailboxInfo(com.zimbra.cs.mailclient.imap.MailboxInfo) Test(org.junit.Test)

Example 2 with ZTag

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

the class TestPurge method testTagRetentionPolicy.

@Test
public void testTagRetentionPolicy() throws Exception {
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    ZTag tag = mbox.createTag(NAME_PREFIX + "-testTagRetentionPolicy", null);
    // Set user keep policy for folder.
    TagActionSelector action = new TagActionSelector(tag.getId(), "retentionpolicy");
    RetentionPolicy rp = new RetentionPolicy(Arrays.asList(Policy.newUserPolicy("30d")), null);
    action.setRetentionPolicy(rp);
    TagActionRequest req = new TagActionRequest(action);
    TagActionResponse res = mbox.invokeJaxb(req);
    Assert.assertEquals("retentionpolicy", res.getAction().getOperation());
    Assert.assertEquals(tag.getId(), res.getAction().getSuccesses());
    // Make sure that the retention policy is now set.
    tag = mbox.getTagById(tag.getId());
    rp = tag.getRetentionPolicy();
    Assert.assertEquals(1, rp.getKeepPolicy().size());
    Assert.assertEquals(0, rp.getPurgePolicy().size());
    Policy p = rp.getKeepPolicy().get(0);
    Assert.assertEquals(Policy.Type.USER, p.getType());
    Assert.assertEquals("30d", p.getLifetime());
    // Turn off keep policy and set purge policy.
    action = new TagActionSelector(tag.getId(), "retentionpolicy");
    rp = new RetentionPolicy(null, Arrays.asList(Policy.newUserPolicy("45d")));
    action.setRetentionPolicy(rp);
    req = new TagActionRequest(action);
    res = mbox.invokeJaxb(req);
    Assert.assertEquals("retentionpolicy", res.getAction().getOperation());
    Assert.assertEquals(tag.getId(), res.getAction().getSuccesses());
    // Make sure that the retention policy is now set.
    tag = mbox.getTagById(tag.getId());
    rp = tag.getRetentionPolicy();
    Assert.assertEquals(0, rp.getKeepPolicy().size());
    Assert.assertEquals(1, rp.getPurgePolicy().size());
    p = rp.getPurgePolicy().get(0);
    Assert.assertEquals(Policy.Type.USER, p.getType());
    Assert.assertEquals("45d", p.getLifetime());
    // Start a new session and make sure that the retention policy is still returned.
    mbox = TestUtil.getZMailbox(USER_NAME);
    tag = mbox.getTagById(tag.getId());
    Assert.assertTrue(tag.getRetentionPolicy().isSet());
}
Also used : Policy(com.zimbra.soap.mail.type.Policy) RetentionPolicy(com.zimbra.soap.mail.type.RetentionPolicy) ZMailbox(com.zimbra.client.ZMailbox) TagActionResponse(com.zimbra.soap.mail.message.TagActionResponse) TagActionRequest(com.zimbra.soap.mail.message.TagActionRequest) ZTag(com.zimbra.client.ZTag) RetentionPolicy(com.zimbra.soap.mail.type.RetentionPolicy) TagActionSelector(com.zimbra.soap.mail.type.TagActionSelector) Test(org.junit.Test)

Example 3 with ZTag

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

the class TestImap method testAppendTags.

@Test
public void testAppendTags() throws Exception {
    Flags flags = Flags.fromSpec("afs");
    //new tag; does not exist in mbox
    String tag1 = "APPENDTAG1";
    flags.set(tag1);
    Date date = new Date(System.currentTimeMillis());
    Literal msg = message(10);
    try {
        AppendResult res = connection.append("INBOX", flags, date, msg);
        MessageData data = connection.uidFetch(res.getUid(), "FLAGS");
        assertTrue(data.getFlags().isSet(tag1));
    } finally {
        msg.dispose();
    }
    //should not have created a visible tag
    ZMailbox mbox = TestUtil.getZMailbox(USER);
    List<ZTag> tags = mbox.getAllTags();
    assertTrue("APPEND created new visible tag", tags == null || tags.size() == 0);
    //now create a visible tag, add it to a message in inbox then try append to message in different folder
    String tag2 = "APPENDTAG2";
    ZTag tag = mbox.getTag(tag2);
    if (tag == null) {
        tag = mbox.createTag(tag2, Color.blue);
    }
    tags = mbox.getAllTags();
    assertTrue(tags != null && tags.size() == 1);
    assertEquals(tag2, tags.get(0).getName());
    mbox.addMessage(Mailbox.ID_FOLDER_INBOX + "", "u", tag.getId(), System.currentTimeMillis(), simpleMessage("foo1"), true);
    MailboxInfo info = connection.select("INBOX");
    assertTrue("INBOX does not contain expected flag " + tag2, info.getFlags().isSet(tag2));
    String folderName = "newfolder1";
    ZFolder folder = mbox.createFolder(Mailbox.ID_FOLDER_USER_ROOT + "", folderName, ZFolder.View.message, ZFolder.Color.DEFAULTCOLOR, null, null);
    info = connection.select(folderName);
    assertFalse("new tag unexpectedly set in new folder", info.getFlags().isSet(tag2));
    msg = message(10);
    flags = Flags.fromSpec("afs");
    flags.set(tag2);
    try {
        AppendResult res = connection.append(folderName, flags, date, msg);
        MessageData data = connection.uidFetch(res.getUid(), "FLAGS");
        assertTrue(data.getFlags().isSet(tag2));
    } finally {
        msg.dispose();
    }
    info = connection.select(folderName);
    assertTrue("new tag not set in new folder", info.getFlags().isSet(tag2));
}
Also used : ZMailbox(com.zimbra.client.ZMailbox) MessageData(com.zimbra.cs.mailclient.imap.MessageData) Literal(com.zimbra.cs.mailclient.imap.Literal) AppendResult(com.zimbra.cs.mailclient.imap.AppendResult) ZTag(com.zimbra.client.ZTag) MailboxInfo(com.zimbra.cs.mailclient.imap.MailboxInfo) ZFolder(com.zimbra.client.ZFolder) Flags(com.zimbra.cs.mailclient.imap.Flags) Date(java.sql.Date) Test(org.junit.Test)

Example 4 with ZTag

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

the class TestFilterExisting method testTag.

@Test
public void testTag() throws Exception {
    // Add message
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    ZTag tag = mbox.createTag(TAG_NAME, null);
    String subject = NAME_PREFIX + " test tag";
    String id = TestUtil.addMessage(mbox, subject);
    ZMessage msg = mbox.getMessageById(id);
    // Run flag rule and make sure the message is not tagged.
    Set<String> affectedIds = runRules(new String[] { FLAG_RULE_NAME }, id, null);
    Assert.assertEquals(0, affectedIds.size());
    Assert.assertFalse(msg.hasTags());
    // Run tag rule and make sure the message is tagged.
    affectedIds = runRules(new String[] { TAG_RULE_NAME }, id, null);
    Assert.assertEquals(1, affectedIds.size());
    Assert.assertTrue(affectedIds.contains(id));
    mbox.noOp();
    msg = mbox.getMessageById(id);
    Assert.assertEquals(tag.getId(), msg.getTagIds());
}
Also used : ZMessage(com.zimbra.client.ZMessage) ZMailbox(com.zimbra.client.ZMailbox) ZTag(com.zimbra.client.ZTag) Test(org.junit.Test)

Example 5 with ZTag

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

the class TestFilter method testAttachment.

@Test
public void testAttachment() throws Exception {
    List<ZFilterCondition> conditions = new ArrayList<ZFilterCondition>();
    List<ZFilterAction> actions = new ArrayList<ZFilterAction>();
    List<ZFilterRule> rules = new ArrayList<ZFilterRule>();
    // If attachment exists, set tag1.  If attachment doesn't exist, set tag2.
    ZTag tag1 = mMbox.createTag(NAME_PREFIX + " testAttachment1", null);
    ZTag tag2 = mMbox.createTag(NAME_PREFIX + " testAttachment2", null);
    conditions.add(new ZAttachmentExistsCondition(true));
    actions.add(new ZTagAction(tag1.getName()));
    rules.add(new ZFilterRule("testAttachment1", true, false, conditions, actions));
    conditions = new ArrayList<ZFilterCondition>();
    actions = new ArrayList<ZFilterAction>();
    conditions.add(new ZAttachmentExistsCondition(false));
    actions.add(new ZTagAction(tag2.getName()));
    rules.add(new ZFilterRule("testAttachment2", true, false, conditions, actions));
    ZFilterRules zRules = new ZFilterRules(rules);
    saveIncomingRules(mMbox, zRules);
    // Add a message with an attachment.
    String address = TestUtil.getAddress(USER_NAME);
    String subject = NAME_PREFIX + " testAttachment1";
    String msgContent = new MessageBuilder().withSubject(subject).withAttachment("attach this", "attach.txt", "text/plain").create();
    TestUtil.addMessageLmtp(new String[] { address }, address, msgContent);
    ZMessage msg = TestUtil.getMessage(mMbox, "in:inbox subject:\"" + subject + "\"");
    // Check the tag states.
    Set<String> tagIds = getTagIds(msg);
    assertEquals(1, tagIds.size());
    assertTrue(tagIds.contains(tag1.getId()));
    // Add a message with no attachment.
    subject = NAME_PREFIX + " testAttachment2";
    msgContent = new MessageBuilder().withSubject(subject).create();
    TestUtil.addMessageLmtp(new String[] { address }, address, msgContent);
    msg = TestUtil.getMessage(mMbox, "in:inbox subject:\"" + subject + "\"");
    // Check the tag states.
    tagIds = getTagIds(msg);
    assertEquals(1, tagIds.size());
    assertTrue(tagIds.contains(tag2.getId()));
}
Also used : ZMessage(com.zimbra.client.ZMessage) ZFilterCondition(com.zimbra.client.ZFilterCondition) ArrayList(java.util.ArrayList) ZFilterAction(com.zimbra.client.ZFilterAction) ZFilterRule(com.zimbra.client.ZFilterRule) ZTag(com.zimbra.client.ZTag) ZTagAction(com.zimbra.client.ZFilterAction.ZTagAction) ZAttachmentExistsCondition(com.zimbra.client.ZFilterCondition.ZAttachmentExistsCondition) ZFilterRules(com.zimbra.client.ZFilterRules) Test(org.junit.Test)

Aggregations

ZTag (com.zimbra.client.ZTag)15 ZMailbox (com.zimbra.client.ZMailbox)8 Test (org.junit.Test)8 ZMessage (com.zimbra.client.ZMessage)5 ZFilterAction (com.zimbra.client.ZFilterAction)4 ZTagAction (com.zimbra.client.ZFilterAction.ZTagAction)4 ZFilterCondition (com.zimbra.client.ZFilterCondition)4 ZFilterRule (com.zimbra.client.ZFilterRule)4 ZFilterRules (com.zimbra.client.ZFilterRules)4 ZFolder (com.zimbra.client.ZFolder)4 ArrayList (java.util.ArrayList)4 MailboxInfo (com.zimbra.cs.mailclient.imap.MailboxInfo)3 MessageData (com.zimbra.cs.mailclient.imap.MessageData)3 Flags (com.zimbra.cs.mailclient.imap.Flags)2 Date (java.util.Date)2 ZContact (com.zimbra.client.ZContact)1 ZDataSource (com.zimbra.client.ZDataSource)1 ZAttachmentExistsCondition (com.zimbra.client.ZFilterCondition.ZAttachmentExistsCondition)1 ZDateCondition (com.zimbra.client.ZFilterCondition.ZDateCondition)1 ZHeaderCondition (com.zimbra.client.ZFilterCondition.ZHeaderCondition)1