Skip to content

Commit

Permalink
Add ConfigurationBinder to be able to bind a whole section to a compl…
Browse files Browse the repository at this point in the history
…ex object. Update the tests so they use an actual IConfiguration because we're not able to Mock .Bind().
  • Loading branch information
Robbware committed Oct 6, 2023
1 parent c3c7838 commit d00131a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 22 deletions.
4 changes: 4 additions & 0 deletions COMET.Web.Common.Tests/COMET.Web.Common.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
<None Remove="Data\server_configuration_tests.json" />
<Content Include="Data\server_configuration_tests.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
Expand Down
9 changes: 9 additions & 0 deletions COMET.Web.Common.Tests/Data/server_configuration_tests.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"ServerConfiguration": {
"ServerAddress": "https://a.b.c",
"BookInputConfiguration": {
"ShowName": true,
"ShowShortName": true
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
Expand All @@ -65,8 +62,6 @@ public async Task VerifyInitializeServiceWithEmptyConfiguration()
[Test]
public async Task VerifyInitializeServiceWithConfiguration()
{
var serverAddressMockConfigurationSection = new Mock<IConfigurationSection>();

var serverConfiguration = new ServerConfiguration
{
ServerAddress = "https://a.b.c",
Expand All @@ -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<IConfiguration>();
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(() =>
Expand Down
1 change: 1 addition & 0 deletions COMET.Web.Common/COMET.Web.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="7.0.4" />
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.0" />
<PackageReference Include="ReactiveUI" Version="18.4.26" />
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ServerConfiguration>(serverConfigurationSection.Value);
}

this.ServerConfiguration = this.configuration.GetSection(ServerConfigurationSection).Get<ServerConfiguration>();
this.IsInitialized = true;
return Task.CompletedTask;
}
Expand Down

0 comments on commit d00131a

Please sign in to comment.