public async Task ClearWorkFlow_ShouldRemoveAllCompiledCache() { var workflow = new WorkflowRules { WorkflowName = "Test", Rules = new Rule[] { new Rule { RuleName = "RuleWithLocalParam", LocalParams = new LocalParam[] { new LocalParam { Name = "lp1", Expression = "true" } }, RuleExpressionType = RuleExpressionType.LambdaExpression, Expression = "lp1 == true" } } }; var re = new RulesEngine(); re.AddWorkflow(workflow); var result1 = await re.ExecuteAllRulesAsync("Test", "hello"); Assert.True(result1.All(c => c.IsSuccess)); re.ClearWorkflows(); workflow.Rules.First().LocalParams.First().Expression = "false"; re.AddWorkflow(workflow); var result2 = await re.ExecuteAllRulesAsync("Test", "hello"); Assert.True(result2.All(c => c.IsSuccess == false)); }
public async Task WorkflowUpdate_GlobalParam_ShouldReflect(string workflowName) { var workflows = GetWorkflowRulesList(); var engine = new RulesEngine(null, null); engine.AddWorkflow(workflows); var input1 = new { trueValue = true, falseValue = false }; var result = await engine.ExecuteAllRulesAsync(workflowName, input1); Assert.True(result.All(c => c.IsSuccess)); var workflowToUpdate = workflows.Single(c => c.WorkflowName == workflowName); engine.RemoveWorkflow(workflowName); workflowToUpdate.GlobalParams.First().Expression = "true == false"; engine.AddWorkflow(workflowToUpdate); var result2 = await engine.ExecuteAllRulesAsync(workflowName, input1); Assert.True(result2.All(c => c.IsSuccess == false)); }
public async Task WorkflowUpdatedRuleEnabled_ShouldReflect(string workflowName, bool[] expectedRuleResults) { var workflow = GetWorkflows().Single(c => c.WorkflowName == workflowName); var rulesEngine = new RulesEngine(); rulesEngine.AddWorkflow(workflow); var input1 = new { TrueValue = true }; var result = await rulesEngine.ExecuteAllRulesAsync(workflowName, input1); Assert.NotNull(result); Assert.True(NestedEnabledCheck(result)); Assert.Equal(expectedRuleResults.Length, result.Count); for (var i = 0; i < expectedRuleResults.Length; i++) { Assert.Equal(expectedRuleResults[i], result[i].IsSuccess); } rulesEngine.RemoveWorkflow(workflowName); var firstRule = workflow.Rules.First(); firstRule.Enabled = false; rulesEngine.AddWorkflow(workflow); var expectedLength = workflow.Rules.Count(c => c.Enabled); var result2 = await rulesEngine.ExecuteAllRulesAsync(workflowName, input1); Assert.Equal(expectedLength, result2.Count); Assert.DoesNotContain(result2, c => c.Rule.RuleName == firstRule.RuleName); }
public async Task DisabledScopedParam_ShouldReflect(string workflowName, bool[] outputs) { var workflows = GetWorkflowRulesList(); var engine = new RulesEngine(new string[] { }, null, new ReSettings { EnableScopedParams = false }); engine.AddWorkflow(workflows); var input1 = new { trueValue = true, falseValue = false }; var result = await engine.ExecuteAllRulesAsync(workflowName, input1); for (var i = 0; i < result.Count; i++) { Assert.Equal(result[i].IsSuccess, outputs[i]); if (result[i].IsSuccess == false) { Assert.StartsWith("Exception while parsing expression", result[i].ExceptionMessage); } } }
public async Task ErrorInScopedParam_ShouldAppearAsErrorMessage(string workflowName) { var workflows = GetWorkflowRulesList(); var engine = new RulesEngine(new string[] { }, null); engine.AddWorkflow(workflows); var input = new { }; var result = await engine.ExecuteAllRulesAsync(workflowName, input); Assert.All(result, c => { Assert.False(c.IsSuccess); Assert.StartsWith("Error while compiling rule", c.ExceptionMessage); }); }
public async Task BasicWorkflowRules_ReturnsTrue(string workflowName) { var workflows = GetWorkflowRulesList(); var engine = new RulesEngine(null, null); engine.AddWorkflow(workflows); var input1 = new { trueValue = true, falseValue = false }; var result = await engine.ExecuteAllRulesAsync(workflowName, input1); Assert.True(result.All(c => c.IsSuccess)); CheckResultTreeContainsAllInputs(workflowName, result); }
public async Task ExecuteRule_WithNullInput_ShouldNotThrowException() { var workflow = new WorkflowRules { WorkflowName = "Test", Rules = new Rule[] { new Rule { RuleName = "RuleWithLocalParam", RuleExpressionType = RuleExpressionType.LambdaExpression, Expression = "input1 == null || input1.hello.world = \"wow\"" } } }; var re = new RulesEngine(); re.AddWorkflow(workflow); var result1 = await re.ExecuteAllRulesAsync("Test", new RuleParameter("input1", value : null)); Assert.True(result1.All(c => c.IsSuccess)); var result2 = await re.ExecuteAllRulesAsync("Test", new object[] { null }); Assert.True(result2.All(c => c.IsSuccess)); dynamic input1 = new ExpandoObject(); input1.hello = new ExpandoObject(); input1.hello.world = "wow"; List <RuleResultTree> result3 = await re.ExecuteAllRulesAsync("Test", input1); Assert.True(result3.All(c => c.IsSuccess)); }