Exemplo n.º 1
0
        public void ShouldCollectDataResults()
        {
            const int count          = 3;
            var       ruleSetHandler = new MaximumCountRuleSetHandler <TestObject>
            {
                RuleSetName        = RuleSet,
                ErrorDescription   = ErrorDescription,
                MaximumResultCount = 999,
            };

            var stubs = AddTestObjects(count);

            foreach (var st in stubs)
            {
                ruleSetHandler.VerifyRule(st);
            }
            Assert.Equal(count, ruleSetHandler.Items.Count());
        }
Exemplo n.º 2
0
        public void ShouldThrowIfMaxCountExceeded()
        {
            var ruleSetHandler = new MaximumCountRuleSetHandler <TestObject>
            {
                RuleSetName        = RuleSet,
                ErrorDescription   = ErrorDescription,
                MaximumResultCount = 3
            };

            ruleSetHandler.UseMemoryCacheStorage(Guid.NewGuid().ToString());
            const int count = 3;
            var       stubs = AddTestObjects(count);

            foreach (var st in stubs.Skip(1))
            {
                ruleSetHandler.VerifyRule(st);
            }
            Assert.Throws <HttpException>(() => ruleSetHandler.VerifyRule(stubs.First()));
        }
Exemplo n.º 3
0
        public void ShouldNotAddToCollectionWhenUpdateRuleCollectionOnSuccessSetToFalse()
        {
            const int count          = 3;
            var       ruleSetHandler = new MaximumCountRuleSetHandler <TestObject>
            {
                RuleSetName                   = RuleSet,
                ErrorDescription              = ErrorDescription,
                MaximumResultCount            = 2,
                UpdateRuleCollectionOnSuccess = false
            };

            ruleSetHandler.UseMemoryCacheStorage(Guid.NewGuid().ToString());
            var stubs = AddTestObjects(count);

            foreach (var st in stubs)
            {
                ruleSetHandler.VerifyRule(st);
            }

            Assert.Equal(0, ruleSetHandler.Items.Count());
        }
Exemplo n.º 4
0
        public void ShouldThrowCustomAction()
        {
            const int count          = 5;
            var       ruleSetHandler = new MaximumCountRuleSetHandler <TestObject>
            {
                RuleSetName        = RuleSet,
                ErrorDescription   = ErrorDescription,
                MaximumResultCount = 5,
                InvalidAction      = () => { throw new DivideByZeroException(ErrorDescription); },
            };

            ruleSetHandler.UseMemoryCacheStorage(Guid.NewGuid().ToString());
            var stubs = AddTestObjects(count);

            foreach (var st in stubs.Skip(1))
            {
                ruleSetHandler.VerifyRule(st);
            }

            Assert.Throws <DivideByZeroException>(() => ruleSetHandler.VerifyRule(TestObject.Build()));
        }
Exemplo n.º 5
0
        public void ShouldVerifyAllRules()
        {
            int count = 0;
            var rule1 = new MaximumCountRuleSetHandler <TestObject>()
            {
                MaximumResultCount = 0,
                InvalidAction      = () => count++
            };

            var rule2 = new MaximumCountRuleSetHandler <IpAddressRequest>()
            {
                MaximumResultCount = 0,
                InvalidAction      = () => count++,
            };

            RuleSetRunner.Instance.AddRule <MaximumCountRuleSetHandler <TestObject> >(rule1);
            RuleSetRunner.Instance.AddRule <MaximumCountRuleSetHandler <IpAddressRequest> >(rule2);

            rule1.VerifyRule(new TestObject());
            rule2.VerifyRule(new IpAddressRequest());

            RuleSetRunner.Instance.VerifyAllRules();
            Assert.Equal(4, count);  // rule will auto trigger once each time it hits the rule - then again once run
        }