From 0ca8ea716e3a6b19b96148b537671a8973c7e679 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Dudak?= Date: Wed, 8 Apr 2020 19:00:43 +0200 Subject: [PATCH] Introduced SsnValidator class; returning ValidationResult instead of bool --- Adia.SsnValidator.Tests/SsnValidatorTests.cs | 29 +++++++++++ .../Validators/AhvValidatorTests.cs | 51 +++++++++++-------- Adia.SsnValidator/Adia.SsnValidator.csproj | 4 +- Adia.SsnValidator/Properties/AssemblyInfo.cs | 1 + Adia.SsnValidator/SsnValidator.cs | 12 +++++ Adia.SsnValidator/SsnValidatorFactory.cs | 4 +- Adia.SsnValidator/ValidationResult.cs | 9 ++++ Adia.SsnValidator/Validators/AhvValidator.cs | 18 ++++--- Adia.SsnValidator/Validators/IValidator.cs | 6 +-- CHANGELOG.md | 5 ++ 10 files changed, 104 insertions(+), 35 deletions(-) create mode 100644 Adia.SsnValidator.Tests/SsnValidatorTests.cs create mode 100644 Adia.SsnValidator/Properties/AssemblyInfo.cs create mode 100644 Adia.SsnValidator/SsnValidator.cs create mode 100644 Adia.SsnValidator/ValidationResult.cs diff --git a/Adia.SsnValidator.Tests/SsnValidatorTests.cs b/Adia.SsnValidator.Tests/SsnValidatorTests.cs new file mode 100644 index 0000000..1894554 --- /dev/null +++ b/Adia.SsnValidator.Tests/SsnValidatorTests.cs @@ -0,0 +1,29 @@ +using System; +using NUnit.Framework; + +namespace Adia.SsnValidator.Tests +{ + public class SsnValidatorTests + { + private SsnValidator _validator; + + [SetUp] + public void Setup() + { + _validator = new SsnValidator(); + } + + [Test] + public void GivenCorrectSwissArguments_ShouldRunSwissSsnValidation() + { + var result = _validator.Validate("756.4089.0811.50", CountryCode.CH); + Assert.AreEqual(result, ValidationResult.Valid); + } + + [Test] + public void GivenIncorrectCountryCode_ShouldThrowExceptiom() + { + Assert.Throws(() => _validator.Validate("756.4089.0811.50", (CountryCode) 100)); + } + } +} diff --git a/Adia.SsnValidator.Tests/Validators/AhvValidatorTests.cs b/Adia.SsnValidator.Tests/Validators/AhvValidatorTests.cs index 0e6a233..a13f23c 100644 --- a/Adia.SsnValidator.Tests/Validators/AhvValidatorTests.cs +++ b/Adia.SsnValidator.Tests/Validators/AhvValidatorTests.cs @@ -1,9 +1,10 @@ -using Adia.SsnValidator.Validators; +using System; +using Adia.SsnValidator; +using Adia.SsnValidator.Validators; using NUnit.Framework; namespace Adia.Ssn.Tests.Validators { - [TestFixture] public class AhvValidatorTests { private AhvValidator _validator; @@ -14,28 +15,34 @@ public void Setup() _validator = new AhvValidator(); } - [Test] - [TestCase("756.4089.0811.50", true)] - [TestCase("756.5220.1643.81", true)] - [TestCase("756.5837.4745.52", true)] - [TestCase("756.1656.9672.13", true)] - [TestCase("756.6168.0333.64", true)] - [TestCase("756.0246.1057.45", true)] - [TestCase("756.8478.9370.66", true)] - [TestCase("756.0298.4726.97", true)] - [TestCase("756.5113.6381.28", true)] - [TestCase("756.5466.9658.89", true)] - [TestCase("756.5466.965889", false)] - [TestCase("755:5466.9658.89", false)] - [TestCase("7561656967213", false)] - [TestCase("756.1234.5678.95", false)] - [TestCase("7561234567895", false)] - [TestCase("75612345648955", false)] - public void ValidateAhvNumberAsExpected(string ssn, bool expectedResult) + [TestCase("756.4089.0811.50", ValidationResult.Valid)] + [TestCase("756.5220.1643.81", ValidationResult.Valid)] + [TestCase("756.5837.4745.52", ValidationResult.Valid)] + [TestCase("756.1656.9672.13", ValidationResult.Valid)] + [TestCase("756.6168.0333.64", ValidationResult.Valid)] + [TestCase("756.0246.1057.45", ValidationResult.Valid)] + [TestCase("756.8478.9370.66", ValidationResult.Valid)] + [TestCase("756.0298.4726.97", ValidationResult.Valid)] + [TestCase("756.5113.6381.28", ValidationResult.Valid)] + [TestCase("756.5466.9658.89", ValidationResult.Valid)] + [TestCase("756.1234.5678.95", ValidationResult.InvalidChecksum)] + [TestCase("756.5466.965889", ValidationResult.InvalidFormat)] + [TestCase("755:5466.9658.89", ValidationResult.InvalidFormat)] + [TestCase("7561656967213", ValidationResult.InvalidFormat)] + [TestCase("7561234567895", ValidationResult.InvalidFormat)] + [TestCase("75612345648955", ValidationResult.InvalidFormat)] + [TestCase("000.4089.0811.50", ValidationResult.InvalidFormat)] + [TestCase("", ValidationResult.InvalidFormat)] + public void GivenNonNullAhv_ShouldValidateIt(string ssn, ValidationResult expectedResult) { var result = _validator.Validate(ssn); - Assert.That(result, Is.EqualTo(expectedResult)); } + + [Test] + public void GivenNullAhv_ShouldThrowArgumentNullException() + { + Assert.Throws(() => _validator.Validate(null)); + } } -} \ No newline at end of file +} diff --git a/Adia.SsnValidator/Adia.SsnValidator.csproj b/Adia.SsnValidator/Adia.SsnValidator.csproj index 2cc4855..a1d0c1c 100644 --- a/Adia.SsnValidator/Adia.SsnValidator.csproj +++ b/Adia.SsnValidator/Adia.SsnValidator.csproj @@ -2,10 +2,10 @@ netstandard2.0 - 0.0.1 + 0.1.0 PiotrSzperka; doodack Adia - C# library for validating SSN number for countries supported by Adia + C# library for validating Social Security Numbers for countries supported by Adia Adia MIT https://github.com/adia-technology/Adia.SsnValidator diff --git a/Adia.SsnValidator/Properties/AssemblyInfo.cs b/Adia.SsnValidator/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..e57f045 --- /dev/null +++ b/Adia.SsnValidator/Properties/AssemblyInfo.cs @@ -0,0 +1 @@ +[assembly:System.Runtime.CompilerServices.InternalsVisibleTo("Adia.SsnValidator.Tests")] diff --git a/Adia.SsnValidator/SsnValidator.cs b/Adia.SsnValidator/SsnValidator.cs new file mode 100644 index 0000000..6684dfe --- /dev/null +++ b/Adia.SsnValidator/SsnValidator.cs @@ -0,0 +1,12 @@ +namespace Adia.SsnValidator +{ + public class SsnValidator + { + public ValidationResult Validate(string ssn, CountryCode country) + { + var factory = new SsnValidatorFactory(); + var validator = factory.Create(country); + return validator.Validate(ssn); + } + } +} diff --git a/Adia.SsnValidator/SsnValidatorFactory.cs b/Adia.SsnValidator/SsnValidatorFactory.cs index 23b09ff..feb7aa7 100644 --- a/Adia.SsnValidator/SsnValidatorFactory.cs +++ b/Adia.SsnValidator/SsnValidatorFactory.cs @@ -3,7 +3,7 @@ namespace Adia.SsnValidator { - public class SsnValidatorFactory + internal class SsnValidatorFactory { public IValidator Create(CountryCode country) { @@ -13,7 +13,7 @@ public IValidator Create(CountryCode country) return new AhvValidator(); default: - throw new NotSupportedException("Your country is not supported"); + throw new NotSupportedException($"The provided country ({country}) is not supported."); } } } diff --git a/Adia.SsnValidator/ValidationResult.cs b/Adia.SsnValidator/ValidationResult.cs new file mode 100644 index 0000000..b1be9c0 --- /dev/null +++ b/Adia.SsnValidator/ValidationResult.cs @@ -0,0 +1,9 @@ +namespace Adia.SsnValidator +{ + public enum ValidationResult + { + Valid, + InvalidFormat, + InvalidChecksum + } +} diff --git a/Adia.SsnValidator/Validators/AhvValidator.cs b/Adia.SsnValidator/Validators/AhvValidator.cs index 73d709e..0a6bdd8 100644 --- a/Adia.SsnValidator/Validators/AhvValidator.cs +++ b/Adia.SsnValidator/Validators/AhvValidator.cs @@ -4,14 +4,19 @@ namespace Adia.SsnValidator.Validators { - public class AhvValidator : IValidator + internal sealed class AhvValidator : IValidator { - public bool Validate(string ahv) + public ValidationResult Validate(string ahv) { + if (ahv == null) + { + throw new ArgumentNullException(nameof(ahv)); + } + var regex = new Regex(@"^756\.\d{4}\.\d{4}\.\d{2}$"); - if(!regex.IsMatch(ahv)) + if (!regex.IsMatch(ahv)) { - return false; + return ValidationResult.InvalidFormat; } var ahv13 = ahv.Where(char.IsDigit).ToArray(); @@ -33,7 +38,8 @@ public bool Validate(string ahv) var nextTimesTen = Math.Ceiling(totalChecksum / 10.0) * 10; var checksumDigit = (int)nextTimesTen - totalChecksum; - return checksumDigit == int.Parse(ahv.Last().ToString()); + var isChecksumCorrect = checksumDigit == int.Parse(ahv.Last().ToString()); + return isChecksumCorrect ? ValidationResult.Valid : ValidationResult.InvalidChecksum; } } -} \ No newline at end of file +} diff --git a/Adia.SsnValidator/Validators/IValidator.cs b/Adia.SsnValidator/Validators/IValidator.cs index c817661..382566f 100644 --- a/Adia.SsnValidator/Validators/IValidator.cs +++ b/Adia.SsnValidator/Validators/IValidator.cs @@ -1,7 +1,7 @@ namespace Adia.SsnValidator.Validators { - public interface IValidator + internal interface IValidator { - bool Validate(string ssn); + ValidationResult Validate(string ssn); } -} \ No newline at end of file +} diff --git a/CHANGELOG.md b/CHANGELOG.md index a40ddc2..cd03bae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 0.1.0 + +* **BREAKING CHANGE** Changed the main validation class to be `SsnValidator`. This is the only publicly available class in the library. +* `SsnValidator.Validate` returns a `ValidationResult` instance that can be used to determine why an SSN is invalid. + ## 0.0.1 Initial version with support for Swiss SSN (AHV).