mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 06:11:10 +00:00
Refactors the book, artist, and album slices across the stack
This commit is contained in:
1 parent
8aaf9fe64e
commit
ea6eb9f796
439 files changed
+28235
-3156
No files matched your search
+3
-2
@@ -49,8 +49,9 @@ public class BookMappingTests
|
||||
Assert.Equal(book.Metadata.OriginalLanguage.HasValue ? book.Metadata.OriginalLanguage.Value.LanguageCode : null, result.OriginalLanguageCode);
|
||||
Assert.Equal(book.Metadata.OriginalLanguage.HasValue ? book.Metadata.OriginalLanguage.Value.LanguageName : null, result.OriginalLanguageName);
|
||||
Assert.Equal(book.Metadata.OriginalLanguage.HasValue ? book.Metadata.OriginalLanguage.Value.NativeName.Value : null, result.OriginalLanguageNativeName);
|
||||
Assert.Equal(book.Metadata.Tags.ToRepositoryEntities().OrderBy(tag => tag.Name), result.Tags.OrderBy(tag => tag.Name));
|
||||
Assert.Equal(book.Metadata.Genres.ToRepositoryEntities().OrderBy(genre => genre.Name), result.Genres.OrderBy(genre => genre.Name));
|
||||
// The repository entity stores tags and genres as sets, so duplicate names in the domain collection collapse into a single entry.
|
||||
Assert.Equal(book.Metadata.Tags.ToRepositoryEntities().Distinct().OrderBy(tag => tag.Name), result.Tags.OrderBy(tag => tag.Name));
|
||||
Assert.Equal(book.Metadata.Genres.ToRepositoryEntities().Distinct().OrderBy(genre => genre.Name), result.Genres.OrderBy(genre => genre.Name));
|
||||
Assert.Equal(book.Metadata.Publisher.HasValue ? book.Metadata.Publisher.Value : null, result.Publisher);
|
||||
Assert.Equal(book.Metadata.PageCount.HasValue ? book.Metadata.PageCount.Value : null, result.PageCount);
|
||||
Assert.Equal(book.Format.HasValue ? book.Format.Value : null, result.Format);
|
||||
|
||||
-32
@@ -1,32 +0,0 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.Mapping.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
|
||||
using Lumina.Application.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Reading.Queries.GetReadingAvailability;
|
||||
using Lumina.Contracts.Fixtures.Core.Requests.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Reading;
|
||||
using Lumina.Contracts.Requests.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Reading;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.UnitTests.Common.Mapping.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="GetReadingAvailabilityRequestMapping"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class GetReadingAvailabilityRequestMappingTests
|
||||
{
|
||||
private readonly GetReadingAvailabilityRequestFixture _getReadingAvailabilityRequestFixture = new();
|
||||
|
||||
[Fact]
|
||||
public void ToQuery_WhenMappingValidRequest_ShouldMapCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
GetReadingAvailabilityRequest request = _getReadingAvailabilityRequestFixture.Create();
|
||||
|
||||
// Act
|
||||
GetReadingAvailabilityQuery result = request.ToQuery();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(request.BookId, result.BookId);
|
||||
}
|
||||
}
|
||||
-32
@@ -1,32 +0,0 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.Mapping.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
|
||||
using Lumina.Application.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Reading.Queries.GetReadingManifest;
|
||||
using Lumina.Contracts.Fixtures.Core.Requests.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Reading;
|
||||
using Lumina.Contracts.Requests.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Reading;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.UnitTests.Common.Mapping.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="GetReadingManifestRequestMapping"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class GetReadingManifestRequestMappingTests
|
||||
{
|
||||
private readonly GetReadingManifestRequestFixture _requestFixture = new();
|
||||
|
||||
[Fact]
|
||||
public void ToQuery_WhenMappingValidRequest_ShouldMapCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
GetReadingManifestRequest request = _requestFixture.Create();
|
||||
|
||||
// Act
|
||||
GetReadingManifestQuery result = request.ToQuery();
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(request.BookId, result.BookId);
|
||||
}
|
||||
}
|
||||
-33
@@ -1,33 +0,0 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.Mapping.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
|
||||
using Lumina.Application.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Reading.Queries.GetReadingResource;
|
||||
using Lumina.Contracts.Fixtures.Core.Requests.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Reading;
|
||||
using Lumina.Contracts.Requests.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Reading;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.UnitTests.Common.Mapping.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="GetReadingResourceRequestMapping"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class GetReadingResourceRequestMappingTests
|
||||
{
|
||||
private readonly GetReadingResourceRequestFixture _requestFixture = new();
|
||||
|
||||
[Fact]
|
||||
public void ToQuery_WhenMappingValidRequest_ShouldMapCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
GetReadingResourceRequest request = _requestFixture.Create();
|
||||
|
||||
// Act
|
||||
GetReadingResourceQuery result = request.ToQuery();
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(request.BookId, result.BookId);
|
||||
Assert.Equal(request.ResourceKey, result.ResourceKey);
|
||||
}
|
||||
}
|
||||
-33
@@ -1,33 +0,0 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.Mapping.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
|
||||
using Lumina.Application.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Reading.Queries.GetReadingSection;
|
||||
using Lumina.Contracts.Fixtures.Core.Requests.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Reading;
|
||||
using Lumina.Contracts.Requests.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Reading;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.UnitTests.Common.Mapping.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="GetReadingSectionRequestMapping"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class GetReadingSectionRequestMappingTests
|
||||
{
|
||||
private readonly GetReadingSectionRequestFixture _requestFixture = new();
|
||||
|
||||
[Fact]
|
||||
public void ToQuery_WhenMappingValidRequest_ShouldMapCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
GetReadingSectionRequest request = _requestFixture.Create();
|
||||
|
||||
// Act
|
||||
GetReadingSectionQuery result = request.ToQuery();
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(request.BookId, result.BookId);
|
||||
Assert.Equal(request.LocationRef, result.LocationRef);
|
||||
}
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.Application.Common.Mapping.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
|
||||
using Lumina.Application.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Commands.UpdateBookCover;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.Application.Fixtures.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Commands.UpdateBookCover;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.BookLibrary;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.UnitTests.Common.Mapping.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="UpdateBookCoverCommandMapping"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class UpdateBookCoverCommandMappingTests
|
||||
{
|
||||
private readonly UpdateBookCoverCommandFixture _commandFixture = new();
|
||||
private readonly BookArtworkEntityFixture _bookArtworkEntityFixture = new();
|
||||
|
||||
[Fact]
|
||||
public void ToRepositoryEntity_WhenBookHasNoCover_ShouldReturnANewEnrichedCoverArtwork()
|
||||
{
|
||||
// Arrange
|
||||
Guid userId = Guid.NewGuid();
|
||||
Guid bookId = Guid.NewGuid();
|
||||
UpdateBookCoverCommand command = _commandFixture.Create(bookId: bookId.ToString());
|
||||
string storedCoverPath = "/media/books/cover.jpg";
|
||||
|
||||
// Act
|
||||
BookArtworkEntity cover = command.ToRepositoryEntity(null, bookId, storedCoverPath, userId);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(bookId, cover.BookId);
|
||||
Assert.Equal(ArtworkType.Cover, cover.ArtworkType);
|
||||
Assert.Equal(0, cover.Ordinal);
|
||||
Assert.Equal(storedCoverPath, cover.FileName);
|
||||
Assert.Equal(ArtworkStatus.Enriched, cover.Status);
|
||||
Assert.Equal(userId, cover.CreatedBy);
|
||||
Assert.NotEqual(default, cover.CreatedOnUtc);
|
||||
Assert.Null(cover.UpdatedBy);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToRepositoryEntity_WhenBookHasAnExistingCover_ShouldReplaceAndReturnTheStoredCover()
|
||||
{
|
||||
// Arrange
|
||||
Guid userId = Guid.NewGuid();
|
||||
Guid bookId = Guid.NewGuid();
|
||||
UpdateBookCoverCommand command = _commandFixture.Create(bookId: bookId.ToString());
|
||||
BookArtworkEntity existingCover = _bookArtworkEntityFixture.Create(bookId: bookId, artworkType: ArtworkType.Cover, fileName: "/media/books/old-cover.jpg", status: ArtworkStatus.Pending);
|
||||
string storedCoverPath = "/media/books/cover.jpg";
|
||||
|
||||
// Act
|
||||
BookArtworkEntity cover = command.ToRepositoryEntity(existingCover, bookId, storedCoverPath, userId);
|
||||
|
||||
// Assert
|
||||
Assert.Same(existingCover, cover);
|
||||
Assert.Equal(storedCoverPath, cover.FileName);
|
||||
Assert.Equal(ArtworkStatus.Enriched, cover.Status);
|
||||
Assert.Equal(userId, cover.UpdatedBy);
|
||||
Assert.NotNull(cover.UpdatedOnUtc);
|
||||
Assert.NotNull(cover.LastUpdateUtc);
|
||||
}
|
||||
}
|
||||
+119
@@ -0,0 +1,119 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.Mapping.MediaLibrary.WrittenContentLibrary.Common;
|
||||
using Lumina.Contracts.DTO.MediaLibrary.WrittenContentLibrary;
|
||||
using Lumina.Contracts.Fixtures.Core.DTO.Common;
|
||||
using Lumina.Contracts.Fixtures.Core.DTO.MediaLibrary.WrittenContentLibrary;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.Core.BoundedContexts.WrittenContentLibraryBoundedContext.BookLibraryAggregate.ValueObjects;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.UnitTests.Common.Mapping.MediaLibrary.WrittenContentLibrary.Common;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="WrittenContentMetadataDtoMapping"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class WrittenContentMetadataDtoMappingTests
|
||||
{
|
||||
private readonly WrittenContentMetadataDtoFixture _writtenContentMetadataDtoFixture = new();
|
||||
private readonly ReleaseInfoDtoFixture _releaseInfoDtoFixture = new();
|
||||
private readonly GenreDtoFixture _genreDtoFixture = new();
|
||||
private readonly TagDtoFixture _tagDtoFixture = new();
|
||||
|
||||
[Fact]
|
||||
public void ToDomainEntity_WhenMappingCompleteWrittenContentMetadataDto_ShouldMapAllPropertiesCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
WrittenContentMetadataDto dto = _writtenContentMetadataDtoFixture.Create();
|
||||
|
||||
// Act
|
||||
Result<WrittenContentMetadata> result = dto.ToDomainEntity();
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(dto.Title, result.Value.Title);
|
||||
Assert.True(result.Value.OriginalTitle.HasValue);
|
||||
Assert.Equal(dto.OriginalTitle, result.Value.OriginalTitle.Value);
|
||||
Assert.True(result.Value.Description.HasValue);
|
||||
Assert.Equal(dto.Description, result.Value.Description.Value);
|
||||
Assert.Equal(dto.Genres!.Count, result.Value.Genres.Count);
|
||||
Assert.Equal(dto.Tags!.Count, result.Value.Tags.Count);
|
||||
Assert.True(result.Value.Language.HasValue);
|
||||
Assert.Equal(dto.Language!.LanguageCode!.ToLowerInvariant(), result.Value.Language.Value.LanguageCode);
|
||||
Assert.True(result.Value.OriginalLanguage.HasValue);
|
||||
Assert.Equal(dto.OriginalLanguage!.LanguageCode!.ToLowerInvariant(), result.Value.OriginalLanguage.Value.LanguageCode);
|
||||
Assert.True(result.Value.Publisher.HasValue);
|
||||
Assert.Equal(dto.Publisher, result.Value.Publisher.Value);
|
||||
Assert.True(result.Value.PageCount.HasValue);
|
||||
Assert.Equal(dto.PageCount!.Value, result.Value.PageCount.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToDomainEntity_WhenOptionalValuesAreMissing_ShouldMapWithoutOptionalValues()
|
||||
{
|
||||
// Arrange
|
||||
WrittenContentMetadataDto dto = _writtenContentMetadataDtoFixture.Create(
|
||||
includeOriginalTitle: false,
|
||||
includeDescription: false,
|
||||
includeLanguage: false,
|
||||
includeOriginalLanguage: false,
|
||||
includePublisher: false,
|
||||
includePageCount: false);
|
||||
|
||||
// Act
|
||||
Result<WrittenContentMetadata> result = dto.ToDomainEntity();
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.False(result.Value.OriginalTitle.HasValue);
|
||||
Assert.False(result.Value.Description.HasValue);
|
||||
Assert.False(result.Value.Language.HasValue);
|
||||
Assert.False(result.Value.OriginalLanguage.HasValue);
|
||||
Assert.False(result.Value.Publisher.HasValue);
|
||||
Assert.False(result.Value.PageCount.HasValue);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToDomainEntity_WhenReleaseInfoCreationFails_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
WrittenContentMetadataDto dto = _writtenContentMetadataDtoFixture.Create(
|
||||
releaseInfo: _releaseInfoDtoFixture.Create(
|
||||
originalReleaseDate: new DateOnly(2025, 1, 1),
|
||||
originalReleaseYear: 2024));
|
||||
|
||||
// Act
|
||||
Result<WrittenContentMetadata> result = dto.ToDomainEntity();
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToDomainEntity_WhenGenreCreationFails_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
WrittenContentMetadataDto dto = _writtenContentMetadataDtoFixture.Create(genres: [_genreDtoFixture.Create(name: "")]);
|
||||
|
||||
// Act
|
||||
Result<WrittenContentMetadata> result = dto.ToDomainEntity();
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToDomainEntity_WhenTagCreationFails_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
WrittenContentMetadataDto dto = _writtenContentMetadataDtoFixture.Create(tags: [_tagDtoFixture.Create(name: "")]);
|
||||
|
||||
// Act
|
||||
Result<WrittenContentMetadata> result = dto.ToDomainEntity();
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
}
|
||||
}
|
||||
+9
-9
@@ -1,10 +1,10 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using AutoFixture;
|
||||
using AutoFixture.AutoNSubstitute;
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaContributors;
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management;
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Books;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.BookLibrary;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.MediaContributors;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.MediaLibrary;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
@@ -321,9 +321,9 @@ public class AddBookCommandHandlerTests
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.NotNull(insertedEntity);
|
||||
Assert.Equal(2, insertedEntity!.BookContributors.Count);
|
||||
Assert.Contains(insertedEntity.BookContributors, contributor => contributor.MediaContributorId == firstContributorId && contributor.Role == MediaContributorRole.Author);
|
||||
Assert.Contains(insertedEntity.BookContributors, contributor => contributor.MediaContributorId == secondContributorId && contributor.Role == MediaContributorRole.Illustrator);
|
||||
Assert.Equal(2, insertedEntity!.Contributors.Count);
|
||||
Assert.Contains(insertedEntity.Contributors, contributor => contributor.MediaContributorId == firstContributorId && contributor.Role == MediaContributorRole.Author);
|
||||
Assert.Contains(insertedEntity.Contributors, contributor => contributor.MediaContributorId == secondContributorId && contributor.Role == MediaContributorRole.Illustrator);
|
||||
await _mockBookRepository.Received(1).InsertAsync(Arg.Any<BookEntity>(), Arg.Any<CancellationToken>());
|
||||
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
@@ -355,10 +355,10 @@ public class AddBookCommandHandlerTests
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.NotNull(insertedEntity);
|
||||
Assert.Equal(2, insertedEntity!.BookContributors.Count);
|
||||
Assert.All(insertedEntity.BookContributors, linkedContributor => Assert.Equal(contributorId, linkedContributor.MediaContributorId));
|
||||
Assert.Contains(insertedEntity.BookContributors, linkedContributor => linkedContributor.Role == MediaContributorRole.Author);
|
||||
Assert.Contains(insertedEntity.BookContributors, linkedContributor => linkedContributor.Role == MediaContributorRole.Illustrator);
|
||||
Assert.Equal(2, insertedEntity!.Contributors.Count);
|
||||
Assert.All(insertedEntity.Contributors, linkedContributor => Assert.Equal(contributorId, linkedContributor.MediaContributorId));
|
||||
Assert.Contains(insertedEntity.Contributors, linkedContributor => linkedContributor.Role == MediaContributorRole.Author);
|
||||
Assert.Contains(insertedEntity.Contributors, linkedContributor => linkedContributor.Role == MediaContributorRole.Illustrator);
|
||||
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -1545,7 +1545,7 @@ public class AddBookCommandValidatorTests
|
||||
List<Error> result = _validator.TestValidate(bookCommand);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.WrittenContent.ContributorsListCannotBeNull);
|
||||
result.ShouldHaveValidationError(Errors.MediaContributor.ContributorsListCannotBeNull);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -1558,7 +1558,7 @@ public class AddBookCommandValidatorTests
|
||||
List<Error> result = _validator.TestValidate(bookCommand);
|
||||
|
||||
// Assert
|
||||
result.ShouldNotHaveValidationError(Errors.WrittenContent.ContributorsListCannotBeNull);
|
||||
result.ShouldNotHaveValidationError(Errors.MediaContributor.ContributorsListCannotBeNull);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
+40
-51
@@ -1,8 +1,10 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaContributors;
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management;
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Books;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.BookLibrary;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.MediaContributors;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.MediaLibrary;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Common.Infrastructure.Authentication;
|
||||
using Lumina.Application.Common.Infrastructure.Authorization;
|
||||
@@ -10,6 +12,7 @@ using Lumina.Application.Common.Infrastructure.Authorization.Policies.LibraryOwn
|
||||
using Lumina.Application.Common.Infrastructure.Validation;
|
||||
using Lumina.Application.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Commands.UpdateBook;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaContributors;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaLibrary.Management;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.Application.Fixtures.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Commands.UpdateBook;
|
||||
using Lumina.Contracts.Fixtures.Core.DTO.Common;
|
||||
@@ -42,6 +45,7 @@ public class UpdateBookCommandHandlerTests
|
||||
{
|
||||
private readonly IUnitOfWork _mockUnitOfWork;
|
||||
private readonly IBookRepository _mockBookRepository;
|
||||
private readonly ILibraryRepository _mockLibraryRepository;
|
||||
private readonly IMediaContributorRepository _mockMediaContributorRepository;
|
||||
private readonly IAuthorizationService _mockAuthorizationService;
|
||||
private readonly ICurrentUserService _mockCurrentUserService;
|
||||
@@ -57,6 +61,7 @@ public class UpdateBookCommandHandlerTests
|
||||
private readonly ReleaseInfoDtoFixture _releaseInfoDtoFixture = new();
|
||||
private readonly WrittenContentMetadataDtoFixture _writtenContentMetadataDtoFixture = new();
|
||||
private readonly MediaContributorReferenceDtoFixture _mediaContributorReferenceDtoFixture = new();
|
||||
private readonly LibraryEntityFixture _libraryEntityFixture = new();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="UpdateBookCommandHandlerTests"/> class.
|
||||
@@ -64,10 +69,12 @@ public class UpdateBookCommandHandlerTests
|
||||
public UpdateBookCommandHandlerTests()
|
||||
{
|
||||
_mockBookRepository = Substitute.For<IBookRepository>();
|
||||
_mockLibraryRepository = Substitute.For<ILibraryRepository>();
|
||||
_mockMediaContributorRepository = Substitute.For<IMediaContributorRepository>();
|
||||
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
|
||||
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
|
||||
_mockUnitOfWork.BookRepository.Returns(_mockBookRepository);
|
||||
_mockUnitOfWork.LibraryRepository.Returns(_mockLibraryRepository);
|
||||
_mockUnitOfWork.MediaContributorRepository.Returns(_mockMediaContributorRepository);
|
||||
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
|
||||
_mockCurrentUserService = Substitute.For<ICurrentUserService>();
|
||||
@@ -82,6 +89,9 @@ public class UpdateBookCommandHandlerTests
|
||||
_mockValidator.Validate(Arg.Any<UpdateBookCommand>()).Returns([]);
|
||||
_mockBookRepository.UpdateAsync(Arg.Any<BookEntity>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Result.Updated);
|
||||
// Default stub: the library of the book exists.
|
||||
_mockLibraryRepository.GetByIdAsync(Arg.Any<Guid>(), Arg.Any<bool>(), Arg.Any<bool>(), Arg.Any<CancellationToken>())
|
||||
.Returns(_ => Result.From<LibraryEntity?>(_libraryEntityFixture.Create()));
|
||||
// Default stub: every referenced media contributor already exists.
|
||||
_mockMediaContributorRepository.GetByIdsAsync(Arg.Any<IReadOnlyCollection<Guid>>(), Arg.Any<CancellationToken>())
|
||||
.Returns(callInfo =>
|
||||
@@ -268,10 +278,10 @@ public class UpdateBookCommandHandlerTests
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.NotNull(updatedEntity);
|
||||
Assert.Equal(2, updatedEntity!.BookContributors.Count);
|
||||
Assert.All(updatedEntity.BookContributors, linkedContributor => Assert.Equal(contributorId, linkedContributor.MediaContributorId));
|
||||
Assert.Contains(updatedEntity.BookContributors, linkedContributor => linkedContributor.Role == MediaContributorRole.Author);
|
||||
Assert.Contains(updatedEntity.BookContributors, linkedContributor => linkedContributor.Role == MediaContributorRole.Illustrator);
|
||||
Assert.Equal(2, updatedEntity!.Contributors.Count);
|
||||
Assert.All(updatedEntity.Contributors, linkedContributor => Assert.Equal(contributorId, linkedContributor.MediaContributorId));
|
||||
Assert.Contains(updatedEntity.Contributors, linkedContributor => linkedContributor.Role == MediaContributorRole.Author);
|
||||
Assert.Contains(updatedEntity.Contributors, linkedContributor => linkedContributor.Role == MediaContributorRole.Illustrator);
|
||||
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
@@ -304,54 +314,12 @@ public class UpdateBookCommandHandlerTests
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.NotNull(updatedEntity);
|
||||
Assert.Equal(2, updatedEntity!.BookContributors.Count);
|
||||
Assert.Equal(firstContributorId, updatedEntity.BookContributors[0].MediaContributorId);
|
||||
Assert.Equal(secondContributorId, updatedEntity.BookContributors[1].MediaContributorId);
|
||||
Assert.Equal(2, updatedEntity!.Contributors.Count);
|
||||
Assert.Equal(firstContributorId, updatedEntity.Contributors[0].MediaContributorId);
|
||||
Assert.Equal(secondContributorId, updatedEntity.Contributors[1].MediaContributorId);
|
||||
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenStoredBookHasEnrichmentAndCover_ShouldPreserveThemInTheResponse()
|
||||
{
|
||||
// Arrange
|
||||
UpdateBookCommand command = _commandBookFixture.Create();
|
||||
BookEntity existingBook = _bookEntityFixture.Create(id: Guid.Parse(command.BookId!), libraryId: Guid.Parse(command.LibraryId!));
|
||||
existingBook.MetadataStatus = MetadataStatus.Enriched;
|
||||
existingBook.LastMetadataUpdateUtc = new DateTime(2024, 1, 2, 3, 4, 5, DateTimeKind.Utc);
|
||||
existingBook.MetadataProvider = "TestProvider";
|
||||
existingBook.BookArtwork.Add(new BookArtworkEntity
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
BookId = existingBook.Id,
|
||||
ArtworkType = ArtworkType.Cover,
|
||||
Ordinal = 0,
|
||||
FileName = "/media/covers/cover.jpg",
|
||||
Status = ArtworkStatus.Enriched,
|
||||
CreatedOnUtc = DateTime.UtcNow,
|
||||
CreatedBy = Guid.NewGuid(),
|
||||
UpdatedBy = null
|
||||
});
|
||||
BookEntity? updatedBook = null;
|
||||
_mockBookRepository.GetByIdAsync(Arg.Any<Guid>(), Arg.Any<bool>(), Arg.Any<bool>(), Arg.Any<CancellationToken>())
|
||||
.Returns(callInfo => Result.From<BookEntity?>(callInfo.ArgAt<bool>(2) ? existingBook : updatedBook));
|
||||
_mockBookRepository.UpdateAsync(Arg.Any<BookEntity>(), Arg.Any<CancellationToken>())
|
||||
.Returns(callInfo =>
|
||||
{
|
||||
updatedBook = callInfo.Arg<BookEntity>();
|
||||
return Result.Updated;
|
||||
});
|
||||
|
||||
// Act
|
||||
Result<BookResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(MetadataStatus.Enriched, result.Value.MetadataStatus);
|
||||
Assert.Equal(existingBook.LastMetadataUpdateUtc, result.Value.LastMetadataUpdateUtc);
|
||||
Assert.Equal(existingBook.MetadataProvider, result.Value.MetadataProvider);
|
||||
Assert.Equal("/media/covers/cover.jpg", result.Value.CoverPath);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenCalledWithInvalidIsbn_ShouldReturnFailureResult()
|
||||
{
|
||||
@@ -492,4 +460,25 @@ public class UpdateBookCommandHandlerTests
|
||||
await _mockBookRepository.DidNotReceive().UpdateAsync(Arg.Any<BookEntity>(), Arg.Any<CancellationToken>());
|
||||
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenLibraryDoesNotExist_ShouldReturnLibraryNotFoundErrorWithoutUpdating()
|
||||
{
|
||||
// Arrange
|
||||
UpdateBookCommand command = _commandBookFixture.Create();
|
||||
BookEntity existingBook = _bookEntityFixture.Create(id: Guid.Parse(command.BookId!), libraryId: Guid.Parse(command.LibraryId!));
|
||||
_mockBookRepository.GetByIdAsync(Arg.Any<Guid>(), Arg.Any<bool>(), Arg.Any<bool>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<BookEntity?>(existingBook));
|
||||
_mockLibraryRepository.GetByIdAsync(Arg.Any<Guid>(), Arg.Any<bool>(), Arg.Any<bool>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<LibraryEntity?>(null));
|
||||
|
||||
// Act
|
||||
Result<BookResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Library.LibraryNotFound, result.FirstError);
|
||||
await _mockBookRepository.DidNotReceive().UpdateAsync(Arg.Any<BookEntity>(), Arg.Any<CancellationToken>());
|
||||
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -1532,7 +1532,7 @@ public class UpdateBookCommandValidatorTests
|
||||
List<Error> result = _validator.TestValidate(bookCommand);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.WrittenContent.ContributorsListCannotBeNull);
|
||||
result.ShouldHaveValidationError(Errors.MediaContributor.ContributorsListCannotBeNull);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -1545,7 +1545,7 @@ public class UpdateBookCommandValidatorTests
|
||||
List<Error> result = _validator.TestValidate(bookCommand);
|
||||
|
||||
// Assert
|
||||
result.ShouldNotHaveValidationError(Errors.WrittenContent.ContributorsListCannotBeNull);
|
||||
result.ShouldNotHaveValidationError(Errors.MediaContributor.ContributorsListCannotBeNull);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
+142
-61
@@ -2,7 +2,7 @@
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaContributors;
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management;
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Books;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.BookLibrary;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.MediaContributors;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.MediaLibrary;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
@@ -12,6 +12,7 @@ using Lumina.Application.Common.Infrastructure.Authorization.Policies.LibraryOwn
|
||||
using Lumina.Application.Common.Infrastructure.Validation;
|
||||
using Lumina.Application.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Artwork;
|
||||
using Lumina.Application.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Commands.UpdateBookCover;
|
||||
using Lumina.Contracts.Responses.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaContributors;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaLibrary.Management;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
@@ -64,6 +65,7 @@ public class UpdateBookCoverCommandHandlerTests
|
||||
_mockLibraryRepository = Substitute.For<ILibraryRepository>();
|
||||
_mockMediaContributorRepository = Substitute.For<IMediaContributorRepository>();
|
||||
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
|
||||
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
|
||||
_mockUnitOfWork.BookRepository.Returns(_mockBookRepository);
|
||||
_mockUnitOfWork.LibraryRepository.Returns(_mockLibraryRepository);
|
||||
_mockUnitOfWork.MediaContributorRepository.Returns(_mockMediaContributorRepository);
|
||||
@@ -90,24 +92,24 @@ public class UpdateBookCoverCommandHandlerTests
|
||||
{
|
||||
// Arrange
|
||||
UpdateBookCoverCommand command = _commandFixture.Create();
|
||||
BookEntity existingBook = _bookEntityFixture.Create(id: command.BookId);
|
||||
existingBook.BookArtwork = [];
|
||||
BookEntity existingBook = _bookEntityFixture.Create(id: Guid.Parse(command.BookId!), libraryId: Guid.Parse(command.LibraryId!));
|
||||
existingBook.Artwork = [];
|
||||
LibraryEntity library = _libraryEntityFixture.Create(id: existingBook.LibraryId);
|
||||
_mockBookRepository.GetByIdAsync(command.BookId, Arg.Any<CancellationToken>())
|
||||
_mockBookRepository.GetByIdAsync(Guid.Parse(command.BookId!), cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<BookEntity?>(existingBook));
|
||||
_mockLibraryRepository.GetByIdAsync(existingBook.LibraryId, Arg.Any<CancellationToken>())
|
||||
_mockLibraryRepository.GetByIdAsync(existingBook.LibraryId, cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<LibraryEntity?>(library));
|
||||
|
||||
// Act
|
||||
Result<string> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
Result<UpdateBookCoverResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal("/media/books/cover.jpg", result.Value);
|
||||
Assert.Single(existingBook.BookArtwork);
|
||||
Assert.Equal(ArtworkType.Cover, existingBook.BookArtwork[0].ArtworkType);
|
||||
Assert.Equal(result.Value, existingBook.BookArtwork[0].FileName);
|
||||
Assert.Equal(ArtworkStatus.Enriched, existingBook.BookArtwork[0].Status);
|
||||
Assert.Equal("/media/books/cover.jpg", result.Value.CoverPath);
|
||||
Assert.Single(existingBook.Artwork);
|
||||
Assert.Equal(ArtworkType.Cover, existingBook.Artwork[0].ArtworkType);
|
||||
Assert.Equal(result.Value.CoverPath, existingBook.Artwork[0].FileName);
|
||||
Assert.Equal(ArtworkStatus.Enriched, existingBook.Artwork[0].Status);
|
||||
await _mockBookArtworkService.Received(1).SaveBookArtworkAsync(
|
||||
existingBook.LibraryId,
|
||||
existingBook.Id,
|
||||
@@ -125,23 +127,23 @@ public class UpdateBookCoverCommandHandlerTests
|
||||
{
|
||||
// Arrange
|
||||
UpdateBookCoverCommand command = _commandFixture.Create();
|
||||
BookEntity existingBook = _bookEntityFixture.Create(id: command.BookId);
|
||||
BookArtworkEntity existingCover = _bookArtworkEntityFixture.Create(bookId: command.BookId, artworkType: ArtworkType.Cover, fileName: "/media/books/old-cover.jpg");
|
||||
existingBook.BookArtwork = [existingCover];
|
||||
BookEntity existingBook = _bookEntityFixture.Create(id: Guid.Parse(command.BookId!), libraryId: Guid.Parse(command.LibraryId!));
|
||||
BookArtworkEntity existingCover = _bookArtworkEntityFixture.Create(bookId: Guid.Parse(command.BookId!), artworkType: ArtworkType.Cover, fileName: "/media/books/old-cover.jpg");
|
||||
existingBook.Artwork = [existingCover];
|
||||
LibraryEntity library = _libraryEntityFixture.Create(id: existingBook.LibraryId);
|
||||
_mockBookRepository.GetByIdAsync(command.BookId, Arg.Any<CancellationToken>())
|
||||
_mockBookRepository.GetByIdAsync(Guid.Parse(command.BookId!), cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<BookEntity?>(existingBook));
|
||||
_mockLibraryRepository.GetByIdAsync(existingBook.LibraryId, Arg.Any<CancellationToken>())
|
||||
_mockLibraryRepository.GetByIdAsync(existingBook.LibraryId, cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<LibraryEntity?>(library));
|
||||
|
||||
// Act
|
||||
Result<string> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
Result<UpdateBookCoverResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal("/media/books/cover.jpg", result.Value);
|
||||
Assert.Single(existingBook.BookArtwork);
|
||||
Assert.Equal(result.Value, existingCover.FileName);
|
||||
Assert.Equal("/media/books/cover.jpg", result.Value.CoverPath);
|
||||
Assert.Single(existingBook.Artwork);
|
||||
Assert.Equal(result.Value.CoverPath, existingCover.FileName);
|
||||
Assert.Equal(ArtworkStatus.Enriched, existingCover.Status);
|
||||
Assert.Equal(_userId, existingCover.UpdatedBy);
|
||||
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
@@ -152,24 +154,24 @@ public class UpdateBookCoverCommandHandlerTests
|
||||
{
|
||||
// Arrange
|
||||
UpdateBookCoverCommand command = _commandFixture.Create();
|
||||
BookEntity existingBook = _bookEntityFixture.Create(id: command.BookId);
|
||||
BookEntity existingBook = _bookEntityFixture.Create(id: Guid.Parse(command.BookId!), libraryId: Guid.Parse(command.LibraryId!));
|
||||
MediaContributorEntity author = _mediaContributorEntityFixture.Create(displayName: "J.R.R. Tolkien");
|
||||
MediaContributorEntity illustrator = _mediaContributorEntityFixture.Create(displayName: "Alan Lee");
|
||||
existingBook.BookContributors =
|
||||
existingBook.Contributors =
|
||||
[
|
||||
_bookContributorEntityFixture.Create(bookId: command.BookId, mediaContributorId: author.Id, roleCategory: MediaContributorRoleCategory.Author),
|
||||
_bookContributorEntityFixture.Create(bookId: command.BookId, mediaContributorId: illustrator.Id, roleCategory: MediaContributorRoleCategory.Illustrator)
|
||||
_bookContributorEntityFixture.Create(bookId: Guid.Parse(command.BookId!), mediaContributorId: author.Id, role: MediaContributorRole.Author),
|
||||
_bookContributorEntityFixture.Create(bookId: Guid.Parse(command.BookId!), mediaContributorId: illustrator.Id, role: MediaContributorRole.Illustrator)
|
||||
];
|
||||
LibraryEntity library = _libraryEntityFixture.Create(id: existingBook.LibraryId);
|
||||
_mockBookRepository.GetByIdAsync(command.BookId, Arg.Any<CancellationToken>())
|
||||
_mockBookRepository.GetByIdAsync(Guid.Parse(command.BookId!), cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<BookEntity?>(existingBook));
|
||||
_mockLibraryRepository.GetByIdAsync(existingBook.LibraryId, Arg.Any<CancellationToken>())
|
||||
_mockLibraryRepository.GetByIdAsync(existingBook.LibraryId, cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<LibraryEntity?>(library));
|
||||
_mockMediaContributorRepository.GetByIdsAsync(Arg.Any<IReadOnlyCollection<Guid>>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IReadOnlyList<MediaContributorEntity>>([author, illustrator]));
|
||||
|
||||
// Act
|
||||
Result<string> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
Result<UpdateBookCoverResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
@@ -185,37 +187,31 @@ public class UpdateBookCoverCommandHandlerTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenAuthorResolutionFails_ShouldStoreArtworkWithEmptyAuthorName()
|
||||
public async Task HandleAsync_WhenAuthorResolutionFails_ShouldReturnFailureResultWithoutStoringArtwork()
|
||||
{
|
||||
// Arrange
|
||||
UpdateBookCoverCommand command = _commandFixture.Create();
|
||||
BookEntity existingBook = _bookEntityFixture.Create(id: command.BookId);
|
||||
existingBook.BookContributors =
|
||||
BookEntity existingBook = _bookEntityFixture.Create(id: Guid.Parse(command.BookId!), libraryId: Guid.Parse(command.LibraryId!));
|
||||
existingBook.Contributors =
|
||||
[
|
||||
_bookContributorEntityFixture.Create(bookId: command.BookId, roleCategory: MediaContributorRoleCategory.Author)
|
||||
_bookContributorEntityFixture.Create(bookId: Guid.Parse(command.BookId!), role: MediaContributorRole.Author)
|
||||
];
|
||||
LibraryEntity library = _libraryEntityFixture.Create(id: existingBook.LibraryId);
|
||||
_mockBookRepository.GetByIdAsync(command.BookId, Arg.Any<CancellationToken>())
|
||||
_mockBookRepository.GetByIdAsync(Guid.Parse(command.BookId!), cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<BookEntity?>(existingBook));
|
||||
_mockLibraryRepository.GetByIdAsync(existingBook.LibraryId, Arg.Any<CancellationToken>())
|
||||
_mockLibraryRepository.GetByIdAsync(existingBook.LibraryId, cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<LibraryEntity?>(library));
|
||||
_mockMediaContributorRepository.GetByIdsAsync(Arg.Any<IReadOnlyCollection<Guid>>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Errors.Library.LibraryIdCannotBeEmpty);
|
||||
|
||||
// Act
|
||||
Result<string> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
Result<UpdateBookCoverResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
await _mockBookArtworkService.Received(1).SaveBookArtworkAsync(
|
||||
existingBook.LibraryId,
|
||||
existingBook.Id,
|
||||
library.Title,
|
||||
string.Empty,
|
||||
existingBook.Title,
|
||||
Arg.Any<System.IO.Stream>(),
|
||||
Arg.Any<string>(),
|
||||
Arg.Any<CancellationToken>());
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Library.LibraryIdCannotBeEmpty, result.FirstError);
|
||||
await _mockBookArtworkService.DidNotReceive().SaveBookArtworkAsync(Arg.Any<Guid>(), Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<System.IO.Stream>(), Arg.Any<string>(), Arg.Any<CancellationToken>());
|
||||
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -226,12 +222,12 @@ public class UpdateBookCoverCommandHandlerTests
|
||||
_mockValidator.Validate(Arg.Any<UpdateBookCoverCommand>()).Returns([Errors.WrittenContent.BookCoverCannotBeNull]);
|
||||
|
||||
// Act
|
||||
Result<string> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
Result<UpdateBookCoverResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.WrittenContent.BookCoverCannotBeNull, result.FirstError);
|
||||
await _mockBookRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
|
||||
await _mockBookRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>());
|
||||
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
@@ -243,7 +239,7 @@ public class UpdateBookCoverCommandHandlerTests
|
||||
_mockCurrentUserService.UserId.Returns((Guid?)null);
|
||||
|
||||
// Act
|
||||
Result<string> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
Result<UpdateBookCoverResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
@@ -256,16 +252,16 @@ public class UpdateBookCoverCommandHandlerTests
|
||||
{
|
||||
// Arrange
|
||||
UpdateBookCoverCommand command = _commandFixture.Create();
|
||||
_mockBookRepository.GetByIdAsync(command.BookId, Arg.Any<CancellationToken>())
|
||||
_mockBookRepository.GetByIdAsync(Guid.Parse(command.BookId!), cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<BookEntity?>(null));
|
||||
|
||||
// Act
|
||||
Result<string> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
Result<UpdateBookCoverResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.WrittenContent.BookNotFound, result.FirstError);
|
||||
await _mockLibraryRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
|
||||
await _mockLibraryRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>());
|
||||
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
@@ -274,14 +270,14 @@ public class UpdateBookCoverCommandHandlerTests
|
||||
{
|
||||
// Arrange
|
||||
UpdateBookCoverCommand command = _commandFixture.Create();
|
||||
BookEntity existingBook = _bookEntityFixture.Create(id: command.BookId);
|
||||
_mockBookRepository.GetByIdAsync(command.BookId, Arg.Any<CancellationToken>())
|
||||
BookEntity existingBook = _bookEntityFixture.Create(id: Guid.Parse(command.BookId!), libraryId: Guid.Parse(command.LibraryId!));
|
||||
_mockBookRepository.GetByIdAsync(Guid.Parse(command.BookId!), cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<BookEntity?>(existingBook));
|
||||
_mockAuthorizationService.EvaluatePolicyAsync<ILibraryOwnershipPolicy>(_userId, Arg.Any<LibraryOwnershipPolicyContext>(), Arg.Any<CancellationToken>())
|
||||
.Returns(false);
|
||||
|
||||
// Act
|
||||
Result<string> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
Result<UpdateBookCoverResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
@@ -295,14 +291,14 @@ public class UpdateBookCoverCommandHandlerTests
|
||||
{
|
||||
// Arrange
|
||||
UpdateBookCoverCommand command = _commandFixture.Create();
|
||||
BookEntity existingBook = _bookEntityFixture.Create(id: command.BookId);
|
||||
_mockBookRepository.GetByIdAsync(command.BookId, Arg.Any<CancellationToken>())
|
||||
BookEntity existingBook = _bookEntityFixture.Create(id: Guid.Parse(command.BookId!), libraryId: Guid.Parse(command.LibraryId!));
|
||||
_mockBookRepository.GetByIdAsync(Guid.Parse(command.BookId!), cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<BookEntity?>(existingBook));
|
||||
_mockLibraryRepository.GetByIdAsync(existingBook.LibraryId, Arg.Any<CancellationToken>())
|
||||
_mockLibraryRepository.GetByIdAsync(existingBook.LibraryId, cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<LibraryEntity?>(null));
|
||||
|
||||
// Act
|
||||
Result<string> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
Result<UpdateBookCoverResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
@@ -316,21 +312,106 @@ public class UpdateBookCoverCommandHandlerTests
|
||||
{
|
||||
// Arrange
|
||||
UpdateBookCoverCommand command = _commandFixture.Create();
|
||||
BookEntity existingBook = _bookEntityFixture.Create(id: command.BookId);
|
||||
BookEntity existingBook = _bookEntityFixture.Create(id: Guid.Parse(command.BookId!), libraryId: Guid.Parse(command.LibraryId!));
|
||||
LibraryEntity library = _libraryEntityFixture.Create(id: existingBook.LibraryId);
|
||||
_mockBookRepository.GetByIdAsync(command.BookId, Arg.Any<CancellationToken>())
|
||||
_mockBookRepository.GetByIdAsync(Guid.Parse(command.BookId!), cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<BookEntity?>(existingBook));
|
||||
_mockLibraryRepository.GetByIdAsync(existingBook.LibraryId, Arg.Any<CancellationToken>())
|
||||
_mockLibraryRepository.GetByIdAsync(existingBook.LibraryId, cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<LibraryEntity?>(library));
|
||||
_mockBookArtworkService.SaveBookArtworkAsync(Arg.Any<Guid>(), Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<System.IO.Stream>(), Arg.Any<string>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Errors.FileSystemManagement.FileTooLarge);
|
||||
|
||||
// Act
|
||||
Result<string> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
Result<UpdateBookCoverResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.FileTooLarge, result.FirstError);
|
||||
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenBookBelongsToAnotherLibrary_ShouldReturnBookNotFoundError()
|
||||
{
|
||||
// Arrange
|
||||
UpdateBookCoverCommand command = _commandFixture.Create();
|
||||
// The stored book belongs to a different library than the one named by the route.
|
||||
BookEntity existingBook = _bookEntityFixture.Create(id: Guid.Parse(command.BookId!), libraryId: Guid.NewGuid());
|
||||
_mockBookRepository.GetByIdAsync(Guid.Parse(command.BookId!), cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<BookEntity?>(existingBook));
|
||||
|
||||
// Act
|
||||
Result<UpdateBookCoverResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.WrittenContent.BookNotFound, result.FirstError);
|
||||
await _mockAuthorizationService.DidNotReceive().EvaluatePolicyAsync<ILibraryOwnershipPolicy>(Arg.Any<Guid>(), Arg.Any<LibraryOwnershipPolicyContext>(), Arg.Any<CancellationToken>());
|
||||
await _mockBookArtworkService.DidNotReceive().SaveBookArtworkAsync(Arg.Any<Guid>(), Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<System.IO.Stream>(), Arg.Any<string>(), Arg.Any<CancellationToken>());
|
||||
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenBookRepositoryReturnsError_ShouldReturnFailureResult()
|
||||
{
|
||||
// Arrange
|
||||
UpdateBookCoverCommand command = _commandFixture.Create();
|
||||
_mockBookRepository.GetByIdAsync(Guid.Parse(command.BookId!), cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Errors.Library.LibraryIdCannotBeEmpty);
|
||||
|
||||
// Act
|
||||
Result<UpdateBookCoverResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Library.LibraryIdCannotBeEmpty, result.FirstError);
|
||||
await _mockLibraryRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>());
|
||||
await _mockBookArtworkService.DidNotReceive().SaveBookArtworkAsync(Arg.Any<Guid>(), Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<System.IO.Stream>(), Arg.Any<string>(), Arg.Any<CancellationToken>());
|
||||
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenLibraryRepositoryReturnsError_ShouldReturnFailureResult()
|
||||
{
|
||||
// Arrange
|
||||
UpdateBookCoverCommand command = _commandFixture.Create();
|
||||
BookEntity existingBook = _bookEntityFixture.Create(id: Guid.Parse(command.BookId!), libraryId: Guid.Parse(command.LibraryId!));
|
||||
_mockBookRepository.GetByIdAsync(Guid.Parse(command.BookId!), cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<BookEntity?>(existingBook));
|
||||
_mockLibraryRepository.GetByIdAsync(existingBook.LibraryId, cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Errors.Library.LibraryIdCannotBeEmpty);
|
||||
|
||||
// Act
|
||||
Result<UpdateBookCoverResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Library.LibraryIdCannotBeEmpty, result.FirstError);
|
||||
await _mockBookArtworkService.DidNotReceive().SaveBookArtworkAsync(Arg.Any<Guid>(), Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<System.IO.Stream>(), Arg.Any<string>(), Arg.Any<CancellationToken>());
|
||||
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenSaveChangesFails_ShouldReturnFailureResultWithoutReturningTheCoverPath()
|
||||
{
|
||||
// Arrange
|
||||
UpdateBookCoverCommand command = _commandFixture.Create();
|
||||
BookEntity existingBook = _bookEntityFixture.Create(id: Guid.Parse(command.BookId!), libraryId: Guid.Parse(command.LibraryId!));
|
||||
LibraryEntity library = _libraryEntityFixture.Create(id: existingBook.LibraryId);
|
||||
_mockBookRepository.GetByIdAsync(Guid.Parse(command.BookId!), cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<BookEntity?>(existingBook));
|
||||
_mockLibraryRepository.GetByIdAsync(existingBook.LibraryId, cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<LibraryEntity?>(library));
|
||||
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>())
|
||||
.Returns(ApplicationErrors.Persistence.UniqueConstraintViolation);
|
||||
|
||||
// Act
|
||||
Result<UpdateBookCoverResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(ApplicationErrors.Persistence.UniqueConstraintViolation, result.FirstError);
|
||||
await _mockBookArtworkService.Received(1).SaveBookArtworkAsync(Arg.Any<Guid>(), Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<System.IO.Stream>(), Arg.Any<string>(), Arg.Any<CancellationToken>());
|
||||
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
}
|
||||
+92
-1
@@ -33,11 +33,50 @@ public class UpdateBookCoverCommandValidatorTests
|
||||
Assert.Empty(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenLibraryIdIsEmpty_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
UpdateBookCoverCommand command = _commandFixture.Create(libraryId: Guid.Empty.ToString());
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(command);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.Library.LibraryIdCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenLibraryIdIsNotAGuid_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
UpdateBookCoverCommand command = _commandFixture.Create(libraryId: "not-a-guid");
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(command);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.Library.LibraryIdCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenLibraryIdIsValid_ShouldNotHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
UpdateBookCoverCommand command = _commandFixture.Create(libraryId: Guid.NewGuid().ToString());
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(command);
|
||||
|
||||
// Assert
|
||||
result.ShouldNotHaveValidationError(Errors.Library.LibraryIdCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenBookIdIsEmpty_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
UpdateBookCoverCommand command = _commandFixture.Create(bookId: Guid.Empty);
|
||||
UpdateBookCoverCommand command = _commandFixture.Create(bookId: Guid.Empty.ToString());
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(command);
|
||||
@@ -46,6 +85,32 @@ public class UpdateBookCoverCommandValidatorTests
|
||||
result.ShouldHaveValidationError(Errors.WrittenContent.BookIdCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenBookIdIsNotAGuid_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
UpdateBookCoverCommand command = _commandFixture.Create(bookId: "not-a-guid");
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(command);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.WrittenContent.BookIdCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenBookIdIsValid_ShouldNotHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
UpdateBookCoverCommand command = _commandFixture.Create(bookId: Guid.NewGuid().ToString());
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(command);
|
||||
|
||||
// Assert
|
||||
result.ShouldNotHaveValidationError(Errors.WrittenContent.BookIdCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenCoverIsNull_ShouldHaveValidationError()
|
||||
{
|
||||
@@ -58,4 +123,30 @@ public class UpdateBookCoverCommandValidatorTests
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.WrittenContent.BookCoverCannotBeNull);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenCoverIsPresentAndFileNameIsEmpty_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
UpdateBookCoverCommand command = _commandFixture.Create(fileName: string.Empty);
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(command);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.FileSystemManagement.FileNameCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenCoverIsMissingAndFileNameIsEmpty_ShouldNotHaveFileNameValidationError()
|
||||
{
|
||||
// Arrange
|
||||
UpdateBookCoverCommand command = _commandFixture.Create(fileName: string.Empty, includeCover: false);
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(command);
|
||||
|
||||
// Assert
|
||||
result.ShouldNotHaveValidationError(Errors.FileSystemManagement.FileNameCannotBeEmpty);
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Books;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.BookLibrary;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Common.Infrastructure.Authentication;
|
||||
using Lumina.Application.Common.Infrastructure.Authorization;
|
||||
@@ -94,7 +94,7 @@ public class GetBookQueryHandlerTests
|
||||
Guid bookId = Guid.Parse(query.BookId!);
|
||||
BookEntity bookEntity = _bookEntityFixture.Create(id: bookId, libraryId: libraryId);
|
||||
Guid contributorId = Guid.NewGuid();
|
||||
bookEntity.BookContributors =
|
||||
bookEntity.Contributors =
|
||||
[
|
||||
_bookContributorEntityFixture.Create(bookId: bookId, mediaContributorId: contributorId, role: MediaContributorRole.Author)
|
||||
];
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Books;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.BookLibrary;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Common.DTO.Filtering;
|
||||
using Lumina.Application.Common.DTO.Pagination;
|
||||
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Books;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.BookLibrary;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Common.DTO.Filtering;
|
||||
using Lumina.Application.Common.DTO.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
|
||||
+64
-18
@@ -1,7 +1,7 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management;
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Books;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.BookLibrary;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.MediaLibrary;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Common.Infrastructure.Authentication;
|
||||
@@ -57,6 +57,7 @@ public class GetReadingAvailabilityQueryHandlerTests
|
||||
_mockBookRepository = Substitute.For<IBookRepository>();
|
||||
_mockLibraryRepository = Substitute.For<ILibraryRepository>();
|
||||
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
|
||||
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
|
||||
_mockUnitOfWork.BookRepository.Returns(_mockBookRepository);
|
||||
_mockUnitOfWork.LibraryRepository.Returns(_mockLibraryRepository);
|
||||
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
|
||||
@@ -79,12 +80,14 @@ public class GetReadingAvailabilityQueryHandlerTests
|
||||
{
|
||||
// Arrange
|
||||
GetReadingAvailabilityQuery query = _getReadingAvailabilityQueryFixture.Create();
|
||||
Guid libraryId = Guid.Parse(query.LibraryId!);
|
||||
Guid bookId = Guid.Parse(query.BookId!);
|
||||
CancellationToken cancellationToken = CancellationToken.None;
|
||||
BookEntity book = _bookEntityFixture.Create(id: query.BookId);
|
||||
BookEntity book = _bookEntityFixture.Create(id: bookId, libraryId: libraryId);
|
||||
LibraryEntity library = _libraryEntityFixture.Create(id: book.LibraryId, libraryType: LibraryType.EBook);
|
||||
ReadingAvailabilityResponse expectedResponse = _readingAvailabilityResponseFixture.Create(bookId: book.Id, libraryId: book.LibraryId, isAvailable: true);
|
||||
_mockBookRepository.GetByIdAsync(query.BookId, cancellationToken).Returns(Result.From<BookEntity?>(book));
|
||||
_mockLibraryRepository.GetByIdAsync(book.LibraryId, cancellationToken).Returns(Result.From<LibraryEntity?>(library));
|
||||
_mockBookRepository.GetByIdAsync(bookId, shouldTrackEntities: false, cancellationToken: cancellationToken).Returns(Result.From<BookEntity?>(book));
|
||||
_mockLibraryRepository.GetByIdAsync(book.LibraryId, shouldTrackEntities: false, cancellationToken: cancellationToken).Returns(Result.From<LibraryEntity?>(library));
|
||||
_mockBookReadingService.GetAvailabilityAsync(book.Id, book.LibraryId, book.Path, library.LibraryType, cancellationToken).Returns(expectedResponse);
|
||||
|
||||
// Act
|
||||
@@ -100,12 +103,14 @@ public class GetReadingAvailabilityQueryHandlerTests
|
||||
{
|
||||
// Arrange
|
||||
GetReadingAvailabilityQuery query = _getReadingAvailabilityQueryFixture.Create();
|
||||
Guid libraryId = Guid.Parse(query.LibraryId!);
|
||||
Guid bookId = Guid.Parse(query.BookId!);
|
||||
CancellationToken cancellationToken = CancellationToken.None;
|
||||
BookEntity book = _bookEntityFixture.Create(id: query.BookId);
|
||||
BookEntity book = _bookEntityFixture.Create(id: bookId, libraryId: libraryId);
|
||||
LibraryEntity library = _libraryEntityFixture.Create(id: book.LibraryId, libraryType: LibraryType.EBook);
|
||||
ReadingAvailabilityResponse expectedResponse = _readingAvailabilityResponseFixture.Create(bookId: book.Id, libraryId: book.LibraryId, isAvailable: false, errorCode: nameof(Errors.Reading.ReaderDisabled));
|
||||
_mockBookRepository.GetByIdAsync(query.BookId, cancellationToken).Returns(Result.From<BookEntity?>(book));
|
||||
_mockLibraryRepository.GetByIdAsync(book.LibraryId, cancellationToken).Returns(Result.From<LibraryEntity?>(library));
|
||||
_mockBookRepository.GetByIdAsync(bookId, shouldTrackEntities: false, cancellationToken: cancellationToken).Returns(Result.From<BookEntity?>(book));
|
||||
_mockLibraryRepository.GetByIdAsync(book.LibraryId, shouldTrackEntities: false, cancellationToken: cancellationToken).Returns(Result.From<LibraryEntity?>(library));
|
||||
_mockBookReadingService.GetAvailabilityAsync(book.Id, book.LibraryId, book.Path, library.LibraryType, cancellationToken).Returns(expectedResponse);
|
||||
|
||||
// Act
|
||||
@@ -122,7 +127,8 @@ public class GetReadingAvailabilityQueryHandlerTests
|
||||
{
|
||||
// Arrange
|
||||
GetReadingAvailabilityQuery query = _getReadingAvailabilityQueryFixture.Create();
|
||||
_mockBookRepository.GetByIdAsync(query.BookId, Arg.Any<CancellationToken>()).Returns(Result.From<BookEntity?>(null));
|
||||
Guid bookId = Guid.Parse(query.BookId!);
|
||||
_mockBookRepository.GetByIdAsync(bookId, shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>()).Returns(Result.From<BookEntity?>(null));
|
||||
|
||||
// Act
|
||||
Result<ReadingAvailabilityResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
@@ -133,14 +139,35 @@ public class GetReadingAvailabilityQueryHandlerTests
|
||||
await _mockBookReadingService.DidNotReceive().GetAvailabilityAsync(Arg.Any<Guid>(), Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<LibraryType>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenBookBelongsToAnotherLibrary_ShouldReturnBookNotFoundError()
|
||||
{
|
||||
// Arrange
|
||||
GetReadingAvailabilityQuery query = _getReadingAvailabilityQueryFixture.Create();
|
||||
Guid bookId = Guid.Parse(query.BookId!);
|
||||
BookEntity book = _bookEntityFixture.Create(id: bookId, libraryId: Guid.NewGuid());
|
||||
_mockBookRepository.GetByIdAsync(bookId, shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>()).Returns(Result.From<BookEntity?>(book));
|
||||
|
||||
// Act
|
||||
Result<ReadingAvailabilityResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(DomainErrors.Reading.BookNotFound, result.FirstError);
|
||||
await _mockLibraryRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>());
|
||||
await _mockBookReadingService.DidNotReceive().GetAvailabilityAsync(Arg.Any<Guid>(), Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<LibraryType>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenLibraryDoesNotExist_ShouldReturnLibraryNotFoundError()
|
||||
{
|
||||
// Arrange
|
||||
GetReadingAvailabilityQuery query = _getReadingAvailabilityQueryFixture.Create();
|
||||
BookEntity book = _bookEntityFixture.Create(id: query.BookId);
|
||||
_mockBookRepository.GetByIdAsync(query.BookId, Arg.Any<CancellationToken>()).Returns(Result.From<BookEntity?>(book));
|
||||
_mockLibraryRepository.GetByIdAsync(book.LibraryId, Arg.Any<CancellationToken>()).Returns(Result.From<LibraryEntity?>(null));
|
||||
Guid libraryId = Guid.Parse(query.LibraryId!);
|
||||
Guid bookId = Guid.Parse(query.BookId!);
|
||||
BookEntity book = _bookEntityFixture.Create(id: bookId, libraryId: libraryId);
|
||||
_mockBookRepository.GetByIdAsync(bookId, shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>()).Returns(Result.From<BookEntity?>(book));
|
||||
_mockLibraryRepository.GetByIdAsync(book.LibraryId, shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>()).Returns(Result.From<LibraryEntity?>(null));
|
||||
|
||||
// Act
|
||||
Result<ReadingAvailabilityResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
@@ -156,8 +183,10 @@ public class GetReadingAvailabilityQueryHandlerTests
|
||||
{
|
||||
// Arrange
|
||||
GetReadingAvailabilityQuery query = _getReadingAvailabilityQueryFixture.Create();
|
||||
BookEntity book = _bookEntityFixture.Create(id: query.BookId);
|
||||
_mockBookRepository.GetByIdAsync(query.BookId, Arg.Any<CancellationToken>()).Returns(Result.From<BookEntity?>(book));
|
||||
Guid libraryId = Guid.Parse(query.LibraryId!);
|
||||
Guid bookId = Guid.Parse(query.BookId!);
|
||||
BookEntity book = _bookEntityFixture.Create(id: bookId, libraryId: libraryId);
|
||||
_mockBookRepository.GetByIdAsync(bookId, shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>()).Returns(Result.From<BookEntity?>(book));
|
||||
_mockAuthorizationService.EvaluatePolicyAsync<ILibraryOwnershipPolicy>(_userId, Arg.Any<LibraryOwnershipPolicyContext>(), Arg.Any<CancellationToken>())
|
||||
.Returns(false);
|
||||
|
||||
@@ -167,7 +196,7 @@ public class GetReadingAvailabilityQueryHandlerTests
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(ApplicationErrors.Authorization.NotAuthorized, result.FirstError);
|
||||
await _mockLibraryRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
|
||||
await _mockLibraryRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>());
|
||||
await _mockBookReadingService.DidNotReceive().GetAvailabilityAsync(Arg.Any<Guid>(), Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<LibraryType>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
@@ -185,7 +214,24 @@ public class GetReadingAvailabilityQueryHandlerTests
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(ApplicationErrors.Authorization.NotAuthorized, result.FirstError);
|
||||
await _mockAuthorizationService.DidNotReceive().EvaluatePolicyAsync<ILibraryOwnershipPolicy>(Arg.Any<Guid>(), Arg.Any<LibraryOwnershipPolicyContext>(), Arg.Any<CancellationToken>());
|
||||
await _mockBookRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
|
||||
await _mockBookRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenRepositoryReturnsError_ShouldReturnFailureResult()
|
||||
{
|
||||
// Arrange
|
||||
GetReadingAvailabilityQuery query = _getReadingAvailabilityQueryFixture.Create();
|
||||
Guid bookId = Guid.Parse(query.BookId!);
|
||||
_mockBookRepository.GetByIdAsync(bookId, shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Errors.Library.LibraryIdCannotBeEmpty);
|
||||
|
||||
// Act
|
||||
Result<ReadingAvailabilityResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Library.LibraryIdCannotBeEmpty, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -193,16 +239,16 @@ public class GetReadingAvailabilityQueryHandlerTests
|
||||
{
|
||||
// Arrange
|
||||
GetReadingAvailabilityQuery query = _getReadingAvailabilityQueryFixture.Create();
|
||||
_mockValidator.Validate(Arg.Any<GetReadingAvailabilityQuery>()).Returns([Errors.Reading.BookIdCannotBeEmpty]);
|
||||
_mockValidator.Validate(Arg.Any<GetReadingAvailabilityQuery>()).Returns([Errors.WrittenContent.BookIdCannotBeEmpty]);
|
||||
|
||||
// Act
|
||||
Result<ReadingAvailabilityResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Reading.BookIdCannotBeEmpty, result.FirstError);
|
||||
Assert.Equal(Errors.WrittenContent.BookIdCannotBeEmpty, result.FirstError);
|
||||
await _mockAuthorizationService.DidNotReceive().EvaluatePolicyAsync<ILibraryOwnershipPolicy>(Arg.Any<Guid>(), Arg.Any<LibraryOwnershipPolicyContext>(), Arg.Any<CancellationToken>());
|
||||
await _mockBookRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
|
||||
await _mockBookRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>());
|
||||
await _mockBookReadingService.DidNotReceive().GetAvailabilityAsync(Arg.Any<Guid>(), Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<LibraryType>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
}
|
||||
+37
-16
@@ -4,7 +4,6 @@ using Lumina.Application.Fixtures.Core.MediaLibrary.WrittenContentLibrary.BookLi
|
||||
using Lumina.Application.UnitTests.Common.Setup;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
@@ -21,20 +20,7 @@ public class GetReadingAvailabilityQueryValidatorTests
|
||||
private readonly GetReadingAvailabilityQueryFixture _getReadingAvailabilityQueryFixture = new();
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenBookIdIsEmpty_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
GetReadingAvailabilityQuery query = _getReadingAvailabilityQueryFixture.Create(bookId: Guid.Empty);
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.Reading.BookIdCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenBookIdIsValid_ShouldNotHaveValidationError()
|
||||
public void Validate_WhenIdsAreValid_ShouldNotHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
GetReadingAvailabilityQuery query = _getReadingAvailabilityQueryFixture.Create();
|
||||
@@ -43,6 +29,41 @@ public class GetReadingAvailabilityQueryValidatorTests
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldNotHaveValidationError(Errors.Reading.BookIdCannotBeEmpty);
|
||||
result.ShouldNotHaveValidationError(Errors.Library.LibraryIdCannotBeEmpty);
|
||||
result.ShouldNotHaveValidationError(Errors.WrittenContent.BookIdCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)] // missing route value
|
||||
[InlineData("")] // empty route value
|
||||
[InlineData("not-a-library-guid")] // non-Guid route value
|
||||
[InlineData("00000000-0000-0000-0000-000000000000")] // empty Guid route value
|
||||
public void Validate_WhenLibraryIdIsEmptyOrInvalid_ShouldHaveValidationError(string? libraryId)
|
||||
{
|
||||
// Arrange
|
||||
GetReadingAvailabilityQuery query = _getReadingAvailabilityQueryFixture.Create() with { LibraryId = libraryId };
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.Library.LibraryIdCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)] // missing route value
|
||||
[InlineData("")] // empty route value
|
||||
[InlineData("not-a-guid")] // non-Guid route value
|
||||
[InlineData("00000000-0000-0000-0000-000000000000")] // empty Guid route value
|
||||
public void Validate_WhenBookIdIsEmptyOrInvalid_ShouldHaveValidationError(string? bookId)
|
||||
{
|
||||
// Arrange
|
||||
GetReadingAvailabilityQuery query = _getReadingAvailabilityQueryFixture.Create() with { BookId = bookId };
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.WrittenContent.BookIdCannotBeEmpty);
|
||||
}
|
||||
}
|
||||
+64
-18
@@ -2,7 +2,7 @@
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management;
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Books;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.BookLibrary;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.MediaLibrary;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Users;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
@@ -63,6 +63,7 @@ public class GetReadingManifestQueryHandlerTests
|
||||
_mockLibraryRepository = Substitute.For<ILibraryRepository>();
|
||||
_mockUserSettingsRepository = Substitute.For<IUserSettingsRepository>();
|
||||
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
|
||||
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
|
||||
_mockUnitOfWork.BookRepository.Returns(_mockBookRepository);
|
||||
_mockUnitOfWork.LibraryRepository.Returns(_mockLibraryRepository);
|
||||
_mockUnitOfWork.UserSettingsRepository.Returns(_mockUserSettingsRepository);
|
||||
@@ -87,12 +88,14 @@ public class GetReadingManifestQueryHandlerTests
|
||||
{
|
||||
// Arrange
|
||||
GetReadingManifestQuery query = _getReadingManifestQueryFixture.Create();
|
||||
Guid libraryId = Guid.Parse(query.LibraryId!);
|
||||
Guid bookId = Guid.Parse(query.BookId!);
|
||||
CancellationToken cancellationToken = CancellationToken.None;
|
||||
BookEntity book = _bookEntityFixture.Create(id: query.BookId);
|
||||
BookEntity book = _bookEntityFixture.Create(id: bookId, libraryId: libraryId);
|
||||
LibraryEntity library = _libraryEntityFixture.Create(id: book.LibraryId, libraryType: LibraryType.EBook);
|
||||
ReadingManifestResponse expectedResponse = _readingManifestResponseFixture.Create();
|
||||
_mockBookRepository.GetByIdAsync(query.BookId, cancellationToken).Returns(Result.From<BookEntity?>(book));
|
||||
_mockLibraryRepository.GetByIdAsync(book.LibraryId, cancellationToken).Returns(Result.From<LibraryEntity?>(library));
|
||||
_mockBookRepository.GetByIdAsync(bookId, shouldTrackEntities: false, cancellationToken: cancellationToken).Returns(Result.From<BookEntity?>(book));
|
||||
_mockLibraryRepository.GetByIdAsync(book.LibraryId, shouldTrackEntities: false, cancellationToken: cancellationToken).Returns(Result.From<LibraryEntity?>(library));
|
||||
_mockBookReadingService.GetManifestAsync(book.Id, book.LibraryId, book.Path, library.LibraryType, shouldRenderPdfAsImages: false, cancellationToken).Returns(expectedResponse);
|
||||
|
||||
// Act
|
||||
@@ -108,14 +111,16 @@ public class GetReadingManifestQueryHandlerTests
|
||||
{
|
||||
// Arrange
|
||||
GetReadingManifestQuery query = _getReadingManifestQueryFixture.Create();
|
||||
Guid libraryId = Guid.Parse(query.LibraryId!);
|
||||
Guid bookId = Guid.Parse(query.BookId!);
|
||||
CancellationToken cancellationToken = CancellationToken.None;
|
||||
BookEntity book = _bookEntityFixture.Create(id: query.BookId);
|
||||
BookEntity book = _bookEntityFixture.Create(id: bookId, libraryId: libraryId);
|
||||
LibraryEntity library = _libraryEntityFixture.Create(id: book.LibraryId, libraryType: LibraryType.EBook);
|
||||
UserSettingsEntity settings = _userSettingsEntityFixture.Create(userId: _userId, shouldRenderPdfAsImages: true);
|
||||
ReadingManifestResponse expectedResponse = _readingManifestResponseFixture.Create();
|
||||
_mockUserSettingsRepository.GetByUserIdAsync(_userId, cancellationToken).Returns(Result.From<UserSettingsEntity?>(settings));
|
||||
_mockBookRepository.GetByIdAsync(query.BookId, cancellationToken).Returns(Result.From<BookEntity?>(book));
|
||||
_mockLibraryRepository.GetByIdAsync(book.LibraryId, cancellationToken).Returns(Result.From<LibraryEntity?>(library));
|
||||
_mockBookRepository.GetByIdAsync(bookId, shouldTrackEntities: false, cancellationToken: cancellationToken).Returns(Result.From<BookEntity?>(book));
|
||||
_mockLibraryRepository.GetByIdAsync(book.LibraryId, shouldTrackEntities: false, cancellationToken: cancellationToken).Returns(Result.From<LibraryEntity?>(library));
|
||||
_mockBookReadingService.GetManifestAsync(book.Id, book.LibraryId, book.Path, library.LibraryType, shouldRenderPdfAsImages: true, cancellationToken).Returns(expectedResponse);
|
||||
|
||||
// Act
|
||||
@@ -131,7 +136,8 @@ public class GetReadingManifestQueryHandlerTests
|
||||
{
|
||||
// Arrange
|
||||
GetReadingManifestQuery query = _getReadingManifestQueryFixture.Create();
|
||||
_mockBookRepository.GetByIdAsync(query.BookId, Arg.Any<CancellationToken>()).Returns(Result.From<BookEntity?>(null));
|
||||
Guid bookId = Guid.Parse(query.BookId!);
|
||||
_mockBookRepository.GetByIdAsync(bookId, shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>()).Returns(Result.From<BookEntity?>(null));
|
||||
|
||||
// Act
|
||||
Result<ReadingManifestResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
@@ -142,14 +148,35 @@ public class GetReadingManifestQueryHandlerTests
|
||||
await _mockBookReadingService.DidNotReceive().GetManifestAsync(Arg.Any<Guid>(), Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<LibraryType>(), Arg.Any<bool>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenBookBelongsToAnotherLibrary_ShouldReturnBookNotFoundError()
|
||||
{
|
||||
// Arrange
|
||||
GetReadingManifestQuery query = _getReadingManifestQueryFixture.Create();
|
||||
Guid bookId = Guid.Parse(query.BookId!);
|
||||
BookEntity book = _bookEntityFixture.Create(id: bookId, libraryId: Guid.NewGuid());
|
||||
_mockBookRepository.GetByIdAsync(bookId, shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>()).Returns(Result.From<BookEntity?>(book));
|
||||
|
||||
// Act
|
||||
Result<ReadingManifestResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(DomainErrors.Reading.BookNotFound, result.FirstError);
|
||||
await _mockLibraryRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>());
|
||||
await _mockBookReadingService.DidNotReceive().GetManifestAsync(Arg.Any<Guid>(), Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<LibraryType>(), Arg.Any<bool>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenLibraryDoesNotExist_ShouldReturnLibraryNotFoundError()
|
||||
{
|
||||
// Arrange
|
||||
GetReadingManifestQuery query = _getReadingManifestQueryFixture.Create();
|
||||
BookEntity book = _bookEntityFixture.Create(id: query.BookId);
|
||||
_mockBookRepository.GetByIdAsync(query.BookId, Arg.Any<CancellationToken>()).Returns(Result.From<BookEntity?>(book));
|
||||
_mockLibraryRepository.GetByIdAsync(book.LibraryId, Arg.Any<CancellationToken>()).Returns(Result.From<LibraryEntity?>(null));
|
||||
Guid libraryId = Guid.Parse(query.LibraryId!);
|
||||
Guid bookId = Guid.Parse(query.BookId!);
|
||||
BookEntity book = _bookEntityFixture.Create(id: bookId, libraryId: libraryId);
|
||||
_mockBookRepository.GetByIdAsync(bookId, shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>()).Returns(Result.From<BookEntity?>(book));
|
||||
_mockLibraryRepository.GetByIdAsync(book.LibraryId, shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>()).Returns(Result.From<LibraryEntity?>(null));
|
||||
|
||||
// Act
|
||||
Result<ReadingManifestResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
@@ -165,8 +192,10 @@ public class GetReadingManifestQueryHandlerTests
|
||||
{
|
||||
// Arrange
|
||||
GetReadingManifestQuery query = _getReadingManifestQueryFixture.Create();
|
||||
BookEntity book = _bookEntityFixture.Create(id: query.BookId);
|
||||
_mockBookRepository.GetByIdAsync(query.BookId, Arg.Any<CancellationToken>()).Returns(Result.From<BookEntity?>(book));
|
||||
Guid libraryId = Guid.Parse(query.LibraryId!);
|
||||
Guid bookId = Guid.Parse(query.BookId!);
|
||||
BookEntity book = _bookEntityFixture.Create(id: bookId, libraryId: libraryId);
|
||||
_mockBookRepository.GetByIdAsync(bookId, shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>()).Returns(Result.From<BookEntity?>(book));
|
||||
_mockAuthorizationService.EvaluatePolicyAsync<ILibraryOwnershipPolicy>(_userId, Arg.Any<LibraryOwnershipPolicyContext>(), Arg.Any<CancellationToken>())
|
||||
.Returns(false);
|
||||
|
||||
@@ -176,7 +205,7 @@ public class GetReadingManifestQueryHandlerTests
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(ApplicationErrors.Authorization.NotAuthorized, result.FirstError);
|
||||
await _mockLibraryRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
|
||||
await _mockLibraryRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>());
|
||||
await _mockBookReadingService.DidNotReceive().GetManifestAsync(Arg.Any<Guid>(), Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<LibraryType>(), Arg.Any<bool>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
@@ -194,7 +223,24 @@ public class GetReadingManifestQueryHandlerTests
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(ApplicationErrors.Authorization.NotAuthorized, result.FirstError);
|
||||
await _mockAuthorizationService.DidNotReceive().EvaluatePolicyAsync<ILibraryOwnershipPolicy>(Arg.Any<Guid>(), Arg.Any<LibraryOwnershipPolicyContext>(), Arg.Any<CancellationToken>());
|
||||
await _mockBookRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
|
||||
await _mockBookRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenRepositoryReturnsError_ShouldReturnFailureResult()
|
||||
{
|
||||
// Arrange
|
||||
GetReadingManifestQuery query = _getReadingManifestQueryFixture.Create();
|
||||
Guid bookId = Guid.Parse(query.BookId!);
|
||||
_mockBookRepository.GetByIdAsync(bookId, shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Errors.Library.LibraryIdCannotBeEmpty);
|
||||
|
||||
// Act
|
||||
Result<ReadingManifestResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Library.LibraryIdCannotBeEmpty, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -202,16 +248,16 @@ public class GetReadingManifestQueryHandlerTests
|
||||
{
|
||||
// Arrange
|
||||
GetReadingManifestQuery query = _getReadingManifestQueryFixture.Create();
|
||||
_mockValidator.Validate(Arg.Any<GetReadingManifestQuery>()).Returns([Errors.Reading.BookIdCannotBeEmpty]);
|
||||
_mockValidator.Validate(Arg.Any<GetReadingManifestQuery>()).Returns([Errors.WrittenContent.BookIdCannotBeEmpty]);
|
||||
|
||||
// Act
|
||||
Result<ReadingManifestResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Reading.BookIdCannotBeEmpty, result.FirstError);
|
||||
Assert.Equal(Errors.WrittenContent.BookIdCannotBeEmpty, result.FirstError);
|
||||
await _mockAuthorizationService.DidNotReceive().EvaluatePolicyAsync<ILibraryOwnershipPolicy>(Arg.Any<Guid>(), Arg.Any<LibraryOwnershipPolicyContext>(), Arg.Any<CancellationToken>());
|
||||
await _mockBookRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
|
||||
await _mockBookRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>());
|
||||
await _mockBookReadingService.DidNotReceive().GetManifestAsync(Arg.Any<Guid>(), Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<LibraryType>(), Arg.Any<bool>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
}
|
||||
+37
-16
@@ -4,7 +4,6 @@ using Lumina.Application.Fixtures.Core.MediaLibrary.WrittenContentLibrary.BookLi
|
||||
using Lumina.Application.UnitTests.Common.Setup;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
@@ -21,20 +20,7 @@ public class GetReadingManifestQueryValidatorTests
|
||||
private readonly GetReadingManifestQueryFixture _getReadingManifestQueryFixture = new();
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenBookIdIsEmpty_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
GetReadingManifestQuery query = _getReadingManifestQueryFixture.Create(bookId: Guid.Empty);
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.Reading.BookIdCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenBookIdIsValid_ShouldNotHaveValidationError()
|
||||
public void Validate_WhenIdsAreValid_ShouldNotHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
GetReadingManifestQuery query = _getReadingManifestQueryFixture.Create();
|
||||
@@ -43,6 +29,41 @@ public class GetReadingManifestQueryValidatorTests
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldNotHaveValidationError(Errors.Reading.BookIdCannotBeEmpty);
|
||||
result.ShouldNotHaveValidationError(Errors.Library.LibraryIdCannotBeEmpty);
|
||||
result.ShouldNotHaveValidationError(Errors.WrittenContent.BookIdCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)] // missing route value
|
||||
[InlineData("")] // empty route value
|
||||
[InlineData("not-a-library-guid")] // non-Guid route value
|
||||
[InlineData("00000000-0000-0000-0000-000000000000")] // empty Guid route value
|
||||
public void Validate_WhenLibraryIdIsEmptyOrInvalid_ShouldHaveValidationError(string? libraryId)
|
||||
{
|
||||
// Arrange
|
||||
GetReadingManifestQuery query = _getReadingManifestQueryFixture.Create() with { LibraryId = libraryId };
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.Library.LibraryIdCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)] // missing route value
|
||||
[InlineData("")] // empty route value
|
||||
[InlineData("not-a-guid")] // non-Guid route value
|
||||
[InlineData("00000000-0000-0000-0000-000000000000")] // empty Guid route value
|
||||
public void Validate_WhenBookIdIsEmptyOrInvalid_ShouldHaveValidationError(string? bookId)
|
||||
{
|
||||
// Arrange
|
||||
GetReadingManifestQuery query = _getReadingManifestQueryFixture.Create() with { BookId = bookId };
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.WrittenContent.BookIdCannotBeEmpty);
|
||||
}
|
||||
}
|
||||
+61
-15
@@ -2,7 +2,7 @@
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management;
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Books;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.BookLibrary;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.MediaLibrary;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Users;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
@@ -63,6 +63,7 @@ public class GetReadingResourceQueryHandlerTests
|
||||
_mockLibraryRepository = Substitute.For<ILibraryRepository>();
|
||||
_mockUserSettingsRepository = Substitute.For<IUserSettingsRepository>();
|
||||
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
|
||||
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
|
||||
_mockUnitOfWork.BookRepository.Returns(_mockBookRepository);
|
||||
_mockUnitOfWork.LibraryRepository.Returns(_mockLibraryRepository);
|
||||
_mockUnitOfWork.UserSettingsRepository.Returns(_mockUserSettingsRepository);
|
||||
@@ -87,12 +88,14 @@ public class GetReadingResourceQueryHandlerTests
|
||||
{
|
||||
// Arrange
|
||||
GetReadingResourceQuery query = _getReadingResourceQueryFixture.Create();
|
||||
Guid libraryId = Guid.Parse(query.LibraryId!);
|
||||
Guid bookId = Guid.Parse(query.BookId!);
|
||||
CancellationToken cancellationToken = CancellationToken.None;
|
||||
BookEntity book = _bookEntityFixture.Create(id: query.BookId);
|
||||
BookEntity book = _bookEntityFixture.Create(id: bookId, libraryId: libraryId);
|
||||
LibraryEntity library = _libraryEntityFixture.Create(id: book.LibraryId, libraryType: LibraryType.EBook);
|
||||
ReadingResourceDataDto expectedResponse = _readingResourceDataDtoFixture.Create();
|
||||
_mockBookRepository.GetByIdAsync(query.BookId, cancellationToken).Returns(Result.From<BookEntity?>(book));
|
||||
_mockLibraryRepository.GetByIdAsync(book.LibraryId, cancellationToken).Returns(Result.From<LibraryEntity?>(library));
|
||||
_mockBookRepository.GetByIdAsync(bookId, shouldTrackEntities: false, cancellationToken: cancellationToken).Returns(Result.From<BookEntity?>(book));
|
||||
_mockLibraryRepository.GetByIdAsync(book.LibraryId, shouldTrackEntities: false, cancellationToken: cancellationToken).Returns(Result.From<LibraryEntity?>(library));
|
||||
_mockBookReadingService.GetResourceAsync(book.Id, book.LibraryId, book.Path, library.LibraryType, query.ResourceKey, shouldRenderPdfAsImages: false, cancellationToken).Returns(expectedResponse);
|
||||
|
||||
// Act
|
||||
@@ -108,14 +111,16 @@ public class GetReadingResourceQueryHandlerTests
|
||||
{
|
||||
// Arrange
|
||||
GetReadingResourceQuery query = _getReadingResourceQueryFixture.Create();
|
||||
Guid libraryId = Guid.Parse(query.LibraryId!);
|
||||
Guid bookId = Guid.Parse(query.BookId!);
|
||||
CancellationToken cancellationToken = CancellationToken.None;
|
||||
BookEntity book = _bookEntityFixture.Create(id: query.BookId);
|
||||
BookEntity book = _bookEntityFixture.Create(id: bookId, libraryId: libraryId);
|
||||
LibraryEntity library = _libraryEntityFixture.Create(id: book.LibraryId, libraryType: LibraryType.EBook);
|
||||
UserSettingsEntity settings = _userSettingsEntityFixture.Create(userId: _userId, shouldRenderPdfAsImages: true);
|
||||
ReadingResourceDataDto expectedResponse = _readingResourceDataDtoFixture.Create();
|
||||
_mockUserSettingsRepository.GetByUserIdAsync(_userId, cancellationToken).Returns(Result.From<UserSettingsEntity?>(settings));
|
||||
_mockBookRepository.GetByIdAsync(query.BookId, cancellationToken).Returns(Result.From<BookEntity?>(book));
|
||||
_mockLibraryRepository.GetByIdAsync(book.LibraryId, cancellationToken).Returns(Result.From<LibraryEntity?>(library));
|
||||
_mockBookRepository.GetByIdAsync(bookId, shouldTrackEntities: false, cancellationToken: cancellationToken).Returns(Result.From<BookEntity?>(book));
|
||||
_mockLibraryRepository.GetByIdAsync(book.LibraryId, shouldTrackEntities: false, cancellationToken: cancellationToken).Returns(Result.From<LibraryEntity?>(library));
|
||||
_mockBookReadingService.GetResourceAsync(book.Id, book.LibraryId, book.Path, library.LibraryType, query.ResourceKey, shouldRenderPdfAsImages: true, cancellationToken).Returns(expectedResponse);
|
||||
|
||||
// Act
|
||||
@@ -131,7 +136,8 @@ public class GetReadingResourceQueryHandlerTests
|
||||
{
|
||||
// Arrange
|
||||
GetReadingResourceQuery query = _getReadingResourceQueryFixture.Create();
|
||||
_mockBookRepository.GetByIdAsync(query.BookId, Arg.Any<CancellationToken>()).Returns(Result.From<BookEntity?>(null));
|
||||
Guid bookId = Guid.Parse(query.BookId!);
|
||||
_mockBookRepository.GetByIdAsync(bookId, shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>()).Returns(Result.From<BookEntity?>(null));
|
||||
|
||||
// Act
|
||||
Result<ReadingResourceDataDto> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
@@ -142,14 +148,35 @@ public class GetReadingResourceQueryHandlerTests
|
||||
await _mockBookReadingService.DidNotReceive().GetResourceAsync(Arg.Any<Guid>(), Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<LibraryType>(), Arg.Any<string>(), Arg.Any<bool>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenBookBelongsToAnotherLibrary_ShouldReturnBookNotFoundError()
|
||||
{
|
||||
// Arrange
|
||||
GetReadingResourceQuery query = _getReadingResourceQueryFixture.Create();
|
||||
Guid bookId = Guid.Parse(query.BookId!);
|
||||
BookEntity book = _bookEntityFixture.Create(id: bookId, libraryId: Guid.NewGuid());
|
||||
_mockBookRepository.GetByIdAsync(bookId, shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>()).Returns(Result.From<BookEntity?>(book));
|
||||
|
||||
// Act
|
||||
Result<ReadingResourceDataDto> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(DomainErrors.Reading.BookNotFound, result.FirstError);
|
||||
await _mockLibraryRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>());
|
||||
await _mockBookReadingService.DidNotReceive().GetResourceAsync(Arg.Any<Guid>(), Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<LibraryType>(), Arg.Any<string>(), Arg.Any<bool>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenLibraryDoesNotExist_ShouldReturnLibraryNotFoundError()
|
||||
{
|
||||
// Arrange
|
||||
GetReadingResourceQuery query = _getReadingResourceQueryFixture.Create();
|
||||
BookEntity book = _bookEntityFixture.Create(id: query.BookId);
|
||||
_mockBookRepository.GetByIdAsync(query.BookId, Arg.Any<CancellationToken>()).Returns(Result.From<BookEntity?>(book));
|
||||
_mockLibraryRepository.GetByIdAsync(book.LibraryId, Arg.Any<CancellationToken>()).Returns(Result.From<LibraryEntity?>(null));
|
||||
Guid libraryId = Guid.Parse(query.LibraryId!);
|
||||
Guid bookId = Guid.Parse(query.BookId!);
|
||||
BookEntity book = _bookEntityFixture.Create(id: bookId, libraryId: libraryId);
|
||||
_mockBookRepository.GetByIdAsync(bookId, shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>()).Returns(Result.From<BookEntity?>(book));
|
||||
_mockLibraryRepository.GetByIdAsync(book.LibraryId, shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>()).Returns(Result.From<LibraryEntity?>(null));
|
||||
|
||||
// Act
|
||||
Result<ReadingResourceDataDto> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
@@ -165,8 +192,10 @@ public class GetReadingResourceQueryHandlerTests
|
||||
{
|
||||
// Arrange
|
||||
GetReadingResourceQuery query = _getReadingResourceQueryFixture.Create();
|
||||
BookEntity book = _bookEntityFixture.Create(id: query.BookId);
|
||||
_mockBookRepository.GetByIdAsync(query.BookId, Arg.Any<CancellationToken>()).Returns(Result.From<BookEntity?>(book));
|
||||
Guid libraryId = Guid.Parse(query.LibraryId!);
|
||||
Guid bookId = Guid.Parse(query.BookId!);
|
||||
BookEntity book = _bookEntityFixture.Create(id: bookId, libraryId: libraryId);
|
||||
_mockBookRepository.GetByIdAsync(bookId, shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>()).Returns(Result.From<BookEntity?>(book));
|
||||
_mockAuthorizationService.EvaluatePolicyAsync<ILibraryOwnershipPolicy>(_userId, Arg.Any<LibraryOwnershipPolicyContext>(), Arg.Any<CancellationToken>())
|
||||
.Returns(false);
|
||||
|
||||
@@ -192,7 +221,24 @@ public class GetReadingResourceQueryHandlerTests
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(ApplicationErrors.Authorization.NotAuthorized, result.FirstError);
|
||||
await _mockBookRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
|
||||
await _mockBookRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenRepositoryReturnsError_ShouldReturnFailureResult()
|
||||
{
|
||||
// Arrange
|
||||
GetReadingResourceQuery query = _getReadingResourceQueryFixture.Create();
|
||||
Guid bookId = Guid.Parse(query.BookId!);
|
||||
_mockBookRepository.GetByIdAsync(bookId, shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Errors.Library.LibraryIdCannotBeEmpty);
|
||||
|
||||
// Act
|
||||
Result<ReadingResourceDataDto> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Library.LibraryIdCannotBeEmpty, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -208,7 +254,7 @@ public class GetReadingResourceQueryHandlerTests
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Reading.ResourceKeyCannotBeEmpty, result.FirstError);
|
||||
await _mockBookRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
|
||||
await _mockBookRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>());
|
||||
await _mockBookReadingService.DidNotReceive().GetResourceAsync(Arg.Any<Guid>(), Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<LibraryType>(), Arg.Any<string>(), Arg.Any<bool>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
}
|
||||
+51
-28
@@ -4,7 +4,6 @@ using Lumina.Application.Fixtures.Core.MediaLibrary.WrittenContentLibrary.BookLi
|
||||
using Lumina.Application.UnitTests.Common.Setup;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
@@ -20,32 +19,6 @@ public class GetReadingResourceQueryValidatorTests
|
||||
private readonly GetReadingResourceQueryValidator _validator = new();
|
||||
private readonly GetReadingResourceQueryFixture _getReadingResourceQueryFixture = new();
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenBookIdIsEmpty_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
GetReadingResourceQuery query = _getReadingResourceQueryFixture.Create(bookId: Guid.Empty);
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.Reading.BookIdCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenResourceKeyIsEmpty_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
GetReadingResourceQuery query = _getReadingResourceQueryFixture.Create(resourceKey: string.Empty);
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.Reading.ResourceKeyCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenQueryIsValid_ShouldNotHaveValidationError()
|
||||
{
|
||||
@@ -56,7 +29,57 @@ public class GetReadingResourceQueryValidatorTests
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldNotHaveValidationError(Errors.Reading.BookIdCannotBeEmpty);
|
||||
result.ShouldNotHaveValidationError(Errors.Library.LibraryIdCannotBeEmpty);
|
||||
result.ShouldNotHaveValidationError(Errors.WrittenContent.BookIdCannotBeEmpty);
|
||||
result.ShouldNotHaveValidationError(Errors.Reading.ResourceKeyCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)] // missing route value
|
||||
[InlineData("")] // empty route value
|
||||
[InlineData("not-a-library-guid")] // non-Guid route value
|
||||
[InlineData("00000000-0000-0000-0000-000000000000")] // empty Guid route value
|
||||
public void Validate_WhenLibraryIdIsEmptyOrInvalid_ShouldHaveValidationError(string? libraryId)
|
||||
{
|
||||
// Arrange
|
||||
GetReadingResourceQuery query = _getReadingResourceQueryFixture.Create() with { LibraryId = libraryId };
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.Library.LibraryIdCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)] // missing route value
|
||||
[InlineData("")] // empty route value
|
||||
[InlineData("not-a-guid")] // non-Guid route value
|
||||
[InlineData("00000000-0000-0000-0000-000000000000")] // empty Guid route value
|
||||
public void Validate_WhenBookIdIsEmptyOrInvalid_ShouldHaveValidationError(string? bookId)
|
||||
{
|
||||
// Arrange
|
||||
GetReadingResourceQuery query = _getReadingResourceQueryFixture.Create() with { BookId = bookId };
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.WrittenContent.BookIdCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)] // missing route value
|
||||
[InlineData("")] // empty route value
|
||||
public void Validate_WhenResourceKeyIsEmpty_ShouldHaveValidationError(string? resourceKey)
|
||||
{
|
||||
// Arrange
|
||||
GetReadingResourceQuery query = _getReadingResourceQueryFixture.Create() with { ResourceKey = resourceKey };
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.Reading.ResourceKeyCannotBeEmpty);
|
||||
}
|
||||
}
|
||||
+71
-21
@@ -2,7 +2,7 @@
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management;
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Books;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.BookLibrary;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.MediaLibrary;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Users;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
@@ -63,6 +63,7 @@ public class GetReadingSectionQueryHandlerTests
|
||||
_mockLibraryRepository = Substitute.For<ILibraryRepository>();
|
||||
_mockUserSettingsRepository = Substitute.For<IUserSettingsRepository>();
|
||||
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
|
||||
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
|
||||
_mockUnitOfWork.BookRepository.Returns(_mockBookRepository);
|
||||
_mockUnitOfWork.LibraryRepository.Returns(_mockLibraryRepository);
|
||||
_mockUnitOfWork.UserSettingsRepository.Returns(_mockUserSettingsRepository);
|
||||
@@ -87,12 +88,14 @@ public class GetReadingSectionQueryHandlerTests
|
||||
{
|
||||
// Arrange
|
||||
GetReadingSectionQuery query = _getReadingSectionQueryFixture.Create();
|
||||
Guid libraryId = Guid.Parse(query.LibraryId!);
|
||||
Guid bookId = Guid.Parse(query.BookId!);
|
||||
CancellationToken cancellationToken = CancellationToken.None;
|
||||
BookEntity book = _bookEntityFixture.Create(id: query.BookId);
|
||||
BookEntity book = _bookEntityFixture.Create(id: bookId, libraryId: libraryId);
|
||||
LibraryEntity library = _libraryEntityFixture.Create(id: book.LibraryId, libraryType: LibraryType.EBook);
|
||||
ReadingSectionDto expectedResponse = _readingSectionDtoFixture.Create(locationRef: query.LocationRef);
|
||||
_mockBookRepository.GetByIdAsync(query.BookId, cancellationToken).Returns(Result.From<BookEntity?>(book));
|
||||
_mockLibraryRepository.GetByIdAsync(book.LibraryId, cancellationToken).Returns(Result.From<LibraryEntity?>(library));
|
||||
_mockBookRepository.GetByIdAsync(bookId, shouldTrackEntities: false, cancellationToken: cancellationToken).Returns(Result.From<BookEntity?>(book));
|
||||
_mockLibraryRepository.GetByIdAsync(book.LibraryId, shouldTrackEntities: false, cancellationToken: cancellationToken).Returns(Result.From<LibraryEntity?>(library));
|
||||
_mockBookReadingService.GetSectionAsync(book.Id, book.LibraryId, book.Path, library.LibraryType, query.LocationRef, shouldRenderPdfAsImages: false, shouldPreserveStyles: true, cancellationToken).Returns(expectedResponse);
|
||||
|
||||
// Act
|
||||
@@ -108,14 +111,16 @@ public class GetReadingSectionQueryHandlerTests
|
||||
{
|
||||
// Arrange
|
||||
GetReadingSectionQuery query = _getReadingSectionQueryFixture.Create();
|
||||
Guid libraryId = Guid.Parse(query.LibraryId!);
|
||||
Guid bookId = Guid.Parse(query.BookId!);
|
||||
CancellationToken cancellationToken = CancellationToken.None;
|
||||
BookEntity book = _bookEntityFixture.Create(id: query.BookId);
|
||||
BookEntity book = _bookEntityFixture.Create(id: bookId, libraryId: libraryId);
|
||||
LibraryEntity library = _libraryEntityFixture.Create(id: book.LibraryId, libraryType: LibraryType.EBook);
|
||||
UserSettingsEntity settings = _userSettingsEntityFixture.Create(userId: _userId, shouldRenderPdfAsImages: true, shouldPreserveBookStyles: true);
|
||||
ReadingSectionDto expectedResponse = _readingSectionDtoFixture.Create(locationRef: query.LocationRef);
|
||||
_mockUserSettingsRepository.GetByUserIdAsync(_userId, cancellationToken).Returns(Result.From<UserSettingsEntity?>(settings));
|
||||
_mockBookRepository.GetByIdAsync(query.BookId, cancellationToken).Returns(Result.From<BookEntity?>(book));
|
||||
_mockLibraryRepository.GetByIdAsync(book.LibraryId, cancellationToken).Returns(Result.From<LibraryEntity?>(library));
|
||||
_mockBookRepository.GetByIdAsync(bookId, shouldTrackEntities: false, cancellationToken: cancellationToken).Returns(Result.From<BookEntity?>(book));
|
||||
_mockLibraryRepository.GetByIdAsync(book.LibraryId, shouldTrackEntities: false, cancellationToken: cancellationToken).Returns(Result.From<LibraryEntity?>(library));
|
||||
_mockBookReadingService.GetSectionAsync(book.Id, book.LibraryId, book.Path, library.LibraryType, query.LocationRef, shouldRenderPdfAsImages: true, shouldPreserveStyles: true, cancellationToken).Returns(expectedResponse);
|
||||
|
||||
// Act
|
||||
@@ -133,14 +138,16 @@ public class GetReadingSectionQueryHandlerTests
|
||||
{
|
||||
// Arrange
|
||||
GetReadingSectionQuery query = _getReadingSectionQueryFixture.Create();
|
||||
Guid libraryId = Guid.Parse(query.LibraryId!);
|
||||
Guid bookId = Guid.Parse(query.BookId!);
|
||||
CancellationToken cancellationToken = CancellationToken.None;
|
||||
BookEntity book = _bookEntityFixture.Create(id: query.BookId);
|
||||
BookEntity book = _bookEntityFixture.Create(id: bookId, libraryId: libraryId);
|
||||
LibraryEntity library = _libraryEntityFixture.Create(id: book.LibraryId, libraryType: LibraryType.EBook);
|
||||
UserSettingsEntity settings = _userSettingsEntityFixture.Create(userId: _userId, shouldRenderPdfAsImages: false, shouldPreserveBookStyles: shouldPreserveBookStyles);
|
||||
ReadingSectionDto expectedResponse = _readingSectionDtoFixture.Create(locationRef: query.LocationRef);
|
||||
_mockUserSettingsRepository.GetByUserIdAsync(_userId, cancellationToken).Returns(Result.From<UserSettingsEntity?>(settings));
|
||||
_mockBookRepository.GetByIdAsync(query.BookId, cancellationToken).Returns(Result.From<BookEntity?>(book));
|
||||
_mockLibraryRepository.GetByIdAsync(book.LibraryId, cancellationToken).Returns(Result.From<LibraryEntity?>(library));
|
||||
_mockBookRepository.GetByIdAsync(bookId, shouldTrackEntities: false, cancellationToken: cancellationToken).Returns(Result.From<BookEntity?>(book));
|
||||
_mockLibraryRepository.GetByIdAsync(book.LibraryId, shouldTrackEntities: false, cancellationToken: cancellationToken).Returns(Result.From<LibraryEntity?>(library));
|
||||
_mockBookReadingService.GetSectionAsync(Arg.Any<Guid>(), Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<LibraryType>(), Arg.Any<string>(), Arg.Any<bool>(), Arg.Any<bool>(), Arg.Any<CancellationToken>()).Returns(expectedResponse);
|
||||
|
||||
// Act
|
||||
@@ -157,12 +164,14 @@ public class GetReadingSectionQueryHandlerTests
|
||||
{
|
||||
// Arrange
|
||||
GetReadingSectionQuery query = _getReadingSectionQueryFixture.Create();
|
||||
Guid libraryId = Guid.Parse(query.LibraryId!);
|
||||
Guid bookId = Guid.Parse(query.BookId!);
|
||||
CancellationToken cancellationToken = CancellationToken.None;
|
||||
BookEntity book = _bookEntityFixture.Create(id: query.BookId);
|
||||
BookEntity book = _bookEntityFixture.Create(id: bookId, libraryId: libraryId);
|
||||
LibraryEntity library = _libraryEntityFixture.Create(id: book.LibraryId, libraryType: LibraryType.EBook);
|
||||
ReadingSectionDto expectedResponse = _readingSectionDtoFixture.Create(locationRef: query.LocationRef);
|
||||
_mockBookRepository.GetByIdAsync(query.BookId, cancellationToken).Returns(Result.From<BookEntity?>(book));
|
||||
_mockLibraryRepository.GetByIdAsync(book.LibraryId, cancellationToken).Returns(Result.From<LibraryEntity?>(library));
|
||||
_mockBookRepository.GetByIdAsync(bookId, shouldTrackEntities: false, cancellationToken: cancellationToken).Returns(Result.From<BookEntity?>(book));
|
||||
_mockLibraryRepository.GetByIdAsync(book.LibraryId, shouldTrackEntities: false, cancellationToken: cancellationToken).Returns(Result.From<LibraryEntity?>(library));
|
||||
_mockBookReadingService.GetSectionAsync(Arg.Any<Guid>(), Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<LibraryType>(), Arg.Any<string>(), Arg.Any<bool>(), Arg.Any<bool>(), Arg.Any<CancellationToken>()).Returns(expectedResponse);
|
||||
|
||||
// Act
|
||||
@@ -180,7 +189,8 @@ public class GetReadingSectionQueryHandlerTests
|
||||
{
|
||||
// Arrange
|
||||
GetReadingSectionQuery query = _getReadingSectionQueryFixture.Create();
|
||||
_mockBookRepository.GetByIdAsync(query.BookId, Arg.Any<CancellationToken>()).Returns(Result.From<BookEntity?>(null));
|
||||
Guid bookId = Guid.Parse(query.BookId!);
|
||||
_mockBookRepository.GetByIdAsync(bookId, shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>()).Returns(Result.From<BookEntity?>(null));
|
||||
|
||||
// Act
|
||||
Result<ReadingSectionDto> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
@@ -191,14 +201,35 @@ public class GetReadingSectionQueryHandlerTests
|
||||
await _mockBookReadingService.DidNotReceive().GetSectionAsync(Arg.Any<Guid>(), Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<LibraryType>(), Arg.Any<string>(), Arg.Any<bool>(), Arg.Any<bool>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenBookBelongsToAnotherLibrary_ShouldReturnBookNotFoundError()
|
||||
{
|
||||
// Arrange
|
||||
GetReadingSectionQuery query = _getReadingSectionQueryFixture.Create();
|
||||
Guid bookId = Guid.Parse(query.BookId!);
|
||||
BookEntity book = _bookEntityFixture.Create(id: bookId, libraryId: Guid.NewGuid());
|
||||
_mockBookRepository.GetByIdAsync(bookId, shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>()).Returns(Result.From<BookEntity?>(book));
|
||||
|
||||
// Act
|
||||
Result<ReadingSectionDto> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(DomainErrors.Reading.BookNotFound, result.FirstError);
|
||||
await _mockLibraryRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>());
|
||||
await _mockBookReadingService.DidNotReceive().GetSectionAsync(Arg.Any<Guid>(), Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<LibraryType>(), Arg.Any<string>(), Arg.Any<bool>(), Arg.Any<bool>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenLibraryDoesNotExist_ShouldReturnLibraryNotFoundError()
|
||||
{
|
||||
// Arrange
|
||||
GetReadingSectionQuery query = _getReadingSectionQueryFixture.Create();
|
||||
BookEntity book = _bookEntityFixture.Create(id: query.BookId);
|
||||
_mockBookRepository.GetByIdAsync(query.BookId, Arg.Any<CancellationToken>()).Returns(Result.From<BookEntity?>(book));
|
||||
_mockLibraryRepository.GetByIdAsync(book.LibraryId, Arg.Any<CancellationToken>()).Returns(Result.From<LibraryEntity?>(null));
|
||||
Guid libraryId = Guid.Parse(query.LibraryId!);
|
||||
Guid bookId = Guid.Parse(query.BookId!);
|
||||
BookEntity book = _bookEntityFixture.Create(id: bookId, libraryId: libraryId);
|
||||
_mockBookRepository.GetByIdAsync(bookId, shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>()).Returns(Result.From<BookEntity?>(book));
|
||||
_mockLibraryRepository.GetByIdAsync(book.LibraryId, shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>()).Returns(Result.From<LibraryEntity?>(null));
|
||||
|
||||
// Act
|
||||
Result<ReadingSectionDto> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
@@ -214,8 +245,10 @@ public class GetReadingSectionQueryHandlerTests
|
||||
{
|
||||
// Arrange
|
||||
GetReadingSectionQuery query = _getReadingSectionQueryFixture.Create();
|
||||
BookEntity book = _bookEntityFixture.Create(id: query.BookId);
|
||||
_mockBookRepository.GetByIdAsync(query.BookId, Arg.Any<CancellationToken>()).Returns(Result.From<BookEntity?>(book));
|
||||
Guid libraryId = Guid.Parse(query.LibraryId!);
|
||||
Guid bookId = Guid.Parse(query.BookId!);
|
||||
BookEntity book = _bookEntityFixture.Create(id: bookId, libraryId: libraryId);
|
||||
_mockBookRepository.GetByIdAsync(bookId, shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>()).Returns(Result.From<BookEntity?>(book));
|
||||
_mockAuthorizationService.EvaluatePolicyAsync<ILibraryOwnershipPolicy>(_userId, Arg.Any<LibraryOwnershipPolicyContext>(), Arg.Any<CancellationToken>())
|
||||
.Returns(false);
|
||||
|
||||
@@ -241,7 +274,24 @@ public class GetReadingSectionQueryHandlerTests
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(ApplicationErrors.Authorization.NotAuthorized, result.FirstError);
|
||||
await _mockBookRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
|
||||
await _mockBookRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenRepositoryReturnsError_ShouldReturnFailureResult()
|
||||
{
|
||||
// Arrange
|
||||
GetReadingSectionQuery query = _getReadingSectionQueryFixture.Create();
|
||||
Guid bookId = Guid.Parse(query.BookId!);
|
||||
_mockBookRepository.GetByIdAsync(bookId, shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Errors.Library.LibraryIdCannotBeEmpty);
|
||||
|
||||
// Act
|
||||
Result<ReadingSectionDto> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Library.LibraryIdCannotBeEmpty, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -257,7 +307,7 @@ public class GetReadingSectionQueryHandlerTests
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Reading.LocationRefCannotBeEmpty, result.FirstError);
|
||||
await _mockBookRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
|
||||
await _mockBookRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>());
|
||||
await _mockBookReadingService.DidNotReceive().GetSectionAsync(Arg.Any<Guid>(), Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<LibraryType>(), Arg.Any<string>(), Arg.Any<bool>(), Arg.Any<bool>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
}
|
||||
+51
-28
@@ -4,7 +4,6 @@ using Lumina.Application.Fixtures.Core.MediaLibrary.WrittenContentLibrary.BookLi
|
||||
using Lumina.Application.UnitTests.Common.Setup;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
@@ -20,32 +19,6 @@ public class GetReadingSectionQueryValidatorTests
|
||||
private readonly GetReadingSectionQueryValidator _validator = new();
|
||||
private readonly GetReadingSectionQueryFixture _getReadingSectionQueryFixture = new();
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenBookIdIsEmpty_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
GetReadingSectionQuery query = _getReadingSectionQueryFixture.Create(bookId: Guid.Empty);
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.Reading.BookIdCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenLocationRefIsEmpty_ShouldHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
GetReadingSectionQuery query = _getReadingSectionQueryFixture.Create(locationRef: string.Empty);
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.Reading.LocationRefCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenQueryIsValid_ShouldNotHaveValidationError()
|
||||
{
|
||||
@@ -56,7 +29,57 @@ public class GetReadingSectionQueryValidatorTests
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldNotHaveValidationError(Errors.Reading.BookIdCannotBeEmpty);
|
||||
result.ShouldNotHaveValidationError(Errors.Library.LibraryIdCannotBeEmpty);
|
||||
result.ShouldNotHaveValidationError(Errors.WrittenContent.BookIdCannotBeEmpty);
|
||||
result.ShouldNotHaveValidationError(Errors.Reading.LocationRefCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)] // missing route value
|
||||
[InlineData("")] // empty route value
|
||||
[InlineData("not-a-library-guid")] // non-Guid route value
|
||||
[InlineData("00000000-0000-0000-0000-000000000000")] // empty Guid route value
|
||||
public void Validate_WhenLibraryIdIsEmptyOrInvalid_ShouldHaveValidationError(string? libraryId)
|
||||
{
|
||||
// Arrange
|
||||
GetReadingSectionQuery query = _getReadingSectionQueryFixture.Create() with { LibraryId = libraryId };
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.Library.LibraryIdCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)] // missing route value
|
||||
[InlineData("")] // empty route value
|
||||
[InlineData("not-a-guid")] // non-Guid route value
|
||||
[InlineData("00000000-0000-0000-0000-000000000000")] // empty Guid route value
|
||||
public void Validate_WhenBookIdIsEmptyOrInvalid_ShouldHaveValidationError(string? bookId)
|
||||
{
|
||||
// Arrange
|
||||
GetReadingSectionQuery query = _getReadingSectionQueryFixture.Create() with { BookId = bookId };
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.WrittenContent.BookIdCannotBeEmpty);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)] // missing route value
|
||||
[InlineData("")] // empty route value
|
||||
public void Validate_WhenLocationRefIsEmpty_ShouldHaveValidationError(string? locationRef)
|
||||
{
|
||||
// Arrange
|
||||
GetReadingSectionQuery query = _getReadingSectionQueryFixture.Create() with { LocationRef = locationRef };
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.Reading.LocationRefCannotBeEmpty);
|
||||
}
|
||||
}
|
||||
+1
@@ -45,6 +45,7 @@ public class SetLibraryBookReaderEnabledCommandHandlerTests
|
||||
public SetLibraryBookReaderEnabledCommandHandlerTests()
|
||||
{
|
||||
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
|
||||
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
|
||||
_mockLibraryBookReaderConfigurationRepository = Substitute.For<ILibraryBookReaderConfigurationRepository>();
|
||||
_mockUnitOfWork.LibraryBookReaderConfigurationRepository.Returns(_mockLibraryBookReaderConfigurationRepository);
|
||||
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
|
||||
|
||||
+12
-8
@@ -2,6 +2,8 @@
|
||||
using Lumina.Application.Common.DataAccess.Entities.Plugins;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Plugins;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Common.DTO.Filtering;
|
||||
using Lumina.Application.Common.DTO.Pagination;
|
||||
using Lumina.Application.Common.Infrastructure.Authentication;
|
||||
using Lumina.Application.Common.Infrastructure.Authorization;
|
||||
using Lumina.Application.Common.Infrastructure.Authorization.Policies.LibraryOwnership;
|
||||
@@ -49,6 +51,7 @@ public class GetLibraryBookReadersQueryHandlerTests
|
||||
public GetLibraryBookReadersQueryHandlerTests()
|
||||
{
|
||||
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
|
||||
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
|
||||
_mockLibraryBookReaderConfigurationRepository = Substitute.For<ILibraryBookReaderConfigurationRepository>();
|
||||
_mockPluginRepository = Substitute.For<IPluginRepository>();
|
||||
_mockUnitOfWork.LibraryBookReaderConfigurationRepository.Returns(_mockLibraryBookReaderConfigurationRepository);
|
||||
@@ -104,7 +107,8 @@ public class GetLibraryBookReadersQueryHandlerTests
|
||||
_pluginEntityFixture.Create(enabledPluginId, "A Reader"),
|
||||
_pluginEntityFixture.Create(disabledPluginId, "B Reader")
|
||||
];
|
||||
_mockPluginRepository.GetAllAsync(Arg.Any<CancellationToken>()).Returns(plugins);
|
||||
_mockPluginRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From(new PaginatedResultDto<PluginEntity> { Data = plugins, CurrentPage = 1, PerPage = plugins.Count, Count = plugins.Count, NumberOfPages = 1 }));
|
||||
Dictionary<Guid, IReadOnlyList<string>> supportedExtensions = new()
|
||||
{
|
||||
[enabledPluginId] = [".epub"],
|
||||
@@ -138,8 +142,8 @@ public class GetLibraryBookReadersQueryHandlerTests
|
||||
];
|
||||
_mockLibraryBookReaderConfigurationRepository.GetByLibraryIdAsync(query.LibraryId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IReadOnlyList<LibraryBookReaderConfigurationEntity>>(configurations));
|
||||
_mockPluginRepository.GetAllAsync(Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IEnumerable<PluginEntity>>([_pluginEntityFixture.Create()]));
|
||||
_mockPluginRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From(new PaginatedResultDto<PluginEntity> { Data = [_pluginEntityFixture.Create()], CurrentPage = 1, PerPage = 1, Count = 1, NumberOfPages = 1 }));
|
||||
|
||||
// Act
|
||||
Result<IReadOnlyList<LibraryBookReaderResponse>> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
@@ -159,8 +163,8 @@ public class GetLibraryBookReadersQueryHandlerTests
|
||||
GetLibraryBookReadersQuery query = _getLibraryBookReadersQueryFixture.Create();
|
||||
_mockLibraryBookReaderConfigurationRepository.GetByLibraryIdAsync(query.LibraryId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IReadOnlyList<LibraryBookReaderConfigurationEntity>>([]));
|
||||
_mockPluginRepository.GetAllAsync(Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IEnumerable<PluginEntity>>([]));
|
||||
_mockPluginRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From(new PaginatedResultDto<PluginEntity> { Data = [], CurrentPage = 1, PerPage = 0, Count = 0, NumberOfPages = 1 }));
|
||||
|
||||
// Act
|
||||
Result<IReadOnlyList<LibraryBookReaderResponse>> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
@@ -184,7 +188,7 @@ public class GetLibraryBookReadersQueryHandlerTests
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
await _mockPluginRepository.DidNotReceive().GetAllAsync(Arg.Any<CancellationToken>());
|
||||
await _mockPluginRepository.DidNotReceive().GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -194,7 +198,7 @@ public class GetLibraryBookReadersQueryHandlerTests
|
||||
GetLibraryBookReadersQuery query = _getLibraryBookReadersQueryFixture.Create();
|
||||
_mockLibraryBookReaderConfigurationRepository.GetByLibraryIdAsync(query.LibraryId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IReadOnlyList<LibraryBookReaderConfigurationEntity>>([]));
|
||||
_mockPluginRepository.GetAllAsync(Arg.Any<CancellationToken>())
|
||||
_mockPluginRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Error.Failure(description: "Failed to get plugins"));
|
||||
|
||||
// Act
|
||||
@@ -219,7 +223,7 @@ public class GetLibraryBookReadersQueryHandlerTests
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(ApplicationErrors.Authorization.NotAuthorized, result.FirstError);
|
||||
await _mockLibraryBookReaderConfigurationRepository.DidNotReceive().GetByLibraryIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
|
||||
await _mockPluginRepository.DidNotReceive().GetAllAsync(Arg.Any<CancellationToken>());
|
||||
await _mockPluginRepository.DidNotReceive().GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
Reference in new issue
Block a user