Exemple #1
0
        public void GetResolvedValidationOptionsShouldReturnHardcodedDefaultsIfSpecifiedAndDefaultOptionsAreNull([Frozen] IOptions <ValidationOptions> opts,
                                                                                                                 ValidationOptionsResolver sut,
                                                                                                                 ValidationOptions defaultOptions)
        {
            Mock.Get(opts).SetupGet(x => x.Value).Returns(defaultOptions);
            defaultOptions.AccessorExceptionBehaviour = null;
            defaultOptions.EnableMessageGeneration    = null;
            defaultOptions.RuleThrowingBehaviour      = null;
            defaultOptions.EnableRuleParallelization  = null;

            var result = sut.GetResolvedValidationOptions(null);

            Assert.Multiple(() =>
            {
                Assert.That(result.AccessorExceptionBehaviour,
                            Is.EqualTo(ValueAccessExceptionBehaviour.TreatAsError),
                            nameof(ValidationOptions.AccessorExceptionBehaviour));
                Assert.That(result.EnableMessageGeneration,
                            Is.EqualTo(false),
                            nameof(ValidationOptions.EnableMessageGeneration));
                Assert.That(result.RuleThrowingBehaviour,
                            Is.EqualTo(RuleThrowingBehaviour.OnError),
                            nameof(ValidationOptions.RuleThrowingBehaviour));
                Assert.That(result.EnableRuleParallelization,
                            Is.EqualTo(false),
                            nameof(ValidationOptions.EnableRuleParallelization));
            });
        }
Exemple #2
0
        async Task <ValidationResult> IValidator.ValidateAsync(object validatedObject, ValidationOptions options, CancellationToken cancellationToken)
        {
            var result = await ValidateAsync((TValidated)validatedObject, options, cancellationToken).ConfigureAwait(false);

            return((ValidationResult)result);
        }
Exemple #3
0
        public void GetResolvedValidationOptionsShouldReturnValuesFromDefaultOptionsIfSpecifiedOptionsAreNull([Frozen] IOptions <ValidationOptions> opts,
                                                                                                              ValidationOptionsResolver sut,
                                                                                                              ValidationOptions defaultOptions)
        {
            Mock.Get(opts).SetupGet(x => x.Value).Returns(defaultOptions);
            defaultOptions.AccessorExceptionBehaviour = ValueAccessExceptionBehaviour.Throw;
            defaultOptions.EnableMessageGeneration    = true;
            defaultOptions.RuleThrowingBehaviour      = RuleThrowingBehaviour.OnFailure;
            defaultOptions.EnableRuleParallelization  = true;

            var result = sut.GetResolvedValidationOptions(null);

            Assert.Multiple(() =>
            {
                Assert.That(result.AccessorExceptionBehaviour,
                            Is.EqualTo(ValueAccessExceptionBehaviour.Throw),
                            nameof(ValidationOptions.AccessorExceptionBehaviour));
                Assert.That(result.EnableMessageGeneration,
                            Is.EqualTo(true),
                            nameof(ValidationOptions.EnableMessageGeneration));
                Assert.That(result.RuleThrowingBehaviour,
                            Is.EqualTo(RuleThrowingBehaviour.OnFailure),
                            nameof(ValidationOptions.RuleThrowingBehaviour));
                Assert.That(result.EnableRuleParallelization,
                            Is.EqualTo(true),
                            nameof(ValidationOptions.EnableRuleParallelization));
            });
        }
        /// <inheritdoc/>
        public async Task <IQueryableValidationResult <TValidated> > ValidateAsync(TValidated validatedObject, ValidationOptions options = null, CancellationToken cancellationToken = default)
        {
            var result = await wrapped.ValidateAsync(validatedObject, options, cancellationToken).ConfigureAwait(false);

            ThrowForUnsuccessfulValidationIfApplicable(result, optionsResolver.GetResolvedValidationOptions(options));

            return(result);
        }
Exemple #5
0
        /// <inheritdoc/>
        public async Task <IQueryableValidationResult <TValidated> > ValidateAsync(TValidated validatedObject, ValidationOptions options = null, CancellationToken cancellationToken = default)
        {
            var result = await validator.ValidateAsync(validatedObject, options, cancellationToken).ConfigureAwait(false);

            if (options?.EnableMessageGeneration != true)
            {
                return(result);
            }

            return(await failureMessageEnricher.GetResultWithMessagesAsync(result, cancellationToken).ConfigureAwait(false));
        }
Exemple #6
0
        public void ValidateAsyncShouldUseTheWrappedValidatorThenReturnTheResultEnrichedWithMessagesIfOptionsEnableIt([Frozen] IValidator <object> validator,
                                                                                                                      [Frozen] IAddsFailureMessagesToResult failureMessageEnricher,
                                                                                                                      MessageEnrichingValidatorDecorator <object> sut,
                                                                                                                      [RuleResult] ValidationResult <object> originalResult,
                                                                                                                      [RuleResult] ValidationResult <object> resultWithMessages,
                                                                                                                      object validatedObject,
                                                                                                                      ValidationOptions options)
        {
            options.EnableMessageGeneration = true;
            Mock.Get(validator).Setup(x => x.ValidateAsync(validatedObject, options, default)).Returns(Task.FromResult <IQueryableValidationResult <object> >(originalResult));
            Mock.Get(failureMessageEnricher).Setup(x => x.GetResultWithMessagesAsync(originalResult, default)).Returns(Task.FromResult <IQueryableValidationResult <object> >(resultWithMessages));

            Assert.That(async() => await sut.ValidateAsync(validatedObject, options), Is.SameAs(resultWithMessages));
        }
Exemple #7
0
        public void ValidateAsyncShouldReturnTheWrappedValidatorResultIfOptionsDisableMessageGeneration([Frozen] IValidator <object> validator,
                                                                                                        [Frozen] IAddsFailureMessagesToResult failureMessageEnricher,
                                                                                                        MessageEnrichingValidatorDecorator <object> sut,
                                                                                                        [RuleResult] ValidationResult <object> originalResult,
                                                                                                        [RuleResult] ValidationResult <object> resultWithMessages,
                                                                                                        object validatedObject,
                                                                                                        ValidationOptions options)
        {
            options.EnableMessageGeneration = false;
            Mock.Get(validator).Setup(x => x.ValidateAsync(validatedObject, options, default)).Returns(Task.FromResult <IQueryableValidationResult <object> >(originalResult));

            Assert.That(async() => await sut.ValidateAsync(validatedObject, options), Is.SameAs(originalResult));
            Mock.Get(failureMessageEnricher).Verify(x => x.GetResultWithMessagesAsync <object>(It.IsAny <IQueryableValidationResult <object> >(), It.IsAny <CancellationToken>()), Times.Never);
        }
Exemple #8
0
        /// <inheritdoc/>
        public async Task <IQueryableValidationResult <TValidated> > ValidateAsync(TValidated validatedObject, ValidationOptions options = null, CancellationToken cancellationToken = default)
        {
            var resolvedOptions = optionsResolver.GetResolvedValidationOptions(options);
            var rules           = ruleFactory.GetRulesWithDependencies(manifest.RootValue, validatedObject, resolvedOptions);
            var executor        = await executorFactory.GetRuleExecutorAsync(resolvedOptions, cancellationToken).ConfigureAwait(false);

            var context     = contextFactory.GetExecutionContext(rules, resolvedOptions);
            var ruleResults = await executor.ExecuteAllRulesAsync(context, cancellationToken).ConfigureAwait(false);

            return(new ValidationResult <TValidated>(ruleResults, manifest));
        }
        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));
        }
 public void GenericValidateAsyncShouldNotThrowIfBehaviourIsNeverAndResultContainsBothErrorsAndFailures([Frozen] IValidator <string> wrapped,
                                                                                                        [Frozen] IGetsResolvedValidationOptions optionsResolver,
                                                                                                        ThrowingBehaviourValidatorDecorator <string> sut,
                                                                                                        string validated,
                                                                                                        ValidationOptions options)
 {
     Assert.That(async() => await ExerciseGenericSut(sut, validated, options, wrapped, optionsResolver, RuleThrowingBehaviour.Never, Error(), Fail()),
                 Throws.Nothing);
 }
 public void GenericValidateAsyncShouldThrowIfBehaviourIsOnFailureAndResultsContainAFailure([Frozen] IValidator <string> wrapped,
                                                                                            [Frozen] IGetsResolvedValidationOptions optionsResolver,
                                                                                            ThrowingBehaviourValidatorDecorator <string> sut,
                                                                                            string validated,
                                                                                            ValidationOptions options)
 {
     Assert.That(async() => await ExerciseGenericSut(sut, validated, options, wrapped, optionsResolver, RuleThrowingBehaviour.OnFailure, Fail()),
                 Throws.InstanceOf <ValidationException>());
 }
 public void NonGenericValidateAsyncShouldNotThrowIfBehaviourIsNeverAndResultContainsBothErrorsAndFailures(string validated,
                                                                                                           ValidationOptions options)
 {
     Assert.That(async() => await ExerciseNonGenericSut(validated, options, RuleThrowingBehaviour.Never, Error(), Fail()),
                 Throws.Nothing);
 }
 public void NonGenericValidateAsyncShouldNotThrowIfBehaviourIsOnFailureAndResultsContainNoErrorsOrFailures(string validated,
                                                                                                            ValidationOptions options)
 {
     Assert.That(async() => await ExerciseNonGenericSut(validated, options, RuleThrowingBehaviour.OnFailure, Pass()),
                 Throws.Nothing);
 }
 public void NonGenericValidateAsyncShouldThrowIfBehaviourIsOnFailureAndResultsContainAFailure(string validated,
                                                                                               ValidationOptions options)
 {
     Assert.That(async() => await ExerciseNonGenericSut(validated, options, RuleThrowingBehaviour.OnFailure, Fail()),
                 Throws.InstanceOf <ValidationException>());
 }