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 System.Data; using System.Text; namespace OI.Metrology.Tests; [TestClass] public class UnitTestExportController { #pragma warning disable CS8618 private static ILogger? _Logger; private static string _ControllerName; private static string? _TestContextTestName; private static WebApplicationFactory? _WebApplicationFactory; #pragma warning restore [ClassInitialize] public static void ClassInitAsync(TestContext testContext) { _TestContextTestName = testContext.TestName; _WebApplicationFactory = new WebApplicationFactory(); IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider; _Logger = serviceProvider.GetRequiredService>(); _ControllerName = nameof(Server.ApiControllers.ExportController)[..^10]; } private static void NonThrowTryCatch() { try { throw new Exception(); } catch (Exception) { } } [TestMethod] public void TestControllerName() { _Logger?.LogInformation("Starting Web Application"); Assert.AreEqual(IExportController.GetRouteName(), _ControllerName); _Logger?.LogInformation("{TestName} completed", _TestContextTestName); NonThrowTryCatch(); } private static HeaderCommon GetHeaderCommon() => new() { PSN = "5131", Reactor = "23", RDS = "631836", ID = 1710783849 }; private static StringContent GetStringContent() => new(System.Text.Json.JsonSerializer.Serialize(GetHeaderCommon()), Encoding.UTF8, "application/json"); [TestMethod] public void GetExport() { _Logger?.LogInformation("Starting Web Application"); IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; IExportRepository? exportRepository = serviceProvider?.GetRequiredService(); string? result = exportRepository?.GetExport(GetHeaderCommon()); Assert.IsNotNull(result); _Logger?.LogInformation("{TestName} completed", _TestContextTestName); NonThrowTryCatch(); } [TestMethod] public async Task GetExportApi() { HttpClient? httpClient = _WebApplicationFactory?.CreateClient(); _Logger?.LogInformation("Starting Web Application"); Assert.IsTrue(httpClient is not null); string? result = await httpClient.GetStringAsync($"api/{_ControllerName}/export"); File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetExport)}.txt"), result); Assert.IsNotNull(result); _Logger?.LogInformation("{TestName} completed", _TestContextTestName); NonThrowTryCatch(); } [TestMethod] public async Task PostExportApi() { HttpClient? httpClient = _WebApplicationFactory?.CreateClient(); _Logger?.LogInformation("Starting Web Application"); Assert.IsTrue(httpClient is not null); HttpResponseMessage httpResponseMessage = await httpClient.PostAsync($"api/{_ControllerName}/export", GetStringContent()); Assert.IsTrue(httpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK); _Logger?.LogInformation("{TestName} completed", _TestContextTestName); NonThrowTryCatch(); } [TestMethod] public void GetHeaders() { _Logger?.LogInformation("Starting Web Application"); IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; IExportRepository? exportRepository = serviceProvider?.GetRequiredService(); Result? result = exportRepository?.GetHeaders(GetHeaderCommon()); Assert.IsNotNull(result?.Results); _Logger?.LogInformation("{TestName} completed", _TestContextTestName); NonThrowTryCatch(); } [TestMethod] public async Task GetHeadersApi() { HttpClient? httpClient = _WebApplicationFactory?.CreateClient(); _Logger?.LogInformation("Starting Web Application"); Assert.IsTrue(httpClient is not null); string? json = await httpClient.GetStringAsync($"api/{_ControllerName}/headers"); File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetHeaders)}.json"), json); Result? result = System.Text.Json.JsonSerializer.Deserialize>(json); Assert.IsNotNull(result?.Results); _Logger?.LogInformation("{TestName} completed", _TestContextTestName); NonThrowTryCatch(); } [TestMethod] public async Task PostHeadersApi() { HttpClient? httpClient = _WebApplicationFactory?.CreateClient(); _Logger?.LogInformation("Starting Web Application"); Assert.IsTrue(httpClient is not null); HttpResponseMessage httpResponseMessage = await httpClient.PostAsync($"api/{_ControllerName}/headers", GetStringContent()); Assert.IsTrue(httpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK); _Logger?.LogInformation("{TestName} completed", _TestContextTestName); NonThrowTryCatch(); } [TestMethod] public void GetLogistics() { _Logger?.LogInformation("Starting Web Application"); IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; IExportRepository? exportRepository = serviceProvider?.GetRequiredService(); Result? result = exportRepository?.GetLogistics(GetHeaderCommon()); Assert.IsNotNull(result?.Results); _Logger?.LogInformation("{TestName} completed", _TestContextTestName); NonThrowTryCatch(); } [TestMethod] public async Task GetLogisticsApi() { HttpClient? httpClient = _WebApplicationFactory?.CreateClient(); _Logger?.LogInformation("Starting Web Application"); Assert.IsTrue(httpClient is not null); string? json = await httpClient.GetStringAsync($"api/{_ControllerName}/logistics"); File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetLogistics)}.json"), json); Result? result = System.Text.Json.JsonSerializer.Deserialize>(json); Assert.IsNotNull(result?.Results); _Logger?.LogInformation("{TestName} completed", _TestContextTestName); NonThrowTryCatch(); } [TestMethod] public async Task PostLogisticsApi() { HttpClient? httpClient = _WebApplicationFactory?.CreateClient(); _Logger?.LogInformation("Starting Web Application"); Assert.IsTrue(httpClient is not null); HttpResponseMessage httpResponseMessage = await httpClient.PostAsync($"api/{_ControllerName}/logistics", GetStringContent()); Assert.IsTrue(httpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK); _Logger?.LogInformation("{TestName} completed", _TestContextTestName); NonThrowTryCatch(); } [TestMethod] public void GetProcessDataStandardFormat() { _Logger?.LogInformation("Starting Web Application"); IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; IExportRepository? exportRepository = serviceProvider?.GetRequiredService(); string? result = exportRepository?.GetProcessDataStandardFormat(GetHeaderCommon()); Assert.IsNotNull(result); _Logger?.LogInformation("{TestName} completed", _TestContextTestName); NonThrowTryCatch(); } [TestMethod] public async Task GetProcessDataStandardFormatApi() { HttpClient? httpClient = _WebApplicationFactory?.CreateClient(); _Logger?.LogInformation("Starting Web Application"); Assert.IsTrue(httpClient is not null); string? result = await httpClient.GetStringAsync($"api/{_ControllerName}/processDataStandardFormat"); File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetProcessDataStandardFormat)}.pdsf"), result); Assert.IsNotNull(result); _Logger?.LogInformation("{TestName} completed", _TestContextTestName); NonThrowTryCatch(); } [TestMethod] public async Task PostProcessDataStandardFormatApi() { HttpClient? httpClient = _WebApplicationFactory?.CreateClient(); _Logger?.LogInformation("Starting Web Application"); Assert.IsTrue(httpClient is not null); HttpResponseMessage httpResponseMessage = await httpClient.PostAsync($"api/{_ControllerName}/processDataStandardFormat", GetStringContent()); Assert.IsTrue(httpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK); _Logger?.LogInformation("{TestName} completed", _TestContextTestName); NonThrowTryCatch(); } [Ignore] [TestMethod] public void GetExportData() { _Logger?.LogInformation("Starting Web Application"); IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; IMetrologyRepository? metrologyRepository = serviceProvider?.GetRequiredService(); IExportRepository? exportRepository = serviceProvider?.GetRequiredService(); Assert.IsTrue(metrologyRepository is not null); Result? result = exportRepository?.GetExportData(metrologyRepository, toolTypeId: 1, datebegin: null, dateend: null); Assert.IsNotNull(result?.Results); Assert.IsNotNull(result.Results.Rows.Count > 0); _Logger?.LogInformation("{TestName} completed", _TestContextTestName); } [Ignore] [TestMethod] public async Task GetExportDataApi() { HttpClient? httpClient = _WebApplicationFactory?.CreateClient(); _Logger?.LogInformation("Starting Web Application"); Assert.IsTrue(httpClient is not null); string? json = await httpClient.GetStringAsync($"api/{_ControllerName}/1/export?datebegin=&dateend="); File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetExportData)}.json"), json); Result? result = Newtonsoft.Json.JsonConvert.DeserializeObject>(json); Assert.IsNotNull(result?.Results); Assert.IsNotNull(result.Results.Rows.Count > 0); _Logger?.LogInformation("{TestName} completed", _TestContextTestName); } [TestMethod] public void GetCSVExport() { _Logger?.LogInformation("Starting Web Application"); IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; IMetrologyRepository? metrologyRepository = serviceProvider?.GetRequiredService(); IExportRepository? exportRepository = serviceProvider?.GetRequiredService(); Assert.IsTrue(metrologyRepository is not null); string? result = exportRepository?.GetCSVExport(metrologyRepository, toolTypeId: 1, datebegin: null, dateend: null); Assert.IsNotNull(result); _Logger?.LogInformation("{TestName} completed", _TestContextTestName); } [Ignore] [TestMethod] public async Task GetCSVExportDat() { HttpClient? httpClient = _WebApplicationFactory?.CreateClient(); _Logger?.LogInformation("Starting Web Application"); Assert.IsTrue(httpClient is not null); string? result = await httpClient.GetStringAsync($"api/{_ControllerName}/1/csv?datebegin=&dateend="); File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetCSVExport)}.csv"), result); Assert.IsNotNull(result); _Logger?.LogInformation("{TestName} completed", _TestContextTestName); } }