Пример #1
0
        public void AtBottom_NoConditions_ReturnsNewObjectSettedAtBottomPriority()
        {
            // Arrange
            PriorityOptions priorityOption = PriorityOptions.AtBottom;

            // Act
            RuleAddPriorityOption actual = RuleAddPriorityOption.AtBottom;

            // Assert

            actual.Should().NotBeNull();
            actual.AtPriorityNumberOptionValue.Should().Be(0);
            actual.AtRuleNameOptionValue.Should().BeNull();
            actual.PriorityOption.Should().Be(priorityOption);
        }
Пример #2
0
        public void ByRuleName_GivenRuleNameSample_ReturnsNewObjectSettedByRuleNamePriorityAndAtNameSample()
        {
            // Arrange
            string          ruleName       = "Sample";
            PriorityOptions priorityOption = PriorityOptions.AtRuleName;

            // Act
            RuleAddPriorityOption actual = RuleAddPriorityOption.ByRuleName(ruleName);

            // Assert

            actual.Should().NotBeNull();
            actual.AtPriorityNumberOptionValue.Should().Be(0);
            actual.AtRuleNameOptionValue.Should().Be(ruleName);
            actual.PriorityOption.Should().Be(priorityOption);
        }
Пример #3
0
        public void ByPriorityNumber_GivenPriority1_ReturnsNewObjectSettedByPriorityNumberAndAtPriority1()
        {
            // Arrange
            int             priorityNumber = 1;
            PriorityOptions priorityOption = PriorityOptions.AtPriorityNumber;

            // Act
            RuleAddPriorityOption actual = RuleAddPriorityOption.ByPriorityNumber(priorityNumber);

            // Assert

            actual.Should().NotBeNull();
            actual.AtPriorityNumberOptionValue.Should().Be(priorityNumber);
            actual.AtRuleNameOptionValue.Should().BeNull();
            actual.PriorityOption.Should().Be(priorityOption);
        }
        public async Task AddRule_AddingNewRuleWithAgeConditionOnTop_NewRuleIsInsertedAndExistentRulePriorityUpdated()
        {
            // Arrange
            IRulesDataSource <ContentTypes, ConditionTypes> rulesDataSource = await RulesFromJsonFile.Load
                                                                              .FromJsonFileAsync <ContentTypes, ConditionTypes>(DataSourceFilePath);

            RulesEngine <ContentTypes, ConditionTypes> rulesEngine = RulesEngineBuilder.CreateRulesEngine()
                                                                     .WithContentType <ContentTypes>()
                                                                     .WithConditionType <ConditionTypes>()
                                                                     .SetDataSource(rulesDataSource)
                                                                     .Build();

            RuleBuilderResult <ContentTypes, ConditionTypes> newRuleResult = RuleBuilder.NewRule <ContentTypes, ConditionTypes>()
                                                                             .WithName("Body Mass Index up to 18 years formula")
                                                                             .WithDateBegin(DateTime.Parse("2018-01-01"))
                                                                             .WithContentContainer(new ContentContainer <ContentTypes>(ContentTypes.BodyMassIndexFormula, (t) => new Formula
            {
                Description = "Body Mass Index up to 18 years formula",
                Value       = "weight / ((height + 1) ^ 2)" // Not real, for the sake of the test.
            }))
                                                                             .WithCondition(cnb => cnb
                                                                                            .AsValued(ConditionTypes.Age)
                                                                                            .OfDataType <int>()
                                                                                            .WithComparisonOperator(Operators.LesserThanOrEqual)
                                                                                            .SetOperand(18)
                                                                                            .Build())
                                                                             .Build();

            Rule <ContentTypes, ConditionTypes> newRule = newRuleResult.Rule;
            RuleAddPriorityOption ruleAddPriorityOption = new RuleAddPriorityOption
            {
                PriorityOption = PriorityOptions.AtTop
            };

            // Act
            RuleOperationResult ruleOperationResult = await rulesEngine.AddRuleAsync(newRule, ruleAddPriorityOption).ConfigureAwait(false);

            // Assert
            ruleOperationResult.Should().NotBeNull();
            ruleOperationResult.IsSuccess.Should().BeTrue();

            IEnumerable <Rule <ContentTypes, ConditionTypes> > rules = await rulesDataSource.GetRulesByAsync(new RulesFilterArgs <ContentTypes>()).ConfigureAwait(false);

            rules.Should().NotBeNull().And.HaveCount(2);
            rules.Should().ContainEquivalentOf(newRule);
            newRule.Priority.Should().Be(1, "rule should to priority 1 if inserted at top.");
        }
        public async Task AddRule_AddingNewRuleWithAgeConditionAtPriority1AndNewRuleAtPriority3_NewRuleIsInsertedAndExistentRulePriorityUpdatedAndNewRuleInsertedAfter()
        {
            // Arrange
            IRulesDataSource <ContentTypes, ConditionTypes> rulesDataSource = await RulesFromJsonFile.Load
                                                                              .FromJsonFileAsync <ContentTypes, ConditionTypes>(DataSourceFilePath);

            RulesEngine <ContentTypes, ConditionTypes> rulesEngine = RulesEngineBuilder.CreateRulesEngine()
                                                                     .WithContentType <ContentTypes>()
                                                                     .WithConditionType <ConditionTypes>()
                                                                     .SetDataSource(rulesDataSource)
                                                                     .Build();

            RuleBuilderResult <ContentTypes, ConditionTypes> newRuleResult1 = RuleBuilder.NewRule <ContentTypes, ConditionTypes>()
                                                                              .WithName("Body Mass Index up to 18 years formula")
                                                                              .WithDateBegin(DateTime.Parse("2018-01-01"))
                                                                              .WithContentContainer(new ContentContainer <ContentTypes>(ContentTypes.BodyMassIndexFormula, (t) => new Formula
            {
                Description = "Body Mass Index up to 18 years formula",
                Value       = "weight / ((height + 1) ^ 2)" // Not real, for the sake of the test.
            }))
                                                                              .WithCondition(cnb => cnb
                                                                                             .AsValued(ConditionTypes.Age)
                                                                                             .OfDataType <int>()
                                                                                             .WithComparisonOperator(Operators.LesserThanOrEqual)
                                                                                             .SetOperand(18)
                                                                                             .Build())
                                                                              .Build();

            Rule <ContentTypes, ConditionTypes> newRule1 = newRuleResult1.Rule;
            RuleAddPriorityOption ruleAddPriorityOption1 = RuleAddPriorityOption.ByPriorityNumber(1);

            RuleBuilderResult <ContentTypes, ConditionTypes> ruleBuilderResult2 = RuleBuilder.NewRule <ContentTypes, ConditionTypes>()
                                                                                  .WithName("Sample rule")
                                                                                  .WithDateBegin(DateTime.Parse("2021-01-01"))
                                                                                  .WithContentContainer(new ContentContainer <ContentTypes>(ContentTypes.BodyMassIndexFormula, (t) => new Formula
            {
                Description = "Sample formula",
                Value       = "0"
            }))
                                                                                  .Build();

            Rule <ContentTypes, ConditionTypes> newRule2 = ruleBuilderResult2.Rule;
            RuleAddPriorityOption ruleAddPriorityOption2 = RuleAddPriorityOption.ByPriorityNumber(4);

            // Act
            RuleOperationResult ruleOperationResult1 = await rulesEngine.AddRuleAsync(newRule1, ruleAddPriorityOption1).ConfigureAwait(false);

            RuleOperationResult ruleOperationResult2 = await rulesEngine.AddRuleAsync(newRule2, ruleAddPriorityOption2).ConfigureAwait(false);

            // Assert
            ruleOperationResult1.Should().NotBeNull();
            ruleOperationResult1.IsSuccess.Should().BeTrue();

            ruleOperationResult2.Should().NotBeNull();
            ruleOperationResult2.IsSuccess.Should().BeTrue();

            IEnumerable <Rule <ContentTypes, ConditionTypes> > rules = await rulesDataSource.GetRulesByAsync(new RulesFilterArgs <ContentTypes>()).ConfigureAwait(false);

            rules.Should().NotBeNull().And.HaveCount(3);
            rules.Should().ContainEquivalentOf(newRule1);
            newRule1.Priority.Should().Be(1, "rule should to priority 1 if inserted at priority 1");
            newRule2.Priority.Should().Be(3, "rule should have priority 3 if inserted at priority 3, given that last rule after insert was at priority 2.");
        }