2025-01-26 22:57:27 +01:00
using Microsoft.VisualStudio.TestTools.UnitTesting ;
using Moq ;
using Newtonsoft.Json.Linq ;
using System ;
using System.Collections.Generic ;
using System.Threading.Tasks ;
using Tests ;
2025-01-30 18:03:24 +01:00
using Microsoft.Extensions.Configuration ;
using Core.Configurations.SmartConfiguration ;
2025-01-26 22:57:27 +01:00
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 ) ;
}
2025-01-31 22:08:54 +01:00
2025-01-26 22:57:27 +01:00
[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
}
}
}
}
} ;
2025-01-31 22:08:54 +01:00
Microsoft . Extensions . Configuration . IConfiguration configuration = null ; // new JsonConfiguration(configData, new Microsoft.Extensions.Configuration.ConfigurationReloadToken());
2025-01-26 22:57:27 +01:00
// 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 ; }
}
}