public void TestNestedParallelRules2()
 {
     Foo f  = new Foo();
     var rr = RuleEngine <Foo> .GetInstance(f)
              .ApplyRules(new UpdateName())
              .ExecuteAsync().Result;
 }
        public void TestInvoke()
        {
            var ruleResults = RuleEngine <Product> .GetInstance(new Product())
                              .ApplyRules(new ProductRule())
                              .Execute();

            Assert.Equal("Product Description", ruleResults.FindRuleResult <ProductRule>().Result);
        }
Example #3
0
        public void TestTerminate()
        {
            var ruleEngineExecutor = RuleEngine <Product> .GetInstance(new Product());

            ruleEngineExecutor.AddRules(new ProductTerminateA(), new ProductTerminateB());
            var ruleResults = ruleEngineExecutor.Execute();

            Assert.AreEqual(1, ruleResults.Length);
        }
Example #4
0
        public void TestExecutionOrder()
        {
            var ruleResults = RuleEngine <Product> .GetInstance(new Product())
                              .ApplyRules(new ProductAExecutionOrderRuleAsync(), new ProductBExecutionOrderRuleAsync())
                              .ExecuteAsync().Result;

            Assert.AreEqual("ProductBExecutionOrderRuleAsync", ruleResults.First().Name);
            Assert.AreEqual("ProductAExecutionOrderRuleAsync", ruleResults.Skip(1).First().Name);
        }
Example #5
0
        public void TestTryAddTryGetValueAsync()
        {
            var ruleEngineExecutor = RuleEngine <Product> .GetInstance(new Product());

            ruleEngineExecutor.AddRules(new ProductTryAddAsync(), new ProductTryGetValueAsync());
            var ruleResults = ruleEngineExecutor.ExecuteAsync().Result;

            Assert.AreEqual("Product Description", ruleResults.First().Result);
        }
Example #6
0
        public void TestConstraintAsync()
        {
            var ruleEngineExecutor = RuleEngine <Product> .GetInstance(new Product());

            ruleEngineExecutor.AddRules(new ProductConstraintAsyncA(), new ProductConstraintAsyncB());
            var ruleResults = ruleEngineExecutor.ExecuteAsync().Result;

            Assert.AreEqual(1, ruleResults.Length);
        }
        public void TestSkip()
        {
            var ruleEngineExecutor = RuleEngine <Product> .GetInstance(new Product());

            ruleEngineExecutor.AddRules(new ProductSkip());
            var ruleResults = ruleEngineExecutor.Execute();

            Assert.False(ruleResults.Any());
        }
        public void TestConstraint()
        {
            var ruleEngineExecutor = RuleEngine <Product> .GetInstance(new Product());

            ruleEngineExecutor.AddRules(new ProductConstraintA(), new ProductConstraintB());
            var ruleResults = ruleEngineExecutor.Execute();

            Assert.Single(ruleResults);
        }
        public void TestExecutionOrder()
        {
            var ruleResults = RuleEngine <Product> .GetInstance(new Product())
                              .ApplyRules(new ProductExecutionOrderRuleA(), new ProductExecutionOrderRuleB())
                              .Execute();

            Assert.Equal("ProductExecutionOrderRuleB", ruleResults.First().Name);
            Assert.Equal("ProductExecutionOrderRuleA", ruleResults.Skip(1).First().Name);
        }
        public void TestTerminateAsync()
        {
            var ruleEngineExecutor = RuleEngine <Product> .GetInstance(new Product());

            ruleEngineExecutor.AddRules(new ProductTerminateAsyncA(), new ProductTerminateAsyncB());
            var ruleResults = ruleEngineExecutor.ExecuteAsync().Result;

            Assert.Single(ruleResults);
        }
        public void TestInvokeAsync()
        {
            var ruleEngineExecutor = RuleEngine <Product> .GetInstance(new Product());

            ruleEngineExecutor.AddRules(new ProductRuleAsync());
            var ruleResults = ruleEngineExecutor.ExecuteAsync().Result;

            Assert.Equal("Product Description", ruleResults.First().Result);
        }
Example #12
0
        public void TestErrorResult()
        {
            var errors = RuleEngine <Product> .GetInstance(new Product())
                         .ApplyRules(new ProductRule(), new ProductRuleError())
                         .Execute()
                         .GetErrors();

            Assert.Equal("Error", errors.FindRuleResult <ProductRuleError>().Error.Message);
            Assert.Equal(typeof(Exception), errors.FindRuleResult <ProductRuleError>().Error.Exception.GetType());
        }
        public void TestGlobalExceptionHandlerAsync()
        {
            var product            = new Product();
            var ruleEngineExecutor = RuleEngine <Product> .GetInstance(product);

            ruleEngineExecutor.AddRules(new ProductGlobalExceptionHandlerAsync(), new ProductExceptionThrownAsync());
            var rr = ruleEngineExecutor.ExecuteAsync().Result;

            Assert.NotNull(rr.FindRuleResult <ProductGlobalExceptionHandlerAsync>().Error.Exception);
        }
        public void TestExceptionHandler()
        {
            var product            = new Product();
            var ruleEngineExecutor = RuleEngine <Product> .GetInstance(product);

            ruleEngineExecutor.AddRules(new ProductExceptionHandler(), new ProductExceptionThrown());
            var rr = ruleEngineExecutor.Execute();

            Assert.IsNotNull(rr.FindRuleResult <ProductExceptionHandler>().Error.Exception);
        }
Example #15
0
        public void TestExecutionOrder()
        {
            var ruleResults = RuleEngine <Product> .GetInstance(new Product())
                              .ApplyRules(new ProductAExecutionOrderRuleAsync(), new ProductBExecutionOrderRuleAsync())
                              .ExecuteAsync().Result;

            var productBExecutionOrderRuleAsync = ruleResults.FindRuleResult <ProductBExecutionOrderRuleAsync>();
            var productAExecutionOrderRuleAsync = ruleResults.FindRuleResult <ProductAExecutionOrderRuleAsync>();

            Assert.IsTrue(productBExecutionOrderRuleAsync.Result.To <long>() <= productAExecutionOrderRuleAsync.Result.To <long>());
        }
        public void TestAsyncNestedRules()
        {
            var ruleEngineExecutor = RuleEngine <Product> .GetInstance(new Product());

            ruleEngineExecutor.AddRules(new ProductNestedRuleAsync());

            var ruleResults = ruleEngineExecutor.ExecuteAsync().Result;

            Assert.NotNull(ruleResults);
            Assert.Equal("ProductNestedRuleAsyncC", ruleResults.FindRuleResult <ProductNestedRuleAsyncC>().Name);
        }
        public void TestProactiveRules()
        {
            var product            = new Product();
            var ruleEngineExecutor = RuleEngine <Product> .GetInstance(product);

            ruleEngineExecutor.AddRules(new ProductRule(), new ProductProactiveRule());
            var rr = ruleEngineExecutor.Execute();

            Assert.True(rr.FindRuleResult <ProductProactiveRule>().Data["Ticks"].To <long>() <= rr.FindRuleResult <ProductRule>().Data["Ticks"].To <long>(),
                        $"expected {rr.FindRuleResult<ProductProactiveRule>().Data["Ticks"]} actual {rr.FindRuleResult<ProductRule>().Data["Ticks"]}");
        }
Example #18
0
        public void TestNestedRuleError()
        {
            var ruleEngineExecutor = RuleEngine <Product> .GetInstance(new Product());

            ruleEngineExecutor.AddRules(new ProductNestedErrorRule());
            var ruleResults = ruleEngineExecutor.Execute();
            var errors      = ruleResults.GetErrors();

            Assert.IsNotNull(errors);
            Assert.AreEqual("Error", errors.FindRuleResult <ProductChildErrorRule>().Error.Message);
        }
Example #19
0
        public void TestAfterInvoke()
        {
            var ruleEngineExecutor = RuleEngine <Product> .GetInstance(new Product());

            ruleEngineExecutor.AddRules(new ProductTerminateA(), new ProductTerminateB());
            var ruleResults = ruleEngineExecutor.Execute();
            var ruleResult  = ruleResults.FindRuleResult("ProductRule");

            Assert.Single(ruleResults);
            Assert.NotNull(ruleResult);
        }
Example #20
0
        public void TestNestedRules()
        {
            var ruleEngineExecutor = RuleEngine <Product> .GetInstance(new Product());

            ruleEngineExecutor.AddRules(new ProductNestedRule());
            var ruleResults      = ruleEngineExecutor.Execute();
            var nestedRuleResult = ruleResults.FindRuleResult <ProductNestedRuleC>();

            Assert.IsNotNull(nestedRuleResult);
            Assert.AreEqual("ProductNestedRuleC", nestedRuleResult.Name);
        }
Example #21
0
        public void TestTryAddTryGetValue()
        {
            var ruleEngineExecutor = RuleEngine <Product> .GetInstance(new Product());

            ruleEngineExecutor.AddRules(new ProductTryAdd(), new ProductTryGetValue());
            var ruleResults = ruleEngineExecutor.Execute().FindRuleResult("ProductRule").Result.To <List <string> >();

            Assert.Equal("Product Description1", ruleResults[0]);
            Assert.Equal("Product Description2", ruleResults[1]);
            Assert.Equal("Product Description3", ruleResults[2]);
            Assert.Equal("Product Description4", ruleResults[3]);
        }
Example #22
0
        public void TestBeforeInvokeAsync()
        {
            var ruleEngineExecutor = RuleEngine <Product> .GetInstance(new Product());

            ruleEngineExecutor.AddRules(new ProductRuleAsync());
            var ruleResults = ruleEngineExecutor.ExecuteAsync().Result;

            object value;

            ruleResults.First().Data.TryGetValue("Description", out value);
            Assert.AreEqual("Description", value);
        }
Example #23
0
        public void TestBeforeInvoke()
        {
            var ruleEngineExecutor = RuleEngine <Product> .GetInstance(new Product());

            var ruleResults = ruleEngineExecutor.ApplyRules(new ProductRule())
                              .Execute();

            object value;

            ruleResults.FindRuleResult("ProductRule").Data.TryGetValue("Key", out value);
            Assert.Equal("Value", value);
        }
        public void TestNestedParallelRules()
        {
            var product        = new Product();
            var engineExecutor = RuleEngine <Product> .GetInstance(product);

            var ruleEngineExecutor = engineExecutor;

            ruleEngineExecutor.AddRules(
                new ProductNestedParallelUpdateA(),
                new ProductNestedParallelUpdateB(),
                new ProductNestedParallelUpdateC());

            var ruleResults = ruleEngineExecutor.ExecuteAsync().Result;

            Assert.Equal(8, ruleResults.Count());
        }
Example #25
0
        public void TestNestedRuleInheritsConstraint()
        {
            var ruleEngineExecutor = RuleEngine <Product> .GetInstance(new Product { Price = 49.99m });

            ruleEngineExecutor.AddRules(new ProductNestedRule
            {
                Configuration = new Configuration <Product>
                {
                    Constraint = product => product.Price > 50,
                    NestedRulesInheritConstraint = true
                }
            });

            var ruleResults = ruleEngineExecutor.Execute();

            Assert.AreEqual(0, ruleResults.Length);
        }
        public void TestParallelRules()
        {
            var product        = new Product();
            var engineExecutor = RuleEngine <Product> .GetInstance(product);

            var ruleEngineExecutor = engineExecutor;

            ruleEngineExecutor.AddRules(
                new ProductParallelUpdateNameRuleAsync(),
                new ProductParallelUpdateDescriptionRuleAsync(),
                new ProductParallelUpdatePriceRuleAsync());

            var ruleResults = ruleEngineExecutor.ExecuteAsync().Result;

            Assert.NotNull(ruleResults);
            Assert.Equal("Product", product.Name);
            Assert.Equal(0.0m, product.Price);
            Assert.Equal("Description", product.Description);
        }