PlanTempusApp/Tests/ConfigurationTests/JsonConfigurationProviderTests.cs

142 lines
4.4 KiB
C#
Raw Normal View History

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