use of com.zimbra.client.ZFilterRule in project zm-mailbox by Zimbra.
the class TestFolderFilterRules method testDeleteFolder.
/**
* Confirms that when a folder is deleted, any rules that filed into that
* folder or its subfolders are disabled (bug 17797).
*/
@Test
public void testDeleteFolder() throws Exception {
ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
mbox.deleteFolder(mFolder2.getId());
// Deliver messages that used to match rules 2 and 3, and make sure
// that they get delivered to inbox.
TestUtil.addMessageLmtp(SUBJECT2, USER_NAME, USER_NAME);
TestUtil.getMessage(mbox, "in:inbox subject:\"" + SUBJECT2 + "\"");
TestUtil.addMessageLmtp(SUBJECT3, USER_NAME, USER_NAME);
TestUtil.getMessage(mbox, "in:inbox subject:\"" + SUBJECT3 + "\"");
// Confirm that rules for folders 2 and 3 are disabled.
List<ZFilterRule> rules = mbox.getIncomingFilterRules(true).getRules();
assertEquals(4, rules.size());
for (ZFilterRule rule : rules) {
if (rule.getName().equals("Folder 1")) {
assertTrue(rule.isActive());
} else if (rule.getName().equals("Folder 2")) {
assertFalse(rule.isActive());
} else if (rule.getName().equals("Folder 3")) {
assertFalse(rule.isActive());
} else if (rule.getName().equals("Folder 4")) {
assertTrue(rule.isActive());
} else {
fail("Unexpected rule name: " + rule.getName());
}
}
}
use of com.zimbra.client.ZFilterRule in project zm-mailbox by Zimbra.
the class TestFolderFilterRules method testMoveFolderToTrash.
/**
* Confirms that when a folder moved to the trash, any rules that filed into that
* folder or its subfolders are disabled (bug 17797).
*/
@Test
public void testMoveFolderToTrash() throws Exception {
ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
mbox.trashFolder(mFolder2.getId());
// Deliver messages that used to match rules 2 and 3, and make sure
// that they get delivered to inbox.
TestUtil.addMessageLmtp(SUBJECT2, USER_NAME, USER_NAME);
TestUtil.getMessage(mbox, "in:inbox subject:\"" + SUBJECT2 + "\"");
TestUtil.addMessageLmtp(SUBJECT3, USER_NAME, USER_NAME);
TestUtil.getMessage(mbox, "in:inbox subject:\"" + SUBJECT3 + "\"");
// Confirm that rules for folders 2 and 3 are disabled.
List<ZFilterRule> rules = mbox.getIncomingFilterRules(true).getRules();
assertEquals(4, rules.size());
for (ZFilterRule rule : rules) {
if (rule.getName().equals("Folder 1")) {
assertTrue(rule.isActive());
} else if (rule.getName().equals("Folder 2")) {
assertFalse(rule.isActive());
} else if (rule.getName().equals("Folder 3")) {
assertFalse(rule.isActive());
} else if (rule.getName().equals("Folder 4")) {
assertTrue(rule.isActive());
} else {
fail("Unexpected rule name: " + rule.getName());
}
}
}
use of com.zimbra.client.ZFilterRule in project zm-mailbox by Zimbra.
the class TestFilter method testAddressTestPart.
@Test
public void testAddressTestPart() throws Exception {
List<ZFilterRule> rules = new ArrayList<ZFilterRule>();
List<ZFilterCondition> conditions = new ArrayList<ZFilterCondition>();
List<ZFilterAction> actions = new ArrayList<ZFilterAction>();
// use address test for address domain match
conditions.add(new ZFilterCondition.ZAddressCondition("From", Sieve.AddressPart.domain, HeaderOp.IS, false, "example.com"));
actions.add(new ZMarkAction(MarkOp.FLAGGED));
rules.add(new ZFilterRule("testAddressTestPart1", true, false, conditions, actions));
saveIncomingRules(mMbox, new ZFilterRules(rules));
String subject = NAME_PREFIX + " testAddressTestPart1";
String mime = new MessageBuilder().withFrom("John Doe <JOHN.DOE@EXAMPLE.COM>").withSubject(subject).create();
TestUtil.addMessageLmtp(new String[] { USER_NAME }, USER_NAME, mime);
ZMessage msg = TestUtil.getMessage(mMbox, "in:inbox subject:\"" + subject + "\"");
assertTrue("Unexpected message flag state", msg.isFlagged());
// use address test for address local-part match
conditions.add(new ZFilterCondition.ZAddressCondition("From", Sieve.AddressPart.localpart, HeaderOp.MATCHES, true, "j*doe"));
actions.add(new ZMarkAction(MarkOp.FLAGGED));
rules.add(new ZFilterRule("testAddressTestPart2", true, false, conditions, actions));
saveIncomingRules(mMbox, new ZFilterRules(rules));
subject = NAME_PREFIX + " testAddressTestPart2";
mime = new MessageBuilder().withFrom("John Doe <john.doe@example.com>").withSubject(subject).create();
TestUtil.addMessageLmtp(new String[] { USER_NAME }, USER_NAME, mime);
msg = TestUtil.getMessage(mMbox, "in:inbox subject:\"" + subject + "\"");
assertTrue("Unexpected message flag state", msg.isFlagged());
}
use of com.zimbra.client.ZFilterRule in project zm-mailbox by Zimbra.
the class TestPop3Import method testFiltering.
/**
* Confirms that messages pulled from a POP3 account are affected by
* mail filtering (bug 13821).
*/
@Test
public void testFiltering() throws Exception {
String folderPath = "/" + NAME_PREFIX + "-testFiltering";
String filteredPath = "/" + NAME_PREFIX + "-testFiltering-filtered";
// Create remote account
prov.createAccount(TestUtil.getAddress(TEMP_USER_NAME), "test123", null);
// Add message to remote mailbox
ZMailbox remoteMbox = TestUtil.getZMailbox(TEMP_USER_NAME);
TestUtil.addMessage(remoteMbox, NAME_PREFIX + " testFiltering");
// Create local folders
ZMailbox localMbox = TestUtil.getZMailbox(USER_NAME);
localMbox.getFolderByPath("/Inbox");
ZFolder dsFolder = TestUtil.createFolder(localMbox, folderPath);
TestUtil.createFolder(localMbox, filteredPath);
// Create filter rule that files to the local folder
List<ZFilterRule> rules = new ArrayList<ZFilterRule>();
List<ZFilterCondition> conditions = new ArrayList<ZFilterCondition>();
List<ZFilterAction> actions = new ArrayList<ZFilterAction>();
conditions.add(new ZHeaderCondition("subject", HeaderOp.CONTAINS, "testFiltering"));
actions.add(new ZFileIntoAction(filteredPath));
rules.add(new ZFilterRule("testFiltering", true, false, conditions, actions));
localMbox.saveIncomingFilterRules(new ZFilterRules(rules));
// Set up data source and run import
ZPop3DataSource ds = getZDataSource();
ds.setUsername(TEMP_USER_NAME);
ds.setFolderId(dsFolder.getId());
ds.setEnabled(true);
localMbox.modifyDataSource(ds);
// Import data and make sure the message was filed to the folder
TestUtil.importDataSource(ds, localMbox, remoteMbox);
List<ZMessage> messages = TestUtil.search(localMbox, "in:" + folderPath);
Assert.assertEquals("Found unexpected messages in " + folderPath, 0, messages.size());
messages = TestUtil.search(localMbox, "in:" + filteredPath);
Assert.assertEquals("Message not found in " + filteredPath, 1, messages.size());
}
use of com.zimbra.client.ZFilterRule in project zm-mailbox by Zimbra.
the class ZMailboxUtil method doAddFilterRule.
private void doAddFilterRule(String[] args, ZFilterRules rules) throws ServiceException {
ZFilterRule newRule = ZFilterRule.parseFilterRule(args);
List<ZFilterRule> list = rules.getRules();
if (firstOpt()) {
list.add(0, newRule);
} else if (afterOpt() != null) {
boolean found = false;
String name = afterOpt();
for (int i = 0; i < list.size(); i++) {
found = list.get(i).getName().equalsIgnoreCase(name);
if (found) {
if (i + 1 >= list.size())
list.add(newRule);
else
list.add(i + 1, newRule);
break;
}
}
if (!found) {
throw ZClientException.CLIENT_ERROR("can't find rule: " + name, null);
}
} else if (beforeOpt() != null) {
String name = beforeOpt();
boolean found = false;
for (int i = 0; i < list.size(); i++) {
found = list.get(i).getName().equalsIgnoreCase(name);
if (found) {
list.add(i, newRule);
break;
}
}
if (!found) {
throw ZClientException.CLIENT_ERROR("can't find rule: " + name, null);
}
} else {
// add to end
list.add(newRule);
}
}
Aggregations