Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Source/Licensing/SystemEntitlements.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace Fossa.Licensing;

using System.Globalization;
using LanguageExt;
using TIKSN.Deployment;
using TIKSN.Globalization;

/// <summary>
/// System Entitlements.
Expand All @@ -15,4 +15,4 @@ public record SystemEntitlements(
Ulid SystemId,
EnvironmentName EnvironmentName,
int MaximumCompanyCount,
Seq<RegionInfo> Countries);
Seq<CountryInfo> Countries);
18 changes: 9 additions & 9 deletions Source/Licensing/SystemEntitlementsConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ public class SystemEntitlementsConverter : IEntitlementsConverter<SystemEntitlem
private static readonly Seq<Ulid> InvalidSystemIds =
Seq(Ulid.Empty, Ulid.MinValue, Ulid.MaxValue);

private readonly IRegionFactory regionFactory;
private readonly ICountryFactory countryFactory;

/// <summary>
/// Initializes a new instance of the <see cref="SystemEntitlementsConverter"/> class.
/// </summary>
/// <param name="regionFactory"><see cref="RegionInfo"/> Factory.</param>
/// <param name="countryFactory"><see cref="CountryInfo"/> Factory.</param>
public SystemEntitlementsConverter(
IRegionFactory regionFactory)
=> this.regionFactory = regionFactory ?? throw new ArgumentNullException(nameof(regionFactory));
ICountryFactory countryFactory)
=> this.countryFactory = countryFactory ?? throw new ArgumentNullException(nameof(countryFactory));

/// <summary>
/// Convert from Domain Model to Data Model.
Expand Down Expand Up @@ -144,7 +144,7 @@ public Validation<Error, SystemEntitlements> Convert(
}

var countries = (entitlementsData.CountryCodes ?? [])
.Choose(x => this.CreateRegion(x, errors.Add))
.Choose(x => this.CreateCountry(x, errors.Add))
.ToSeq();

if (errors.Count > 0)
Expand All @@ -159,15 +159,15 @@ public Validation<Error, SystemEntitlements> Convert(
countries);
}

private Option<RegionInfo> CreateRegion(string? name, Action<Error> addError)
private Option<CountryInfo> CreateCountry(string? name, Action<Error> addError)
{
this.ValidateCountryCode(name, addError);

if (name is not null)
{
try
{
return this.regionFactory.Create(name);
return this.countryFactory.Create(name);
}
catch (ArgumentException)
{
Expand All @@ -179,7 +179,7 @@ private Option<RegionInfo> CreateRegion(string? name, Action<Error> addError)
return None;
}

private void ValidateCountryCode(RegionInfo? country, Action<Error> addError)
private void ValidateCountryCode(CountryInfo? country, Action<Error> addError)
=> this.ValidateCountryCode(country?.TwoLetterISORegionName, addError);

private void ValidateCountryCode(string? code, Action<Error> addError)
Expand Down Expand Up @@ -210,7 +210,7 @@ private void ValidateCountryCode(string? code, Action<Error> addError)

try
{
_ = this.regionFactory.Create(code);
_ = this.countryFactory.Create(code);
}
catch (ArgumentException)
{
Expand Down
25 changes: 23 additions & 2 deletions Tests/Licensing.Test/SystemEntitlementsConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -30,11 +33,12 @@ public SystemEntitlementsConverterTests()
containerBuilder.Populate(services);

this.serviceProvider = new AutofacServiceProvider(containerBuilder.Build());
this.countryFactory = this.serviceProvider.GetRequiredService<ICountryFactory>();
}

[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)
Expand All @@ -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<CountryInfo> 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));

Expand Down
Loading