From ca53c0403c7f4a00fd87a96b1de7c5ac61310220 Mon Sep 17 00:00:00 2001 From: Stuart Baillie Date: Thu, 16 Nov 2023 17:41:06 +0000 Subject: [PATCH] JR-7629 : Update FluentValidation from 8.1.2 to 11.8.0 --- JudoPayDotNet/JudoPayDotNet.csproj | 2 +- .../Validation/PaymentReceiptValidator.cs | 20 ++++++ ...PaymentRequiresThreeDSecureTwoValidator.cs | 23 +++++++ .../Validation/PolymorphicValidator.cs | 63 ------------------- .../Validations/PaymentReceiptValidation.cs | 17 ----- ...ntRequiresThreeDSecureTwoModelValidator.cs | 20 ------ .../TransactionResultBaseValidation.cs | 14 ----- .../TransactionResultValidation.cs | 8 --- .../Validation/PaymentsValidationTests.cs | 4 +- .../TransactionResultValidationTests.cs | 29 ++++----- 10 files changed, 60 insertions(+), 140 deletions(-) create mode 100644 JudoPayDotNet/Validation/PaymentReceiptValidator.cs create mode 100644 JudoPayDotNet/Validation/PaymentRequiresThreeDSecureTwoValidator.cs delete mode 100644 JudoPayDotNet/Validation/PolymorphicValidator.cs delete mode 100644 JudoPayDotNetTests/Model/Validations/PaymentReceiptValidation.cs delete mode 100644 JudoPayDotNetTests/Model/Validations/PaymentRequiresThreeDSecureTwoModelValidator.cs delete mode 100644 JudoPayDotNetTests/Model/Validations/TransactionResultBaseValidation.cs delete mode 100644 JudoPayDotNetTests/Model/Validations/TransactionResultValidation.cs diff --git a/JudoPayDotNet/JudoPayDotNet.csproj b/JudoPayDotNet/JudoPayDotNet.csproj index bde64b7..3c73be0 100644 --- a/JudoPayDotNet/JudoPayDotNet.csproj +++ b/JudoPayDotNet/JudoPayDotNet.csproj @@ -20,7 +20,7 @@ - + diff --git a/JudoPayDotNet/Validation/PaymentReceiptValidator.cs b/JudoPayDotNet/Validation/PaymentReceiptValidator.cs new file mode 100644 index 0000000..3d18940 --- /dev/null +++ b/JudoPayDotNet/Validation/PaymentReceiptValidator.cs @@ -0,0 +1,20 @@ +using FluentValidation; +using JudoPayDotNet.Models; + +namespace JudoPayDotNet.Validation +{ + internal class PaymentReceiptValidator : AbstractValidator + { + public PaymentReceiptValidator() + { + RuleFor(model => model.ReceiptId) + .NotEmpty().WithMessage("The response must contain a receipt ID"); + + RuleFor(model => model.Type) + .NotEmpty().WithMessage("The response must contain an Type"); + + RuleFor(model => model.JudoId) + .NotEmpty().WithMessage("The response must contain an Judo Id"); + } + } +} diff --git a/JudoPayDotNet/Validation/PaymentRequiresThreeDSecureTwoValidator.cs b/JudoPayDotNet/Validation/PaymentRequiresThreeDSecureTwoValidator.cs new file mode 100644 index 0000000..3ccb605 --- /dev/null +++ b/JudoPayDotNet/Validation/PaymentRequiresThreeDSecureTwoValidator.cs @@ -0,0 +1,23 @@ +using FluentValidation; +using JudoPayDotNet.Models; + +namespace JudoPayDotNet.Validation +{ + internal class PaymentRequiresThreeDSecureTwoValidator : AbstractValidator + { + public PaymentRequiresThreeDSecureTwoValidator() + { + RuleFor(model => model.ReceiptId) + .NotEmpty().WithMessage("The response must contain a receipt ID"); + + RuleFor(model => model.MethodUrl) + .NotEmpty().WithMessage("The response must contain a MethodUrl"); + + RuleFor(model => model.Version) + .NotEmpty().WithMessage("The response must contain a Version"); + + RuleFor(model => model.Md) + .NotEmpty().WithMessage("The response must contain a Md"); + } + } +} \ No newline at end of file diff --git a/JudoPayDotNet/Validation/PolymorphicValidator.cs b/JudoPayDotNet/Validation/PolymorphicValidator.cs deleted file mode 100644 index 19f377d..0000000 --- a/JudoPayDotNet/Validation/PolymorphicValidator.cs +++ /dev/null @@ -1,63 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using FluentValidation; -using FluentValidation.Results; -using FluentValidation.Validators; - -namespace JudoPayDotNet.Validation -{ - internal class PolymorphicValidator : NoopPropertyValidator where TBaseClass : class - { - readonly Dictionary _derivedValidators = new Dictionary(); - readonly IValidator _baseValidator; - - public PolymorphicValidator(IValidator baseValidator) - { - _baseValidator = baseValidator; - } - - private IEnumerable ActualValidation(TBaseClass value) - { - // bail out if the property is null or the collection is empty - if (value == null) return Enumerable.Empty(); - - // get the first element out of the collection and check its real type. - var actualType = value.GetType(); - - IValidator derivedValidator; - - if (_derivedValidators.TryGetValue(actualType, out derivedValidator)) - { - var context = new ValidationContext(value); - // we found a validator for the specific subclass. - return derivedValidator.Validate(context).Errors; - } - - // Otherwise fall back to the validator for the base class. - return _baseValidator.Validate(value).Errors; - } - - - public PolymorphicValidator Add(IValidator derivedValidator) where TDerived : TBaseClass - { - _derivedValidators[typeof(TDerived)] = derivedValidator; - return this; - } - -// ReSharper disable UnusedMethodReturnValue.Global - public IEnumerable Validate(TBaseClass value) -// ReSharper restore UnusedMethodReturnValue.Global - { - return ActualValidation(value); - } - - public override IEnumerable Validate(PropertyValidatorContext context) - { - var value = context.PropertyValue as TBaseClass; - - return ActualValidation(value); - } - } - -} diff --git a/JudoPayDotNetTests/Model/Validations/PaymentReceiptValidation.cs b/JudoPayDotNetTests/Model/Validations/PaymentReceiptValidation.cs deleted file mode 100644 index 87beda9..0000000 --- a/JudoPayDotNetTests/Model/Validations/PaymentReceiptValidation.cs +++ /dev/null @@ -1,17 +0,0 @@ -using FluentValidation; -using JudoPayDotNet.Models; - -namespace JudoPayDotNetTests.Model.Validations -{ - internal class PaymentReceiptValidation : TransactionResultBaseValidation - { - public PaymentReceiptValidation() - { - RuleFor(model => model.Type) - .NotEmpty().WithMessage("The response must contain an Type"); - - RuleFor(model => model.JudoId) - .NotEmpty().WithMessage("The response must contain an Judo Id"); - } - } -} \ No newline at end of file diff --git a/JudoPayDotNetTests/Model/Validations/PaymentRequiresThreeDSecureTwoModelValidator.cs b/JudoPayDotNetTests/Model/Validations/PaymentRequiresThreeDSecureTwoModelValidator.cs deleted file mode 100644 index 9b66dae..0000000 --- a/JudoPayDotNetTests/Model/Validations/PaymentRequiresThreeDSecureTwoModelValidator.cs +++ /dev/null @@ -1,20 +0,0 @@ -using FluentValidation; -using JudoPayDotNet.Models; - -namespace JudoPayDotNetTests.Model.Validations -{ - internal class PaymentRequiresThreeDSecureTwoModelValidator : TransactionResultBaseValidation - { - public PaymentRequiresThreeDSecureTwoModelValidator() - { - RuleFor(model => model.MethodUrl) - .NotEmpty().WithMessage("The response must contain a MethodUrl"); - - RuleFor(model => model.Version) - .NotEmpty().WithMessage("The response must contain a Version"); - - RuleFor(model => model.Md) - .NotEmpty().WithMessage("The response must contain a Md"); - } - } -} \ No newline at end of file diff --git a/JudoPayDotNetTests/Model/Validations/TransactionResultBaseValidation.cs b/JudoPayDotNetTests/Model/Validations/TransactionResultBaseValidation.cs deleted file mode 100644 index 9de94f0..0000000 --- a/JudoPayDotNetTests/Model/Validations/TransactionResultBaseValidation.cs +++ /dev/null @@ -1,14 +0,0 @@ -using FluentValidation; -using JudoPayDotNet.Models; - -namespace JudoPayDotNetTests.Model.Validations -{ - internal class TransactionResultBaseValidation : AbstractValidator where TTransactionResult : ITransactionResult - { - protected TransactionResultBaseValidation() - { - RuleFor(model => model.ReceiptId) - .NotEmpty().WithMessage("The response must contain a receipt ID"); - } - } -} \ No newline at end of file diff --git a/JudoPayDotNetTests/Model/Validations/TransactionResultValidation.cs b/JudoPayDotNetTests/Model/Validations/TransactionResultValidation.cs deleted file mode 100644 index 305f8e4..0000000 --- a/JudoPayDotNetTests/Model/Validations/TransactionResultValidation.cs +++ /dev/null @@ -1,8 +0,0 @@ -using JudoPayDotNet.Models; - -namespace JudoPayDotNetTests.Model.Validations -{ - internal class TransactionResultValidation : TransactionResultBaseValidation - { - } -} diff --git a/JudoPayDotNetTests/Validation/PaymentsValidationTests.cs b/JudoPayDotNetTests/Validation/PaymentsValidationTests.cs index 6fd7d08..e90b686 100644 --- a/JudoPayDotNetTests/Validation/PaymentsValidationTests.cs +++ b/JudoPayDotNetTests/Validation/PaymentsValidationTests.cs @@ -15,7 +15,9 @@ public void ValidatePkPaymentWithErrorOnSpecificCardPaymentInformation() var validator = new PKPaymentValidator(); - validator.ShouldHaveValidationErrorFor(model => model.PkPayment, payment); + var result = validator.TestValidate(payment); + + result.ShouldHaveValidationErrorFor(model => model.PkPayment); } } } diff --git a/JudoPayDotNetTests/Validation/TransactionResultValidationTests.cs b/JudoPayDotNetTests/Validation/TransactionResultValidationTests.cs index 834b30d..82c259d 100644 --- a/JudoPayDotNetTests/Validation/TransactionResultValidationTests.cs +++ b/JudoPayDotNetTests/Validation/TransactionResultValidationTests.cs @@ -1,7 +1,6 @@ using System.Linq; using JudoPayDotNet.Models; using JudoPayDotNet.Validation; -using JudoPayDotNetTests.Model.Validations; using NUnit.Framework; namespace JudoPayDotNetTests.Validation @@ -12,39 +11,37 @@ public class TransactionResultValidationTests [Test] public void ValidateATransactionResult() { - ITransactionResult transactionResult = new PaymentReceiptModel(); + var receipt = new PaymentReceiptModel(); - var validator = new PolymorphicValidator(new TransactionResultValidation()) - // ReSharper disable RedundantTypeArgumentsOfMethod - .Add(new PaymentReceiptValidation()); - // ReSharper restore RedundantTypeArgumentsOfMethod + var validator = new PaymentReceiptValidator(); - var result = validator.Validate(transactionResult); + var result = validator.Validate(receipt); Assert.IsNotNull(result); - var inner = result.First(); + Assert.IsFalse(result.IsValid); + + var firstError = result.Errors.First(); - Assert.IsTrue(inner.PropertyName == "ReceiptId"); + Assert.IsTrue(firstError.PropertyName == "ReceiptId"); } [Test] public void ValidateAThreeDSecureTwoTransactionResult() { - ITransactionResult transactionResult = new PaymentRequiresThreeDSecureTwoModel(); + var transactionResult = new PaymentRequiresThreeDSecureTwoModel(); - var validator = new PolymorphicValidator(new TransactionResultValidation()) - // ReSharper disable RedundantTypeArgumentsOfMethod - .Add(new PaymentRequiresThreeDSecureTwoModelValidator()); - // ReSharper restore RedundantTypeArgumentsOfMethod + var validator = new PaymentRequiresThreeDSecureTwoValidator(); var result = validator.Validate(transactionResult); Assert.IsNotNull(result); - var inner = result.First(); + Assert.IsFalse(result.IsValid); + + var firstError = result.Errors.First(); - Assert.IsTrue(inner.PropertyName == "ReceiptId"); + Assert.IsTrue(firstError.PropertyName == "ReceiptId"); } } }