public void TestSyncValidationModelForValidResultsShouldBeSucceed()
 {
     var targetModel = new FakeValidationTargetModel();
     ValidationModelTestTemplate(
         targetModel,
         arrange: (model, mockExecutor) =>
         {
             mockExecutor
             .Setup(x => x.Execute(model, It.IsAny<IEnumerable<ValidatorContainer<FakeValidationTargetModel>>>()))
             .Returns(() => new List<ValidationResult> { CreateValidationResult(true) });
         },
         act: SyncValidationModelAct,
         assert: (result, mockExecutor) => CreateGenericAssert(true)(result, mockExecutor));
 }
 public void TestSyncValidationModelForInvalidValidatorShouldBeFailed()
 {
     var targetModel = new FakeValidationTargetModel();
     var failedRuleId = Guid.NewGuid();
     ValidationModelTestTemplate(
         targetModel,
         arrange: (model, mockExecutor) =>
         {
             mockExecutor
             .Setup(x => x.Execute(model, It.IsAny<IEnumerable<ValidatorContainer<FakeValidationTargetModel>>>()))
             .Returns(() => new List<ValidationResult> { CreateValidationResult(false, failedRuleId) });
         },
         act: SyncValidationModelAct,
         assert: (result, mockExecutor) => CreateGenericAssert(false, CreateSingleResultAssertion(failedRuleId))(result, mockExecutor));
         
 }
        public void TestExecuteSuncWithTwoInvalidValidatorWithDifferentPriorityOneValidatorShouldBeFailed()
        {
            var fakeModel = new FakeValidationTargetModel();
            var failedValidatorId = Guid.NewGuid();
            var firstMockValidator = ValidatorExecutorTestHelper.CreateMockValidator(false, Guid.NewGuid());
            var secondMockValidator = ValidatorExecutorTestHelper.CreateMockValidator(false, failedValidatorId);
            var validators = new List<ValidatorContainer<FakeValidationTargetModel>>
            {
                ValidatorExecutorTestHelper.CreateValidatorContainer(firstMockValidator.Object, 1),
                ValidatorExecutorTestHelper.CreateValidatorContainer(secondMockValidator.Object, 0)
            };

            var target = new HierarchicalValidatorExecutor<FakeValidationTargetModel>();

            IEnumerable<ValidationResult> validationResults = target.Execute(fakeModel, validators);

            Assert.IsNotNull(validationResults);
            var failedValidator = validationResults.FirstOrDefault();
            Assert.IsNotNull(failedValidator);
            Assert.IsFalse(failedValidator.IsValid());
            Assert.AreEqual(failedValidatorId, failedValidator.Id);
            secondMockValidator.Verify();
            firstMockValidator.Verify(x => x.Validate(It.IsAny<FakeValidationTargetModel>()), Times.Never);
        }
 private AggregateValidationResult AsyncValidationModelAct(
    FakeValidationTargetModel model,
    IValidatorExecutor<FakeValidationTargetModel> executor)
 {
     var target = new GenericValidationModel<FakeValidationTargetModel>(executor, CreateFakeConfig());
     return target.ValidateAsync(model).Result;
 }
 private void ValidationModelTestTemplate(
     FakeValidationTargetModel model,
     Action<FakeValidationTargetModel,
         Mock<IValidatorExecutor<FakeValidationTargetModel>>> arrange,
     Func<FakeValidationTargetModel,
         IValidatorExecutor<FakeValidationTargetModel>,
         AggregateValidationResult> act,
     Action<AggregateValidationResult,
         Mock<IValidatorExecutor<FakeValidationTargetModel>>> assert)
 {
     var mockValidatorExecutor = new Mock<IValidatorExecutor<FakeValidationTargetModel>>();
     arrange(model, mockValidatorExecutor);
     var result = act(model, mockValidatorExecutor.Object);
     assert(result, mockValidatorExecutor);
 }
 public void TestAsyncValidationModelForEmptyResultsShouldBeSucceed()
 {
     var targetModel = new FakeValidationTargetModel();
     var failedRuleId = Guid.NewGuid();
     ValidationModelTestTemplate(
         targetModel,
         arrange: (model, mockExecutor) =>
         {
             mockExecutor
             .Setup(x => x.ExecuteAsync(model, It.IsAny<IEnumerable<ValidatorContainer<FakeValidationTargetModel>>>()))
             .Returns(() => Task.FromResult(Enumerable.Empty<ValidationResult>() as IEnumerable<ValidationResult>));
         },
         act: AsyncValidationModelAct,
         assert: (result, mockExecutor) => CreateGenericAssert(true)(result, mockExecutor));
 }
        public void TestExecuteSuncWithValidValidatorShouldBeSucceed()
        {
            var fakeModel = new FakeValidationTargetModel();
            var mockValidator = ValidatorExecutorTestHelper.CreateMockValidator(true, Guid.NewGuid());
            
            var validators = new List<ValidatorContainer<FakeValidationTargetModel>>
            {
                ValidatorExecutorTestHelper.CreateValidatorContainer(mockValidator.Object, 0)
            };

            var target = new HierarchicalValidatorExecutor<FakeValidationTargetModel>();

            IEnumerable<ValidationResult> validationResults = target.Execute(fakeModel, validators);

            Assert.IsNotNull(validationResults);
            var failedValidatorTask = validationResults.SingleOrDefault();
            Assert.IsNotNull(failedValidatorTask);
            var failedValidator = failedValidatorTask;
            Assert.IsNotNull(failedValidator);
            Assert.IsTrue(failedValidator.IsValid());
            mockValidator.Verify();
        }