From e73f428c49db13dd7b89a8727197f941b81fd31f Mon Sep 17 00:00:00 2001 From: "Janus C. H. Knudsen" Date: Wed, 19 Feb 2025 14:21:40 +0100 Subject: [PATCH] wip --- Core/Logging/SeqLogger.cs | 2 +- .../JsonConfigurationProviderTests.cs | 28 ++-- Tests/TestFixture.cs | 140 +++++++++--------- Tests/Tests.csproj | 2 +- Tests/appconfiguration.dev.json | 8 + Tests/xappconfiguration.dev.json | 60 -------- 6 files changed, 96 insertions(+), 144 deletions(-) create mode 100644 Tests/appconfiguration.dev.json delete mode 100644 Tests/xappconfiguration.dev.json diff --git a/Core/Logging/SeqLogger.cs b/Core/Logging/SeqLogger.cs index c1a7564..cf3868a 100644 --- a/Core/Logging/SeqLogger.cs +++ b/Core/Logging/SeqLogger.cs @@ -116,7 +116,7 @@ namespace Core.Logging { "ResponseCode", req.ResponseCode }, { "Duration", req.Duration.TotalMilliseconds } }; - + foreach (var prop in req.Properties) { seqEvent.Add(prop.Key, prop.Value); diff --git a/Tests/ConfigurationTests/JsonConfigurationProviderTests.cs b/Tests/ConfigurationTests/JsonConfigurationProviderTests.cs index 20497c3..cae464b 100644 --- a/Tests/ConfigurationTests/JsonConfigurationProviderTests.cs +++ b/Tests/ConfigurationTests/JsonConfigurationProviderTests.cs @@ -9,7 +9,11 @@ namespace Tests.ConfigurationTests [TestClass] public class JsonConfigurationProviderTests : TestFixture { - public JsonConfigurationProviderTests() : base("ConfigurationTests") { } + const string _testFolder = "ConfigurationTests/"; + + public JsonConfigurationProviderTests() : base(_testFolder) { } + + [TestMethod] public void GetSection_ShouldReturnCorrectFeatureSection() { @@ -21,7 +25,7 @@ namespace Tests.ConfigurationTests }") as JToken; var builder = new ConfigurationBuilder() - .AddJsonFile("appconfiguration.dev.json") + .AddJsonFile($"{_testFolder}appconfiguration.dev.json") .Build(); // Act @@ -44,8 +48,8 @@ namespace Tests.ConfigurationTests }; var builder = new ConfigurationBuilder() - .AddJsonFile("appconfiguration.dev.json") - .Build(); + .AddJsonFile($"{_testFolder}appconfiguration.dev.json") + .Build(); // Act var actualFeature = builder.GetSection("Feature").ToObject(); @@ -64,8 +68,8 @@ namespace Tests.ConfigurationTests var expectedFeature = "123"; var builder = new ConfigurationBuilder() - .AddJsonFile("appconfiguration.dev.json") - .Build(); + .AddJsonFile($"{_testFolder}appconfiguration.dev.json") + .Build(); // Act var actualFeature = builder.GetSection("AnotherSetting").Get("Thresholds:High"); @@ -83,8 +87,8 @@ namespace Tests.ConfigurationTests var expected = "SHA256"; var builder = new ConfigurationBuilder() - .AddJsonFile("appconfiguration.dev.json") - .Build(); + .AddJsonFile($"{_testFolder}appconfiguration.dev.json") + .Build(); // Act var actual = builder["Authentication"]; @@ -99,8 +103,8 @@ namespace Tests.ConfigurationTests var expectedFeature = 22; var builder = new ConfigurationBuilder() - .AddJsonFile("appconfiguration.dev.json") - .Build(); + .AddJsonFile($"{_testFolder}appconfiguration.dev.json") + .Build(); // Act var actualFeature = builder.GetSection("AnotherSetting:Temperature").Get("Indoor:Max:Limit"); @@ -115,8 +119,8 @@ namespace Tests.ConfigurationTests var expectedFeature = true; var configRoot = new ConfigurationBuilder() - .AddJsonFile("appconfiguration.dev.json") - .AddSmartConfig() + .AddJsonFile($"{_testFolder}appconfiguration.dev.json") + .AddSmartConfig() .Build(); // Act diff --git a/Tests/TestFixture.cs b/Tests/TestFixture.cs index 5ca8926..c357397 100644 --- a/Tests/TestFixture.cs +++ b/Tests/TestFixture.cs @@ -8,92 +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 - { - private readonly string _configurationFilePath; + /// + /// Act as base class for tests. Avoids duplication of test setup code + /// + [TestClass] + public abstract partial class TestFixture + { + private readonly string _configurationFilePath; - protected IContainer Container { get; private set; } - protected ContainerBuilder ContainerBuilder { get; private set; } + protected IContainer Container { get; private set; } + protected ContainerBuilder ContainerBuilder { get; private set; } - public virtual IConfigurationRoot Configuration() - { - var configuration = new ConfigurationBuilder() - .AddJsonFile($"{_configurationFilePath}appconfiguration.dev.json") - .Build(); + public virtual IConfigurationRoot Configuration() + { + var configuration = new ConfigurationBuilder() + .AddJsonFile($"{_configurationFilePath}appconfiguration.dev.json") + .Build(); - return configuration; - } + return configuration; + } - protected TestFixture() - { - CreateContainerBuilder(); - Container = ContainerBuilder.Build(); - } - public TestFixture(string configurationFilePath) : this() - { - _configurationFilePath = configurationFilePath; - } - protected virtual void CreateContainerBuilder() - { - IConfigurationRoot configuration = Configuration(); + protected TestFixture() : this(null) { } + public TestFixture(string configurationFilePath) + { + if (configurationFilePath is not null) + _configurationFilePath = configurationFilePath?.TrimEnd('/') + "/"; - //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(); + CreateContainerBuilder(); + Container = ContainerBuilder.Build(); + } + protected virtual void CreateContainerBuilder() + { + IConfigurationRoot configuration = Configuration(); - //Log.Logger = logger; + //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.Verbose("Is thos work"); - var builder = new ContainerBuilder(); + //Log.Logger = logger; + + //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 ab08f19..49bd897 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -27,7 +27,7 @@ - + Always diff --git a/Tests/appconfiguration.dev.json b/Tests/appconfiguration.dev.json new file mode 100644 index 0000000..7afbfa5 --- /dev/null +++ b/Tests/appconfiguration.dev.json @@ -0,0 +1,8 @@ +{ + "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/" + } +} \ No newline at end of file diff --git a/Tests/xappconfiguration.dev.json b/Tests/xappconfiguration.dev.json deleted file mode 100644 index 9575ea2..0000000 --- a/Tests/xappconfiguration.dev.json +++ /dev/null @@ -1,60 +0,0 @@ -{ - "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