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 { #pragma warning disable CS8618 private static ILogger? _Logger; private static string _ControllerName; private static TestContext _TestContext; private static WebApplicationFactory? _WebApplicationFactory; #pragma warning restore [ClassInitialize] public static void ClassInitAsync(TestContext testContext) { _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) { } } [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.IsTrue(metrologyRepository is not null); Result? result = toolTypesRepository?.Index(metrologyRepository); Assert.IsNotNull(result?.Results); Assert.IsTrue(result.Results.Length != 0); 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.IsTrue(httpClient is not null); 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.IsTrue(result.Results.Length != 0); 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.IsTrue(metrologyRepository is not null); Result? result = toolTypesRepository?.GetToolTypeMetadata(metrologyRepository, id: 1, sortby: string.Empty); Assert.IsNotNull(result?.Results); Assert.IsNotNull(result.Results.Metadata); Assert.IsTrue(result.Results.Metadata.Length != 0); 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.IsTrue(httpClient is not null); 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.IsTrue(result.Results.Metadata.Length != 0); 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.IsTrue(metrologyRepository is not null); 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.IsTrue(httpClient is not null); 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.IsTrue(metrologyRepository is not null); Result? result = toolTypesRepository?.GetHeaderTitles(metrologyRepository, id: -1, page: null, pagesize: null); Assert.IsNotNull(result?.Results); Assert.IsTrue(result?.Results.Length != 0); _Logger?.LogInformation("{TestName} completed", _TestContext?.TestName); } [TestMethod] public async Task GetHeaderTitlesApi() { HttpClient? httpClient = _WebApplicationFactory?.CreateClient(); _Logger?.LogInformation("Starting Web Application"); Assert.IsTrue(httpClient is not null); 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.IsTrue(result.Results.Length != 0); _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.IsTrue(metrologyRepository is not null); Result? result = toolTypesRepository?.GetHeaderFields(metrologyRepository, id: 1, headerid: 1); Assert.IsNotNull(result?.Results); Assert.IsTrue(result.Results.Length != 0); _Logger?.LogInformation("{TestName} completed", _TestContext?.TestName); } [TestMethod] public async Task GetHeaderFieldsApi() { HttpClient? httpClient = _WebApplicationFactory?.CreateClient(); _Logger?.LogInformation("Starting Web Application"); Assert.IsTrue(httpClient is not null); 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.IsTrue(result.Results.Length != 0); _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.IsTrue(metrologyRepository is not null); 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.IsTrue(httpClient is not null); 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 GetExportData() { _Logger?.LogInformation("Starting Web Application"); IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; IMetrologyRepository? metrologyRepository = serviceProvider?.GetRequiredService(); IToolTypesRepository? toolTypesRepository = serviceProvider?.GetRequiredService(); Assert.IsTrue(metrologyRepository is not null); Result? result = toolTypesRepository?.GetExportData(metrologyRepository, toolTypeId: 1, datebegin: null, dateend: null); Assert.IsNotNull(result?.Results); Assert.IsNotNull(result.Results.Rows.Count > 0); _Logger?.LogInformation("{TestName} completed", _TestContext?.TestName); } [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"); 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", _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.IsTrue(attachmentsService is not null); Assert.IsTrue(metrologyRepository is not null); Assert.IsTrue(toolTypesRepository is not null); (string? message, string? _, Stream? _) = toolTypesRepository.GetAttachment(metrologyRepository, attachmentsService, toolTypeId, tabletype, attachmentId, filename); Assert.IsTrue(message is null); _Logger?.LogInformation("{TestName} completed", _TestContext?.TestName); } [Ignore] [TestMethod] public async Task GetAttachmentApi() { HttpClient? httpClient = _WebApplicationFactory?.CreateClient(); _Logger?.LogInformation("Starting Web Application"); Assert.IsTrue(httpClient is not null); _ = 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.IsTrue(appSettings is not null); Assert.IsTrue(attachmentsService is not null); Assert.IsTrue(metrologyRepository is not null); string? message = toolTypesRepository?.OIExport(metrologyRepository, attachmentsService, appSettings.AttachmentPath, appSettings.TableToPath, toolTypeId: 1, headerid: 1); Assert.IsTrue(message is null); _Logger?.LogInformation("{TestName} completed", _TestContext?.TestName); } [Ignore] [TestMethod] public async Task OIExportApi() { HttpClient? httpClient = _WebApplicationFactory?.CreateClient(); _Logger?.LogInformation("Starting Web Application"); Assert.IsTrue(httpClient is not null); _ = await httpClient.GetFromJsonAsync($"api/{_ControllerName}/1/headers/1/export"); _Logger?.LogInformation("{TestName} completed", _TestContext?.TestName); } }