ValidationRuleResult GetValidationRuleResult(RuleResult result, ManifestRule manifestRule, RuleIdentifier ruleIdentifier, IValidationLogic logic) { var valueContext = new ValueContext(Guid.NewGuid(), ParentValue, manifestRule.ManifestValue); var context = new RuleContext(manifestRule, ruleIdentifier, ValidatedValue, new [] { valueContext }, RuleInterface); return(new ValidationRuleResult(result, context, logic)); }
public async Task GetValidationLogicShouldReturnWorkingLogicForRuleWhichOperatesOnCollectionOfStrings([Frozen] IResolvesRule ruleResolver, ValidationLogicFactory sut, string str, [RuleContext] RuleContext context) { var value = new ManifestValue { ValidatedType = typeof(IEnumerable <string>), CollectionItemValue = new ManifestCollectionItem { ValidatedType = typeof(string), }, }; var rule = new ManifestRule(value.CollectionItemValue, new ManifestRuleIdentifier(value.CollectionItemValue, typeof(StringRule))); value.Rules.Add(rule); var ruleBody = new StringRule(); Mock.Get(ruleResolver).Setup(x => x.ResolveRule(typeof(StringRule))).Returns(ruleBody); var result = sut.GetValidationLogic(rule); await result.GetResultAsync(str, null, context); Assert.That(ruleBody.Executed, Is.True); }
/// <summary> /// Selects the rule-logic interface with the best/closest match to the rule type and the way in which it is /// to be used by the validator. /// </summary> /// <remarks> /// <para> /// If the validation rule has a parent validated type - the <seealso cref="IManifestItem.Parent"/> property /// of the <see cref="ManifestRule.ManifestValue"/> is not <see langword="null"/> - and the rule type implements /// <see cref="IRule{TValue, TValidated}"/> of the appropriate generic types the type of the value rule /// interface will be returned. /// </para> /// <para> /// A second attempt will be made if the rule has no parent validated type, or if it does not implement the <see cref="IRule{TValue, TValidated}"/> /// interface with appropriare generic types. If it implements <see cref="IRule{TValidated}"/> with the /// appropriate generic type then the type of that interface will be returned. /// </para> /// <para> /// If neither mechanism above succeeds in getting a rule interface then an exception will be raised. /// </para> /// </remarks> /// <param name="ruleDefinition">The rule definition.</param> /// <returns>The interface-type which most closely matches the rule and its usage.</returns> /// <seealso cref="IRule{TValue, TValidated}"/> /// <seealso cref="IRule{TValidated}"/> /// <exception cref="ValidatorBuildingException">If the rule class does not implement either <see cref="IRule{TValue, TValidated}"/> /// or <see cref="IRule{TValidated}"/> with appropriate generic types.</exception> static Type GetBestRuleInterface(ManifestRule ruleDefinition) { var ruleType = ruleDefinition.Identifier.RuleType; var validatedType = ruleDefinition.ManifestValue.ValidatedType; var parentValidatedType = ruleDefinition.ManifestValue.Parent?.ValidatedType; var valueRuleInterface = TryGetValueRuleInterface(ruleType, validatedType, parentValidatedType); if (valueRuleInterface != null) { return(valueRuleInterface); } var ruleInterface = TryGetRuleInterface(ruleType, validatedType); if (ruleInterface != null) { return(ruleInterface); } var messageTemplate = (parentValidatedType != null) ? "RuleTypeMustImplementAppropriateRuleOrValueRuleInterface" : "RuleTypeMustImplementAppropriateRuleInterface"; var message = String.Format(GetExceptionMessage(messageTemplate), ruleType, validatedType, parentValidatedType, nameof(IRule <object>), nameof(IRule <object, object>)); throw new ValidatorBuildingException(message); }
/// <summary> /// Gets the underlying rule logic. /// This is returned as <see cref="object"/> because we cannot be sure as to which rule interface is being used. /// </summary> /// <param name="ruleDefinition">The rule definition</param> /// <returns>The rule logic</returns> /// <seealso cref="IRule{TValue, TValidated}"/> /// <seealso cref="IRule{TValidated}"/> /// <exception cref="ValidatorBuildingException">If the <see cref="ManifestRule.RuleConfiguration"/> action is not null and throws an exception.</exception> object GetRuleLogic(ManifestRule ruleDefinition) { var rule = ruleResolver.ResolveRule(ruleDefinition.Identifier.RuleType); ConfigureRule(rule, ruleDefinition); return(rule); }
public async Task RecursiveValidationShouldReturnaResultFromADescendentObjectValidatedUsingTheSameManifestAsAnAncestor([IntegrationTesting] IGetsValidator validatorFactory) { var manifest = new ValidationManifest { ValidatedType = typeof(Node), RootValue = new ManifestValue { ValidatedType = typeof(Node), IdentityAccessor = obj => ((Node)obj).Identity, Children = new[] { new ManifestValue { ValidatedType = typeof(NodeChild), IdentityAccessor = obj => ((NodeChild)obj).Identity, AccessorFromParent = obj => ((Node)obj).Child, }, new ManifestValue { ValidatedType = typeof(string), AccessorFromParent = obj => ((Node)obj).Name, } } } }; var nameValue = manifest.RootValue.Children.Single(x => x.ValidatedType == typeof(string)); var nameRule = new ManifestRule(nameValue, new ManifestRuleIdentifier(nameValue, typeof(MatchesRegex))) { RuleConfiguration = obj => ((MatchesRegex)obj).Pattern = "^Foo", }; nameValue.Rules.Add(nameRule); var childValue = manifest.RootValue.Children.Single(x => x.ValidatedType == typeof(NodeChild)); var recursiveValue = new RecursiveManifestValue(manifest.RootValue) { AccessorFromParent = obj => ((NodeChild)obj).Node, }; childValue.Children.Add(recursiveValue); var validatedObject = new Node { Child = new NodeChild { Node = new Node { Child = new NodeChild { Node = new Node { Name = "Invalid" } } } } }; var sut = validatorFactory.GetValidator <Node>(manifest); var result = await sut.ValidateAsync(validatedObject); Assert.That(result, Has.One.Matches <ValidationRuleResult>(r => r.Outcome == RuleOutcome.Failed && Equals(r.ValidatedValue, "Invalid"))); }
public void ValidatingACircularReferenceShouldNotThrowOrTimeOut([IntegrationTesting] IGetsValidator validatorFactory) { var manifest = new ValidationManifest { ValidatedType = typeof(Node), RootValue = new ManifestValue { ValidatedType = typeof(Node), IdentityAccessor = obj => ((Node)obj).Identity, Children = new[] { new ManifestValue { ValidatedType = typeof(NodeChild), IdentityAccessor = obj => ((NodeChild)obj).Identity, AccessorFromParent = obj => ((Node)obj).Child, }, new ManifestValue { ValidatedType = typeof(string), AccessorFromParent = obj => ((Node)obj).Name, } } } }; var nameValue = manifest.RootValue.Children.Single(x => x.ValidatedType == typeof(string)); var nameRule = new ManifestRule(nameValue, new ManifestRuleIdentifier(nameValue, typeof(MatchesRegex))) { RuleConfiguration = obj => ((MatchesRegex)obj).Pattern = "^Foo", }; nameValue.Rules.Add(nameRule); var childValue = manifest.RootValue.Children.Single(x => x.ValidatedType == typeof(NodeChild)); var recursiveValue = new RecursiveManifestValue(manifest.RootValue) { AccessorFromParent = obj => ((NodeChild)obj).Node, }; childValue.Children.Add(recursiveValue); var validatedObject = new Node { Child = new NodeChild { Node = new Node { Child = new NodeChild { Node = new Node { Name = "Invalid" } } } } }; validatedObject.Child.Node.Child.Node.Child = validatedObject.Child; var sut = validatorFactory.GetValidator <Node>(manifest); Assert.That(() => sut.ValidateAsync(validatedObject).Wait(300), Is.True, "Validation completes within 300ms"); }
/// <summary> /// Gets a validation logic instance for the specified manifest rule definition. /// </summary> /// <param name="ruleDefinition">A manifest rule definition.</param> /// <returns>A validation logic instance by which the rule's logic may be executed in a generalised manner.</returns> /// <exception cref="ValidatorBuildingException">If the <see cref="ManifestRule.RuleConfiguration"/> action is not null and throws an exception.</exception> public IValidationLogic GetValidationLogic(ManifestRule ruleDefinition) { if (ruleDefinition is null) { throw new ArgumentNullException(nameof(ruleDefinition)); } var ruleLogic = GetRuleLogic(ruleDefinition); return(WrapRuleLogicWithAdapter(ruleDefinition, ruleLogic)); }
/// <summary> /// Initialises an instance of <see cref="ManifestRuleInfo"/>. /// This is essentially a copy-constructor for a <see cref="ManifestRule"/>. /// </summary> /// <param name="manifestRule">The manifest rule from which to create this instance.</param> /// <exception cref="System.ArgumentNullException">If <paramref name="manifestRule"/> is <see langword="null" />.</exception> public ManifestRuleInfo(ManifestRule manifestRule) { if (manifestRule is null) { throw new System.ArgumentNullException(nameof(manifestRule)); } Identifier = manifestRule.Identifier; DependencyRules = new List <ManifestRuleIdentifier>(manifestRule.DependencyRules); ManifestValue = new ManifestValueInfo(manifestRule.ManifestValue); }
/// <summary> /// Wraps the <paramref name="ruleLogic"/> object with an adapter that allows it to be used via /// the interface <see cref="IValidationLogic"/>. /// </summary> /// <param name="ruleDefinition">The manifest rule definition.</param> /// <param name="ruleLogic">The rule logic object.</param> /// <returns>The rule logic object wrapped with an adapter allowing it to be executed via the interface <see cref="IValidationLogic"/>.</returns> static IValidationLogic WrapRuleLogicWithAdapter(ManifestRule ruleDefinition, object ruleLogic) { var ruleInterface = GetBestRuleInterface(ruleDefinition); var ruleAdapter = ruleInterface.GetGenericTypeDefinition() == typeof(IRule <>) ? GetRuleAdapter(ruleInterface.GenericTypeArguments[0], ruleLogic) : GetValueRuleAdapter(ruleInterface.GenericTypeArguments[0], ruleInterface.GenericTypeArguments[1], ruleLogic); ruleAdapter = WrapWithExceptionHandlingDecorator(ruleAdapter); return(ruleAdapter); }
public void AddRulesShouldAddBuilderReturnedFromManifestFactory([Frozen] IGetsValidatorManifest manifestFactory, [Frozen, ManifestModel] ValidatorBuilderContext context, ValueAccessorBuilder <ValidatedObject, string> sut, IGetsManifestValue manifest, [ManifestModel] ManifestRule rule, [ManifestModel] ManifestValue value) { Mock.Get(manifestFactory) .Setup(x => x.GetValidatorManifest(typeof(StringValidator), context)) .Returns(manifest); Mock.Get(manifest).Setup(x => x.GetManifestValue()).Returns(() => value); sut.AddRules <StringValidator>(); Assert.That(sut.GetManifestValue().Children.Single(), Is.SameAs(value)); }
public void GetValidationLogicShouldThrowValidatorBuildingExceptionIfRuleIsWrongInterfaceForRule([Frozen] IResolvesRule ruleResolver, ValidationLogicFactory sut, string str) { var value = new ManifestValue { ValidatedType = typeof(string) }; var rule = new ManifestRule(value, new ManifestRuleIdentifier(value, typeof(object))); value.Rules.Add(rule); var ruleBody = new object(); Mock.Get(ruleResolver).Setup(x => x.ResolveRule(typeof(object))).Returns(ruleBody); Assert.That(() => sut.GetValidationLogic(rule), Throws.InstanceOf <ValidatorBuildingException>()); }
public void GetFailureMessageAsyncShouldCallWrappedServiceWithOneGenericType([Frozen] IGetsFailureMessage <string> wrapped, FailureMessageProviderAdapter <string> sut, [RuleResult] RuleResult ruleResult, [ManifestModel] ManifestRule rule, Type ruleInterface, RuleIdentifier id, string actualValue, string expectedResult, IValidationLogic validationLogic) { var context = new RuleContext(rule, id, actualValue, Enumerable.Empty <ValueContext>(), ruleInterface); var validationRuleResult = new ValidationRuleResult(ruleResult, context, validationLogic); Mock.Get(wrapped).Setup(x => x.GetFailureMessageAsync(actualValue, validationRuleResult, default)).Returns(Task.FromResult(expectedResult)); Assert.That(async() => await sut.GetFailureMessageAsync(validationRuleResult), Is.EqualTo(expectedResult)); }
public void GetValidationLogicShouldThrowValidatorBuildingExceptionIfTheRuleConfigurationActionThrowsAnException([Frozen] IResolvesRule ruleResolver, ValidationLogicFactory sut, string str) { var value = new ManifestValue { ValidatedType = typeof(string) }; var rule = new ManifestRule(value, new ManifestRuleIdentifier(value, typeof(StringRule))); rule.RuleConfiguration = obj => throw new Exception(); value.Rules.Add(rule); var ruleBody = new StringRule(); Mock.Get(ruleResolver).Setup(x => x.ResolveRule(typeof(StringRule))).Returns(ruleBody); Assert.That(() => sut.GetValidationLogic(rule), Throws.InstanceOf <ValidatorBuildingException>()); }
/// <summary> /// Where <see cref="ManifestRule.RuleConfiguration"/> is not null, this method applies that configuration /// to the <paramref name="rule"/> instance. /// </summary> /// <param name="rule">The rule object to be configured.</param> /// <param name="ruleDefinition">The rule definition.</param> /// <exception cref="ValidatorBuildingException">If the configuration action throws an exception.</exception> static void ConfigureRule(object rule, ManifestRule ruleDefinition) { if (ruleDefinition.RuleConfiguration is null) { return; } try { ruleDefinition.RuleConfiguration(rule); } catch (Exception e) { var message = String.Format(Resources.ExceptionMessages.GetExceptionMessage("UnexpectedExceptionConfiguringRule"), ruleDefinition.Identifier); throw new ValidatorBuildingException(message, e); } }
public async Task GetValidationLogicShouldReturnRuleThatUsesCorrectInterfaceWhenOriginalLogicWasAmbiguousBetweenRuleAndValueRule([Frozen] IResolvesRule ruleResolver, ValidationLogicFactory sut, string str, [RuleContext] RuleContext context) { var value = new ManifestValue { ValidatedType = typeof(string) }; var rule = new ManifestRule(value, new ManifestRuleIdentifier(value, typeof(StringValueRule))); value.Rules.Add(rule); var ruleBody = new StringValueRule(); Mock.Get(ruleResolver).Setup(x => x.ResolveRule(typeof(StringValueRule))).Returns(ruleBody); var result = sut.GetValidationLogic(rule); await result.GetResultAsync(str, null, context); Assert.That(ruleBody.ExecutedAsRule, Is.True); }
public void GetValidationLogicShouldThrowValidatorBuildingExceptionIfRuleWhichOperatesOnCollectionButValueIsNotEnumerable([Frozen] IResolvesRule ruleResolver, ValidationLogicFactory sut, string str) { var value = new ManifestValue { ValidatedType = typeof(int), CollectionItemValue = new ManifestCollectionItem { ValidatedType = typeof(int) } }; var rule = new ManifestRule(value.CollectionItemValue, new ManifestRuleIdentifier(value.CollectionItemValue, typeof(StringRule))); value.Rules.Add(rule); var ruleBody = new StringRule(); Mock.Get(ruleResolver).Setup(x => x.ResolveRule(typeof(StringRule))).Returns(ruleBody); Assert.That(() => sut.GetValidationLogic(rule), Throws.InstanceOf <ValidatorBuildingException>()); }
public void GetValidationLogicShouldConfigureRuleWithConfigurationAction([Frozen] IResolvesRule ruleResolver, ValidationLogicFactory sut, string str, string configValue) { var value = new ManifestValue { ValidatedType = typeof(string) }; var rule = new ManifestRule(value, new ManifestRuleIdentifier(value, typeof(StringRule))); rule.RuleConfiguration = obj => ((StringRule)obj).ConfigurableValue = configValue; value.Rules.Add(rule); var ruleBody = new StringRule(); Mock.Get(ruleResolver).Setup(x => x.ResolveRule(typeof(StringRule))).Returns(ruleBody); var result = sut.GetValidationLogic(rule); Assert.That(ruleBody.ConfigurableValue, Is.EqualTo(configValue)); }
public void AddRuleShouldProvideConfigFunctionToRuleBuilder([Frozen] IGetsRuleBuilder ruleBuilderFactory, [Frozen, ManifestModel] ValidatorBuilderContext context, ValueAccessorBuilder <ValidatedObject, string> sut, IBuildsRule <StringValueRule> ruleBuilder, [ManifestModel] ManifestRule rule, [ManifestModel] ManifestValue value) { Mock.Get(ruleBuilderFactory) .Setup(x => x.GetRuleBuilder <StringValueRule>(It.IsAny <ValidatorBuilderContext>(), It.IsAny <Action <IConfiguresRule <StringValueRule> > >())) .Returns(ruleBuilder); Mock.Get(ruleBuilder) .Setup(x => x.GetManifestValue()) .Returns(() => value); Action <IConfiguresRule <StringValueRule> > configFunction = r => { }; sut.AddRuleWithParent <StringValueRule>(configFunction); Mock.Get(ruleBuilderFactory) .Verify(x => x.GetRuleBuilder <StringValueRule>(It.IsAny <ValidatorBuilderContext>(), configFunction), Times.Once); }
/// <summary> /// Initialises a new instance of <see cref="RuleContext"/>. /// </summary> /// <param name="manifestRule">The manifest rule from which this context was created.</param> /// <param name="ruleIdentifier">The rule identifier.</param> /// <param name="actualValue">The actual validated value.</param> /// <param name="ancestorContexts">A collection of ancestor contexts.</param> /// <param name="ruleInterface">The rule interface used for this rule execution.</param> /// <param name="collectionItemOrder">An optional collection item order.</param> /// <exception cref="ArgumentNullException"></exception> public RuleContext(ManifestRule manifestRule, RuleIdentifier ruleIdentifier, object actualValue, IEnumerable <ValueContext> ancestorContexts, Type ruleInterface, long?collectionItemOrder = null) : base(ruleIdentifier?.ObjectIdentity, actualValue, manifestRule?.ManifestValue, collectionItemOrder) { if (manifestRule is null) { throw new ArgumentNullException(nameof(manifestRule)); } if (ancestorContexts is null) { throw new ArgumentNullException(nameof(ancestorContexts)); } RuleInfo = new ManifestRuleInfo(manifestRule); RuleIdentifier = ruleIdentifier ?? throw new ArgumentNullException(nameof(ruleIdentifier)); RuleInterface = ruleInterface ?? throw new ArgumentNullException(nameof(ruleInterface)); AncestorContexts = new List <ValueContext>(ancestorContexts); }
public async Task GetValidationLogicShouldReturnWorkingLogicForValueRule([Frozen] IResolvesRule ruleResolver, ValidationLogicFactory sut, string str, [RuleContext] RuleContext context) { var value = new ManifestValue { ValidatedType = typeof(string), Parent = new ManifestValue { ValidatedType = typeof(ComplexObject) } }; var rule = new ManifestRule(value, new ManifestRuleIdentifier(value, typeof(StringValueRule))); value.Rules.Add(rule); var ruleBody = new StringValueRule(); Mock.Get(ruleResolver).Setup(x => x.ResolveRule(typeof(StringValueRule))).Returns(ruleBody); var result = sut.GetValidationLogic(rule); await result.GetResultAsync(str, null, context); Assert.That(ruleBody.ExecutedAsValueRule, Is.True); }
public void CanGetFailureMessageShouldCallWrappedServiceWithOneGenericType([Frozen] IHasFailureMessageUsageCriteria <string> wrapped, FailureMessageCriteriaAdapter <string> sut, [RuleResult] RuleResult ruleResult, [ManifestModel] ManifestRule rule, Type ruleInterface, RuleIdentifier id, string actualValue, bool expectedResult, IValidationLogic validationLogic) { var context = new RuleContext(rule, id, actualValue, Enumerable.Empty <ValueContext>(), ruleInterface); var validationRuleResult = new ValidationRuleResult(ruleResult, context, validationLogic); Mock.Get(wrapped).Setup(x => x.CanGetFailureMessage(actualValue, validationRuleResult)).Returns(expectedResult); var actualResult = sut.CanGetFailureMessage(validationRuleResult); Assert.Multiple(() => { Assert.That(actualResult, Is.EqualTo(expectedResult), "Actual result matches expected"); Mock.Get(wrapped).Verify(x => x.CanGetFailureMessage(actualValue, validationRuleResult), Times.Once, "Wrapped service was called"); }); }
public void IsMatchShouldReturnFalseIfActualValidatedTypeIsNotASubclassOfAttributeType([RuleResult] RuleResult result, [ManifestModel] ManifestRule rule, Type ruleType, object objectId, object actualValue, IValidationLogic validationLogic) { var id = new RuleIdentifier(ruleType, typeof(Employee), objectId); var context = new RuleContext(rule, id, actualValue, Enumerable.Empty <ValueContext>(), typeof(string)); var ruleResult = new ValidationRuleResult(result, context, validationLogic); var sut = new FailureMessageStrategyAttribute { ValidatedType = typeof(string), }; Assert.That(() => sut.IsMatch(ruleResult), Is.False); }
public void GetManifestRulesShouldCombineApplicablePolymorphicRulesWithManifestValue([ManifestModel] ManifestValue value, [ManifestModel] ManifestPolymorphicType type1, [ManifestModel] ManifestPolymorphicType type2, [ManifestModel] ManifestPolymorphicType type3, [ManifestModel] ManifestRule rule1, [ManifestModel] ManifestRule rule2, [ManifestModel] ManifestRule rule3, [ManifestModel] ManifestRule rule4) { value.ValidatedType = typeof(Person); type1.ValidatedType = typeof(Employee); type2.ValidatedType = typeof(Manager); type3.ValidatedType = typeof(Cleaner); value.Rules = new[] { rule1 }; type1.Rules = new[] { rule2 }; type2.Rules = new[] { rule3 }; type3.Rules = new[] { rule4 }; value.PolymorphicTypes = new[] { type1, type2, type3 }; var response = new SuccessfulGetValueToBeValidatedResponse(new Manager()); var sut = new ValidatedValueBasis(value, response, null); Assert.That(() => sut.GetManifestRules(), Is.EquivalentTo(new[] { rule1, rule2, rule3 })); }
public void GetValidatedValueShouldGetAnExecutableRuleUsingTheLogicFactoryFromEachManifestRule([Frozen] IGetsValidationLogic validationLogicFactory, ValidatedValueFromBasisFactory sut, [ExecutableModel] ValidatedValueBasis basis, [ManifestModel] ManifestRule rule1, [ManifestModel] ManifestRule rule2, IValidationLogic logic1, IValidationLogic logic2) { Mock.Get(validationLogicFactory).Setup(x => x.GetValidationLogic(rule1)).Returns(logic1); Mock.Get(validationLogicFactory).Setup(x => x.GetValidationLogic(rule2)).Returns(logic2); basis.ManifestValue.Rules.Add(rule1); basis.ManifestValue.Rules.Add(rule2); var result = sut.GetValidatedValue(basis); Assert.That(result.Rules.Select(x => x.RuleLogic).ToList(), Is.EqualTo(new[] { logic1, logic2 })); }
public void GetExpressionShouldReturnAnExpressionWhichReturnsTrueForAResultWhichMatchesTheCurrentContextWithAValue([ManifestModel] ManifestRule rule, [RuleId] RuleIdentifier ruleIdentifier, object actualValue, Type ruleInterface, [RuleResult] RuleResult ruleResult, IValidationLogic logic) { ((ManifestValueBase)rule.ManifestValue).IdentityAccessor = null; var context = new RuleContext(rule, ruleIdentifier, actualValue, Enumerable.Empty <ValueContext>(), ruleInterface); var validationRuleResult = new ValidationRuleResult(ruleResult, context, logic); var sut = new RuleResultIsForDescendentOfValue(rule.ManifestValue, actualValue); Assert.That(() => sut.Matches(validationRuleResult), Is.True); }
public void GetExpressionShouldReturnAnExpressionWhichReturnsFalseForAResultWhichMatchesAnAncestorContextWhenAllowAncestorsIsFalse([ManifestModel] ManifestValue value, [ManifestModel] ManifestValue otherValue, [ManifestModel] ManifestRule rule, [RuleId] RuleIdentifier ruleIdentifier, object identity, object actualValue, Type ruleInterface, [RuleResult] RuleResult ruleResult, IValidationLogic logic) { value.IdentityAccessor = null; var context = new RuleContext(rule, ruleIdentifier, actualValue, new [] { new ValueContext(identity, actualValue, value) }, ruleInterface); var validationRuleResult = new ValidationRuleResult(ruleResult, context, logic); var sut = new RuleResultIsForDescendentOfValue(value, false); Assert.That(() => sut.Matches(validationRuleResult), Is.False); }
public void GetRulesWithDependenciesShouldReturnAnObjectWithDependencyExecutableRulesWhereItHasADependencyUponAParentValue([Frozen] IGetsAllExecutableRules executableRulesProvider, [ManifestModel] ManifestValue manifestValue, object objectToBeValidated, IValidationLogic logic, ManifestRule manifestRule, ManifestRule manifestDependency, [ExecutableModel] ValidatedValue validatedValue, [ExecutableModel] ValidatedValue parentValue, ExecutableRulesAndDependenciesProvider sut, ResolvedValidationOptions validationOptions) { var rule = new ExecutableRule { ValidatedValue = validatedValue, ManifestRule = manifestRule, RuleLogic = logic }; validatedValue.ParentValue = parentValue; var dependency = new ExecutableRule { ValidatedValue = parentValue, ManifestRule = manifestDependency, RuleLogic = logic }; parentValue.ManifestValue = manifestDependency.Identifier.ManifestValue; parentValue.Rules.Add(dependency); Mock.Get(executableRulesProvider) .Setup(x => x.GetExecutableRules(manifestValue, objectToBeValidated, validationOptions)) .Returns(new [] { rule, dependency }); rule.ManifestRule.DependencyRules.Clear(); rule.ManifestRule.DependencyRules.Add(manifestDependency.Identifier); var result = sut.GetRulesWithDependencies(manifestValue, objectToBeValidated, validationOptions); Assert.That(result.First(x => x.ExecutableRule == rule).Dependencies, Is.EqualTo(new[] { dependency })); }
public void GetRulesWithDependenciesShouldThrowIfTheMatchingValueDoesNotHaveTheSpecifiedRule([Frozen] IGetsAllExecutableRules executableRulesProvider, [ManifestModel] ManifestValue manifestValue, object objectToBeValidated, IValidationLogic logic, ManifestRule manifestRule, ManifestRule manifestDependency, [ExecutableModel] ValidatedValue validatedValue, [ExecutableModel] ValidatedValue parentValue, ExecutableRulesAndDependenciesProvider sut, ResolvedValidationOptions validationOptions) { var rule = new ExecutableRule { ValidatedValue = validatedValue, ManifestRule = manifestRule, RuleLogic = logic }; validatedValue.ParentValue = parentValue; var dependency = new ExecutableRule { ValidatedValue = parentValue, ManifestRule = manifestDependency, RuleLogic = logic }; parentValue.ManifestValue = manifestDependency.Identifier.ManifestValue; Mock.Get(executableRulesProvider) .Setup(x => x.GetExecutableRules(manifestValue, objectToBeValidated, validationOptions)) .Returns(new [] { rule, dependency }); rule.ManifestRule.DependencyRules.Clear(); rule.ManifestRule.DependencyRules.Add(manifestDependency.Identifier); Assert.That(() => sut.GetRulesWithDependencies(manifestValue, objectToBeValidated, validationOptions), Throws.InstanceOf <ValidationException>()); }
/// <summary> /// Initializes a new instance of <see cref="RuleIdentifier"/> based upon an instance of <see cref="ManifestRule"/>. /// </summary> /// <param name="manifestRule">The manifest rule.</param> /// <param name="objectIdentity">The identity of the validated object.</param> public RuleIdentifier(ManifestRule manifestRule, object objectIdentity) : this(manifestRule.Identifier.RuleType, manifestRule.Identifier.ValidatedType, objectIdentity, (manifestRule.ManifestValue is IManifestValue val)? val.MemberName : null,
public void IsMatchShouldReturnTrueIfActualParentValidatedTypeIsASubclassOfAttributeParentType([RuleResult] RuleResult result, [ManifestModel] ManifestRule rule, Type type, object objectId, object actualValue, IValidationLogic validationLogic) { var id = new RuleIdentifier(type, type, objectId); var context = new RuleContext(rule, id, actualValue, new [] { new ValueContext(objectId, actualValue, new ManifestValue { ValidatedType = typeof(Employee) }) }, typeof(string)); var ruleResult = new ValidationRuleResult(result, context, validationLogic); var sut = new FailureMessageStrategyAttribute { ParentValidatedType = typeof(Person), }; Assert.That(() => sut.IsMatch(ruleResult), Is.True); }