From d00131a0bcd21aaabc2f7cf3a92fc957b011bd54 Mon Sep 17 00:00:00 2001 From: Robbware Date: Fri, 6 Oct 2023 15:04:19 +0100 Subject: [PATCH] Add ConfigurationBinder to be able to bind a whole section to a complex object. Update the tests so they use an actual IConfiguration because we're not able to Mock .Bind(). --- .../COMET.Web.Common.Tests.csproj | 4 ++++ .../Data/server_configuration_tests.json | 9 +++++++++ .../ConfigurationServiceTestFixture.cs | 18 ++++++------------ COMET.Web.Common/COMET.Web.Common.csproj | 1 + .../ConfigurationService.cs | 11 +---------- 5 files changed, 21 insertions(+), 22 deletions(-) create mode 100644 COMET.Web.Common.Tests/Data/server_configuration_tests.json diff --git a/COMET.Web.Common.Tests/COMET.Web.Common.Tests.csproj b/COMET.Web.Common.Tests/COMET.Web.Common.Tests.csproj index 00522345..55c1006c 100644 --- a/COMET.Web.Common.Tests/COMET.Web.Common.Tests.csproj +++ b/COMET.Web.Common.Tests/COMET.Web.Common.Tests.csproj @@ -35,6 +35,10 @@ true PreserveNewest + + + Always + diff --git a/COMET.Web.Common.Tests/Data/server_configuration_tests.json b/COMET.Web.Common.Tests/Data/server_configuration_tests.json new file mode 100644 index 00000000..2b2e5917 --- /dev/null +++ b/COMET.Web.Common.Tests/Data/server_configuration_tests.json @@ -0,0 +1,9 @@ +{ + "ServerConfiguration": { + "ServerAddress": "https://a.b.c", + "BookInputConfiguration": { + "ShowName": true, + "ShowShortName": true + } + } +} diff --git a/COMET.Web.Common.Tests/Server/Services/ConfigurationService/ConfigurationServiceTestFixture.cs b/COMET.Web.Common.Tests/Server/Services/ConfigurationService/ConfigurationServiceTestFixture.cs index b49b8c8b..9ec3814c 100644 --- a/COMET.Web.Common.Tests/Server/Services/ConfigurationService/ConfigurationServiceTestFixture.cs +++ b/COMET.Web.Common.Tests/Server/Services/ConfigurationService/ConfigurationServiceTestFixture.cs @@ -25,8 +25,6 @@ namespace COMET.Web.Common.Tests.Server.Services.ConfigurationService { - using System.Text.Json; - using COMET.Web.Common.Model.Configuration; using COMET.Web.Common.Server.Services.ConfigurationService; @@ -50,8 +48,7 @@ public async Task VerifyInitializeServiceWithEmptyConfiguration() Assert.Multiple(() => { configuration.Verify(x => x.GetSection(ConfigurationService.ServerConfigurationSection), Times.Once); - Assert.That(service.ServerConfiguration.ServerAddress, Is.Null); - Assert.That(service.ServerConfiguration.BookInputConfiguration, Is.Null); + Assert.That(service.ServerConfiguration, Is.Null); }); await service.InitializeService(); @@ -65,8 +62,6 @@ public async Task VerifyInitializeServiceWithEmptyConfiguration() [Test] public async Task VerifyInitializeServiceWithConfiguration() { - var serverAddressMockConfigurationSection = new Mock(); - var serverConfiguration = new ServerConfiguration { ServerAddress = "https://a.b.c", @@ -76,13 +71,12 @@ public async Task VerifyInitializeServiceWithConfiguration() ShowShortName = true } }; - - var serverConfigurationJson = JsonSerializer.Serialize(serverConfiguration); - serverAddressMockConfigurationSection.Setup(x => x.Value).Returns(serverConfigurationJson); - var configuration = new Mock(); - configuration.Setup(x => x.GetSection(ConfigurationService.ServerConfigurationSection)).Returns(serverAddressMockConfigurationSection.Object); - var service = new ConfigurationService(configuration.Object); + var config = new ConfigurationBuilder() + .AddJsonFile("Data/server_configuration_tests.json") + .Build(); + + var service = new ConfigurationService(config); await service.InitializeService(); Assert.Multiple(() => diff --git a/COMET.Web.Common/COMET.Web.Common.csproj b/COMET.Web.Common/COMET.Web.Common.csproj index 57eddb38..79e6c010 100644 --- a/COMET.Web.Common/COMET.Web.Common.csproj +++ b/COMET.Web.Common/COMET.Web.Common.csproj @@ -30,6 +30,7 @@ + diff --git a/COMET.Web.Common/Server/Services/ConfigurationService/ConfigurationService.cs b/COMET.Web.Common/Server/Services/ConfigurationService/ConfigurationService.cs index ea151de6..f2938b3f 100644 --- a/COMET.Web.Common/Server/Services/ConfigurationService/ConfigurationService.cs +++ b/COMET.Web.Common/Server/Services/ConfigurationService/ConfigurationService.cs @@ -28,7 +28,6 @@ namespace COMET.Web.Common.Server.Services.ConfigurationService using System.Text.Json; using COMET.Web.Common.Model.Configuration; - using COMET.Web.Common.Model.DTO; using COMET.Web.Common.Services.ConfigurationService; using Microsoft.Extensions.Configuration; @@ -68,15 +67,7 @@ public override Task InitializeService() return Task.CompletedTask; } - this.ServerConfiguration = new ServerConfiguration(); - - var serverConfigurationSection = this.configuration.GetSection(ServerConfigurationSection); - - if (serverConfigurationSection.Exists()) - { - this.ServerConfiguration = JsonSerializer.Deserialize(serverConfigurationSection.Value); - } - + this.ServerConfiguration = this.configuration.GetSection(ServerConfigurationSection).Get(); this.IsInitialized = true; return Task.CompletedTask; }