public async Task UpdateRuleAsync_GivenNullRule_ThrowsArgumentNullException()
        {
            // Arrange
            Rule <ContentType, ConditionType> rule = null;

            IInMemoryRulesStorage <ContentType, ConditionType> inMemoryRulesStorage = Mock.Of <IInMemoryRulesStorage <ContentType, ConditionType> >();
            IRuleFactory <ContentType, ConditionType>          ruleFactory          = Mock.Of <IRuleFactory <ContentType, ConditionType> >();

            Mock.Get(ruleFactory)
            .Setup(x => x.CreateRule(rule))
            .Verifiable();

            Mock.Get(inMemoryRulesStorage)
            .Setup(x => x.UpdateRule(It.IsAny <RuleDataModel <ContentType, ConditionType> >()))
            .Verifiable();

            InMemoryProviderRulesDataSource <ContentType, ConditionType> inMemoryProviderRulesDataSource
                = new InMemoryProviderRulesDataSource <ContentType, ConditionType>(inMemoryRulesStorage, ruleFactory);

            // Act
            ArgumentNullException argumentNullException = await Assert.ThrowsAsync <ArgumentNullException>(async() =>
                                                                                                           await inMemoryProviderRulesDataSource.UpdateRuleAsync(rule).ConfigureAwait(false)
                                                                                                           ).ConfigureAwait(false);

            // Assert
            argumentNullException.Should().NotBeNull();
            argumentNullException.ParamName.Should().Be(nameof(rule));
        }
示例#2
0
 internal InMemoryProviderRulesDataSource(
     IInMemoryRulesStorage <TContentType, TConditionType> inMemoryRulesStorage,
     IRuleFactory <TContentType, TConditionType> ruleFactory)
 {
     this.inMemoryRulesStorage = inMemoryRulesStorage ?? throw new ArgumentNullException(nameof(inMemoryRulesStorage));
     this.ruleFactory          = ruleFactory ?? throw new ArgumentNullException(nameof(ruleFactory));
 }
        public async Task AddRuleAsync_GivenRule_ConvertsToRuleDataModelAndAddsToDataSource()
        {
            // Arrange
            Rule <ContentType, ConditionType>          rule          = new Rule <ContentType, ConditionType>();
            RuleDataModel <ContentType, ConditionType> ruleDataModel = new RuleDataModel <ContentType, ConditionType>();

            IInMemoryRulesStorage <ContentType, ConditionType> inMemoryRulesStorage = Mock.Of <IInMemoryRulesStorage <ContentType, ConditionType> >();
            IRuleFactory <ContentType, ConditionType>          ruleFactory          = Mock.Of <IRuleFactory <ContentType, ConditionType> >();

            Mock.Get(ruleFactory)
            .Setup(x => x.CreateRule(rule))
            .Returns(ruleDataModel)
            .Verifiable();

            Mock.Get(inMemoryRulesStorage)
            .Setup(x => x.AddRule(ruleDataModel))
            .Verifiable();

            InMemoryProviderRulesDataSource <ContentType, ConditionType> inMemoryProviderRulesDataSource
                = new InMemoryProviderRulesDataSource <ContentType, ConditionType>(inMemoryRulesStorage, ruleFactory);

            // Act
            await inMemoryProviderRulesDataSource.AddRuleAsync(rule).ConfigureAwait(false);

            // Assert
            Mock.VerifyAll(Mock.Get(inMemoryRulesStorage), Mock.Get(ruleFactory));
        }
        public void Ctor_GivenNullParameters_ThrowsArgumentNullException(object param1, object param2)
        {
            // Arrange
            IInMemoryRulesStorage <ContentType, ConditionType> inMemoryRulesStorage = param1 as IInMemoryRulesStorage <ContentType, ConditionType>;
            IRuleFactory <ContentType, ConditionType>          ruleFactory          = param2 as IRuleFactory <ContentType, ConditionType>;

            // Act
            ArgumentNullException argumentNullException = Assert.Throws <ArgumentNullException>(() => new InMemoryProviderRulesDataSource <ContentType, ConditionType>(inMemoryRulesStorage, ruleFactory));

            //Assert
            argumentNullException.Should().NotBeNull();
            argumentNullException.ParamName.Should().BeOneOf(nameof(inMemoryRulesStorage), nameof(ruleFactory));
        }
示例#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));
        }
示例#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));
        }
        internal void LoadInMemoryStorage <TContentType, TConditionType, TContent>(
            string dataSourceFilePath,
            IInMemoryRulesStorage <TContentType, TConditionType> inMemoryRulesStorage,
            Func <dynamic, TContent> contentConvertFunc)
        {
            Stream rulesFile = File.OpenRead(dataSourceFilePath);

            IEnumerable <RuleDataModel <TContentType, TConditionType> > rules;

            using (StreamReader streamReader = new StreamReader(rulesFile ?? throw new InvalidOperationException("Could not load rules file.")))
            {
                string json = streamReader.ReadToEnd();

                IEnumerable <dynamic> array = JsonConvert.DeserializeObject(json, new JsonSerializerSettings
                {
                    TypeNameHandling = TypeNameHandling.All
                }) as IEnumerable <dynamic>;

                rules = array.Select(t =>
                {
                    return(new RuleDataModel <TContentType, TConditionType>
                    {
                        Content = contentConvertFunc.Invoke(t.Content),
                        ContentType = (TContentType)t.ContentTypeCode,
                        DateBegin = t.DateBegin,
                        DateEnd = t.DateEnd,
                        Name = t.Name,
                        Priority = t.Priority,
                        RootCondition = this.CreateConditionNodeDataModel <TConditionType>(t.RootCondition)
                    });
                }).ToList();
            }

            foreach (var rule in rules)
            {
                inMemoryRulesStorage.AddRule(rule);
            }
        }