diff --git a/lib/Logitar.Identity.Core/ApiKeys/ApiKeyId.cs b/lib/Logitar.Identity.Core/ApiKeys/ApiKeyId.cs index 505f5fb..f4a587e 100644 --- a/lib/Logitar.Identity.Core/ApiKeys/ApiKeyId.cs +++ b/lib/Logitar.Identity.Core/ApiKeys/ApiKeyId.cs @@ -31,34 +31,34 @@ public readonly struct ApiKeyId public EntityId EntityId { get; } /// - /// Initializes a new instance of the struct. + /// Initializes a new instance of the class. /// /// The tenant identifier. /// The entity identifier. - public ApiKeyId(TenantId? tenantId, Guid entityId) : this(tenantId, Convert.ToBase64String(entityId.ToByteArray()).ToUriSafeBase64()) - { - } - /// - /// Initializes a new instance of the struct. - /// - /// The tenant identifier. - /// The entity identifier. - public ApiKeyId(TenantId? tenantId, string entityId) + public ApiKeyId(TenantId? tenantId, EntityId entityId) { + StreamId = new(tenantId == null ? entityId.Value : string.Join(Separator, tenantId, entityId)); TenantId = tenantId; - EntityId = new(entityId); - StreamId = new(tenantId.HasValue ? string.Join(Separator, tenantId, entityId) : entityId); + EntityId = entityId; } + /// - /// Initializes a new instance of the struct. + /// Initializes a new instance of the class. /// - /// A stream identifier. + /// The identifier of the event stream. public ApiKeyId(StreamId streamId) { StreamId = streamId; string[] values = streamId.Value.Split(Separator); - TenantId = values.Length == 2 ? new(values[0]) : null; + if (values.Length > 2) + { + throw new ArgumentException($"The value '{streamId}' is not a valid API key ID.", nameof(streamId)); + } + else if (values.Length == 2) + { + TenantId = new(values.First()); + } EntityId = new(values.Last()); } @@ -67,7 +67,7 @@ public ApiKeyId(StreamId streamId) /// /// The tenant identifier. /// The generated identifier. - public static ApiKeyId NewId(TenantId? tenantId = null) => new(tenantId, Guid.NewGuid()); + public static ApiKeyId NewId(TenantId? tenantId = null) => new(tenantId, EntityId.NewId()); /// /// Returns a value indicating whether or not the specified identifiers are equal. diff --git a/lib/Logitar.Identity.Core/Passwords/OneTimePassword.cs b/lib/Logitar.Identity.Core/Passwords/OneTimePassword.cs index 7da32c9..f80eab3 100644 --- a/lib/Logitar.Identity.Core/Passwords/OneTimePassword.cs +++ b/lib/Logitar.Identity.Core/Passwords/OneTimePassword.cs @@ -1,5 +1,4 @@ using Logitar.EventSourcing; -using Logitar.Identity.Core.ApiKeys; using Logitar.Identity.Core.Passwords.Events; namespace Logitar.Identity.Core.Passwords; @@ -23,7 +22,7 @@ public class OneTimePassword : AggregateRoot /// /// Gets the identifier of the One-Time Password (OTP). /// - public new ApiKeyId Id => new(base.Id); + public new OneTimePasswordId Id => new(base.Id); /// /// Gets the tenant identifier of the One-Time Password (OTP). /// diff --git a/lib/Logitar.Identity.Core/Passwords/OneTimePasswordId.cs b/lib/Logitar.Identity.Core/Passwords/OneTimePasswordId.cs index 34388d1..67599af 100644 --- a/lib/Logitar.Identity.Core/Passwords/OneTimePasswordId.cs +++ b/lib/Logitar.Identity.Core/Passwords/OneTimePasswordId.cs @@ -7,6 +7,11 @@ namespace Logitar.Identity.Core.Passwords; /// public readonly struct OneTimePasswordId { + /// + /// The separator between the tenant ID and the entity ID. + /// + private const char Separator = ':'; + /// /// Gets the identifier of the event stream. /// @@ -26,39 +31,43 @@ public readonly struct OneTimePasswordId public EntityId EntityId { get; } /// - /// Initializes a new instance of the struct. + /// Initializes a new instance of the class. /// /// The tenant identifier. /// The entity identifier. - public OneTimePasswordId(TenantId? tenantId, Guid entityId) : this(tenantId, Convert.ToBase64String(entityId.ToByteArray()).ToUriSafeBase64()) - { - } - /// - /// Initializes a new instance of the struct. - /// - /// The tenant identifier. - /// The entity identifier. - public OneTimePasswordId(TenantId? tenantId, string entityId) + public OneTimePasswordId(TenantId? tenantId, EntityId entityId) { + StreamId = new(tenantId == null ? entityId.Value : string.Join(Separator, tenantId, entityId)); TenantId = tenantId; - EntityId = new(entityId); - StreamId = new(tenantId.HasValue ? $"{tenantId}:{entityId}" : entityId); + EntityId = entityId; } + /// - /// Initializes a new instance of the struct. + /// Initializes a new instance of the class. /// - /// A stream identifier. + /// The identifier of the event stream. public OneTimePasswordId(StreamId streamId) { StreamId = streamId; + + string[] values = streamId.Value.Split(Separator); + if (values.Length > 2) + { + throw new ArgumentException($"The value '{streamId}' is not a valid session ID.", nameof(streamId)); + } + else if (values.Length == 2) + { + TenantId = new(values.First()); + } + EntityId = new(values.Last()); } /// - /// Randomly generates a new One-Time Password (OTP) identifier. + /// Randomly generates a new session identifier. /// /// The tenant identifier. /// The generated identifier. - public static OneTimePasswordId NewId(TenantId? tenantId = null) => new(tenantId, Guid.NewGuid()); + public static OneTimePasswordId NewId(TenantId? tenantId = null) => new(tenantId, EntityId.NewId()); /// /// Returns a value indicating whether or not the specified identifiers are equal. diff --git a/lib/Logitar.Identity.Core/Roles/RoleId.cs b/lib/Logitar.Identity.Core/Roles/RoleId.cs index 82c9f4e..3deba50 100644 --- a/lib/Logitar.Identity.Core/Roles/RoleId.cs +++ b/lib/Logitar.Identity.Core/Roles/RoleId.cs @@ -31,34 +31,34 @@ public readonly struct RoleId public EntityId EntityId { get; } /// - /// Initializes a new instance of the struct. + /// Initializes a new instance of the class. /// /// The tenant identifier. /// The entity identifier. - public RoleId(TenantId? tenantId, Guid entityId) : this(tenantId, Convert.ToBase64String(entityId.ToByteArray()).ToUriSafeBase64()) - { - } - /// - /// Initializes a new instance of the struct. - /// - /// The tenant identifier. - /// The entity identifier. - public RoleId(TenantId? tenantId, string entityId) + public RoleId(TenantId? tenantId, EntityId entityId) { + StreamId = new(tenantId == null ? entityId.Value : string.Join(Separator, tenantId, entityId)); TenantId = tenantId; - EntityId = new(entityId); - StreamId = new(tenantId.HasValue ? string.Join(Separator, tenantId, entityId) : entityId); + EntityId = entityId; } + /// - /// Initializes a new instance of the struct. + /// Initializes a new instance of the class. /// - /// A stream identifier. + /// The identifier of the event stream. public RoleId(StreamId streamId) { StreamId = streamId; string[] values = streamId.Value.Split(Separator); - TenantId = values.Length == 2 ? new(values[0]) : null; + if (values.Length > 2) + { + throw new ArgumentException($"The value '{streamId}' is not a valid role ID.", nameof(streamId)); + } + else if (values.Length == 2) + { + TenantId = new(values.First()); + } EntityId = new(values.Last()); } @@ -67,7 +67,7 @@ public RoleId(StreamId streamId) /// /// The tenant identifier. /// The generated identifier. - public static RoleId NewId(TenantId? tenantId = null) => new(tenantId, Guid.NewGuid()); + public static RoleId NewId(TenantId? tenantId = null) => new(tenantId, EntityId.NewId()); /// /// Returns a value indicating whether or not the specified identifiers are equal. diff --git a/lib/Logitar.Identity.Core/Sessions/SessionId.cs b/lib/Logitar.Identity.Core/Sessions/SessionId.cs index 85fb717..1a4508b 100644 --- a/lib/Logitar.Identity.Core/Sessions/SessionId.cs +++ b/lib/Logitar.Identity.Core/Sessions/SessionId.cs @@ -7,6 +7,11 @@ namespace Logitar.Identity.Core.Sessions; /// public readonly struct SessionId { + /// + /// The separator between the tenant ID and the entity ID. + /// + private const char Separator = ':'; + /// /// Gets the identifier of the event stream. /// @@ -26,31 +31,35 @@ public readonly struct SessionId public EntityId EntityId { get; } /// - /// Initializes a new instance of the struct. + /// Initializes a new instance of the class. /// /// The tenant identifier. /// The entity identifier. - public SessionId(TenantId? tenantId, Guid entityId) : this(tenantId, Convert.ToBase64String(entityId.ToByteArray()).ToUriSafeBase64()) - { - } - /// - /// Initializes a new instance of the struct. - /// - /// The tenant identifier. - /// The entity identifier. - public SessionId(TenantId? tenantId, string entityId) + public SessionId(TenantId? tenantId, EntityId entityId) { + StreamId = new(tenantId == null ? entityId.Value : string.Join(Separator, tenantId, entityId)); TenantId = tenantId; - EntityId = new(entityId); - StreamId = new(tenantId.HasValue ? $"{tenantId}:{entityId}" : entityId); + EntityId = entityId; } + /// - /// Initializes a new instance of the struct. + /// Initializes a new instance of the class. /// - /// A stream identifier. + /// The identifier of the event stream. public SessionId(StreamId streamId) { StreamId = streamId; + + string[] values = streamId.Value.Split(Separator); + if (values.Length > 2) + { + throw new ArgumentException($"The value '{streamId}' is not a valid session ID.", nameof(streamId)); + } + else if (values.Length == 2) + { + TenantId = new(values.First()); + } + EntityId = new(values.Last()); } /// @@ -58,7 +67,7 @@ public SessionId(StreamId streamId) /// /// The tenant identifier. /// The generated identifier. - public static SessionId NewId(TenantId? tenantId = null) => new(tenantId, Guid.NewGuid()); + public static SessionId NewId(TenantId? tenantId = null) => new(tenantId, EntityId.NewId()); /// /// Returns a value indicating whether or not the specified identifiers are equal. diff --git a/lib/Logitar.Identity.Core/Users/UserId.cs b/lib/Logitar.Identity.Core/Users/UserId.cs index 93c9c8c..d01350c 100644 --- a/lib/Logitar.Identity.Core/Users/UserId.cs +++ b/lib/Logitar.Identity.Core/Users/UserId.cs @@ -3,7 +3,7 @@ namespace Logitar.Identity.Core.Users; /// -/// Represents the identifier of an user. +/// Represents the identifier of a user. /// public readonly struct UserId { @@ -31,34 +31,34 @@ public readonly struct UserId public EntityId EntityId { get; } /// - /// Initializes a new instance of the struct. + /// Initializes a new instance of the class. /// /// The tenant identifier. /// The entity identifier. - public UserId(TenantId? tenantId, Guid entityId) : this(tenantId, Convert.ToBase64String(entityId.ToByteArray()).ToUriSafeBase64()) - { - } - /// - /// Initializes a new instance of the struct. - /// - /// The tenant identifier. - /// The entity identifier. - public UserId(TenantId? tenantId, string entityId) + public UserId(TenantId? tenantId, EntityId entityId) { + StreamId = new(tenantId == null ? entityId.Value : string.Join(Separator, tenantId, entityId)); TenantId = tenantId; - EntityId = new(entityId); - StreamId = new(tenantId.HasValue ? string.Join(Separator, tenantId, entityId) : entityId); + EntityId = entityId; } + /// - /// Initializes a new instance of the struct. + /// Initializes a new instance of the class. /// - /// A stream identifier. + /// The identifier of the event stream. public UserId(StreamId streamId) { StreamId = streamId; string[] values = streamId.Value.Split(Separator); - TenantId = values.Length == 2 ? new(values[0]) : null; + if (values.Length > 2) + { + throw new ArgumentException($"The value '{streamId}' is not a valid user ID.", nameof(streamId)); + } + else if (values.Length == 2) + { + TenantId = new(values.First()); + } EntityId = new(values.Last()); } @@ -67,7 +67,7 @@ public UserId(StreamId streamId) /// /// The tenant identifier. /// The generated identifier. - public static UserId NewId(TenantId? tenantId = null) => new(tenantId, Guid.NewGuid()); + public static UserId NewId(TenantId? tenantId = null) => new(tenantId, EntityId.NewId()); /// /// Returns a value indicating whether or not the specified identifiers are equal. diff --git a/lib/Logitar.Identity.Core/Users/UserManager.cs b/lib/Logitar.Identity.Core/Users/UserManager.cs index ee3264d..0879574 100644 --- a/lib/Logitar.Identity.Core/Users/UserManager.cs +++ b/lib/Logitar.Identity.Core/Users/UserManager.cs @@ -75,7 +75,8 @@ public virtual async Task FindAsync(string? tenantIdValue, string id UserId? userId = null; try { - userId = new(tenantId, id); + EntityId entityId = new(id); + userId = new(tenantId, entityId); } catch (Exception) { diff --git a/lib/Logitar.Identity.Infrastructure/Converters/ApiKeyIdConverter.cs b/lib/Logitar.Identity.Infrastructure/Converters/ApiKeyIdConverter.cs new file mode 100644 index 0000000..fe59271 --- /dev/null +++ b/lib/Logitar.Identity.Infrastructure/Converters/ApiKeyIdConverter.cs @@ -0,0 +1,23 @@ +using Logitar.EventSourcing; +using Logitar.Identity.Core.ApiKeys; + +namespace Logitar.Identity.Infrastructure.Converters; + +public class ApiKeyIdConverter : JsonConverter +{ + public override ApiKeyId Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + string? value = reader.GetString(); + if (string.IsNullOrWhiteSpace(value)) + { + return new ApiKeyId(); + } + StreamId streamId = new(value); + return new(streamId); + } + + public override void Write(Utf8JsonWriter writer, ApiKeyId apiKeyId, JsonSerializerOptions options) + { + writer.WriteStringValue(apiKeyId.Value); + } +} diff --git a/lib/Logitar.Identity.Infrastructure/Converters/CustomIdentifierConverter.cs b/lib/Logitar.Identity.Infrastructure/Converters/CustomIdentifierConverter.cs new file mode 100644 index 0000000..aacea57 --- /dev/null +++ b/lib/Logitar.Identity.Infrastructure/Converters/CustomIdentifierConverter.cs @@ -0,0 +1,16 @@ +using Logitar.Identity.Core; + +namespace Logitar.Identity.Infrastructure.Converters; + +public class CustomIdentifierConverter : JsonConverter +{ + public override CustomIdentifier? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + return CustomIdentifier.TryCreate(reader.GetString()); + } + + public override void Write(Utf8JsonWriter writer, CustomIdentifier customIdentifier, JsonSerializerOptions options) + { + writer.WriteStringValue(customIdentifier.Value); + } +} diff --git a/lib/Logitar.Identity.Infrastructure/Converters/DescriptionConverter.cs b/lib/Logitar.Identity.Infrastructure/Converters/DescriptionConverter.cs new file mode 100644 index 0000000..5a3177e --- /dev/null +++ b/lib/Logitar.Identity.Infrastructure/Converters/DescriptionConverter.cs @@ -0,0 +1,16 @@ +using Logitar.Identity.Core; + +namespace Logitar.Identity.Infrastructure.Converters; + +public class DescriptionConverter : JsonConverter +{ + public override Description? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + return Description.TryCreate(reader.GetString()); + } + + public override void Write(Utf8JsonWriter writer, Description description, JsonSerializerOptions options) + { + writer.WriteStringValue(description.Value); + } +} diff --git a/lib/Logitar.Identity.Infrastructure/Converters/DisplayNameConverter.cs b/lib/Logitar.Identity.Infrastructure/Converters/DisplayNameConverter.cs new file mode 100644 index 0000000..ec81ab1 --- /dev/null +++ b/lib/Logitar.Identity.Infrastructure/Converters/DisplayNameConverter.cs @@ -0,0 +1,16 @@ +using Logitar.Identity.Core; + +namespace Logitar.Identity.Infrastructure.Converters; + +public class DisplayNameConverter : JsonConverter +{ + public override DisplayName? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + return DisplayName.TryCreate(reader.GetString()); + } + + public override void Write(Utf8JsonWriter writer, DisplayName displayName, JsonSerializerOptions options) + { + writer.WriteStringValue(displayName.Value); + } +} diff --git a/lib/Logitar.Identity.Infrastructure/Converters/EntityIdConverter.cs b/lib/Logitar.Identity.Infrastructure/Converters/EntityIdConverter.cs new file mode 100644 index 0000000..f049c71 --- /dev/null +++ b/lib/Logitar.Identity.Infrastructure/Converters/EntityIdConverter.cs @@ -0,0 +1,17 @@ +using Logitar.Identity.Core; + +namespace Logitar.Identity.Infrastructure.Converters; + +public class EntityIdConverter : JsonConverter +{ + public override EntityId Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + string? value = reader.GetString(); + return string.IsNullOrWhiteSpace(value) ? new EntityId() : new(value); + } + + public override void Write(Utf8JsonWriter writer, EntityId entityId, JsonSerializerOptions options) + { + writer.WriteStringValue(entityId.Value); + } +} diff --git a/lib/Logitar.Identity.Infrastructure/Converters/GenderConverter.cs b/lib/Logitar.Identity.Infrastructure/Converters/GenderConverter.cs new file mode 100644 index 0000000..1d04a34 --- /dev/null +++ b/lib/Logitar.Identity.Infrastructure/Converters/GenderConverter.cs @@ -0,0 +1,16 @@ +using Logitar.Identity.Core.Users; + +namespace Logitar.Identity.Infrastructure.Converters; + +public class GenderConverter : JsonConverter +{ + public override Gender? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + return Gender.TryCreate(reader.GetString()); + } + + public override void Write(Utf8JsonWriter writer, Gender gender, JsonSerializerOptions options) + { + writer.WriteStringValue(gender.Value); + } +} diff --git a/lib/Logitar.Identity.Infrastructure/Converters/IdentifierConverter.cs b/lib/Logitar.Identity.Infrastructure/Converters/IdentifierConverter.cs index c587867..d4ae674 100644 --- a/lib/Logitar.Identity.Infrastructure/Converters/IdentifierConverter.cs +++ b/lib/Logitar.Identity.Infrastructure/Converters/IdentifierConverter.cs @@ -1,7 +1,5 @@ using Logitar.Identity.Core; using System.Diagnostics.CodeAnalysis; -using System.Text.Json; -using System.Text.Json.Serialization; namespace Logitar.Identity.Infrastructure.Converters; diff --git a/lib/Logitar.Identity.Infrastructure/Converters/LocaleConverter.cs b/lib/Logitar.Identity.Infrastructure/Converters/LocaleConverter.cs new file mode 100644 index 0000000..b00aed5 --- /dev/null +++ b/lib/Logitar.Identity.Infrastructure/Converters/LocaleConverter.cs @@ -0,0 +1,16 @@ +using Logitar.Identity.Core; + +namespace Logitar.Identity.Infrastructure.Converters; + +public class LocaleConverter : JsonConverter +{ + public override Locale? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + return Locale.TryCreate(reader.GetString()); + } + + public override void Write(Utf8JsonWriter writer, Locale locale, JsonSerializerOptions options) + { + writer.WriteStringValue(locale.Code); + } +} diff --git a/lib/Logitar.Identity.Infrastructure/Converters/OneTimePasswordIdConverter.cs b/lib/Logitar.Identity.Infrastructure/Converters/OneTimePasswordIdConverter.cs new file mode 100644 index 0000000..6292b13 --- /dev/null +++ b/lib/Logitar.Identity.Infrastructure/Converters/OneTimePasswordIdConverter.cs @@ -0,0 +1,23 @@ +using Logitar.EventSourcing; +using Logitar.Identity.Core.Passwords; + +namespace Logitar.Identity.Infrastructure.Converters; + +public class OneTimePasswordIdConverter : JsonConverter +{ + public override OneTimePasswordId Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + string? value = reader.GetString(); + if (string.IsNullOrWhiteSpace(value)) + { + return new OneTimePasswordId(); + } + StreamId streamId = new(value); + return new(streamId); + } + + public override void Write(Utf8JsonWriter writer, OneTimePasswordId oneTimePasswordId, JsonSerializerOptions options) + { + writer.WriteStringValue(oneTimePasswordId.Value); + } +} diff --git a/old/src/Logitar.Identity.Infrastructure/Converters/PasswordConverter.cs b/lib/Logitar.Identity.Infrastructure/Converters/PasswordConverter.cs similarity index 93% rename from old/src/Logitar.Identity.Infrastructure/Converters/PasswordConverter.cs rename to lib/Logitar.Identity.Infrastructure/Converters/PasswordConverter.cs index 6847434..0b55c89 100644 --- a/old/src/Logitar.Identity.Infrastructure/Converters/PasswordConverter.cs +++ b/lib/Logitar.Identity.Infrastructure/Converters/PasswordConverter.cs @@ -1,4 +1,4 @@ -using Logitar.Identity.Domain.Passwords; +using Logitar.Identity.Core.Passwords; namespace Logitar.Identity.Infrastructure.Converters; diff --git a/lib/Logitar.Identity.Infrastructure/Converters/PersonNameConverter.cs b/lib/Logitar.Identity.Infrastructure/Converters/PersonNameConverter.cs new file mode 100644 index 0000000..8a7ad56 --- /dev/null +++ b/lib/Logitar.Identity.Infrastructure/Converters/PersonNameConverter.cs @@ -0,0 +1,16 @@ +using Logitar.Identity.Core.Users; + +namespace Logitar.Identity.Infrastructure.Converters; + +public class PersonNameConverter : JsonConverter +{ + public override PersonName? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + return PersonName.TryCreate(reader.GetString()); + } + + public override void Write(Utf8JsonWriter writer, PersonName personName, JsonSerializerOptions options) + { + writer.WriteStringValue(personName.Value); + } +} diff --git a/lib/Logitar.Identity.Infrastructure/Converters/RoleIdConverter.cs b/lib/Logitar.Identity.Infrastructure/Converters/RoleIdConverter.cs new file mode 100644 index 0000000..f969cc7 --- /dev/null +++ b/lib/Logitar.Identity.Infrastructure/Converters/RoleIdConverter.cs @@ -0,0 +1,23 @@ +using Logitar.EventSourcing; +using Logitar.Identity.Core.Roles; + +namespace Logitar.Identity.Infrastructure.Converters; + +public class RoleIdConverter : JsonConverter +{ + public override RoleId Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + string? value = reader.GetString(); + if (string.IsNullOrWhiteSpace(value)) + { + return new RoleId(); + } + StreamId streamId = new(value); + return new(streamId); + } + + public override void Write(Utf8JsonWriter writer, RoleId roleId, JsonSerializerOptions options) + { + writer.WriteStringValue(roleId.Value); + } +} diff --git a/lib/Logitar.Identity.Infrastructure/Converters/SessionIdConverter.cs b/lib/Logitar.Identity.Infrastructure/Converters/SessionIdConverter.cs new file mode 100644 index 0000000..15a1e96 --- /dev/null +++ b/lib/Logitar.Identity.Infrastructure/Converters/SessionIdConverter.cs @@ -0,0 +1,23 @@ +using Logitar.EventSourcing; +using Logitar.Identity.Core.Sessions; + +namespace Logitar.Identity.Infrastructure.Converters; + +public class SessionIdConverter : JsonConverter +{ + public override SessionId Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + string? value = reader.GetString(); + if (string.IsNullOrWhiteSpace(value)) + { + return new SessionId(); + } + StreamId streamId = new(value); + return new(streamId); + } + + public override void Write(Utf8JsonWriter writer, SessionId sessionId, JsonSerializerOptions options) + { + writer.WriteStringValue(sessionId.Value); + } +} diff --git a/old/src/Logitar.Identity.Infrastructure/Converters/TenantIdConverter.cs b/lib/Logitar.Identity.Infrastructure/Converters/TenantIdConverter.cs similarity index 52% rename from old/src/Logitar.Identity.Infrastructure/Converters/TenantIdConverter.cs rename to lib/Logitar.Identity.Infrastructure/Converters/TenantIdConverter.cs index a69813c..1ca6ec0 100644 --- a/old/src/Logitar.Identity.Infrastructure/Converters/TenantIdConverter.cs +++ b/lib/Logitar.Identity.Infrastructure/Converters/TenantIdConverter.cs @@ -1,12 +1,13 @@ -using Logitar.Identity.Domain.Shared; +using Logitar.Identity.Core; namespace Logitar.Identity.Infrastructure.Converters; public class TenantIdConverter : JsonConverter { - public override TenantId? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + public override TenantId Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { - return TenantId.TryCreate(reader.GetString()); + string? value = reader.GetString(); + return string.IsNullOrWhiteSpace(value) ? new TenantId() : new(value); } public override void Write(Utf8JsonWriter writer, TenantId tenantId, JsonSerializerOptions options) diff --git a/lib/Logitar.Identity.Infrastructure/Converters/TimeZoneConverter.cs b/lib/Logitar.Identity.Infrastructure/Converters/TimeZoneConverter.cs new file mode 100644 index 0000000..ba0ccff --- /dev/null +++ b/lib/Logitar.Identity.Infrastructure/Converters/TimeZoneConverter.cs @@ -0,0 +1,16 @@ +using TimeZone = Logitar.Identity.Core.TimeZone; + +namespace Logitar.Identity.Infrastructure.Converters; + +public class TimeZoneConverter : JsonConverter +{ + public override TimeZone? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + return TimeZone.TryCreate(reader.GetString()); + } + + public override void Write(Utf8JsonWriter writer, TimeZone timeZone, JsonSerializerOptions options) + { + writer.WriteStringValue(timeZone.Id); + } +} diff --git a/lib/Logitar.Identity.Infrastructure/Converters/UniqueNameConverter.cs b/lib/Logitar.Identity.Infrastructure/Converters/UniqueNameConverter.cs new file mode 100644 index 0000000..0981846 --- /dev/null +++ b/lib/Logitar.Identity.Infrastructure/Converters/UniqueNameConverter.cs @@ -0,0 +1,22 @@ +using Logitar.Identity.Core; +using Logitar.Identity.Core.Settings; + +namespace Logitar.Identity.Infrastructure.Converters; + +public class UniqueNameConverter : JsonConverter +{ + private readonly UniqueNameSettings _uniqueNameSettings = new() + { + AllowedCharacters = null // NOTE(fpion): strict validation is not required when deserializing an unique name. + }; + + public override UniqueName? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + return UniqueName.TryCreate(reader.GetString(), _uniqueNameSettings); + } + + public override void Write(Utf8JsonWriter writer, UniqueName uniqueName, JsonSerializerOptions options) + { + writer.WriteStringValue(uniqueName.Value); + } +} diff --git a/lib/Logitar.Identity.Infrastructure/Converters/UrlConverter.cs b/lib/Logitar.Identity.Infrastructure/Converters/UrlConverter.cs new file mode 100644 index 0000000..3e1c5ee --- /dev/null +++ b/lib/Logitar.Identity.Infrastructure/Converters/UrlConverter.cs @@ -0,0 +1,16 @@ +using Logitar.Identity.Core; + +namespace Logitar.Identity.Infrastructure.Converters; + +public class UrlConverter : JsonConverter +{ + public override Url? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + return Url.TryCreate(reader.GetString()); + } + + public override void Write(Utf8JsonWriter writer, Url url, JsonSerializerOptions options) + { + writer.WriteStringValue(url.Value); + } +} diff --git a/lib/Logitar.Identity.Infrastructure/Converters/UserIdConverter.cs b/lib/Logitar.Identity.Infrastructure/Converters/UserIdConverter.cs new file mode 100644 index 0000000..59b59d4 --- /dev/null +++ b/lib/Logitar.Identity.Infrastructure/Converters/UserIdConverter.cs @@ -0,0 +1,23 @@ +using Logitar.EventSourcing; +using Logitar.Identity.Core.Users; + +namespace Logitar.Identity.Infrastructure.Converters; + +public class UserIdConverter : JsonConverter +{ + public override UserId Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + string? value = reader.GetString(); + if (string.IsNullOrWhiteSpace(value)) + { + return new UserId(); + } + StreamId streamId = new(value); + return new(streamId); + } + + public override void Write(Utf8JsonWriter writer, UserId userId, JsonSerializerOptions options) + { + writer.WriteStringValue(userId.Value); + } +} diff --git a/lib/Logitar.Identity.Infrastructure/Logitar.Identity.Infrastructure.csproj b/lib/Logitar.Identity.Infrastructure/Logitar.Identity.Infrastructure.csproj index 05b0c4d..53ea6b6 100644 --- a/lib/Logitar.Identity.Infrastructure/Logitar.Identity.Infrastructure.csproj +++ b/lib/Logitar.Identity.Infrastructure/Logitar.Identity.Infrastructure.csproj @@ -18,4 +18,9 @@ + + + + + diff --git a/old/src/Logitar.Identity.Infrastructure/Converters/ApiKeyIdConverter.cs b/old/src/Logitar.Identity.Infrastructure/Converters/ApiKeyIdConverter.cs deleted file mode 100644 index 8cc6e12..0000000 --- a/old/src/Logitar.Identity.Infrastructure/Converters/ApiKeyIdConverter.cs +++ /dev/null @@ -1,16 +0,0 @@ -using Logitar.Identity.Domain.ApiKeys; - -namespace Logitar.Identity.Infrastructure.Converters; - -public class ApiKeyIdConverter : JsonConverter -{ - public override ApiKeyId? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - return ApiKeyId.TryCreate(reader.GetString()); - } - - public override void Write(Utf8JsonWriter writer, ApiKeyId apiKeyId, JsonSerializerOptions options) - { - writer.WriteStringValue(apiKeyId.Value); - } -} diff --git a/old/src/Logitar.Identity.Infrastructure/Converters/DescriptionConverter.cs b/old/src/Logitar.Identity.Infrastructure/Converters/DescriptionConverter.cs deleted file mode 100644 index 3f48c19..0000000 --- a/old/src/Logitar.Identity.Infrastructure/Converters/DescriptionConverter.cs +++ /dev/null @@ -1,16 +0,0 @@ -using Logitar.Identity.Domain.Shared; - -namespace Logitar.Identity.Infrastructure.Converters; - -public class DescriptionConverter : JsonConverter -{ - public override DescriptionUnit? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - return DescriptionUnit.TryCreate(reader.GetString()); - } - - public override void Write(Utf8JsonWriter writer, DescriptionUnit description, JsonSerializerOptions options) - { - writer.WriteStringValue(description.Value); - } -} diff --git a/old/src/Logitar.Identity.Infrastructure/Converters/DisplayNameConverter.cs b/old/src/Logitar.Identity.Infrastructure/Converters/DisplayNameConverter.cs deleted file mode 100644 index 2a9a3a4..0000000 --- a/old/src/Logitar.Identity.Infrastructure/Converters/DisplayNameConverter.cs +++ /dev/null @@ -1,16 +0,0 @@ -using Logitar.Identity.Domain.Shared; - -namespace Logitar.Identity.Infrastructure.Converters; - -public class DisplayNameConverter : JsonConverter -{ - public override DisplayNameUnit? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - return DisplayNameUnit.TryCreate(reader.GetString()); - } - - public override void Write(Utf8JsonWriter writer, DisplayNameUnit displayName, JsonSerializerOptions options) - { - writer.WriteStringValue(displayName.Value); - } -} diff --git a/old/src/Logitar.Identity.Infrastructure/Converters/GenderConverter.cs b/old/src/Logitar.Identity.Infrastructure/Converters/GenderConverter.cs deleted file mode 100644 index 7411884..0000000 --- a/old/src/Logitar.Identity.Infrastructure/Converters/GenderConverter.cs +++ /dev/null @@ -1,16 +0,0 @@ -using Logitar.Identity.Domain.Users; - -namespace Logitar.Identity.Infrastructure.Converters; - -public class GenderConverter : JsonConverter -{ - public override GenderUnit? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - return GenderUnit.TryCreate(reader.GetString()); - } - - public override void Write(Utf8JsonWriter writer, GenderUnit gender, JsonSerializerOptions options) - { - writer.WriteStringValue(gender.Value); - } -} diff --git a/old/src/Logitar.Identity.Infrastructure/Converters/LocaleConverter.cs b/old/src/Logitar.Identity.Infrastructure/Converters/LocaleConverter.cs deleted file mode 100644 index 7063dd0..0000000 --- a/old/src/Logitar.Identity.Infrastructure/Converters/LocaleConverter.cs +++ /dev/null @@ -1,16 +0,0 @@ -using Logitar.Identity.Domain.Shared; - -namespace Logitar.Identity.Infrastructure.Converters; - -public class LocaleConverter : JsonConverter -{ - public override LocaleUnit? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - return LocaleUnit.TryCreate(reader.GetString()); - } - - public override void Write(Utf8JsonWriter writer, LocaleUnit locale, JsonSerializerOptions options) - { - writer.WriteStringValue(locale.Code); - } -} diff --git a/old/src/Logitar.Identity.Infrastructure/Converters/OneTimePasswordIdConverter.cs b/old/src/Logitar.Identity.Infrastructure/Converters/OneTimePasswordIdConverter.cs deleted file mode 100644 index af4836d..0000000 --- a/old/src/Logitar.Identity.Infrastructure/Converters/OneTimePasswordIdConverter.cs +++ /dev/null @@ -1,16 +0,0 @@ -using Logitar.Identity.Domain.Passwords; - -namespace Logitar.Identity.Infrastructure.Converters; - -public class OneTimePasswordIdConverter : JsonConverter -{ - public override OneTimePasswordId? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - return OneTimePasswordId.TryCreate(reader.GetString()); - } - - public override void Write(Utf8JsonWriter writer, OneTimePasswordId oneTimePasswordId, JsonSerializerOptions options) - { - writer.WriteStringValue(oneTimePasswordId.Value); - } -} diff --git a/old/src/Logitar.Identity.Infrastructure/Converters/PersonNameConverter.cs b/old/src/Logitar.Identity.Infrastructure/Converters/PersonNameConverter.cs deleted file mode 100644 index 90cec39..0000000 --- a/old/src/Logitar.Identity.Infrastructure/Converters/PersonNameConverter.cs +++ /dev/null @@ -1,16 +0,0 @@ -using Logitar.Identity.Domain.Users; - -namespace Logitar.Identity.Infrastructure.Converters; - -public class PersonNameConverter : JsonConverter -{ - public override PersonNameUnit? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - return PersonNameUnit.TryCreate(reader.GetString()); - } - - public override void Write(Utf8JsonWriter writer, PersonNameUnit personName, JsonSerializerOptions options) - { - writer.WriteStringValue(personName.Value); - } -} diff --git a/old/src/Logitar.Identity.Infrastructure/Converters/RoleIdConverter.cs b/old/src/Logitar.Identity.Infrastructure/Converters/RoleIdConverter.cs deleted file mode 100644 index 295762a..0000000 --- a/old/src/Logitar.Identity.Infrastructure/Converters/RoleIdConverter.cs +++ /dev/null @@ -1,16 +0,0 @@ -using Logitar.Identity.Domain.Roles; - -namespace Logitar.Identity.Infrastructure.Converters; - -public class RoleIdConverter : JsonConverter -{ - public override RoleId? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - return RoleId.TryCreate(reader.GetString()); - } - - public override void Write(Utf8JsonWriter writer, RoleId roleId, JsonSerializerOptions options) - { - writer.WriteStringValue(roleId.Value); - } -} diff --git a/old/src/Logitar.Identity.Infrastructure/Converters/SessionIdConverter.cs b/old/src/Logitar.Identity.Infrastructure/Converters/SessionIdConverter.cs deleted file mode 100644 index a30d780..0000000 --- a/old/src/Logitar.Identity.Infrastructure/Converters/SessionIdConverter.cs +++ /dev/null @@ -1,16 +0,0 @@ -using Logitar.Identity.Domain.Sessions; - -namespace Logitar.Identity.Infrastructure.Converters; - -public class SessionIdConverter : JsonConverter -{ - public override SessionId? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - return SessionId.TryCreate(reader.GetString()); - } - - public override void Write(Utf8JsonWriter writer, SessionId sessionId, JsonSerializerOptions options) - { - writer.WriteStringValue(sessionId.Value); - } -} diff --git a/old/src/Logitar.Identity.Infrastructure/Converters/TimeZoneConverter.cs b/old/src/Logitar.Identity.Infrastructure/Converters/TimeZoneConverter.cs deleted file mode 100644 index 1c05745..0000000 --- a/old/src/Logitar.Identity.Infrastructure/Converters/TimeZoneConverter.cs +++ /dev/null @@ -1,16 +0,0 @@ -using Logitar.Identity.Domain.Shared; - -namespace Logitar.Identity.Infrastructure.Converters; - -public class TimeZoneConverter : JsonConverter -{ - public override TimeZoneUnit? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - return TimeZoneUnit.TryCreate(reader.GetString()); - } - - public override void Write(Utf8JsonWriter writer, TimeZoneUnit timeZone, JsonSerializerOptions options) - { - writer.WriteStringValue(timeZone.Id); - } -} diff --git a/old/src/Logitar.Identity.Infrastructure/Converters/UniqueNameConverter.cs b/old/src/Logitar.Identity.Infrastructure/Converters/UniqueNameConverter.cs deleted file mode 100644 index 44ace47..0000000 --- a/old/src/Logitar.Identity.Infrastructure/Converters/UniqueNameConverter.cs +++ /dev/null @@ -1,22 +0,0 @@ -using Logitar.Identity.Domain.Settings; -using Logitar.Identity.Domain.Shared; - -namespace Logitar.Identity.Infrastructure.Converters; - -public class UniqueNameConverter : JsonConverter -{ - private readonly UniqueNameSettings _uniqueNameSettings = new() - { - AllowedCharacters = null // NOTE(fpion): strict validation is not required when deserializing an unique name. - }; - - public override UniqueNameUnit? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - return UniqueNameUnit.TryCreate(_uniqueNameSettings, reader.GetString()); - } - - public override void Write(Utf8JsonWriter writer, UniqueNameUnit uniqueName, JsonSerializerOptions options) - { - writer.WriteStringValue(uniqueName.Value); - } -} diff --git a/old/src/Logitar.Identity.Infrastructure/Converters/UrlConverter.cs b/old/src/Logitar.Identity.Infrastructure/Converters/UrlConverter.cs deleted file mode 100644 index 6a6e339..0000000 --- a/old/src/Logitar.Identity.Infrastructure/Converters/UrlConverter.cs +++ /dev/null @@ -1,16 +0,0 @@ -using Logitar.Identity.Domain.Shared; - -namespace Logitar.Identity.Infrastructure.Converters; - -public class UrlConverter : JsonConverter -{ - public override UrlUnit? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - return UrlUnit.TryCreate(reader.GetString()); - } - - public override void Write(Utf8JsonWriter writer, UrlUnit url, JsonSerializerOptions options) - { - writer.WriteStringValue(url.Value); - } -} diff --git a/old/src/Logitar.Identity.Infrastructure/Converters/UserIdConverter.cs b/old/src/Logitar.Identity.Infrastructure/Converters/UserIdConverter.cs deleted file mode 100644 index 7a2bd27..0000000 --- a/old/src/Logitar.Identity.Infrastructure/Converters/UserIdConverter.cs +++ /dev/null @@ -1,16 +0,0 @@ -using Logitar.Identity.Domain.Users; - -namespace Logitar.Identity.Infrastructure.Converters; - -public class UserIdConverter : JsonConverter -{ - public override UserId? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - return UserId.TryCreate(reader.GetString()); - } - - public override void Write(Utf8JsonWriter writer, UserId userId, JsonSerializerOptions options) - { - writer.WriteStringValue(userId.Value); - } -} diff --git a/tests/Logitar.Identity.Core.UnitTests/ApiKeys/ApiKeyIdTests.cs b/tests/Logitar.Identity.Core.UnitTests/ApiKeys/ApiKeyIdTests.cs index 803a6ee..85d4edc 100644 --- a/tests/Logitar.Identity.Core.UnitTests/ApiKeys/ApiKeyIdTests.cs +++ b/tests/Logitar.Identity.Core.UnitTests/ApiKeys/ApiKeyIdTests.cs @@ -5,21 +5,7 @@ namespace Logitar.Identity.Core.ApiKeys; [Trait(Traits.Category, Categories.Unit)] public class ApiKeyIdTests { - [Theory(DisplayName = "ctor: it should construct the correct ID from a Guid.")] - [InlineData(null)] - [InlineData("TenantId")] - public void Given_GuidValue_When_ctor_Then_CorrectIdConstructed(string? tenantIdValue) - { - TenantId? tenantId = tenantIdValue == null ? null : new(tenantIdValue); - Guid entityId = Guid.NewGuid(); - - ApiKeyId id = new(tenantId, entityId); - - Assert.Equal(tenantId, id.TenantId); - Assert.Equal(entityId, id.EntityId.ToGuid()); - } - - [Theory(DisplayName = "ctor: it should construct the correct ID from a StreamId.")] + [Theory(DisplayName = "ctor: it should construct the correct ID from a stream ID.")] [InlineData(null)] [InlineData("TenantId")] public void Given_StreamId_When_ctor_Then_CorrectIdConstructed(string? tenantIdValue) @@ -34,25 +20,25 @@ public void Given_StreamId_When_ctor_Then_CorrectIdConstructed(string? tenantIdV Assert.Equal(entityId, id.EntityId); } - [Theory(DisplayName = "ctor: it should construct the correct ID from a string.")] + [Theory(DisplayName = "ctor: it should construct the correct ID from a tenant ID and an entity ID.")] [InlineData(null)] [InlineData("TenantId")] - public void Given_StringValue_When_ctor_Then_CorrectIdConstructed(string? tenantIdValue) + public void Given_TenantAndEntityId_When_ctor_Then_CorrectIdConstructed(string? tenantIdValue) { TenantId? tenantId = tenantIdValue == null ? null : new(tenantIdValue); - string entityId = "EntityId"; + EntityId entityId = EntityId.NewId(); ApiKeyId id = new(tenantId, entityId); Assert.Equal(tenantId, id.TenantId); - Assert.Equal(entityId, id.EntityId.Value); + Assert.Equal(entityId, id.EntityId); } [Fact(DisplayName = "Equals: it should return false when the IDs are different.")] public void Given_DifferentIds_When_Equals_Then_FalseReturned() { - ApiKeyId id1 = new(TenantId.NewId(), Guid.NewGuid()); - ApiKeyId id2 = new(tenantId: null, id1.EntityId.Value); + ApiKeyId id1 = new(TenantId.NewId(), EntityId.NewId()); + ApiKeyId id2 = new(tenantId: null, id1.EntityId); Assert.False(id1.Equals(id2)); } @@ -68,7 +54,7 @@ public void Given_DifferentTypes_When_Equals_Then_FalseReturned(object? value) [Fact(DisplayName = "Equals: it should return true when the IDs are the same.")] public void Given_SameIds_When_Equals_Then_TrueReturned() { - ApiKeyId id1 = new(TenantId.NewId(), Guid.NewGuid()); + ApiKeyId id1 = new(TenantId.NewId(), EntityId.NewId()); ApiKeyId id2 = new(id1.StreamId); Assert.True(id1.Equals(id1)); Assert.True(id1.Equals(id2)); @@ -77,15 +63,15 @@ public void Given_SameIds_When_Equals_Then_TrueReturned() [Fact(DisplayName = "EqualOperator: it should return false when the IDs are different.")] public void Given_DifferentIds_When_EqualOperator_Then_FalseReturned() { - ApiKeyId id1 = new(TenantId.NewId(), Guid.NewGuid()); - ApiKeyId id2 = new(tenantId: null, id1.EntityId.Value); + ApiKeyId id1 = new(TenantId.NewId(), EntityId.NewId()); + ApiKeyId id2 = new(tenantId: null, id1.EntityId); Assert.False(id1 == id2); } [Fact(DisplayName = "EqualOperator: it should return true when the IDs are the same.")] public void Given_SameIds_When_EqualOperator_Then_TrueReturned() { - ApiKeyId id1 = new(TenantId.NewId(), Guid.NewGuid()); + ApiKeyId id1 = new(TenantId.NewId(), EntityId.NewId()); ApiKeyId id2 = new(id1.StreamId); Assert.True(id1 == id2); } @@ -109,7 +95,7 @@ public void Given_TenantId_When_NewId_Then_NewRandomIdGenerated(string? tenantId public void Given_Id_When_GetHashCode_Then_CorrectHashCodeReturned(string? tenantIdValue) { TenantId? tenantId = tenantIdValue == null ? null : new(tenantIdValue); - Guid entityId = Guid.NewGuid(); + EntityId entityId = EntityId.NewId(); ApiKeyId id = new(tenantId, entityId); @@ -119,7 +105,7 @@ public void Given_Id_When_GetHashCode_Then_CorrectHashCodeReturned(string? tenan [Fact(DisplayName = "NotEqualOperator: it should return false when the IDs are the same.")] public void Given_SameIds_When_NotEqualOperator_Then_TrueReturned() { - ApiKeyId id1 = new(TenantId.NewId(), Guid.NewGuid()); + ApiKeyId id1 = new(TenantId.NewId(), EntityId.NewId()); ApiKeyId id2 = new(id1.StreamId); Assert.False(id1 != id2); } @@ -127,8 +113,8 @@ public void Given_SameIds_When_NotEqualOperator_Then_TrueReturned() [Fact(DisplayName = "NotEqualOperator: it should return true when the IDs are different.")] public void Given_DifferentIds_When_NotEqualOperator_Then_TrueReturned() { - ApiKeyId id1 = new(TenantId.NewId(), Guid.NewGuid()); - ApiKeyId id2 = new(tenantId: null, id1.EntityId.Value); + ApiKeyId id1 = new(TenantId.NewId(), EntityId.NewId()); + ApiKeyId id2 = new(tenantId: null, id1.EntityId); Assert.True(id1 != id2); } @@ -138,7 +124,7 @@ public void Given_DifferentIds_When_NotEqualOperator_Then_TrueReturned() public void Given_Id_When_ToString_Then_CorrectStringReturned(string? tenantIdValue) { TenantId? tenantId = tenantIdValue == null ? null : new(tenantIdValue); - Guid entityId = Guid.NewGuid(); + EntityId entityId = EntityId.NewId(); ApiKeyId id = new(tenantId, entityId); diff --git a/tests/Logitar.Identity.Core.UnitTests/ApiKeys/ApiKeyTests.cs b/tests/Logitar.Identity.Core.UnitTests/ApiKeys/ApiKeyTests.cs index 5dd0d76..a9352cd 100644 --- a/tests/Logitar.Identity.Core.UnitTests/ApiKeys/ApiKeyTests.cs +++ b/tests/Logitar.Identity.Core.UnitTests/ApiKeys/ApiKeyTests.cs @@ -37,7 +37,7 @@ public void Given_Role_When_AddRole_Then_RoleAdded() [Fact(DisplayName = "AddRole: it should throw TenantMismatchException when the role is in another tenant.")] public void Given_DifferentTenants_When_AddRole_Then_TenantMismatchException() { - Role role = new(new UniqueName(new UniqueNameSettings(), "manage_api"), actorId: null, new RoleId(TenantId.NewId(), Guid.NewGuid())); + Role role = new(new UniqueName(new UniqueNameSettings(), "manage_api"), actorId: null, new RoleId(TenantId.NewId(), EntityId.NewId())); var exception = Assert.Throws(() => _apiKey.AddRole(role)); Assert.Equal(_apiKey.TenantId?.Value, exception.ExpectedTenantId); @@ -201,7 +201,7 @@ public void Given_Expired_When_IsExpired_Then_TrueReturned() [Fact(DisplayName = "It should have the correct IDs.")] public void Given_ApiKey_When_getIds_Then_CorrectIds() { - ApiKeyId id = new(TenantId.NewId(), Guid.NewGuid()); + ApiKeyId id = new(TenantId.NewId(), EntityId.NewId()); ApiKey apiKey = new(_apiKey.DisplayName, _secret, actorId: null, id); Assert.Equal(id, apiKey.Id); Assert.Equal(id.TenantId, apiKey.TenantId); diff --git a/tests/Logitar.Identity.Core.UnitTests/Passwords/OneTimePasswordIdTests.cs b/tests/Logitar.Identity.Core.UnitTests/Passwords/OneTimePasswordIdTests.cs new file mode 100644 index 0000000..a0edccf --- /dev/null +++ b/tests/Logitar.Identity.Core.UnitTests/Passwords/OneTimePasswordIdTests.cs @@ -0,0 +1,133 @@ +using Logitar.EventSourcing; + +namespace Logitar.Identity.Core.Passwords; + +[Trait(Traits.Category, Categories.Unit)] +public class OneTimePasswordIdTests +{ + [Theory(DisplayName = "ctor: it should construct the correct ID from a stream ID.")] + [InlineData(null)] + [InlineData("TenantId")] + public void Given_StreamId_When_ctor_Then_CorrectIdConstructed(string? tenantIdValue) + { + TenantId? tenantId = tenantIdValue == null ? null : new(tenantIdValue); + EntityId entityId = EntityId.NewId(); + StreamId streamId = new(tenantId.HasValue ? string.Join(':', tenantId, entityId) : entityId.Value); + + OneTimePasswordId id = new(streamId); + + Assert.Equal(tenantId, id.TenantId); + Assert.Equal(entityId, id.EntityId); + } + + [Theory(DisplayName = "ctor: it should construct the correct ID from a tenant ID and an entity ID.")] + [InlineData(null)] + [InlineData("TenantId")] + public void Given_TenantAndEntityId_When_ctor_Then_CorrectIdConstructed(string? tenantIdValue) + { + TenantId? tenantId = tenantIdValue == null ? null : new(tenantIdValue); + EntityId entityId = EntityId.NewId(); + + OneTimePasswordId id = new(tenantId, entityId); + + Assert.Equal(tenantId, id.TenantId); + Assert.Equal(entityId, id.EntityId); + } + + [Fact(DisplayName = "Equals: it should return false when the IDs are different.")] + public void Given_DifferentIds_When_Equals_Then_FalseReturned() + { + OneTimePasswordId id1 = new(TenantId.NewId(), EntityId.NewId()); + OneTimePasswordId id2 = new(tenantId: null, id1.EntityId); + Assert.False(id1.Equals(id2)); + } + + [Theory(DisplayName = "Equals: it should return false when the object do not have the same types.")] + [InlineData(null)] + [InlineData(123)] + public void Given_DifferentTypes_When_Equals_Then_FalseReturned(object? value) + { + OneTimePasswordId id = OneTimePasswordId.NewId(); + Assert.False(id.Equals(value)); + } + + [Fact(DisplayName = "Equals: it should return true when the IDs are the same.")] + public void Given_SameIds_When_Equals_Then_TrueReturned() + { + OneTimePasswordId id1 = new(TenantId.NewId(), EntityId.NewId()); + OneTimePasswordId id2 = new(id1.StreamId); + Assert.True(id1.Equals(id1)); + Assert.True(id1.Equals(id2)); + } + + [Fact(DisplayName = "EqualOperator: it should return false when the IDs are different.")] + public void Given_DifferentIds_When_EqualOperator_Then_FalseReturned() + { + OneTimePasswordId id1 = new(TenantId.NewId(), EntityId.NewId()); + OneTimePasswordId id2 = new(tenantId: null, id1.EntityId); + Assert.False(id1 == id2); + } + + [Fact(DisplayName = "EqualOperator: it should return true when the IDs are the same.")] + public void Given_SameIds_When_EqualOperator_Then_TrueReturned() + { + OneTimePasswordId id1 = new(TenantId.NewId(), EntityId.NewId()); + OneTimePasswordId id2 = new(id1.StreamId); + Assert.True(id1 == id2); + } + + [Theory(DisplayName = "NewId: it should generate a new random ID with or without a tenant ID.")] + [InlineData(null)] + [InlineData("TenantId")] + public void Given_TenantId_When_NewId_Then_NewRandomIdGenerated(string? tenantIdValue) + { + TenantId? tenantId = tenantIdValue == null ? null : new(tenantIdValue); + + OneTimePasswordId id = OneTimePasswordId.NewId(tenantId); + + Assert.Equal(tenantId, id.TenantId); + Assert.NotEqual(Guid.Empty, id.EntityId.ToGuid()); + } + + [Theory(DisplayName = "GetHashCode: it should return the correct hash code.")] + [InlineData(null)] + [InlineData("TenantId")] + public void Given_Id_When_GetHashCode_Then_CorrectHashCodeReturned(string? tenantIdValue) + { + TenantId? tenantId = tenantIdValue == null ? null : new(tenantIdValue); + EntityId entityId = EntityId.NewId(); + + OneTimePasswordId id = new(tenantId, entityId); + + Assert.Equal(id.Value.GetHashCode(), id.GetHashCode()); + } + + [Fact(DisplayName = "NotEqualOperator: it should return false when the IDs are the same.")] + public void Given_SameIds_When_NotEqualOperator_Then_TrueReturned() + { + OneTimePasswordId id1 = new(TenantId.NewId(), EntityId.NewId()); + OneTimePasswordId id2 = new(id1.StreamId); + Assert.False(id1 != id2); + } + + [Fact(DisplayName = "NotEqualOperator: it should return true when the IDs are different.")] + public void Given_DifferentIds_When_NotEqualOperator_Then_TrueReturned() + { + OneTimePasswordId id1 = new(TenantId.NewId(), EntityId.NewId()); + OneTimePasswordId id2 = new(tenantId: null, id1.EntityId); + Assert.True(id1 != id2); + } + + [Theory(DisplayName = "ToString: it should return the correct string representation.")] + [InlineData(null)] + [InlineData("TenantId")] + public void Given_Id_When_ToString_Then_CorrectStringReturned(string? tenantIdValue) + { + TenantId? tenantId = tenantIdValue == null ? null : new(tenantIdValue); + EntityId entityId = EntityId.NewId(); + + OneTimePasswordId id = new(tenantId, entityId); + + Assert.Equal(id.Value, id.ToString()); + } +} diff --git a/tests/Logitar.Identity.Core.UnitTests/Roles/RoleIdTests.cs b/tests/Logitar.Identity.Core.UnitTests/Roles/RoleIdTests.cs index 215c194..082f9ea 100644 --- a/tests/Logitar.Identity.Core.UnitTests/Roles/RoleIdTests.cs +++ b/tests/Logitar.Identity.Core.UnitTests/Roles/RoleIdTests.cs @@ -5,21 +5,7 @@ namespace Logitar.Identity.Core.Roles; [Trait(Traits.Category, Categories.Unit)] public class RoleIdTests { - [Theory(DisplayName = "ctor: it should construct the correct ID from a Guid.")] - [InlineData(null)] - [InlineData("TenantId")] - public void Given_GuidValue_When_ctor_Then_CorrectIdConstructed(string? tenantIdValue) - { - TenantId? tenantId = tenantIdValue == null ? null : new(tenantIdValue); - Guid entityId = Guid.NewGuid(); - - RoleId id = new(tenantId, entityId); - - Assert.Equal(tenantId, id.TenantId); - Assert.Equal(entityId, id.EntityId.ToGuid()); - } - - [Theory(DisplayName = "ctor: it should construct the correct ID from a StreamId.")] + [Theory(DisplayName = "ctor: it should construct the correct ID from a stream ID.")] [InlineData(null)] [InlineData("TenantId")] public void Given_StreamId_When_ctor_Then_CorrectIdConstructed(string? tenantIdValue) @@ -34,25 +20,25 @@ public void Given_StreamId_When_ctor_Then_CorrectIdConstructed(string? tenantIdV Assert.Equal(entityId, id.EntityId); } - [Theory(DisplayName = "ctor: it should construct the correct ID from a string.")] + [Theory(DisplayName = "ctor: it should construct the correct ID from a tenant ID and an entity ID.")] [InlineData(null)] [InlineData("TenantId")] - public void Given_StringValue_When_ctor_Then_CorrectIdConstructed(string? tenantIdValue) + public void Given_TenantAndEntityId_When_ctor_Then_CorrectIdConstructed(string? tenantIdValue) { TenantId? tenantId = tenantIdValue == null ? null : new(tenantIdValue); - string entityId = "EntityId"; + EntityId entityId = EntityId.NewId(); RoleId id = new(tenantId, entityId); Assert.Equal(tenantId, id.TenantId); - Assert.Equal(entityId, id.EntityId.Value); + Assert.Equal(entityId, id.EntityId); } [Fact(DisplayName = "Equals: it should return false when the IDs are different.")] public void Given_DifferentIds_When_Equals_Then_FalseReturned() { - RoleId id1 = new(TenantId.NewId(), Guid.NewGuid()); - RoleId id2 = new(tenantId: null, id1.EntityId.Value); + RoleId id1 = new(TenantId.NewId(), EntityId.NewId()); + RoleId id2 = new(tenantId: null, id1.EntityId); Assert.False(id1.Equals(id2)); } @@ -68,7 +54,7 @@ public void Given_DifferentTypes_When_Equals_Then_FalseReturned(object? value) [Fact(DisplayName = "Equals: it should return true when the IDs are the same.")] public void Given_SameIds_When_Equals_Then_TrueReturned() { - RoleId id1 = new(TenantId.NewId(), Guid.NewGuid()); + RoleId id1 = new(TenantId.NewId(), EntityId.NewId()); RoleId id2 = new(id1.StreamId); Assert.True(id1.Equals(id1)); Assert.True(id1.Equals(id2)); @@ -77,15 +63,15 @@ public void Given_SameIds_When_Equals_Then_TrueReturned() [Fact(DisplayName = "EqualOperator: it should return false when the IDs are different.")] public void Given_DifferentIds_When_EqualOperator_Then_FalseReturned() { - RoleId id1 = new(TenantId.NewId(), Guid.NewGuid()); - RoleId id2 = new(tenantId: null, id1.EntityId.Value); + RoleId id1 = new(TenantId.NewId(), EntityId.NewId()); + RoleId id2 = new(tenantId: null, id1.EntityId); Assert.False(id1 == id2); } [Fact(DisplayName = "EqualOperator: it should return true when the IDs are the same.")] public void Given_SameIds_When_EqualOperator_Then_TrueReturned() { - RoleId id1 = new(TenantId.NewId(), Guid.NewGuid()); + RoleId id1 = new(TenantId.NewId(), EntityId.NewId()); RoleId id2 = new(id1.StreamId); Assert.True(id1 == id2); } @@ -109,7 +95,7 @@ public void Given_TenantId_When_NewId_Then_NewRandomIdGenerated(string? tenantId public void Given_Id_When_GetHashCode_Then_CorrectHashCodeReturned(string? tenantIdValue) { TenantId? tenantId = tenantIdValue == null ? null : new(tenantIdValue); - Guid entityId = Guid.NewGuid(); + EntityId entityId = EntityId.NewId(); RoleId id = new(tenantId, entityId); @@ -119,7 +105,7 @@ public void Given_Id_When_GetHashCode_Then_CorrectHashCodeReturned(string? tenan [Fact(DisplayName = "NotEqualOperator: it should return false when the IDs are the same.")] public void Given_SameIds_When_NotEqualOperator_Then_TrueReturned() { - RoleId id1 = new(TenantId.NewId(), Guid.NewGuid()); + RoleId id1 = new(TenantId.NewId(), EntityId.NewId()); RoleId id2 = new(id1.StreamId); Assert.False(id1 != id2); } @@ -127,8 +113,8 @@ public void Given_SameIds_When_NotEqualOperator_Then_TrueReturned() [Fact(DisplayName = "NotEqualOperator: it should return true when the IDs are different.")] public void Given_DifferentIds_When_NotEqualOperator_Then_TrueReturned() { - RoleId id1 = new(TenantId.NewId(), Guid.NewGuid()); - RoleId id2 = new(tenantId: null, id1.EntityId.Value); + RoleId id1 = new(TenantId.NewId(), EntityId.NewId()); + RoleId id2 = new(tenantId: null, id1.EntityId); Assert.True(id1 != id2); } @@ -138,7 +124,7 @@ public void Given_DifferentIds_When_NotEqualOperator_Then_TrueReturned() public void Given_Id_When_ToString_Then_CorrectStringReturned(string? tenantIdValue) { TenantId? tenantId = tenantIdValue == null ? null : new(tenantIdValue); - Guid entityId = Guid.NewGuid(); + EntityId entityId = EntityId.NewId(); RoleId id = new(tenantId, entityId); diff --git a/tests/Logitar.Identity.Core.UnitTests/Roles/RoleManagerTests.cs b/tests/Logitar.Identity.Core.UnitTests/Roles/RoleManagerTests.cs index bd37eb4..6c3073d 100644 --- a/tests/Logitar.Identity.Core.UnitTests/Roles/RoleManagerTests.cs +++ b/tests/Logitar.Identity.Core.UnitTests/Roles/RoleManagerTests.cs @@ -24,7 +24,7 @@ public class RoleManagerTests public RoleManagerTests() { UniqueName uniqueName = new(_uniqueNameSettings, "admin"); - _role = new(uniqueName, actorId: null, new RoleId(_tenantId, Guid.NewGuid())); + _role = new(uniqueName, actorId: null, new RoleId(_tenantId, EntityId.NewId())); _roleManager = new(_apiKeyRepository.Object, _roleRepository.Object, _userRepository.Object); } @@ -61,16 +61,16 @@ public async Task SaveAsync_it_should_not_load_any_role_when_the_unique_name_has [Fact(DisplayName = "SaveAsync: it should remove associations when it has been deleted.")] public async Task SaveAsync_it_should_remove_associations_when_it_has_been_deleted() { - Role guest = new(new UniqueName(_uniqueNameSettings, "guest"), actorId: null, new RoleId(_tenantId, Guid.NewGuid())); + Role guest = new(new UniqueName(_uniqueNameSettings, "guest"), actorId: null, new RoleId(_tenantId, EntityId.NewId())); DisplayName displayName = new("Test"); Base64Password secret = new("S3cr3+!*"); - ApiKey apiKey = new(displayName, secret, actorId: null, new ApiKeyId(_tenantId, Guid.NewGuid())); + ApiKey apiKey = new(displayName, secret, actorId: null, new ApiKeyId(_tenantId, EntityId.NewId())); apiKey.AddRole(_role); apiKey.AddRole(guest); _apiKeyRepository.Setup(x => x.LoadAsync(_role, _cancellationToken)).ReturnsAsync([apiKey]); - User user = new(new UniqueName(_uniqueNameSettings, "test"), actorId: null, new UserId(_tenantId, Guid.NewGuid())); + User user = new(new UniqueName(_uniqueNameSettings, "test"), actorId: null, UserId.NewId(_tenantId)); user.AddRole(_role); user.AddRole(guest); _userRepository.Setup(x => x.LoadAsync(_role, _cancellationToken)).ReturnsAsync([user]); diff --git a/tests/Logitar.Identity.Core.UnitTests/Roles/RoleTests.cs b/tests/Logitar.Identity.Core.UnitTests/Roles/RoleTests.cs index 85ded1e..fa0499e 100644 --- a/tests/Logitar.Identity.Core.UnitTests/Roles/RoleTests.cs +++ b/tests/Logitar.Identity.Core.UnitTests/Roles/RoleTests.cs @@ -85,7 +85,7 @@ public void Given_DisplayNameUpdates_When_setDisplayName_Then_UpdatesHandledCorr [Fact(DisplayName = "It should have the correct IDs.")] public void Given_Role_When_getIds_Then_CorrectIds() { - RoleId id = new(TenantId.NewId(), Guid.NewGuid()); + RoleId id = new(TenantId.NewId(), EntityId.NewId()); Role role = new(_role.UniqueName, actorId: null, id); Assert.Equal(id, role.Id); Assert.Equal(id.TenantId, role.TenantId); diff --git a/tests/Logitar.Identity.Core.UnitTests/Sessions/SessionIdTests.cs b/tests/Logitar.Identity.Core.UnitTests/Sessions/SessionIdTests.cs new file mode 100644 index 0000000..d514d58 --- /dev/null +++ b/tests/Logitar.Identity.Core.UnitTests/Sessions/SessionIdTests.cs @@ -0,0 +1,133 @@ +using Logitar.EventSourcing; + +namespace Logitar.Identity.Core.Sessions; + +[Trait(Traits.Category, Categories.Unit)] +public class SessionIdTests +{ + [Theory(DisplayName = "ctor: it should construct the correct ID from a stream ID.")] + [InlineData(null)] + [InlineData("TenantId")] + public void Given_StreamId_When_ctor_Then_CorrectIdConstructed(string? tenantIdValue) + { + TenantId? tenantId = tenantIdValue == null ? null : new(tenantIdValue); + EntityId entityId = EntityId.NewId(); + StreamId streamId = new(tenantId.HasValue ? string.Join(':', tenantId, entityId) : entityId.Value); + + SessionId id = new(streamId); + + Assert.Equal(tenantId, id.TenantId); + Assert.Equal(entityId, id.EntityId); + } + + [Theory(DisplayName = "ctor: it should construct the correct ID from a tenant ID and an entity ID.")] + [InlineData(null)] + [InlineData("TenantId")] + public void Given_TenantAndEntityId_When_ctor_Then_CorrectIdConstructed(string? tenantIdValue) + { + TenantId? tenantId = tenantIdValue == null ? null : new(tenantIdValue); + EntityId entityId = EntityId.NewId(); + + SessionId id = new(tenantId, entityId); + + Assert.Equal(tenantId, id.TenantId); + Assert.Equal(entityId, id.EntityId); + } + + [Fact(DisplayName = "Equals: it should return false when the IDs are different.")] + public void Given_DifferentIds_When_Equals_Then_FalseReturned() + { + SessionId id1 = new(TenantId.NewId(), EntityId.NewId()); + SessionId id2 = new(tenantId: null, id1.EntityId); + Assert.False(id1.Equals(id2)); + } + + [Theory(DisplayName = "Equals: it should return false when the object do not have the same types.")] + [InlineData(null)] + [InlineData(123)] + public void Given_DifferentTypes_When_Equals_Then_FalseReturned(object? value) + { + SessionId id = SessionId.NewId(); + Assert.False(id.Equals(value)); + } + + [Fact(DisplayName = "Equals: it should return true when the IDs are the same.")] + public void Given_SameIds_When_Equals_Then_TrueReturned() + { + SessionId id1 = new(TenantId.NewId(), EntityId.NewId()); + SessionId id2 = new(id1.StreamId); + Assert.True(id1.Equals(id1)); + Assert.True(id1.Equals(id2)); + } + + [Fact(DisplayName = "EqualOperator: it should return false when the IDs are different.")] + public void Given_DifferentIds_When_EqualOperator_Then_FalseReturned() + { + SessionId id1 = new(TenantId.NewId(), EntityId.NewId()); + SessionId id2 = new(tenantId: null, id1.EntityId); + Assert.False(id1 == id2); + } + + [Fact(DisplayName = "EqualOperator: it should return true when the IDs are the same.")] + public void Given_SameIds_When_EqualOperator_Then_TrueReturned() + { + SessionId id1 = new(TenantId.NewId(), EntityId.NewId()); + SessionId id2 = new(id1.StreamId); + Assert.True(id1 == id2); + } + + [Theory(DisplayName = "NewId: it should generate a new random ID with or without a tenant ID.")] + [InlineData(null)] + [InlineData("TenantId")] + public void Given_TenantId_When_NewId_Then_NewRandomIdGenerated(string? tenantIdValue) + { + TenantId? tenantId = tenantIdValue == null ? null : new(tenantIdValue); + + SessionId id = SessionId.NewId(tenantId); + + Assert.Equal(tenantId, id.TenantId); + Assert.NotEqual(Guid.Empty, id.EntityId.ToGuid()); + } + + [Theory(DisplayName = "GetHashCode: it should return the correct hash code.")] + [InlineData(null)] + [InlineData("TenantId")] + public void Given_Id_When_GetHashCode_Then_CorrectHashCodeReturned(string? tenantIdValue) + { + TenantId? tenantId = tenantIdValue == null ? null : new(tenantIdValue); + EntityId entityId = EntityId.NewId(); + + SessionId id = new(tenantId, entityId); + + Assert.Equal(id.Value.GetHashCode(), id.GetHashCode()); + } + + [Fact(DisplayName = "NotEqualOperator: it should return false when the IDs are the same.")] + public void Given_SameIds_When_NotEqualOperator_Then_TrueReturned() + { + SessionId id1 = new(TenantId.NewId(), EntityId.NewId()); + SessionId id2 = new(id1.StreamId); + Assert.False(id1 != id2); + } + + [Fact(DisplayName = "NotEqualOperator: it should return true when the IDs are different.")] + public void Given_DifferentIds_When_NotEqualOperator_Then_TrueReturned() + { + SessionId id1 = new(TenantId.NewId(), EntityId.NewId()); + SessionId id2 = new(tenantId: null, id1.EntityId); + Assert.True(id1 != id2); + } + + [Theory(DisplayName = "ToString: it should return the correct string representation.")] + [InlineData(null)] + [InlineData("TenantId")] + public void Given_Id_When_ToString_Then_CorrectStringReturned(string? tenantIdValue) + { + TenantId? tenantId = tenantIdValue == null ? null : new(tenantIdValue); + EntityId entityId = EntityId.NewId(); + + SessionId id = new(tenantId, entityId); + + Assert.Equal(id.Value, id.ToString()); + } +} diff --git a/tests/Logitar.Identity.Core.UnitTests/Users/UserIdTests.cs b/tests/Logitar.Identity.Core.UnitTests/Users/UserIdTests.cs index 2ed1cd7..c98a21b 100644 --- a/tests/Logitar.Identity.Core.UnitTests/Users/UserIdTests.cs +++ b/tests/Logitar.Identity.Core.UnitTests/Users/UserIdTests.cs @@ -5,21 +5,7 @@ namespace Logitar.Identity.Core.Users; [Trait(Traits.Category, Categories.Unit)] public class UserIdTests { - [Theory(DisplayName = "ctor: it should construct the correct ID from a Guid.")] - [InlineData(null)] - [InlineData("TenantId")] - public void Given_GuidValue_When_ctor_Then_CorrectIdConstructed(string? tenantIdValue) - { - TenantId? tenantId = tenantIdValue == null ? null : new(tenantIdValue); - Guid entityId = Guid.NewGuid(); - - UserId id = new(tenantId, entityId); - - Assert.Equal(tenantId, id.TenantId); - Assert.Equal(entityId, id.EntityId.ToGuid()); - } - - [Theory(DisplayName = "ctor: it should construct the correct ID from a StreamId.")] + [Theory(DisplayName = "ctor: it should construct the correct ID from a stream ID.")] [InlineData(null)] [InlineData("TenantId")] public void Given_StreamId_When_ctor_Then_CorrectIdConstructed(string? tenantIdValue) @@ -34,25 +20,25 @@ public void Given_StreamId_When_ctor_Then_CorrectIdConstructed(string? tenantIdV Assert.Equal(entityId, id.EntityId); } - [Theory(DisplayName = "ctor: it should construct the correct ID from a string.")] + [Theory(DisplayName = "ctor: it should construct the correct ID from a tenant ID and an entity ID.")] [InlineData(null)] [InlineData("TenantId")] - public void Given_StringValue_When_ctor_Then_CorrectIdConstructed(string? tenantIdValue) + public void Given_TenantAndEntityId_When_ctor_Then_CorrectIdConstructed(string? tenantIdValue) { TenantId? tenantId = tenantIdValue == null ? null : new(tenantIdValue); - string entityId = "EntityId"; + EntityId entityId = EntityId.NewId(); UserId id = new(tenantId, entityId); Assert.Equal(tenantId, id.TenantId); - Assert.Equal(entityId, id.EntityId.Value); + Assert.Equal(entityId, id.EntityId); } [Fact(DisplayName = "Equals: it should return false when the IDs are different.")] public void Given_DifferentIds_When_Equals_Then_FalseReturned() { - UserId id1 = new(TenantId.NewId(), Guid.NewGuid()); - UserId id2 = new(tenantId: null, id1.EntityId.Value); + UserId id1 = new(TenantId.NewId(), EntityId.NewId()); + UserId id2 = new(tenantId: null, id1.EntityId); Assert.False(id1.Equals(id2)); } @@ -68,7 +54,7 @@ public void Given_DifferentTypes_When_Equals_Then_FalseReturned(object? value) [Fact(DisplayName = "Equals: it should return true when the IDs are the same.")] public void Given_SameIds_When_Equals_Then_TrueReturned() { - UserId id1 = new(TenantId.NewId(), Guid.NewGuid()); + UserId id1 = new(TenantId.NewId(), EntityId.NewId()); UserId id2 = new(id1.StreamId); Assert.True(id1.Equals(id1)); Assert.True(id1.Equals(id2)); @@ -77,15 +63,15 @@ public void Given_SameIds_When_Equals_Then_TrueReturned() [Fact(DisplayName = "EqualOperator: it should return false when the IDs are different.")] public void Given_DifferentIds_When_EqualOperator_Then_FalseReturned() { - UserId id1 = new(TenantId.NewId(), Guid.NewGuid()); - UserId id2 = new(tenantId: null, id1.EntityId.Value); + UserId id1 = new(TenantId.NewId(), EntityId.NewId()); + UserId id2 = new(tenantId: null, id1.EntityId); Assert.False(id1 == id2); } [Fact(DisplayName = "EqualOperator: it should return true when the IDs are the same.")] public void Given_SameIds_When_EqualOperator_Then_TrueReturned() { - UserId id1 = new(TenantId.NewId(), Guid.NewGuid()); + UserId id1 = new(TenantId.NewId(), EntityId.NewId()); UserId id2 = new(id1.StreamId); Assert.True(id1 == id2); } @@ -109,7 +95,7 @@ public void Given_TenantId_When_NewId_Then_NewRandomIdGenerated(string? tenantId public void Given_Id_When_GetHashCode_Then_CorrectHashCodeReturned(string? tenantIdValue) { TenantId? tenantId = tenantIdValue == null ? null : new(tenantIdValue); - Guid entityId = Guid.NewGuid(); + EntityId entityId = EntityId.NewId(); UserId id = new(tenantId, entityId); @@ -119,7 +105,7 @@ public void Given_Id_When_GetHashCode_Then_CorrectHashCodeReturned(string? tenan [Fact(DisplayName = "NotEqualOperator: it should return false when the IDs are the same.")] public void Given_SameIds_When_NotEqualOperator_Then_TrueReturned() { - UserId id1 = new(TenantId.NewId(), Guid.NewGuid()); + UserId id1 = new(TenantId.NewId(), EntityId.NewId()); UserId id2 = new(id1.StreamId); Assert.False(id1 != id2); } @@ -127,8 +113,8 @@ public void Given_SameIds_When_NotEqualOperator_Then_TrueReturned() [Fact(DisplayName = "NotEqualOperator: it should return true when the IDs are different.")] public void Given_DifferentIds_When_NotEqualOperator_Then_TrueReturned() { - UserId id1 = new(TenantId.NewId(), Guid.NewGuid()); - UserId id2 = new(tenantId: null, id1.EntityId.Value); + UserId id1 = new(TenantId.NewId(), EntityId.NewId()); + UserId id2 = new(tenantId: null, id1.EntityId); Assert.True(id1 != id2); } @@ -138,7 +124,7 @@ public void Given_DifferentIds_When_NotEqualOperator_Then_TrueReturned() public void Given_Id_When_ToString_Then_CorrectStringReturned(string? tenantIdValue) { TenantId? tenantId = tenantIdValue == null ? null : new(tenantIdValue); - Guid entityId = Guid.NewGuid(); + EntityId entityId = EntityId.NewId(); UserId id = new(tenantId, entityId);