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 OI.Metrology.Shared.Services; using System.Data; using System.Net.Http.Json; namespace OI.Metrology.Tests; [TestClass] public class UnitTestToolTypesController : BaseTestClass { #pragma warning disable CS8618 private static ILogger? _Logger; private static string _ControllerName; public TestContext TestContext { get; set; } private static WebApplicationFactory? _WebApplicationFactory; #pragma warning restore [ClassInitialize] public static void ClassInitAsync(TestContext testContext) { _WebApplicationFactory = new WebApplicationFactory(); IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider; _Logger = serviceProvider.GetRequiredService>(); _ControllerName = nameof(Server.ApiControllers.ToolTypesController)[..^10]; } private static void NonThrowTryCatch() { try { throw new Exception(); } catch (Exception) { } } [TestCleanup] public void TestCleanup() { if (TestContext.CurrentTestOutcome == UnitTestOutcome.Failed) IncrementFailedTests(); } [TestMethod] public void TestControllerName() { _Logger?.LogInformation("Starting Web Application"); Assert.AreEqual(IToolTypesController.GetRouteName(), _ControllerName); _Logger?.LogInformation("{TestName} completed", TestContext.TestName); } [TestMethod] public void Index() { _Logger?.LogInformation("Starting Web Application"); IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; IMetrologyRepository? metrologyRepository = serviceProvider?.GetRequiredService(); IToolTypesRepository? toolTypesRepository = serviceProvider?.GetRequiredService(); Assert.IsNotNull(metrologyRepository); Result? result = toolTypesRepository?.Index(metrologyRepository); Assert.IsNotNull(result?.Results); Assert.AreNotEqual(0, result.Results.Length); Assert.IsFalse(result.Results.All(l => l.ID == 0)); _Logger?.LogInformation("{TestName} completed", TestContext.TestName); } [TestMethod] public async Task IndexApi() { HttpClient? httpClient = _WebApplicationFactory?.CreateClient(); _Logger?.LogInformation("Starting Web Application"); Assert.IsNotNull(httpClient); string? json = await httpClient.GetStringAsync($"api/{_ControllerName}"); File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(Index)}.json"), json); Result? result = System.Text.Json.JsonSerializer.Deserialize>(json); Assert.IsNotNull(result?.Results); Assert.AreNotEqual(0, result.Results.Length); Assert.IsFalse(result.Results.All(l => l.ID == 0)); _Logger?.LogInformation("{TestName} completed", TestContext.TestName); } [TestMethod] public void GetToolTypeMetadata() { _Logger?.LogInformation("Starting Web Application"); IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; IMetrologyRepository? metrologyRepository = serviceProvider?.GetRequiredService(); IToolTypesRepository? toolTypesRepository = serviceProvider?.GetRequiredService(); Assert.IsNotNull(metrologyRepository); Result? result = toolTypesRepository?.GetToolTypeMetadata(metrologyRepository, id: 1, sortby: string.Empty); Assert.IsNotNull(result?.Results); Assert.IsNotNull(result.Results.Metadata); Assert.AreNotEqual(0, result.Results.Metadata.Length); Assert.IsFalse(result.Results.Metadata.All(l => l.ToolTypeID == 0)); _Logger?.LogInformation("{TestName} completed", TestContext.TestName); } [TestMethod] public async Task GetToolTypeMetadataApi() { HttpClient? httpClient = _WebApplicationFactory?.CreateClient(); _Logger?.LogInformation("Starting Web Application"); Assert.IsNotNull(httpClient); string? json = await httpClient.GetStringAsync($"api/{_ControllerName}/1"); File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetToolTypeMetadata)}.json"), json); Result? result = System.Text.Json.JsonSerializer.Deserialize>(json); Assert.IsNotNull(result?.Results); Assert.IsNotNull(result.Results.Metadata); Assert.AreNotEqual(0, result.Results.Metadata.Length); Assert.IsFalse(result.Results.Metadata.All(l => l.ToolTypeID == 0)); _Logger?.LogInformation("{TestName} completed", TestContext.TestName); } [TestMethod] public void GetHeaders() { _Logger?.LogInformation("Starting Web Application"); IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; IMetrologyRepository? metrologyRepository = serviceProvider?.GetRequiredService(); IToolTypesRepository? toolTypesRepository = serviceProvider?.GetRequiredService(); Assert.IsNotNull(metrologyRepository); Result? result = toolTypesRepository?.GetHeaders(metrologyRepository, id: 1, datebegin: null, dateend: null, page: null, pagesize: null, headerid: null); Assert.IsNotNull(result?.Results); Assert.IsNotNull(result.Results.Rows.Count > 0); _Logger?.LogInformation("{TestName} completed", TestContext.TestName); } [TestMethod] public async Task GetHeadersApi() { HttpClient? httpClient = _WebApplicationFactory?.CreateClient(); _Logger?.LogInformation("Starting Web Application"); Assert.IsNotNull(httpClient); string? json = await httpClient.GetStringAsync($"api/{_ControllerName}/1/headers"); File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetHeaders)}.json"), json); Result? result = Newtonsoft.Json.JsonConvert.DeserializeObject>(json); Assert.IsNotNull(result?.Results); Assert.IsNotNull(result.Results.Rows.Count > 0); _Logger?.LogInformation("{TestName} completed", TestContext.TestName); } [TestMethod] public void GetHeaderTitles() { _Logger?.LogInformation("Starting Web Application"); IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; IMetrologyRepository? metrologyRepository = serviceProvider?.GetRequiredService(); IToolTypesRepository? toolTypesRepository = serviceProvider?.GetRequiredService(); Assert.IsNotNull(metrologyRepository); Result? result = toolTypesRepository?.GetHeaderTitles(metrologyRepository, id: -1, page: null, pagesize: null); Assert.IsNotNull(result); Assert.IsNotNull(result.Results); Assert.AreNotEqual(0, result.Results.Length); _Logger?.LogInformation("{TestName} completed", TestContext.TestName); } [TestMethod] public async Task GetHeaderTitlesApi() { HttpClient? httpClient = _WebApplicationFactory?.CreateClient(); _Logger?.LogInformation("Starting Web Application"); Assert.IsNotNull(httpClient); string? json = await httpClient.GetStringAsync($"api/{_ControllerName}/-1/headertitles"); File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetHeaderTitles)}.json"), json); Result? result = System.Text.Json.JsonSerializer.Deserialize>(json); Assert.IsNotNull(result?.Results); Assert.AreNotEqual(0, result.Results.Length); _Logger?.LogInformation("{TestName} completed", TestContext.TestName); } [TestMethod] public void GetHeaderFields() { _Logger?.LogInformation("Starting Web Application"); IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; IMetrologyRepository? metrologyRepository = serviceProvider?.GetRequiredService(); IToolTypesRepository? toolTypesRepository = serviceProvider?.GetRequiredService(); Assert.IsNotNull(metrologyRepository); Result? result = toolTypesRepository?.GetHeaderFields(metrologyRepository, id: 1, headerid: 1); Assert.IsNotNull(result?.Results); Assert.AreNotEqual(0, result.Results.Length); _Logger?.LogInformation("{TestName} completed", TestContext.TestName); } [TestMethod] public async Task GetHeaderFieldsApi() { HttpClient? httpClient = _WebApplicationFactory?.CreateClient(); _Logger?.LogInformation("Starting Web Application"); Assert.IsNotNull(httpClient); string? json = await httpClient.GetStringAsync($"api/{_ControllerName}/1/headers/1/fields"); File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetHeaderFields)}.json"), json); Result? result = System.Text.Json.JsonSerializer.Deserialize>(json); Assert.IsNotNull(result?.Results); Assert.AreNotEqual(0, result.Results.Length); _Logger?.LogInformation("{TestName} completed", TestContext.TestName); } [TestMethod] public void GetData() { _Logger?.LogInformation("Starting Web Application"); IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; IMetrologyRepository? metrologyRepository = serviceProvider?.GetRequiredService(); IToolTypesRepository? toolTypesRepository = serviceProvider?.GetRequiredService(); Assert.IsNotNull(metrologyRepository); Result? result = toolTypesRepository?.GetData(metrologyRepository, id: 1, headerid: 1); Assert.IsNotNull(result?.Results); Assert.IsNotNull(result.Results.Rows.Count > 0); _Logger?.LogInformation("{TestName} completed", TestContext.TestName); } [TestMethod] public async Task GetDataApi() { HttpClient? httpClient = _WebApplicationFactory?.CreateClient(); _Logger?.LogInformation("Starting Web Application"); Assert.IsNotNull(httpClient); string? json = await httpClient.GetStringAsync($"api/{_ControllerName}/1/headers/1/data"); File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetData)}.json"), json); Result? result = Newtonsoft.Json.JsonConvert.DeserializeObject>(json); Assert.IsNotNull(result?.Results); Assert.IsNotNull(result.Results.Rows.Count > 0); _Logger?.LogInformation("{TestName} completed", TestContext.TestName); } [Ignore] [TestMethod] public void GetAttachment() { _Logger?.LogInformation("Starting Web Application"); int toolTypeId = 1; string tableType = "data"; string filename = "data.txt"; string attachmentId = "ffdf5410-ca19-4097-bfa4-b398e236d07e"; IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; IAttachmentsService? attachmentsService = serviceProvider?.GetRequiredService(); IMetrologyRepository? metrologyRepository = serviceProvider?.GetRequiredService(); IToolTypesRepository? toolTypesRepository = serviceProvider?.GetRequiredService(); Assert.IsNotNull(attachmentsService); Assert.IsNotNull(metrologyRepository); Assert.IsNotNull(toolTypesRepository); (string? message, string? _, Stream? _) = toolTypesRepository.GetAttachment(metrologyRepository, attachmentsService, toolTypeId, tableType, attachmentId, filename); Assert.IsNull(message); _Logger?.LogInformation("{TestName} completed", TestContext.TestName); } [Ignore] [TestMethod] public async Task GetAttachmentApi() { HttpClient? httpClient = _WebApplicationFactory?.CreateClient(); _Logger?.LogInformation("Starting Web Application"); Assert.IsNotNull(httpClient); _ = await httpClient.GetFromJsonAsync($"api/{_ControllerName}/1/data/files/ffdf5410-ca19-4097-bfa4-b398e236d07e/data.txt"); _Logger?.LogInformation("{TestName} completed", TestContext.TestName); } [Ignore] [TestMethod] public void OIExport() { _Logger?.LogInformation("Starting Web Application"); IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; IAttachmentsService? attachmentsService = serviceProvider?.GetRequiredService(); IMetrologyRepository? metrologyRepository = serviceProvider?.GetRequiredService(); IToolTypesRepository? toolTypesRepository = serviceProvider?.GetRequiredService(); Server.Models.AppSettings? appSettings = serviceProvider?.GetRequiredService(); Assert.IsNotNull(appSettings); Assert.IsNotNull(attachmentsService); Assert.IsNotNull(metrologyRepository); string? message = toolTypesRepository?.OIExport(metrologyRepository, attachmentsService, toolTypeId: 1, headerid: 1); Assert.IsNull(message); _Logger?.LogInformation("{TestName} completed", TestContext.TestName); } [Ignore] [TestMethod] public async Task OIExportApi() { HttpClient? httpClient = _WebApplicationFactory?.CreateClient(); _Logger?.LogInformation("Starting Web Application"); Assert.IsNotNull(httpClient); _ = await httpClient.GetFromJsonAsync($"api/{_ControllerName}/1/headers/1/export"); _Logger?.LogInformation("{TestName} completed", TestContext.TestName); } }