using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using OI.Metrology.Shared.Models.Stateless; using System.Net; namespace OI.Metrology.Tests; [TestClass] public class UnitTestClientSettingsController { #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.ClientSettingsController)[..^10]; } [TestMethod] public void TestControllerName() { _Logger?.LogInformation("Starting Web Application"); Assert.AreEqual(IClientSettingsController.GetRouteName(), _ControllerName); _Logger?.LogInformation("{TestName} completed", _TestContext?.TestName); } [TestMethod] public void GetClientSettings() { _Logger?.LogInformation("Starting Web Application"); IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; IClientSettingsRepository? clientSettingsRepository = serviceProvider?.GetRequiredService(); #if DEBUG List? clientSettings = clientSettingsRepository?.GetClientSettings(null); Assert.IsTrue(clientSettings is not null); #endif _Logger?.LogInformation("{TestName} completed", _TestContext?.TestName); } [TestMethod] public async Task GetClientSettingsApi() { HttpClient? httpClient = _WebApplicationFactory?.CreateClient(); _Logger?.LogInformation("Starting Web Application"); #if DEBUG Assert.IsTrue(httpClient is not null); string actionName = nameof(IClientSettingsController.Action.Client); HttpResponseMessage httpResponseMessage = await httpClient.GetAsync($"api/{_ControllerName}/{actionName}"); Assert.AreEqual(HttpStatusCode.OK, httpResponseMessage.StatusCode); Assert.AreEqual("application/json; charset=utf-8", httpResponseMessage.Content.Headers.ContentType?.ToString()); string json = await httpResponseMessage.Content.ReadAsStringAsync(); File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetClientSettings)}.json"), json); Assert.IsNotNull(json); Assert.IsTrue(json != "[]"); #endif _Logger?.LogInformation("{TestName} completed", _TestContext?.TestName); } [TestMethod] public void GetIpAddress() { _Logger?.LogInformation("Starting Web Application"); IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; IClientSettingsRepository? clientSettingsRepository = serviceProvider?.GetRequiredService(); string? ipAddress = clientSettingsRepository?.GetIpAddress(null); Assert.IsTrue(ipAddress is not null); _Logger?.LogInformation("{TestName} completed", _TestContext?.TestName); } [TestMethod] public async Task GetIpAddressApi() { HttpClient? httpClient = _WebApplicationFactory?.CreateClient(); _Logger?.LogInformation("Starting Web Application"); Assert.IsTrue(httpClient is not null); string actionName = nameof(IClientSettingsController.Action.IP); HttpResponseMessage httpResponseMessage = await httpClient.GetAsync($"api/{_ControllerName}/{actionName}"); Assert.AreEqual(HttpStatusCode.OK, httpResponseMessage.StatusCode); Assert.AreEqual("text/plain; charset=utf-8", httpResponseMessage.Content.Headers.ContentType?.ToString()); string json = await httpResponseMessage.Content.ReadAsStringAsync(); File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetIpAddress)}.json"), json); Assert.IsNotNull(json); _Logger?.LogInformation("{TestName} completed", _TestContext?.TestName); } }