Example #1
0
        public void Merge_WithValidationCollectors()
        {
            var collector1 = new TypeWithoutBaseTypeCollector1();
            var collector2 = new TypeWithoutBaseTypeCollector2();
            var validationCollectorInfos = new[]
            {
                new[]
                {
                    new ValidationCollectorInfo(
                        collector1,
                        typeof(ValidationAttributesBasedCollectorProvider))
                },
                new[] { new ValidationCollectorInfo(collector2, typeof(ApiBasedComponentValidationCollectorProvider)) }
            };

            var userNameExpression = ExpressionHelper.GetTypedMemberExpression <Customer, string> (c => c.UserName);
            var lastNameExpression = ExpressionHelper.GetTypedMemberExpression <Customer, string> (c => c.LastName);
            var stubValidator1     = new NotNullValidator();
            var stubValidator2     = new NotEmptyValidator(null);
            var stubValidator3     = new NotEqualValidator("test");
            var stubValidator4     = new StubPropertyValidator();

            _validatorFormatterStub.Stub(
                stub => stub.Format(Arg <IPropertyValidator> .Matches(c => c.GetType() == typeof(NotNullValidator)), Arg <Func <Type, string> > .Is.Anything))
            .Return("FluentValidation.Validators.NotNullValidator");
            _validatorFormatterStub.Stub(
                stub => stub.Format(Arg <IPropertyValidator> .Matches(c => c.GetType() == typeof(LengthValidator)), Arg <Func <Type, string> > .Is.Anything))
            .Return("FluentValidation.Validators.LengthValidator");
            _validatorFormatterStub.Stub(
                stub => stub.Format(Arg <IPropertyValidator> .Matches(c => c.GetType() == typeof(NotEmptyValidator)), Arg <Func <Type, string> > .Is.Anything))
            .Return("FluentValidation.Validators.NotEmptyValidator");
            _validatorFormatterStub.Stub(
                stub => stub.Format(Arg <IPropertyValidator> .Matches(c => c.GetType() == typeof(NotEqualValidator)), Arg <Func <Type, string> > .Is.Anything))
            .Return("FluentValidation.Validators.NotEqualValidator");
            _validatorFormatterStub.Stub(
                stub =>
                stub.Format(Arg <IPropertyValidator> .Matches(c => c.GetType() == typeof(StubPropertyValidator)), Arg <Func <Type, string> > .Is.Anything))
            .Return("Remotion.Validation.UnitTests.TestHelpers.StubPropertyValidator");

            var userNamePropertyRule = PropertyRule.Create(userNameExpression);

            userNamePropertyRule.AddValidator(stubValidator1);
            userNamePropertyRule.AddValidator(stubValidator1);
            userNamePropertyRule.AddValidator(stubValidator2);
            var lastNamePropertyRule = PropertyRule.Create(lastNameExpression);

            lastNamePropertyRule.AddValidator(stubValidator3);

            var noPropertyRuleStub = new ValidationRuleStub();

            noPropertyRuleStub.AddValidator(stubValidator4);

            var removingPropertyRuleStub1 = MockRepository.GenerateStub <IRemovingComponentPropertyRule>();

            removingPropertyRuleStub1.Stub(stub => stub.CollectorType).Return(typeof(CustomerValidationCollector1));
            var removingPropertyRuleStub2 = MockRepository.GenerateStub <IRemovingComponentPropertyRule>();

            removingPropertyRuleStub2.Stub(stub => stub.CollectorType).Return(typeof(CustomerValidationCollector2));

            var logContextInfo1 = new LogContextInfo(
                stubValidator2,
                new[]
            {
                new ValidatorRegistrationWithContext(new ValidatorRegistration(typeof(NotEmptyValidator), null), removingPropertyRuleStub1),
                new ValidatorRegistrationWithContext(new ValidatorRegistration(typeof(NotEmptyValidator), null), removingPropertyRuleStub1),
                new ValidatorRegistrationWithContext(new ValidatorRegistration(typeof(NotEmptyValidator), null), removingPropertyRuleStub2)
            });
            var logContextInfo2 = new LogContextInfo(
                stubValidator1,
                new[] { new ValidatorRegistrationWithContext(new ValidatorRegistration(typeof(NotNullValidator), null), removingPropertyRuleStub2) });
            var logContextInfo3 = new LogContextInfo(
                stubValidator3,
                new[] { new ValidatorRegistrationWithContext(new ValidatorRegistration(typeof(NotEqualValidator), null), removingPropertyRuleStub1) });

            _logContextStub.Stub(stub => stub.GetLogContextInfos(userNamePropertyRule)).Return(new[] { logContextInfo1, logContextInfo2 });
            _logContextStub.Stub(stub => stub.GetLogContextInfos(lastNamePropertyRule)).Return(new[] { logContextInfo3 });
            _logContextStub.Stub(stub => stub.GetLogContextInfos(noPropertyRuleStub)).Return(new LogContextInfo[0]);

            var fakeValidationRules = new IValidationRule[] { userNamePropertyRule, lastNamePropertyRule, noPropertyRuleStub };

            _wrappedMergerStub.Stub(
                stub =>
                stub.Merge(
                    validationCollectorInfos)).Return(new ValidationCollectorMergeResult(fakeValidationRules, _logContextStub));

            var expectedAfterMerge =
                "\r\nAFTER MERGE:"
                + "\r\n\r\n    -> Remotion.Validation.UnitTests.TestDomain.Customer#UserName"
                + "\r\n        VALIDATORS:"
                + "\r\n        -> FluentValidation.Validators.NotNullValidator (x2)"
                + "\r\n        -> FluentValidation.Validators.NotEmptyValidator (x1)"
                + "\r\n        MERGE LOG:"
                + "\r\n        -> 'FluentValidation.Validators.NotEmptyValidator' was removed from collectors 'CustomerValidationCollector1, CustomerValidationCollector2'"
                + "\r\n        -> 'FluentValidation.Validators.NotNullValidator' was removed from collector 'CustomerValidationCollector2'"
                + "\r\n\r\n    -> Remotion.Validation.UnitTests.TestDomain.Person#LastName"
                + "\r\n        VALIDATORS:"
                + "\r\n        -> FluentValidation.Validators.NotEqualValidator (x1)"
                + "\r\n        MERGE LOG:"
                + "\r\n        -> 'FluentValidation.Validators.NotEqualValidator' was removed from collector 'CustomerValidationCollector1'"
                + "\r\n\r\n    -> Remotion.Validation.UnitTests.Implementation.ValidationRuleStub"
                + "\r\n        VALIDATORS:"
                + "\r\n        -> Remotion.Validation.UnitTests.TestHelpers.StubPropertyValidator (x1)";

            CheckLoggingMethod(() => _diagnosticOutputRuleMergeDecorator.Merge(validationCollectorInfos), expectedAfterMerge, 0);

            var expectedBeforeMerge =
                "\r\nBEFORE MERGE:"
                + "\r\n\r\n-> Remotion.Validation.Providers.ValidationAttributesBasedCollectorProvider#Remotion.Validation.UnitTests.Implementation.TestDomain.TypeWithoutBaseTypeCollector1"
                + "\r\n\r\n    -> Remotion.Validation.UnitTests.Implementation.TestDomain.TypeWithoutBaseType#Property1"
                + "\r\n        ADDED HARD CONSTRAINT VALIDATORS:"
                + "\r\n        -> FluentValidation.Validators.NotNullValidator (x1)"
                + "\r\n        -> FluentValidation.Validators.NotEqualValidator (x1)"
                + "\r\n\r\n    -> Remotion.Validation.UnitTests.Implementation.TestDomain.TypeWithoutBaseType#Property2"
                + "\r\n        ADDED SOFT CONSTRAINT VALIDATORS:"
                + "\r\n        -> FluentValidation.Validators.LengthValidator (x1)"
                + "\r\n        ADDED META VALIDATION RULES:"
                + "\r\n        -> Remotion.Validation.UnitTests.TestDomain.ValidationRules.MaxLengthMetaValidationRule"
                + "\r\n\r\n-> Remotion.Validation.Providers.ApiBasedComponentValidationCollectorProvider#Remotion.Validation.UnitTests.Implementation.TestDomain.TypeWithoutBaseTypeCollector2"
                + "\r\n\r\n    -> Remotion.Validation.UnitTests.Implementation.TestDomain.TypeWithoutBaseType#Property2"
                + "\r\n        REMOVED VALIDATORS:"
                + "\r\n        -> FluentValidation.Validators.NotEmptyValidator (x1)"
                + "\r\n        -> FluentValidation.Validators.MaximumLengthValidator#Remotion.Validation.UnitTests.Implementation.TestDomain.TypeWithoutBaseTypeCollector1 (x1)";

            CheckLoggingMethod(() => _diagnosticOutputRuleMergeDecorator.Merge(validationCollectorInfos), expectedBeforeMerge, 1);
        }