Exemple #1
0
        public void GetContext_PrimitiveInputs_ReturnsResult()
        {
            // Arrange
            var fixture    = new Fixture();
            var intInput   = fixture.Create <int>();
            var strInput   = fixture.Create <string>();
            var floatInput = fixture.Create <float>();

            var context = new Dictionary <string, object> {
                { nameof(intInput), intInput },
                { nameof(strInput), strInput },
                { nameof(floatInput), floatInput },
            };
            var parentRuleResult = new RuleResultTree();

            var actionContext = new ActionContext(context, parentRuleResult);

            // Act
            var intResult   = actionContext.GetContext <int>(nameof(intInput));
            var strResult   = actionContext.GetContext <string>(nameof(strInput));
            var floatResult = actionContext.GetContext <float>(nameof(floatInput));

            // Assert
            Assert.Equal(intInput, intResult);
            Assert.Equal(strInput, strResult);
            Assert.Equal(floatInput, floatResult);
        }
Exemple #2
0
        public ActionContext(IDictionary <string, object> context, RuleResultTree parentResult)
        {
            _context = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);
            foreach (var kv in context)
            {
                string key   = kv.Key;
                string value = kv.Value is string?kv.Value.ToString() : JsonConvert.SerializeObject(kv.Value);

                _context.Add(key, value);
            }
            _parentResult = parentResult;
        }
Exemple #3
0
        public void GetContext_InvalidTypeConversion_ThrowsArgumentException()
        {
            // Arrange
            var fixture      = new Fixture();
            var contextInput = fixture.CreateMany <string>();
            var context      = new Dictionary <string, object> {
                { nameof(contextInput), contextInput }
            };
            var parentRuleResult = new RuleResultTree();

            var    actionContext = new ActionContext(context, parentRuleResult);
            string name          = nameof(contextInput);

            // Act
            Assert.Throws <ArgumentException>(() => actionContext.GetContext <RuleResultTree>(name));
        }
Exemple #4
0
        public void GetContext_ValidNameWithStringCaseDiffernce_ReturnsContext()
        {
            // Arrange
            var fixture      = new Fixture();
            var contextInput = fixture.Create <string>();
            var context      = new Dictionary <string, object> {
                { nameof(contextInput), contextInput }
            };
            var parentRuleResult = new RuleResultTree();

            var    actionContext = new ActionContext(context, parentRuleResult);
            string name          = nameof(contextInput).ToUpper();

            // Act
            var result = actionContext.GetContext <string>(name);

            // Assert
            Assert.Equal(contextInput, result);
        }
Exemple #5
0
        public void GetParentRuleResult_ReturnsParentRule()
        {
            // Arrange
            var fixture      = new Fixture();
            var contextInput = fixture.Create <string>();
            var context      = new Dictionary <string, object> {
                { nameof(contextInput), contextInput }
            };
            var parentRuleResult = new RuleResultTree();

            var actionContext = new ActionContext(context, parentRuleResult);

            // Act
            var result = actionContext.GetParentRuleResult();

            // Assert
            Assert.NotNull(result);
            Assert.Equal(parentRuleResult, result);
        }
Exemple #6
0
        public void GetContext_ObjectContext_ReturnsTypedContext()
        {
            // Arrange
            var fixture      = new Fixture();
            var contextInput = fixture.CreateMany <string>();
            var context      = new Dictionary <string, object> {
                { nameof(contextInput), contextInput }
            };
            var parentRuleResult = new RuleResultTree();


            var    actionContext = new ActionContext(context, parentRuleResult);
            string name          = nameof(contextInput);

            // Act
            var result = actionContext.GetContext <List <string> >(name);

            // Assert
            Assert.Equal(contextInput, result);
        }
Exemple #7
0
        private static void CheckInputs(IEnumerable <string> expectedInputs, RuleResultTree resultTree)
        {
            Assert.All(expectedInputs, input => Assert.True(resultTree.Inputs.ContainsKey(input)));

            var localParamNames = resultTree.Rule.LocalParams?.Select(c => c.Name) ?? new List <string>();

            Assert.All(localParamNames, input => Assert.True(resultTree.Inputs.ContainsKey(input)));

#pragma warning disable CS0618 // Type or member is obsolete
            Assert.All(localParamNames, lp => Assert.Contains(resultTree.RuleEvaluatedParams, c => c.Name == lp));
#pragma warning restore CS0618 // Type or member is obsolete

            if (resultTree.ChildResults?.Any() == true)
            {
                foreach (var childResultTree in resultTree.ChildResults)
                {
                    CheckInputs(expectedInputs.Concat(localParamNames), childResultTree);
                }
            }
        }
Exemple #8
0
        private async ValueTask <ActionRuleResult> ExecuteActionForRuleResult(RuleResultTree resultTree, bool includeRuleResults = false)
        {
            var triggerType = resultTree?.IsSuccess == true ? ActionTriggerType.onSuccess : ActionTriggerType.onFailure;

            if (resultTree?.Rule?.Actions != null && resultTree.Rule.Actions.ContainsKey(triggerType))
            {
                var actionInfo     = resultTree.Rule.Actions[triggerType];
                var action         = _actionFactory.Get(actionInfo.Name);
                var ruleParameters = resultTree.Inputs.Select(kv => new RuleParameter(kv.Key, kv.Value)).ToArray();
                return(await action.ExecuteAndReturnResultAsync(new ActionContext(actionInfo.Context, resultTree), ruleParameters, includeRuleResults));
            }
            else
            {
                //If there is no action,return output as null and return the result for rule
                return(new ActionRuleResult {
                    Output = null,
                    Results = includeRuleResults ? new List <RuleResultTree>()
                    {
                        resultTree
                    } : null
                });
            }
        }
Exemple #9
0
        internal static void ToResultTreeMessages(RuleResultTree ruleResultTree, ref RuleResultMessage ruleResultMessage)
        {
            if (ruleResultTree.ChildResults != null)
            {
                GetChildRuleMessages(ruleResultTree.ChildResults, ref ruleResultMessage);
            }
            else
            {
                if (!ruleResultTree.IsSuccess)
                {
                    string errMsg = ruleResultTree.Rule.ErrorMessage;
                    errMsg = string.IsNullOrEmpty(errMsg) ? $"Error message is not configured for {ruleResultTree.Rule.RuleName}" : errMsg;

                    if (ruleResultTree.Rule.ErrorType == ErrorType.Error && !ruleResultMessage.ErrorMessages.Contains(errMsg))
                    {
                        ruleResultMessage.ErrorMessages.Add(errMsg);
                    }
                    else if (ruleResultTree.Rule.ErrorType == ErrorType.Warning && !ruleResultMessage.WarningMessages.Contains(errMsg))
                    {
                        ruleResultMessage.WarningMessages.Add(errMsg);
                    }
                }
            }
        }