Nested rules null operator check (#230)

* conventional progress

* Revert "conventional progress"

This reverts commit 9b404416b9.

* Check for issue on Rules field with missing (null) operator

* Rule Validator cleanup + Rule Validation Tests

Co-authored-by: Alex Reich <Alex_Reich@mechanicsbank.com>
pull/238/head
Alex Reich 2021-09-06 20:58:23 -07:00 committed by GitHub
parent 548faba2a8
commit 7b089a8260
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 100 additions and 1 deletions

View File

@ -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";

View File

@ -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);
});
}

View File

@ -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<Task> action = () => {
new RulesEngine(workflow, reSettings: reSettings);
return Task.CompletedTask;
};
Exception ex = await Assert.ThrowsAsync<Exceptions.RuleValidationException>(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<Task> action = () => {
new RulesEngine(workflow, reSettings: reSettings);
return Task.CompletedTask;
};
Exception ex = await Assert.ThrowsAsync<Exceptions.RuleValidationException>(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"
}
}
}
}
}
};
}
}
}