Beispiel #1
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);
        }
Beispiel #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);
        }
Beispiel #3
0
        public void TestAddUpdateDelete()
        {
            int item1 = 10000;
            int item2 = 20000;

            var          container   = GetConfiguredContainer();
            IRuleManager ruleManager = container.Resolve <IRuleManager>();

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

            ruleManager.AddRule(rule);

            IList <RuleDefinition> rulesFetch_1_1 = ruleManager.GetRulesForItemId(item1);

            Assert.IsNotNull(rulesFetch_1_1);
            Assert.AreEqual(1, rulesFetch_1_1.Count);
            Assert.IsTrue(rulesFetch_1_1.SequenceEqual(new List <RuleDefinition>()
            {
                rule
            }, new RuleEqualityComparer()));

            // Update rule. This is now associated with Item 2
            rule.ItemId = item2;
            ruleManager.UpdateRule(rule);

            // The rule is not associated to item 1 anymore
            IList <RuleDefinition> rulesFetch_1_0 = ruleManager.GetRulesForItemId(item1);

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

            // The rule should be associated with item 2
            IList <RuleDefinition> rulesFetch_2_1 = ruleManager.GetRulesForItemId(item2);

            Assert.IsNotNull(rulesFetch_2_1);
            Assert.AreEqual(1, rulesFetch_2_1.Count);
            Assert.IsTrue(rulesFetch_2_1.SequenceEqual(new List <RuleDefinition>()
            {
                rule
            }, new RuleEqualityComparer()));

            // Update rule. This is now associated with Item 2
            ruleManager.RemoveRule(rule);

            // No rule should be associated with item 2
            IList <RuleDefinition> rulesFetch_2_0 = ruleManager.GetRulesForItemId(item2);

            Assert.IsNotNull(rulesFetch_2_0);
            Assert.AreEqual(0, rulesFetch_2_0.Count);
        }
Beispiel #4
0
        public void TestAddRule()
        {
            int item1 = 10000;
            int item2 = 20000;

            var          container   = GetConfiguredContainer();
            IRuleManager ruleManager = container.Resolve <IRuleManager>();

            DateTime       now   = DateTime.Now;
            RuleDefinition rule1 = new RuleDefinition(null, now, item1, "My Rule 1");
            RuleDefinition rule2 = new RuleDefinition(null, now, item2, "My Rule 2");
            RuleDefinition rule3 = new RuleDefinition(null, now, item2, "My Rule 3");

            ruleManager.AddRule(rule1);
            ruleManager.AddRule(rule2);
            ruleManager.AddRule(rule3);

            // Only 1 rule
            IList <RuleDefinition> rulesFetch1 = ruleManager.GetRulesForItemId(item1);

            Assert.IsNotNull(rulesFetch1);
            Assert.AreEqual(rulesFetch1.Count, 1);

            Assert.IsTrue(rulesFetch1.SequenceEqual(new List <RuleDefinition>()
            {
                rule1
            }, new RuleEqualityComparer()));

            // 2 rules
            IList <RuleDefinition> rulesFetch2 = ruleManager.GetRulesForItemId(item2);

            Assert.IsNotNull(rulesFetch2);
            Assert.AreEqual(rulesFetch2.Count, 2);
            Assert.IsTrue(rulesFetch2.SequenceEqual(new List <RuleDefinition>()
            {
                rule2, rule3
            }, new RuleEqualityComparer()));
        }
Beispiel #5
0
        public void AddRule(WfActivityDefinition wfActivity, RuleDefinition ruleDefinition, List <RuleConditionDefinition> conditions)
        {
            Debug.Assert(wfActivity != null);
            Debug.Assert(ruleDefinition != null);
            Debug.Assert(conditions != null);
            // --
            ruleDefinition.ItemId = wfActivity.WfadId;
            _ruleManager.AddRule(ruleDefinition);

            foreach (RuleConditionDefinition ruleConditionDefinition in conditions)
            {
                ruleConditionDefinition.RudId = ruleDefinition.Id;
                _ruleManager.AddCondition(ruleConditionDefinition);
            }
        }
Beispiel #6
0
        public void Add_Physical_Product_Rule()
        {
            Payment payment = new Payment();

            payment.PaymentType = Enum_PaymentType.PhysicalProduct;
            payment.ProductName = "Computer";
            IRule rule = new PhysicalProductRule();

            ruleManager.AddRule(rule);
            var  ruleResult            = ruleManager.ExecuteRules(payment);
            bool isPhysicalRuleApplied = ruleResult.Contains("Generate Packing Slip");

            Assert.IsTrue(isPhysicalRuleApplied);
        }