Files

95 lines
2.4 KiB
C#
Raw Permalink Normal View History

using StevanFreeborn.Extensions.Configuration.Secure.Storage;
2026-04-02 20:31:06 -05:00
using StevanFreeborn.Extensions.Configuration.Secure.Tests.Common;
namespace StevanFreeborn.Extensions.Configuration.Secure.Tests.Unit.Storage;
2026-03-29 21:07:48 -05:00
public class JsonFileStorageProviderTests : IDisposable
{
2026-04-02 20:31:06 -05:00
private readonly TempDirectory _tempDir = new();
private readonly JsonStorageOptions _options;
private readonly JsonFileStorageProvider _sut;
public JsonFileStorageProviderTests()
{
_options = new()
{
2026-03-29 21:07:48 -05:00
FileName = "testsettings.json",
2026-04-02 20:31:06 -05:00
DirectoryPath = _tempDir.Path,
};
_sut = new(_options);
}
[Fact]
public void Constructor_WhenCalledWithNullOptions_ItShouldThrowArgumentNullException()
{
var act = static () => new JsonFileStorageProvider(null!);
act.Should().Throw<ArgumentNullException>();
}
[Fact]
public async Task WriteAsync_And_ReadAsync_WhenCalled_ItShouldBeAbleToPersistAndRetrieveValues()
{
2026-03-29 21:07:48 -05:00
var configKey = "KeyA";
var configValue = "Value";
await _sut.WriteAsync(configKey, configValue);
var result = await _sut.ReadAsync(configKey);
result.Should().Be(configValue);
File.Exists(_options.FullPath).Should().BeTrue();
}
2026-03-29 21:07:48 -05:00
[Fact]
public async Task ReadAllAsync_WhenCalled_ItShouldReturnAllStoredKeys()
{
await _sut.WriteAsync("Key1", "Val1");
await _sut.WriteAsync("Key2", "Val2");
var allData = await _sut.ReadAllAsync();
allData.Should().BeEquivalentTo(new Dictionary<string, string>()
{
["Key1"] = "Val1",
["Key2"] = "Val2",
});
}
[Fact]
public async Task DeleteAsync_WhenCalled_ItShouldRemoveKey()
{
await _sut.WriteAsync("KeyToDelete", "SomeValue");
var deleteResult = await _sut.DeleteAsync("KeyToDelete");
var readResult = await _sut.ReadAsync("KeyToDelete");
deleteResult.Should().BeTrue();
readResult.Should().BeEmpty();
}
2026-03-29 21:07:48 -05:00
public void Dispose()
{
2026-04-02 20:31:06 -05:00
_tempDir.Dispose();
2026-03-29 21:07:48 -05:00
GC.SuppressFinalize(this);
}
[Fact]
public async Task WriteAsync_WhenCalledConcurrently_ItShouldNotThrowFileInUseException()
{
const int numberOfWrites = 50;
var tasks = new List<Task>();
foreach (var index in Enumerable.Range(0, numberOfWrites))
{
tasks.Add(Task.Run(() => _sut.WriteAsync($"Key{index}", $"Val{index}")));
}
var act = async () => await Task.WhenAll(tasks);
await act.Should().NotThrowAsync();
var allData = await _sut.ReadAllAsync();
allData.Should().HaveCount(numberOfWrites);
}
}