コード例 #1
0
        public void TestValidationNoRuleNoCondition()
        {
            var          container   = GetConfiguredContainer();
            IRuleManager ruleManager = container.Resolve <IRuleManager>();

            // Rule created to Item 1
            MyDummyDtObject myDummyDtObject = new MyDummyDtObject();
            RuleContext     ruleContext     = new RuleContext(myDummyDtObject, RuleConstants.EmptyRuleConstants);

            //The division field is null here
            bool isValid = ruleManager.IsRuleValid(1, ruleContext);

            //The rule should NOT be valid here.
            Assert.IsFalse(isValid);

            //The division is set to "BTL" here
            myDummyDtObject.Division = "BTL";
            //The rule should NOT be valid too
            isValid = ruleManager.IsRuleValid(1, ruleContext);
            Assert.IsFalse(isValid);

            //The division is set to "ABC" here
            myDummyDtObject.Division = "ABC";
            //The rule should be valid too
            isValid = ruleManager.IsRuleValid(1, ruleContext);
            Assert.IsFalse(isValid);
        }
コード例 #2
0
        public void TestValidationOneRuleOneCondition()
        {
            var          container   = GetConfiguredContainer();
            IRuleManager ruleManager = container.Resolve <IRuleManager>();

            // Rule created to Item 1
            RuleDefinition rule = new RuleDefinition(null, null, 1, "My Rule 1");

            ruleManager.AddRule(rule);

            RuleConditionDefinition condition = new RuleConditionDefinition(null, "Division", "=", "BTL", rule.Id);

            ruleManager.AddCondition(condition);

            MyDummyDtObject myDummyDtObject = new MyDummyDtObject();
            RuleContext     ruleContext     = new RuleContext(myDummyDtObject, RuleConstants.EmptyRuleConstants);

            //The division field is null here
            bool isValid = ruleManager.IsRuleValid(1, ruleContext);

            //The rule should NOT be valid here.
            Assert.IsFalse(isValid);

            //The division is set to "BTL" here
            myDummyDtObject.Division = "BTL";
            ruleContext = new RuleContext(myDummyDtObject, RuleConstants.EmptyRuleConstants);
            //The rule should be valid now
            isValid = ruleManager.IsRuleValid(1, ruleContext);
            Assert.IsTrue(isValid);
        }
コード例 #3
0
        public void TestValidationManyRulesOneCondition()
        {
            var          container   = GetConfiguredContainer();
            IRuleManager ruleManager = container.Resolve <IRuleManager>();

            // Rule created to Item 1
            RuleDefinition rule_1 = new RuleDefinition(null, null, 1, "My Rule 1");

            ruleManager.AddRule(rule_1);

            RuleConditionDefinition condition_1 = new RuleConditionDefinition(null, "Division", "=", "BTL", rule_1.Id);

            ruleManager.AddCondition(condition_1);

            RuleDefinition rule_2 = new RuleDefinition(null, null, 1, "My Rule 2");

            ruleManager.AddRule(rule_2);

            RuleConditionDefinition condition_2 = new RuleConditionDefinition(null, "Entity", "=", "ENT_1", rule_2.Id);

            ruleManager.AddCondition(condition_2);

            MyDummyDtObject myDummyDtObject = new MyDummyDtObject();
            RuleContext     ruleContext     = new RuleContext(myDummyDtObject, RuleConstants.EmptyRuleConstants);

            //The division and entity field are null here
            bool isValid = ruleManager.IsRuleValid(1, ruleContext);

            //The rule should NOT be valid here.
            Assert.IsFalse(isValid);

            //The division is set to "BTL" here
            myDummyDtObject.Division = "BTL";
            ruleContext = new RuleContext(myDummyDtObject, RuleConstants.EmptyRuleConstants);
            //The rule should be valid as it match 1 rule
            isValid = ruleManager.IsRuleValid(1, ruleContext);
            Assert.IsTrue(isValid);

            //The entity is set to "ENT_1" here
            myDummyDtObject.Entity = "ENT_1";
            ruleContext            = new RuleContext(myDummyDtObject, RuleConstants.EmptyRuleConstants);
            //The rule should be valid now (2 rules valid)
            isValid = ruleManager.IsRuleValid(1, ruleContext);
            Assert.IsTrue(isValid);

            //The division is set to "UNKNOWN_ENT" here
            myDummyDtObject.Entity   = "UNKNOWN_ENT";
            myDummyDtObject.Division = "DIV";
            ruleContext = new RuleContext(myDummyDtObject, RuleConstants.EmptyRuleConstants);
            //The rule should NOT be valid anymore
            isValid = ruleManager.IsRuleValid(1, ruleContext);
            Assert.IsFalse(isValid);
        }
コード例 #4
0
        public void TestValidationOneSelectorOneFilter()
        {
            var             container      = GetConfiguredContainer();
            IRuleManager    ruleManager    = container.Resolve <IRuleManager>();
            IAccountManager accountManager = container.Resolve <IAccountManager>();

            int actId1 = 10000;

            AccountGroup accountGroup = new AccountGroup("1", "Group activity 1");
            AccountUser  account      = new AccountUserBuilder("0").WithDisplayName("User 1")
                                        .WithEmail("*****@*****.**")
                                        .Build();

            accountManager.GetStore().SaveGroup(accountGroup);
            accountManager.GetStore().SaveAccounts(new List <AccountUser>()
            {
                account
            });
            accountManager.GetStore().Attach(account.Id, accountGroup.Id);

            // Selector created to Item 1
            SelectorDefinition selector = new SelectorDefinition(null, DateTime.Now, actId1, accountGroup.Id);

            ruleManager.AddSelector(selector);

            RuleFilterDefinition filterDefinition = new RuleFilterDefinition(null, "Division", "=", "BTL", selector.Id);

            ruleManager.AddFilter(filterDefinition);

            MyDummyDtObject myDummyDtObject = new MyDummyDtObject();

            myDummyDtObject.Division = "BTL";
            RuleContext         ruleContext      = new RuleContext(myDummyDtObject, RuleConstants.EmptyRuleConstants);
            IList <AccountUser> selectedAccounts = ruleManager.SelectAccounts(actId1, ruleContext);

            Assert.IsNotNull(selectedAccounts);
            Assert.AreEqual(1, selectedAccounts.Count);
            Assert.IsTrue(selectedAccounts.SequenceEqual(new List <AccountUser>()
            {
                account
            }, new AccountEqualityComparer()));
        }
コード例 #5
0
        public void TestValidationManySelectorsManyFilters()
        {
            var             container      = GetConfiguredContainer();
            IRuleManager    ruleManager    = container.Resolve <IRuleManager>();
            IAccountManager accountManager = container.Resolve <IAccountManager>();

            int actId1 = 10000;

            AccountGroup accountGroup_1 = new AccountGroup("1", "Group activity 1");

            AccountUser account_1_1 = new AccountUserBuilder("0")
                                      .WithDisplayName("User 1 Group 1")
                                      .WithEmail("*****@*****.**")
                                      .Build();

            AccountUser account_1_2 = new AccountUserBuilder("1")
                                      .WithDisplayName("User 2 Group 1")
                                      .WithEmail("*****@*****.**")
                                      .Build();

            accountManager.GetStore().SaveGroup(accountGroup_1);
            accountManager.GetStore().SaveAccounts(new List <AccountUser>()
            {
                account_1_1, account_1_2
            });
            accountManager.GetStore().Attach(account_1_1.Id, accountGroup_1.Id);
            accountManager.GetStore().Attach(account_1_2.Id, accountGroup_1.Id);

            AccountGroup accountGroup_2 = new AccountGroup("2", "Group activity 2");

            AccountUser account_2_1 = new AccountUserBuilder("2")
                                      .WithDisplayName("User 1 Group 2")
                                      .WithEmail("*****@*****.**")
                                      .Build();

            AccountUser account_2_2 = new AccountUserBuilder("3")
                                      .WithDisplayName("User 2 Group 2")
                                      .WithEmail("*****@*****.**")
                                      .Build();

            accountManager.GetStore().SaveAccounts(new List <AccountUser>()
            {
                account_2_1, account_2_2
            });
            accountManager.GetStore().SaveGroup(accountGroup_2);
            accountManager.GetStore().Attach(account_2_1.Id, accountGroup_2.Id);
            accountManager.GetStore().Attach(account_2_2.Id, accountGroup_2.Id);

            // Selector 1 created to Item 1
            SelectorDefinition selector_1 = new SelectorDefinition(null, DateTime.Now, actId1, accountGroup_1.Id);

            ruleManager.AddSelector(selector_1);

            RuleFilterDefinition filterDefinition_1_1 = new RuleFilterDefinition(null, "Division", "=", "BTL", selector_1.Id);

            ruleManager.AddFilter(filterDefinition_1_1);

            RuleFilterDefinition filterDefinition_1_2 = new RuleFilterDefinition(null, "Entity", "=", "ENT", selector_1.Id);

            ruleManager.AddFilter(filterDefinition_1_2);

            // Selector 2 created to Item 1
            SelectorDefinition selector_2 = new SelectorDefinition(null, DateTime.Now, actId1, accountGroup_2.Id);

            ruleManager.AddSelector(selector_2);

            RuleFilterDefinition filterDefinition_2_1 = new RuleFilterDefinition(null, "Division", "=", "BTL", selector_2.Id);

            ruleManager.AddFilter(filterDefinition_2_1);

            RuleFilterDefinition filterDefinition_2_2 = new RuleFilterDefinition(null, "Nom", "=", "DONALD", selector_2.Id);

            ruleManager.AddFilter(filterDefinition_2_2);

            //
            MyDummyDtObject myDummyDtObject = new MyDummyDtObject();

            myDummyDtObject.Division = "BTL";
            RuleContext ruleContext = new RuleContext(myDummyDtObject, RuleConstants.EmptyRuleConstants);

            // The entity only has entity set to ENT. No selectors should match
            IList <AccountUser> selectedAccounts_1 = ruleManager.SelectAccounts(actId1, ruleContext);

            Assert.IsNotNull(selectedAccounts_1);
            Assert.AreEqual(selectedAccounts_1.Count, 0);

            // Set entity to ENT
            myDummyDtObject.Entity = "ENT";
            ruleContext            = new RuleContext(myDummyDtObject, RuleConstants.EmptyRuleConstants);
            // Only Group 1 should match
            IList <AccountUser> selectedAccounts_2 = ruleManager.SelectAccounts(actId1, ruleContext);

            Assert.IsNotNull(selectedAccounts_2);
            Assert.AreEqual(selectedAccounts_2.Count, 2);
            Assert.IsTrue(selectedAccounts_2.SequenceEqual(new List <AccountUser>()
            {
                account_1_1, account_1_2
            }, new AccountEqualityComparer()));

            // Set entity to XXX
            myDummyDtObject.Entity = "XXX";
            myDummyDtObject.Nom    = "DONALD";
            ruleContext            = new RuleContext(myDummyDtObject, RuleConstants.EmptyRuleConstants);
            // Only Group 2 should match
            IList <AccountUser> selectedAccounts_3 = ruleManager.SelectAccounts(actId1, ruleContext);

            Assert.IsNotNull(selectedAccounts_3);
            Assert.AreEqual(selectedAccounts_3.Count, 2);
            Assert.IsTrue(selectedAccounts_3.SequenceEqual(new List <AccountUser>()
            {
                account_2_1, account_2_2
            }, new AccountEqualityComparer()));

            // Set entity to ENT
            myDummyDtObject.Entity = "ENT";
            ruleContext            = new RuleContext(myDummyDtObject, RuleConstants.EmptyRuleConstants);
            // Group 1 and Group 2 should match
            IList <AccountUser> selectedAccounts_4 = ruleManager.SelectAccounts(actId1, ruleContext);

            Assert.IsNotNull(selectedAccounts_4);
            Assert.AreEqual(selectedAccounts_4.Count, 4);
            Assert.IsTrue(selectedAccounts_4.SequenceEqual(new List <AccountUser>()
            {
                account_1_1, account_1_2, account_2_1, account_2_2
            }, new AccountEqualityComparer()));
        }
コード例 #6
0
        public void TestValidationOneSelectorManyFilters()
        {
            var             container      = GetConfiguredContainer();
            IRuleManager    ruleManager    = container.Resolve <IRuleManager>();
            IAccountManager accountManager = container.Resolve <IAccountManager>();

            int actId1 = 10000;

            AccountGroup accountGroup = new AccountGroup("1", "Group activity 1");
            AccountUser  account      = new AccountUserBuilder("0")
                                        .WithDisplayName("User 1")
                                        .WithEmail("*****@*****.**")
                                        .Build();

            accountManager.GetStore().SaveGroup(accountGroup);
            accountManager.GetStore().SaveAccounts(new List <AccountUser>()
            {
                account
            });
            accountManager.GetStore().Attach(account.Id, accountGroup.Id);

            // Selector created to Item 1
            SelectorDefinition selector_1 = new SelectorDefinition(null, DateTime.Now, actId1, accountGroup.Id);

            ruleManager.AddSelector(selector_1);

            RuleFilterDefinition filterDefinition_1_1 = new RuleFilterDefinition(null, "Division", "=", "BTL", selector_1.Id);

            ruleManager.AddFilter(filterDefinition_1_1);

            RuleFilterDefinition filterDefinition_1_2 = new RuleFilterDefinition(null, "Entity", "=", "ENT", selector_1.Id);

            ruleManager.AddFilter(filterDefinition_1_2);

            MyDummyDtObject myDummyDtObject = new MyDummyDtObject();

            myDummyDtObject.Division = "BTL";

            RuleContext ruleContext = new RuleContext(myDummyDtObject, RuleConstants.EmptyRuleConstants);

            // The entity is not set to ENT. The selector should not match
            IList <AccountUser> selectedAccounts_1 = ruleManager.SelectAccounts(actId1, ruleContext);

            Assert.IsNotNull(selectedAccounts_1);
            Assert.AreEqual(selectedAccounts_1.Count, 0);

            //We set the entity to 'ENT'
            myDummyDtObject.Entity = "ENT";
            ruleContext            = new RuleContext(myDummyDtObject, RuleConstants.EmptyRuleConstants);
            // The selector should match now.
            IList <AccountUser> selectedAccounts_2 = ruleManager.SelectAccounts(actId1, ruleContext);

            Assert.IsNotNull(selectedAccounts_2);
            Assert.AreEqual(selectedAccounts_2.Count, 1);
            Assert.IsTrue(selectedAccounts_2.SequenceEqual(new List <AccountUser>()
            {
                account
            }, new AccountEqualityComparer()));

            //We set the entity to 'XXX'
            myDummyDtObject.Entity = "XXX";
            ruleContext            = new RuleContext(myDummyDtObject, RuleConstants.EmptyRuleConstants);
            // The selector should not match .
            IList <AccountUser> selectedAccounts_3 = ruleManager.SelectAccounts(actId1, ruleContext);

            Assert.IsNotNull(selectedAccounts_3);
            Assert.AreEqual(selectedAccounts_3.Count, 0);
        }