From 03bd20fc8cd216ef60945a14eb946531fa18a9e3 Mon Sep 17 00:00:00 2001 From: Mike Phares Date: Thu, 2 Nov 2023 14:41:32 -0700 Subject: [PATCH] Archive --- Archive/ApiControllers/InboundController.cs | 14 +-- Archive/ApiControllers/ToolTypesController.cs | 6 +- Archive/ApiLoggingMiddleware.cs | 3 +- Archive/Controllers/ErrorHandlerController.cs | 2 +- Archive/Controllers/ExportController.cs | 11 +- Archive/Models/Binder/.editorconfig | 2 + Archive/Models/Binder/AppSettings.cs | 103 +++++++++--------- Archive/OI.Metrology.Archive.csproj | 8 +- Archive/Program.cs | 27 ++--- Archive/Repositories/MetrologyRepo.cs | 2 + Archive/Repositories/RdsMaxRepo.cs | 2 + .../Repositories/SQLDbConnectionFactory.cs | 2 + Archive/Services/AttachmentsService.cs | 2 + Archive/Services/InboundDataService.cs | 2 + Tests/UnitTestArchive.cs | 1 - Tests/UnitTestInfinityQSController.cs | 98 +++++++++-------- 16 files changed, 142 insertions(+), 143 deletions(-) create mode 100644 Archive/Models/Binder/.editorconfig diff --git a/Archive/ApiControllers/InboundController.cs b/Archive/ApiControllers/InboundController.cs index 51d947a..d4f563c 100644 --- a/Archive/ApiControllers/InboundController.cs +++ b/Archive/ApiControllers/InboundController.cs @@ -35,8 +35,8 @@ public class InboundController : ControllerBase { public bool Success { get; set; } public long HeaderID { get; set; } - public List Errors { get; set; } - public List Warnings { get; set; } + public List? Errors { get; set; } + public List? Warnings { get; set; } } // this is the main endpoint, it accepts a JSON message that contains both the header and data records together @@ -86,7 +86,7 @@ public class InboundController : ControllerBase else r.Errors.Add("Invalid json"); - if (r.Errors.Count == 0) + if (r.Errors.Count == 0 && jsonbody is not null) { try { @@ -147,14 +147,14 @@ public class InboundController : ControllerBase if (string.IsNullOrWhiteSpace(_AppSettings.InboundApiAllowedIPList)) return true; - System.Net.IPAddress remoteIP = HttpContext.Connection.RemoteIpAddress; - byte[] remoteIPBytes = remoteIP.GetAddressBytes(); + System.Net.IPAddress? remoteIP = HttpContext.Connection.RemoteIpAddress; + byte[]? remoteIPBytes = remoteIP?.GetAddressBytes(); string[] allowedIPs = _AppSettings.InboundApiAllowedIPList.Split(';'); foreach (string ip in allowedIPs) { - System.Net.IPAddress parsedIP; - if (System.Net.IPAddress.TryParse(ip, out parsedIP)) + System.Net.IPAddress? parsedIP; + if (remoteIPBytes is not null && System.Net.IPAddress.TryParse(ip, out parsedIP)) { if (parsedIP.GetAddressBytes().SequenceEqual(remoteIPBytes)) return true; diff --git a/Archive/ApiControllers/ToolTypesController.cs b/Archive/ApiControllers/ToolTypesController.cs index 33bfc36..abc5f5f 100644 --- a/Archive/ApiControllers/ToolTypesController.cs +++ b/Archive/ApiControllers/ToolTypesController.cs @@ -203,7 +203,7 @@ public class ToolTypesController : Controller if (ds.Tables[0].Rows.Count != 1) throw new Exception("Error exporting, invalid filename"); - string filename = Convert.ToString(ds.Tables[0].Rows[0][0]); + string? filename = Convert.ToString(ds.Tables[0].Rows[0][0]); // The second table has the header data if (ds.Tables[1].Rows.Count != 1) @@ -211,7 +211,7 @@ public class ToolTypesController : Controller System.Text.StringBuilder sb = new(); - foreach (object o in ds.Tables[1].Rows[0].ItemArray) + foreach (object? o in ds.Tables[1].Rows[0].ItemArray) { if ((o is not null) && (!Convert.IsDBNull(o))) _ = sb.Append(Convert.ToString(o)); @@ -221,7 +221,7 @@ public class ToolTypesController : Controller // The third table has the detail data foreach (System.Data.DataRow dr in ds.Tables[2].Rows) { - foreach (object o in dr.ItemArray) + foreach (object? o in dr.ItemArray) { if ((o is not null) && (!Convert.IsDBNull(o))) _ = sb.Append(Convert.ToString(o)); diff --git a/Archive/ApiLoggingMiddleware.cs b/Archive/ApiLoggingMiddleware.cs index faac320..67c745f 100644 --- a/Archive/ApiLoggingMiddleware.cs +++ b/Archive/ApiLoggingMiddleware.cs @@ -43,7 +43,8 @@ public class ApiLoggingMiddleware else { // if there are content type filters configured, only log is the request begins with one of them - doLogging = _AppSettings.ApiLoggingContentTypes.Split(';').Any(ct => httpContext.Request.ContentType.StartsWith(ct)); + string? contentType = httpContext.Request.ContentType; + doLogging = contentType is not null && _AppSettings.ApiLoggingContentTypes.Split(';').Any(ct => contentType.StartsWith(ct)); } } } diff --git a/Archive/Controllers/ErrorHandlerController.cs b/Archive/Controllers/ErrorHandlerController.cs index ea1ed91..2b55d00 100644 --- a/Archive/Controllers/ErrorHandlerController.cs +++ b/Archive/Controllers/ErrorHandlerController.cs @@ -14,7 +14,7 @@ public class ErrorHandlerController : Controller public IActionResult Index() { - IExceptionHandlerFeature error = HttpContext.Features.Get(); + IExceptionHandlerFeature? error = HttpContext.Features.Get(); if (error is null) { return Redirect("~/"); diff --git a/Archive/Controllers/ExportController.cs b/Archive/Controllers/ExportController.cs index f2bd78f..1eb7855 100644 --- a/Archive/Controllers/ExportController.cs +++ b/Archive/Controllers/ExportController.cs @@ -52,7 +52,7 @@ public class ExportController : Controller [Route("/ExportData")] public ActionResult ExportData(Export model) { - ToolType toolType = null; + ToolType? toolType = null; if (string.IsNullOrEmpty(model.ToolType)) ModelState.AddModelError("Exception", "Invalid selection"); else @@ -66,7 +66,7 @@ public class ExportController : Controller else if (string.IsNullOrWhiteSpace(toolType.ExportSPName)) ModelState.AddModelError("ToolType", "Tool type is not exportable"); } - if (ModelState.IsValid) + if (ModelState.IsValid && toolType?.ToolTypeName is not null && toolType.ExportSPName is not null) { try { @@ -114,10 +114,13 @@ public class ExportController : Controller { if (i > 0) _ = r.Append(','); - object v = dr[i]; if (!Convert.IsDBNull(v)) - _ = r.Append(FormatForCSV(Convert.ToString(v))); + { + string? c = Convert.ToString(v); + if (c is not null) + _ = r.Append(FormatForCSV(c)); + } } return r.ToString(); } diff --git a/Archive/Models/Binder/.editorconfig b/Archive/Models/Binder/.editorconfig new file mode 100644 index 0000000..1c444cd --- /dev/null +++ b/Archive/Models/Binder/.editorconfig @@ -0,0 +1,2 @@ +[*.cs] +csharp_preserve_single_line_statements = true \ No newline at end of file diff --git a/Archive/Models/Binder/AppSettings.cs b/Archive/Models/Binder/AppSettings.cs index aa6a0ca..d56de6f 100644 --- a/Archive/Models/Binder/AppSettings.cs +++ b/Archive/Models/Binder/AppSettings.cs @@ -1,6 +1,5 @@ using Microsoft.Extensions.Configuration; using System; -using System.ComponentModel.DataAnnotations; using System.Text.Json; namespace OI.Metrology.Archive.Models.Binder; @@ -8,25 +7,21 @@ namespace OI.Metrology.Archive.Models.Binder; public class AppSettings { -#nullable disable - - [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 Resource"), Required] public string MonAResource { get; set; } - [Display(Name = "MonA Site"), Required] public string MonASite { get; set; } - [Display(Name = "Oi 2 Sql Connection String"), Required] public string Oi2SqlConnectionString { 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; } - -#nullable restore + public string? ApiLoggingContentTypes { get; set; } + public string? ApiLoggingPathPrefixes { get; set; } + public string? ApiLogPath { get; set; } + public string? AttachmentPath { get; set; } + public string? BuildNumber { get; set; } + public string? Company { get; set; } + public string? ConnectionString { get; set; } + public string? GitCommitSeven { get; set; } + public string? InboundApiAllowedIPList { get; set; } + public string? MonAResource { get; set; } + public string? MonASite { get; set; } + public string? Oi2SqlConnectionString { get; set; } + public string? OIExportPath { get; set; } + public string? URLs { get; set; } + public string? WorkingDirectoryName { get; set; } public override string ToString() { @@ -34,41 +29,25 @@ public class AppSettings return result; } - private static Models.AppSettings Get(AppSettings appSettings) + private static Models.AppSettings Get(AppSettings? appSettings) { Models.AppSettings result; - if (appSettings is null) - throw new NullReferenceException(nameof(appSettings)); - if (appSettings.ApiLoggingContentTypes is null) - throw new NullReferenceException(nameof(ApiLoggingContentTypes)); - if (appSettings.ApiLoggingPathPrefixes is null) - throw new NullReferenceException(nameof(ApiLoggingPathPrefixes)); - if (appSettings.ApiLogPath is null) - throw new NullReferenceException(nameof(ApiLogPath)); - if (appSettings.AttachmentPath is null) - throw new NullReferenceException(nameof(AttachmentPath)); - if (appSettings.BuildNumber is null) - throw new NullReferenceException(nameof(BuildNumber)); - if (appSettings.Company is null) - throw new NullReferenceException(nameof(Company)); - if (appSettings.ConnectionString is null) - throw new NullReferenceException(nameof(ConnectionString)); - if (appSettings.GitCommitSeven is null) - throw new NullReferenceException(nameof(GitCommitSeven)); - if (appSettings.InboundApiAllowedIPList is null) - throw new NullReferenceException(nameof(InboundApiAllowedIPList)); - if (appSettings.MonAResource is null) - throw new NullReferenceException(nameof(MonAResource)); - if (appSettings.MonASite is null) - throw new NullReferenceException(nameof(MonASite)); - if (appSettings.Oi2SqlConnectionString is null) - throw new NullReferenceException(nameof(Oi2SqlConnectionString)); - if (appSettings.OIExportPath is null) - throw new NullReferenceException(nameof(OIExportPath)); - if (appSettings.URLs is null) - throw new NullReferenceException(nameof(URLs)); - if (appSettings.WorkingDirectoryName is null) - throw new NullReferenceException(nameof(WorkingDirectoryName)); + if (appSettings is null) throw new NullReferenceException(nameof(appSettings)); + if (appSettings.ApiLoggingContentTypes is null) throw new NullReferenceException(nameof(ApiLoggingContentTypes)); + if (appSettings.ApiLoggingPathPrefixes is null) throw new NullReferenceException(nameof(ApiLoggingPathPrefixes)); + if (appSettings.ApiLogPath is null) throw new NullReferenceException(nameof(ApiLogPath)); + if (appSettings.AttachmentPath is null) throw new NullReferenceException(nameof(AttachmentPath)); + if (appSettings.BuildNumber is null) throw new NullReferenceException(nameof(BuildNumber)); + if (appSettings.Company is null) throw new NullReferenceException(nameof(Company)); + if (appSettings.ConnectionString is null) throw new NullReferenceException(nameof(ConnectionString)); + if (appSettings.GitCommitSeven is null) throw new NullReferenceException(nameof(GitCommitSeven)); + if (appSettings.InboundApiAllowedIPList is null) throw new NullReferenceException(nameof(InboundApiAllowedIPList)); + if (appSettings.MonAResource is null) throw new NullReferenceException(nameof(MonAResource)); + if (appSettings.MonASite is null) throw new NullReferenceException(nameof(MonASite)); + if (appSettings.Oi2SqlConnectionString is null) throw new NullReferenceException(nameof(Oi2SqlConnectionString)); + if (appSettings.OIExportPath is null) throw new NullReferenceException(nameof(OIExportPath)); + if (appSettings.URLs is null) throw new NullReferenceException(nameof(URLs)); + if (appSettings.WorkingDirectoryName is null) throw new NullReferenceException(nameof(WorkingDirectoryName)); result = new( appSettings.ApiLoggingContentTypes, appSettings.ApiLoggingPathPrefixes, @@ -91,7 +70,23 @@ public class AppSettings public static Models.AppSettings Get(IConfigurationRoot configurationRoot) { Models.AppSettings result; - AppSettings appSettings = configurationRoot.Get(); +#pragma warning disable IL3050, IL2026 + AppSettings? appSettings = configurationRoot.Get(); +#pragma warning restore IL3050, IL2026 + if (appSettings?.ApiLoggingContentTypes is null) + { + foreach (IConfigurationProvider configurationProvider in configurationRoot.Providers) + { + if (configurationProvider is not Microsoft.Extensions.Configuration.Json.JsonConfigurationProvider jsonConfigurationProvider) + continue; + if (jsonConfigurationProvider.Source.FileProvider is not Microsoft.Extensions.FileProviders.PhysicalFileProvider physicalFileProvider) + continue; + if (!physicalFileProvider.Root.Contains("UserSecrets")) + continue; + throw new NotSupportedException(physicalFileProvider.Root); + } + throw new NotSupportedException("Not found!"); + } result = Get(appSettings); return result; } diff --git a/Archive/OI.Metrology.Archive.csproj b/Archive/OI.Metrology.Archive.csproj index 7fedc4c..c725f3e 100644 --- a/Archive/OI.Metrology.Archive.csproj +++ b/Archive/OI.Metrology.Archive.csproj @@ -8,8 +8,7 @@ disable false - 10.0 - disable + enable Exe win-x64 net7.0 @@ -35,11 +34,6 @@ - - - - - diff --git a/Archive/Program.cs b/Archive/Program.cs index fe7c0ba..16ea495 100644 --- a/Archive/Program.cs +++ b/Archive/Program.cs @@ -12,7 +12,6 @@ using OI.Metrology.Shared.Models; using OI.Metrology.Shared.Models.Stateless; using OI.Metrology.Shared.Repositories; using OI.Metrology.Shared.Services; -using Serilog; using System; using System.IO; using System.Reflection; @@ -22,13 +21,11 @@ namespace OI.Metrology.Archive; public class Program { - internal readonly AppSettings _AppSettings; - private static (string, WebApplicationOptions) Get(string[] args) { string webRootPath; Assembly assembly = Assembly.GetExecutingAssembly(); - string assemblyName = assembly.GetName()?.Name; + string? assemblyName = assembly.GetName()?.Name; if (string.IsNullOrEmpty(assemblyName)) throw new Exception(); string baseAssemblyName = assemblyName.Split('.')[0]; @@ -49,7 +46,7 @@ public class Program public static int Main(string[] args) { - LoggerConfiguration loggerConfiguration = new(); + ILogger? logger = null; (string assemblyName, WebApplicationOptions _) = Get(args); WebApplicationBuilder webApplicationBuilder = WebApplication.CreateBuilder(args); _ = webApplicationBuilder.Configuration.AddUserSecrets(); @@ -58,10 +55,6 @@ public class Program throw new Exception("Working directory name must have a value!"); string workingDirectory = IWorkingDirectory.GetWorkingDirectory(assemblyName, _AppSettings.WorkingDirectoryName); Environment.SetEnvironmentVariable(nameof(workingDirectory), workingDirectory); - _ = ConfigurationLoggerConfigurationExtensions.Configuration(loggerConfiguration.ReadFrom, webApplicationBuilder.Configuration); - _ = SerilogHostBuilderExtensions.UseSerilog(webApplicationBuilder.Host); - Log.Logger = loggerConfiguration.CreateLogger(); - Serilog.ILogger log = Log.ForContext(); try { _ = webApplicationBuilder.Services.Configure(options => options.SuppressModelStateInvalidFilter = true); @@ -95,6 +88,7 @@ public class Program #pragma warning restore } WebApplication webApplication = webApplicationBuilder.Build(); + logger = webApplication.Services.GetRequiredService>(); if (!webApplicationBuilder.Environment.IsDevelopment()) { _ = webApplication.UseExceptionHandler("/Error"); @@ -110,27 +104,22 @@ public class Program _ = webApplication.UseSwagger(); _ = webApplication.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Archive V1")); } - _ = webApplication.Lifetime.ApplicationStopped.Register(Log.CloseAndFlush); - _ = ApplicationBuilderSerilogClientExtensions.UseSerilogIngestion(webApplication); - _ = SerilogApplicationBuilderExtensions.UseSerilogRequestLogging(webApplication); _ = webApplication.UseFileServer(enableDirectoryBrowsing: true); _ = webApplication.UseStaticFiles(); _ = webApplication.UseSession(); _ = webApplication.UseHttpsRedirection(); _ = webApplication.UseMiddleware(); _ = webApplication.MapControllers(); - log.Information("Starting Web Application"); + logger.LogInformation("Starting Web Application"); webApplication.Run(); return 0; } catch (Exception ex) { - log.Fatal(ex, "Host terminated unexpectedly"); - return 1; - } - finally - { - Log.CloseAndFlush(); + try + { logger?.LogCritical(ex, "Host terminated unexpectedly"); } + catch (Exception) { } + throw; } } diff --git a/Archive/Repositories/MetrologyRepo.cs b/Archive/Repositories/MetrologyRepo.cs index 3d35e21..1fafdce 100644 --- a/Archive/Repositories/MetrologyRepo.cs +++ b/Archive/Repositories/MetrologyRepo.cs @@ -13,6 +13,8 @@ using System.Transactions; namespace OI.Metrology.Archive.Repositories; +#nullable disable + public class MetrologyRepository : IMetrologyRepository { private readonly IDbConnectionFactory _DBConnectionFactory; diff --git a/Archive/Repositories/RdsMaxRepo.cs b/Archive/Repositories/RdsMaxRepo.cs index 3bcd7c8..8dd87a6 100644 --- a/Archive/Repositories/RdsMaxRepo.cs +++ b/Archive/Repositories/RdsMaxRepo.cs @@ -11,6 +11,8 @@ using System.Text; namespace OI.Metrology.Archive.Repositories; +#nullable disable + public class RdsMaxRepo : IRdsMaxRepo { diff --git a/Archive/Repositories/SQLDbConnectionFactory.cs b/Archive/Repositories/SQLDbConnectionFactory.cs index 66ed16c..4df8c2a 100644 --- a/Archive/Repositories/SQLDbConnectionFactory.cs +++ b/Archive/Repositories/SQLDbConnectionFactory.cs @@ -6,6 +6,8 @@ using System.Data.SqlClient; namespace OI.Metrology.Archive.Repositories; +#nullable disable + public class SQLDbConnectionFactory : IDbConnectionFactory { private readonly AppSettings _AppSettings; diff --git a/Archive/Services/AttachmentsService.cs b/Archive/Services/AttachmentsService.cs index 5a8862a..5c4efe4 100644 --- a/Archive/Services/AttachmentsService.cs +++ b/Archive/Services/AttachmentsService.cs @@ -4,6 +4,8 @@ using System.IO; namespace OI.Metrology.Archive.Services; +#nullable disable + using OI.Metrology.Archive.Models; using OI.Metrology.Shared.DataModels; using OI.Metrology.Shared.Models.Stateless; diff --git a/Archive/Services/InboundDataService.cs b/Archive/Services/InboundDataService.cs index 576fd3f..12d6113 100644 --- a/Archive/Services/InboundDataService.cs +++ b/Archive/Services/InboundDataService.cs @@ -8,6 +8,8 @@ using System.Linq; namespace OI.Metrology.Archive.Services; +#nullable disable + public class InboundDataService : IInboundDataService { private readonly IMetrologyRepository _MetrologyRepository; diff --git a/Tests/UnitTestArchive.cs b/Tests/UnitTestArchive.cs index 9acd65c..dc84e02 100644 --- a/Tests/UnitTestArchive.cs +++ b/Tests/UnitTestArchive.cs @@ -5,7 +5,6 @@ // using OI.Metrology.Shared.Models; // using OI.Metrology.Shared.Repositories; // using OI.Metrology.Tests.Models; -// using Serilog; // using System.Reflection; // using System.Text.Json; diff --git a/Tests/UnitTestInfinityQSController.cs b/Tests/UnitTestInfinityQSController.cs index 8f1ff4f..542ea9a 100644 --- a/Tests/UnitTestInfinityQSController.cs +++ b/Tests/UnitTestInfinityQSController.cs @@ -1,8 +1,8 @@ using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; using OI.Metrology.Shared.DataModels; using OI.Metrology.Shared.Models.Stateless; -using Serilog; namespace OI.Metrology.Tests; @@ -12,10 +12,10 @@ public class UnitTestInfinityQSController #pragma warning disable CS8618 - private static ILogger _Logger; + private static ILogger? _Logger; private static string _ControllerName; private static TestContext _TestContext; - private static WebApplicationFactory _WebApplicationFactory; + private static WebApplicationFactory? _WebApplicationFactory; #pragma warning restore @@ -23,8 +23,9 @@ public class UnitTestInfinityQSController public static void ClassInitAsync(TestContext testContext) { _TestContext = testContext; - _Logger = Log.ForContext(); _WebApplicationFactory = new WebApplicationFactory(); + IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider; + _Logger = serviceProvider.GetRequiredService>(); _ControllerName = nameof(Server.ApiControllers.InfinityQSController)[..^10]; } @@ -38,9 +39,9 @@ public class UnitTestInfinityQSController [TestMethod] public void TestControllerName() { - _Logger.Information("Starting Web Application"); + _Logger?.LogInformation("Starting Web Application"); Assert.AreEqual(IInfinityQSController.GetRouteName(), _ControllerName); - _Logger.Information($"{_TestContext?.TestName} completed"); + _Logger?.LogInformation("{TestName} completed", _TestContext?.TestName); NonThrowTryCatch(); } @@ -50,12 +51,12 @@ public class UnitTestInfinityQSController [TestMethod] public void GetCommandText() { - _Logger.Information("Starting Web Application"); - IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider; - IInfinityQSRepository infinityQSRepository = serviceProvider.GetRequiredService(); - string result = infinityQSRepository.GetCommandText("1677273357", "61", "CDE5", "5012", "575908", ""); + _Logger?.LogInformation("Starting Web Application"); + IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; + IInfinityQSRepository? infinityQSRepository = serviceProvider?.GetRequiredService(); + string? result = infinityQSRepository?.GetCommandText("1677273357", "61", "CDE5", "5012", "575908", ""); Assert.IsNotNull(result); - _Logger.Information($"{_TestContext?.TestName} completed"); + _Logger?.LogInformation("{TestName} completed", _TestContext?.TestName); NonThrowTryCatch(); } @@ -65,12 +66,13 @@ public class UnitTestInfinityQSController [TestMethod] public async Task GetCommandTextApi() { - HttpClient httpClient = _WebApplicationFactory.CreateClient(); - _Logger.Information("Starting Web Application"); + HttpClient? httpClient = _WebApplicationFactory?.CreateClient(); + _Logger?.LogInformation("Starting Web Application"); + Assert.IsTrue(httpClient is not null); string? json = await httpClient.GetStringAsync($"api/{_ControllerName}/commandText/?sub_group_id=1677273357&process=61&job=CDE5&part=5012&lot=575908&date_time=2023-02-24 15:15:00"); File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetCommandText)}.sql"), json); Assert.IsNotNull(json); - _Logger.Information($"{_TestContext?.TestName} completed"); + _Logger?.LogInformation("{TestName} completed", _TestContext?.TestName); NonThrowTryCatch(); } @@ -80,10 +82,10 @@ public class UnitTestInfinityQSController [TestMethod] public void GetData() { - _Logger.Information("Starting Web Application"); - IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider; - IInfinityQSRepository infinityQSRepository = serviceProvider.GetRequiredService(); - Result result = infinityQSRepository.GetData("1677273357"); + _Logger?.LogInformation("Starting Web Application"); + IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; + IInfinityQSRepository? infinityQSRepository = serviceProvider?.GetRequiredService(); + Result? result = infinityQSRepository?.GetData("1677273357"); Assert.IsNotNull(result?.Results); Assert.IsTrue(result?.Results.Length != 0); Assert.IsNotNull(result?.Results[0].PR_NAME); @@ -91,7 +93,7 @@ public class UnitTestInfinityQSController Assert.IsNotNull(result?.Results[0].SE_TSNO); Assert.IsNotNull(result?.Results[0].TD_NAME); Assert.IsNotNull(result?.Results[0].TD_TEST); - _Logger.Information($"{_TestContext?.TestName} completed"); + _Logger?.LogInformation("{TestName} completed", _TestContext?.TestName); NonThrowTryCatch(); } @@ -101,14 +103,15 @@ public class UnitTestInfinityQSController [TestMethod] public async Task GetDataApi() { - HttpClient httpClient = _WebApplicationFactory.CreateClient(); - _Logger.Information("Starting Web Application"); + HttpClient? httpClient = _WebApplicationFactory?.CreateClient(); + _Logger?.LogInformation("Starting Web Application"); + Assert.IsTrue(httpClient is not null); //string? json = await httpClient.GetStringAsync($"api/{_ControllerName}/1677273357 575908_2023-02-24 14-18-05.txt/data"); string? json = await httpClient.GetStringAsync($"api/{_ControllerName}/1677273357/data"); File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetData)}.json"), json); Result? result = System.Text.Json.JsonSerializer.Deserialize>(json); Assert.IsNotNull(result?.Results); - _Logger.Information($"{_TestContext?.TestName} completed"); + _Logger?.LogInformation("{TestName} completed", _TestContext?.TestName); NonThrowTryCatch(); } @@ -118,15 +121,15 @@ public class UnitTestInfinityQSController [TestMethod] public void GetDescriptors() { - _Logger.Information("Starting Web Application"); - IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider; - IInfinityQSRepository infinityQSRepository = serviceProvider.GetRequiredService(); - Result result = infinityQSRepository.GetDescriptors("1677273357"); + _Logger?.LogInformation("Starting Web Application"); + IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; + IInfinityQSRepository? infinityQSRepository = serviceProvider?.GetRequiredService(); + Result? result = infinityQSRepository?.GetDescriptors("1677273357"); Assert.IsNotNull(result?.Results); Assert.IsTrue(result?.Results.Length != 0); Assert.IsNotNull(result?.Results[0].SD_SGRP); Assert.IsNotNull(result?.Results[0].SD_TSNO); - _Logger.Information($"{_TestContext?.TestName} completed"); + _Logger?.LogInformation("{TestName} completed", _TestContext?.TestName); NonThrowTryCatch(); } @@ -136,14 +139,15 @@ public class UnitTestInfinityQSController [TestMethod] public async Task GetDescriptorsApi() { - HttpClient httpClient = _WebApplicationFactory.CreateClient(); - _Logger.Information("Starting Web Application"); + HttpClient? httpClient = _WebApplicationFactory?.CreateClient(); + _Logger?.LogInformation("Starting Web Application"); + Assert.IsTrue(httpClient is not null); //string? json = await httpClient.GetStringAsync($"api/{_ControllerName}/1677273357 575908_2023-02-24 14-18-05.txt/descriptors"); string? json = await httpClient.GetStringAsync($"api/{_ControllerName}/1677273357/descriptors"); File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetDescriptors)}.json"), json); Result? result = System.Text.Json.JsonSerializer.Deserialize>(json); Assert.IsNotNull(result?.Results); - _Logger.Information($"{_TestContext?.TestName} completed"); + _Logger?.LogInformation("{TestName} completed", _TestContext?.TestName); NonThrowTryCatch(); } @@ -153,12 +157,12 @@ public class UnitTestInfinityQSController [TestMethod] public void GetEvents() { - _Logger.Information("Starting Web Application"); - IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider; - IInfinityQSRepository infinityQSRepository = serviceProvider.GetRequiredService(); - Result result = infinityQSRepository.GetEvents("1677273357"); + _Logger?.LogInformation("Starting Web Application"); + IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; + IInfinityQSRepository? infinityQSRepository = serviceProvider?.GetRequiredService(); + Result? result = infinityQSRepository?.GetEvents("1677273357"); Assert.IsNotNull(result?.Results); - _Logger.Information($"{_TestContext?.TestName} completed"); + _Logger?.LogInformation("{TestName} completed", _TestContext?.TestName); NonThrowTryCatch(); } @@ -168,13 +172,14 @@ public class UnitTestInfinityQSController [TestMethod] public async Task GetEventsApi() { - HttpClient httpClient = _WebApplicationFactory.CreateClient(); - _Logger.Information("Starting Web Application"); + HttpClient? httpClient = _WebApplicationFactory?.CreateClient(); + _Logger?.LogInformation("Starting Web Application"); + Assert.IsTrue(httpClient is not null); string? json = await httpClient.GetStringAsync($"api/{_ControllerName}/1677273357/events"); File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetEvents)}.json"), json); Result? result = System.Text.Json.JsonSerializer.Deserialize>(json); Assert.IsNotNull(result?.Results); - _Logger.Information($"{_TestContext?.TestName} completed"); + _Logger?.LogInformation("{TestName} completed", _TestContext?.TestName); NonThrowTryCatch(); } @@ -184,12 +189,12 @@ public class UnitTestInfinityQSController [TestMethod] public void GetHeader() { - _Logger.Information("Starting Web Application"); - IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider; - IInfinityQSRepository infinityQSRepository = serviceProvider.GetRequiredService(); - Result result = infinityQSRepository.GetHeader("1677273357"); + _Logger?.LogInformation("Starting Web Application"); + IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; + IInfinityQSRepository? infinityQSRepository = serviceProvider?.GetRequiredService(); + Result? result = infinityQSRepository?.GetHeader("1677273357"); Assert.IsNotNull(result?.Results); - _Logger.Information($"{_TestContext?.TestName} completed"); + _Logger?.LogInformation("{TestName} completed", _TestContext?.TestName); NonThrowTryCatch(); } @@ -199,13 +204,14 @@ public class UnitTestInfinityQSController [TestMethod] public async Task GetHeaderApi() { - HttpClient httpClient = _WebApplicationFactory.CreateClient(); - _Logger.Information("Starting Web Application"); + HttpClient? httpClient = _WebApplicationFactory?.CreateClient(); + _Logger?.LogInformation("Starting Web Application"); + Assert.IsTrue(httpClient is not null); string? json = await httpClient.GetStringAsync($"api/{_ControllerName}/1677273357/header"); File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetHeader)}.json"), json); Result? result = System.Text.Json.JsonSerializer.Deserialize>(json); Assert.IsNotNull(result?.Results); - _Logger.Information($"{_TestContext?.TestName} completed"); + _Logger?.LogInformation("{TestName} completed", _TestContext?.TestName); NonThrowTryCatch(); }