using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using OI.Metrology.Server.Models; using OI.Metrology.Shared.Models.Stateless; using System.Net; namespace OI.Metrology.Tests; [TestClass] public class UnitTestAppSettingsController : BaseTestClass { #pragma warning disable CS8618 private static ILogger? _Logger; private static string _ControllerName; public TestContext TestContext { get; set; } private static WebApplicationFactory? _WebApplicationFactory; #pragma warning restore [ClassInitialize] public static void ClassInitAsync(TestContext testContext) { _WebApplicationFactory = new WebApplicationFactory(); IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider; _Logger = serviceProvider.GetRequiredService>(); _ControllerName = nameof(Server.ApiControllers.AppSettingsController)[..^10]; } private static void NonThrowTryCatch() { try { throw new Exception(); } catch (Exception) { } } [TestCleanup] public void TestCleanup() { if (TestContext.CurrentTestOutcome == UnitTestOutcome.Failed) IncrementFailedTests(); } [TestMethod] public void TestControllerName() { _Logger?.LogInformation("Starting Web Application"); Assert.AreEqual(IAppSettingsController.GetRouteName(), _ControllerName); _Logger?.LogInformation("{TestName} completed", TestContext.TestName); NonThrowTryCatch(); } [TestMethod] public void TestConnectionString() { _Logger?.LogInformation("Starting Web Application"); IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; IAppSettingsRepository? appSettingsRepository = serviceProvider?.GetRequiredService>(); _ = Assert.ThrowsException(() => appSettingsRepository?.VerifyConnectionStrings()); _Logger?.LogInformation("{TestName} completed", TestContext.TestName); NonThrowTryCatch(); } [TestMethod] public void AppSettings() { _Logger?.LogInformation("Starting Web Application"); IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; AppSettings? appSettings = serviceProvider?.GetRequiredService(); Assert.IsNotNull(appSettings); _Logger?.LogInformation("{TestName} completed", TestContext.TestName); NonThrowTryCatch(); } [TestMethod] public void GetAppSettings() { _Logger?.LogInformation("Starting Web Application"); IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; IAppSettingsRepository? appSettingsRepository = serviceProvider?.GetRequiredService>(); AppSettings? appSettings = appSettingsRepository?.GetAppSettings(); Assert.IsNotNull(appSettings); _Logger?.LogInformation("{TestName} completed", TestContext.TestName); NonThrowTryCatch(); } [TestMethod] public async Task GetAppSettingsApi() { HttpClient? httpClient = _WebApplicationFactory?.CreateClient(); _Logger?.LogInformation("Starting Web Application"); Assert.IsNotNull(httpClient); string actionName = nameof(IAppSettingsController.Action.App); HttpResponseMessage? httpResponseMessage = await httpClient.GetAsync($"api/{_ControllerName}/{actionName}"); Assert.AreEqual(HttpStatusCode.OK, httpResponseMessage.StatusCode); Assert.IsNotNull(httpResponseMessage.Content.Headers.ContentType); 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(GetAppSettings)}.json"), json); Assert.IsNotNull(json); Assert.AreNotEqual("[]", json); _Logger?.LogInformation("{TestName} completed", TestContext.TestName); NonThrowTryCatch(); } [TestMethod] public void GetBuildNumberAndGitCommitSeven() { _Logger?.LogInformation("Starting Web Application"); IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; IAppSettingsRepository? appSettingsRepository = serviceProvider?.GetRequiredService>(); string? result = appSettingsRepository?.GetBuildNumberAndGitCommitSeven(); Assert.IsNotNull(result); _Logger?.LogInformation("{TestName} completed", TestContext.TestName); NonThrowTryCatch(); } [TestMethod] public async Task GetBuildNumberAndGitCommitSevenApi() { HttpClient? httpClient = _WebApplicationFactory?.CreateClient(); _Logger?.LogInformation("Starting Web Application"); Assert.IsNotNull(httpClient); string actionName = nameof(IAppSettingsController.Action.DevOps); HttpResponseMessage? httpResponseMessage = await httpClient.GetAsync($"api/{_ControllerName}/{actionName}"); Assert.AreEqual(HttpStatusCode.OK, httpResponseMessage.StatusCode); Assert.IsNotNull(httpResponseMessage.Content.Headers.ContentType); 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(GetBuildNumberAndGitCommitSeven)}.json"), json); Assert.IsNotNull(json); _Logger?.LogInformation("{TestName} completed", TestContext.TestName); NonThrowTryCatch(); } }