Skip to content

Commit df3a678

Browse files
committed
Initial push
1 parent b2636b4 commit df3a678

11 files changed

+216
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="nunit" Version="3.12.0" />
11+
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<ProjectReference Include="..\Adia.SsnValidator\Adia.SsnValidator.csproj" />
17+
</ItemGroup>
18+
19+
</Project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using Adia.SsnValidator;
3+
using Adia.SsnValidator.Validators;
4+
using NUnit.Framework;
5+
6+
namespace Adia.Ssn.Tests
7+
{
8+
public class SsnValidatorFactoryTests
9+
{
10+
private SsnValidatorFactory _factory;
11+
12+
[SetUp]
13+
public void Setup()
14+
{
15+
_factory = new SsnValidatorFactory();
16+
}
17+
18+
[Test]
19+
public void SelectCorrectValidatorForCountry()
20+
{
21+
var validator = _factory.Create(CountryCode.CH);
22+
23+
Assert.That(validator, Is.TypeOf(typeof(AhvValidator)));
24+
}
25+
26+
[Test]
27+
public void ThrowExceptionForUnsupportedCountry()
28+
{
29+
Assert.Throws<NotSupportedException>(() => _factory.Create((CountryCode) 100));
30+
}
31+
}
32+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using Adia.SsnValidator.Validators;
2+
using NUnit.Framework;
3+
4+
namespace Adia.Ssn.Tests.Validators
5+
{
6+
[TestFixture]
7+
public class AhvValidatorTests
8+
{
9+
private AhvValidator _validator;
10+
11+
[SetUp]
12+
public void Setup()
13+
{
14+
_validator = new AhvValidator();
15+
}
16+
17+
[Test]
18+
[TestCase("756.4089.0811.50", true)]
19+
[TestCase("756.5220.1643.81", true)]
20+
[TestCase("756.5837.4745.52", true)]
21+
[TestCase("756.1656.9672.13", true)]
22+
[TestCase("756.6168.0333.64", true)]
23+
[TestCase("756.0246.1057.45", true)]
24+
[TestCase("756.8478.9370.66", true)]
25+
[TestCase("756.0298.4726.97", true)]
26+
[TestCase("756.5113.6381.28", true)]
27+
[TestCase("756.5466.9658.89", true)]
28+
[TestCase("7561656967213", true)]
29+
[TestCase("756.1234.5678.95", false)]
30+
[TestCase("7561234567895", false)]
31+
[TestCase("75612345648955", false)]
32+
33+
public void ValidateAhvNumberAsExpected(string ssn, bool expectedResult)
34+
{
35+
var result = _validator.Validate(ssn);
36+
37+
Assert.That(result, Is.EqualTo(expectedResult));
38+
}
39+
}
40+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<Version>0.0.1</Version>
6+
<Authors>PiotrSzperka</Authors>
7+
<Company>Adia</Company>
8+
<Description>C# library for validating SSN number for countries supported by Adia</Description>
9+
<Copyright>Adia</Copyright>
10+
<PackageLicenseFile>LICENSE</PackageLicenseFile>
11+
<PackageProjectUrl>https://github.com/adia-technology/Adia.SsnValidator</PackageProjectUrl>
12+
<RepositoryUrl>https://github.com/adia-technology/Adia.SsnValidator</RepositoryUrl>
13+
<RepositoryType>Library</RepositoryType>
14+
<PackageTags>SSN AHV validator</PackageTags>
15+
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
16+
</PropertyGroup>
17+
18+
</Project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29911.84
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Adia.SsnValidator", "Adia.SsnValidator.csproj", "{57B3C10F-9B13-496E-9328-A2E5806F89C5}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Adia.Ssn.Tests", "..\Adia.Ssn.Tests\Adia.Ssn.Tests.csproj", "{89E2FC82-E62F-45A9-88D5-0A9A6121278F}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{57B3C10F-9B13-496E-9328-A2E5806F89C5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{57B3C10F-9B13-496E-9328-A2E5806F89C5}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{57B3C10F-9B13-496E-9328-A2E5806F89C5}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{57B3C10F-9B13-496E-9328-A2E5806F89C5}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{89E2FC82-E62F-45A9-88D5-0A9A6121278F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{89E2FC82-E62F-45A9-88D5-0A9A6121278F}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{89E2FC82-E62F-45A9-88D5-0A9A6121278F}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{89E2FC82-E62F-45A9-88D5-0A9A6121278F}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {2641EDDB-3CFE-42F3-A4CC-14D8EA9543D8}
30+
EndGlobalSection
31+
EndGlobal
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Adia/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

Adia.SsnValidator/CountryCode.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Adia.SsnValidator
2+
{
3+
public enum CountryCode
4+
{
5+
CH
6+
}
7+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using Adia.SsnValidator.Validators;
3+
4+
namespace Adia.SsnValidator
5+
{
6+
public class SsnValidatorFactory
7+
{
8+
public IValidator Create(CountryCode country)
9+
{
10+
switch (country)
11+
{
12+
case CountryCode.CH:
13+
return new AhvValidator();
14+
15+
default:
16+
throw new NotSupportedException("Your country is not supported");
17+
}
18+
}
19+
}
20+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Linq;
3+
4+
namespace Adia.SsnValidator.Validators
5+
{
6+
public class AhvValidator : IValidator
7+
{
8+
public bool Validate(string ahv)
9+
{
10+
var ahv13 = ahv.Where(char.IsDigit).ToArray();
11+
if(ahv13.Count() != 13)
12+
{
13+
return false;
14+
}
15+
16+
var ahv12 = ahv13.Take(12).Reverse().ToArray();
17+
var totalChecksum = 0;
18+
for (var i = 0; i < 12; i++)
19+
{
20+
var number = int.Parse(ahv12[i].ToString());
21+
if (i % 2 == 0)
22+
{
23+
totalChecksum += 3 * number;
24+
}
25+
else
26+
{
27+
totalChecksum += number;
28+
}
29+
}
30+
31+
var nextTimesTen = Math.Ceiling(totalChecksum / 10.0) * 10;
32+
var checksumDigit = (int)nextTimesTen - totalChecksum;
33+
34+
return checksumDigit == int.Parse(ahv.Last().ToString());
35+
}
36+
}
37+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Adia.SsnValidator.Validators
2+
{
3+
public interface IValidator
4+
{
5+
bool Validate(string ssn);
6+
}
7+
}

0 commit comments

Comments
 (0)