PlanTempusApp/Tests/ConfigurationTests/JsonConfigurationProviderTests.cs

143 lines
4 KiB
C#
Raw Normal View History

using Newtonsoft.Json.Linq;
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-03-07 00:17:08 +01:00
namespace PlanTempus.X.TDD.ConfigurationTests;
[TestClass]
public class JsonConfigurationProviderTests : TestFixture
{
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(@"{
'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-03-07 00:17:08 +01:00
// Assert
section.ShouldNotBeNull();
section.Value.ShouldBeEquivalentTo(expectedJObject);
}
2025-03-07 00:17:08 +01:00
[TestMethod]
public void Get_ShouldReturnCorrectFeatureObject()
{
// Arrange
var expectedFeature = new Feature
{
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>();
#pragma warning disable CS0618 // Type or member is obsolete
2025-03-07 00:17:08 +01:00
var actualFeatureObsoleted = builder.GetSection("Feature").Get<Feature>();
#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-03-07 00:17:08 +01:00
[TestMethod]
public void Get_ShouldReturnCorrectValueAsString()
{
// Arrange
var expectedFeature = "123";
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-03-07 00:17:08 +01:00
// Act
var actualFeature = builder.GetSection("AnotherSetting:Temperature").Get<int>("Indoor:Max:Limit");
2025-03-07 00:17:08 +01:00
// Assert
actualFeature.ShouldBe(expectedFeature);
}
2025-03-07 00:17:08 +01:00
[TestMethod]
public void Get_ShouldReturnCorrectValueAsNullBool()
{
2025-03-07 00:17:08 +01:00
// Arrange
bool? expectedFeature = null;
2025-03-07 00:17:08 +01:00
var configRoot = new ConfigurationBuilder()
.AddJsonFile($"{_testFolder}appconfiguration.dev.json")
.Build();
// Act
var actualFeature = configRoot.Get<bool?>("MissingKey:WhatThen");
2025-03-07 00:17:08 +01:00
// Assert
actualFeature.ShouldBe(expectedFeature);
}
2025-03-07 00:17:08 +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; }
}