rama creada apartir de master en j

This commit is contained in:
jrojas
2026-06-26 10:29:23 +02:00
parent 319fd3dfb0
commit c1517fda87
2810 changed files with 1927392 additions and 25392 deletions
@@ -13,14 +13,17 @@ namespace adas_core.Test.Services;
[NonParallelizable]
internal class HistoricalConfigChangesServiceTest
{
/// <summary>
/// Initializes the mock repository, mock logger, and the <see cref="HistoricalConfigChangesService"/> instance under test prior to each unit test execution.
/// </summary>
[SetUp]
public void Setup()
{
_mockRepo = new Mock<IHistoricalConfigChangesRepository>();
_mockLogger = new Mock<ILogger<HistoricalConfigChangesService>>();
_service = new HistoricalConfigChangesService(_mockRepo.Object, _mockLogger.Object, _httpContextAccessor.Object,
_auditService.Object);
}
public void Setup()
{
_mockRepo = new Mock<IHistoricalConfigChangesRepository>();
_mockLogger = new Mock<ILogger<HistoricalConfigChangesService>>();
_service = new HistoricalConfigChangesService(_mockRepo.Object, _mockLogger.Object, _httpContextAccessor.Object,
_auditService.Object);
}
private HistoricalConfigChangesService _service;
private Mock<IHistoricalConfigChangesRepository> _mockRepo;
@@ -29,108 +32,129 @@ internal class HistoricalConfigChangesServiceTest
private readonly Mock<ILocalAuditService> _auditService = new();
/// <summary>
/// Verifies that <see cref="FindLastConfigChanges"/> returns the last configuration change retrieved from the repository for the specified configuration type.
/// </summary>
/// <param name="configType">The type of configuration whose last historical changes are being retrieved.</param>
/// <returns>A task that represents the asynchronous test execution.</returns>
[Test]
public async Task FindLastConfigChanges_ReturnsLastConfigChange()
{
// Arrange
var configType = DisplayConfigEnums.ConfigTypes.ObservationCfg;
var mockResult = new List<HistoricalConfigChanges>
public async Task FindLastConfigChanges_ReturnsLastConfigChange()
{
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
}
// Arrange
var configType = DisplayConfigEnums.ConfigTypes.ObservationCfg;
var mockResult = new List<HistoricalConfigChanges>
{
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
}
/// <summary>
/// Verifies that the service returns <c>null</c> when the underlying repository throws an exception while inserting a <see cref="HistoricalConfigChanges"/> entity.
/// </summary>
[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);
}
[Test]
public async Task GetAll_ReturnsAllConfigChanges()
{
// Arrange
var mockResult = new List<HistoricalConfigChanges>
public async Task InsertOne_WhenExceptionOccurs_ReturnsNull()
{
new(),
new()
};
_mockRepo.Setup(r => r.FindAll()).ReturnsAsync(mockResult);
// 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);
}
// Act
var result = await _service.GetAll();
// Assert
Assert.That(result, Has.Count.EqualTo(2));
}
/// <summary>
/// Verifies that the service's <c>GetAll</c> method returns all historical configuration changes retrieved from the repository.
/// </summary>
[Test]
public async Task GetByType_ReturnsCorrectAmountOfConfigs()
{
// Arrange
var configType = DisplayConfigEnums.ConfigTypes.ObservationCfg;
var mockResult = new List<HistoricalConfigChanges>
public async Task GetAll_ReturnsAllConfigChanges()
{
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));
}
// Arrange
var mockResult = new List<HistoricalConfigChanges>
{
new(),
new()
};
_mockRepo.Setup(r => r.FindAll()).ReturnsAsync(mockResult);
// Act
var result = await _service.GetAll();
// Assert
Assert.That(result, Has.Count.EqualTo(2));
}
/// <summary>
/// Verifies that the service's <c>GetByType</c> method returns the expected number of historical configuration changes for the specified configuration type.
/// </summary>
[Test]
public async Task GetByUser_ReturnsCorrectConfigs()
{
// Arrange
var user = "TestUser";
var mockResult = new List<HistoricalConfigChanges>
public async Task GetByType_ReturnsCorrectAmountOfConfigs()
{
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));
}
// Arrange
var configType = DisplayConfigEnums.ConfigTypes.ObservationCfg;
var mockResult = new List<HistoricalConfigChanges>
{
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));
}
/// <summary>
/// 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.
/// </summary>
[Test]
public async Task UpdateHistoricalConfigChange_WhenExceptionOccurs_ReturnsNull()
{
// Arrange
var mockChange = new HistoricalConfigChanges();
_mockRepo.Setup(r => r.Update(mockChange)).Throws(new Exception());
public async Task GetByUser_ReturnsCorrectConfigs()
{
// Arrange
var user = "TestUser";
var mockResult = new List<HistoricalConfigChanges>
{
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));
}
// Act
var result = await _service.UpdateHistoricalConfigChange(mockChange);
// Assert
Assert.That(result, Is.Null);
}
/// <summary>
/// Verifies that the service returns null when an exception is thrown while updating a historical configuration change, ensuring graceful error handling.
/// </summary>
[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);
}
}