Skip to content

Commit

Permalink
Refactor the ConfigurationService so that we only have to deserialize…
Browse files Browse the repository at this point in the history
… the whole settings object once. Add more documentation. Fix the tests due to the refactor.
  • Loading branch information
Robbware committed Oct 6, 2023
1 parent e2f5d95 commit f5f4df7
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

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 @@ -33,25 +35,21 @@ namespace COMET.Web.Common.Tests.Server.Services.ConfigurationService
using Moq;

using NUnit.Framework;

using JsonSerializer = System.Text.Json.JsonSerializer;


[TestFixture]
public class ConfigurationServiceTestFixture
{
[Test]
public async Task VerifyInitializeServiceWithEmptyConfiguration()
{
var configuration = new Mock<IConfiguration>();
configuration.Setup(x => x.GetSection(ConfigurationService.AddressSection)).Returns(new Mock<IConfigurationSection>().Object);
configuration.Setup(x => x.GetSection(ConfigurationService.BookInputConfigurationSection)).Returns(new Mock<IConfigurationSection>().Object);
configuration.Setup(x => x.GetSection(ConfigurationService.ServerConfigurationSection)).Returns(new Mock<IConfigurationSection>().Object);
var service = new ConfigurationService(configuration.Object);
await service.InitializeService();

Assert.Multiple(() =>
{
configuration.Verify(x => x.GetSection(ConfigurationService.AddressSection), Times.Once);
configuration.Verify(x => x.GetSection(ConfigurationService.BookInputConfigurationSection), Times.Once);
configuration.Verify(x => x.GetSection(ConfigurationService.ServerConfigurationSection), Times.Once);
Assert.That(service.ServerConfiguration.ServerAddress, Is.Null);
Assert.That(service.ServerConfiguration.BookInputConfiguration, Is.Null);
});
Expand All @@ -60,34 +58,39 @@ public async Task VerifyInitializeServiceWithEmptyConfiguration()

Assert.Multiple(() =>
{
configuration.Verify(x => x.GetSection(ConfigurationService.AddressSection), Times.Once);
configuration.Verify(x => x.GetSection(ConfigurationService.BookInputConfigurationSection), Times.Once);
configuration.Verify(x => x.GetSection(ConfigurationService.ServerConfigurationSection), Times.Once);
});
}

[Test]
public async Task VerifyInitializeServiceWithConfiguration()
{
var serverAddressMockConfigurationSection = new Mock<IConfigurationSection>();
serverAddressMockConfigurationSection.Setup(x => x.Value).Returns("https://a.b.c");

var bookInputMockConfigurationSection = new Mock<IConfigurationSection>();
var bookInputConfiguration = new BookInputConfiguration { ShowName = true, ShowShortName = true };
var defaultBookInputConfigurationJson = JsonSerializer.Serialize(bookInputConfiguration);
bookInputMockConfigurationSection.Setup(x => x.Value).Returns(defaultBookInputConfigurationJson);

var serverConfiguration = new ServerConfiguration
{
ServerAddress = "https://a.b.c",
BookInputConfiguration = new BookInputConfiguration()
{
ShowName = true,
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.AddressSection)).Returns(serverAddressMockConfigurationSection.Object);
configuration.Setup(x => x.GetSection(ConfigurationService.BookInputConfigurationSection)).Returns(bookInputMockConfigurationSection.Object);
configuration.Setup(x => x.GetSection(ConfigurationService.ServerConfigurationSection)).Returns(serverAddressMockConfigurationSection.Object);
var service = new ConfigurationService(configuration.Object);
await service.InitializeService();

Assert.Multiple(() =>
{
Assert.That(service.ServerConfiguration.ServerAddress, Is.EqualTo(serverAddressMockConfigurationSection.Object.Value));
Assert.That(service.ServerConfiguration.ServerAddress, Is.EqualTo(serverConfiguration.ServerAddress));
Assert.That(service.ServerConfiguration.BookInputConfiguration, Is.Not.Null);
Assert.That(service.ServerConfiguration.BookInputConfiguration.ShowName, Is.EqualTo(bookInputConfiguration.ShowName));
Assert.That(service.ServerConfiguration.BookInputConfiguration.ShowShortName, Is.EqualTo(bookInputConfiguration.ShowShortName));
Assert.That(service.ServerConfiguration.BookInputConfiguration.ShowName, Is.EqualTo(serverConfiguration.BookInputConfiguration.ShowName));
Assert.That(service.ServerConfiguration.BookInputConfiguration.ShowShortName, Is.EqualTo(serverConfiguration.BookInputConfiguration.ShowShortName));
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@

namespace COMET.Web.Common.Model.Configuration;

using System.Text.Json.Serialization;

/// <summary>
/// Holds all of the configuration related to the Book feature
/// </summary>
public class BookInputConfiguration
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,9 @@ namespace COMET.Web.Common.Server.Services.ConfigurationService
public class ConfigurationService : BaseConfigurationService
{
/// <summary>
/// Gets the ServerAddress section key
/// Gets the ServerConfiguration section key
/// </summary>
public const string AddressSection = "ServerAddress";

/// <summary>
/// Gets the BookInputConfiguration section key
/// </summary>
public const string BookInputConfigurationSection = "BookInputConfiguration";
public const string ServerConfigurationSection = "ServerConfiguration";

/// <summary>
/// Gets the <see cref="IConfiguration" />
Expand Down Expand Up @@ -74,19 +69,12 @@ public override Task InitializeService()
}

this.ServerConfiguration = new ServerConfiguration();

var addressSection = this.configuration.GetSection(AddressSection);

if (addressSection.Exists())
{
this.ServerConfiguration.ServerAddress = addressSection.Value;
}

var bookInputConfigurationSection = this.configuration.GetSection(BookInputConfigurationSection);

if (bookInputConfigurationSection.Exists())
var serverConfigurationSection = this.configuration.GetSection(ServerConfigurationSection);

if (serverConfigurationSection.Exists())
{
this.ServerConfiguration.BookInputConfiguration = JsonSerializer.Deserialize<BookInputConfiguration>(bookInputConfigurationSection.Value);
this.ServerConfiguration = JsonSerializer.Deserialize<ServerConfiguration>(serverConfigurationSection.Value);
}

this.IsInitialized = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
namespace COMET.Web.Common.Services.ConfigurationService
{
using COMET.Web.Common.Model.Configuration;
using COMET.Web.Common.Model.DTO;

/// <summary>
/// Service that holds the configuration for the application
Expand All @@ -36,7 +35,7 @@ public interface IConfigurationService
/// <summary>
/// Holds all of the configuration related values
/// </summary>
public ServerConfiguration ServerConfiguration { get; set; }
ServerConfiguration ServerConfiguration { get; set; }

/// <summary>
/// Initializes the <see cref="IConfigurationService"/>
Expand Down
10 changes: 6 additions & 4 deletions COMET.Web.Common/wwwroot/server_configuration.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"ServerAddress": "",
"BookInputConfiguration": {
"ShowName": true,
"ShowShortName": true
"ServerConfiguration": {
"ServerAddress": "",
"BookInputConfiguration": {
"ShowName": true,
"ShowShortName": true
}
}
}

0 comments on commit f5f4df7

Please sign in to comment.