using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.Extensions.DependencyInjection; using OI.Metrology.Shared.DataModels; using OI.Metrology.Shared.Models.Stateless; using Serilog; namespace OI.Metrology.Tests; [TestClass] public class UnitTestPinController { #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; _Logger = Log.ForContext(); _WebApplicationFactory = new WebApplicationFactory(); _ControllerName = nameof(Server.ApiControllers.PinController)[..^10]; } [TestMethod] public void TestControllerName() { _Logger.Information("Starting Web Application"); Assert.AreEqual(IPinController.GetRouteName(), _ControllerName); _Logger.Information($"{_TestContext?.TestName} completed"); } [TestMethod] public void GetPinnedTable() { _Logger.Information("Starting Web Application"); IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider; IMetrologyRepository metrologyRepository = serviceProvider.GetRequiredService(); IPinRepository pinRepository = serviceProvider.GetRequiredService(); Result result = pinRepository.GetPinnedTable(metrologyRepository, id: 1, cde_id: null, biorad_id: null, rds: null); Assert.IsNotNull(result?.Results); _Logger.Information($"{_TestContext?.TestName} completed"); } [TestMethod] public async Task GetPinnedTableApi() { HttpClient httpClient = _WebApplicationFactory.CreateClient(); _Logger.Information("Starting Web Application"); string? json = await httpClient.GetStringAsync($"api/{_ControllerName}/-1/pinned"); File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetPinnedTable)}.json"), json); Result? result = System.Text.Json.JsonSerializer.Deserialize>(json); Assert.IsNotNull(result?.Results); _Logger.Information($"{_TestContext?.TestName} completed"); } }