using System.Security.Claims;
using adas_core.Application.Repositories.Interfaces;
using adas_core.Application.Services;
using adas_core.Application.Services.Interfaces;
using adas_core.Domain.Enums;
using adas_core.Domain.Models.AppSettings;
using adas_core.Domain.Models.Masters;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using MongoDB.Bson;
using Moq;
namespace adas_core.Test.Services;
[TestFixture]
public class MasterListServiceTest
{
///
/// Initializes all required mock dependencies and constructs a instance
/// for use in unit tests. Configures the HTTP context with a test user principal, registers the master list repository
/// in the service provider, and supplies default API settings.
///
[SetUp]
public void Setup()
{
_loggerMock = new Mock>>();
_serviceProviderMock = new Mock();
_clientMessageServiceMock = new Mock();
_subscribersServiceMock = new Mock();
_unitServiceMock = new Mock();
_displayServiceMock = new Mock();
_repositoryMock = new Mock>();
_patientServiceMock = new Mock();
_dischargeServiceMock = new Mock();
_admissionServiceMock = new Mock();
_apiSettingsMock = new Mock>();
var userClaims = new ClaimsPrincipal(new ClaimsIdentity([
new Claim(ClaimTypes.Name, "TestUser")
], "mock"));
var httpContextMock = new DefaultHttpContext
{
User = userClaims
};
_httpContextAccessorMock.Setup(accessor => accessor.HttpContext)
.Returns(httpContextMock);
var apiSettings = new ApiSettings
{
PathToDisplayAssets = ["path/to/assets"]
};
_apiSettingsMock.Setup(ap => ap.Value).Returns(apiSettings);
_serviceProviderMock.Setup(sp => sp.GetService(typeof(IMasterListRepository)))
.Returns(_repositoryMock.Object);
_service = new MasterListService(
_loggerMock.Object,
_serviceProviderMock.Object,
new Lazy(() => _clientMessageServiceMock.Object),
_subscribersServiceMock.Object,
new Lazy(() => _unitServiceMock.Object),
new Lazy(() => _displayServiceMock.Object),
new Lazy(() => _patientServiceMock.Object),
new Lazy(() => _dischargeServiceMock.Object),
new Lazy(() => _admissionServiceMock.Object),
_apiSettingsMock.Object,
_httpContextAccessorMock.Object,
_auditServiceMock.Object
);
}
private Mock>> _loggerMock = null!;
private Mock _serviceProviderMock = null!;
private Mock _clientMessageServiceMock = null!;
private Mock _subscribersServiceMock = null!;
private Mock _unitServiceMock = null!;
private Mock _displayServiceMock = null!;
private Mock _patientServiceMock = null!;
private Mock _dischargeServiceMock = null!;
private Mock _admissionServiceMock = null!;
private Mock> _repositoryMock = null!;
private Mock> _apiSettingsMock = null!;
private readonly Mock _httpContextAccessorMock = new();
private readonly Mock _auditServiceMock = new();
private MasterListService _service = null!;
///
/// Verifies that DeleteMasterListById invokes the repository's Delete method once with the provided identifier when the master list is found.
///
[Test]
public async Task DeleteMasterListById_ShouldCallRepositoryDelete_WhenMasterListFound()
{
// Arrange
var id = ObjectId.GenerateNewId();
var masterList = new MasterList { Id = id };
_repositoryMock.Setup(repo => repo.FindById(It.IsAny(), It.IsAny()))
.ReturnsAsync(masterList);
_repositoryMock.Setup(repo => repo.Delete(It.IsAny()))
.Returns(Task.CompletedTask);
_unitServiceMock.Setup(c => c.CountUnitsByMasterListId(It.IsAny(), It.IsAny()))
.ReturnsAsync(0);
// Act
await _service.DeleteMasterListById(id);
// Assert
_repositoryMock.Verify(repo => repo.Delete(id), Times.Once);
}
// [Test]
// public async Task GetAllMasterList_ShouldReturnAllMasterLists()
// {
// // Arrange
// var masterLists = new List { new MasterList(), new MasterList() };
// _repositoryMock.Setup(repo => repo.GetAll())
// .ReturnsAsync(masterLists);
//
// // Act
// var result = await _service.GetAllMasterList();
//
// // Assert
// Assert.That(masterLists, Is.EqualTo(result));
// }
///
/// Verifies that the service returns the master list when it is found by the specified id.
///
[Test]
public async Task GetMasterListById_ShouldReturnMasterList_WhenFound()
{
// Arrange
var id = ObjectId.GenerateNewId();
var masterList = new MasterList { Id = id };
_repositoryMock.Setup(repo => repo.FindById(It.IsAny(), It.IsAny()))
.ReturnsAsync(masterList);
// Act
var result = await _service.GetMasterListById(id, LocaleEnum.Default);
// Assert
Assert.That(masterList, Is.EqualTo(result));
}
///
/// Verifies that GetMasterListByName returns the matching when a master list with the specified name is found in the repository.
///
[Test]
public async Task GetMasterListByName_ShouldReturnMasterList_WhenFound()
{
// Arrange
var name = "TestName";
var masterList = new MasterList { Id = ObjectId.GenerateNewId(), Name = name };
_repositoryMock.Setup(repo => repo.FindByName(It.IsAny()))
.ReturnsAsync(masterList);
// Act
var result = await _service.GetMasterListByName(name);
// Assert
Assert.That(masterList, Is.EqualTo(result));
}
///
/// Verifies that the InsertMasterList service method returns the same instance that was inserted, ensuring the service correctly retrieves the inserted entity by its identifier after persistence.
///
[Test]
public async Task InsertMasterList_ShouldReturnInsertedMasterList()
{
// Arrange
var masterList = new MasterList { Id = ObjectId.GenerateNewId() };
_repositoryMock.Setup(repo => repo.InsertOneAsync(It.IsAny()))
.Returns(Task.CompletedTask);
_repositoryMock.Setup(repo => repo.FindById(It.IsAny()))
.ReturnsAsync(masterList);
// Act
var result = await _service.InsertMasterList(masterList);
// Assert
Assert.That(masterList, Is.EqualTo(result));
}
///
/// Verifies that UpdateMasterList returns the updated master list when the repository successfully completes the update and finds the entity by id.
///
/// A representing the asynchronous test execution.
[Test]
public async Task UpdateMasterList_ShouldReturnUpdatedMasterList()
{
// Arrange
var masterList = new MasterList { Id = ObjectId.GenerateNewId() };
_repositoryMock.Setup(repo => repo.Update(It.IsAny()))
.Returns(Task.CompletedTask);
_repositoryMock.Setup(repo => repo.FindById(It.IsAny(), It.IsAny()))
.ReturnsAsync(masterList);
// Act
var result = await _service.UpdateMasterList(masterList);
// Assert
Assert.That(masterList, Is.EqualTo(result));
}
}