Search in sources :

Example 1 with FilterAction

use of com.zimbra.soap.mail.type.FilterAction in project zm-mailbox by Zimbra.

the class ModifyFilterRulesTest method SetIncomingXMLRulesForEnvelope.

@Test
public void SetIncomingXMLRulesForEnvelope() {
    try {
        Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
        RuleManager.clearCachedRules(account);
        // Construct a filter rule with 'address' test whose value is empty (null)
        FilterRule rule = new FilterRule("testSetIncomingXMLRulesForEnvelope", true);
        FilterTest.EnvelopeTest test = new FilterTest.EnvelopeTest();
        test.setHeader("to");
        test.setStringComparison("is");
        test.setPart("all");
        test.setValue("u1@zimbra.com");
        test.setIndex(0);
        FilterTests tests = new FilterTests("anyof");
        tests.addTest(test);
        FilterAction action = new FilterAction.KeepAction();
        action.setIndex(0);
        rule.setFilterTests(tests);
        rule.addFilterAction(action);
        List<FilterRule> filterRuleList = new ArrayList<FilterRule>();
        filterRuleList.add(rule);
        // When the ModifyFilterRulesRequest is submitted from the Web client,
        // eventually this RuleManager.setIncomingXMLRules is called to convert
        // the request in JSON to the SIEVE rule text.
        RuleManager.setIncomingXMLRules(account, filterRuleList);
        // Verify that the saved zimbraMailSieveScript
        String sieve = account.getMailSieveScript();
        int result = sieve.indexOf("envelope :all :is :comparator \"i;ascii-casemap\" [\"to\"] \"u1@zimbra.com\"");
        Assert.assertNotSame(-1, result);
    } catch (Exception e) {
        fail("No exception should be thrown" + e);
    }
}
Also used : Account(com.zimbra.cs.account.Account) ArrayList(java.util.ArrayList) FilterRule(com.zimbra.soap.mail.type.FilterRule) ServiceException(com.zimbra.common.service.ServiceException) FilterTest(com.zimbra.soap.mail.type.FilterTest) FilterTests(com.zimbra.soap.mail.type.FilterTests) FilterAction(com.zimbra.soap.mail.type.FilterAction) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Example 2 with FilterAction

use of com.zimbra.soap.mail.type.FilterAction in project zm-mailbox by Zimbra.

the class GetFilterRulesAdminTest method testSieveToSoapDeleteheaderActionBasicWithValue.

@Test
public void testSieveToSoapDeleteheaderActionBasicWithValue() throws ServiceException {
    RuleManager.clearCachedRules(account);
    String script = "require [\"editheader\"];\n" + "# rule1\n" + // matchType and comparator should be added by default
    "deleteheader \"X-My-Header\" \"Test Value\";";
    account.setAdminSieveScriptBefore(script);
    List<FilterRule> filterRules = RuleManager.getAdminRulesAsXML(account, FilterType.INCOMING, AdminFilterType.BEFORE);
    Assert.assertEquals(filterRules.size(), 1);
    FilterRule filterRule = filterRules.get(0);
    Assert.assertEquals("rule1", filterRule.getName());
    Assert.assertTrue(filterRule.isActive());
    Assert.assertEquals(1, filterRule.getFilterActions().size());
    FilterAction filterAction = filterRule.getFilterActions().get(0);
    Assert.assertTrue(filterAction instanceof FilterAction.DeleteheaderAction);
    FilterAction.DeleteheaderAction action = (FilterAction.DeleteheaderAction) filterAction;
    Assert.assertNull(action.getLast());
    Assert.assertNull(action.getOffset());
    EditheaderTest test = action.getTest();
    Assert.assertEquals("i;ascii-casemap", test.getComparator());
    Assert.assertEquals("is", test.getMatchType());
    Assert.assertNull(test.getRelationalComparator());
    Assert.assertNull(test.getCount());
    Assert.assertNull(test.getValue());
    Assert.assertEquals("X-My-Header", test.getHeaderName());
    List<String> values = test.getHeaderValue();
    Assert.assertEquals(1, values.size());
    Assert.assertEquals("Test Value", values.get(0));
}
Also used : FilterRule(com.zimbra.soap.mail.type.FilterRule) EditheaderTest(com.zimbra.soap.mail.type.EditheaderTest) FilterAction(com.zimbra.soap.mail.type.FilterAction) EditheaderTest(com.zimbra.soap.mail.type.EditheaderTest) Test(org.junit.Test)

Example 3 with FilterAction

use of com.zimbra.soap.mail.type.FilterAction in project zm-mailbox by Zimbra.

the class GetFilterRulesAdminTest method testSieveToSoapReplaceheaderActionBasicWithRelationalComparatorAndCount.

@Test
public void testSieveToSoapReplaceheaderActionBasicWithRelationalComparatorAndCount() throws ServiceException {
    RuleManager.clearCachedRules(account);
    String script = "require [\"editheader\", \"variables\"];\n" + "# rule1\n" + "replaceheader :newname \"X-My-Header2\" :newvalue \"[new] ${1}\" :comparator \"i;ascii-numeric\" :count \"eq\" \"X-My-Header\" \"3\";";
    account.setAdminSieveScriptBefore(script);
    List<FilterRule> filterRules = RuleManager.getAdminRulesAsXML(account, FilterType.INCOMING, AdminFilterType.BEFORE);
    Assert.assertEquals(filterRules.size(), 1);
    FilterRule filterRule = filterRules.get(0);
    Assert.assertEquals("rule1", filterRule.getName());
    Assert.assertTrue(filterRule.isActive());
    Assert.assertEquals(1, filterRule.getFilterActions().size());
    FilterAction filterAction = filterRule.getFilterActions().get(0);
    Assert.assertTrue(filterAction instanceof FilterAction.ReplaceheaderAction);
    FilterAction.ReplaceheaderAction action = (FilterAction.ReplaceheaderAction) filterAction;
    Assert.assertNull(action.getLast());
    Assert.assertNull(action.getOffset());
    Assert.assertEquals("X-My-Header2", action.getNewName());
    Assert.assertEquals("[new] ${1}", action.getNewValue());
    EditheaderTest test = action.getTest();
    Assert.assertEquals("i;ascii-numeric", test.getComparator());
    Assert.assertNull(test.getMatchType());
    Assert.assertEquals("eq", test.getRelationalComparator());
    Assert.assertTrue(test.getCount());
    Assert.assertNull(test.getValue());
    Assert.assertEquals("X-My-Header", test.getHeaderName());
    List<String> values = test.getHeaderValue();
    Assert.assertEquals(1, values.size());
    Assert.assertEquals("3", values.get(0));
}
Also used : FilterRule(com.zimbra.soap.mail.type.FilterRule) EditheaderTest(com.zimbra.soap.mail.type.EditheaderTest) FilterAction(com.zimbra.soap.mail.type.FilterAction) EditheaderTest(com.zimbra.soap.mail.type.EditheaderTest) Test(org.junit.Test)

Example 4 with FilterAction

use of com.zimbra.soap.mail.type.FilterAction in project zm-mailbox by Zimbra.

the class GetFilterRulesAdminTest method testSieveToSoapDeleteheaderActionWithIndexAndLast.

@Test
public void testSieveToSoapDeleteheaderActionWithIndexAndLast() throws ServiceException {
    RuleManager.clearCachedRules(account);
    String script = "require [\"editheader\"];\n" + "# rule1\n" + // matchType and comparator should be added by default
    "deleteheader :last :index 3 \"X-My-Header\" \"Test Value\";";
    account.setAdminSieveScriptBefore(script);
    List<FilterRule> filterRules = RuleManager.getAdminRulesAsXML(account, FilterType.INCOMING, AdminFilterType.BEFORE);
    Assert.assertEquals(filterRules.size(), 1);
    FilterRule filterRule = filterRules.get(0);
    Assert.assertEquals("rule1", filterRule.getName());
    Assert.assertTrue(filterRule.isActive());
    Assert.assertEquals(1, filterRule.getFilterActions().size());
    FilterAction filterAction = filterRule.getFilterActions().get(0);
    Assert.assertTrue(filterAction instanceof FilterAction.DeleteheaderAction);
    FilterAction.DeleteheaderAction action = (FilterAction.DeleteheaderAction) filterAction;
    Assert.assertTrue(action.getLast());
    Assert.assertEquals(3, action.getOffset().intValue());
    EditheaderTest test = action.getTest();
    Assert.assertEquals("i;ascii-casemap", test.getComparator());
    Assert.assertEquals("is", test.getMatchType());
    Assert.assertNull(test.getRelationalComparator());
    Assert.assertNull(test.getCount());
    Assert.assertNull(test.getValue());
    Assert.assertEquals("X-My-Header", test.getHeaderName());
    List<String> values = test.getHeaderValue();
    Assert.assertEquals(1, values.size());
    Assert.assertEquals("Test Value", values.get(0));
}
Also used : FilterRule(com.zimbra.soap.mail.type.FilterRule) EditheaderTest(com.zimbra.soap.mail.type.EditheaderTest) FilterAction(com.zimbra.soap.mail.type.FilterAction) EditheaderTest(com.zimbra.soap.mail.type.EditheaderTest) Test(org.junit.Test)

Example 5 with FilterAction

use of com.zimbra.soap.mail.type.FilterAction in project zm-mailbox by Zimbra.

the class GetFilterRulesAdminTest method testSieveToSoapReplaceheaderActionBasicWithRelationalComparatorAndValue.

@Test
public void testSieveToSoapReplaceheaderActionBasicWithRelationalComparatorAndValue() throws ServiceException {
    RuleManager.clearCachedRules(account);
    String script = "require [\"editheader\", \"variables\"];\n" + "# rule1\n" + "replaceheader :newname \"X-My-Header2\" :newvalue \"[new] ${1}\" :comparator \"i;ascii-numeric\" :value \"ge\" \"X-My-Header\" \"2\";";
    account.setAdminSieveScriptBefore(script);
    List<FilterRule> filterRules = RuleManager.getAdminRulesAsXML(account, FilterType.INCOMING, AdminFilterType.BEFORE);
    Assert.assertEquals(filterRules.size(), 1);
    FilterRule filterRule = filterRules.get(0);
    Assert.assertEquals("rule1", filterRule.getName());
    Assert.assertTrue(filterRule.isActive());
    Assert.assertEquals(1, filterRule.getFilterActions().size());
    FilterAction filterAction = filterRule.getFilterActions().get(0);
    Assert.assertTrue(filterAction instanceof FilterAction.ReplaceheaderAction);
    FilterAction.ReplaceheaderAction action = (FilterAction.ReplaceheaderAction) filterAction;
    Assert.assertNull(action.getLast());
    Assert.assertNull(action.getOffset());
    Assert.assertEquals("X-My-Header2", action.getNewName());
    Assert.assertEquals("[new] ${1}", action.getNewValue());
    EditheaderTest test = action.getTest();
    Assert.assertEquals("i;ascii-numeric", test.getComparator());
    Assert.assertNull(test.getMatchType());
    Assert.assertEquals("ge", test.getRelationalComparator());
    Assert.assertNull(test.getCount());
    Assert.assertTrue(test.getValue());
    Assert.assertEquals("X-My-Header", test.getHeaderName());
    List<String> values = test.getHeaderValue();
    Assert.assertEquals(1, values.size());
    Assert.assertEquals("2", values.get(0));
}
Also used : FilterRule(com.zimbra.soap.mail.type.FilterRule) EditheaderTest(com.zimbra.soap.mail.type.EditheaderTest) FilterAction(com.zimbra.soap.mail.type.FilterAction) EditheaderTest(com.zimbra.soap.mail.type.EditheaderTest) Test(org.junit.Test)

Aggregations

FilterAction (com.zimbra.soap.mail.type.FilterAction)30 FilterRule (com.zimbra.soap.mail.type.FilterRule)24 Test (org.junit.Test)23 EditheaderTest (com.zimbra.soap.mail.type.EditheaderTest)19 FilterTest (com.zimbra.soap.mail.type.FilterTest)10 FilterTests (com.zimbra.soap.mail.type.FilterTests)8 FilterVariables (com.zimbra.soap.mail.type.FilterVariables)5 Sieve (com.zimbra.common.filter.Sieve)4 TreeMap (java.util.TreeMap)4 ServiceException (com.zimbra.common.service.ServiceException)3 Account (com.zimbra.cs.account.Account)3 ArrayList (java.util.ArrayList)3 Element (com.zimbra.common.soap.Element)2 JSONElement (com.zimbra.common.soap.Element.JSONElement)2 XMLElement (com.zimbra.common.soap.Element.XMLElement)2 GetFilterRulesResponse (com.zimbra.soap.mail.message.GetFilterRulesResponse)2 NestedRule (com.zimbra.soap.mail.type.NestedRule)2 XmlAnyElement (javax.xml.bind.annotation.XmlAnyElement)2 XmlElement (javax.xml.bind.annotation.XmlElement)2 List (java.util.List)1