54620ccb2e
Add config file loading and saving service following TDD approach: - IConfigFileService interface for loading/saving config files - ConfigLoadException for descriptive error handling - ConfigFileService implementation with JSON serialization - Unit tests with mocked IFileSystem dependency
55 lines
1.6 KiB
C#
55 lines
1.6 KiB
C#
using JdeScoping.ConfigManager.Models;
|
|
using JdeScoping.ConfigManager.Services;
|
|
|
|
namespace JdeScoping.ConfigManager.Tests.Services;
|
|
|
|
public class ConfigFileServiceTests
|
|
{
|
|
private readonly IFileSystem _fileSystem;
|
|
private readonly ConfigFileService _sut;
|
|
|
|
public ConfigFileServiceTests()
|
|
{
|
|
_fileSystem = Substitute.For<IFileSystem>();
|
|
_sut = new ConfigFileService(_fileSystem);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task LoadAppSettingsAsync_WithValidJson_ReturnsConfigModel()
|
|
{
|
|
// Arrange
|
|
var json = """
|
|
{
|
|
"DataSync": {
|
|
"Enabled": true,
|
|
"MaxDegreeOfParallelism": 8
|
|
}
|
|
}
|
|
""";
|
|
_fileSystem.ReadAllTextAsync(Arg.Any<string>(), Arg.Any<CancellationToken>())
|
|
.Returns(Task.FromResult(json));
|
|
|
|
// Act
|
|
var result = await _sut.LoadAppSettingsAsync("/config/appsettings.json");
|
|
|
|
// Assert
|
|
result.ShouldNotBeNull();
|
|
result.DataSync.Enabled.ShouldBeTrue();
|
|
result.DataSync.MaxDegreeOfParallelism.ShouldBe(8);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task LoadAppSettingsAsync_WithInvalidJson_ThrowsWithHelpfulMessage()
|
|
{
|
|
// Arrange
|
|
var json = "{ invalid json }";
|
|
_fileSystem.ReadAllTextAsync(Arg.Any<string>(), Arg.Any<CancellationToken>())
|
|
.Returns(Task.FromResult(json));
|
|
|
|
// Act & Assert
|
|
var ex = await Should.ThrowAsync<ConfigLoadException>(
|
|
() => _sut.LoadAppSettingsAsync("/config/appsettings.json"));
|
|
ex.Message.ShouldContain("parse");
|
|
}
|
|
}
|