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();
        }
Example #2
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));
        }
        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();
        }
        public void SetInMemoryDataSource_GivenNullServiceProvider_ThrowsArgumentNullException()
        {
            // Arrange
            IServiceProvider serviceProvider = null;

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

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

            // Assert
            actual.Should().NotBeNull();
            actual.ParamName.Should().Be(nameof(serviceProvider));
        }
Example #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>
 /// <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>());