Files

93 lines
4.0 KiB
C#

#region ========================================================================= USING =====================================================================================
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Nemera.Application.Common.DataAccess.Entities.Themes;
using Nemera.Application.Common.Infrastructure.Models.DTO.Themes;
using Nemera.Application.Common.Infrastructure.Themes;
using Nemera.Application.Common.Infrastructure.Time;
using Nemera.Application.Fixtures.Common.Infrastructure.Models.DTO.Themes;
using Nemera.DataAccess.Core.UoW;
using Nemera.Domain.Common.Primitives;
using Nemera.Infrastructure.Core.Themes;
using NSubstitute;
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Nemera.Infrastructure.IntegrationTests.Core.Themes;
/// <summary>
/// Contains integration tests for the <see cref="ThemeSynchronizer"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class ThemeSynchronizerTests : IDisposable
{
private readonly DbContextOptions<NemeraDbContext> _dbContextOptions;
private readonly NemeraDbContext _dbContext;
private readonly UnitOfWork _unitOfWork;
private readonly IThemeService _mockThemeService;
private readonly ILogger _mockLogger;
private readonly ThemeManifestDtoFixture _themeManifestDtoFixture = new();
private readonly string _archivePath;
/// <summary>
/// Initializes a new instance of the <see cref="ThemeSynchronizerTests"/> class.
/// </summary>
public ThemeSynchronizerTests()
{
_dbContextOptions = new DbContextOptionsBuilder<NemeraDbContext>()
.UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
.Options;
_dbContext = new NemeraDbContext(_dbContextOptions);
_unitOfWork = new UnitOfWork(_dbContext, Substitute.For<IDateTimeProvider>());
_mockThemeService = Substitute.For<IThemeService>();
_mockLogger = Substitute.For<ILogger>();
_mockLogger.IsEnabled(Arg.Any<LogLevel>()).Returns(true);
_archivePath = Path.Combine(Path.GetTempPath(), $"nemera-theme-sync-{Guid.NewGuid():N}.zip");
}
[Fact]
public async Task SynchronizeAsync_WhenANewBundledThemeIsInstalled_ShouldPersistTheActivatedTheme()
{
// Arrange
ThemeManifestDto manifest = _themeManifestDtoFixture.Create();
File.WriteAllText(_archivePath, "theme-pack");
_mockThemeService.GetBundledThemeArchivePaths().Returns([_archivePath]);
_mockThemeService.ReadManifestFromArchiveAsync(_archivePath, Arg.Any<CancellationToken>()).Returns(Result.From(manifest));
_mockThemeService.InstallAsync(Arg.Any<Stream>(), Arg.Any<CancellationToken>()).Returns(Result.From(manifest));
_mockThemeService.HasThemePack(Arg.Any<string>()).Returns(true);
_mockThemeService.DefaultThemeId.Returns(manifest.Id);
// Act
await ThemeSynchronizer.SynchronizeAsync(_mockThemeService, _unitOfWork, _mockLogger, CancellationToken.None);
// Assert
// Activating an inserted theme must not attempt an update, which the repository would report as a failed activation warning.
_mockLogger.DidNotReceive().Log(
LogLevel.Warning,
Arg.Any<EventId>(),
Arg.Any<object>(),
Arg.Any<Exception?>(),
Arg.Any<Func<object, Exception?, string>>());
// Read through a fresh context so the assertion proves SaveChanges wrote the activation, not only that the entity was tracked.
using (NemeraDbContext verificationContext = new(_dbContextOptions))
{
ThemeEntity persistedTheme = Assert.Single(verificationContext.Themes);
Assert.True(persistedTheme.IsCurrent);
}
}
/// <summary>
/// Releases the database context and removes the temporary archive file.
/// </summary>
public void Dispose()
{
_dbContext.Dispose();
if (File.Exists(_archivePath))
File.Delete(_archivePath);
}
}