2025-02-02 23:13:17 +01:00
|
|
|
using Newtonsoft.Json.Linq;
|
2025-02-20 00:23:13 +01:00
|
|
|
using PlanTempus.Core.Configurations;
|
|
|
|
|
using PlanTempus.Core.Configurations.JsonConfigProvider;
|
|
|
|
|
using PlanTempus.Core.Configurations.SmartConfigProvider;
|
2025-03-07 00:17:08 +01:00
|
|
|
using Shouldly;
|
2025-02-02 23:13:17 +01:00
|
|
|
|
2025-03-07 00:17:08 +01:00
|
|
|
namespace PlanTempus.X.TDD.ConfigurationTests;
|
|
|
|
|
|
|
|
|
|
[TestClass]
|
|
|
|
|
public class JsonConfigurationProviderTests : TestFixture
|
2025-02-02 23:13:17 +01:00
|
|
|
{
|
2025-03-07 00:17:08 +01:00
|
|
|
private const string _testFolder = "ConfigurationTests/";
|
2025-02-19 14:21:40 +01:00
|
|
|
|
2025-03-07 00:17:08 +01:00
|
|
|
public JsonConfigurationProviderTests() : base(_testFolder)
|
|
|
|
|
{
|
|
|
|
|
}
|
2025-02-19 14:21:40 +01:00
|
|
|
|
|
|
|
|
|
2025-03-07 00:17:08 +01:00
|
|
|
[TestMethod]
|
|
|
|
|
public void GetSection_ShouldReturnCorrectFeatureSection()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
var expectedJObject = JObject.Parse(@"{
|
2025-02-02 23:13:17 +01:00
|
|
|
'Enabled': true,
|
|
|
|
|
'RolloutPercentage': 25,
|
|
|
|
|
'AllowedUserGroups': ['beta']
|
|
|
|
|
}") as JToken;
|
|
|
|
|
|
2025-03-07 00:17:08 +01:00
|
|
|
var builder = new ConfigurationBuilder()
|
|
|
|
|
.AddJsonFile($"{_testFolder}appconfiguration.dev.json")
|
|
|
|
|
.Build();
|
2025-02-18 17:35:00 +01:00
|
|
|
|
2025-03-07 00:17:08 +01:00
|
|
|
// Act
|
|
|
|
|
var section = builder.GetSection("Feature");
|
2025-02-20 00:23:13 +01:00
|
|
|
|
2025-03-07 00:17:08 +01:00
|
|
|
// Assert
|
|
|
|
|
section.ShouldNotBeNull();
|
|
|
|
|
section.Value.ShouldBeEquivalentTo(expectedJObject);
|
|
|
|
|
}
|
2025-02-20 00:23:13 +01:00
|
|
|
|
2025-03-07 00:17:08 +01:00
|
|
|
[TestMethod]
|
|
|
|
|
public void Get_ShouldReturnCorrectFeatureObject()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
var expectedFeature = new Feature
|
2025-02-20 00:23:13 +01:00
|
|
|
{
|
2025-03-07 00:17:08 +01:00
|
|
|
Enabled = true,
|
|
|
|
|
RolloutPercentage = 25,
|
|
|
|
|
AllowedUserGroups = new List<string> { "beta" }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var builder = new ConfigurationBuilder()
|
|
|
|
|
.AddJsonFile($"{_testFolder}appconfiguration.dev.json")
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
var actualFeature = builder.GetSection("Feature").ToObject<Feature>();
|
2025-02-11 19:34:45 +01:00
|
|
|
#pragma warning disable CS0618 // Type or member is obsolete
|
2025-03-07 00:17:08 +01:00
|
|
|
var actualFeatureObsoleted = builder.GetSection("Feature").Get<Feature>();
|
2025-02-11 19:34:45 +01:00
|
|
|
#pragma warning restore CS0618 // Type or member is obsolete
|
2025-03-07 00:17:08 +01:00
|
|
|
// Assert
|
|
|
|
|
actualFeature.ShouldBeEquivalentTo(expectedFeature);
|
|
|
|
|
actualFeatureObsoleted.ShouldBeEquivalentTo(expectedFeature);
|
|
|
|
|
}
|
2025-02-20 00:23:13 +01:00
|
|
|
|
2025-03-07 00:17:08 +01:00
|
|
|
[TestMethod]
|
|
|
|
|
public void Get_ShouldReturnCorrectValueAsString()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
var expectedFeature = "123";
|
2025-02-20 00:23:13 +01:00
|
|
|
|
2025-03-07 00:17:08 +01:00
|
|
|
var builder = new ConfigurationBuilder()
|
|
|
|
|
.AddJsonFile($"{_testFolder}appconfiguration.dev.json")
|
|
|
|
|
.Build();
|
2025-02-18 17:35:00 +01:00
|
|
|
|
2025-03-07 00:17:08 +01:00
|
|
|
// Act
|
|
|
|
|
var actualFeature = builder.GetSection("AnotherSetting").Get<string>("Thresholds:High");
|
2025-02-18 17:35:00 +01:00
|
|
|
|
2025-03-07 00:17:08 +01:00
|
|
|
// Assert
|
|
|
|
|
actualFeature.ShouldBeEquivalentTo(expectedFeature);
|
|
|
|
|
}
|
2025-02-18 17:35:00 +01:00
|
|
|
|
2025-03-07 00:17:08 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Testing a stupid indexer for compability with Microsoft ConfigurationBuilder
|
|
|
|
|
/// </summary>
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void Indexer_ShouldReturnValueAsString()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
var expected = "SHA256";
|
2025-02-18 17:35:00 +01:00
|
|
|
|
2025-03-07 00:17:08 +01:00
|
|
|
var builder = new ConfigurationBuilder()
|
|
|
|
|
.AddJsonFile($"{_testFolder}appconfiguration.dev.json")
|
|
|
|
|
.Build();
|
2025-02-18 17:35:00 +01:00
|
|
|
|
2025-03-07 00:17:08 +01:00
|
|
|
// Act
|
|
|
|
|
var actual = builder["Authentication"];
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
actual.ShouldBeEquivalentTo(expected);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void Get_ShouldReturnCorrectValueAsInt()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
var expectedFeature = 22;
|
2025-02-18 17:35:00 +01:00
|
|
|
|
2025-03-07 00:17:08 +01:00
|
|
|
var builder = new ConfigurationBuilder()
|
|
|
|
|
.AddJsonFile($"{_testFolder}appconfiguration.dev.json")
|
|
|
|
|
.Build();
|
2025-02-20 00:23:13 +01:00
|
|
|
|
2025-03-07 00:17:08 +01:00
|
|
|
// Act
|
|
|
|
|
var actualFeature = builder.GetSection("AnotherSetting:Temperature").Get<int>("Indoor:Max:Limit");
|
2025-02-20 00:23:13 +01:00
|
|
|
|
2025-03-07 00:17:08 +01:00
|
|
|
// Assert
|
|
|
|
|
actualFeature.ShouldBe(expectedFeature);
|
2025-02-20 00:23:13 +01:00
|
|
|
}
|
|
|
|
|
|
2025-03-07 00:17:08 +01:00
|
|
|
[TestMethod]
|
|
|
|
|
public void Get_ShouldReturnCorrectValueAsBool()
|
2025-02-20 00:23:13 +01:00
|
|
|
{
|
2025-03-07 00:17:08 +01:00
|
|
|
// Arrange
|
|
|
|
|
var expectedFeature = true;
|
|
|
|
|
|
|
|
|
|
var configRoot = new ConfigurationBuilder()
|
|
|
|
|
.AddJsonFile($"{_testFolder}appconfiguration.dev.json")
|
|
|
|
|
.AddSmartConfig()
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
var actualFeature = configRoot.Get<bool>("Database:UseSSL");
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
actualFeature.ShouldBe(expectedFeature);
|
2025-02-20 00:23:13 +01:00
|
|
|
}
|
2025-03-07 00:17:08 +01:00
|
|
|
}
|
2025-02-02 23:13:17 +01:00
|
|
|
|
2025-03-07 00:17:08 +01:00
|
|
|
internal class Feature
|
|
|
|
|
{
|
|
|
|
|
public bool Enabled { get; set; }
|
|
|
|
|
public int RolloutPercentage { get; set; }
|
|
|
|
|
public List<string> AllowedUserGroups { get; set; }
|
2025-02-02 23:13:17 +01:00
|
|
|
}
|