/// <summary> /// Initialises a new instance of <see cref="Validator{TValidated}"/>. /// </summary> /// <param name="manifest">A validation manifest from which to creator the validator.</param> /// <param name="executorFactory">The rule-executor factory.</param> /// <param name="ruleFactory">The rule factory.</param> /// <param name="optionsResolver">An options resolver.</param> /// <param name="contextFactory">A rule execution context factory.</param> /// <exception cref="ArgumentNullException">If any parameter is <see langword="null" />.</exception> public Validator(ValidationManifest manifest, IGetsRuleExecutor executorFactory, IGetsAllExecutableRulesWithDependencies ruleFactory, IGetsResolvedValidationOptions optionsResolver, IGetsRuleExecutionContext contextFactory) { this.manifest = manifest ?? throw new ArgumentNullException(nameof(manifest)); this.executorFactory = executorFactory ?? throw new ArgumentNullException(nameof(executorFactory)); this.ruleFactory = ruleFactory ?? throw new ArgumentNullException(nameof(ruleFactory)); this.optionsResolver = optionsResolver ?? throw new ArgumentNullException(nameof(optionsResolver)); this.contextFactory = contextFactory ?? throw new ArgumentNullException(nameof(contextFactory)); }
public void GetRulesWithDependenciesShouldThrowValidationExceptionWithCorrectMessageIfThereAreCircularDependencies([Frozen] IGetsAllExecutableRulesWithDependencies wrapped, [Frozen] IDetectsCircularDependencies circularDependencyDetector, CircularDependencyPreventingRulesWithDependenciesDecorator sut, [ManifestModel] ManifestValue manifestValue, object objectToBeValidated, ResolvedValidationOptions validationOptions) { var circularDependencies = GetSomeCircularDependencies(manifestValue); var expectedMessage = @"Validation rules may not have circular dependencies. Following is a list of the circular dependencies found, to a maximum of the first 10. Type = System.String Name = Foo Validated type = System.Int32 Validated identity = Identity 1 -> Type = System.DateTime Validated type = System.Int64 -> Type = System.String Name = Foo Validated type = System.Int32 Validated identity = Identity 1 Type = System.String Name = Bar Validated type = System.Int32 Validated identity = Identity 2 -> Type = System.Object Validated type = System.Int64 -> Type = System.String Name = Bar Validated type = System.Int32 Validated identity = Identity 2 "; Mock.Get(wrapped) .Setup(x => x.GetRulesWithDependencies(It.IsAny <ManifestValue>(), It.IsAny <object>(), validationOptions)) .Returns(new ExecutableRuleAndDependencies[0]); Mock.Get(circularDependencyDetector) .Setup(x => x.GetCircularDependencies(It.IsAny <IEnumerable <ExecutableRuleAndDependencies> >())) .Returns(circularDependencies); Assert.That(() => sut.GetRulesWithDependencies(manifestValue, objectToBeValidated, validationOptions), Throws.InstanceOf <ValidationException>().And.Message.EqualTo(expectedMessage)); }
public void GetRulesWithDependenciesShouldNotThrowIfThereAreNoCircularDependencies([Frozen] IGetsAllExecutableRulesWithDependencies wrapped, [Frozen] IDetectsCircularDependencies circularDependencyDetector, CircularDependencyPreventingRulesWithDependenciesDecorator sut, [ManifestModel] ManifestValue manifestValue, object objectToBeValidated, ResolvedValidationOptions validationOptions) { Mock.Get(wrapped) .Setup(x => x.GetRulesWithDependencies(It.IsAny <ManifestValue>(), It.IsAny <object>(), validationOptions)) .Returns(new ExecutableRuleAndDependencies[0]); Mock.Get(circularDependencyDetector) .Setup(x => x.GetCircularDependencies(It.IsAny <IEnumerable <ExecutableRuleAndDependencies> >())) .Returns(Enumerable.Empty <CircularDependency>()); Assert.That(() => sut.GetRulesWithDependencies(manifestValue, objectToBeValidated, validationOptions), Throws.Nothing); }
/// <summary> /// Initialises a new instance of <see cref="CircularDependencyPreventingRulesWithDependenciesDecorator"/> /// </summary> /// <param name="wrapped">The wrapped implementation.</param> /// <param name="circularDependencyDetector">A circular dependency detector.</param> /// <exception cref="ArgumentNullException">If <paramref name="circularDependencyDetector"/> or <paramref name="wrapped"/> are <see langword="null"/>.</exception> public CircularDependencyPreventingRulesWithDependenciesDecorator(IGetsAllExecutableRulesWithDependencies wrapped, IDetectsCircularDependencies circularDependencyDetector) { this.wrapped = wrapped ?? throw new ArgumentNullException(nameof(wrapped)); this.circularDependencyDetector = circularDependencyDetector ?? throw new ArgumentNullException(nameof(circularDependencyDetector)); }
public async Task ValidateAsyncNonGenericShouldReturnReturnValidationResultContainingRuleResults([ManifestModel, Frozen] ValidationManifest manifest, [Frozen] IGetsRuleExecutor executorFactory, [Frozen] IGetsAllExecutableRulesWithDependencies ruleFactory, [Frozen] IGetsRuleExecutionContext contextFactory, IExecutesAllRules executor, Validator <object> sut, object validatedObject, ValidationOptions options, CancellationToken cancellationToken, IRuleExecutionContext context, [ExecutableModel] ExecutableRuleAndDependencies[] ruleAndDependencies, [RuleResult] ValidationRuleResult[] results) { Mock.Get(executorFactory) .Setup(x => x.GetRuleExecutorAsync(It.IsAny <ResolvedValidationOptions>(), cancellationToken)) .Returns(Task.FromResult(executor)); Mock.Get(contextFactory) .Setup(x => x.GetExecutionContext(ruleAndDependencies, It.IsAny <ResolvedValidationOptions>())) .Returns(context); Mock.Get(executor) .Setup(x => x.ExecuteAllRulesAsync(context, cancellationToken)) .Returns(Task.FromResult((IReadOnlyCollection <ValidationRuleResult>)results)); Mock.Get(ruleFactory) .Setup(x => x.GetRulesWithDependencies(manifest.RootValue, validatedObject, It.IsAny <ResolvedValidationOptions>())) .Returns(ruleAndDependencies); var result = await((IValidator)sut).ValidateAsync(validatedObject, options, cancellationToken); Assert.That(result.RuleResults, Is.EqualTo(results)); }