diff --git a/src/RulesEngine/HelperFunctions/Constants.cs b/src/RulesEngine/HelperFunctions/Constants.cs index 488602a..a6a3ae5 100644 --- a/src/RulesEngine/HelperFunctions/Constants.cs +++ b/src/RulesEngine/HelperFunctions/Constants.cs @@ -17,6 +17,7 @@ namespace RulesEngine.HelperFunctions public const string OPERATOR_NULL_ERRMSG = "Operator can not be null"; public const string OPERATOR_INCORRECT_ERRMSG = "Operator {PropertyValue} is not allowed"; public const string RULE_NAME_NULL_ERRMSG = "Rule Name can not be null"; + public const string OPERATOR_RULES_ERRMSG = "Cannot use Rules field when Operator is null"; public const string LAMBDA_EXPRESSION_EXPRESSION_NULL_ERRMSG = "Expression cannot be null or empty when RuleExpressionType is LambdaExpression"; public const string LAMBDA_EXPRESSION_OPERATOR_ERRMSG = "Cannot use Operator field when RuleExpressionType is LambdaExpression"; public const string LAMBDA_EXPRESSION_RULES_ERRMSG = "Cannot use Rules field when RuleExpressionType is LambdaExpression"; diff --git a/src/RulesEngine/Validators/RuleValidator.cs b/src/RulesEngine/Validators/RuleValidator.cs index c14156e..8ce4314 100644 --- a/src/RulesEngine/Validators/RuleValidator.cs +++ b/src/RulesEngine/Validators/RuleValidator.cs @@ -39,7 +39,7 @@ namespace RulesEngine.Validators { When(c => c.Operator == null && c.RuleExpressionType == RuleExpressionType.LambdaExpression, () => { RuleFor(c => c.Expression).NotEmpty().WithMessage(Constants.LAMBDA_EXPRESSION_EXPRESSION_NULL_ERRMSG); - RuleFor(c => c.Rules).Empty().WithMessage(Constants.LAMBDA_EXPRESSION_RULES_ERRMSG); + RuleFor(c => c.Rules).Empty().WithMessage(Constants.OPERATOR_RULES_ERRMSG); }); } diff --git a/test/RulesEngine.UnitTest/RuleValidationTest.cs b/test/RulesEngine.UnitTest/RuleValidationTest.cs new file mode 100644 index 0000000..d8dca9f --- /dev/null +++ b/test/RulesEngine.UnitTest/RuleValidationTest.cs @@ -0,0 +1,98 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +using Newtonsoft.Json; +using RulesEngine.HelperFunctions; +using RulesEngine.Models; +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Dynamic; +using System.Linq; +using System.Text.Json.Serialization; +using System.Threading.Tasks; +using Xunit; + +namespace RulesEngine.UnitTest +{ + [ExcludeFromCodeCoverage] + public class RuleValidationTest + { + [Fact] + public async Task NullExpressionithLambdaExpression_ReturnsExepectedResults() + { + var workflow = GetNullExpressionithLambdaExpressionWorkflow(); + var reSettings = new ReSettings { }; + RulesEngine rulesEngine = new RulesEngine(); + + Func action = () => { + new RulesEngine(workflow, reSettings: reSettings); + return Task.CompletedTask; + }; + + Exception ex = await Assert.ThrowsAsync(action); + + Assert.Contains(Constants.LAMBDA_EXPRESSION_EXPRESSION_NULL_ERRMSG, ex.Message); + + } + + [Fact] + public async Task NestedRulesWithMissingOperator_ReturnsExepectedResults() + { + var workflow = GetEmptyOperatorWorkflow(); + var reSettings = new ReSettings { }; + RulesEngine rulesEngine = new RulesEngine(); + + Func action = () => { + new RulesEngine(workflow, reSettings: reSettings); + return Task.CompletedTask; + }; + + Exception ex = await Assert.ThrowsAsync(action); + + Assert.Contains(Constants.OPERATOR_RULES_ERRMSG, ex.Message); + + } + + private Workflow[] GetNullExpressionithLambdaExpressionWorkflow() + { + return new[] { + new Workflow { + WorkflowName = "NestedRulesTest", + Rules = new Rule[] { + new Rule { + RuleName = "TestRule", + RuleExpressionType = RuleExpressionType.LambdaExpression, + } + } + } + }; + } + + private Workflow[] GetEmptyOperatorWorkflow() + { + return new[] { + new Workflow { + WorkflowName = "NestedRulesTest", + Rules = new Rule[] { + new Rule { + RuleName = "AndRuleTrueFalse", + Expression = "true == true", + Rules = new Rule[] { + new Rule{ + RuleName = "trueRule1", + Expression = "input1.TrueValue == true", + }, + new Rule { + RuleName = "falseRule1", + Expression = "input1.TrueValue == false" + } + + } + } + } + } + }; + } + } +}