#region ========================================================================= USING =====================================================================================
using Nemera.Infrastructure.Core.Plugins;
using Nemera.Plugins.Contracts.Core.Plugins;
using NSubstitute;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Nemera.Infrastructure.UnitTests.Core.Plugins;
///
/// Contains unit tests for the class.
///
[ExcludeFromCodeCoverage]
public class PluginManagerTests
{
private PluginManager _sut = new([], [], new HashSet());
[Fact]
public void GetPlugins_WhenCalled_ShouldReturnAllLoadedPlugins()
{
// Arrange
IPlugin firstPlugin = CreatePlugin(Guid.NewGuid(), "First Plugin");
IPlugin secondPlugin = CreatePlugin(Guid.NewGuid(), "Second Plugin");
_sut = new([firstPlugin, secondPlugin], [], new HashSet());
// Act
IReadOnlyList result = _sut.GetPlugins();
// Assert
Assert.Equal(2, result.Count);
Assert.Contains(firstPlugin, result);
Assert.Contains(secondPlugin, result);
}
[Fact]
public void GetPlugins_WhenNoPluginsWereLoaded_ShouldReturnEmptyList()
{
// Arrange
_sut = new([], [], new HashSet());
// Act
IReadOnlyList result = _sut.GetPlugins();
// Assert
Assert.Empty(result);
}
[Fact]
public void GetPlugin_WhenPluginWithIdWasLoaded_ShouldReturnThePlugin()
{
// Arrange
Guid pluginId = Guid.NewGuid();
IPlugin expectedPlugin = CreatePlugin(pluginId, "Expected Plugin");
_sut = new([expectedPlugin, CreatePlugin(Guid.NewGuid(), "Other Plugin")], [], new HashSet());
// Act
IPlugin? result = _sut.GetPlugin(pluginId);
// Assert
Assert.Same(expectedPlugin, result);
}
[Fact]
public void GetPlugin_WhenPluginWithIdWasNotLoaded_ShouldReturnNull()
{
// Arrange
_sut = new([CreatePlugin(Guid.NewGuid(), "Loaded Plugin")], [], new HashSet());
// Act
IPlugin? result = _sut.GetPlugin(Guid.NewGuid());
// Assert
Assert.Null(result);
}
[Fact]
public void IsOfficial_WhenPluginIsInTheOfficialSet_ShouldReturnTrue()
{
// Arrange
Guid pluginId = Guid.NewGuid();
_sut = new([CreatePlugin(pluginId, "Official Plugin")], [], new HashSet { pluginId });
// Act
bool result = _sut.IsOfficial(pluginId);
// Assert
Assert.True(result);
}
[Fact]
public void IsOfficial_WhenPluginIsNotInTheOfficialSet_ShouldReturnFalse()
{
// Arrange
Guid pluginId = Guid.NewGuid();
_sut = new([CreatePlugin(pluginId, "Unofficial Plugin")], [], new HashSet());
// Act
bool result = _sut.IsOfficial(pluginId);
// Assert
Assert.False(result);
}
///
/// Creates a mocked plugin with the provided identity.
///
/// The Id of the plugin.
/// The name of the plugin.
/// The created plugin mock.
private static IPlugin CreatePlugin(Guid id, string name)
{
IPlugin plugin = Substitute.For();
plugin.Id.Returns(id);
plugin.Name.Returns(name);
return plugin;
}
}