Ejemplo n.º 1
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_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.");
        }