feat: implement read and write methods for json file storage provider

This commit is contained in:
Stevan Freeborn
2026-03-29 20:46:45 -05:00
parent b41669cbc6
commit 38daead9b2
12 changed files with 266 additions and 6 deletions
@@ -0,0 +1,44 @@
using StevanFreeborn.Extensions.Configuration.Secure.Storage;
namespace StevanFreeborn.Extensions.Configuration.Secure.Tests.Unit.Storage;
public class JsonFileStorageProviderTests
{
private readonly string _tmpDirectory;
private readonly JsonStorageOptions _options;
private readonly JsonFileStorageProvider _sut;
public JsonFileStorageProviderTests()
{
_tmpDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(_tmpDirectory);
_options = new()
{
FileName = "testsettings.json"
};
_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()
{
var configKey = "Key";
var configValue = "Value";
await _sut.WriteAsync(configKey, configValue);
var result = await _sut.ReadAsync(configKey);
result.Should().Be(configValue);
File.Exists(_options.FullPath).Should().BeTrue();
}
}
@@ -0,0 +1,36 @@
using StevanFreeborn.Extensions.Configuration.Secure.Storage;
namespace StevanFreeborn.Extensions.Configuration.Secure.Tests.Unit.Storage;
public class JsonStorageOptionsTests
{
[Fact]
public void Path_WhenCalledWithFileNameSet_ItShouldReturnExpectedPath()
{
var fileName = "appsettings.json";
var expectedPath = Path.Combine(AppContext.BaseDirectory, fileName);
var opts = new JsonStorageOptions()
{
FileName = fileName,
};
opts.FullPath.Should().Be(expectedPath);
}
[Fact]
public void Path_WhenCalledWithFileNameAndDirectoryPathSet_ItShouldReturnExpectedPath()
{
var fileName = "appsettings.json";
var directoryPath = @"C:\Path\To\Some\Directory";
var expectedPath = Path.Combine(directoryPath, fileName);
var opts = new JsonStorageOptions()
{
FileName = fileName,
DirectoryPath = directoryPath,
};
opts.FullPath.Should().Be(expectedPath);
}
}