using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using OI.Metrology.Shared.Models.Stateless; namespace OI.Metrology.Tests; [TestClass] public class UnitTestOpenInsightV1Controller { #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.OpenInsightV1Controller)[..^10]; } private static void NonThrowTryCatch() { try { throw new Exception(); } catch (Exception) { } } [TestMethod] public void TestControllerName() { _Logger?.LogInformation("Starting Web Application"); Assert.AreEqual(IOpenInsightV1Controller.GetRouteName(), _ControllerName); _Logger?.LogInformation("{TestName} completed", _TestContext?.TestName); } [TestMethod] public void GetTencorRun() { _Logger?.LogInformation("Starting Web Application"); IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; IOpenInsightV1Repository? openInsightRepository = serviceProvider?.GetRequiredService(); string? result = openInsightRepository?.GetTencorRun("615071", "10/30/2023 06:48:34PM", "6IN_EPP ROTR"); Assert.IsNotNull(result); _Logger?.LogInformation("{TestName} completed", _TestContext?.TestName); NonThrowTryCatch(); } [TestMethod] public async Task GetTencorRunApi() { HttpClient? httpClient = _WebApplicationFactory?.CreateClient(); _Logger?.LogInformation("Starting Web Application"); Assert.IsTrue(httpClient is not null); string? result = await httpClient.GetStringAsync($"api/{_ControllerName}/615071/tencor-run?insert_date=10/30/2023 06:48:34PM&recipe=6IN_EPP ROTR"); File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetTencorRun)}.json"), result); Assert.IsNotNull(result); _Logger?.LogInformation("{TestName} completed", _TestContext?.TestName); NonThrowTryCatch(); } }