public void MinLengthTest()
        {
            var obj1 = new MinLengthTestClass()
            {
                MinLengthOf3 = "1234",
                anotherStr   = "123"
            };

            Assert.IsTrue(SwagValidator.Validate(obj1));

            try
            {
                var obj2 = new MinLengthTestClass()
                {
                    MinLengthOf3 = "12" //not long enough
                };

                SwagValidator.Validate(obj2); //should throw exception
                Assert.Fail();                // should not reach here because empty string threw exception
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex.Message.Contains("has length 2.  Minlength is 3"));
            }
        }
        public void TestRegExFilePath()
        {
            try
            {
                //Valid value
                var obj = new RegExFilePathTestClass()
                {
                    Text = @"D:\server\tshare\folder\myfile.txt"
                };
                Assert.IsTrue(SwagValidator.Validate(obj));

                obj.Text = @"\\server1\Folder\File.txt";
                Assert.IsTrue(SwagValidator.Validate(obj));

                obj.Text = "\\\\server1\\Folder\\File.txt";
                Assert.IsTrue(SwagValidator.Validate(obj));

                obj.Text = "D:\\server\\tshare\\folder\\myfile.txt";
                Assert.IsTrue(SwagValidator.Validate(obj));

                //Invalid Value
                obj.Text = "1098.3456.33.44";
                SwagValidator.Validate(obj);
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex.Message.Contains("is invalid. Received value"));
            }
        }
        public void TestRequiredFieldHappyPathForInheritedClass()
        {
            RequiredFieldBase inheritedObject = new RequiredFieldDerived()
            {
                BaseStringType     = "Payments",
                DerivedIntegerType = 10
            };

            Assert.IsTrue(SwagValidator.Validate(inheritedObject));
        }
        public void ObjectWithNestedClassNotRequired()
        {
            var obj1 = new SomeObjectWithNestedClassWithoutRequired()
            {
                SomethingElse         = "something",
                ThisIsNotARequiredObj = null  //since this is not required this should not throw error
            };

            Assert.IsTrue(SwagValidator.Validate(obj1));
        }
        public void RegexTest()
        {
            var obj = new RegularExpressionAttrTest()
            {
                Country = "uS"
            };

            var res = SwagValidator.Validate(obj); //should throw exception

            Assert.IsTrue(res);
        }
        public void RequiredStringListWithValueTest()
        {
            var obj3 = new ObjWithRequiredList()
            {
                RequiredStringCollection = new List <string>()
                {
                    "Test"
                }
            };

            Assert.IsTrue(SwagValidator.Validate(obj3));
        }
        public void OptioanlStringListWithValueTest()
        {
            var obj1 = new ObjWithOptionalList()
            {
                OptionalStringCollection = new List <string>()
                {
                    "Test"
                },
            };

            Assert.IsTrue(SwagValidator.Validate(obj1));
        }
        public void RequiredObjectListWithOutValueTest()
        {
            try
            {
                var obj8 = new ComplexTypeWithRequiredList(); //empty list test

                SwagValidator.Validate(obj8);                 //should throw exception
                Assert.Fail();                                // should not reach here because empty string threw exception
            }
            catch (Exception ex)
            {
                Assert.AreEqual("Value cannot be null. Parameter name: RequiredObject", ex.Message);
            }
        }
        public void RequiredObjectListWithValueTest()
        {
            var obj7 = new ComplexTypeWithRequiredList
            {
                RequiredObject = new List <slot>()
                {
                    new slot()
                    {
                        MyProperty1 = "value"
                    }
                }
            };

            Assert.IsTrue(SwagValidator.Validate(obj7));
        }
        public void TestRequiredFieldHappyPath()
        {
            m_TestObject = new RequiredFieldTestObject()
            {
                DecimalType         = 10,
                IntegerType         = 10,
                GuidType            = Guid.NewGuid(),
                NullableIntegerType = 20,
                NullableDecimalType = 20,
                StringType          = "Payments",
                NullableGuidType    = Guid.NewGuid()
            };

            Assert.IsTrue(SwagValidator.Validate(m_TestObject));
        }
        public void OptioanlObjectListWithValueTest()
        {
            var obj5 = new ComplexTypeWithOptionalList()
            {
                OptionalObject = new List <slot>()
                {
                    new slot()
                    {
                        MyProperty1 = "value"
                    }
                }
            };

            Assert.IsTrue(SwagValidator.Validate(obj5));
        }
Ejemplo n.º 12
0
        public SoMuch AddFunsxx(SoMuch soMuch)
        {
            //var results = new List<ValidationResult>();
            //var isValid = Validator.TryValidateObject(soMuch, new ValidationContext(soMuch, null, null), results, true);
            //Console.WriteLine("Validation resulted in " + isValid);

            //isValid = Validator.TryValidateObject(soMuch.NestedObjTest, new ValidationContext(soMuch.NestedObjTest, null, null), results, true);
            //Console.WriteLine("Validation resulted in " + isValid);

            //Validator.ValidateObject(soMuch, new ValidationContext(soMuch, null, null));
            //Validator.ValidateObject(soMuch.NestedObjTest, new ValidationContext(soMuch.NestedObjTest, null, null));

            SwagValidator.Validate(soMuch);

            throw new NotImplementedException();
        }
        public void RegexTestNotValid()
        {
            try
            {
                var obj = new RegularExpressionAttrTest()
                {
                    Country = "uX"
                };

                SwagValidator.Validate(obj); //should throw exception
                Assert.Fail();               // should not reach here because empty string threw exception
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex.Message.Contains("Country is invalid"));
            }
        }
        public void ObjectWithNestedClass()
        {
            var obj1 = new SomeObjectWithNestedClass()
            {
                SomethingElse      = "something",
                ThisIsARequiredObj = null
            };

            try
            {
                SwagValidator.Validate(obj1);
                Assert.Fail(); // should not reach here
            }
            catch (Exception ex)
            {
                Assert.AreEqual("Value cannot be null. Parameter name: ThisIsARequiredObj", ex.Message);
            }
        }
        public void TestRequiredFieldForDerivedClassProperties()
        {
            RequiredFieldDerived derivedObject = new RequiredFieldDerived()
            {
                BaseStringType = "Payments"
            };

            try
            {
                SwagValidator.Validate(derivedObject);
            }
            catch (Exception ex)
            {
                Assert.AreEqual(ex.Message, "Value cannot be null or zero. Parameter name: DerivedIntegerType");
                return;
            }

            Assert.IsTrue(false);
        }
        public void TestRegExIPAddress()
        {
            try
            {
                //Valid value
                var obj = new RegExIPAddressTestClass()
                {
                    Text = "168.0.2.456"
                };
                Assert.IsTrue(SwagValidator.Validate(obj));

                //Invalid Value
                obj.Text = "1098.3456.33.44";
                SwagValidator.Validate(obj);
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex.Message.Contains("is invalid. Received value"));
            }
        }
        public void TestRegExEmail()
        {
            try
            {
                //Valid value
                var obj = new RegExEmailTestClass()
                {
                    Text = "*****@*****.**"
                };
                Assert.IsTrue(SwagValidator.Validate(obj));

                //Invalid Value
                obj.Text = "test.com";
                SwagValidator.Validate(obj);
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex.Message.Contains("is invalid. Received value"));
            }
        }
        public void TestRegExCountry()
        {
            try
            {
                //Valid value
                var obj = new RegExCountyTestClass()
                {
                    Text = "US"
                };
                Assert.IsTrue(SwagValidator.Validate(obj));

                //Invalid Value
                obj.Text = "PPP";
                SwagValidator.Validate(obj); //should throw exception
                Assert.Fail();               // should not reach here because empty string threw exception
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex.Message.Contains("is invalid. Received value"));
            }
        }
        public void StrLengthTest2()
        {
            var obj1 = new StrLengthTestClass2()
            {
                SomeString = "as44"
            };

            Assert.IsTrue(SwagValidator.Validate(obj1));

            try
            {
                var obj2 = new StrLengthTestClass2()
                {
                    SomeString = "1" //too short
                };

                SwagValidator.Validate(obj2); //should throw exception
                Assert.Fail();                // should not reach here because empty string threw exception
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex.Message.Contains("SomeString has length 1.  Minlength is 2"));
            }

            try
            {
                var obj2 = new StrLengthTestClass2()
                {
                    SomeString = "1324234234234234" //too long
                };

                SwagValidator.Validate(obj2); //should throw exception
                Assert.Fail();                // should not reach here because empty string threw exception
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex.Message.Contains("SomeString has length 16.  Maxlength is 5"));
            }
        }
        public void RangeTest()
        {
            var obj1 = new RangeTestClass()
            {
                SomeInt = 5
            };

            Assert.IsTrue(SwagValidator.Validate(obj1));

            try
            {
                var obj2 = new RangeTestClass()
                {
                    SomeInt = 1
                };

                SwagValidator.Validate(obj2); //should throw exception
                Assert.Fail();                // should not reach here because empty string threw exception
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex.Message.Contains("SomeInt is invalid. Received value: 1"));
            }

            try
            {
                var obj3 = new RangeTestClass()
                {
                    SomeInt = 1123123
                };

                SwagValidator.Validate(obj3); //should throw exception
                Assert.Fail();                // should not reach here because empty string threw exception
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex.Message.Contains("SomeInt is invalid. Received value: 1123123, accepted values: 3 to 8000"));
            }
        }
        //[PaymentsExpectedException(typeof(ArgumentNullException), "IntegerType")]
        public void TestRequiredFieldIntegerTypeForDefaultValues()
        {
            m_TestObject = new RequiredFieldTestObject()
            {
                DecimalType         = 10,
                GuidType            = Guid.NewGuid(),
                NullableDecimalType = 20,
                NullableIntegerType = 20,
                StringType          = "Payments",
            };

            try
            {
                SwagValidator.Validate(m_TestObject);
            }
            catch (Exception ex)
            {
                Assert.AreEqual(ex.Message, "Value cannot be null or zero. Parameter name: IntegerType");
                return;
            }

            Assert.IsTrue(false);
        }
        public void MaxLengthTest()
        {
            var obj1 = new MaxLengthTestClass()
            {
                MaxLengthOf3 = "14"
            };

            Assert.IsTrue(SwagValidator.Validate(obj1));

            try
            {
                var obj2 = new MaxLengthTestClass()
                {
                    MaxLengthOf3 = "12444" //too long
                };

                SwagValidator.Validate(obj2); //should throw exception
                Assert.Fail();                // should not reach here because empty string threw exception
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex.Message.Contains("has length 5.  Maxlength is 3"));
            }
        }
Ejemplo n.º 23
0
        public SoMuch AddReward([SwaggerExample("GUID")] int id, [RequiredConditionally] Reward <string> reward)
        {
            SwagValidator.Validate(reward);

            return(new SoMuch());
        }
        public void OptioanlStringListWithOutValueTest()
        {
            var obj2 = new ObjWithOptionalList(); // Empty string list

            Assert.IsTrue(SwagValidator.Validate(obj2));
        }
        public void OptioanlObjectListWithOutValueTest()
        {
            var obj6 = new ComplexTypeWithOptionalList(); // Empty object list

            Assert.IsTrue(SwagValidator.Validate(obj6));
        }