public void SetInMemoryDataSource_GivenServiceProvider_RequestsInMemoryRulesStorageAndSetsOnSelector()
        {
            // Arrange
            InMemoryRulesStorage <ContentType, ConditionType> inMemoryRulesStorage = Mock.Of <InMemoryRulesStorage <ContentType, ConditionType> >();

            IServiceCollection serviceDescriptors = new ServiceCollection();

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

            IRulesDataSourceSelector <ContentType, ConditionType> rulesDataSourceSelector = Mock.Of <IRulesDataSourceSelector <ContentType, ConditionType> >();

            IRulesDataSource <ContentType, ConditionType> actualRulesDataSource = null;

            Mock.Get(rulesDataSourceSelector)
            .Setup(x => x.SetDataSource(It.IsAny <IRulesDataSource <ContentType, ConditionType> >()))
            .Callback <IRulesDataSource <ContentType, ConditionType> >((rds) =>
            {
                actualRulesDataSource = rds;
            });

            // Act
            rulesDataSourceSelector.SetInMemoryDataSource(serviceProvider);

            // Assert
            actualRulesDataSource.Should().NotBeNull();
            actualRulesDataSource.Should().BeOfType <InMemoryProviderRulesDataSource <ContentType, ConditionType> >();
            Mock.Get(rulesDataSourceSelector)
            .Verify();
        }
Exemple #2
0
        /// <summary>
        /// Sets the rules engine data source from a Mongo DB database.
        /// </summary>
        /// <typeparam name="TContentType">The type of the content type.</typeparam>
        /// <typeparam name="TConditionType">The type of the condition type.</typeparam>
        /// <param name="rulesDataSourceSelector">The rules data source selector.</param>
        /// <param name="mongoClient">The mongo client.</param>
        /// <param name="mongoDbProviderSettings">The mongo database provider settings.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">
        /// rulesDataSourceSelector
        /// or
        /// mongoClient
        /// or
        /// mongoDbProviderSettings
        /// </exception>
        public static IConfiguredRulesEngineBuilder <TContentType, TConditionType> SetMongoDbDataSource <TContentType, TConditionType>(
            this IRulesDataSourceSelector <TContentType, TConditionType> rulesDataSourceSelector,
            IMongoClient mongoClient,
            MongoDbProviderSettings mongoDbProviderSettings)
        {
            if (rulesDataSourceSelector is null)
            {
                throw new ArgumentNullException(nameof(rulesDataSourceSelector));
            }

            if (mongoClient is null)
            {
                throw new ArgumentNullException(nameof(mongoClient));
            }

            if (mongoDbProviderSettings is null)
            {
                throw new ArgumentNullException(nameof(mongoDbProviderSettings));
            }

            IContentSerializationProvider <TContentType> contentSerializationProvider = new DynamicToStrongTypeContentSerializationProvider <TContentType>();
            IRuleFactory <TContentType, TConditionType>  ruleFactory = new RuleFactory <TContentType, TConditionType>(contentSerializationProvider);
            MongoDbProviderRulesDataSource <TContentType, TConditionType> mongoDbProviderRulesDataSource
                = new MongoDbProviderRulesDataSource <TContentType, TConditionType>(
                      mongoClient,
                      mongoDbProviderSettings,
                      ruleFactory);

            return(rulesDataSourceSelector.SetDataSource(mongoDbProviderRulesDataSource));
        }
Exemple #3
0
        public void ConditionTypeSelector_WithConditionType_GivenTypeOfCondition_ReturnsNewRulesDataSourceSelector()
        {
            // Arrange
            ConditionTypeSelector <ContentType> sut = new ConditionTypeSelector <ContentType>();

            // Act
            IRulesDataSourceSelector <ContentType, ConditionType> actual = sut.WithConditionType <ConditionType>();

            // Assert
            actual.Should().BeOfType <RulesDataSourceSelector <ContentType, ConditionType> >();
        }
        public void SetInMemoryDataSource_GivenNullRulesDataSourceSelector_ThrowsArgumentNullException()
        {
            // Arrange
            IRulesDataSourceSelector <ContentType, ConditionType> rulesDataSourceSelector = null;

            // Act
            ArgumentNullException actual = Assert.Throws <ArgumentNullException>(() => rulesDataSourceSelector.SetInMemoryDataSource());

            // Assert
            actual.Should().NotBeNull();
            actual.ParamName.Should().Be(nameof(rulesDataSourceSelector));
        }
Exemple #5
0
        /// <summary>
        /// Sets the rules engine data source from a in-memory data source.
        /// </summary>
        /// <typeparam name="TContentType">The type of the content type.</typeparam>
        /// <typeparam name="TConditionType">The type of the condition type.</typeparam>
        /// <param name="rulesDataSourceSelector">The rules data source selector.</param>
        /// <param name="serviceProvider">The service provider.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">
        /// rulesDataSourceSelector
        /// or
        /// serviceProvider
        /// </exception>
        public static IConfiguredRulesEngineBuilder <TContentType, TConditionType> SetInMemoryDataSource <TContentType, TConditionType>(
            this IRulesDataSourceSelector <TContentType, TConditionType> rulesDataSourceSelector,
            IServiceProvider serviceProvider)
        {
            if (serviceProvider is null)
            {
                throw new ArgumentNullException(nameof(serviceProvider));
            }

            IInMemoryRulesStorage <TContentType, TConditionType> inMemoryRulesStorage = serviceProvider
                                                                                        .GetService(typeof(InMemoryRulesStorage <TContentType, TConditionType>)) as InMemoryRulesStorage <TContentType, TConditionType>;

            return(rulesDataSourceSelector.SetInMemoryDataSource(inMemoryRulesStorage));
        }
Exemple #6
0
        private static IConfiguredRulesEngineBuilder <TContentType, TConditionType> SetInMemoryDataSource <TContentType, TConditionType>(
            this IRulesDataSourceSelector <TContentType, TConditionType> rulesDataSourceSelector,
            IInMemoryRulesStorage <TContentType, TConditionType> inMemoryRulesStorage)
        {
            if (rulesDataSourceSelector is null)
            {
                throw new ArgumentNullException(nameof(rulesDataSourceSelector));
            }

            IRuleFactory <TContentType, TConditionType> ruleFactory = new RuleFactory <TContentType, TConditionType>();
            InMemoryProviderRulesDataSource <TContentType, TConditionType> inMemoryProviderRulesDataSource
                = new InMemoryProviderRulesDataSource <TContentType, TConditionType>(inMemoryRulesStorage, ruleFactory);

            return(rulesDataSourceSelector.SetDataSource(inMemoryProviderRulesDataSource));
        }
        public void SetInMemoryDataSource_NoParametersGiven_CreatesTransientInMemoryRulesStorageAndSetsOnSelector()
        {
            // Arrange
            IRulesDataSourceSelector <ContentType, ConditionType> rulesDataSourceSelector = Mock.Of <IRulesDataSourceSelector <ContentType, ConditionType> >();

            IRulesDataSource <ContentType, ConditionType> actualRulesDataSource = null;

            Mock.Get(rulesDataSourceSelector)
            .Setup(x => x.SetDataSource(It.IsAny <IRulesDataSource <ContentType, ConditionType> >()))
            .Callback <IRulesDataSource <ContentType, ConditionType> >((rds) =>
            {
                actualRulesDataSource = rds;
            });

            // Act
            rulesDataSourceSelector.SetInMemoryDataSource();

            // Assert
            actualRulesDataSource.Should().NotBeNull();
            actualRulesDataSource.Should().BeOfType <InMemoryProviderRulesDataSource <ContentType, ConditionType> >();
            Mock.Get(rulesDataSourceSelector)
            .Verify();
        }
Exemple #8
0
 /// <summary>
 /// Sets the rules engine data source from a in-memory data source.
 /// </summary>
 /// <typeparam name="TContentType">The type of the content type.</typeparam>
 /// <typeparam name="TConditionType">The type of the condition type.</typeparam>
 /// <param name="rulesDataSourceSelector">The rules data source selector.</param>
 /// <returns></returns>
 /// <exception cref="ArgumentNullException">
 /// rulesDataSourceSelector
 /// </exception>
 public static IConfiguredRulesEngineBuilder <TContentType, TConditionType> SetInMemoryDataSource <TContentType, TConditionType>(
     this IRulesDataSourceSelector <TContentType, TConditionType> rulesDataSourceSelector)
 => rulesDataSourceSelector.SetInMemoryDataSource(new InMemoryRulesStorage <TContentType, TConditionType>());