Adds the media library item deletion strategies and their domain event handler integration

This commit is contained in:
BinaryAlley committed 2026-10-09 01:23:00 +03:00
1 parent 35ac1dbdcb
commit b7db29eee5
7 files changed
+811 -113

No files matched your search

@@ -0,0 +1,179 @@
#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.BookLibrary;
using Lumina.Application.Common.DataAccess.Repositories.MediaLibrary;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Core.MediaLibrary.Management.Deletion;
using Lumina.Application.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Artwork;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaLibrary.Management;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
using Lumina.Domain.Common.Primitives;
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryAggregate.ValueObjects;
using Lumina.Domain.Fixtures.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryAggregate.ValueObjects;
using Microsoft.Extensions.Logging;
using NSubstitute;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.UnitTests.Core.MediaLibrary.Management.Deletion;
/// <summary>
/// Contains unit tests for the <see cref="BooksMediaLibraryItemDeletionStrategy"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class BooksMediaLibraryItemDeletionStrategyTests
{
private readonly IUnitOfWork _mockUnitOfWork;
private readonly IBookRepository _mockBookRepository;
private readonly ILibraryRepository _mockLibraryRepository;
private readonly IBookArtworkService _mockBookArtworkService;
private readonly ILogger<BooksMediaLibraryItemDeletionStrategy> _mockLogger;
private readonly BooksMediaLibraryItemDeletionStrategy _sut;
private readonly LibraryIdFixture _libraryIdFixture = new();
private readonly BookEntityFixture _bookEntityFixture = new();
private readonly LibraryEntityFixture _libraryEntityFixture = new();
/// <summary>
/// Initializes a new instance of the <see cref="BooksMediaLibraryItemDeletionStrategyTests"/> class.
/// </summary>
public BooksMediaLibraryItemDeletionStrategyTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockBookRepository = Substitute.For<IBookRepository>();
_mockLibraryRepository = Substitute.For<ILibraryRepository>();
_mockUnitOfWork.BookRepository.Returns(_mockBookRepository);
_mockUnitOfWork.LibraryRepository.Returns(_mockLibraryRepository);
_mockBookArtworkService = Substitute.For<IBookArtworkService>();
_mockLogger = Substitute.For<ILogger<BooksMediaLibraryItemDeletionStrategy>>();
_sut = new BooksMediaLibraryItemDeletionStrategy(_mockUnitOfWork, _mockBookArtworkService, _mockLogger);
}
[Fact]
public async Task DeleteItemAsync_WhenBookExistsAtPath_ShouldDeleteTheBookAndItsArtwork()
{
// Arrange
LibraryId libraryId = _libraryIdFixture.Create();
BookEntity book = _bookEntityFixture.Create(path: "/books/deleted.epub");
LibraryEntity library = _libraryEntityFixture.Create(id: libraryId.Value, title: "My Library");
_mockBookRepository.GetByPathAsync(libraryId.Value, book.Path, Arg.Any<CancellationToken>())
.Returns(Result.From<BookEntity?>(book));
_mockLibraryRepository.GetByIdAsync(libraryId.Value, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(library));
_mockBookRepository.GetAuthorsDisplayNamesByBookIdsAsync(Arg.Any<IReadOnlyCollection<Guid>>(), Arg.Any<CancellationToken>())
.Returns(Result.From<IReadOnlyDictionary<Guid, string?>>(new Dictionary<Guid, string?> { [book.Id] = "Frank Herbert" }));
_mockBookRepository.DeleteByIdAsync(book.Id, Arg.Any<CancellationToken>())
.Returns(Result.From(Result.Deleted));
_mockBookArtworkService.DeleteBookArtwork(Arg.Any<Guid>(), Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>())
.Returns(Result.Deleted);
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
// Act
Result<Success> result = await _sut.DeleteItemAsync(libraryId.Value, book.Path, CancellationToken.None);
// Assert
Assert.True(result.IsSuccess);
_mockBookArtworkService.Received(1).DeleteBookArtwork(libraryId.Value, book.Id, "My Library", "Frank Herbert", book.Title);
await _mockBookRepository.Received(1).DeleteByIdAsync(book.Id, Arg.Any<CancellationToken>());
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
}
[Fact]
public async Task DeleteItemAsync_WhenNoBookExistsAtPath_ShouldDoNothing()
{
// Arrange
LibraryId libraryId = _libraryIdFixture.Create();
_mockBookRepository.GetByPathAsync(libraryId.Value, "/books/deleted.epub", Arg.Any<CancellationToken>())
.Returns(Result.From<BookEntity?>(null));
// Act
Result<Success> result = await _sut.DeleteItemAsync(libraryId.Value, "/books/deleted.epub", CancellationToken.None);
// Assert
Assert.True(result.IsSuccess);
_mockBookArtworkService.DidNotReceive().DeleteBookArtwork(Arg.Any<Guid>(), Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>());
await _mockBookRepository.DidNotReceive().DeleteByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
[Fact]
public async Task DeleteItemAsync_WhenDeleteArtworkFails_ShouldStillDeleteTheBook()
{
// Arrange
LibraryId libraryId = _libraryIdFixture.Create();
BookEntity book = _bookEntityFixture.Create(path: "/books/deleted.epub");
LibraryEntity library = _libraryEntityFixture.Create(id: libraryId.Value, title: "My Library");
_mockBookRepository.GetByPathAsync(libraryId.Value, book.Path, Arg.Any<CancellationToken>())
.Returns(Result.From<BookEntity?>(book));
_mockLibraryRepository.GetByIdAsync(libraryId.Value, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(library));
_mockBookRepository.GetAuthorsDisplayNamesByBookIdsAsync(Arg.Any<IReadOnlyCollection<Guid>>(), Arg.Any<CancellationToken>())
.Returns(Result.From<IReadOnlyDictionary<Guid, string?>>(new Dictionary<Guid, string?> { [book.Id] = "Frank Herbert" }));
_mockBookArtworkService.DeleteBookArtwork(Arg.Any<Guid>(), Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>())
.Returns(Error.Failure("Artwork.DeleteFailed", "Failed to delete the stored artwork"));
_mockBookRepository.DeleteByIdAsync(book.Id, Arg.Any<CancellationToken>())
.Returns(Result.From(Result.Deleted));
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
// Act
Result<Success> result = await _sut.DeleteItemAsync(libraryId.Value, book.Path, CancellationToken.None);
// Assert
// a failing artwork deletion must not prevent the book from being removed, so the deletion is only logged
Assert.True(result.IsSuccess);
await _mockBookRepository.Received(1).DeleteByIdAsync(book.Id, Arg.Any<CancellationToken>());
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
}
[Fact]
public async Task DeleteItemAsync_WhenGetBookFails_ShouldReturnFailure()
{
// Arrange
LibraryId libraryId = _libraryIdFixture.Create();
Error error = Error.Failure("Database.Error", "Failed to get the book");
_mockBookRepository.GetByPathAsync(libraryId.Value, "/books/deleted.epub", Arg.Any<CancellationToken>())
.Returns(error);
// Act
Result<Success> result = await _sut.DeleteItemAsync(libraryId.Value, "/books/deleted.epub", CancellationToken.None);
// Assert
Assert.True(result.IsFailure);
Assert.Equal(error, result.FirstError);
await _mockBookRepository.DidNotReceive().DeleteByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task DeleteItemAsync_WhenDeleteBookFails_ShouldReturnFailure()
{
// Arrange
LibraryId libraryId = _libraryIdFixture.Create();
BookEntity book = _bookEntityFixture.Create(path: "/books/deleted.epub");
LibraryEntity library = _libraryEntityFixture.Create(id: libraryId.Value, title: "My Library");
_mockBookRepository.GetByPathAsync(libraryId.Value, book.Path, Arg.Any<CancellationToken>())
.Returns(Result.From<BookEntity?>(book));
_mockLibraryRepository.GetByIdAsync(libraryId.Value, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(library));
_mockBookRepository.GetAuthorsDisplayNamesByBookIdsAsync(Arg.Any<IReadOnlyCollection<Guid>>(), Arg.Any<CancellationToken>())
.Returns(Result.From<IReadOnlyDictionary<Guid, string?>>(new Dictionary<Guid, string?> { [book.Id] = "Frank Herbert" }));
_mockBookArtworkService.DeleteBookArtwork(Arg.Any<Guid>(), Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>())
.Returns(Result.Deleted);
Error error = Error.Failure("Database.Error", "Failed to delete the book");
_mockBookRepository.DeleteByIdAsync(book.Id, Arg.Any<CancellationToken>())
.Returns(error);
// Act
Result<Success> result = await _sut.DeleteItemAsync(libraryId.Value, book.Path, CancellationToken.None);
// Assert
Assert.True(result.IsFailure);
Assert.Equal(error, result.FirstError);
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
}
@@ -0,0 +1,329 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.AudioLibrary.MusicLibrary;
using Lumina.Application.Common.DataAccess.Repositories.MusicLibrary;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Core.MediaLibrary.Management.Deletion;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaLibrary.AudioLibrary.MusicLibrary;
using Lumina.Domain.Common.Primitives;
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryAggregate.ValueObjects;
using Lumina.Domain.Fixtures.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryAggregate.ValueObjects;
using Lumina.Domain.SharedKernel.Common.Enums.AudioLibrary;
using NSubstitute;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.UnitTests.Core.MediaLibrary.Management.Deletion;
/// <summary>
/// Contains unit tests for the <see cref="MusicMediaLibraryItemDeletionStrategy"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class MusicMediaLibraryItemDeletionStrategyTests
{
private readonly IUnitOfWork _mockUnitOfWork;
private readonly ITrackRepository _mockTrackRepository;
private readonly IAlbumRepository _mockAlbumRepository;
private readonly IArtistRepository _mockArtistRepository;
private readonly IMusicArtworkRepository _mockMusicArtworkRepository;
private readonly MusicMediaLibraryItemDeletionStrategy _sut;
private readonly LibraryIdFixture _libraryIdFixture = new();
private readonly TrackEntityFixture _trackEntityFixture = new();
private readonly AlbumEntityFixture _albumEntityFixture = new();
/// <summary>
/// Initializes a new instance of the <see cref="MusicMediaLibraryItemDeletionStrategyTests"/> class.
/// </summary>
public MusicMediaLibraryItemDeletionStrategyTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockTrackRepository = Substitute.For<ITrackRepository>();
_mockAlbumRepository = Substitute.For<IAlbumRepository>();
_mockArtistRepository = Substitute.For<IArtistRepository>();
_mockMusicArtworkRepository = Substitute.For<IMusicArtworkRepository>();
_mockUnitOfWork.TrackRepository.Returns(_mockTrackRepository);
_mockUnitOfWork.AlbumRepository.Returns(_mockAlbumRepository);
_mockUnitOfWork.ArtistRepository.Returns(_mockArtistRepository);
_mockUnitOfWork.MusicArtworkRepository.Returns(_mockMusicArtworkRepository);
_sut = new MusicMediaLibraryItemDeletionStrategy(_mockUnitOfWork);
}
[Fact]
public async Task DeleteItemAsync_WhenAlbumHasOtherTracks_ShouldDeleteOnlyTheTrackAndItsArtwork()
{
// Arrange
LibraryId libraryId = _libraryIdFixture.Create();
Guid albumId = Guid.NewGuid();
TrackEntity track = _trackEntityFixture.Create(albumId: albumId, path: "/music/deleted.flac");
AlbumEntity album = _albumEntityFixture.Create(id: albumId, includeTracks: false);
TrackEntity otherTrack = _trackEntityFixture.Create(albumId: albumId);
_mockTrackRepository.GetByPathAsync(libraryId.Value, track.Path, Arg.Any<CancellationToken>())
.Returns(Result.From<TrackEntity?>(track));
_mockMusicArtworkRepository.DeleteByOwnerAsync(MusicArtworkOwnerType.Track, track.Id, Arg.Any<CancellationToken>())
.Returns(Result.Deleted);
_mockTrackRepository.DeleteByIdAsync(track.Id, Arg.Any<CancellationToken>())
.Returns(Result.From(Result.Deleted));
_mockAlbumRepository.GetByIdAsync(albumId, Arg.Any<bool>(), Arg.Any<bool>(), Arg.Any<CancellationToken>())
.Returns(Result.From<AlbumEntity?>(album));
_mockTrackRepository.GetByAlbumIdAsync(albumId, Arg.Any<CancellationToken>())
.Returns(Result.From<IReadOnlyList<TrackEntity>>([otherTrack]));
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
// Act
Result<Success> result = await _sut.DeleteItemAsync(libraryId.Value, track.Path, CancellationToken.None);
// Assert
Assert.True(result.IsSuccess);
await _mockMusicArtworkRepository.Received(1).DeleteByOwnerAsync(MusicArtworkOwnerType.Track, track.Id, Arg.Any<CancellationToken>());
await _mockTrackRepository.Received(1).DeleteByIdAsync(track.Id, Arg.Any<CancellationToken>());
await _mockMusicArtworkRepository.DidNotReceive().DeleteByOwnerAsync(MusicArtworkOwnerType.Album, Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockAlbumRepository.DidNotReceive().DeleteByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockArtistRepository.DidNotReceive().DeleteByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
}
[Fact]
public async Task DeleteItemAsync_WhenAlbumHasNoOtherTracksButArtistHasOtherAlbums_ShouldDeleteTheAlbumAndItsArtwork()
{
// Arrange
LibraryId libraryId = _libraryIdFixture.Create();
Guid albumId = Guid.NewGuid();
Guid artistId = Guid.NewGuid();
TrackEntity track = _trackEntityFixture.Create(albumId: albumId, path: "/music/deleted.flac");
AlbumEntity album = _albumEntityFixture.Create(id: albumId, artistId: artistId, includeTracks: false);
AlbumEntity otherAlbum = _albumEntityFixture.Create(artistId: artistId, includeTracks: false);
_mockTrackRepository.GetByPathAsync(libraryId.Value, track.Path, Arg.Any<CancellationToken>())
.Returns(Result.From<TrackEntity?>(track));
_mockMusicArtworkRepository.DeleteByOwnerAsync(MusicArtworkOwnerType.Track, track.Id, Arg.Any<CancellationToken>())
.Returns(Result.Deleted);
_mockTrackRepository.DeleteByIdAsync(track.Id, Arg.Any<CancellationToken>())
.Returns(Result.From(Result.Deleted));
_mockAlbumRepository.GetByIdAsync(albumId, Arg.Any<bool>(), Arg.Any<bool>(), Arg.Any<CancellationToken>())
.Returns(Result.From<AlbumEntity?>(album));
_mockTrackRepository.GetByAlbumIdAsync(albumId, Arg.Any<CancellationToken>())
.Returns(Result.From<IReadOnlyList<TrackEntity>>([track]));
_mockMusicArtworkRepository.DeleteByOwnerAsync(MusicArtworkOwnerType.Album, albumId, Arg.Any<CancellationToken>())
.Returns(Result.Deleted);
_mockAlbumRepository.DeleteByIdAsync(albumId, Arg.Any<CancellationToken>())
.Returns(Result.From(Result.Deleted));
_mockAlbumRepository.GetByArtistIdAsync(artistId, Arg.Any<CancellationToken>())
.Returns(Result.From<IReadOnlyList<AlbumEntity>>([otherAlbum]));
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
// Act
Result<Success> result = await _sut.DeleteItemAsync(libraryId.Value, track.Path, CancellationToken.None);
// Assert
Assert.True(result.IsSuccess);
await _mockMusicArtworkRepository.Received(1).DeleteByOwnerAsync(MusicArtworkOwnerType.Album, albumId, Arg.Any<CancellationToken>());
await _mockAlbumRepository.Received(1).DeleteByIdAsync(albumId, Arg.Any<CancellationToken>());
await _mockMusicArtworkRepository.DidNotReceive().DeleteByOwnerAsync(MusicArtworkOwnerType.Artist, Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockArtistRepository.DidNotReceive().DeleteByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
}
[Fact]
public async Task DeleteItemAsync_WhenAlbumAndArtistAreLeftWithNoChildren_ShouldDeleteTheTrackAlbumAndArtist()
{
// Arrange
LibraryId libraryId = _libraryIdFixture.Create();
Guid albumId = Guid.NewGuid();
Guid artistId = Guid.NewGuid();
TrackEntity track = _trackEntityFixture.Create(albumId: albumId, path: "/music/deleted.flac");
AlbumEntity album = _albumEntityFixture.Create(id: albumId, artistId: artistId, includeTracks: false);
_mockTrackRepository.GetByPathAsync(libraryId.Value, track.Path, Arg.Any<CancellationToken>())
.Returns(Result.From<TrackEntity?>(track));
_mockMusicArtworkRepository.DeleteByOwnerAsync(MusicArtworkOwnerType.Track, track.Id, Arg.Any<CancellationToken>())
.Returns(Result.Deleted);
_mockTrackRepository.DeleteByIdAsync(track.Id, Arg.Any<CancellationToken>())
.Returns(Result.From(Result.Deleted));
_mockAlbumRepository.GetByIdAsync(albumId, Arg.Any<bool>(), Arg.Any<bool>(), Arg.Any<CancellationToken>())
.Returns(Result.From<AlbumEntity?>(album));
_mockTrackRepository.GetByAlbumIdAsync(albumId, Arg.Any<CancellationToken>())
.Returns(Result.From<IReadOnlyList<TrackEntity>>([track]));
_mockMusicArtworkRepository.DeleteByOwnerAsync(MusicArtworkOwnerType.Album, albumId, Arg.Any<CancellationToken>())
.Returns(Result.Deleted);
_mockAlbumRepository.DeleteByIdAsync(albumId, Arg.Any<CancellationToken>())
.Returns(Result.From(Result.Deleted));
_mockAlbumRepository.GetByArtistIdAsync(artistId, Arg.Any<CancellationToken>())
.Returns(Result.From<IReadOnlyList<AlbumEntity>>([album]));
_mockMusicArtworkRepository.DeleteByOwnerAsync(MusicArtworkOwnerType.Artist, artistId, Arg.Any<CancellationToken>())
.Returns(Result.Deleted);
_mockArtistRepository.DeleteByIdAsync(artistId, Arg.Any<CancellationToken>())
.Returns(Result.From(Result.Deleted));
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
// Act
Result<Success> result = await _sut.DeleteItemAsync(libraryId.Value, track.Path, CancellationToken.None);
// Assert
Assert.True(result.IsSuccess);
await _mockMusicArtworkRepository.Received(1).DeleteByOwnerAsync(MusicArtworkOwnerType.Album, albumId, Arg.Any<CancellationToken>());
await _mockMusicArtworkRepository.Received(1).DeleteByOwnerAsync(MusicArtworkOwnerType.Artist, artistId, Arg.Any<CancellationToken>());
await _mockAlbumRepository.Received(1).DeleteByIdAsync(albumId, Arg.Any<CancellationToken>());
await _mockArtistRepository.Received(1).DeleteByIdAsync(artistId, Arg.Any<CancellationToken>());
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
}
[Fact]
public async Task DeleteItemAsync_WhenAlbumWasAlreadyRemoved_ShouldPersistTheTrackDeletion()
{
// Arrange
LibraryId libraryId = _libraryIdFixture.Create();
Guid albumId = Guid.NewGuid();
TrackEntity track = _trackEntityFixture.Create(albumId: albumId, path: "/music/deleted.flac");
_mockTrackRepository.GetByPathAsync(libraryId.Value, track.Path, Arg.Any<CancellationToken>())
.Returns(Result.From<TrackEntity?>(track));
_mockMusicArtworkRepository.DeleteByOwnerAsync(MusicArtworkOwnerType.Track, track.Id, Arg.Any<CancellationToken>())
.Returns(Result.Deleted);
_mockTrackRepository.DeleteByIdAsync(track.Id, Arg.Any<CancellationToken>())
.Returns(Result.From(Result.Deleted));
_mockAlbumRepository.GetByIdAsync(albumId, Arg.Any<bool>(), Arg.Any<bool>(), Arg.Any<CancellationToken>())
.Returns(Result.From<AlbumEntity?>(null));
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
// Act
Result<Success> result = await _sut.DeleteItemAsync(libraryId.Value, track.Path, CancellationToken.None);
// Assert
Assert.True(result.IsSuccess);
await _mockTrackRepository.Received(1).DeleteByIdAsync(track.Id, Arg.Any<CancellationToken>());
await _mockAlbumRepository.DidNotReceive().DeleteByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
}
[Fact]
public async Task DeleteItemAsync_WhenNoTrackExistsAtPath_ShouldDoNothing()
{
// Arrange
LibraryId libraryId = _libraryIdFixture.Create();
_mockTrackRepository.GetByPathAsync(libraryId.Value, "/music/deleted.flac", Arg.Any<CancellationToken>())
.Returns(Result.From<TrackEntity?>(null));
// Act
Result<Success> result = await _sut.DeleteItemAsync(libraryId.Value, "/music/deleted.flac", CancellationToken.None);
// Assert
Assert.True(result.IsSuccess);
await _mockMusicArtworkRepository.DidNotReceive().DeleteByOwnerAsync(Arg.Any<MusicArtworkOwnerType>(), Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockTrackRepository.DidNotReceive().DeleteByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
[Fact]
public async Task DeleteItemAsync_WhenGetTrackFails_ShouldReturnFailure()
{
// Arrange
LibraryId libraryId = _libraryIdFixture.Create();
Error error = Error.Failure("Database.Error", "Failed to get the track");
_mockTrackRepository.GetByPathAsync(libraryId.Value, "/music/deleted.flac", Arg.Any<CancellationToken>())
.Returns(error);
// Act
Result<Success> result = await _sut.DeleteItemAsync(libraryId.Value, "/music/deleted.flac", CancellationToken.None);
// Assert
Assert.True(result.IsFailure);
Assert.Equal(error, result.FirstError);
await _mockTrackRepository.DidNotReceive().DeleteByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task DeleteItemAsync_WhenDeleteTrackArtworkFails_ShouldReturnFailure()
{
// Arrange
LibraryId libraryId = _libraryIdFixture.Create();
Guid albumId = Guid.NewGuid();
TrackEntity track = _trackEntityFixture.Create(albumId: albumId, path: "/music/deleted.flac");
Error error = Error.Failure("Database.Error", "Failed to delete the track artwork");
_mockTrackRepository.GetByPathAsync(libraryId.Value, track.Path, Arg.Any<CancellationToken>())
.Returns(Result.From<TrackEntity?>(track));
_mockMusicArtworkRepository.DeleteByOwnerAsync(MusicArtworkOwnerType.Track, track.Id, Arg.Any<CancellationToken>())
.Returns(error);
// Act
Result<Success> result = await _sut.DeleteItemAsync(libraryId.Value, track.Path, CancellationToken.None);
// Assert
Assert.True(result.IsFailure);
Assert.Equal(error, result.FirstError);
await _mockTrackRepository.DidNotReceive().DeleteByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
[Fact]
public async Task DeleteItemAsync_WhenDeleteAlbumArtworkFails_ShouldReturnFailure()
{
// Arrange
LibraryId libraryId = _libraryIdFixture.Create();
Guid albumId = Guid.NewGuid();
Guid artistId = Guid.NewGuid();
TrackEntity track = _trackEntityFixture.Create(albumId: albumId, path: "/music/deleted.flac");
AlbumEntity album = _albumEntityFixture.Create(id: albumId, artistId: artistId, includeTracks: false);
Error error = Error.Failure("Database.Error", "Failed to delete the album artwork");
_mockTrackRepository.GetByPathAsync(libraryId.Value, track.Path, Arg.Any<CancellationToken>())
.Returns(Result.From<TrackEntity?>(track));
_mockMusicArtworkRepository.DeleteByOwnerAsync(MusicArtworkOwnerType.Track, track.Id, Arg.Any<CancellationToken>())
.Returns(Result.Deleted);
_mockTrackRepository.DeleteByIdAsync(track.Id, Arg.Any<CancellationToken>())
.Returns(Result.From(Result.Deleted));
_mockAlbumRepository.GetByIdAsync(albumId, Arg.Any<bool>(), Arg.Any<bool>(), Arg.Any<CancellationToken>())
.Returns(Result.From<AlbumEntity?>(album));
_mockTrackRepository.GetByAlbumIdAsync(albumId, Arg.Any<CancellationToken>())
.Returns(Result.From<IReadOnlyList<TrackEntity>>([track]));
_mockMusicArtworkRepository.DeleteByOwnerAsync(MusicArtworkOwnerType.Album, albumId, Arg.Any<CancellationToken>())
.Returns(error);
// Act
Result<Success> result = await _sut.DeleteItemAsync(libraryId.Value, track.Path, CancellationToken.None);
// Assert
Assert.True(result.IsFailure);
Assert.Equal(error, result.FirstError);
await _mockAlbumRepository.DidNotReceive().DeleteByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
[Fact]
public async Task DeleteItemAsync_WhenDeleteArtistArtworkFails_ShouldReturnFailure()
{
// Arrange
LibraryId libraryId = _libraryIdFixture.Create();
Guid albumId = Guid.NewGuid();
Guid artistId = Guid.NewGuid();
TrackEntity track = _trackEntityFixture.Create(albumId: albumId, path: "/music/deleted.flac");
AlbumEntity album = _albumEntityFixture.Create(id: albumId, artistId: artistId, includeTracks: false);
Error error = Error.Failure("Database.Error", "Failed to delete the artist artwork");
_mockTrackRepository.GetByPathAsync(libraryId.Value, track.Path, Arg.Any<CancellationToken>())
.Returns(Result.From<TrackEntity?>(track));
_mockMusicArtworkRepository.DeleteByOwnerAsync(MusicArtworkOwnerType.Track, track.Id, Arg.Any<CancellationToken>())
.Returns(Result.Deleted);
_mockTrackRepository.DeleteByIdAsync(track.Id, Arg.Any<CancellationToken>())
.Returns(Result.From(Result.Deleted));
_mockAlbumRepository.GetByIdAsync(albumId, Arg.Any<bool>(), Arg.Any<bool>(), Arg.Any<CancellationToken>())
.Returns(Result.From<AlbumEntity?>(album));
_mockTrackRepository.GetByAlbumIdAsync(albumId, Arg.Any<CancellationToken>())
.Returns(Result.From<IReadOnlyList<TrackEntity>>([track]));
_mockMusicArtworkRepository.DeleteByOwnerAsync(MusicArtworkOwnerType.Album, albumId, Arg.Any<CancellationToken>())
.Returns(Result.Deleted);
_mockAlbumRepository.DeleteByIdAsync(albumId, Arg.Any<CancellationToken>())
.Returns(Result.From(Result.Deleted));
_mockAlbumRepository.GetByArtistIdAsync(artistId, Arg.Any<CancellationToken>())
.Returns(Result.From<IReadOnlyList<AlbumEntity>>([album]));
_mockMusicArtworkRepository.DeleteByOwnerAsync(MusicArtworkOwnerType.Artist, artistId, Arg.Any<CancellationToken>())
.Returns(error);
// Act
Result<Success> result = await _sut.DeleteItemAsync(libraryId.Value, track.Path, CancellationToken.None);
// Assert
Assert.True(result.IsFailure);
Assert.Equal(error, result.FirstError);
await _mockArtistRepository.DidNotReceive().DeleteByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
}
@@ -1,23 +1,20 @@
#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.BookLibrary;
using Lumina.Application.Common.DataAccess.Repositories.MediaLibrary;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Core.MediaLibrary.Management.Deletion;
using Lumina.Application.Core.MediaLibrary.Management.Events;
using Lumina.Application.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Artwork;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaLibrary.Management;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
using Lumina.Domain.Common.Exceptions;
using Lumina.Domain.Common.Primitives;
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryAggregate.ValueObjects;
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.Events;
using Lumina.Domain.Fixtures.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryAggregate.ValueObjects;
using Lumina.Domain.Fixtures.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.Events;
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
using Microsoft.Extensions.Logging;
using NSubstitute;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
@@ -32,13 +29,11 @@ namespace Lumina.Application.UnitTests.Core.MediaLibrary.Management.Events;
public class LibraryMediaItemDeletedDomainEventHandlerTests
{
private readonly IUnitOfWork _mockUnitOfWork;
private readonly IBookRepository _mockBookRepository;
private readonly ILibraryRepository _mockLibraryRepository;
private readonly IBookArtworkService _mockBookArtworkService;
private readonly IMediaLibraryItemDeletionStrategy _mockDeletionStrategy;
private readonly ILogger<LibraryMediaItemDeletedDomainEventHandler> _mockLogger;
private readonly LibraryMediaItemDeletedDomainEventHandler _sut;
private readonly LibraryIdFixture _libraryIdFixture = new();
private readonly BookEntityFixture _bookEntityFixture = new();
private readonly LibraryEntityFixture _libraryEntityFixture = new();
private readonly LibraryMediaItemDeletedDomainEventFixture _libraryMediaItemDeletedDomainEventFixture = new();
@@ -48,54 +43,26 @@ public class LibraryMediaItemDeletedDomainEventHandlerTests
public LibraryMediaItemDeletedDomainEventHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockBookRepository = Substitute.For<IBookRepository>();
_mockLibraryRepository = Substitute.For<ILibraryRepository>();
_mockUnitOfWork.BookRepository.Returns(_mockBookRepository);
_mockUnitOfWork.LibraryRepository.Returns(_mockLibraryRepository);
_mockBookArtworkService = Substitute.For<IBookArtworkService>();
_mockDeletionStrategy = Substitute.For<IMediaLibraryItemDeletionStrategy>();
_mockLogger = Substitute.For<ILogger<LibraryMediaItemDeletedDomainEventHandler>>();
_sut = new LibraryMediaItemDeletedDomainEventHandler(_mockUnitOfWork, _mockBookArtworkService, _mockLogger);
_sut = new LibraryMediaItemDeletedDomainEventHandler(_mockUnitOfWork, [_mockDeletionStrategy], _mockLogger);
}
[Fact]
public async Task HandleAsync_WhenBookExistsAtPath_ShouldDeleteTheBookAndItsArtwork()
public async Task HandleAsync_WhenDeletionStrategyExistsForTheLibraryType_ShouldDelegateToIt()
{
// Arrange
LibraryId libraryId = _libraryIdFixture.Create();
BookEntity book = _bookEntityFixture.Create(path: "/books/deleted.epub");
LibraryEntity library = _libraryEntityFixture.Create(id: libraryId.Value, title: "My Library");
_mockBookRepository.GetByPathAsync(libraryId.Value, book.Path, Arg.Any<CancellationToken>())
.Returns(Result.From<BookEntity?>(book));
LibraryEntity library = _libraryEntityFixture.Create(id: libraryId.Value, libraryType: LibraryType.Book);
_mockLibraryRepository.GetByIdAsync(libraryId.Value, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(library));
_mockBookRepository.GetAuthorsDisplayNamesByBookIdsAsync(Arg.Any<IReadOnlyCollection<Guid>>(), Arg.Any<CancellationToken>())
.Returns(Result.From<IReadOnlyDictionary<Guid, string?>>(new Dictionary<Guid, string?> { [book.Id] = "Frank Herbert" }));
_mockBookRepository.DeleteByIdAsync(book.Id, Arg.Any<CancellationToken>())
.Returns(Result.From(Result.Deleted));
_mockBookArtworkService.DeleteBookArtwork(Arg.Any<Guid>(), Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>())
.Returns(Result.Deleted);
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
LibraryMediaItemDeletedDomainEvent domainEvent = _libraryMediaItemDeletedDomainEventFixture.Create(libraryId: libraryId, path: book.Path);
// Act
await _sut.HandleAsync(domainEvent, CancellationToken.None);
// Assert
_mockBookArtworkService.Received(1).DeleteBookArtwork(libraryId.Value, book.Id, "My Library", "Frank Herbert", book.Title);
await _mockBookRepository.Received(1).DeleteByIdAsync(book.Id, Arg.Any<CancellationToken>());
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
}
[Fact]
public async Task HandleAsync_WhenNoBookExistsAtPath_ShouldDoNothing()
{
// Arrange
LibraryId libraryId = _libraryIdFixture.Create();
_mockBookRepository.GetByPathAsync(libraryId.Value, "/books/deleted.epub", Arg.Any<CancellationToken>())
.Returns(Result.From<BookEntity?>(null));
_mockDeletionStrategy.SupportedLibraryType.Returns(LibraryType.Book);
_mockDeletionStrategy.DeleteItemAsync(libraryId.Value, "/books/deleted.epub", Arg.Any<CancellationToken>())
.Returns(Result.Success);
LibraryMediaItemDeletedDomainEvent domainEvent = _libraryMediaItemDeletedDomainEventFixture.Create(libraryId: libraryId, path: "/books/deleted.epub");
@@ -103,48 +70,35 @@ public class LibraryMediaItemDeletedDomainEventHandlerTests
await _sut.HandleAsync(domainEvent, CancellationToken.None);
// Assert
_mockBookArtworkService.DidNotReceive().DeleteBookArtwork(Arg.Any<Guid>(), Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>());
await _mockBookRepository.DidNotReceive().DeleteByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
await _mockDeletionStrategy.Received(1).DeleteItemAsync(libraryId.Value, "/books/deleted.epub", Arg.Any<CancellationToken>());
}
[Fact]
public async Task HandleAsync_WhenDeleteArtworkFails_ShouldStillDeleteTheBook()
public async Task HandleAsync_WhenNoDeletionStrategyExistsForTheLibraryType_ShouldNotDelete()
{
// Arrange
LibraryId libraryId = _libraryIdFixture.Create();
BookEntity book = _bookEntityFixture.Create(path: "/books/deleted.epub");
LibraryEntity library = _libraryEntityFixture.Create(id: libraryId.Value, title: "My Library");
_mockBookRepository.GetByPathAsync(libraryId.Value, book.Path, Arg.Any<CancellationToken>())
.Returns(Result.From<BookEntity?>(book));
LibraryEntity library = _libraryEntityFixture.Create(id: libraryId.Value, libraryType: LibraryType.Music);
_mockLibraryRepository.GetByIdAsync(libraryId.Value, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(library));
_mockBookRepository.GetAuthorsDisplayNamesByBookIdsAsync(Arg.Any<IReadOnlyCollection<Guid>>(), Arg.Any<CancellationToken>())
.Returns(Result.From<IReadOnlyDictionary<Guid, string?>>(new Dictionary<Guid, string?> { [book.Id] = "Frank Herbert" }));
_mockBookArtworkService.DeleteBookArtwork(Arg.Any<Guid>(), Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>())
.Returns(Error.Failure("Artwork.DeleteFailed", "Failed to delete the stored artwork"));
_mockBookRepository.DeleteByIdAsync(book.Id, Arg.Any<CancellationToken>())
.Returns(Result.From(Result.Deleted));
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockDeletionStrategy.SupportedLibraryType.Returns(LibraryType.Book);
LibraryMediaItemDeletedDomainEvent domainEvent = _libraryMediaItemDeletedDomainEventFixture.Create(libraryId: libraryId, path: book.Path);
LibraryMediaItemDeletedDomainEvent domainEvent = _libraryMediaItemDeletedDomainEventFixture.Create(libraryId: libraryId, path: "/music/deleted.flac");
// Act
await _sut.HandleAsync(domainEvent, CancellationToken.None);
// Assert
// a failing artwork deletion must not prevent the book from being removed, so the deletion is only logged
await _mockBookRepository.Received(1).DeleteByIdAsync(book.Id, Arg.Any<CancellationToken>());
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
await _mockDeletionStrategy.DidNotReceive().DeleteItemAsync(Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task HandleAsync_WhenGetBookFails_ShouldThrowEventualConsistencyException()
public async Task HandleAsync_WhenGetLibraryFails_ShouldThrowEventualConsistencyException()
{
// Arrange
LibraryId libraryId = _libraryIdFixture.Create();
Error error = Error.Failure("Database.Error", "Failed to get the book");
_mockBookRepository.GetByPathAsync(libraryId.Value, "/books/deleted.epub", Arg.Any<CancellationToken>())
Error error = Error.Failure("Database.Error", "Failed to get the library");
_mockLibraryRepository.GetByIdAsync(libraryId.Value, cancellationToken: Arg.Any<CancellationToken>())
.Returns(error);
LibraryMediaItemDeletedDomainEvent domainEvent = _libraryMediaItemDeletedDomainEventFixture.Create(libraryId: libraryId, path: "/books/deleted.epub");
@@ -155,29 +109,39 @@ public class LibraryMediaItemDeletedDomainEventHandlerTests
// Assert
Assert.Equal(error, exception.EventualConsistencyError);
await _mockBookRepository.DidNotReceive().DeleteByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockDeletionStrategy.DidNotReceive().DeleteItemAsync(Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task HandleAsync_WhenDeleteBookFails_ShouldThrowEventualConsistencyException()
public async Task HandleAsync_WhenLibraryDoesNotExist_ShouldThrowEventualConsistencyException()
{
// Arrange
LibraryId libraryId = _libraryIdFixture.Create();
BookEntity book = _bookEntityFixture.Create(path: "/books/deleted.epub");
LibraryEntity library = _libraryEntityFixture.Create(id: libraryId.Value, title: "My Library");
_mockBookRepository.GetByPathAsync(libraryId.Value, book.Path, Arg.Any<CancellationToken>())
.Returns(Result.From<BookEntity?>(book));
_mockLibraryRepository.GetByIdAsync(libraryId.Value, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(null));
LibraryMediaItemDeletedDomainEvent domainEvent = _libraryMediaItemDeletedDomainEventFixture.Create(libraryId: libraryId, path: "/books/deleted.epub");
// Act & Assert
await Assert.ThrowsAsync<EventualConsistencyException>(
() => _sut.HandleAsync(domainEvent, CancellationToken.None).AsTask());
await _mockDeletionStrategy.DidNotReceive().DeleteItemAsync(Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task HandleAsync_WhenDeletionFails_ShouldThrowEventualConsistencyException()
{
// Arrange
LibraryId libraryId = _libraryIdFixture.Create();
LibraryEntity library = _libraryEntityFixture.Create(id: libraryId.Value, libraryType: LibraryType.Book);
Error error = Error.Failure("Database.Error", "Failed to delete the item");
_mockLibraryRepository.GetByIdAsync(libraryId.Value, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(library));
_mockBookRepository.GetAuthorsDisplayNamesByBookIdsAsync(Arg.Any<IReadOnlyCollection<Guid>>(), Arg.Any<CancellationToken>())
.Returns(Result.From<IReadOnlyDictionary<Guid, string?>>(new Dictionary<Guid, string?> { [book.Id] = "Frank Herbert" }));
_mockBookArtworkService.DeleteBookArtwork(Arg.Any<Guid>(), Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>())
.Returns(Result.Deleted);
Error error = Error.Failure("Database.Error", "Failed to delete the book");
_mockBookRepository.DeleteByIdAsync(book.Id, Arg.Any<CancellationToken>())
_mockDeletionStrategy.SupportedLibraryType.Returns(LibraryType.Book);
_mockDeletionStrategy.DeleteItemAsync(libraryId.Value, "/books/deleted.epub", Arg.Any<CancellationToken>())
.Returns(error);
LibraryMediaItemDeletedDomainEvent domainEvent = _libraryMediaItemDeletedDomainEventFixture.Create(libraryId: libraryId, path: book.Path);
LibraryMediaItemDeletedDomainEvent domainEvent = _libraryMediaItemDeletedDomainEventFixture.Create(libraryId: libraryId, path: "/books/deleted.epub");
// Act
EventualConsistencyException exception = await Assert.ThrowsAsync<EventualConsistencyException>(
@@ -185,6 +149,5 @@ public class LibraryMediaItemDeletedDomainEventHandlerTests
// Assert
Assert.Equal(error, exception.EventualConsistencyError);
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
}