From a8774aefe670add3c9b1684c75d99c71e80ed040 Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Sun, 29 Mar 2026 21:07:48 -0500 Subject: [PATCH] feat: implement readallasync method --- .../Storage/JsonFileStorageProvider.cs | 44 ++++++++++++------- .../Storage/JsonFileStorageProviderTests.cs | 32 ++++++++++++-- 2 files changed, 56 insertions(+), 20 deletions(-) diff --git a/src/StevanFreeborn.Extensions.Configuration.Secure/Storage/JsonFileStorageProvider.cs b/src/StevanFreeborn.Extensions.Configuration.Secure/Storage/JsonFileStorageProvider.cs index 9e24588..8caf22a 100644 --- a/src/StevanFreeborn.Extensions.Configuration.Secure/Storage/JsonFileStorageProvider.cs +++ b/src/StevanFreeborn.Extensions.Configuration.Secure/Storage/JsonFileStorageProvider.cs @@ -13,19 +13,7 @@ public sealed class JsonFileStorageProvider(JsonStorageOptions options) public async Task ReadAsync(string key, CancellationToken ct = default) { - if (File.Exists(_options.FullPath) is false) - { - return string.Empty; - } - - using var stream = new FileStream(_options.FullPath, FileMode.Open, FileAccess.Read); - - if (stream.Length is 0) - { - return string.Empty; - } - - var data = await JsonSerializer.DeserializeAsync>(stream, cancellationToken: ct); + var data = await LoadAsync(ct); if (data is not null && data.TryGetValue(key, out var v)) { @@ -35,14 +23,17 @@ public sealed class JsonFileStorageProvider(JsonStorageOptions options) return string.Empty; } + public Task> ReadAllAsync(CancellationToken ct = default) + { + return LoadAsync(ct); + } + public async Task WriteAsync(string key, string encryptedData, CancellationToken ct = default) { Directory.CreateDirectory(_options.DirectoryPath); - var data = new Dictionary - { - [key] = encryptedData - }; + var data = await LoadAsync(ct); + data[key] = encryptedData; using var stream = new FileStream( _options.FullPath, @@ -53,4 +44,23 @@ public sealed class JsonFileStorageProvider(JsonStorageOptions options) await JsonSerializer.SerializeAsync(stream, data, JsonOptions, ct); } + + private async Task> LoadAsync(CancellationToken ct) + { + if (File.Exists(_options.FullPath) is false) + { + return []; + } + + using var stream = new FileStream(_options.FullPath, FileMode.Open, FileAccess.Read); + + if (stream.Length is 0) + { + return []; + } + + var data = await JsonSerializer.DeserializeAsync>(stream, cancellationToken: ct); + + return data ?? []; + } } \ No newline at end of file diff --git a/tests/StevanFreeborn.Extensions.Configuration.Secure.Tests/Unit/Storage/JsonFileStorageProviderTests.cs b/tests/StevanFreeborn.Extensions.Configuration.Secure.Tests/Unit/Storage/JsonFileStorageProviderTests.cs index 71fcfd7..ed35f14 100644 --- a/tests/StevanFreeborn.Extensions.Configuration.Secure.Tests/Unit/Storage/JsonFileStorageProviderTests.cs +++ b/tests/StevanFreeborn.Extensions.Configuration.Secure.Tests/Unit/Storage/JsonFileStorageProviderTests.cs @@ -2,7 +2,7 @@ using StevanFreeborn.Extensions.Configuration.Secure.Storage; namespace StevanFreeborn.Extensions.Configuration.Secure.Tests.Unit.Storage; -public class JsonFileStorageProviderTests +public class JsonFileStorageProviderTests : IDisposable { private readonly string _tmpDirectory; private readonly JsonStorageOptions _options; @@ -15,7 +15,8 @@ public class JsonFileStorageProviderTests _options = new() { - FileName = "testsettings.json" + FileName = "testsettings.json", + DirectoryPath = _tmpDirectory, }; _sut = new(_options); @@ -32,7 +33,7 @@ public class JsonFileStorageProviderTests [Fact] public async Task WriteAsync_And_ReadAsync_WhenCalled_ItShouldBeAbleToPersistAndRetrieveValues() { - var configKey = "Key"; + var configKey = "KeyA"; var configValue = "Value"; await _sut.WriteAsync(configKey, configValue); @@ -41,4 +42,29 @@ public class JsonFileStorageProviderTests result.Should().Be(configValue); File.Exists(_options.FullPath).Should().BeTrue(); } + + [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() + { + ["Key1"] = "Val1", + ["Key2"] = "Val2", + }); + } + + public void Dispose() + { + if (Directory.Exists(_tmpDirectory)) + { + Directory.Delete(_tmpDirectory, true); + } + + GC.SuppressFinalize(this); + } }