using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using OI.Metrology.Shared.DataModels; using OI.Metrology.Shared.Models.Stateless; namespace OI.Metrology.Tests; [TestClass] public class UnitTestInfinityQSController { #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.InfinityQSController)[..^10]; } private static void NonThrowTryCatch() { try { throw new Exception(); } catch (Exception) { } } [TestMethod] public void TestControllerName() { _Logger?.LogInformation("Starting Web Application"); Assert.AreEqual(IInfinityQSController.GetRouteName(), _ControllerName); _Logger?.LogInformation("{TestName} completed", _TestContext?.TestName); NonThrowTryCatch(); } #if DEBUG [Ignore] #endif [TestMethod] public void GetCommandText() { _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?.LogInformation("{TestName} completed", _TestContext?.TestName); NonThrowTryCatch(); } #if DEBUG [Ignore] #endif [TestMethod] public async Task GetCommandTextApi() { 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?.LogInformation("{TestName} completed", _TestContext?.TestName); NonThrowTryCatch(); } #if DEBUG [Ignore] #endif [TestMethod] public void GetData() { _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); Assert.IsNotNull(result?.Results[0].SE_SGTM); Assert.IsNotNull(result?.Results[0].SE_TSNO); Assert.IsNotNull(result?.Results[0].TD_NAME); Assert.IsNotNull(result?.Results[0].TD_TEST); _Logger?.LogInformation("{TestName} completed", _TestContext?.TestName); NonThrowTryCatch(); } #if DEBUG [Ignore] #endif [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}/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?.LogInformation("{TestName} completed", _TestContext?.TestName); NonThrowTryCatch(); } #if DEBUG [Ignore] #endif [TestMethod] public void GetDescriptors() { _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?.LogInformation("{TestName} completed", _TestContext?.TestName); NonThrowTryCatch(); } #if DEBUG [Ignore] #endif [TestMethod] public async Task GetDescriptorsApi() { 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?.LogInformation("{TestName} completed", _TestContext?.TestName); NonThrowTryCatch(); } #if DEBUG [Ignore] #endif [TestMethod] public void GetEvents() { _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?.LogInformation("{TestName} completed", _TestContext?.TestName); NonThrowTryCatch(); } #if DEBUG [Ignore] #endif [TestMethod] public async Task GetEventsApi() { 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?.LogInformation("{TestName} completed", _TestContext?.TestName); NonThrowTryCatch(); } #if DEBUG [Ignore] #endif [TestMethod] public void GetHeader() { _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?.LogInformation("{TestName} completed", _TestContext?.TestName); NonThrowTryCatch(); } #if DEBUG [Ignore] #endif [TestMethod] public async Task GetHeaderApi() { 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?.LogInformation("{TestName} completed", _TestContext?.TestName); NonThrowTryCatch(); } }