public async Task BodyMassIndex_NoConditions_ReturnsDefaultFormula()
        {
            // Arrange
            string             expectedFormulaDescription = "Body Mass Index default formula";
            string             expectedFormulaValue       = "weight / (height ^ 2)";
            const ContentTypes expectedContent            = ContentTypes.BodyMassIndexFormula;
            DateTime           expectedMatchDate          = new DateTime(2018, 06, 01);

            Condition <ConditionTypes>[] expectedConditions = new Condition <ConditionTypes> [0];

            IRulesDataSource <ContentTypes, ConditionTypes> rulesDataSource = await RulesFromJsonFile.Load
                                                                              .FromJsonFileAsync <ContentTypes, ConditionTypes>(DataSourceFilePath);

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

            // Act
            Rule <ContentTypes, ConditionTypes> actual = await rulesEngine.MatchOneAsync(expectedContent, expectedMatchDate, expectedConditions);

            // Assert
            actual.Should().NotBeNull();
            Formula actualFormula = actual.ContentContainer.GetContentAs <Formula>();

            actualFormula.Description.Should().Be(expectedFormulaDescription);
            actualFormula.Value.Should().Be(expectedFormulaValue);
        }
        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 GetCarInsuranceAdvice_SearchForRulesExcludingRulesWithoutSearchConditions_ReturnsNoRules()
        {
            // Arrange
            const ContentTypes expectedContent   = ContentTypes.CarInsuranceAdvice;
            DateTime           expectedMatchDate = new DateTime(2018, 06, 01);
            SearchArgs <ContentTypes, ConditionTypes> searchArgs = new SearchArgs <ContentTypes, ConditionTypes>
            {
                Conditions = new Condition <ConditionTypes>[]
                {
                    new Condition <ConditionTypes>
                    {
                        Type  = ConditionTypes.RepairCosts,
                        Value = 800.00000m
                    },
                    new Condition <ConditionTypes>
                    {
                        Type  = ConditionTypes.RepairCostsCommercialValueRate,
                        Value = 86.33m
                    }
                },
                ContentType = expectedContent,
                DateBegin   = expectedMatchDate,
                DateEnd     = expectedMatchDate,
                ExcludeRulesWithoutSearchConditions = true
            };

            IRulesDataSource <ContentTypes, ConditionTypes> rulesDataSource = await RulesFromJsonFile.Load
                                                                              .FromJsonFileAsync <ContentTypes, ConditionTypes>(DataSourceFilePath, serializedContent : false);

            RulesEngine <ContentTypes, ConditionTypes> rulesEngine = RulesEngineBuilder.CreateRulesEngine()
                                                                     .WithContentType <ContentTypes>()
                                                                     .WithConditionType <ConditionTypes>()
                                                                     .SetDataSource(rulesDataSource)
                                                                     .Configure(reo =>
            {
                reo.PriotityCriteria = PriorityCriterias.BottommostRuleWins;
            })
                                                                     .Build();

            // Act
            IEnumerable <Rule <ContentTypes, ConditionTypes> > actual = await rulesEngine.SearchAsync(searchArgs);

            // Assert
            actual.Should().NotBeNull();
            actual.Should().HaveCount(0);
        }
Exemplo n.º 4
0
        public async Task GetCarInsuranceAdvice_RepairCostsNotWorthIt_ReturnsRefusePaymentPerFranchise()
        {
            // Arrange
            CarInsuranceAdvices expected          = CarInsuranceAdvices.RefusePaymentPerFranchise;
            const ContentTypes  expectedContent   = ContentTypes.CarInsuranceAdvice;
            DateTime            expectedMatchDate = new DateTime(2018, 06, 01);

            Condition <ConditionTypes>[] expectedConditions = new Condition <ConditionTypes>[]
            {
                new Condition <ConditionTypes>
                {
                    Type  = ConditionTypes.RepairCosts,
                    Value = 800.00000m
                },
                new Condition <ConditionTypes>
                {
                    Type  = ConditionTypes.RepairCostsCommercialValueRate,
                    Value = 23.45602m
                }
            };

            IServiceCollection serviceDescriptors = new ServiceCollection();

            serviceDescriptors.AddSingleton(this.inMemoryRulesStorage);
            IServiceProvider serviceProvider = serviceDescriptors.BuildServiceProvider();

            RulesEngine <ContentTypes, ConditionTypes> rulesEngine = RulesEngineBuilder.CreateRulesEngine()
                                                                     .WithContentType <ContentTypes>()
                                                                     .WithConditionType <ConditionTypes>()
                                                                     .SetInMemoryDataSource(serviceProvider)
                                                                     .Configure(reo =>
            {
                reo.PriotityCriteria = PriorityCriterias.BottommostRuleWins;
            })
                                                                     .Build();

            // Act
            Rule <ContentTypes, ConditionTypes> actual = await rulesEngine.MatchOneAsync(expectedContent, expectedMatchDate, expectedConditions);

            // Assert
            actual.Should().NotBeNull();
            CarInsuranceAdvices actualContent = actual.ContentContainer.GetContentAs <CarInsuranceAdvices>();

            actualContent.Should().Be(expected);
        }
        public async Task GetCarInsuranceAdvice_RepairCostsNotWorthIt_ReturnsRefusePaymentPerFranchise()
        {
            // Arrange
            CarInsuranceAdvices expected          = CarInsuranceAdvices.RefusePaymentPerFranchise;
            const ContentTypes  expectedContent   = ContentTypes.CarInsuranceAdvice;
            DateTime            expectedMatchDate = new DateTime(2018, 06, 01);

            Condition <ConditionTypes>[] expectedConditions = new Condition <ConditionTypes>[]
            {
                new Condition <ConditionTypes>
                {
                    Type  = ConditionTypes.RepairCosts,
                    Value = 800.00000m
                },
                new Condition <ConditionTypes>
                {
                    Type  = ConditionTypes.RepairCostsCommercialValueRate,
                    Value = 23.45602m
                }
            };

            IRulesDataSource <ContentTypes, ConditionTypes> rulesDataSource = await RulesFromJsonFile.Load
                                                                              .FromJsonFileAsync <ContentTypes, ConditionTypes>(DataSourceFilePath, serializedContent : false);

            RulesEngine <ContentTypes, ConditionTypes> rulesEngine = RulesEngineBuilder.CreateRulesEngine()
                                                                     .WithContentType <ContentTypes>()
                                                                     .WithConditionType <ConditionTypes>()
                                                                     .SetDataSource(rulesDataSource)
                                                                     .Configure(reo =>
            {
                reo.PriotityCriteria = PriorityCriterias.BottommostRuleWins;
            })
                                                                     .Build();

            // Act
            Rule <ContentTypes, ConditionTypes> actual = await rulesEngine.MatchOneAsync(expectedContent, expectedMatchDate, expectedConditions);

            // Assert
            actual.Should().NotBeNull();
            CarInsuranceAdvices actualContent = actual.ContentContainer.GetContentAs <CarInsuranceAdvices>();

            actualContent.Should().Be(expected);
        }
Exemplo n.º 6
0
        public async Task BuildingSecuritySystem_FireScenario_ReturnsActionsToTrigger()
        {
            // Assert
            const SecuritySystemActionables securitySystemActionable = SecuritySystemActionables.FireSystem;

            DateTime expectedMatchDate = new DateTime(2018, 06, 01);

            Condition <SecuritySystemConditions>[] expectedConditions = new Condition <SecuritySystemConditions>[]
            {
                new Condition <SecuritySystemConditions>
                {
                    Type  = SecuritySystemConditions.TemperatureCelsius,
                    Value = 100.0m
                },
                new Condition <SecuritySystemConditions>
                {
                    Type  = SecuritySystemConditions.SmokeRate,
                    Value = 55.0m
                },
                new Condition <SecuritySystemConditions>
                {
                    Type  = SecuritySystemConditions.PowerStatus,
                    Value = "Online"
                }
            };

            RulesEngine <SecuritySystemActionables, SecuritySystemConditions> rulesEngine = RulesEngineBuilder.CreateRulesEngine()
                                                                                            .WithContentType <SecuritySystemActionables>()
                                                                                            .WithConditionType <SecuritySystemConditions>()
                                                                                            .SetMongoDbDataSource(this.mongoClient, this.mongoDbProviderSettings)
                                                                                            .Build();

            // Act
            IEnumerable <Rule <SecuritySystemActionables, SecuritySystemConditions> > actual = await rulesEngine.MatchManyAsync(securitySystemActionable, expectedMatchDate, expectedConditions);

            // Assert
            actual.Should().NotBeNull();

            IEnumerable <SecuritySystemAction> securitySystemActions = actual.Select(r => r.ContentContainer.GetContentAs <SecuritySystemAction>()).ToList();

            securitySystemActions.Should().Contain(ssa => ssa.ActionName == "CallFireBrigade")
            .And.Contain(ssa => ssa.ActionName == "CallPolice")
            .And.Contain(ssa => ssa.ActionName == "ActivateSprinklers")
            .And.HaveCount(3);
        }
Exemplo n.º 7
0
        public async Task GetCarInsuranceAdvice_UpdatesRuleAndAddsNewOneAndEvaluates_ReturnsPay()
        {
            // Arrange
            const ContentTypes expectedContent   = ContentTypes.CarInsuranceAdvice;
            DateTime           expectedMatchDate = new DateTime(2018, 06, 01);

            Condition <ConditionTypes>[] expectedConditions = new Condition <ConditionTypes>[]
            {
                new Condition <ConditionTypes>
                {
                    Type  = ConditionTypes.RepairCosts,
                    Value = 800.00000m
                },
                new Condition <ConditionTypes>
                {
                    Type  = ConditionTypes.RepairCostsCommercialValueRate,
                    Value = 23.45602m
                }
            };

            RulesEngine <ContentTypes, ConditionTypes> rulesEngine = RulesEngineBuilder.CreateRulesEngine()
                                                                     .WithContentType <ContentTypes>()
                                                                     .WithConditionType <ConditionTypes>()
                                                                     .SetMongoDbDataSource(this.mongoClient, this.mongoDbProviderSettings)
                                                                     .Configure(reo =>
            {
                reo.PriotityCriteria = PriorityCriterias.BottommostRuleWins;
            })
                                                                     .Build();

            IRulesDataSource <ContentTypes, ConditionTypes> rulesDataSource = CreateRulesDataSourceTest <ContentTypes, ConditionTypes>(this.mongoClient, this.mongoDbProviderSettings);

            RuleBuilderResult <ContentTypes, ConditionTypes> ruleBuilderResult = RuleBuilder.NewRule <ContentTypes, ConditionTypes>()
                                                                                 .WithName("Car Insurance Advise on self damage coverage")
                                                                                 .WithDateBegin(DateTime.Parse("2018-01-01"))
                                                                                 .WithContentContainer(new ContentContainer <ContentTypes>(ContentTypes.CarInsuranceAdvice, (t) => CarInsuranceAdvices.Pay))
                                                                                 .Build();
            IEnumerable <Rule <ContentTypes, ConditionTypes> > existentRules1 = await rulesDataSource.GetRulesByAsync(new RulesFilterArgs <ContentTypes>
            {
                Name = "Car Insurance Advise on repair costs lower than franchise boundary"
            });

            Rule <ContentTypes, ConditionTypes> ruleToAdd     = ruleBuilderResult.Rule;
            Rule <ContentTypes, ConditionTypes> ruleToUpdate1 = existentRules1.FirstOrDefault();

            ruleToUpdate1.Priority = 2;

            // Act 1
            RuleOperationResult updateOperationResult1 = await rulesEngine.UpdateRuleAsync(ruleToUpdate1);

            Rule <ContentTypes, ConditionTypes> eval1 = await rulesEngine.MatchOneAsync(expectedContent, expectedMatchDate, expectedConditions);

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

            // Assert 1
            updateOperationResult1.Should().NotBeNull();
            updateOperationResult1.IsSuccess.Should().BeTrue();

            eval1.Priority.Should().Be(2);
            CarInsuranceAdvices content1 = eval1.ContentContainer.GetContentAs <CarInsuranceAdvices>();

            content1.Should().Be(CarInsuranceAdvices.RefusePaymentPerFranchise);

            Rule <ContentTypes, ConditionTypes> rule11 = rules1.FirstOrDefault(r => r.Name == "Car Insurance Advise on repair costs lesser than 80% of commercial value");

            rule11.Should().NotBeNull();
            rule11.Priority.Should().Be(1);
            Rule <ContentTypes, ConditionTypes> rule12 = rules1.FirstOrDefault(r => r.Name == "Car Insurance Advise on repair costs lower than franchise boundary");

            rule12.Should().NotBeNull();
            rule12.Priority.Should().Be(2);
            Rule <ContentTypes, ConditionTypes> rule13 = rules1.FirstOrDefault(r => r.Name == "Car Insurance Advise on repair costs greater than 80% of commercial value");

            rule13.Should().NotBeNull();
            rule13.Priority.Should().Be(3);

            // Act 2
            RuleOperationResult addOperationResult = await rulesEngine.AddRuleAsync(ruleToAdd, new RuleAddPriorityOption
            {
                PriorityOption        = PriorityOptions.AtRuleName,
                AtRuleNameOptionValue = "Car Insurance Advise on repair costs lower than franchise boundary"
            });

            Rule <ContentTypes, ConditionTypes> eval2 = await rulesEngine.MatchOneAsync(expectedContent, expectedMatchDate, expectedConditions);

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

            // Assert 2
            addOperationResult.Should().NotBeNull();
            addOperationResult.IsSuccess.Should().BeTrue();

            eval2.Priority.Should().Be(3);
            CarInsuranceAdvices content2 = eval2.ContentContainer.GetContentAs <CarInsuranceAdvices>();

            content2.Should().Be(CarInsuranceAdvices.RefusePaymentPerFranchise);

            Rule <ContentTypes, ConditionTypes> rule21 = rules2.FirstOrDefault(r => r.Name == "Car Insurance Advise on repair costs lesser than 80% of commercial value");

            rule21.Should().NotBeNull();
            rule21.Priority.Should().Be(1);
            Rule <ContentTypes, ConditionTypes> rule22 = rules2.FirstOrDefault(r => r.Name == "Car Insurance Advise on self damage coverage");

            rule22.Should().NotBeNull();
            rule22.Priority.Should().Be(2);
            Rule <ContentTypes, ConditionTypes> rule23 = rules2.FirstOrDefault(r => r.Name == "Car Insurance Advise on repair costs lower than franchise boundary");

            rule23.Should().NotBeNull();
            rule23.Priority.Should().Be(3);
            Rule <ContentTypes, ConditionTypes> rule24 = rules2.FirstOrDefault(r => r.Name == "Car Insurance Advise on repair costs greater than 80% of commercial value");

            rule24.Should().NotBeNull();
            rule24.Priority.Should().Be(4);

            // Act 3
            IEnumerable <Rule <ContentTypes, ConditionTypes> > existentRules2 = await rulesDataSource.GetRulesByAsync(new RulesFilterArgs <ContentTypes>
            {
                Name = "Car Insurance Advise on repair costs lower than franchise boundary"
            });

            Rule <ContentTypes, ConditionTypes> ruleToUpdate2 = existentRules2.FirstOrDefault();

            ruleToUpdate2.Priority = 4;

            RuleOperationResult updateOperationResult2 = await rulesEngine.UpdateRuleAsync(ruleToUpdate2);

            Rule <ContentTypes, ConditionTypes> eval3 = await rulesEngine.MatchOneAsync(expectedContent, expectedMatchDate, expectedConditions);

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

            // Assert 3
            updateOperationResult2.Should().NotBeNull();
            updateOperationResult2.IsSuccess.Should().BeTrue();

            eval3.Priority.Should().Be(4);
            CarInsuranceAdvices content3 = eval3.ContentContainer.GetContentAs <CarInsuranceAdvices>();

            content3.Should().Be(CarInsuranceAdvices.RefusePaymentPerFranchise);

            Rule <ContentTypes, ConditionTypes> rule31 = rules3.FirstOrDefault(r => r.Name == "Car Insurance Advise on repair costs lesser than 80% of commercial value");

            rule31.Should().NotBeNull();
            rule31.Priority.Should().Be(1);
            Rule <ContentTypes, ConditionTypes> rule32 = rules3.FirstOrDefault(r => r.Name == "Car Insurance Advise on self damage coverage");

            rule32.Should().NotBeNull();
            rule32.Priority.Should().Be(2);
            Rule <ContentTypes, ConditionTypes> rule33 = rules3.FirstOrDefault(r => r.Name == "Car Insurance Advise on repair costs greater than 80% of commercial value");

            rule33.Should().NotBeNull();
            rule33.Priority.Should().Be(3);
            Rule <ContentTypes, ConditionTypes> rule34 = rules3.FirstOrDefault(r => r.Name == "Car Insurance Advise on repair costs lower than franchise boundary");

            rule34.Should().NotBeNull();
            rule34.Priority.Should().Be(4);
        }
        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.");
        }
        public async Task BuildingSecuritySystem_PowerFailureScenario_ReturnsActionsToTrigger()
        {
            // Assert
            const SecuritySystemActionables securitySystemActionable = SecuritySystemActionables.PowerSystem;

            DateTime expectedMatchDate = new DateTime(2018, 06, 01);

            Condition <SecuritySystemConditions>[] expectedConditions = new Condition <SecuritySystemConditions>[]
            {
                new Condition <SecuritySystemConditions>
                {
                    Type  = SecuritySystemConditions.TemperatureCelsius,
                    Value = 100.0m
                },
                new Condition <SecuritySystemConditions>
                {
                    Type  = SecuritySystemConditions.SmokeRate,
                    Value = 55.0m
                },
                new Condition <SecuritySystemConditions>
                {
                    Type  = SecuritySystemConditions.PowerStatus,
                    Value = "Offline"
                }
            };

            IRulesDataSource <SecuritySystemActionables, SecuritySystemConditions> rulesDataSource = await RulesFromJsonFile.Load
                                                                                                     .FromJsonFileAsync <SecuritySystemActionables, SecuritySystemConditions>(DataSourceFilePath);

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

            // Act
            IEnumerable <Rule <SecuritySystemActionables, SecuritySystemConditions> > actual = await rulesEngine.MatchManyAsync(securitySystemActionable, expectedMatchDate, expectedConditions);

            // Assert
            actual.Should().NotBeNull();

            IEnumerable <SecuritySystemAction> securitySystemActions = actual.Select(r => r.ContentContainer.GetContentAs <SecuritySystemAction>()).ToList();

            securitySystemActions.Should().Contain(ssa => ssa.ActionName == "EnableEmergencyLights")
            .And.Contain(ssa => ssa.ActionName == "EnableEmergencyPower")
            .And.Contain(ssa => ssa.ActionName == "CallPowerGridPicket");
        }
Exemplo n.º 10
0
        public async Task BuildingSecuritySystem_PowerFailureScenario_ReturnsActionsToTrigger()
        {
            // Assert
            const SecuritySystemActionables securitySystemActionable = SecuritySystemActionables.PowerSystem;

            DateTime expectedMatchDate = new DateTime(2018, 06, 01);

            Condition <SecuritySystemConditions>[] expectedConditions = new Condition <SecuritySystemConditions>[]
            {
                new Condition <SecuritySystemConditions>
                {
                    Type  = SecuritySystemConditions.TemperatureCelsius,
                    Value = 100.0m
                },
                new Condition <SecuritySystemConditions>
                {
                    Type  = SecuritySystemConditions.SmokeRate,
                    Value = 55.0m
                },
                new Condition <SecuritySystemConditions>
                {
                    Type  = SecuritySystemConditions.PowerStatus,
                    Value = "Offline"
                }
            };

            IServiceCollection serviceDescriptors = new ServiceCollection();

            serviceDescriptors.AddSingleton(this.inMemoryRulesStorage);
            IServiceProvider serviceProvider = serviceDescriptors.BuildServiceProvider();

            RulesEngine <SecuritySystemActionables, SecuritySystemConditions> rulesEngine = RulesEngineBuilder.CreateRulesEngine()
                                                                                            .WithContentType <SecuritySystemActionables>()
                                                                                            .WithConditionType <SecuritySystemConditions>()
                                                                                            .SetInMemoryDataSource(serviceProvider)
                                                                                            .Build();

            // Act
            IEnumerable <Rule <SecuritySystemActionables, SecuritySystemConditions> > actual = await rulesEngine.MatchManyAsync(securitySystemActionable, expectedMatchDate, expectedConditions);

            // Assert
            actual.Should().NotBeNull();

            IEnumerable <SecuritySystemAction> securitySystemActions = actual.Select(r => r.ContentContainer.GetContentAs <SecuritySystemAction>()).ToList();

            securitySystemActions.Should().Contain(ssa => ssa.ActionName == "EnableEmergencyLights")
            .And.Contain(ssa => ssa.ActionName == "EnableEmergencyPower")
            .And.Contain(ssa => ssa.ActionName == "CallPowerGridPicket");
        }