예제 #1
0
        /// <summary>
        /// Performs the validation of this configuration object.
        /// Returns empty list if no problems found, otherwise list contains validation problems.
        /// </summary>
        public IEnumerable <ConfigurationValidationItem> Validate()
        {
            // Helper to collect and perform validations
            var validations = new ConfigurationValidationCollector <SampleConfig>(this);

            // Here are validations
            validations.ValidateNotZero(c => c.SomeValue, "Configuration should not contain default value (=0).");
            validations.ValidateNotNullOrEmpty(c => c.SomeName, "Configuration should specify value for Name.");
            validations.ValidateUri(c => c.SomeEndpoint, "External API endpoint is incorrect");
            validations.ValidateEmail(c => c.SomeEmail, "E-mail address is wrong.");
            validations.ValidateIpV4Address(c => c.SomeIp, "IP address is not valid.");
            validations.ValidatePublicIpV4Address(c => c.SomeIp, "IP address is not a public IP address.");

            // Generic methods, expecting boolean outcome of Linq expression
            validations.ValidateMust(c => c.SomeEndpoint.StartsWith("https", StringComparison.OrdinalIgnoreCase), nameof(this.SomeEndpoint), "Enpoint is no SSL secured.");
            validations.ValidateMust(c => c.SomeEndpoint.EndsWith("/", StringComparison.OrdinalIgnoreCase), nameof(this.SomeEndpoint), "Enpoint should end with shash /.");
            validations.ValidateMust(c =>
                                     c.SomeName.Contains("sparta", StringComparison.OrdinalIgnoreCase) &&
                                     c.SomeValue > 10,
                                     $"{nameof(this.SomeName)} and {nameof(this.SomeValue)}",
                                     "Combined validations failed.");

            // Syntactic sugar
            validations.ValidateStartsWith(c => c.SomeEndpoint, "https", "Enpoint is no SSL secured.");
            validations.ValidateEndsWith(c => c.SomeEndpoint, "/", "Enpoint should end in slash /.");

            // Returning all found validation problems
            return(validations.Result);
        }
        public void Email_ValidVariations_NoProblem(string email)
        {
            var cfg = new TestConfig {
                SomeEmail = email
            };
            var coll = new ConfigurationValidationCollector <TestConfig>(cfg);

            coll.ValidateEmail(c => c.SomeEmail, "Incorrect email!");
            coll.Result.Should().NotBeNull();
            coll.Result.Count.Should().Be(0);
        }
        public void IpPublic_ValidVariations_NoProblem(string ip)
        {
            var cfg = new TestConfig {
                SomeIp = ip
            };
            var coll = new ConfigurationValidationCollector <TestConfig>(cfg);

            coll.ValidatePublicIpV4Address(c => c.SomeIp, "Not public IP!");
            coll.Result.Should().NotBeNull();
            coll.Result.Count.Should().Be(0);
        }
        public void LongNotZero_Success_NoValidation()
        {
            var cfg = new TestConfig {
                SomeLongValue = 1000000
            };
            var coll = new ConfigurationValidationCollector <TestConfig>(cfg);

            coll.ValidateNotZero(c => c.SomeLongValue, "No Zeroes!");
            coll.Result.Should().NotBeNull();
            coll.Result.Count.Should().Be(0);
        }
        public void Url_ValidVariations_Validates(string ep)
        {
            var cfg = new TestConfig {
                SomeEndpoint = ep
            };
            var coll = new ConfigurationValidationCollector <TestConfig>(cfg);

            coll.ValidateUri(c => c.SomeEndpoint, "Not valid URL!");
            coll.Result.Should().NotBeNull();
            coll.Result.Count.Should().Be(0);
        }
        public void NotNullOrEmpty_Filled_NoValidation()
        {
            var cfg = new TestConfig {
                SomeName = "Anrijs"
            };
            var coll = new ConfigurationValidationCollector <TestConfig>(cfg);

            coll.ValidateNotNullOrEmpty(c => c.SomeName, "Wow!");
            coll.Result.Should().NotBeNull();
            coll.Result.Count.Should().Be(0);
        }
        public void StartsWith_Correct_Validation()
        {
            var cfg = new TestConfig {
                SomeEndpoint = "https://domain.com"
            };
            var coll = new ConfigurationValidationCollector <TestConfig>(cfg);

            coll.ValidateStartsWith(c => c.SomeEndpoint, "HTTP", "Should start with web protocol!");
            coll.Result.Should().NotBeNull();
            coll.Result.Count.Should().Be(0);
        }
        public void EndsWith_Correct_Validation()
        {
            var cfg = new TestConfig {
                SomeEndpoint = "https://domain.com"
            };
            var coll = new ConfigurationValidationCollector <TestConfig>(cfg);

            coll.ValidateEndsWith(c => c.SomeEndpoint, "COM", "Should be COM domain!");
            coll.Result.Should().NotBeNull();
            coll.Result.Count.Should().Be(0);
        }
        public void Contains_String_NoValidation()
        {
            var cfg = new TestConfig {
                SomeName = "Test for Domain features."
            };
            var coll = new ConfigurationValidationCollector <TestConfig>(cfg);

            coll.ValidateContains(c => c.SomeName, "domain", "Should have domain!");
            coll.Result.Should().NotBeNull();
            coll.Result.Count.Should().Be(0);
        }
        public void NotNullOrEmpty_Empty_Validation()
        {
            var cfg = new TestConfig {
                SomeName = string.Empty
            };
            var coll = new ConfigurationValidationCollector <TestConfig>(cfg);

            coll.ValidateNotNullOrEmpty(c => c.SomeName, "Wow!");
            coll.Result.Should().NotBeNull();
            coll.Result.Count.Should().Be(1);
            coll.Result[0].ConfigurationSection.Should().Be("TestConfig");
            coll.Result[0].ConfigurationItem.Should().Be("SomeName");
            coll.Result[0].ConfigurationValue.Should().Be(string.Empty);
            coll.Result[0].ValidationMessage.Should().Be("Wow!");
        }
        public void LongNotZero_Validation()
        {
            var cfg = new TestConfig {
                SomeLongValue = 0
            };
            var coll = new ConfigurationValidationCollector <TestConfig>(cfg);

            coll.ValidateNotZero(c => c.SomeLongValue, "No Zeroes!");
            coll.Result.Should().NotBeNull();
            coll.Result.Count.Should().Be(1);
            coll.Result[0].ConfigurationSection.Should().Be("TestConfig");
            coll.Result[0].ConfigurationItem.Should().Be("SomeLongValue");
            coll.Result[0].ConfigurationValue.Should().Be(0);
            coll.Result[0].ValidationMessage.Should().Be("No Zeroes!");
        }
        public void Url_InvalidVariations_Validates(string ep)
        {
            var cfg = new TestConfig {
                SomeEndpoint = ep
            };
            var coll = new ConfigurationValidationCollector <TestConfig>(cfg);

            coll.ValidateUri(c => c.SomeEndpoint, "Not valid URL!");
            coll.Result.Should().NotBeNull();
            coll.Result.Count.Should().Be(1);
            coll.Result[0].ConfigurationSection.Should().Be("TestConfig");
            coll.Result[0].ConfigurationItem.Should().Be("SomeEndpoint");
            coll.Result[0].ConfigurationValue.Should().Be(ep);
            coll.Result[0].ValidationMessage.Should().Be("Not valid URL!");
        }
        public void IpPublic_InvalidIpRangeVariations_Validates(string ip)
        {
            var cfg = new TestConfig {
                SomeIp = ip
            };
            var coll = new ConfigurationValidationCollector <TestConfig>(cfg);

            coll.ValidatePublicIpV4Address(c => c.SomeIp, "Not public IP!");
            coll.Result.Should().NotBeNull();
            coll.Result.Count.Should().Be(1);
            coll.Result[0].ConfigurationSection.Should().Be("TestConfig");
            coll.Result[0].ConfigurationItem.Should().Be("SomeIp");
            coll.Result[0].ConfigurationValue.Should().Be(ip);
            coll.Result[0].ValidationMessage.Should().Be("Not public IP!");
        }
        public void Email_InvalidVariations_Validates(string email)
        {
            var cfg = new TestConfig {
                SomeEmail = email
            };
            var coll = new ConfigurationValidationCollector <TestConfig>(cfg);

            coll.ValidateEmail(c => c.SomeEmail, "Incorrect email!");
            coll.Result.Should().NotBeNull();
            coll.Result.Count.Should().Be(1);
            coll.Result[0].ConfigurationSection.Should().Be("TestConfig");
            coll.Result[0].ConfigurationItem.Should().Be("SomeEmail");
            coll.Result[0].ConfigurationValue.Should().Be(email);
            coll.Result[0].ValidationMessage.Should().Be("Incorrect email!");
        }
        public void EndsWith_Wrong_Validation()
        {
            var cfg = new TestConfig {
                SomeEndpoint = "ftp://domain.lv"
            };
            var coll = new ConfigurationValidationCollector <TestConfig>(cfg);

            coll.ValidateStartsWith(c => c.SomeEndpoint, "COM", "Should be COM domain!");
            coll.Result.Should().NotBeNull();
            coll.Result.Count.Should().Be(1);
            coll.Result[0].ConfigurationSection.Should().Be("TestConfig");
            coll.Result[0].ConfigurationItem.Should().Be("SomeEndpoint");
            coll.Result[0].ConfigurationValue.Should().Be("ftp://domain.lv");
            coll.Result[0].ValidationMessage.Should().Be("Should be COM domain!");
        }
        public void EndsWith_Empty_Validation()
        {
            var cfg = new TestConfig {
                SomeEndpoint = string.Empty
            };
            var coll = new ConfigurationValidationCollector <TestConfig>(cfg);

            coll.ValidateEndsWith(c => c.SomeEndpoint, "COM", "Should be COM domain!");
            coll.Result.Should().NotBeNull();
            coll.Result.Count.Should().Be(1);
            coll.Result[0].ConfigurationSection.Should().Be("TestConfig");
            coll.Result[0].ConfigurationItem.Should().Be("SomeEndpoint");
            coll.Result[0].ConfigurationValue.Should().Be(string.Empty);
            coll.Result[0].ValidationMessage.Should().Be("Should be COM domain!");
        }
        public void StartsWith_Null_Validation()
        {
            var cfg = new TestConfig {
                SomeEndpoint = null
            };
            var coll = new ConfigurationValidationCollector <TestConfig>(cfg);

            coll.ValidateStartsWith(c => c.SomeEndpoint, "HTTP", "Should start with protocol!");
            coll.Result.Should().NotBeNull();
            coll.Result.Count.Should().Be(1);
            coll.Result[0].ConfigurationSection.Should().Be("TestConfig");
            coll.Result[0].ConfigurationItem.Should().Be("SomeEndpoint");
            coll.Result[0].ConfigurationValue.Should().BeNull();
            coll.Result[0].ValidationMessage.Should().Be("Should start with protocol!");
        }
        public void Contains_Empty_Validation()
        {
            var cfg = new TestConfig {
                SomeName = string.Empty
            };
            var coll = new ConfigurationValidationCollector <TestConfig>(cfg);

            coll.ValidateContains(c => c.SomeName, "domain", "Should have domain!");
            coll.Result.Should().NotBeNull();
            coll.Result.Count.Should().Be(1);
            coll.Result[0].ConfigurationSection.Should().Be("TestConfig");
            coll.Result[0].ConfigurationItem.Should().Be("SomeName");
            coll.Result[0].ConfigurationValue.Should().Be(string.Empty);
            coll.Result[0].ValidationMessage.Should().Be("Should have domain!");
        }