diff --git a/src/Lumina.Application/Core/WrittenContentLibrary/BooksLibrary/Books/Commands/AddBookCommandHandler.cs b/src/Lumina.Application/Core/WrittenContentLibrary/BooksLibrary/Books/Commands/AddBookCommandHandler.cs index ae3bc905..aed72e0a 100644 --- a/src/Lumina.Application/Core/WrittenContentLibrary/BooksLibrary/Books/Commands/AddBookCommandHandler.cs +++ b/src/Lumina.Application/Core/WrittenContentLibrary/BooksLibrary/Books/Commands/AddBookCommandHandler.cs @@ -60,7 +60,7 @@ public class AddBookCommandHandler : IRequestHandler BookRating.Create( rating.Value ?? default, @@ -86,7 +86,6 @@ public class AddBookCommandHandler : IRequestHandler domainTags = domainTagsResult.Select(tag => tag.Value).ToList(); - var domainIsbnsResult = request.ISBNs.ConvertAll(isbn => Isbn.Create(isbn.Value!, (IsbnFormat)(int)isbn.Format!)); errors = domainIsbnsResult.Where(isbnResult => isbnResult.IsError).SelectMany(isbnResult => isbnResult.Errors).ToList(); if (errors.Count != 0) diff --git a/src/Lumina.Domain/Core/Aggregates/WrittenContentLibrary/BookLibraryAggregate/ValueObjects/WrittenContentMetadata.cs b/src/Lumina.Domain/Core/Aggregates/WrittenContentLibrary/BookLibraryAggregate/ValueObjects/WrittenContentMetadata.cs index d17fffdd..c498248c 100644 --- a/src/Lumina.Domain/Core/Aggregates/WrittenContentLibrary/BookLibraryAggregate/ValueObjects/WrittenContentMetadata.cs +++ b/src/Lumina.Domain/Core/Aggregates/WrittenContentLibrary/BookLibraryAggregate/ValueObjects/WrittenContentMetadata.cs @@ -3,6 +3,7 @@ using ErrorOr; using Lumina.Domain.Common.Primitives; using Lumina.Domain.Common.ValueObjects.Metadata; using System.Diagnostics; +using Lumina.Domain.Common.Errors; #endregion namespace Lumina.Domain.Core.Aggregates.WrittenContentLibrary.BookLibraryAggregate.ValueObjects; @@ -86,6 +87,14 @@ public class WrittenContentMetadata : BaseMetadata Optional publisher, Optional pageCount) { + if (string.IsNullOrWhiteSpace(title)) + return Errors.Metadata.TitleCannotBeEmpty; + if (releaseInfo is null) + return Errors.Metadata.ReleaseInfoCannotBeNull; + if (genres is null) + return Errors.Metadata.GenresListCannotBeNull; + if (tags is null) + return Errors.Metadata.TagsListCannotBeNull; return new WrittenContentMetadata( title, originalTitle, diff --git a/tests/IntegrationTests/Lumina.Presentation.Api.IntegrationTests/Core/Controllers/Books/BooksControllerTests.cs b/tests/IntegrationTests/Lumina.Presentation.Api.IntegrationTests/Core/Controllers/Books/BooksControllerTests.cs index 83ebb265..f2a4b824 100644 --- a/tests/IntegrationTests/Lumina.Presentation.Api.IntegrationTests/Core/Controllers/Books/BooksControllerTests.cs +++ b/tests/IntegrationTests/Lumina.Presentation.Api.IntegrationTests/Core/Controllers/Books/BooksControllerTests.cs @@ -26,7 +26,7 @@ public class BooksControllerTests : IClassFixture #region ================================================================== FIELD MEMBERS ================================================================================ private readonly LuminaApiFactory _apiFactory; private readonly HttpClient _client; - private readonly RequestBookFixture _requestBookFixture; + private readonly AddBookRequestFixture _requestBookFixture; private readonly JsonSerializerOptions _jsonOptions = new(); #endregion @@ -39,7 +39,7 @@ public class BooksControllerTests : IClassFixture { _apiFactory = apiFactory; _client = _apiFactory.CreateClient(); - _requestBookFixture = new RequestBookFixture(); + _requestBookFixture = new AddBookRequestFixture(); _jsonOptions.Converters.Add(new BookJsonConverter()); } #endregion diff --git a/tests/IntegrationTests/Lumina.Presentation.Api.IntegrationTests/Core/Controllers/Books/Fixtures/RequestBookFixture.cs b/tests/IntegrationTests/Lumina.Presentation.Api.IntegrationTests/Core/Controllers/Books/Fixtures/AddBookRequestFixture.cs similarity index 98% rename from tests/IntegrationTests/Lumina.Presentation.Api.IntegrationTests/Core/Controllers/Books/Fixtures/RequestBookFixture.cs rename to tests/IntegrationTests/Lumina.Presentation.Api.IntegrationTests/Core/Controllers/Books/Fixtures/AddBookRequestFixture.cs index a6edd061..1cdb99a3 100644 --- a/tests/IntegrationTests/Lumina.Presentation.Api.IntegrationTests/Core/Controllers/Books/Fixtures/RequestBookFixture.cs +++ b/tests/IntegrationTests/Lumina.Presentation.Api.IntegrationTests/Core/Controllers/Books/Fixtures/AddBookRequestFixture.cs @@ -13,10 +13,10 @@ using System.Diagnostics.CodeAnalysis; namespace Lumina.Presentation.Api.IntegrationTests.Core.Controllers.Books.Fixtures; /// -/// Contains unit tests for the class. +/// Fixture class for the class. /// [ExcludeFromCodeCoverage] -public class RequestBookFixture +public class AddBookRequestFixture { #region ================================================================== FIELD MEMBERS ================================================================================ private readonly Fixture _fixture; @@ -26,9 +26,9 @@ public class RequestBookFixture #region ====================================================================== CTOR ===================================================================================== /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// - public RequestBookFixture() + public AddBookRequestFixture() { _fixture = new Fixture(); _fixture.Customizations.Add(new DateOnlySpecimenBuilder()); @@ -271,10 +271,6 @@ public class RequestBookFixture var releaseYear = _random.Next(2000, 2010); var reReleaseYear = _random.Next(2010, 2020); - - - - _fixture.Register(() => new ReleaseInfoDto( _faker.DateOnlyBetween(new DateOnly(releaseYear, 1, 1), new DateOnly(releaseYear, 12, 31)), releaseYear, diff --git a/tests/IntegrationTests/Lumina.Presentation.Api.IntegrationTests/Core/Controllers/Books/Fixtures/DomainBookFixture.cs b/tests/IntegrationTests/Lumina.Presentation.Api.IntegrationTests/Core/Controllers/Books/Fixtures/DomainBookFixture.cs index d2097c31..4df98f94 100644 --- a/tests/IntegrationTests/Lumina.Presentation.Api.IntegrationTests/Core/Controllers/Books/Fixtures/DomainBookFixture.cs +++ b/tests/IntegrationTests/Lumina.Presentation.Api.IntegrationTests/Core/Controllers/Books/Fixtures/DomainBookFixture.cs @@ -14,7 +14,7 @@ using System.Diagnostics.CodeAnalysis; namespace Lumina.Presentation.Api.IntegrationTests.Core.Controllers.Books.Fixtures; /// -/// Contains unit tests for the class. +/// Fixture class for the domain entity. /// [ExcludeFromCodeCoverage] public class DomainBookFixture diff --git a/tests/UnitTests/Lumina.Application.UnitTests/Common/Setup/BogusExtensions.cs b/tests/UnitTests/Lumina.Application.UnitTests/Common/Setup/BogusExtensions.cs new file mode 100644 index 00000000..b3eb377e --- /dev/null +++ b/tests/UnitTests/Lumina.Application.UnitTests/Common/Setup/BogusExtensions.cs @@ -0,0 +1,39 @@ +#region ========================================================================= USING ===================================================================================== +using Bogus; +using System.Diagnostics.CodeAnalysis; +#endregion + +namespace Lumina.Application.UnitTests.Common.Setup; + +/// +/// Contains extension methods for the class. +/// +[ExcludeFromCodeCoverage] +public static class BogusExtensions +{ + #region ===================================================================== METHODS =================================================================================== + /// + /// Adds random data generation support to the class. + /// + /// The faker instance to extend. + /// A random instance. + public static DateOnly DateOnly(this Faker faker) + { + return System.DateOnly.FromDateTime(faker.Date.Past()); + } + + /// + /// Adds random data generation within a specified interval support to the class. + /// + /// The faker instance to extend. + /// The start of the interval. + /// The end of the interval. + /// A random instance. + public static DateOnly DateOnlyBetween(this Faker faker, DateOnly start, DateOnly end) + { + var startDateTime = start.ToDateTime(TimeOnly.MinValue); + var endDateTime = end.ToDateTime(TimeOnly.MinValue); + return System.DateOnly.FromDateTime(faker.Date.Between(startDateTime, endDateTime)); + } + #endregion +} \ No newline at end of file diff --git a/tests/UnitTests/Lumina.Application.UnitTests/Common/Setup/DateOnlySpecimenBuilder.cs b/tests/UnitTests/Lumina.Application.UnitTests/Common/Setup/DateOnlySpecimenBuilder.cs new file mode 100644 index 00000000..8de9a4e3 --- /dev/null +++ b/tests/UnitTests/Lumina.Application.UnitTests/Common/Setup/DateOnlySpecimenBuilder.cs @@ -0,0 +1,28 @@ +#region ========================================================================= USING ===================================================================================== +using AutoFixture.Kernel; +using System.Diagnostics.CodeAnalysis; +#endregion + +namespace Lumina.Application.UnitTests.Common.Setup; + +/// +/// Represents a specimen builder for the type. +/// +[ExcludeFromCodeCoverage] +public class DateOnlySpecimenBuilder : ISpecimenBuilder +{ + #region ===================================================================== METHODS =================================================================================== + /// + /// Adds support to the AutoFixture library. + /// + /// The request to process. + /// The context within which the specimen is created. + /// A specimen based on the request. + public object Create(object request, ISpecimenContext context) + { + if (request is Type type && type == typeof(DateOnly)) + return DateOnly.FromDateTime(DateTime.Now); + return new NoSpecimen(); + } + #endregion +} \ No newline at end of file diff --git a/tests/UnitTests/Lumina.Application.UnitTests/Common/Setup/NullableDateOnlySpecimenBuilder.cs b/tests/UnitTests/Lumina.Application.UnitTests/Common/Setup/NullableDateOnlySpecimenBuilder.cs new file mode 100644 index 00000000..84706e0f --- /dev/null +++ b/tests/UnitTests/Lumina.Application.UnitTests/Common/Setup/NullableDateOnlySpecimenBuilder.cs @@ -0,0 +1,28 @@ +#region ========================================================================= USING ===================================================================================== +using AutoFixture.Kernel; +using System.Diagnostics.CodeAnalysis; +#endregion + +namespace Lumina.Application.UnitTests.Common.Setup; + +/// +/// Represents a specimen builder for the type. +/// +[ExcludeFromCodeCoverage] +public class NullableDateOnlySpecimenBuilder : ISpecimenBuilder +{ + #region ===================================================================== METHODS =================================================================================== + /// + /// Adds support to the AutoFixture library. + /// + /// The request to process. + /// The context within which the specimen is created. + /// A specimen based on the request. + public object Create(object request, ISpecimenContext context) + { + if (request is Type type && type == typeof(DateOnly?)) + return (DateOnly?)DateOnly.FromDateTime(DateTime.Now); + return new NoSpecimen(); + } + #endregion +} \ No newline at end of file diff --git a/tests/UnitTests/Lumina.Application.UnitTests/Core/WrittenContentLibrary/BooksLibrary/Books/Commands/AddBookCommandHandlerTests.cs b/tests/UnitTests/Lumina.Application.UnitTests/Core/WrittenContentLibrary/BooksLibrary/Books/Commands/AddBookCommandHandlerTests.cs new file mode 100644 index 00000000..8e552271 --- /dev/null +++ b/tests/UnitTests/Lumina.Application.UnitTests/Core/WrittenContentLibrary/BooksLibrary/Books/Commands/AddBookCommandHandlerTests.cs @@ -0,0 +1,275 @@ +#region ========================================================================= USING ===================================================================================== +using AutoFixture; +using AutoFixture.AutoNSubstitute; +using ErrorOr; +using FluentAssertions; +using Lumina.Application.Common.DataAccess.Repositories.Books; +using Lumina.Application.Common.DataAccess.UoW; +using Lumina.Application.Common.Models.Books; +using Lumina.Application.Common.Models.Common; +using Lumina.Application.Core.WrittenContentLibrary.BooksLibrary.Books.Commands; +using Lumina.Application.UnitTests.Common.Setup; +using Lumina.Application.UnitTests.Core.WrittenContentLibrary.BooksLibrary.Books.Commands.Fixtures; +using Lumina.Domain.Common.Enums; +using Lumina.Domain.Common.Errors; +using Lumina.Domain.Core.Aggregates.WrittenContentLibrary.BookLibraryAggregate; +using MapsterMapper; +using NSubstitute; +using System.Diagnostics.CodeAnalysis; +#endregion + +namespace Lumina.Application.UnitTests.Core.WrittenContentLibrary.BooksLibrary.Books.Commands; + +/// +/// Contains unit tests for the class. +/// +[ExcludeFromCodeCoverage] +public class AddBookCommandHandlerTests +{ + #region ================================================================== FIELD MEMBERS ================================================================================ + private readonly IFixture _fixture; + private readonly IUnitOfWork _mockUnitOfWork; + private readonly IMapper _mockMapper; + private readonly IBookRepository _mockBookRepository; + private readonly AddBookCommandHandler _sut; + private readonly AddBookCommandFixture _commandBookFixture; + #endregion + + #region ====================================================================== CTOR ===================================================================================== + /// + /// Initializes a new instance of the class. + /// + public AddBookCommandHandlerTests() + { + _fixture = new Fixture().Customize(new AutoNSubstituteCustomization()); + _fixture.Customizations.Add(new DateOnlySpecimenBuilder()); + _fixture.Customizations.Add(new NullableDateOnlySpecimenBuilder()); + + _mockUnitOfWork = Substitute.For(); + _mockMapper = Substitute.For(); + _mockBookRepository = Substitute.For(); + + _mockUnitOfWork.GetRepository().Returns(_mockBookRepository); + + _sut = new AddBookCommandHandler(_mockUnitOfWork, _mockMapper); + _commandBookFixture = new AddBookCommandFixture(); + } + #endregion + + #region ===================================================================== METHODS =================================================================================== + [Fact] + public async Task Handle_WhenCalledWithValidCommand_ShouldReturnSuccessResult() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + var bookDto = _fixture.Create(); + + _mockMapper.Map(Arg.Any()).Returns(bookDto); + _mockBookRepository.InsertAsync(Arg.Any(), Arg.Any()) + .Returns(Result.Created); + + // Act + var result = await _sut.Handle(bookCommand, CancellationToken.None); + + // Assert + result.IsError.Should().BeFalse(); + result.Value.Should().BeOfType(); + await _mockBookRepository.Received(1).InsertAsync(Arg.Any(), Arg.Any()); + await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any()); + } + + [Fact] + public async Task Handle_WhenBookCreationFails_ShouldReturnFailureResult() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata with { Title = null } }; + + // Act + var result = await _sut.Handle(bookCommand, CancellationToken.None); + + // Assert + result.IsError.Should().BeTrue(); + result.Errors.Should().Contain(e => e.Code == Errors.Metadata.TitleCannotBeEmpty.Code); + await _mockBookRepository.DidNotReceive().InsertAsync(Arg.Any(), Arg.Any()); + await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any()); + } + + [Fact] + public async Task Handle_WhenRepositoryInsertFails_ShouldReturnFailureResult() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + var bookDto = _fixture.Create(); + + _mockMapper.Map(Arg.Any()).Returns(bookDto); + _mockBookRepository.InsertAsync(Arg.Any(), Arg.Any()) + .Returns(Errors.WrittenContent.BookAlreadyExists); + + // Act + var result = await _sut.Handle(bookCommand, CancellationToken.None); + + // Assert + result.IsError.Should().BeTrue(); + result.Errors.Should().Contain(Errors.WrittenContent.BookAlreadyExists); + await _mockBookRepository.Received(1).InsertAsync(Arg.Any(), Arg.Any()); + await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any()); + } + + [Fact] + public async Task Handle_WhenCalledWithInvalidISBN_ShouldReturnFailureResult() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { ISBNs = [new IsbnDto("invalid", IsbnFormat.Isbn13)] }; + + // Act + var result = await _sut.Handle(bookCommand, CancellationToken.None); + + // Assert + result.IsError.Should().BeTrue(); + result.Errors.Should().Contain(e => e.Code == Errors.WrittenContent.InvalidIsbn13Format.Code); + await _mockBookRepository.DidNotReceive().InsertAsync(Arg.Any(), Arg.Any()); + await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any()); + } + + [Fact] + public async Task Handle_WhenCalledWithInvalidRating_ShouldReturnFailureResult() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Ratings = [new BookRatingDto(-1, 5, null, null)] }; + + // Act + var result = await _sut.Handle(bookCommand, CancellationToken.None); + + // Assert + result.IsError.Should().BeTrue(); + result.Errors.Should().Contain(e => e.Code == Errors.Metadata.RatingValueMustBePositive.Code); + await _mockBookRepository.DidNotReceive().InsertAsync(Arg.Any(), Arg.Any()); + await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any()); + } + + [Fact] + public async Task Handle_WhenGenreCreationFails_ShouldReturnFailureResult() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with + { + Metadata = bookCommand.Metadata with + { + Genres = [new GenreDto("")] + } + }; + + // Act + var result = await _sut.Handle(bookCommand, CancellationToken.None); + + // Assert + result.IsError.Should().BeTrue(); + result.Errors.Should().Contain(e => e.Code == Errors.Metadata.GenreNameCannotBeEmpty.Code); + await _mockBookRepository.DidNotReceive().InsertAsync(Arg.Any(), Arg.Any()); + await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any()); + } + + [Fact] + public async Task Handle_WhenTagCreationFails_ShouldReturnFailureResult() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with + { + Metadata = bookCommand.Metadata with + { + Tags = [new TagDto("")] + } + }; + + // Act + var result = await _sut.Handle(bookCommand, CancellationToken.None); + + // Assert + result.IsError.Should().BeTrue(); + result.Errors.Should().Contain(e => e.Code == Errors.Metadata.TagNameCannotBeEmpty.Code); + await _mockBookRepository.DidNotReceive().InsertAsync(Arg.Any(), Arg.Any()); + await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any()); + } + + [Fact] + public async Task Handle_WhenReleaseInfoCreationFails_ShouldReturnFailureResult() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with + { + Metadata = bookCommand.Metadata with + { + ReleaseInfo = new ReleaseInfoDto( + OriginalReleaseDate: new DateOnly(2025, 1, 1), + ReReleaseDate: new DateOnly(2024, 1, 1), + OriginalReleaseYear: null, + ReReleaseYear: null, + ReleaseCountry: null, + ReleaseVersion: null + ) + } + }; + + // Act + var result = await _sut.Handle(bookCommand, CancellationToken.None); + + // Assert + result.IsError.Should().BeTrue(); + result.Errors.Should().Contain(e => e.Code == Errors.Metadata.ReReleaseDateCannotBeEarlierThanOriginalReleaseDate.Code); + await _mockBookRepository.DidNotReceive().InsertAsync(Arg.Any(), Arg.Any()); + await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any()); + } + + [Fact] + public async Task Handle_WhenLanguageInfoCreationFails_ShouldReturnFailureResult() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with + { + Metadata = bookCommand.Metadata with + { + Language = new LanguageInfoDto("", "English", null) + } + }; + + // Act + var result = await _sut.Handle(bookCommand, CancellationToken.None); + + // Assert + result.IsError.Should().BeTrue(); + result.Errors.Should().Contain(e => e.Code == Errors.Metadata.LanguageCodeCannotBeEmpty.Code); + await _mockBookRepository.DidNotReceive().InsertAsync(Arg.Any(), Arg.Any()); + await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any()); + } + + [Fact] + public async Task Handle_WhenOriginalLanguageInfoCreationFails_ShouldReturnFailureResult() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with + { + Metadata = bookCommand.Metadata with + { + OriginalLanguage = new LanguageInfoDto("", "English", null) + } + }; + + // Act + var result = await _sut.Handle(bookCommand, CancellationToken.None); + + // Assert + result.IsError.Should().BeTrue(); + result.Errors.Should().Contain(e => e.Code == Errors.Metadata.LanguageCodeCannotBeEmpty.Code); + await _mockBookRepository.DidNotReceive().InsertAsync(Arg.Any(), Arg.Any()); + await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any()); + } + #endregion +} \ No newline at end of file diff --git a/tests/UnitTests/Lumina.Application.UnitTests/Core/WrittenContentLibrary/BooksLibrary/Books/Commands/AddBookCommandValidatorTests.cs b/tests/UnitTests/Lumina.Application.UnitTests/Core/WrittenContentLibrary/BooksLibrary/Books/Commands/AddBookCommandValidatorTests.cs new file mode 100644 index 00000000..5f4cd1a6 --- /dev/null +++ b/tests/UnitTests/Lumina.Application.UnitTests/Core/WrittenContentLibrary/BooksLibrary/Books/Commands/AddBookCommandValidatorTests.cs @@ -0,0 +1,1858 @@ +#region ========================================================================= USING ===================================================================================== +using FluentValidation.TestHelper; +using Lumina.Application.Core.WrittenContentLibrary.BooksLibrary.Books.Commands; +using Lumina.Application.UnitTests.Core.WrittenContentLibrary.BooksLibrary.Books.Commands.Fixtures; +using System.Diagnostics.CodeAnalysis; +using Lumina.Domain.Common.Errors; +using Bogus; +using Lumina.Domain.Common.Enums; +using Lumina.Application.Common.Models.Books; +using Lumina.Application.Common.Models.Common; +#endregion + +namespace Lumina.Application.UnitTests.Core.WrittenContentLibrary.BooksLibrary.Books.Commands; + +/// +/// Contains unit tests for the class. +/// +[ExcludeFromCodeCoverage] +public class AddBookCommandValidatorTests +{ + #region ================================================================== FIELD MEMBERS ================================================================================ + private readonly AddBookCommandFixture _commandBookFixture; + private readonly AddBookCommandValidator _validator; + #endregion + + #region ====================================================================== CTOR ===================================================================================== + /// + /// Initializes a new instance of the class. + /// + public AddBookCommandValidatorTests() + { + _validator = new AddBookCommandValidator(); + _commandBookFixture = new AddBookCommandFixture(); + } + #endregion + + #region ===================================================================== METHODS =================================================================================== + [Fact] + public void Validate_WhenTitleIsNull_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { Title = null! } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.Metadata.Title).WithErrorMessage(Errors.Metadata.TitleCannotBeEmpty.Code); + } + + [Fact] + public void Validate_WhenTitleExceeds255Characters_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { Title = new Faker().Random.String2(300) } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.Metadata.Title).WithErrorMessage(Errors.Metadata.TitleMustBeMaximum255CharactersLong.Code); + } + + [Fact] + public void Validate_WhenTitleIsValid_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { Title = new Faker().Random.String2(200) } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.Metadata.Title); + } + + [Fact] + public void Validate_WhenOriginalTitleExceeds255Characters_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { OriginalTitle = new Faker().Random.String2(300) } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.Metadata.OriginalTitle).WithErrorMessage(Errors.Metadata.OriginalTitleMustBeMaximum255CharactersLong.Code); + } + + [Fact] + public void Validate_WhenOriginalTitleIsValid_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { OriginalTitle = new Faker().Random.String2(200) } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.Metadata.OriginalTitle); + } + + [Fact] + public void Validate_WhenOriginalTitleIsNull_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { OriginalTitle = null! } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.Metadata.OriginalTitle); + } + + [Fact] + public void Validate_WhenDescriptionExceeds2000Characters_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { Description = new Faker().Random.String2(2001) } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.Metadata.Description).WithErrorMessage(Errors.Metadata.DescriptionMustBeMaximum2000CharactersLong.Code); + } + + [Fact] + public void Validate_WhenDescriptionIsValid_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { Description = new Faker().Random.String2(1500) } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.Metadata.Description); + } + + [Fact] + public void Validate_WhenDescriptionIsNull_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { Description = null! } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.Metadata.Description); + } + + [Fact] + public void Validate_WhenDescriptionIsEmpty_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { Description = string.Empty } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.Metadata.Description); + } + + [Fact] + public void Validate_WhenReleaseInfoIsNull_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { ReleaseInfo = null! } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.Metadata.ReleaseInfo).WithErrorMessage(Errors.Metadata.ReleaseInfoCannotBeNull.Code); + } + + [Fact] + public void Validate_WhenOriginalReleaseYearIsValid_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { ReleaseInfo = bookCommand.Metadata.ReleaseInfo! with { OriginalReleaseYear = new Faker().Random.Int(2000, 2005), ReReleaseYear = new Faker().Random.Int(2005, 2010) } } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.Metadata.ReleaseInfo!.OriginalReleaseYear); + } + + [Fact] + public void Validate_WhenOriginalReleaseYearIsLessThan1_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { ReleaseInfo = bookCommand.Metadata.ReleaseInfo! with { OriginalReleaseYear = 0 } } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.Metadata.ReleaseInfo!.OriginalReleaseYear).WithErrorMessage(Errors.Metadata.OriginalReleaseYearMustBeBetween1And9999.Code); + } + + [Fact] + public void Validate_WhenOriginalReleaseYearIsGreaterThan9999_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { ReleaseInfo = bookCommand.Metadata.ReleaseInfo! with { OriginalReleaseYear = 10000 } } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.Metadata.ReleaseInfo!.OriginalReleaseYear).WithErrorMessage(Errors.Metadata.OriginalReleaseYearMustBeBetween1And9999.Code); + } + + [Fact] + public void Validate_WhenReReleaseYearIsValid_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { ReleaseInfo = bookCommand.Metadata.ReleaseInfo! + with { OriginalReleaseYear = new Faker().Random.Int(2000, 2005), ReReleaseYear = new Faker().Random.Int(2005, 2010) } } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.Metadata.ReleaseInfo!.ReReleaseYear); + } + + [Fact] + public void Validate_WhenReReleaseYearIsLessThan1_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { ReleaseInfo = bookCommand.Metadata.ReleaseInfo! with { ReReleaseYear = 0 } } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.Metadata.ReleaseInfo!.ReReleaseYear).WithErrorMessage(Errors.Metadata.ReReleaseYearMustBeBetween1And9999.Code); + } + + [Fact] + public void Validate_WhenReReleaseYearIsGreaterThan9999_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { ReleaseInfo = bookCommand.Metadata.ReleaseInfo! with { ReReleaseYear = 10000 } } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.Metadata.ReleaseInfo!.ReReleaseYear).WithErrorMessage(Errors.Metadata.ReReleaseYearMustBeBetween1And9999.Code); + } + + [Fact] + public void Validate_WhenReleaseCountryIsValid_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { ReleaseInfo = bookCommand.Metadata.ReleaseInfo! with { ReleaseCountry = new Faker().Random.String2(2).ToUpper() } } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.Metadata.ReleaseInfo!.ReleaseCountry); + } + + [Fact] + public void Validate_WhenReleaseCountryIsInvalid_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { ReleaseInfo = bookCommand.Metadata.ReleaseInfo! with { ReleaseCountry = new Faker().Random.String2(3) } } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.Metadata.ReleaseInfo!.ReleaseCountry).WithErrorMessage(Errors.Metadata.CountryCodeMustBe2CharactersLong.Code); + } + + [Fact] + public void Validate_WhenReleaseVersionIsValid_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { ReleaseInfo = bookCommand.Metadata.ReleaseInfo! with { ReleaseVersion = new Faker().Random.String2(50) } } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.Metadata.ReleaseInfo!.ReleaseVersion); + } + + [Fact] + public void Validate_WhenReleaseVersionExceeds50Characters_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { ReleaseInfo = bookCommand.Metadata.ReleaseInfo! with { ReleaseVersion = new Faker().Random.String2(51) } } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.Metadata.ReleaseInfo!.ReleaseVersion).WithErrorMessage(Errors.Metadata.ReleaseVersionMustBeMaximum50CharactersLong.Code); + } + + [Fact] + public void Validate_WhenReReleaseYearIsAfterOriginalReleaseYear_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { ReleaseInfo = bookCommand.Metadata.ReleaseInfo! with { OriginalReleaseYear = 2000, ReReleaseYear = 2001 } } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.Metadata.ReleaseInfo!.ReReleaseYear); + } + + [Fact] + public void Validate_WhenReReleaseYearIsBeforeOriginalReleaseYear_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { ReleaseInfo = bookCommand.Metadata.ReleaseInfo! with { OriginalReleaseYear = 2001, ReReleaseYear = 2000 } } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.Metadata.ReleaseInfo!.ReReleaseYear).WithErrorMessage(Errors.Metadata.ReReleaseYearCannotBeEarlierThanOriginalReleaseYear.Code); + } + + [Fact] + public void Validate_WhenReReleaseDateIsAfterOriginalReleaseDate_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { ReleaseInfo = bookCommand.Metadata.ReleaseInfo! with { OriginalReleaseDate = new DateOnly(2000, 1, 1), ReReleaseDate = new DateOnly(2001, 1, 1) } } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.Metadata.ReleaseInfo!.ReReleaseDate); + } + + [Fact] + public void Validate_WhenReReleaseDateIsBeforeOriginalReleaseDate_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { ReleaseInfo = bookCommand.Metadata.ReleaseInfo! with { OriginalReleaseDate = new DateOnly(2001, 1, 1), ReReleaseDate = new DateOnly(2000, 1, 1) } } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.Metadata.ReleaseInfo!.ReReleaseDate).WithErrorMessage(Errors.Metadata.ReReleaseDateCannotBeEarlierThanOriginalReleaseDate.Code); + } + + [Fact] + public void Validate_WhenGenresIsNull_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { Genres = null! } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.Metadata.Genres).WithErrorMessage(Errors.Metadata.GenresListCannotBeNull.Code); + } + + [Fact] + public void Validate_WhenGenreNameIsEmpty_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { Genres = bookCommand.Metadata.Genres!.Select((genre, index) => index == 0 ? genre with { Name = string.Empty } : genre).ToList() } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor("Metadata.Genres[0].Name").WithErrorMessage(Errors.Metadata.GenreNameCannotBeEmpty.Code); + } + + [Fact] + public void Validate_WhenGenreNameExceeds50Characters_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { Genres = bookCommand.Metadata.Genres!.Select((genre, index) => index == 0 ? genre with { Name = new Faker().Random.String2(51) } : genre).ToList() } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor("Metadata.Genres[0].Name").WithErrorMessage(Errors.Metadata.GenreNameMustBeMaximum50CharactersLong.Code); + } + + [Fact] + public void Validate_WhenGenresAreValid_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with + { + Metadata = bookCommand.Metadata! with + { + Genres = bookCommand.Metadata.Genres!.Select((genre, index) => index == 0 ? genre with { Name = new Faker().Random.String2(50) } : genre).ToList() + } + }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.Metadata.Genres); + } + + [Fact] + public void Validate_WhenTagsIsNull_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { Tags = null! } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.Metadata.Tags).WithErrorMessage(Errors.Metadata.TagsListCannotBeNull.Code); + } + + [Fact] + public void Validate_WhenTagNameIsEmpty_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { Tags = bookCommand.Metadata.Tags!.Select((tag, index) => index == 0 ? tag with { Name = string.Empty } : tag).ToList() } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor("Metadata.Tags[0].Name").WithErrorMessage(Errors.Metadata.TagNameCannotBeEmpty.Code); + } + + [Fact] + public void Validate_WhenTagNameExceeds50Characters_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { Tags = bookCommand.Metadata.Tags!.Select((tag, index) => index == 0 ? tag with { Name = new Faker().Random.String2(51) } : tag).ToList() } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor("Metadata.Tags[0].Name").WithErrorMessage(Errors.Metadata.TagNameMustBeMaximum50CharactersLong.Code); + } + + [Fact] + public void Validate_WhenTagsAreValid_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with + { + Metadata = bookCommand.Metadata! with + { + Tags = bookCommand.Metadata.Tags!.Select((tag, index) => index == 0 ? tag with { Name = new Faker().Random.String2(50) } : tag).ToList() + } + }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.Metadata.Tags); + } + + [Fact] + public void Validate_WhenLanguageIsNull_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { Language = null! } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.Metadata.Language); + } + + [Fact] + public void Validate_WhenLanguageCodeIsEmpty_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { Language = bookCommand.Metadata.Language! with { LanguageCode = string.Empty } } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.Metadata.Language!.LanguageCode).WithErrorMessage(Errors.Metadata.LanguageCodeCannotBeEmpty.Code); + } + + [Fact] + public void Validate_WhenLanguageCodeExceeds2Characters_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { Language = bookCommand.Metadata.Language! with { LanguageCode = new Faker().Random.String2(3) } } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.Metadata.Language!.LanguageCode).WithErrorMessage(Errors.Metadata.LanguageCodeMustBe2CharactersLong.Code); + } + + [Fact] + public void Validate_WhenLanguageNameIsEmpty_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { Language = bookCommand.Metadata.Language! with { LanguageName = string.Empty } } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.Metadata.Language!.LanguageName).WithErrorMessage(Errors.Metadata.LanguageNameCannotBeEmpty.Code); + } + + [Fact] + public void Validate_WhenLanguageNameExceeds50Characters_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { Language = bookCommand.Metadata.Language! with { LanguageName = new Faker().Random.String2(51) } } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.Metadata.Language!.LanguageName).WithErrorMessage(Errors.Metadata.LanguageNameMustBeMaximum50CharactersLong.Code); + } + + [Fact] + public void Validate_WhenLanguageNativeNameIsNull_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { Language = bookCommand.Metadata.Language! with { NativeName = null! } } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.Metadata.Language!.NativeName); + } + + [Fact] + public void Validate_WhenLanguageNativeNameExceeds50Characters_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { Language = bookCommand.Metadata.Language! with { NativeName = new Faker().Random.String2(51) } } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.Metadata.Language!.NativeName).WithErrorMessage(Errors.Metadata.LanguageNativeNameMustBeMaximum50CharactersLong.Code); + } + + [Fact] + public void Validate_WhenOriginalLanguageIsNull_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { OriginalLanguage = null! } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.Metadata.OriginalLanguage); + } + + [Fact] + public void Validate_WhenOriginalLanguageCodeIsEmpty_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { OriginalLanguage = bookCommand.Metadata.OriginalLanguage! with { LanguageCode = string.Empty } } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.Metadata.OriginalLanguage!.LanguageCode).WithErrorMessage(Errors.Metadata.LanguageCodeCannotBeEmpty.Code); + } + + [Fact] + public void Validate_WhenOriginalLanguageCodeExceeds2Characters_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { OriginalLanguage = bookCommand.Metadata.OriginalLanguage! with { LanguageCode = new Faker().Random.String2(3) } } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.Metadata.OriginalLanguage!.LanguageCode).WithErrorMessage(Errors.Metadata.LanguageCodeMustBe2CharactersLong.Code); + } + + [Fact] + public void Validate_WhenOriginalLanguageNameIsEmpty_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { OriginalLanguage = bookCommand.Metadata.OriginalLanguage! with { LanguageName = string.Empty } } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.Metadata.OriginalLanguage!.LanguageName).WithErrorMessage(Errors.Metadata.LanguageNameCannotBeEmpty.Code); + } + + [Fact] + public void Validate_WhenOriginalLanguageNameExceeds50Characters_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { OriginalLanguage = bookCommand.Metadata.OriginalLanguage! with { LanguageName = new Faker().Random.String2(51) } } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.Metadata.OriginalLanguage!.LanguageName).WithErrorMessage(Errors.Metadata.LanguageNameMustBeMaximum50CharactersLong.Code); + } + + [Fact] + public void Validate_WhenOriginalLanguageNativeNameIsNull_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { OriginalLanguage = bookCommand.Metadata.OriginalLanguage! with { NativeName = null! } } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.Metadata.OriginalLanguage!.NativeName); + } + + [Fact] + public void Validate_WhenOriginalLanguageNativeNameExceeds50Characters_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { OriginalLanguage = bookCommand.Metadata.OriginalLanguage! with { NativeName = new Faker().Random.String2(51) } } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.Metadata.OriginalLanguage!.NativeName).WithErrorMessage(Errors.Metadata.LanguageNativeNameMustBeMaximum50CharactersLong.Code); + } + + [Fact] + public void Validate_WhenCalledWithEmptyPublisher_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { Publisher = null! } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.Metadata.Publisher); + } + + [Fact] + public void Validate_WhenCalledWithValidPublisher_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { Publisher = new Faker().Random.String2(100) } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.Metadata.Publisher); + } + + [Fact] + public void Validate_WhenCalledWithInvalidLengthPublisher_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { Publisher = new Faker().Random.String2(101) } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.Metadata.Publisher).WithErrorMessage(Errors.WrittenContent.PublisherMustBeMaximum100CharactersLong.Code); + } + + [Fact] + public void Validate_WhenPageCountIsNull_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { PageCount = null } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.Metadata.PageCount); + } + + [Fact] + public void Validate_WhenPageCountIsZero_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { PageCount = 0 } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.Metadata.PageCount).WithErrorMessage(Errors.WrittenContent.PageCountMustBeGreaterThanZero.Code); + } + + [Fact] + public void Validate_WhenPageCountIsNegative_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { PageCount = -1 } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.Metadata.PageCount).WithErrorMessage(Errors.WrittenContent.PageCountMustBeGreaterThanZero.Code); + } + + [Fact] + public void Validate_WhenPageCountIsPositive_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { PageCount = 100 } }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.Metadata.PageCount); + } + + [Fact] + public void Validate_WhenFormatIsNull_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Format = null }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.Format); + } + + [Fact] + public void Validate_WhenFormatIsValid_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Format = BookFormat.Hardcover }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.Format); + } + + [Fact] + public void Validate_WhenFormatIsInvalid_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Format = (BookFormat)99 }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.Format).WithErrorMessage(Errors.WrittenContent.UnknownBookFormat.Code); + } + + [Fact] + public void Validate_WhenEditionIsNull_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Edition = null }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.Edition); + } + + [Fact] + public void Validate_WhenEditionIsValid_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Edition = new Faker().Random.String2(50) }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.Edition); + } + + [Fact] + public void Validate_WhenEditionExceeds50Characters_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Edition = new Faker().Random.String2(51) }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.Edition).WithErrorMessage(Errors.WrittenContent.EditionMustBeMaximum50CharactersLong.Code); + } + + [Fact] + public void Validate_WhenVolumeNumberIsNull_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { VolumeNumber = null }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.VolumeNumber); + } + + [Fact] + public void Validate_WhenVolumeNumberIsZero_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { VolumeNumber = 0 }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.VolumeNumber).WithErrorMessage(Errors.WrittenContent.VolumeNumberMustBeGreaterThanZero.Code); + } + + [Fact] + public void Validate_WhenVolumeNumberIsNegative_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { VolumeNumber = -1 }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.VolumeNumber).WithErrorMessage(Errors.WrittenContent.VolumeNumberMustBeGreaterThanZero.Code); + } + + [Fact] + public void Validate_WhenVolumeNumberIsPositive_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { VolumeNumber = 1 }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.VolumeNumber); + } + + [Fact] + public void Validate_WhenSeriesIsNull_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Series = null }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.Series); + } + + //[Fact] + //public void Validate_WhenSeriesTitleIsEmpty_ShouldHaveValidationError() + //{ + // // Arrange + // var bookCommand = _commandBookFixture.CreateCommandBook(); + // bookCommand = bookCommand with { Series = bookCommand.Series! with { Title = string.Empty } }; + + // // Act + // var result = _validator.TestValidate(bookCommand); + + // // Assert + // result.ShouldHaveValidationErrorFor(x => x.Series.Title).WithErrorMessage(Errors.Metadata.TitleCannotBeEmpty.Code); + //} + + //[Fact] + //public void Validate_WhenSeriesTitleExceeds255Characters_ShouldHaveValidationError() + //{ + // // Arrange + // var bookCommand = _commandBookFixture.CreateCommandBook(); + // bookCommand = bookCommand with { Series = bookCommand.Series! with { Title = new Faker().Random.String2(256) } }; + + // // Act + // var result = _validator.TestValidate(bookCommand); + + // // Assert + // result.ShouldHaveValidationErrorFor(x => x.Series.Title).WithErrorMessage(Errors.Metadata.TitleMustBeMaximum255CharactersLong.Code); + //} + + //[Fact] + //public void Validate_WhenSeriesTitleIsValid_ShouldNotHaveValidationError() + //{ + // // Arrange + // var bookCommand = _commandBookFixture.CreateCommandBook(); + // bookCommand = bookCommand with { Series = bookCommand.Series! with { Title = new Faker().Random.String2(200) } }; + + // // Act + // var result = _validator.TestValidate(bookCommand); + + // // Assert + // result.ShouldNotHaveValidationErrorFor(x => x.Series.Title); + //} + + [Fact] + public void Validate_WhenAsinIsValid_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { ASIN = new Faker().Random.String2(10) }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.ASIN); + } + + [Fact] + public void Validate_WhenAsinIsNull_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { ASIN = null }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.ASIN); + } + + [Fact] + public void Validate_WhenAsinIsNotTenCharacters_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { ASIN = new Faker().Random.String2(9) }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.ASIN).WithErrorMessage(Errors.WrittenContent.AsinMustBe10CharactersLong.Code); + } + + [Fact] + public void Validate_WhenGoodreadsIdIsNull_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { GoodreadsId = null }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.GoodreadsId); + } + + [Fact] + public void Validate_WhenGoodreadsIdIsValid_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { GoodreadsId = "123456789" }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.GoodreadsId); + } + + [Fact] + public void Validate_WhenGoodreadsIdIsNonNumeric_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { GoodreadsId = "abc123" }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.GoodreadsId).WithErrorMessage(Errors.WrittenContent.GoodreadsIdMustBeNumeric.Code); + } + + [Fact] + public void Validate_WhenGoodreadsIdContainsSpaces_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { GoodreadsId = "123 456" }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.GoodreadsId).WithErrorMessage(Errors.WrittenContent.GoodreadsIdMustBeNumeric.Code); + } + + [Fact] + public void Validate_WhenGoodreadsIdContainsSpecialCharacters_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { GoodreadsId = "123-456" }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.GoodreadsId).WithErrorMessage(Errors.WrittenContent.GoodreadsIdMustBeNumeric.Code); + } + + [Fact] + public void Validate_WhenLccnIsNull_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { LCCN = null }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.LCCN); + } + + [Fact] + public void Validate_WhenLccnIsValid_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { LCCN = "n78890351" }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.LCCN); + } + + [Fact] + public void Validate_WhenLccnHasInvalidFormat_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { LCCN = "invalid123" }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.LCCN).WithErrorMessage(Errors.WrittenContent.InvalidLccnFormat.Code); + } + + [Fact] + public void Validate_WhenLccnIsTooLong_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { LCCN = new Faker().Random.String2(15) }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.LCCN).WithErrorMessage(Errors.WrittenContent.InvalidLccnFormat.Code); + } + + [Fact] + public void Validate_WhenLccnIsTooShort_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { LCCN = "n12" }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.LCCN).WithErrorMessage(Errors.WrittenContent.InvalidLccnFormat.Code); + } + + [Fact] + public void Validate_WhenCalledWithEmptyOclcNumber_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { OCLCNumber = null }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.OCLCNumber); + } + + [Fact] + public void Validate_WhenCalledWithValidOclcNumberFormat1_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { OCLCNumber = "ocm12345678" }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.OCLCNumber); + } + + [Fact] + public void Validate_WhenCalledWithValidOclcNumberFormat2_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { OCLCNumber = "ocn123456789" }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.OCLCNumber); + } + + [Fact] + public void Validate_WhenCalledWithValidOclcNumberFormat3_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { OCLCNumber = "on1234567890" }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.OCLCNumber); + } + + [Fact] + public void Validate_WhenCalledWithValidOclcNumberFormat4_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { OCLCNumber = "(OCoLC)1234567890" }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.OCLCNumber); + } + + [Fact] + public void Validate_WhenCalledWithValidOclcNumberFormat5_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { OCLCNumber = "12345678" }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.OCLCNumber); + } + + [Fact] + public void Validate_WhenCalledWithInvalidOclcNumber_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { OCLCNumber = "invalid_oclc_number" }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.OCLCNumber).WithErrorMessage(Errors.WrittenContent.InvalidOclcFormat.Code); + } + + [Fact] + public void Validate_WhenOpenLibraryIdIsNull_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { OpenLibraryId = null! }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.OpenLibraryId); + } + + [Fact] + public void Validate_WhenOpenLibraryIdIsValid_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { OpenLibraryId = "OL123456M" }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.OpenLibraryId); + } + + [Fact] + public void Validate_WhenOpenLibraryIdIsInvalid_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { OpenLibraryId = "InvalidID" }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.OpenLibraryId).WithErrorMessage(Errors.WrittenContent.InvalidOpenLibraryId.Code); + } + + [Fact] + public void Validate_WhenOpenLibraryIdStartsWithOLButIsInvalidFormat_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { OpenLibraryId = "OL123ABC" }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.OpenLibraryId).WithErrorMessage(Errors.WrittenContent.InvalidOpenLibraryId.Code); + } + + [Fact] + public void Validate_WhenOpenLibraryIdHasValidFormatButInvalidSuffix_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { OpenLibraryId = "OL123456X" }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.OpenLibraryId).WithErrorMessage(Errors.WrittenContent.InvalidOpenLibraryId.Code); + } + + [Fact] + public void Validate_WhenCalledWithEmptyLibraryThingId_ShouldAddBook() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { LibraryThingId = null }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.LibraryThingId); + } + + [Fact] + public void Validate_WhenCalledWithValidLibraryThingId_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { LibraryThingId = new Faker().Random.String2(50) }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.LibraryThingId); + } + + [Fact] + public void Validate_WhenCalledWithInvalidLengthLibraryThingId_ShouldReturnBadRequest() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { LibraryThingId = new Faker().Random.String2(51) }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.LibraryThingId).WithErrorMessage(Errors.WrittenContent.LibraryThingIdMustBeMaximum50CharactersLong.Code); + } + + [Fact] + public void Validate_WhenCalledWithEmptyGoogleBooksId_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { GoogleBooksId = null! }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.GoogleBooksId); + } + + [Fact] + public void Validate_WhenCalledWithInvalidLengthGoogleBooksId_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { GoogleBooksId = new Faker().Random.String2(11) }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.GoogleBooksId).WithErrorMessage(Errors.WrittenContent.GoogleBooksIdMustBe12CharactersLong.Code); + } + + [Fact] + public void Validate_WhenCalledWithInvalidFormatGoogleBooksId_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { GoogleBooksId = new Faker().Random.String2(11) + " " }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.GoogleBooksId).WithErrorMessage(Errors.WrittenContent.InvalidGoogleBooksIdFormat.Code); + } + + [Fact] + public void Validate_WhenCalledWithValidGoogleBooksId_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { GoogleBooksId = new Faker().Random.String2(12, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-") }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.GoogleBooksId); + } + + [Fact] + public void Validate_WhenCalledWithEmptyBarnesAndNobleId_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { BarnesAndNobleId = null! }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.BarnesAndNobleId); + } + + [Fact] + public void Validate_WhenCalledWithInvalidLengthBarnesAndNobleId_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { BarnesAndNobleId = new Faker().Random.String2(11) }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.BarnesAndNobleId).WithErrorMessage(Errors.WrittenContent.BarnesAndNoblesIdMustBe10CharactersLong.Code); + } + + [Fact] + public void Validate_WhenCalledWithNonNumericBarnesAndNobleId_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { BarnesAndNobleId = new Faker().Random.AlphaNumeric(10) }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.BarnesAndNobleId).WithErrorMessage(Errors.WrittenContent.InvalidBarnesAndNoblesIdFormat.Code); + } + + [Fact] + public void Validate_WhenCalledWithValidBarnesAndNobleId_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { BarnesAndNobleId = new Faker().Random.Number(1000000000, 999999999).ToString() }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.BarnesAndNobleId); + } + + [Fact] + public void AddBook_WhenCalledWithEmptyAppleBooksId_ShouldAddBook() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { AppleBooksId = null }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.AppleBooksId); + } + + [Fact] + public void AddBook_WhenCalledWithValidAppleBooksId_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { AppleBooksId = "id123456" }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.AppleBooksId); + } + + [Fact] + public void AddBook_WhenCalledWithInvalidAppleBooksId_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { AppleBooksId = "invalid_id" }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.AppleBooksId).WithErrorMessage(Errors.WrittenContent.InvalidAppleBooksIdFormat.Code); + } + + [Fact] + public void Validate_WhenCalledWithNullIsbns_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { ISBNs = null! }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.ISBNs).WithErrorMessage(Errors.WrittenContent.IsbnListCannotBeNull.Code); + } + + [Fact] + public void Validate_WhenCalledWithEmptyIsbnValue_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { ISBNs = bookCommand.ISBNs!.Select((isbn, index) => index == 0 ? isbn with { Value = null! } : isbn).ToList() }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor("ISBNs[0].Value").WithErrorMessage(Errors.WrittenContent.IsbnValueCannotBeEmpty.Code); + } + + [Fact] + public void Validate_WhenCalledWithInvalidIsbn10Value_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { ISBNs = bookCommand.ISBNs!.Select((isbn, index) => index == 0 ? isbn with { Value = new Faker().Random.String2(5), Format = IsbnFormat.Isbn10 } : isbn).ToList() }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor("ISBNs[0].Value").WithErrorMessage(Errors.WrittenContent.InvalidIsbn10Format.Code); + } + + [Fact] + public void Validate_WhenCalledWithInvalidIsbn13Value_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { ISBNs = bookCommand.ISBNs!.Select((isbn, index) => index == 0 ? isbn with { Value = new Faker().Random.String2(5), Format = IsbnFormat.Isbn13 } : isbn).ToList() }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor("ISBNs[0].Value").WithErrorMessage(Errors.WrittenContent.InvalidIsbn13Format.Code); + } + + [Fact] + public void Validate_WhenCalledWithInvalidIsbnFormat_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { ISBNs = bookCommand.ISBNs!.Select((isbn, index) => index == 0 ? isbn with { Format = (IsbnFormat)99 } : isbn).ToList() }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor("ISBNs[0].Format").WithErrorMessage(Errors.WrittenContent.UnknownIsbnFormat.Code); + } + + [Fact] + public void Validate_WhenCalledWithValidIsbn10_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { ISBNs = bookCommand.ISBNs!.Select((isbn, index) => index == 0 ? isbn with { Value = "0-306-40615-2", Format = IsbnFormat.Isbn10 } : isbn).ToList() }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.ISBNs[0].Value); + } + + [Fact] + public void Validate_WhenCalledWithValidIsbn13_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { ISBNs = bookCommand.ISBNs!.Select((isbn, index) => index == 0 ? isbn with { Value = "978-3-16-148410-0", Format = IsbnFormat.Isbn13 } : isbn).ToList() }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.ISBNs[0].Value); + } + + [Fact] + public void Validate_WhenCalledWithNullContributors_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Contributors = null! }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.Contributors).WithErrorMessage(Errors.MediaContributor.ContributorsListCannotBeNull.Code); + } + + [Fact] + public void Validate_WhenCalledWithEmptyContributorName_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Contributors = bookCommand.Contributors!.Select((contributor, index) => index == 0 ? contributor with { Name = null! } : contributor).ToList() }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor("Contributors[0].Name").WithErrorMessage(Errors.MediaContributor.ContributorNameCannotBeEmpty.Code); + } + + [Fact] + public void Validate_WhenCalledWithInvalidLengthContributorName_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Contributors = bookCommand.Contributors!.Select((contributor, index) => index == 0 ? contributor with { Name = new Faker().Random.String2(101) } : contributor).ToList() }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor("Contributors[0].Name").WithErrorMessage(Errors.MediaContributor.ContributorNameMustBeMaximum100CharactersLong.Code); + } + + [Fact] + public void Validate_WhenCalledWithNullContributorRole_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Contributors = bookCommand.Contributors!.Select((contributor, index) => index == 0 ? contributor with { Role = null! } : contributor).ToList() }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor("Contributors[0].Role").WithErrorMessage(Errors.MediaContributor.ContributorRoleCannotBeNull.Code); + } + + [Fact] + public void Validate_WhenCalledWithEmptyContributorRoleName_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Contributors = bookCommand.Contributors!.Select((contributor, index) => index == 0 ? contributor with { Role = contributor.Role! with { Name = null! } } : contributor).ToList() }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor("Contributors[0].Role.Name").WithErrorMessage(Errors.MediaContributor.RoleNameCannotBeEmpty.Code); + } + + [Fact] + public void Validate_WhenCalledWithInvalidLengthContributorRoleName_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Contributors = bookCommand.Contributors!.Select((contributor, index) => index == 0 ? contributor with { Role = contributor.Role! with { Name = new Faker().Random.String2(51) } } : contributor).ToList() }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor("Contributors[0].Role.Name").WithErrorMessage(Errors.MediaContributor.RoleNameMustBeMaximum50CharactersLong.Code); + } + + [Fact] + public void Validate_WhenCalledWithEmptyContributorRoleCategory_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Contributors = bookCommand.Contributors!.Select((contributor, index) => index == 0 ? contributor with { Role = contributor.Role! with { Category = null! } } : contributor).ToList() }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor("Contributors[0].Role.Category").WithErrorMessage(Errors.MediaContributor.RoleCategoryCannotBeEmpty.Code); + } + + [Fact] + public void Validate_WhenCalledWithInvalidLengthContributorRoleCategory_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Contributors = bookCommand.Contributors!.Select((contributor, index) => index == 0 ? contributor with { Role = contributor.Role! with { Category = new Faker().Random.String2(51) } } : contributor).ToList() }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor("Contributors[0].Role.Category").WithErrorMessage(Errors.MediaContributor.RoleCategoryMustBeMaximum50CharactersLong.Code); + } + + [Fact] + public void Validate_WhenCalledWithNullRatings_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Ratings = null! }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor(x => x.Ratings).WithErrorMessage(Errors.Metadata.RatingsListCannotBeNull.Code); + } + + [Fact] + public void Validate_WhenCalledWithNegativeRatingValue_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Ratings = bookCommand.Ratings!.Select((rating, index) => index == 0 ? rating with { Value = -1 } : rating).ToList() }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor("Ratings[0].Value").WithErrorMessage(Errors.Metadata.RatingValueMustBePositive.Code); + } + + [Fact] + public void Validate_WhenCalledWithRatingValueGreaterThanMaxValue_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Ratings = bookCommand.Ratings!.Select((rating, index) => index == 0 ? rating with { Value = 6, MaxValue = 5 } : rating).ToList() }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor("Ratings[0].Value").WithErrorMessage(Errors.Metadata.RatingValueCannotBeGreaterThanMaxValue.Code); + } + + [Fact] + public void Validate_WhenCalledWithNegativeMaxRatingValue_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Ratings = bookCommand.Ratings!.Select((rating, index) => index == 0 ? rating with { MaxValue = -1 } : rating).ToList() }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor("Ratings[0].MaxValue").WithErrorMessage(Errors.Metadata.RatingMaxValueMustBePositive.Code); + } + + [Fact] + public void Validate_WhenCalledWithNegativeVoteCount_ShouldHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Ratings = bookCommand.Ratings!.Select((rating, index) => index == 0 ? rating with { VoteCount = -1 } : rating).ToList() }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldHaveValidationErrorFor("Ratings[0].VoteCount").WithErrorMessage(Errors.Metadata.RatingVoteCountMustBePositive.Code); + } + + [Fact] + public void Validate_WhenCalledWithValidRatings_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Ratings = bookCommand.Ratings!.Select((rating, index) => index == 0 ? rating + with { Value = 4, MaxValue = 5, VoteCount = 100 } : rating).ToList() }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.Ratings); + } + + [Fact] + public void Validate_WhenCalledWithNullVoteCount_ShouldNotHaveValidationError() + { + // Arrange + var bookCommand = _commandBookFixture.CreateCommandBook(); + bookCommand = bookCommand with { Ratings = bookCommand.Ratings!.Select((rating, index) => index == 0 ? rating with { VoteCount = null } : rating).ToList() }; + + // Act + var result = _validator.TestValidate(bookCommand); + + // Assert + result.ShouldNotHaveValidationErrorFor(x => x.Ratings[0].VoteCount); + } + #endregion +} \ No newline at end of file diff --git a/tests/UnitTests/Lumina.Application.UnitTests/Core/WrittenContentLibrary/BooksLibrary/Books/Commands/Fixtures/AddBookCommandFixture.cs b/tests/UnitTests/Lumina.Application.UnitTests/Core/WrittenContentLibrary/BooksLibrary/Books/Commands/Fixtures/AddBookCommandFixture.cs new file mode 100644 index 00000000..e82e4250 --- /dev/null +++ b/tests/UnitTests/Lumina.Application.UnitTests/Core/WrittenContentLibrary/BooksLibrary/Books/Commands/Fixtures/AddBookCommandFixture.cs @@ -0,0 +1,333 @@ +#region ========================================================================= USING ===================================================================================== +using AutoFixture; +using Bogus; +using Lumina.Application.Common.Models.Books; +using Lumina.Application.Common.Models.Common; +using Lumina.Application.Common.Models.Contributors; +using Lumina.Application.Core.WrittenContentLibrary.BooksLibrary.Books.Commands; +using Lumina.Application.UnitTests.Common.Setup; +using Lumina.Domain.Common.Enums; +using System.Diagnostics.CodeAnalysis; +#endregion + +namespace Lumina.Application.UnitTests.Core.WrittenContentLibrary.BooksLibrary.Books.Commands.Fixtures; + +/// +/// Fixture class for the class. +/// +[ExcludeFromCodeCoverage] +public class AddBookCommandFixture +{ + #region ================================================================== FIELD MEMBERS ================================================================================ + private readonly Fixture _fixture; + private readonly Random _random = new(); + private readonly Faker _faker; + #endregion + + #region ====================================================================== CTOR ===================================================================================== + /// + /// Initializes a new instance of the class. + /// + public AddBookCommandFixture() + { + _fixture = new Fixture(); + _fixture.Customizations.Add(new DateOnlySpecimenBuilder()); + _fixture.Customizations.Add(new NullableDateOnlySpecimenBuilder()); + _faker = new Faker(); + } + #endregion + + #region ===================================================================== METHODS =================================================================================== + /// + /// Creates a random valid command to add a book. + /// + /// The created command to add a book. + public AddBookCommand CreateCommandBook() + { + var releaseYear = _random.Next(2000, 2010); + var reReleaseYear = _random.Next(2010, 2020); + + var releaseInfo = new Faker() + .CustomInstantiator(f => new ReleaseInfoDto( + default, + default, + default, + default, + default, + default + )) + .RuleFor(x => x.OriginalReleaseDate, _faker.DateOnlyBetween(new DateOnly(releaseYear, 1, 1), new DateOnly(releaseYear, 12, 31))) + .RuleFor(x => x.OriginalReleaseYear, releaseYear) + .RuleFor(x => x.ReReleaseDate, _faker.DateOnlyBetween(new DateOnly(reReleaseYear, 1, 1), new DateOnly(reReleaseYear, 12, 31))) + .RuleFor(x => x.ReReleaseYear, reReleaseYear) + .RuleFor(x => x.ReleaseCountry, f => f.Random.String2(2)) + .RuleFor(x => x.ReleaseVersion, f => f.Random.String2(f.Random.Number(1, 50))) + .Generate(); + + var genre = new Faker() + .CustomInstantiator(f => new GenreDto( + default! + )) + .RuleFor(e => e.Name, f => f.Random.String2(f.Random.Number(1, 50))); + + var tag = new Faker() + .CustomInstantiator(f => new TagDto( + default! + )) + .RuleFor(e => e.Name, f => f.Random.String2(f.Random.Number(1, 50))); + + var mediaContributorRole = new Faker() + .CustomInstantiator(f => new MediaContributorRoleDto( + default!, + default! + )) + .RuleFor(e => e.Name, f => f.Random.String2(f.Random.Number(1, 50))) + .RuleFor(e => e.Category, f => f.Random.String2(f.Random.Number(1, 50))); + + var mediaContributor = new Faker() + .CustomInstantiator(f => new MediaContributorDto( + default!, + default! + )) + .RuleFor(e => e.Name, f => f.Random.String2(f.Random.Number(1, 50))) + .RuleFor(e => e.Role, mediaContributorRole); + + var rating = new Faker() + .CustomInstantiator(f => new BookRatingDto( + default, + default, + default, + default + )) + .RuleFor(e => e.Value, _random.Next(1, 5)) + .RuleFor(e => e.MaxValue, 5) + .RuleFor(e => e.Source, _fixture.Create()) + .RuleFor(e => e.VoteCount, _random.Next(1, 1000)); + + var language = new Faker() + .CustomInstantiator(f => new LanguageInfoDto( + default!, + default!, + default + )) + .RuleFor(e => e.LanguageName, f => f.Random.String2(f.Random.Number(1, 50))) + .RuleFor(e => e.LanguageCode, f => f.Random.String2(2)) + .RuleFor(e => e.NativeName, f => f.Random.String2(f.Random.Number(1, 50))); + + var originalLanguage = new Faker() + .CustomInstantiator(f => new LanguageInfoDto( + default!, + default!, + default + )) + .RuleFor(e => e.LanguageName, f => f.Random.String2(f.Random.Number(1, 50))) + .RuleFor(e => e.LanguageCode, f => f.Random.String2(2)) + .RuleFor(e => e.NativeName, f => f.Random.String2(f.Random.Number(1, 50))); + + var metadata = new Faker() + .CustomInstantiator(f => new WrittenContentMetadataDto( + default!, + default, + default, + default!, + default!, + default!, + default, + default, + default, + default + )) + .RuleFor(x => x.Title, f => f.Random.String2(f.Random.Number(1, 255))) + .RuleFor(x => x.OriginalTitle, f => f.Random.String2(f.Random.Number(1, 255))) + .RuleFor(x => x.Description, f => f.Random.String2(f.Random.Number(1, 2000))) + .RuleFor(x => x.ReleaseInfo, releaseInfo) + .RuleFor(p => p.Genres, f => genre.Generate(f.Random.Number(1, 5))) + .RuleFor(x => x.Tags, f => tag.Generate(f.Random.Number(1, 5))) + .RuleFor(x => x.Language, language) + .RuleFor(x => x.OriginalLanguage, originalLanguage) + .RuleFor(x => x.Publisher, f => f.Random.String2(f.Random.Number(1, 100))) + .RuleFor(x => x.PageCount, _random.Next(100, 300)); + + var isbn = new Faker() + .CustomInstantiator(f => new IsbnDto( + default!, + default + )) + .RuleFor(i => i.Value, f => + { + var isIsbn13 = f.Random.Bool(); + if (isIsbn13) + { + var prefix = f.Random.Bool() ? "978" : "979"; + var group = f.Random.Number(0, 99999).ToString().PadLeft(5, '0'); + var publisher = f.Random.Number(0, 999999).ToString().PadLeft(6, '0'); + var title = f.Random.Number(0, 99).ToString().PadLeft(2, '0'); + string isbn = $"{prefix}{group.Substring(0, 1)}{publisher}{title}"; + int sum = 0; + for (int i = 0; i < 12; i++) + sum += (i % 2 == 0 ? 1 : 3) * int.Parse(isbn[i].ToString()); + int checkDigit = (10 - (sum % 10)) % 10; + return $"{prefix}-{group.Substring(0, 1)}-{publisher}-{title}-{checkDigit}"; + } + else + { + var digits = new int[9]; + for (int i = 0; i < 9; i++) + digits[i] = f.Random.Number(0, 9); + int sum = 0; + for (int i = 0; i < 9; i++) + sum += (10 - i) * digits[i]; + int checkDigit = (11 - (sum % 11)) % 11; + string checkChar = checkDigit == 10 ? "X" : checkDigit.ToString(); + return $"{digits[0]}-{digits[1]}{digits[2]}-{digits[3]}{digits[4]}{digits[5]}{digits[6]}{digits[7]}{digits[8]}-{checkChar}"; + } + }) + .RuleFor(i => i.Format, (f, i) => i.Value!.Length > 13 ? IsbnFormat.Isbn13 : IsbnFormat.Isbn10); + + return new Faker() + .CustomInstantiator(f => new AddBookCommand( + default!, + default, + default, + default, + default, + default, + default, + default, + default, + default, + default, + default, + default, + default, + default!, + default!, + default! + )) + .RuleFor(x => x.Metadata, metadata) + .RuleFor(x => x.Format, _fixture.Create()) + .RuleFor(x => x.Edition, f => f.Random.String2(f.Random.Number(1, 50))) + .RuleFor(x => x.VolumeNumber, _random.Next(1, 3)) + .RuleFor(x => x.ASIN, f => f.Random.String2(10)) + .RuleFor(x => x.GoodreadsId, _random.Next(100000, 500000).ToString()) + .RuleFor(x => x.LCCN, f => + { + var letters = new string(Enumerable.Range(0, f.Random.Number(0, 3)) + .Select(_ => f.Random.Char('a', 'z')) + .ToArray()); + var digits = f.Random.String2(f.Random.Number(8, 10), "0123456789"); + return letters + digits; + }) + .RuleFor(x => x.OCLCNumber, f => + { + string[] prefixes = ["ocm", "ocn", "on", "(OCoLC)"]; + var prefix = f.Random.ArrayElement(prefixes); + string number; + switch (prefix) + { + case "ocm": + number = f.Random.String2(8, "0123456789"); + break; + case "ocn": + number = f.Random.String2(f.Random.Number(9, 11), "0123456789"); + break; + case "on": + number = f.Random.String2(10, "0123456789"); + break; + case "(OCoLC)": + number = f.Random.String2(f.Random.Number(8, 10), "0123456789"); + break; + default: + number = f.Random.String2(f.Random.Number(8, 10), "0123456789"); + return number; + } + return prefix + number; + }) + .RuleFor(x => x.OpenLibraryId, f => + { + var firstDigit = f.Random.Number(1, 9); + var remainingDigits = f.Random.String2(f.Random.Number(0, 6), "0123456789"); + var suffix = f.Random.ArrayElement(new[] { 'A', 'M', 'W' }); + return $"OL{firstDigit}{remainingDigits}{suffix}"; + }) + .RuleFor(x => x.LibraryThingId, f => f.Random.String2(f.Random.Number(1, 50))) + .RuleFor(x => x.GoogleBooksId, f => + { + const string validChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; + return new string(Enumerable.Repeat(validChars, 12) + .Select(s => s[f.Random.Number(validChars.Length - 1)]) + .ToArray()); + }) + .RuleFor(x => x.BarnesAndNobleId, f => f.Random.String2(10, "0123456789")) + .RuleFor(x => x.AppleBooksId, f => $"id{f.Random.Number(1, 999999)}") + .RuleFor(x => x.AppleBooksId, f => $"id{f.Random.Number(1, 999999)}") + .RuleFor(p => p.ISBNs, f => isbn.Generate(f.Random.Number(1, 5))) + .RuleFor(p => p.Ratings, f => rating.Generate(f.Random.Number(1, 5))) + .RuleFor(x => x.Contributors, f => mediaContributor.Generate(f.Random.Number(1, 5))); + } + + private void ConfigureCustomRequestTypes() + { + var releaseYear = _random.Next(2000, 2010); + var reReleaseYear = _random.Next(2010, 2020); + + _fixture.Register(() => new ReleaseInfoDto( + _faker.DateOnlyBetween(new DateOnly(releaseYear, 1, 1), new DateOnly(releaseYear, 12, 31)), + releaseYear, + _faker.DateOnlyBetween(new DateOnly(reReleaseYear, 1, 1), new DateOnly(reReleaseYear, 12, 31)), + reReleaseYear, + _fixture.Create()[2..], + _fixture.Create())); + + _fixture.Register(() => new GenreDto( + _fixture.Create() + )); + _fixture.Register(() => new TagDto( + _fixture.Create() + )); + _fixture.Register(() => new LanguageInfoDto( + _fixture.Create(), + _fixture.Create(), + _fixture.Create() + )); + _fixture.Register(() => new IsbnDto( + _fixture.Create(), + _fixture.Create() + )); + _fixture.Register(() => new BookRatingDto( + _fixture.Create(), + _fixture.Create(), + _fixture.Create(), + _fixture.Create() + )); + _fixture.Register(() => new WrittenContentMetadataDto( + _fixture.Create()[255..], + LimitStringLength(_fixture.Create(), 255), + LimitStringLength(_fixture.Create(), 2000), + _fixture.Create(), + _fixture.Create>(), + _fixture.Create>(), + _fixture.Create(), + _fixture.Create(), + _fixture.Create(), + _fixture.Create() + )); + _fixture.Register(() => new BookSeriesDto( + _fixture.Create() + )); + _fixture.Register(() => new MediaContributorRoleDto( + _fixture.Create(), + _fixture.Create() + )); + _fixture.Register(() => new MediaContributorDto( + _fixture.Create(), + _fixture.Create() + )); + } + + private static string? LimitStringLength(string? input, int maxLength) + { + return input?.Length > maxLength ? input[..maxLength] : input; + } + #endregion +} \ No newline at end of file diff --git a/tests/UnitTests/Lumina.Application.UnitTests/Lumina.Application.UnitTests.csproj b/tests/UnitTests/Lumina.Application.UnitTests/Lumina.Application.UnitTests.csproj index 0bad1f6a..0388cb46 100644 --- a/tests/UnitTests/Lumina.Application.UnitTests/Lumina.Application.UnitTests.csproj +++ b/tests/UnitTests/Lumina.Application.UnitTests/Lumina.Application.UnitTests.csproj @@ -12,6 +12,7 @@ +