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.MongoModels; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using Moq; namespace adas_core.Test.Services; [TestFixture] [NonParallelizable] internal class HistoricalConfigChangesServiceTest { /// /// Initializes the mock repository, mock logger, and the instance under test prior to each unit test execution. /// [SetUp] public void Setup() { _mockRepo = new Mock(); _mockLogger = new Mock>(); _service = new HistoricalConfigChangesService(_mockRepo.Object, _mockLogger.Object, _httpContextAccessor.Object, _auditService.Object); } private HistoricalConfigChangesService _service; private Mock _mockRepo; private Mock> _mockLogger; private readonly Mock _httpContextAccessor = new(); private readonly Mock _auditService = new(); /// /// Verifies that returns the last configuration change retrieved from the repository for the specified configuration type. /// /// The type of configuration whose last historical changes are being retrieved. /// A task that represents the asynchronous test execution. [Test] public async Task FindLastConfigChanges_ReturnsLastConfigChange() { // Arrange var configType = DisplayConfigEnums.ConfigTypes.ObservationCfg; var mockResult = new List { new() // Mock object with desired properties }; _mockRepo.Setup(r => r.FindLastHistoricalConfigChangesByType(configType, 10)).ReturnsAsync(mockResult); // Act var result = await _service.FindLastConfigChanges(configType); // Assert Assert.That(result, Is.Not.Null); // Further assertions based on the expected result } /// /// Verifies that the service returns null when the underlying repository throws an exception while inserting a entity. /// [Test] public async Task InsertOne_WhenExceptionOccurs_ReturnsNull() { // Arrange var mockChange = new HistoricalConfigChanges(); _mockRepo.Setup(r => r.InsertOneAsync(mockChange)).Throws(new Exception()); // Act var result = await _service.InsertOne(mockChange); // Assert Assert.That(result, Is.Null); } /// /// Verifies that the service's GetAll method returns all historical configuration changes retrieved from the repository. /// [Test] public async Task GetAll_ReturnsAllConfigChanges() { // Arrange var mockResult = new List { new(), new() }; _mockRepo.Setup(r => r.FindAll()).ReturnsAsync(mockResult); // Act var result = await _service.GetAll(); // Assert Assert.That(result, Has.Count.EqualTo(2)); } /// /// Verifies that the service's GetByType method returns the expected number of historical configuration changes for the specified configuration type. /// [Test] public async Task GetByType_ReturnsCorrectAmountOfConfigs() { // Arrange var configType = DisplayConfigEnums.ConfigTypes.ObservationCfg; var mockResult = new List { new(), new(), new() }; _mockRepo.Setup(r => r.FindLastHistoricalConfigChangesByType(configType, 3)).ReturnsAsync(mockResult); // Act var result = await _service.GetByType(configType, 3); // Assert Assert.That(result, Has.Count.EqualTo(3)); } /// /// Verifies that the service returns the expected number of historical configuration changes for a given user, /// confirming the repository result is correctly forwarded to the caller. /// [Test] public async Task GetByUser_ReturnsCorrectConfigs() { // Arrange var user = "TestUser"; var mockResult = new List { new(), new() }; _mockRepo.Setup(r => r.FindLastHistoricalConfigChangesByUser(user, null, 2)).ReturnsAsync(mockResult); // Act var result = await _service.GetByUser(user, null, 2); // Assert Assert.That(result, Has.Count.EqualTo(2)); } /// /// Verifies that the service returns null when an exception is thrown while updating a historical configuration change, ensuring graceful error handling. /// [Test] public async Task UpdateHistoricalConfigChange_WhenExceptionOccurs_ReturnsNull() { // Arrange var mockChange = new HistoricalConfigChanges(); _mockRepo.Setup(r => r.Update(mockChange)).Throws(new Exception()); // Act var result = await _service.UpdateHistoricalConfigChange(mockChange); // Assert Assert.That(result, Is.Null); } }