コード例 #1
0
        public void ExecuteAllRulesAsyncShouldReturnResultsForAllRules([Frozen] IExeucutesSingleRule ruleExecutor,
                                                                       SerialRuleExecutor sut,
                                                                       [ExecutableModel] ExecutableRuleAndDependencies rule1,
                                                                       [ExecutableModel] ExecutableRuleAndDependencies rule2,
                                                                       [ExecutableModel] ExecutableRuleAndDependencies rule3,
                                                                       IRuleExecutionContext executionContext,
                                                                       [RuleResult] ValidationRuleResult result1,
                                                                       [RuleResult] ValidationRuleResult result2,
                                                                       [RuleResult] ValidationRuleResult result3)
        {
            var allRules = new[] { rule1, rule2, rule3 };
            var sequence = new MockSequence();

            Mock.Get(executionContext).InSequence(sequence).Setup(x => x.GetRulesWhichMayBeExecuted()).Returns(() => allRules.Select(r => r.ExecutableRule));
            Mock.Get(executionContext).InSequence(sequence).Setup(x => x.GetRulesWhichMayBeExecuted()).Returns(() => Enumerable.Empty <ExecutableRule>());
            Mock.Get(ruleExecutor)
            .Setup(x => x.ExecuteRuleAsync(rule1.ExecutableRule, It.IsAny <CancellationToken>()))
            .Returns(() => Task.FromResult(result1));
            Mock.Get(ruleExecutor)
            .Setup(x => x.ExecuteRuleAsync(rule2.ExecutableRule, It.IsAny <CancellationToken>()))
            .Returns(() => Task.FromResult(result2));
            Mock.Get(ruleExecutor)
            .Setup(x => x.ExecuteRuleAsync(rule3.ExecutableRule, It.IsAny <CancellationToken>()))
            .Returns(() => Task.FromResult(result3));

            Assert.That(async() => await sut.ExecuteAllRulesAsync(executionContext, default),
                        Is.EquivalentTo(new[] { result1, result2, result3 }));
        }
コード例 #2
0
        internal static async Task <ValidationRuleResult> ExecuteRuleAsync(ExecutableRule rule,
                                                                           IExeucutesSingleRule ruleExecutor,
                                                                           IRuleExecutionContext executionContext,
                                                                           CancellationToken cancellationToken)
        {
            var result = await ruleExecutor.ExecuteRuleAsync(rule, cancellationToken).ConfigureAwait(false);

            rule.Result = result;
            executionContext.HandleValidationRuleResult(rule);
            return(result);
        }
コード例 #3
0
        public async Task ExecuteAllRulesAsyncShouldUpdateTheResultUponTheRule([Frozen] IExeucutesSingleRule ruleExecutor,
                                                                               SerialRuleExecutor sut,
                                                                               [ExecutableModel] ExecutableRuleAndDependencies rule,
                                                                               IRuleExecutionContext executionContext,
                                                                               [RuleResult] ValidationRuleResult result)
        {
            var allRules = new[] { rule };
            var sequence = new MockSequence();

            Mock.Get(executionContext).InSequence(sequence).Setup(x => x.GetRulesWhichMayBeExecuted()).Returns(() => allRules.Select(r => r.ExecutableRule));
            Mock.Get(executionContext).InSequence(sequence).Setup(x => x.GetRulesWhichMayBeExecuted()).Returns(() => Enumerable.Empty <ExecutableRule>());
            Mock.Get(ruleExecutor)
            .Setup(x => x.ExecuteRuleAsync(rule.ExecutableRule, It.IsAny <CancellationToken>()))
            .Returns(() => Task.FromResult(result));

            await sut.ExecuteAllRulesAsync(executionContext);

            Assert.That(rule.ExecutableRule.Result, Is.SameAs(result));
        }
コード例 #4
0
        internal static async Task <IEnumerable <ValidationRuleResult> > ExecuteRulesAsync(IEnumerable <ExecutableRule> availableRules,
                                                                                           IExeucutesSingleRule ruleExecutor,
                                                                                           IRuleExecutionContext executionContext,
                                                                                           CancellationToken cancellationToken)
        {
            var results = new List <ValidationRuleResult>();

            foreach (var rule in availableRules)
            {
                var result = await ExecuteRuleAsync(rule, ruleExecutor, executionContext, cancellationToken).ConfigureAwait(false);

                results.Add(result);
            }

            return(results);
        }
コード例 #5
0
 /// <summary>
 /// Initialises a new instance of <see cref="SerialRuleExecutor"/>.
 /// </summary>
 /// <param name="ruleExecutor">A service which executes rules.</param>
 /// <exception cref="ArgumentNullException">If <paramref name="ruleExecutor"/> is <see langword="null" />.</exception>
 public SerialRuleExecutor(IExeucutesSingleRule ruleExecutor)
 {
     this.ruleExecutor = ruleExecutor ?? throw new ArgumentNullException(nameof(ruleExecutor));
 }
コード例 #6
0
 /// <summary>
 /// Initialises a new instance of <see cref="ParallelRuleExecutor"/>.
 /// </summary>
 /// <param name="ruleExecutor">A service which executes rules.</param>
 /// <exception cref="ArgumentNullException">If <paramref name="ruleExecutor"/> is <see langword="null" />.</exception>
 public ParallelRuleExecutor(IExeucutesSingleRule ruleExecutor)
 {
     this.ruleExecutor = ruleExecutor ?? throw new System.ArgumentNullException(nameof(ruleExecutor));
 }
コード例 #7
0
        public void ExecuteAllRulesAsyncShouldNotExecuteDependantRulesBeforeTheirDependenciesHaveResults([Frozen] IExeucutesSingleRule ruleExecutor,
                                                                                                         SerialRuleExecutor sut,
                                                                                                         [ExecutableModel] ExecutableRuleAndDependencies rule1,
                                                                                                         [ExecutableModel] ExecutableRuleAndDependencies rule2,
                                                                                                         [ExecutableModel] ExecutableRuleAndDependencies rule3,
                                                                                                         IRuleExecutionContext executionContext,
                                                                                                         [RuleResult] ValidationRuleResult result1,
                                                                                                         [RuleResult] ValidationRuleResult result2,
                                                                                                         [RuleResult] ValidationRuleResult result3)
        {
            var allRules = new[] { rule3, rule2, rule1 };

            Mock.Get(executionContext)
            .Setup(x => x.GetRulesWhichMayBeExecuted())
            .Returns(() =>
            {
                if (rule1.ExecutableRule.Result is null)
                {
                    return new[] { rule1.ExecutableRule }
                }
                ;
                if (rule2.ExecutableRule.Result is null)
                {
                    return new[] { rule2.ExecutableRule }
                }
                ;
                if (rule3.ExecutableRule.Result is null)
                {
                    return new[] { rule3.ExecutableRule }
                }
                ;
                return(Enumerable.Empty <ExecutableRule>());
            });
            Mock.Get(ruleExecutor)
            .Setup(x => x.ExecuteRuleAsync(rule1.ExecutableRule, It.IsAny <CancellationToken>()))
            .Returns(() => Task.FromResult(result1));
            Mock.Get(ruleExecutor)
            .Setup(x => x.ExecuteRuleAsync(rule2.ExecutableRule, It.IsAny <CancellationToken>()))
            .Callback(() =>
            {
                if (rule1.ExecutableRule.Result is null)
                {
                    Assert.Fail("Rule 2 should not be executed before rule 1");
                }
            })
            .Returns(() => Task.FromResult(result2));
            Mock.Get(ruleExecutor)
            .Setup(x => x.ExecuteRuleAsync(rule3.ExecutableRule, It.IsAny <CancellationToken>()))
            .Callback(() =>
            {
                if (rule1.ExecutableRule.Result is null || rule2.ExecutableRule.Result is null)
                {
                    Assert.Fail("Rule 3 should not be executed before rules 1 or 2");
                }
            })
            .Returns(() => Task.FromResult(result3));

            Assert.That(async() => await sut.ExecuteAllRulesAsync(executionContext, default),
                        Is.EquivalentTo(new[] { result1, result2, result3 }));
        }