using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using OI.Metrology.Shared.DataModels; using OI.Metrology.Shared.Models; using OI.Metrology.Shared.Models.Stateless; namespace OI.Metrology.Tests; [TestClass] public class UnitTestInfinityQSV4Controller { #pragma warning disable CS8618 private static ILogger? _Logger; private static string _ControllerName; private static string? _TestContextTestName; private static string _ControllerVersion; 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.InfinityQSV4Controller)[..^12]; _ControllerVersion = nameof(Server.ApiControllers.InfinityQSV4Controller)[..^10][^2..]; } private static void NonThrowTryCatch() { try { throw new Exception(); } catch (Exception) { } } [TestMethod] public void TestControllerName() { _Logger?.LogInformation("Starting Web Application"); Assert.AreEqual(IInfinityQSV4Controller.GetRouteName(), _ControllerName); Assert.AreEqual(IInfinityQSV4Controller.GetRouteVersion(), _ControllerVersion); _Logger?.LogInformation("{TestName} completed", _TestContextTestName); NonThrowTryCatch(); } [TestMethod] public void GetCommandText() { _Logger?.LogInformation("Starting Web Application"); IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; IInfinityQSV4Repository? infinityQSV4Repository = serviceProvider?.GetRequiredService(); string? result = infinityQSV4Repository?.GetCommandText("1718539249", "61", "CDE5", "5012", "575908", ""); Assert.IsNotNull(result); _Logger?.LogInformation("{TestName} completed", _TestContextTestName); NonThrowTryCatch(); } [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/{_ControllerVersion}/{_ControllerName}/1718539249/commandText/?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", _TestContextTestName); NonThrowTryCatch(); } [TestMethod] public void GetData() { _Logger?.LogInformation("Starting Web Application"); IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; IInfinityQSV4Repository? infinityQSV4Repository = serviceProvider?.GetRequiredService(); Result? result = infinityQSV4Repository?.GetData("1718539249"); Assert.IsNotNull(result?.Results); Assert.IsTrue(result?.Results.Length != 0); Assert.IsNotNull(result?.Results[0].Process); Assert.IsNotNull(result?.Results[0].Variable); Assert.IsNotNull(result?.Results[0].SiteNumber); Assert.IsNotNull(result?.Results[0].VariableNumber); Assert.IsNotNull(result?.Results[0].SubGroupDateTime); _Logger?.LogInformation("{TestName} completed", _TestContextTestName); NonThrowTryCatch(); } [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/{_ControllerVersion}/{_ControllerName}/1718539249 575908_2023-02-24 14-18-05.txt/data"); string? json = await httpClient.GetStringAsync($"api/{_ControllerVersion}/{_ControllerName}/1718539249/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", _TestContextTestName); NonThrowTryCatch(); } [TestMethod] public void GetDescriptors() { _Logger?.LogInformation("Starting Web Application"); IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; IInfinityQSV4Repository? infinityQSV4Repository = serviceProvider?.GetRequiredService(); Result? result = infinityQSV4Repository?.GetDescriptors("1718539249"); Assert.IsNotNull(result?.Results); Assert.IsTrue(result?.Results.Length != 0); Assert.IsNotNull(result?.Results[0].SubGroupId); Assert.IsNotNull(result?.Results[0].SiteNumber); _Logger?.LogInformation("{TestName} completed", _TestContextTestName); NonThrowTryCatch(); } [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/{_ControllerVersion}/{_ControllerName}/1718539249 575908_2023-02-24 14-18-05.txt/descriptors"); string? json = await httpClient.GetStringAsync($"api/{_ControllerVersion}/{_ControllerName}/1718539249/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", _TestContextTestName); NonThrowTryCatch(); } [TestMethod] public void GetHeader() { _Logger?.LogInformation("Starting Web Application"); IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; IInfinityQSV4Repository? infinityQSV4Repository = serviceProvider?.GetRequiredService(); Result? result = infinityQSV4Repository?.GetHeader("1718539249"); Assert.IsNotNull(result?.Results); Assert.IsNotNull(result?.Results[0].Part); _Logger?.LogInformation("{TestName} completed", _TestContextTestName); NonThrowTryCatch(); } [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/{_ControllerVersion}/{_ControllerName}/1718539249/header"); File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetHeader)}.json"), json); Result? result = System.Text.Json.JsonSerializer.Deserialize>(json); Assert.IsNotNull(result?.Results); Assert.IsNotNull(result?.Results[0].Part); _Logger?.LogInformation("{TestName} completed", _TestContextTestName); NonThrowTryCatch(); } [TestMethod] public void GetProductDataAverageSumOfDefectsProcessMeanProcessSigma() { _Logger?.LogInformation("Starting Web Application"); IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; IInfinityQSV4Repository? infinityQSV4Repository = serviceProvider?.GetRequiredService(); string? result = infinityQSV4Repository?.GetProductDataAverageSumOfDefectsProcessMeanProcessSigma("41", "8IN_THIN ROTR"); Assert.IsNotNull(result); _Logger?.LogInformation("{TestName} completed", _TestContextTestName); NonThrowTryCatch(); } [TestMethod] public async Task GetProductDataAverageSumOfDefectsProcessMeanProcessSigmaApi() { HttpClient? httpClient = _WebApplicationFactory?.CreateClient(); _Logger?.LogInformation("Starting Web Application"); Assert.IsTrue(httpClient is not null); string? result = await httpClient.GetStringAsync($"api/{_ControllerVersion}/{_ControllerName}/41/product-data-average-sum-of-defects-process-mean-process-sigma?recipe=8IN_THIN ROTR"); File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetProductDataAverageSumOfDefectsProcessMeanProcessSigma)}.json"), result); Assert.IsNotNull(result); _Logger?.LogInformation("{TestName} completed", _TestContextTestName); NonThrowTryCatch(); } [TestMethod] public void GetEpiProTempVerificationRows() { _Logger?.LogInformation("Starting Web Application"); int[] night = new int[] { 44, 46, 52, 54 }; IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; IInfinityQSV4Repository? infinityQSV4Repository = serviceProvider?.GetRequiredService(); List? results = infinityQSV4Repository?.GetEpiProTempVerificationRows(night); Assert.IsNotNull(results); _Logger?.LogInformation("{TestName} completed", _TestContextTestName); NonThrowTryCatch(); } [TestMethod] public async Task GetEpiProTempVerificationRowsApi() { HttpClient? httpClient = _WebApplicationFactory?.CreateClient(); _Logger?.LogInformation("Starting Web Application"); Assert.IsTrue(httpClient is not null); string? result = await httpClient.GetStringAsync($"api/{_ControllerVersion}/{_ControllerName}/epi-pro-temp-verification-rows/?night=44&night=46&night=52&night=54"); File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetEpiProTempVerificationRows)}.json"), result); Assert.IsNotNull(result); _Logger?.LogInformation("{TestName} completed", _TestContextTestName); NonThrowTryCatch(); } [TestMethod] public void GetEpiProTempVerification() { _Logger?.LogInformation("Starting Web Application"); int[] night = new int[] { 44, 46, 52, 54 }; IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; IInfinityQSV4Repository? infinityQSV4Repository = serviceProvider?.GetRequiredService(); string? result = infinityQSV4Repository?.GetEpiProTempVerification(night); Assert.IsNotNull(result); _Logger?.LogInformation("{TestName} completed", _TestContextTestName); NonThrowTryCatch(); } [TestMethod] public async Task GetEpiProTempVerificationApi() { HttpClient? httpClient = _WebApplicationFactory?.CreateClient(); _Logger?.LogInformation("Starting Web Application"); Assert.IsTrue(httpClient is not null); string? result = await httpClient.GetStringAsync($"api/{_ControllerVersion}/{_ControllerName}/epi-pro-temp-verification/?night=44&night=46&night=52&night=54"); File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetEpiProTempVerification)}.html"), result); Assert.IsNotNull(result); _Logger?.LogInformation("{TestName} completed", _TestContextTestName); NonThrowTryCatch(); } [TestMethod] public void GetReactors() { _Logger?.LogInformation("Starting Web Application"); IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; IInfinityQSV4Repository? infinityQSV4Repository = serviceProvider?.GetRequiredService(); List? results = infinityQSV4Repository?.GetReactors(); Assert.IsNotNull(results); _Logger?.LogInformation("{TestName} completed", _TestContextTestName); NonThrowTryCatch(); } [TestMethod] public void GetProductionSpecification() { _Logger?.LogInformation("Starting Web Application"); string? result; IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; IInfinityQSV4Repository? infinityQSV4Repository = serviceProvider?.GetRequiredService(); foreach (string part in new string[] { "4992" }) { result = infinityQSV4Repository?.GetProductionSpecification(part); Assert.IsNotNull(result); } _Logger?.LogInformation("{TestName} completed", _TestContextTestName); NonThrowTryCatch(); } [TestMethod] public void GetProductionSpecifications() { _Logger?.LogInformation("Starting Web Application"); string? result; List rdsCollection = new(); IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; IInfinityQSV4Repository? infinityQSV4Repository = serviceProvider?.GetRequiredService(); List? reactors = infinityQSV4Repository?.GetReactors(); Assert.IsNotNull(reactors); foreach (Reactor reactor in reactors) { if (reactor.LoadedRDS is null) continue; rdsCollection.AddRange(reactor.LoadedRDS); } List production = new(); foreach (int rds in rdsCollection) { if (rds is 0) continue; production.Add(""); } foreach (string part in new string[] { "4992" }) { result = infinityQSV4Repository?.GetProductionSpecification(part); Assert.IsNotNull(result); } _Logger?.LogInformation("{TestName} completed", _TestContextTestName); NonThrowTryCatch(); } [TestMethod] public void GetLastGroupIdWithValue() { const string part = "4610"; const int test = 1228920625; const string process = "35"; _Logger?.LogInformation("Starting Web Application"); IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; IInfinityQSV4Repository? infinityQSV4Repository = serviceProvider?.GetRequiredService(); string? result = infinityQSV4Repository?.GetLastGroupIdWithValue(process, part, test); Assert.IsNotNull(result); _Logger?.LogInformation("{TestName} completed", _TestContextTestName); NonThrowTryCatch(); } [TestMethod] public async Task GetLastGroupIdWithValueApi() { const string part = "4610"; const int test = 1228920625; const string process = "35"; HttpClient? httpClient = _WebApplicationFactory?.CreateClient(); _Logger?.LogInformation("Starting Web Application"); Assert.IsTrue(httpClient is not null); string? result = await httpClient.GetStringAsync($"api/{_ControllerVersion}/{_ControllerName}/{process}/last-group-id-with-value/?part={part}&test={test}"); File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetLastGroupIdWithValue)}.html"), result); Assert.IsNotNull(result); _Logger?.LogInformation("{TestName} completed", _TestContextTestName); NonThrowTryCatch(); } }