From d510c43e51d7612126e0a1f2f7e2109536828daf Mon Sep 17 00:00:00 2001 From: Tigran TIKSN Torosyan Date: Thu, 28 May 2026 16:15:27 -0500 Subject: [PATCH] Use CountryInfo instead of RegionInfo --- Source/Licensing/SystemEntitlements.cs | 4 +-- .../Licensing/SystemEntitlementsConverter.cs | 18 ++++++------- .../SystemEntitlementsConverterTests.cs | 25 +++++++++++++++++-- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/Source/Licensing/SystemEntitlements.cs b/Source/Licensing/SystemEntitlements.cs index 0477733..9ef3d94 100644 --- a/Source/Licensing/SystemEntitlements.cs +++ b/Source/Licensing/SystemEntitlements.cs @@ -1,8 +1,8 @@ namespace Fossa.Licensing; -using System.Globalization; using LanguageExt; using TIKSN.Deployment; +using TIKSN.Globalization; /// /// System Entitlements. @@ -15,4 +15,4 @@ public record SystemEntitlements( Ulid SystemId, EnvironmentName EnvironmentName, int MaximumCompanyCount, - Seq Countries); + Seq Countries); diff --git a/Source/Licensing/SystemEntitlementsConverter.cs b/Source/Licensing/SystemEntitlementsConverter.cs index b58d248..04d84c6 100644 --- a/Source/Licensing/SystemEntitlementsConverter.cs +++ b/Source/Licensing/SystemEntitlementsConverter.cs @@ -17,15 +17,15 @@ public class SystemEntitlementsConverter : IEntitlementsConverter InvalidSystemIds = Seq(Ulid.Empty, Ulid.MinValue, Ulid.MaxValue); - private readonly IRegionFactory regionFactory; + private readonly ICountryFactory countryFactory; /// /// Initializes a new instance of the class. /// - /// Factory. + /// Factory. public SystemEntitlementsConverter( - IRegionFactory regionFactory) - => this.regionFactory = regionFactory ?? throw new ArgumentNullException(nameof(regionFactory)); + ICountryFactory countryFactory) + => this.countryFactory = countryFactory ?? throw new ArgumentNullException(nameof(countryFactory)); /// /// Convert from Domain Model to Data Model. @@ -144,7 +144,7 @@ public Validation Convert( } var countries = (entitlementsData.CountryCodes ?? []) - .Choose(x => this.CreateRegion(x, errors.Add)) + .Choose(x => this.CreateCountry(x, errors.Add)) .ToSeq(); if (errors.Count > 0) @@ -159,7 +159,7 @@ public Validation Convert( countries); } - private Option CreateRegion(string? name, Action addError) + private Option CreateCountry(string? name, Action addError) { this.ValidateCountryCode(name, addError); @@ -167,7 +167,7 @@ private Option CreateRegion(string? name, Action addError) { try { - return this.regionFactory.Create(name); + return this.countryFactory.Create(name); } catch (ArgumentException) { @@ -179,7 +179,7 @@ private Option CreateRegion(string? name, Action addError) return None; } - private void ValidateCountryCode(RegionInfo? country, Action addError) + private void ValidateCountryCode(CountryInfo? country, Action addError) => this.ValidateCountryCode(country?.TwoLetterISORegionName, addError); private void ValidateCountryCode(string? code, Action addError) @@ -210,7 +210,7 @@ private void ValidateCountryCode(string? code, Action addError) try { - _ = this.regionFactory.Create(code); + _ = this.countryFactory.Create(code); } catch (ArgumentException) { diff --git a/Tests/Licensing.Test/SystemEntitlementsConverterTests.cs b/Tests/Licensing.Test/SystemEntitlementsConverterTests.cs index 16cbe34..2c2e9e9 100644 --- a/Tests/Licensing.Test/SystemEntitlementsConverterTests.cs +++ b/Tests/Licensing.Test/SystemEntitlementsConverterTests.cs @@ -3,16 +3,19 @@ namespace Fossa.Licensing.Test; using System.Globalization; using Autofac; using Autofac.Extensions.DependencyInjection; +using LanguageExt; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Time.Testing; using TIKSN.DependencyInjection; using TIKSN.Deployment; +using TIKSN.Globalization; using TIKSN.Licensing; using Xunit; using static LanguageExt.Prelude; public class SystemEntitlementsConverterTests { + private readonly ICountryFactory countryFactory; private readonly IServiceProvider serviceProvider; public SystemEntitlementsConverterTests() @@ -30,11 +33,12 @@ public SystemEntitlementsConverterTests() containerBuilder.Populate(services); this.serviceProvider = new AutofacServiceProvider(containerBuilder.Build()); + this.countryFactory = this.serviceProvider.GetRequiredService(); } [Theory] [InlineData("US", true)] - [InlineData("en-US", true)] + [InlineData("DE", true)] [InlineData("001", false)] [InlineData("en-001", false)] public void GivenCountryCode_WhenSystemEntitlementsConverted_Then(string countryCode, bool isValid) @@ -46,7 +50,24 @@ public void GivenCountryCode_WhenSystemEntitlementsConverted_Then(string country var systemId = Ulid.NewUlid(); var environmentName = EnvironmentName.Parse("Development", asciiOnly: true, CultureInfo.InvariantCulture).Single(); - var country = new RegionInfo(countryCode); + + Option maybeCountry; + try + { + maybeCountry = this.countryFactory.Create(countryCode); + } + catch (CountryNotFoundException) + { + maybeCountry = None; + } + + if (maybeCountry.IsNone) + { + Assert.False(isValid); + return; + } + + var country = maybeCountry.Single(); var entitlements = new SystemEntitlements( systemId, environmentName, 10, Seq1(country));