Adds Configuration Manager + tests
This commit is contained in:
parent
55e65a1b21
commit
384cc3c6fd
16 changed files with 657 additions and 137 deletions
171
Tests/TestConfigurationManagement.cs
Normal file
171
Tests/TestConfigurationManagement.cs
Normal file
|
|
@ -0,0 +1,171 @@
|
|||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Tests;
|
||||
using Core.Configurations.ConfigurationManager;
|
||||
|
||||
namespace Configuration.Core.Tests;
|
||||
|
||||
[TestClass]
|
||||
public class ConfigurationTests : TestFixture
|
||||
{
|
||||
private Mock<IConfigurationRepository> _mockRepo;
|
||||
private KeyValueConfigurationBuilder _builder;
|
||||
|
||||
[TestInitialize]
|
||||
public void Init()
|
||||
{
|
||||
_mockRepo = new Mock<IConfigurationRepository>();
|
||||
_builder = new KeyValueConfigurationBuilder(_mockRepo.Object);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task LoadConfiguration_WithValidData_BuildsCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
var configurations = new List<AppConfiguration>
|
||||
{
|
||||
new AppConfiguration
|
||||
{
|
||||
Key = "Email:Templates:Welcome",
|
||||
Value = @"{""subject"":""Welcome"",""sender"":""test@company.com""}",
|
||||
ValidFrom = DateTime.UtcNow.AddDays(-1),
|
||||
ExpiresAt = DateTime.UtcNow.AddDays(1)
|
||||
}
|
||||
};
|
||||
|
||||
_mockRepo.Setup(r => r.GetActiveConfigurations())
|
||||
.ReturnsAsync(configurations);
|
||||
|
||||
// Act
|
||||
await _builder.LoadConfiguration();
|
||||
var config = _builder.Build();
|
||||
|
||||
// Assert
|
||||
Assert.IsNotNull(config);
|
||||
Assert.AreEqual("Welcome", config["Email:Templates:Welcome:subject"]);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task LoadConfiguration_WithExpiredData_NotIncluded()
|
||||
{
|
||||
// Arrange
|
||||
var configurations = new List<AppConfiguration>
|
||||
{
|
||||
new AppConfiguration
|
||||
{
|
||||
Key = "Test:Key",
|
||||
Value = @"{""value"":""test""}",
|
||||
ValidFrom = DateTime.UtcNow.AddDays(1),
|
||||
ExpiresAt = DateTime.UtcNow.AddDays(2)
|
||||
}
|
||||
};
|
||||
|
||||
_mockRepo.Setup(r => r.GetActiveConfigurations())
|
||||
.ReturnsAsync(new List<AppConfiguration>());
|
||||
|
||||
// Act
|
||||
await _builder.LoadConfiguration();
|
||||
var config = _builder.Build();
|
||||
|
||||
// Assert
|
||||
Assert.IsNull(config["Test:Key:value"]);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void AddKeyValue_WithNestedKeys_BuildsCorrectHierarchy()
|
||||
{
|
||||
// Arrange
|
||||
var key = "Level1:Level2:Level3";
|
||||
var value = @"{""setting"":""value""}";
|
||||
|
||||
// Act
|
||||
_builder.AddKeyValue(key, value);
|
||||
var config = _builder.Build();
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual("value", config["Level1:Level2:Level3:setting"]);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task GetSection_ReturnsCorrectSubSection()
|
||||
{
|
||||
// Arrange
|
||||
var configurations = new List<AppConfiguration>
|
||||
{
|
||||
new AppConfiguration
|
||||
{
|
||||
Key = "Parent:Child",
|
||||
Value = @"{""setting"":""value""}",
|
||||
ValidFrom = DateTime.UtcNow.AddDays(-1)
|
||||
}
|
||||
};
|
||||
|
||||
_mockRepo.Setup(r => r.GetActiveConfigurations())
|
||||
.ReturnsAsync(configurations);
|
||||
|
||||
// Act
|
||||
await _builder.LoadConfiguration();
|
||||
var config = _builder.Build();
|
||||
var section = config.GetSection("Parent");
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual("value", section["Child:setting"]);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Configuration_ShouldMapToTypedClass()
|
||||
{
|
||||
// Arrange
|
||||
var configData = new JObject
|
||||
{
|
||||
["Email"] = new JObject
|
||||
{
|
||||
["Templates"] = new JObject
|
||||
{
|
||||
["Welcome"] = new JObject
|
||||
{
|
||||
["subject"] = "Welcome to our service",
|
||||
["template"] = "welcome-template.html",
|
||||
["sender"] = "noreply@test.com",
|
||||
["settings"] = new JObject
|
||||
{
|
||||
["isEnabled"] = true,
|
||||
["retryCount"] = 3
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var configuration = new JsonConfiguration(configData, new Microsoft.Extensions.Configuration.ConfigurationReloadToken());
|
||||
|
||||
// Act
|
||||
var welcomeConfig = configuration.GetSection("Email:Templates:Welcome").Get<WelcomeEmailConfig>();
|
||||
|
||||
// Assert
|
||||
Assert.IsNotNull(welcomeConfig);
|
||||
Assert.AreEqual("Welcome to our service", welcomeConfig.Subject);
|
||||
Assert.AreEqual("welcome-template.html", welcomeConfig.Template);
|
||||
Assert.AreEqual("noreply@test.com", welcomeConfig.Sender);
|
||||
Assert.IsTrue(welcomeConfig.Settings.IsEnabled);
|
||||
Assert.AreEqual(3, welcomeConfig.Settings.RetryCount);
|
||||
}
|
||||
|
||||
public class WelcomeEmailConfig
|
||||
{
|
||||
public string Subject { get; set; }
|
||||
public string Template { get; set; }
|
||||
public string Sender { get; set; }
|
||||
public EmailSettings Settings { get; set; }
|
||||
}
|
||||
|
||||
public class EmailSettings
|
||||
{
|
||||
public bool IsEnabled { get; set; }
|
||||
public int RetryCount { get; set; }
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue