From 75e04062516d12b2a7c833957e4d465bb6909462 Mon Sep 17 00:00:00 2001 From: Janus Knudsen Date: Tue, 18 Feb 2025 17:35:00 +0100 Subject: [PATCH] wip on tests --- .../JsonConfigurationProviderTests.cs | 201 +++++++++--------- .../appconfiguration.dev.json | 60 ++++++ Tests/TestFixture.cs | 135 ++++++------ Tests/Tests.csproj | 3 + 4 files changed, 234 insertions(+), 165 deletions(-) create mode 100644 Tests/ConfigurationTests/appconfiguration.dev.json diff --git a/Tests/ConfigurationTests/JsonConfigurationProviderTests.cs b/Tests/ConfigurationTests/JsonConfigurationProviderTests.cs index 921961d..ca6d05d 100644 --- a/Tests/ConfigurationTests/JsonConfigurationProviderTests.cs +++ b/Tests/ConfigurationTests/JsonConfigurationProviderTests.cs @@ -11,131 +11,132 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; namespace Tests.ConfigurationTests { - [TestClass] - public class JsonConfigurationProviderTests : TestFixture - { - [TestMethod] - public void GetSection_ShouldReturnCorrectFeatureSection() - { - // Arrange - var expectedJObject = JObject.Parse(@"{ + [TestClass] + public class JsonConfigurationProviderTests : TestFixture + { + public JsonConfigurationProviderTests() : base("ConfigurationTests") { } + [TestMethod] + public void GetSection_ShouldReturnCorrectFeatureSection() + { + // Arrange + var expectedJObject = JObject.Parse(@"{ 'Enabled': true, 'RolloutPercentage': 25, 'AllowedUserGroups': ['beta'] }") as JToken; - var builder = new ConfigurationBuilder() - .AddJsonFile("appconfiguration.dev.json") - .Build(); + var builder = new ConfigurationBuilder() + .AddJsonFile("appconfiguration.dev.json") + .Build(); - // Act - var section = builder.GetSection("Feature"); + // Act + var section = builder.GetSection("Feature"); - // Assert - section.Should().NotBeNull(); - section.Value.Should().BeEquivalentTo(expectedJObject); - } + // Assert + section.Should().NotBeNull(); + section.Value.Should().BeEquivalentTo(expectedJObject); + } - [TestMethod] - public void Get_ShouldReturnCorrectFeatureObject() - { - // Arrange - var expectedFeature = new Feature - { - Enabled = true, - RolloutPercentage = 25, - AllowedUserGroups = new() { "beta" } - }; + [TestMethod] + public void Get_ShouldReturnCorrectFeatureObject() + { + // Arrange + var expectedFeature = new Feature + { + Enabled = true, + RolloutPercentage = 25, + AllowedUserGroups = new() { "beta" } + }; - var builder = new ConfigurationBuilder() - .AddJsonFile("appconfiguration.dev.json") - .Build(); + var builder = new ConfigurationBuilder() + .AddJsonFile("appconfiguration.dev.json") + .Build(); - // Act - var actualFeature = builder.GetSection("Feature").ToObject(); + // Act + var actualFeature = builder.GetSection("Feature").ToObject(); #pragma warning disable CS0618 // Type or member is obsolete - var actualFeatureObsoleted = builder.GetSection("Feature").Get(); + var actualFeatureObsoleted = builder.GetSection("Feature").Get(); #pragma warning restore CS0618 // Type or member is obsolete - // Assert - actualFeature.Should().BeEquivalentTo(expectedFeature); - actualFeatureObsoleted.Should().BeEquivalentTo(expectedFeature); + // Assert + actualFeature.Should().BeEquivalentTo(expectedFeature); + actualFeatureObsoleted.Should().BeEquivalentTo(expectedFeature); - } - [TestMethod] - public void Get_ShouldReturnCorrectValueAsString() - { - // Arrange - var expectedFeature = "123"; + } + [TestMethod] + public void Get_ShouldReturnCorrectValueAsString() + { + // Arrange + var expectedFeature = "123"; - var builder = new ConfigurationBuilder() - .AddJsonFile("appconfiguration.dev.json") - .Build(); + var builder = new ConfigurationBuilder() + .AddJsonFile("appconfiguration.dev.json") + .Build(); - // Act - var actualFeature = builder.GetSection("AnotherSetting").Get("Thresholds:High"); + // Act + var actualFeature = builder.GetSection("AnotherSetting").Get("Thresholds:High"); - // Assert - actualFeature.Should().BeEquivalentTo(expectedFeature); - } - /// - /// Testing a stupid indexer for compability with Microsoft ConfigurationBuilder - /// - [TestMethod] - public void Indexer_ShouldReturnValueAsString() - { - // Arrange - var expected = "SHA256"; + // Assert + actualFeature.Should().BeEquivalentTo(expectedFeature); + } + /// + /// Testing a stupid indexer for compability with Microsoft ConfigurationBuilder + /// + [TestMethod] + public void Indexer_ShouldReturnValueAsString() + { + // Arrange + var expected = "SHA256"; - var builder = new ConfigurationBuilder() - .AddJsonFile("appconfiguration.dev.json") - .Build(); + var builder = new ConfigurationBuilder() + .AddJsonFile("appconfiguration.dev.json") + .Build(); - // Act - var actual = builder["Authentication"]; + // Act + var actual = builder["Authentication"]; - // Assert - actual.Should().BeEquivalentTo(expected); - } - [TestMethod] - public void Get_ShouldReturnCorrectValueAsInt() - { - // Arrange - var expectedFeature = 22; + // Assert + actual.Should().BeEquivalentTo(expected); + } + [TestMethod] + public void Get_ShouldReturnCorrectValueAsInt() + { + // Arrange + var expectedFeature = 22; - var builder = new ConfigurationBuilder() - .AddJsonFile("appconfiguration.dev.json") - .Build(); + var builder = new ConfigurationBuilder() + .AddJsonFile("appconfiguration.dev.json") + .Build(); - // Act - var actualFeature = builder.GetSection("AnotherSetting:Temperature").Get("Indoor:Max:Limit"); + // Act + var actualFeature = builder.GetSection("AnotherSetting:Temperature").Get("Indoor:Max:Limit"); - // Assert - actualFeature.Should().Be(expectedFeature); - } - [TestMethod] - public void Get_ShouldReturnCorrectValueAsBool() - { - // Arrange - var expectedFeature = true; + // Assert + actualFeature.Should().Be(expectedFeature); + } + [TestMethod] + public void Get_ShouldReturnCorrectValueAsBool() + { + // Arrange + var expectedFeature = true; - var configRoot = new ConfigurationBuilder() - .AddJsonFile("appconfiguration.dev.json") - .AddSmartConfig() - .Build(); + var configRoot = new ConfigurationBuilder() + .AddJsonFile("appconfiguration.dev.json") + .AddSmartConfig() + .Build(); - // Act - var actualFeature = configRoot.Get("Database:UseSSL"); + // Act + var actualFeature = configRoot.Get("Database:UseSSL"); - // Assert - actualFeature.Should().Be(expectedFeature); - } - } + // Assert + actualFeature.Should().Be(expectedFeature); + } + } - public class Feature - { - public bool Enabled { get; set; } - public int RolloutPercentage { get; set; } - public List AllowedUserGroups { get; set; } - } + public class Feature + { + public bool Enabled { get; set; } + public int RolloutPercentage { get; set; } + public List AllowedUserGroups { get; set; } + } } \ No newline at end of file diff --git a/Tests/ConfigurationTests/appconfiguration.dev.json b/Tests/ConfigurationTests/appconfiguration.dev.json new file mode 100644 index 0000000..9575ea2 --- /dev/null +++ b/Tests/ConfigurationTests/appconfiguration.dev.json @@ -0,0 +1,60 @@ +{ + "AllowedHosts": "*", + "ConnectionStrings": { + "DefaultConnection": "Host=192.168.1.57;Port=5432;Database=ptmain;User Id=sathumper;Password=3911;" + }, + "ApplicationInsights": { + "ConnectionString": "InstrumentationKey=6d2e76ee-5343-4691-a5e3-81add43cb584;IngestionEndpoint=https://northeurope-0.in.applicationinsights.azure.com/" + }, + "Authentication": "SHA256", + "Feature": { + "Enabled": true, + "RolloutPercentage": 25, + "AllowedUserGroups": [ "beta" ] + }, + "AnotherSetting": { + + "Thresholds": { + "High": "123", + "Low": "-1" + }, + "Temperature": { + + "Indoor": { + "Max": { "Limit": 22 }, + "Min": { "Limit": 18 } + }, + "Outdoor": { + "Max": { "Limit": 12 }, + "Min": { "Limit": 9 } + } + } + }, + "Serilog": { + "MinimumLevel": { + "Default": "Information", + "Override": { + "Microsoft": "Warning", + "System": "Warning" + } + }, + "WriteTo": [ + { + "Name": "Seq", + "Args": { + "serverUrl": "http://localhost:5341", + "apiKey": "" + } + } + ], + "Enrich": [ + "WithMachineName", + "WithThreadId", + "WithProcessId", + "WithEnvironmentName" + ], + "Properties": { + "Application": "PlanTempus" + } + } +} \ No newline at end of file diff --git a/Tests/TestFixture.cs b/Tests/TestFixture.cs index fdb8df0..5ca8926 100644 --- a/Tests/TestFixture.cs +++ b/Tests/TestFixture.cs @@ -8,87 +8,92 @@ using Microsoft.Extensions.Logging; using Core.Configurations.JsonConfigProvider; namespace Tests { - /// - /// Act as base class for tests. Avoids duplication of test setup code - /// - [TestClass] - public abstract partial class TestFixture - { - protected IContainer Container { get; private set; } - protected ContainerBuilder ContainerBuilder { get; private set; } + /// + /// Act as base class for tests. Avoids duplication of test setup code + /// + [TestClass] + public abstract partial class TestFixture + { + private readonly string _configurationFilePath; - public virtual IConfigurationRoot Configuration() - { - var configuration = new ConfigurationBuilder() - .AddJsonFile("appconfiguration.dev.json") - .Build(); + protected IContainer Container { get; private set; } + protected ContainerBuilder ContainerBuilder { get; private set; } - return configuration; - } + public virtual IConfigurationRoot Configuration() + { + var configuration = new ConfigurationBuilder() + .AddJsonFile($"{_configurationFilePath}appconfiguration.dev.json") + .Build(); - protected TestFixture() - { - CreateContainerBuilder(); - Container = ContainerBuilder.Build(); - } + return configuration; + } - protected virtual void CreateContainerBuilder() - { - IConfigurationRoot configuration = Configuration(); + protected TestFixture() + { + CreateContainerBuilder(); + Container = ContainerBuilder.Build(); + } + public TestFixture(string configurationFilePath) : this() + { + _configurationFilePath = configurationFilePath; + } + protected virtual void CreateContainerBuilder() + { + IConfigurationRoot configuration = Configuration(); - //var logger = new LoggerConfiguration() - // .MinimumLevel.Verbose() - // .MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Warning) - // .MinimumLevel.Override("System", Serilog.Events.LogEventLevel.Error) - // .WriteTo.Seq("http://localhost:5341", apiKey: "Gt8hS9ClGNfOCAdswDlW") - // .WriteTo.ApplicationInsights(configuration.Get("ApplicationInsights:ConnectionString"), - // TelemetryConverter.Traces) - // .Enrich.FromLogContext() - // .Enrich.WithMachineName() - // .CreateLogger(); + //var logger = new LoggerConfiguration() + // .MinimumLevel.Verbose() + // .MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Warning) + // .MinimumLevel.Override("System", Serilog.Events.LogEventLevel.Error) + // .WriteTo.Seq("http://localhost:5341", apiKey: "Gt8hS9ClGNfOCAdswDlW") + // .WriteTo.ApplicationInsights(configuration.Get("ApplicationInsights:ConnectionString"), + // TelemetryConverter.Traces) + // .Enrich.FromLogContext() + // .Enrich.WithMachineName() + // .CreateLogger(); - //Log.Logger = logger; + //Log.Logger = logger; - //Log.Logger.Verbose("Is thos work"); - var builder = new ContainerBuilder(); + //Log.Logger.Verbose("Is thos work"); + var builder = new ContainerBuilder(); - //builder.Register(c => new SerilogLoggerFactory(logger)) - // .As() - // .SingleInstance(); + //builder.Register(c => new SerilogLoggerFactory(logger)) + // .As() + // .SingleInstance(); - builder.RegisterGeneric(typeof(Logger<>)) - .As(typeof(ILogger<>)) - .SingleInstance(); + builder.RegisterGeneric(typeof(Logger<>)) + .As(typeof(ILogger<>)) + .SingleInstance(); - builder.RegisterModule(new Database.ModuleRegistry.DbPostgreSqlModule - { - ConnectionString = configuration.GetConnectionString("DefaultConnection") - }); + builder.RegisterModule(new Database.ModuleRegistry.DbPostgreSqlModule + { + ConnectionString = configuration.GetConnectionString("DefaultConnection") + }); - builder.RegisterModule(new Core.ModuleRegistry.TelemetryModule - { - TelemetryConfig = configuration.GetSection("ApplicationInsights").ToObject() - }); + builder.RegisterModule(new Core.ModuleRegistry.TelemetryModule + { + TelemetryConfig = configuration.GetSection("ApplicationInsights").ToObject() + }); - ContainerBuilder = builder; - } + ContainerBuilder = builder; + } - [TestCleanup] - public void CleanUp() - { - Trace.Flush(); - var telemetryClient = Container.Resolve(); - telemetryClient.Flush(); + [TestCleanup] + public void CleanUp() + { + Trace.Flush(); + var telemetryClient = Container.Resolve(); + telemetryClient.Flush(); - if (Container != null) - { - Container.Dispose(); - Container = null; - } - } + if (Container != null) + { + Container.Dispose(); + Container = null; + } + } - } + } } \ No newline at end of file diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index cdd90c6..0d693a6 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -30,6 +30,9 @@ Always + + Always +