public void ShouldCollectDataResults() { const int count = 3; var ruleSetHandler = new MaximumCountRuleSetHandler <TestObject> { RuleSetName = RuleSet, ErrorDescription = ErrorDescription, MaximumResultCount = 999, }; var stubs = AddTestObjects(count); foreach (var st in stubs) { ruleSetHandler.VerifyRule(st); } Assert.Equal(count, ruleSetHandler.Items.Count()); }
public void ShouldThrowIfMaxCountExceeded() { var ruleSetHandler = new MaximumCountRuleSetHandler <TestObject> { RuleSetName = RuleSet, ErrorDescription = ErrorDescription, MaximumResultCount = 3 }; ruleSetHandler.UseMemoryCacheStorage(Guid.NewGuid().ToString()); const int count = 3; var stubs = AddTestObjects(count); foreach (var st in stubs.Skip(1)) { ruleSetHandler.VerifyRule(st); } Assert.Throws <HttpException>(() => ruleSetHandler.VerifyRule(stubs.First())); }
public void ShouldNotAddToCollectionWhenUpdateRuleCollectionOnSuccessSetToFalse() { const int count = 3; var ruleSetHandler = new MaximumCountRuleSetHandler <TestObject> { RuleSetName = RuleSet, ErrorDescription = ErrorDescription, MaximumResultCount = 2, UpdateRuleCollectionOnSuccess = false }; ruleSetHandler.UseMemoryCacheStorage(Guid.NewGuid().ToString()); var stubs = AddTestObjects(count); foreach (var st in stubs) { ruleSetHandler.VerifyRule(st); } Assert.Equal(0, ruleSetHandler.Items.Count()); }
public void ShouldThrowCustomAction() { const int count = 5; var ruleSetHandler = new MaximumCountRuleSetHandler <TestObject> { RuleSetName = RuleSet, ErrorDescription = ErrorDescription, MaximumResultCount = 5, InvalidAction = () => { throw new DivideByZeroException(ErrorDescription); }, }; ruleSetHandler.UseMemoryCacheStorage(Guid.NewGuid().ToString()); var stubs = AddTestObjects(count); foreach (var st in stubs.Skip(1)) { ruleSetHandler.VerifyRule(st); } Assert.Throws <DivideByZeroException>(() => ruleSetHandler.VerifyRule(TestObject.Build())); }
public void ShouldVerifyAllRules() { int count = 0; var rule1 = new MaximumCountRuleSetHandler <TestObject>() { MaximumResultCount = 0, InvalidAction = () => count++ }; var rule2 = new MaximumCountRuleSetHandler <IpAddressRequest>() { MaximumResultCount = 0, InvalidAction = () => count++, }; RuleSetRunner.Instance.AddRule <MaximumCountRuleSetHandler <TestObject> >(rule1); RuleSetRunner.Instance.AddRule <MaximumCountRuleSetHandler <IpAddressRequest> >(rule2); rule1.VerifyRule(new TestObject()); rule2.VerifyRule(new IpAddressRequest()); RuleSetRunner.Instance.VerifyAllRules(); Assert.Equal(4, count); // rule will auto trigger once each time it hits the rule - then again once run }