Refactors the get book slice

This commit is contained in:
BinaryAlley committed 2026-09-13 06:58:32 -07:00
1 parent 1d35cd01db
commit 8aaf9fe64e
17 files changed
+395 -347

No files matched your search

@@ -1,15 +1,12 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.MediaContributors;
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
using Lumina.Application.Common.DataAccess.Repositories.Books;
using Lumina.Application.Common.DataAccess.Repositories.MediaContributors;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Common.Infrastructure.Authentication;
using Lumina.Application.Common.Infrastructure.Authorization;
using Lumina.Application.Common.Infrastructure.Authorization.Policies.LibraryOwnership;
using Lumina.Application.Common.Infrastructure.Validation;
using Lumina.Application.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Queries.GetBook;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaContributors;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
using Lumina.Application.Fixtures.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Queries.GetBook;
using Lumina.Contracts.Responses.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
@@ -18,9 +15,7 @@ using Lumina.Domain.Common.Primitives;
using Lumina.Domain.SharedKernel.Common.Enums.MediaContributors;
using NSubstitute;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using ApplicationErrors = Lumina.Application.Common.Errors.Errors;
@@ -36,14 +31,12 @@ public class GetBookQueryHandlerTests
{
private readonly IUnitOfWork _mockUnitOfWork;
private readonly IBookRepository _mockBookRepository;
private readonly IMediaContributorRepository _mockMediaContributorRepository;
private readonly IAuthorizationService _mockAuthorizationService;
private readonly ICurrentUserService _mockCurrentUserService;
private readonly IValidator<GetBookQuery> _mockValidator;
private readonly GetBookQueryHandler _sut;
private readonly Guid _userId;
private readonly BookEntityFixture _bookEntityFixture = new();
private readonly MediaContributorEntityFixture _mediaContributorEntityFixture = new();
private readonly BookContributorEntityFixture _bookContributorEntityFixture = new();
private readonly GetBookQueryFixture _getBookQueryFixture = new();
@@ -53,10 +46,9 @@ public class GetBookQueryHandlerTests
public GetBookQueryHandlerTests()
{
_mockBookRepository = Substitute.For<IBookRepository>();
_mockMediaContributorRepository = Substitute.For<IMediaContributorRepository>();
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockUnitOfWork.BookRepository.Returns(_mockBookRepository);
_mockUnitOfWork.MediaContributorRepository.Returns(_mockMediaContributorRepository);
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
_mockCurrentUserService = Substitute.For<ICurrentUserService>();
_mockValidator = Substitute.For<IValidator<GetBookQuery>>();
@@ -76,8 +68,10 @@ public class GetBookQueryHandlerTests
{
// Arrange
GetBookQuery query = _getBookQueryFixture.Create();
BookEntity bookEntity = _bookEntityFixture.Create(id: query.Id);
_mockBookRepository.GetByIdAsync(query.Id, Arg.Any<CancellationToken>())
Guid libraryId = Guid.Parse(query.LibraryId!);
Guid bookId = Guid.Parse(query.BookId!);
BookEntity bookEntity = _bookEntityFixture.Create(id: bookId, libraryId: libraryId);
_mockBookRepository.GetByIdAsync(bookId, shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<BookEntity?>(bookEntity));
// Act
@@ -85,27 +79,27 @@ public class GetBookQueryHandlerTests
// Assert
Assert.False(result.IsFailure);
Assert.Equal(query.Id, result.Value.Id);
Assert.Equal(bookId, result.Value.Id);
Assert.Equal(bookEntity.Title, result.Value.Metadata!.Title);
Assert.Null(result.Value.Contributors);
await _mockMediaContributorRepository.DidNotReceive().GetByIdsAsync(Arg.Any<IReadOnlyCollection<Guid>>(), Arg.Any<CancellationToken>());
Assert.NotNull(result.Value.Contributors);
Assert.Empty(result.Value.Contributors!);
}
[Fact]
public async Task HandleAsync_WhenBookHasContributors_ShouldReturnResponseWithResolvedContributorNames()
public async Task HandleAsync_WhenBookHasContributors_ShouldReturnResponseWithContributorReferences()
{
// Arrange
GetBookQuery query = _getBookQueryFixture.Create();
BookEntity bookEntity = _bookEntityFixture.Create(id: query.Id);
MediaContributorEntity contributor = _mediaContributorEntityFixture.Create();
Guid libraryId = Guid.Parse(query.LibraryId!);
Guid bookId = Guid.Parse(query.BookId!);
BookEntity bookEntity = _bookEntityFixture.Create(id: bookId, libraryId: libraryId);
Guid contributorId = Guid.NewGuid();
bookEntity.BookContributors =
[
_bookContributorEntityFixture.Create(bookId: query.Id, mediaContributorId: contributor.Id, roleName: "author", roleCategory: MediaContributorRoleCategory.Author)
_bookContributorEntityFixture.Create(bookId: bookId, mediaContributorId: contributorId, role: MediaContributorRole.Author)
];
_mockBookRepository.GetByIdAsync(query.Id, Arg.Any<CancellationToken>())
_mockBookRepository.GetByIdAsync(bookId, shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<BookEntity?>(bookEntity));
_mockMediaContributorRepository.GetByIdsAsync(Arg.Any<IReadOnlyCollection<Guid>>(), Arg.Any<CancellationToken>())
.Returns(Result.From<IReadOnlyList<MediaContributorEntity>>([contributor]));
// Act
Result<BookResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
@@ -114,8 +108,8 @@ public class GetBookQueryHandlerTests
Assert.False(result.IsFailure);
Assert.NotNull(result.Value.Contributors);
Assert.Single(result.Value.Contributors!);
Assert.Equal(contributor.DisplayName, result.Value.Contributors![0].Name!.DisplayName);
Assert.Equal("author", result.Value.Contributors![0].Role!.Name);
Assert.Equal(contributorId, result.Value.Contributors![0].ContributorId);
Assert.Equal(MediaContributorRole.Author, result.Value.Contributors![0].Role);
}
[Fact]
@@ -123,7 +117,8 @@ public class GetBookQueryHandlerTests
{
// Arrange
GetBookQuery query = _getBookQueryFixture.Create();
_mockBookRepository.GetByIdAsync(query.Id, Arg.Any<CancellationToken>())
Guid bookId = Guid.Parse(query.BookId!);
_mockBookRepository.GetByIdAsync(bookId, shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<BookEntity?>(null));
// Act
@@ -135,12 +130,32 @@ public class GetBookQueryHandlerTests
await _mockAuthorizationService.DidNotReceive().EvaluatePolicyAsync<ILibraryOwnershipPolicy>(Arg.Any<Guid>(), Arg.Any<LibraryOwnershipPolicyContext>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task HandleAsync_WhenBookBelongsToAnotherLibrary_ShouldReturnBookNotFoundError()
{
// Arrange
GetBookQuery query = _getBookQueryFixture.Create();
Guid bookId = Guid.Parse(query.BookId!);
BookEntity bookEntity = _bookEntityFixture.Create(id: bookId, libraryId: Guid.NewGuid());
_mockBookRepository.GetByIdAsync(bookId, shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<BookEntity?>(bookEntity));
// Act
Result<BookResponse> result = await _sut.HandleAsync(query, 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>());
}
[Fact]
public async Task HandleAsync_WhenRepositoryReturnsError_ShouldReturnFailureResult()
{
// Arrange
GetBookQuery query = _getBookQueryFixture.Create();
_mockBookRepository.GetByIdAsync(query.Id, Arg.Any<CancellationToken>())
Guid bookId = Guid.Parse(query.BookId!);
_mockBookRepository.GetByIdAsync(bookId, shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Errors.Library.LibraryIdCannotBeEmpty);
// Act
@@ -156,8 +171,10 @@ public class GetBookQueryHandlerTests
{
// Arrange
GetBookQuery query = _getBookQueryFixture.Create();
BookEntity bookEntity = _bookEntityFixture.Create(id: query.Id);
_mockBookRepository.GetByIdAsync(query.Id, Arg.Any<CancellationToken>())
Guid libraryId = Guid.Parse(query.LibraryId!);
Guid bookId = Guid.Parse(query.BookId!);
BookEntity bookEntity = _bookEntityFixture.Create(id: bookId, libraryId: libraryId);
_mockBookRepository.GetByIdAsync(bookId, shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<BookEntity?>(bookEntity));
_mockAuthorizationService.EvaluatePolicyAsync<ILibraryOwnershipPolicy>(_userId, Arg.Any<LibraryOwnershipPolicyContext>(), Arg.Any<CancellationToken>())
.Returns(false);
@@ -170,7 +187,6 @@ public class GetBookQueryHandlerTests
Assert.Equal(ApplicationErrors.Authorization.NotAuthorized, result.FirstError);
await _mockAuthorizationService.Received(1).EvaluatePolicyAsync<ILibraryOwnershipPolicy>(
_userId, Arg.Is<LibraryOwnershipPolicyContext>(context => context.LibraryId == bookEntity.LibraryId), Arg.Any<CancellationToken>());
await _mockMediaContributorRepository.DidNotReceive().GetByIdsAsync(Arg.Any<IReadOnlyCollection<Guid>>(), Arg.Any<CancellationToken>());
}
[Fact]
@@ -186,7 +202,7 @@ public class GetBookQueryHandlerTests
// 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>());
await _mockAuthorizationService.DidNotReceive().EvaluatePolicyAsync<ILibraryOwnershipPolicy>(Arg.Any<Guid>(), Arg.Any<LibraryOwnershipPolicyContext>(), Arg.Any<CancellationToken>());
}
@@ -203,7 +219,7 @@ public class GetBookQueryHandlerTests
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Errors.WrittenContent.BookIdCannotBeEmpty, 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 _mockAuthorizationService.DidNotReceive().EvaluatePolicyAsync<ILibraryOwnershipPolicy>(Arg.Any<Guid>(), Arg.Any<LibraryOwnershipPolicyContext>(), Arg.Any<CancellationToken>());
}
}
@@ -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,7 +20,7 @@ public class GetBookQueryValidatorTests
private readonly GetBookQueryValidator _validator = new();
[Fact]
public void Validate_WhenIdIsValid_ShouldNotHaveValidationError()
public void Validate_WhenIdsAreValid_ShouldNotHaveValidationError()
{
// Arrange
GetBookQuery query = _queryBookFixture.Create();
@@ -30,14 +29,36 @@ public class GetBookQueryValidatorTests
List<Error> result = _validator.TestValidate(query);
// Assert
result.ShouldNotHaveValidationError(Errors.Library.LibraryIdCannotBeEmpty);
result.ShouldNotHaveValidationError(Errors.WrittenContent.BookIdCannotBeEmpty);
}
[Fact]
public void Validate_WhenIdIsEmpty_ShouldHaveValidationError()
[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
GetBookQuery query = _queryBookFixture.Create(id: Guid.Empty);
GetBookQuery query = _queryBookFixture.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
GetBookQuery query = _queryBookFixture.Create() with { BookId = bookId };
// Act
List<Error> result = _validator.TestValidate(query);