#region ========================================================================= USING =====================================================================================
using Nemera.Application.Common.DataAccess.Entities.Themes;
using Nemera.Application.Common.DataAccess.Repositories.Themes;
using Nemera.Application.Common.DataAccess.UoW;
using Nemera.Application.Common.DTO.Filtering;
using Nemera.Application.Common.DTO.Pagination;
using Nemera.Application.Common.Infrastructure.Models.DTO.Themes;
using Nemera.Application.Common.Infrastructure.Themes;
using Nemera.Application.Fixtures.Common.DataAccess.Entities.Themes;
using Nemera.Application.Fixtures.Common.DTO.Pagination;
using Nemera.Application.Fixtures.Common.Infrastructure.Models.DTO.Themes;
using Nemera.Domain.Common.Primitives;
using Nemera.Domain.SharedKernel.Common.Enums.Themes;
using Nemera.Infrastructure.Core.Themes;
using Microsoft.Extensions.Logging;
using NSubstitute;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Nemera.Infrastructure.UnitTests.Core.Themes;
///
/// Contains unit tests for the class.
///
[ExcludeFromCodeCoverage]
public class ThemeSynchronizerTests : IDisposable
{
private readonly IThemeService _mockThemeService;
private readonly IUnitOfWork _mockUnitOfWork;
private readonly IThemeRepository _mockThemeRepository;
private readonly ILogger _mockLogger;
private readonly ThemeEntityFixture _themeEntityFixture = new();
private readonly ThemeManifestDtoFixture _themeManifestDtoFixture = new();
private readonly PaginatedResultDtoFixture _paginatedResultDtoFixture = new();
private readonly string _temporaryDirectoryPath;
///
/// Initializes a new instance of the class.
///
public ThemeSynchronizerTests()
{
_mockThemeService = Substitute.For();
_mockUnitOfWork = Substitute.For();
_mockThemeRepository = Substitute.For();
_mockUnitOfWork.ThemeRepository.Returns(_mockThemeRepository);
_mockUnitOfWork.SaveChangesAsync(Arg.Any()).Returns(Result.Success);
_mockLogger = Substitute.For();
_mockLogger.IsEnabled(Arg.Any()).Returns(true);
_mockThemeService.GetBundledThemeArchivePaths().Returns([]);
_mockThemeService.HasThemePack(Arg.Any()).Returns(true);
_mockThemeService.DefaultThemeId.Returns("nemera-default");
_mockThemeRepository.GetAllAsync(cancellationToken: Arg.Any()).Returns(Result.From(_paginatedResultDtoFixture.Create(data: [], currentPage: 1, perPage: 0, count: 0, numberOfPages: 1)));
_mockThemeRepository.InsertAsync(Arg.Any(), Arg.Any()).Returns(Result.Created);
_mockThemeRepository.UpdateAsync(Arg.Any(), Arg.Any()).Returns(Result.Updated);
_mockThemeRepository.DeleteByIdAsync(Arg.Any(), Arg.Any()).Returns(Result.Deleted);
_temporaryDirectoryPath = Path.Combine(Path.GetTempPath(), $"nemera-theme-sync-{Guid.NewGuid():N}");
Directory.CreateDirectory(_temporaryDirectoryPath);
}
[Fact]
public async Task SynchronizeAsync_WhenTheInstalledThemesCannotBeRead_ShouldLogAndReturn()
{
// Arrange
_mockThemeRepository.GetAllAsync(cancellationToken: Arg.Any())
.Returns(Error.Failure("Themes.NotFound", "Failed to read the installed themes"));
_mockThemeService.GetBundledThemeArchivePaths().Returns(["bundled.zip"]);
// Act
await ThemeSynchronizer.SynchronizeAsync(_mockThemeService, _mockUnitOfWork, _mockLogger, CancellationToken.None);
// Assert
await _mockThemeService.DidNotReceive().ReadManifestFromArchiveAsync(Arg.Any(), Arg.Any());
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any());
}
[Fact]
public async Task SynchronizeAsync_WhenLoggingIsDisabledAndTheInstalledThemesCannotBeRead_ShouldNotLogAWarning()
{
// Arrange
_mockLogger.IsEnabled(Arg.Any()).Returns(false);
_mockThemeRepository.GetAllAsync(cancellationToken: Arg.Any())
.Returns(Error.Failure("Themes.NotFound", "Failed to read the installed themes"));
_mockThemeService.GetBundledThemeArchivePaths().Returns(["bundled.zip"]);
// Act
await ThemeSynchronizer.SynchronizeAsync(_mockThemeService, _mockUnitOfWork, _mockLogger, CancellationToken.None);
// Assert
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any());
_mockLogger.DidNotReceive().Log(
LogLevel.Warning,
Arg.Any(),
Arg.Any