diff --git a/OI-Metrology.sln b/OI-Metrology.sln index 15f46c7..a37fb8b 100644 --- a/OI-Metrology.sln +++ b/OI-Metrology.sln @@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shared", "Shared\Shared.csp EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Viewer", "Viewer\Viewer.csproj", "{25C86DF8-EC1A-4D4B-AD4E-6561174824B9}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{B67FB8C4-402E-4D53-90A6-90F6FDB9D082}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -30,5 +32,9 @@ Global {25C86DF8-EC1A-4D4B-AD4E-6561174824B9}.Debug|Any CPU.Build.0 = Debug|Any CPU {25C86DF8-EC1A-4D4B-AD4E-6561174824B9}.Release|Any CPU.ActiveCfg = Release|Any CPU {25C86DF8-EC1A-4D4B-AD4E-6561174824B9}.Release|Any CPU.Build.0 = Release|Any CPU + {B67FB8C4-402E-4D53-90A6-90F6FDB9D082}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B67FB8C4-402E-4D53-90A6-90F6FDB9D082}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B67FB8C4-402E-4D53-90A6-90F6FDB9D082}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B67FB8C4-402E-4D53-90A6-90F6FDB9D082}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/Shared/Shared.csproj b/Shared/Shared.csproj index 65b69d9..9e48445 100644 --- a/Shared/Shared.csproj +++ b/Shared/Shared.csproj @@ -29,11 +29,7 @@ Linux - - - - diff --git a/Tests/Models/AppSettings.cs b/Tests/Models/AppSettings.cs new file mode 100644 index 0000000..9bf5eeb --- /dev/null +++ b/Tests/Models/AppSettings.cs @@ -0,0 +1,60 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace OI.Metrology.Tests.Models; + +public class AppSettings +{ + + protected string _ApiLoggingContentTypes; + protected string _ApiLoggingPathPrefixes; + protected string _ApiLogPath; + protected string _AttachmentPath; + protected string _BuildNumber; + protected string _Company; + protected string _ConnectionString; + protected string _GitCommitSeven; + protected string _InboundApiAllowedIPList; + protected string _MonARessource; + protected string _OIExportPath; + protected string _URLs; + protected string _WorkingDirectoryName; + public string ApiLoggingContentTypes => _ApiLoggingContentTypes; + public string ApiLoggingPathPrefixes => _ApiLoggingPathPrefixes; + public string ApiLogPath => _ApiLogPath; + public string AttachmentPath => _AttachmentPath; + public string BuildNumber => _BuildNumber; + public string Company => _Company; + public string ConnectionString => _ConnectionString; + public string GitCommitSeven => _GitCommitSeven; + public string InboundApiAllowedIPList => _InboundApiAllowedIPList; + public string MonARessource => _MonARessource; + public string OIExportPath => _OIExportPath; + public string URLs => _URLs; + public string WorkingDirectoryName => _WorkingDirectoryName; + + [JsonConstructor] + public AppSettings(string apiLoggingContentTypes, string apiLoggingPathPrefixes, string apiLogPath, string attachmentPath, string buildNumber, string company, string connectionString, string gitCommitSeven, string inboundApiAllowedIPList, string monARessource, string oiExportPath, string urls, string workingDirectoryName) + { + _ApiLoggingContentTypes = apiLoggingContentTypes; + _ApiLoggingPathPrefixes = apiLoggingPathPrefixes; + _ApiLogPath = apiLogPath; + _AttachmentPath = attachmentPath; + _BuildNumber = buildNumber; + _Company = company; + _ConnectionString = connectionString; + _GitCommitSeven = gitCommitSeven; + _InboundApiAllowedIPList = inboundApiAllowedIPList; + _MonARessource = monARessource; + _OIExportPath = oiExportPath; + _URLs = urls; + _WorkingDirectoryName = workingDirectoryName; + } + + public override string ToString() + { + string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true }); + return result; + } + +} \ No newline at end of file diff --git a/Tests/Models/Binder/AppSettings.cs b/Tests/Models/Binder/AppSettings.cs new file mode 100644 index 0000000..a1cce91 --- /dev/null +++ b/Tests/Models/Binder/AppSettings.cs @@ -0,0 +1,46 @@ +using System.ComponentModel.DataAnnotations; +using System.Text.Json; + +namespace OI.Metrology.Tests.Models.Binder; + +public class AppSettings +{ + + [Display(Name = "Api Logging Content Types"), Required] public string ApiLoggingContentTypes { get; set; } + [Display(Name = "Api Logging Path Prefixes"), Required] public string ApiLoggingPathPrefixes { get; set; } + [Display(Name = "Api Log Path"), Required] public string ApiLogPath { get; set; } + [Display(Name = "Attachment Path"), Required] public string AttachmentPath { get; set; } + [Display(Name = "Build Number"), Required] public string BuildNumber { get; set; } + [Display(Name = "Company"), Required] public string Company { get; set; } + [Display(Name = "Connection String"), Required] public string ConnectionString { get; set; } + [Display(Name = "Git Commit Seven"), Required] public string GitCommitSeven { get; set; } + [Display(Name = "Inbound Api Allowed IP List"), Required] public string InboundApiAllowedIPList { get; set; } + [Display(Name = "MonA Ressource"), Required] public string MonARessource { get; set; } + [Display(Name = "OI Export Path"), Required] public string OIExportPath { get; set; } + [Display(Name = "URLs"), Required] public string URLs { get; set; } + [Display(Name = "Working Directory Name"), Required] public string WorkingDirectoryName { get; set; } + + public AppSettings() + { + ApiLoggingContentTypes = string.Empty; + ApiLoggingPathPrefixes = string.Empty; + ApiLogPath = string.Empty; + AttachmentPath = string.Empty; + BuildNumber = string.Empty; + Company = string.Empty; + ConnectionString = string.Empty; + GitCommitSeven = string.Empty; + InboundApiAllowedIPList = string.Empty; + MonARessource = string.Empty; + OIExportPath = string.Empty; + URLs = string.Empty; + WorkingDirectoryName = string.Empty; + } + + public override string ToString() + { + string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true }); + return result; + } + +} \ No newline at end of file diff --git a/Tests/Models/Stateless/AppSettings.cs b/Tests/Models/Stateless/AppSettings.cs new file mode 100644 index 0000000..3c33475 --- /dev/null +++ b/Tests/Models/Stateless/AppSettings.cs @@ -0,0 +1,44 @@ +using Microsoft.Extensions.Configuration; +using System; +using System.Linq; +using System.Text.Json; + +namespace OI.Metrology.Tests.Models.Stateless; + +public abstract class AppSettings +{ + +#nullable enable + + public static Models.AppSettings Get(IConfigurationRoot configurationRoot) + { + Models.AppSettings? result; + Binder.AppSettings appSettings = configurationRoot.Get(); + string json = JsonSerializer.Serialize(appSettings, new JsonSerializerOptions() { WriteIndented = true }); + result = JsonSerializer.Deserialize(json); + if (result is null) + throw new Exception(json); + if (string.IsNullOrEmpty(result.Company)) + throw new Exception(json); + string jsonThis = result.ToString(); + if (jsonThis != json) + { + int? check = null; + int min = new int[] { json.Length, jsonThis.Length }.Min(); + for (int i = 0; i < min; i++) + { + if (json[i] == jsonThis[i]) + continue; + check = i; + break; + } + if (check is null) + throw new Exception(); + string a = json[..check.Value].Split(',')[^1]; + string b = json[check.Value..].Split(',')[0]; + throw new Exception($"{a}{b}"); + } + return result; + } + +} \ No newline at end of file diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj new file mode 100644 index 0000000..39e1ed3 --- /dev/null +++ b/Tests/Tests.csproj @@ -0,0 +1,49 @@ + + + enable + false + 10.0 + enable + win-x64 + net6.0 + + + true + true + true + + + Windows + + + OSX + + + Linux + + + + + + + + + + + + + + + + + + + + + Always + + + Always + + + \ No newline at end of file diff --git a/Tests/UnitTestArchive.cs b/Tests/UnitTestArchive.cs new file mode 100644 index 0000000..cd8bfa2 --- /dev/null +++ b/Tests/UnitTestArchive.cs @@ -0,0 +1,62 @@ +using Microsoft.Extensions.Configuration; +using Serilog; +using System.Reflection; +using OI.Metrology.Tests.Models; +using OI.Metrology.Shared.Models; + +namespace View_by_Distance.Tests; + +[TestClass] +public class UnitTestArchive +{ + + private readonly ILogger _Logger; + private readonly AppSettings _AppSettings; + private readonly string _WorkingDirectory; + private readonly IConfigurationRoot _ConfigurationRoot; + + public UnitTestArchive() + { + ILogger logger; + AppSettings appSettings; + string workingDirectory; + IConfigurationRoot configurationRoot; + LoggerConfiguration loggerConfiguration = new(); + Assembly assembly = Assembly.GetExecutingAssembly(); + IConfigurationBuilder configurationBuilder = new ConfigurationBuilder() + .AddEnvironmentVariables() + .AddJsonFile("appsettings.Development.json"); + configurationRoot = configurationBuilder.Build(); + appSettings = OI.Metrology.Tests.Models.Stateless.AppSettings.Get(configurationRoot); + workingDirectory = IWorkingDirectory.GetWorkingDirectory(assembly.GetName().Name, appSettings.WorkingDirectoryName); + Environment.SetEnvironmentVariable(nameof(workingDirectory), workingDirectory); + _ = ConfigurationLoggerConfigurationExtensions.Configuration(loggerConfiguration.ReadFrom, configurationRoot); + Log.Logger = loggerConfiguration.CreateLogger(); + logger = Log.ForContext(); + logger.Information("Complete"); + _Logger = logger; + _AppSettings = appSettings; + _WorkingDirectory = workingDirectory; + _ConfigurationRoot = configurationRoot; + } + + [TestMethod] + public void TestMethodNull() + { + Assert.IsFalse(_Logger is null); + Assert.IsFalse(_AppSettings is null); + Assert.IsFalse(_WorkingDirectory is null); + Assert.IsFalse(_ConfigurationRoot is null); + } + + [TestMethod] + public void TestMethodArchive() + { + Assert.IsFalse(_Logger is null); + Assert.IsFalse(_AppSettings is null); + Assert.IsFalse(_WorkingDirectory is null); + Assert.IsFalse(_ConfigurationRoot is null); + + } + +} \ No newline at end of file diff --git a/Tests/UnitTestExample.cs b/Tests/UnitTestExample.cs new file mode 100644 index 0000000..36e8998 --- /dev/null +++ b/Tests/UnitTestExample.cs @@ -0,0 +1,52 @@ +using Microsoft.Extensions.Configuration; +using Serilog; +using System.Reflection; +using OI.Metrology.Tests.Models; +using OI.Metrology.Shared.Models; + +namespace View_by_Distance.Tests; + +[TestClass] +public class UnitTestExample +{ + + private readonly ILogger _Logger; + private readonly AppSettings _AppSettings; + private readonly string _WorkingDirectory; + private readonly IConfigurationRoot _ConfigurationRoot; + + public UnitTestExample() + { + ILogger logger; + AppSettings appSettings; + string workingDirectory; + IConfigurationRoot configurationRoot; + LoggerConfiguration loggerConfiguration = new(); + Assembly assembly = Assembly.GetExecutingAssembly(); + IConfigurationBuilder configurationBuilder = new ConfigurationBuilder() + .AddEnvironmentVariables() + .AddJsonFile("appsettings.Development.json"); + configurationRoot = configurationBuilder.Build(); + appSettings = OI.Metrology.Tests.Models.Stateless.AppSettings.Get(configurationRoot); + workingDirectory = IWorkingDirectory.GetWorkingDirectory(assembly.GetName().Name, appSettings.WorkingDirectoryName); + Environment.SetEnvironmentVariable(nameof(workingDirectory), workingDirectory); + _ = ConfigurationLoggerConfigurationExtensions.Configuration(loggerConfiguration.ReadFrom, configurationRoot); + Log.Logger = loggerConfiguration.CreateLogger(); + logger = Log.ForContext(); + logger.Information("Complete"); + _Logger = logger; + _AppSettings = appSettings; + _WorkingDirectory = workingDirectory; + _ConfigurationRoot = configurationRoot; + } + + [TestMethod] + public void TestMethodNull() + { + Assert.IsFalse(_Logger is null); + Assert.IsFalse(_AppSettings is null); + Assert.IsFalse(_WorkingDirectory is null); + Assert.IsFalse(_ConfigurationRoot is null); + } + +} \ No newline at end of file diff --git a/Tests/Usings.cs b/Tests/Usings.cs new file mode 100644 index 0000000..ab67c7e --- /dev/null +++ b/Tests/Usings.cs @@ -0,0 +1 @@ +global using Microsoft.VisualStudio.TestTools.UnitTesting; \ No newline at end of file diff --git a/Tests/appsettings.Development.json b/Tests/appsettings.Development.json new file mode 100644 index 0000000..4b03ad8 --- /dev/null +++ b/Tests/appsettings.Development.json @@ -0,0 +1,60 @@ +{ + "AllowedHosts": "*", + "ApiLoggingContentTypes": "application/json", + "ApiLoggingPathPrefixes": "/api/inbound", + "ApiLogPath": "D:\\Metrology\\MetrologyAPILogs", + "AttachmentPath": "\\\\messv02ecc1.ec.local\\EC_Metrology_Si\\MetrologyAttachments", + "BuildNumber": "1", + "Company": "Infineon Technologies Americas Corp.", + "ConnectionString": "Data Source=messv01ec.ec.local\\PROD1,53959;Integrated Security=True;Initial Catalog=Metrology_Archive;", + "GitCommitSeven": "1234567", + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Log4netProvider": "Debug", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "InboundApiAllowedIPList": "", + "OIExportPath": "\\\\openinsight-db-srv.na.infineon.com\\apps\\Metrology\\Data", + "Serilog": { + "Using": [ + "Serilog.Sinks.Console", + "Serilog.Sinks.File" + ], + "MinimumLevel": "Debug", + "WriteTo": [ + { + "Name": "Debug", + "Args": { + "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] ({SourceContext}.{MethodName}) ({InstanceId}) ({RemoteIpAddress}) {Message}{NewLine}{Exception}" + } + }, + { + "Name": "Console", + "Args": { + "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] ({SourceContext}.{MethodName}) ({InstanceId}) ({RemoteIpAddress}) {Message}{NewLine}{Exception}" + } + }, + { + "Name": "File", + "Args": { + "path": "%workingDirectory% - Log/log-.txt", + "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] ({SourceContext}.{MethodName}) ({InstanceId}) ({RemoteIpAddress}) {Message}{NewLine}{Exception}", + "rollingInterval": "Hour" + } + } + ], + "Enrich": [ + "FromLogContext", + "WithMachineName", + "WithThreadId" + ], + "Properties": { + "Application": "Sample" + } + }, + "URLs": "https://localhost:7130;http://localhost:5126", + "WorkingDirectoryName": "IFXApps" +} \ No newline at end of file diff --git a/Tests/appsettings.json b/Tests/appsettings.json new file mode 100644 index 0000000..8882280 --- /dev/null +++ b/Tests/appsettings.json @@ -0,0 +1,60 @@ +{ + "AllowedHosts": "*", + "ApiLoggingContentTypes": "application/json", + "ApiLoggingPathPrefixes": "/api/inbound", + "ApiLogPath": "D:\\Metrology\\MetrologyAPILogs", + "AttachmentPath": "\\\\messv02ecc1.ec.local\\EC_Metrology_Si\\MetrologyAttachments", + "BuildNumber": "1", + "Company": "Infineon Technologies Americas Corp.", + "ConnectionString": "Data Source=messv01ec.ec.local\\PROD1,53959;Integrated Security=True;Initial Catalog=Metrology_Archive;", + "GitCommitSeven": "1234567", + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Log4netProvider": "Debug", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "InboundApiAllowedIPList": "", + "OIExportPath": "\\\\openinsight-db-srv.na.infineon.com\\apps\\Metrology\\Data", + "Serilog": { + "Using": [ + "Serilog.Sinks.Console", + "Serilog.Sinks.File" + ], + "MinimumLevel": "Debug", + "WriteTo": [ + { + "Name": "Debug", + "Args": { + "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] ({SourceContext}.{MethodName}) ({InstanceId}) ({RemoteIpAddress}) {Message}{NewLine}{Exception}" + } + }, + { + "Name": "Console", + "Args": { + "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] ({SourceContext}.{MethodName}) ({InstanceId}) ({RemoteIpAddress}) {Message}{NewLine}{Exception}" + } + }, + { + "Name": "File", + "Args": { + "path": "%workingDirectory% - Log/log-.txt", + "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] ({SourceContext}.{MethodName}) ({InstanceId}) ({RemoteIpAddress}) {Message}{NewLine}{Exception}", + "rollingInterval": "Hour" + } + } + ], + "Enrich": [ + "FromLogContext", + "WithMachineName", + "WithThreadId" + ], + "Properties": { + "Application": "Sample" + } + }, + "URLs": "http://localhost:5002;", + "WorkingDirectoryName": "IFXApps" +} \ No newline at end of file