public void Validation_Method_Valid_No_Parameters()
        {
            ValTestClass      vtc     = new ValTestClass();
            ValidationContext context = ValidationUtilities.CreateValidationContext(vtc, null);

            object[] parameters = null;
            bool     isValid    = ValidationUtilities.TryValidateCustomUpdateMethodCall("MethodWithNoParameters", context, parameters, null);

            Assert.IsTrue(isValid);

            List <ValidationResult> output = new List <ValidationResult>();

            isValid = ValidationUtilities.TryValidateCustomUpdateMethodCall("MethodWithNoParameters", context, parameters, output);
            Assert.IsTrue(isValid);
            Assert.AreEqual(0, output.Count);
        }
        public void Validation_Method_No_Attributes()
        {
            ValTestNoAttributesClass instance = new ValTestNoAttributesClass();
            ValidationContext        context  = ValidationUtilities.CreateValidationContext(instance, null);

            object[] parameters = { "hello", 2.0 };
            bool     isValid    = ValidationUtilities.TryValidateCustomUpdateMethodCall("MethodWithoutAttributes", context, parameters, null);

            Assert.IsTrue(isValid);

            List <ValidationResult> output = new List <ValidationResult>();

            isValid = ValidationUtilities.TryValidateCustomUpdateMethodCall("MethodWithoutAttributes", context, parameters, output);
            Assert.IsTrue(isValid);
            Assert.AreEqual(0, output.Count);
        }
        public void Validation_Method_Invalid_No_Params()
        {
            ValTestClass vtc = new ValTestClass();

            vtc._failMethod = true; // forces failure
            ValidationContext context = ValidationUtilities.CreateValidationContext(vtc, null);

            ExceptionHelper.ExpectException <MissingMethodException>(delegate()
            {
                ValidationUtilities.TryValidateCustomUpdateMethodCall("MethodWithParameters", context, null, null);
            }, "Method 'OpenRiaServices.DomainServices.Hosting.Test.ValidationUtilitiesTests+ValTestClass.MethodWithParameters' accepting zero parameters could not be found.");

            ExceptionHelper.ExpectException <MissingMethodException>(delegate()
            {
                ValidationUtilities.TryValidateCustomUpdateMethodCall("MethodWithParameters", context, null, new List <ValidationResult>());
            }, "Method 'OpenRiaServices.DomainServices.Hosting.Test.ValidationUtilitiesTests+ValTestClass.MethodWithParameters' accepting zero parameters could not be found.");
        }
        public void Validation_Method_Invalid_Null_Params()
        {
            ValTestClass      vtc     = new ValTestClass();
            ValidationContext context = ValidationUtilities.CreateValidationContext(vtc, null);

            object[] parameters = null; // actually requires 2 params

            // IsValid entry point
            ExceptionHelper.ExpectException <MissingMethodException>(delegate()
            {
                ValidationUtilities.TryValidateCustomUpdateMethodCall("MethodWithParameters", context, parameters, null);
            }, "Method 'OpenRiaServices.DomainServices.Hosting.Test.ValidationUtilitiesTests+ValTestClass.MethodWithParameters' accepting zero parameters could not be found.");

            // Validate entry point
            ExceptionHelper.ExpectException <MissingMethodException>(delegate()
            {
                ValidationUtilities.ValidateMethodCall("MethodWithParameters", context, parameters);
            }, "Method 'OpenRiaServices.DomainServices.Hosting.Test.ValidationUtilitiesTests+ValTestClass.MethodWithParameters' accepting zero parameters could not be found.");
        }
        public void Validation_Fail_Method_Null_ValueType()
        {
            ValTestClass      vtc     = new ValTestClass();
            ValidationContext context = ValidationUtilities.CreateValidationContext(vtc, null);

            object[] parameters = { "xxx", null }; // 2nd param should be double

            // IsValid entry point
            ExceptionHelper.ExpectException <MissingMethodException>(delegate()
            {
                ValidationUtilities.TryValidateCustomUpdateMethodCall("MethodWithParameters", context, parameters, null);
            }, "Method 'OpenRiaServices.DomainServices.Hosting.Test.ValidationUtilitiesTests+ValTestClass.MethodWithParameters('System.String', null)' could not be found. Parameter count: 2.");

            // Validate entry point
            ExceptionHelper.ExpectException <MissingMethodException>(delegate()
            {
                ValidationUtilities.ValidateMethodCall("MethodWithParameters", context, parameters);
            }, "Method 'OpenRiaServices.DomainServices.Hosting.Test.ValidationUtilitiesTests+ValTestClass.MethodWithParameters('System.String', null)' could not be found. Parameter count: 2.");
        }
        public void Validation_Fail_Method_Too_Many_Params()
        {
            ValTestClass      vtc     = new ValTestClass();
            ValidationContext context = ValidationUtilities.CreateValidationContext(vtc, null);

            object[] parameters = { "xxx", 2.0, 5 }; // actually requires 2 params

            // IsValid entry point
            ExceptionHelper.ExpectException <MissingMethodException>(delegate()
            {
                ValidationUtilities.TryValidateCustomUpdateMethodCall("MethodWithParameters", context, parameters, null);
            }, "Method 'OpenRiaServices.DomainServices.Hosting.Test.ValidationUtilitiesTests+ValTestClass.MethodWithParameters('System.String', 'System.Double', 'System.Int32')' could not be found. Parameter count: 3.");

            // Validate entry point
            ExceptionHelper.ExpectException <MissingMethodException>(delegate()
            {
                ValidationUtilities.ValidateMethodCall("MethodWithParameters", context, parameters);
            }, "Method 'OpenRiaServices.DomainServices.Hosting.Test.ValidationUtilitiesTests+ValTestClass.MethodWithParameters('System.String', 'System.Double', 'System.Int32')' could not be found. Parameter count: 3.");
        }
        public void Validation_Method_Invalid_Null_Required_Nullable()
        {
            ValTestClass      vtc     = new ValTestClass();
            ValidationContext context = ValidationUtilities.CreateValidationContext(vtc, null);
            string            message = "The doubleParam field is required.";

            object[] parameters = { null }; // param is nullable but marked [Required]
            ExceptionHelper.ExpectValidationException(delegate
            {
                ValidationUtilities.TryValidateCustomUpdateMethodCall("MethodWithRequiredNullableParameter", context, parameters, null);
            }, message, typeof(RequiredAttribute), parameters[0]);

            List <ValidationResult> output = new List <ValidationResult>();
            bool isValid = ValidationUtilities.TryValidateCustomUpdateMethodCall("MethodWithRequiredNullableParameter", context, parameters, output);

            Assert.IsFalse(isValid);
            Assert.AreEqual(1, output.Count);
            UnitTestHelper.AssertListContains(output, message);
        }
        public void Validation_Method_Invalid_Bad_Params()
        {
            ValTestClass      vtc     = new ValTestClass();
            ValidationContext context = ValidationUtilities.CreateValidationContext(vtc, null);
            string            message = "The field FirstParameterDisplayName must be a string with a maximum length of 5.";

            object[] parameters = { "LongerThan5Chars", 2.0 }; // legal params
            ExceptionHelper.ExpectValidationException(delegate()
            {
                ValidationUtilities.TryValidateCustomUpdateMethodCall("MethodWithParameters", context, parameters, null);
            }, message, typeof(StringLengthAttribute), parameters[0]);

            List <ValidationResult> output = new List <ValidationResult>();
            bool isValid = ValidationUtilities.TryValidateCustomUpdateMethodCall("MethodWithParameters", context, parameters, output);

            Assert.IsFalse(isValid);
            Assert.AreEqual(1, output.Count);
            UnitTestHelper.AssertListContains(output, message);
        }
        public void Validation_Method_Errors_Must_Match_Expected_Method_Description()
        {
            ValTestClass instance = new ValTestClass()
            {
                _failMethod = true
            };
            ValidationContext       context = new ValidationContext(instance, null, null);
            MethodInfo              method  = typeof(ValTestClass).GetMethod("MethodWithNoParameters");
            List <ValidationResult> results = new List <ValidationResult>();

            ExceptionHelper.ExpectException <ValidationException>(delegate
            {
                ValidationUtilities.ValidateMethodCall("MethodWithNoParameters", context, null);
            }, "-MethodWithNoParameters");

            bool result = ValidationUtilities.TryValidateCustomUpdateMethodCall("MethodWithNoParameters", context, null, results);

            Assert.AreEqual(1, results.Count);
            Assert.AreEqual("-MethodWithNoParameters", results[0].ErrorMessage);
        }
        public void Validation_Method_Invalid_Good_Params()
        {
            ValTestClass      vtc     = new ValTestClass();
            ValidationContext context = ValidationUtilities.CreateValidationContext(vtc, null);
            string            message = "-MethodDisplayName";

            vtc._failMethod = true;                 // tells validation method below to fail the IsValid

            object[] parameters = { "hello", 2.0 }; // legal params
            ExceptionHelper.ExpectValidationException(delegate
            {
                ValidationUtilities.TryValidateCustomUpdateMethodCall("MethodWithParameters", context, parameters, null);
            }, message, typeof(CustomValidationAttribute), vtc);

            List <ValidationResult> output = new List <ValidationResult>();
            bool isValid = ValidationUtilities.TryValidateCustomUpdateMethodCall("MethodWithParameters", context, parameters, output);

            Assert.IsFalse(isValid);
            Assert.AreEqual(1, output.Count);
            UnitTestHelper.AssertListContains(output, message);    // hyphen means it came from default formatter
        }