using Expose.MyIT.Shared.Models.Stateless; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.Extensions.DependencyInjection; using Serilog; using System.Net; namespace Expose.MyIT.Tests; [TestClass] public class UnitTestAppSettingsController { #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.Controllers.AppSettingsController)[..^10]; } [TestMethod] public void TestControllerName() { _Logger.Information("Starting Web Application"); Assert.AreEqual(IAppSettingsController.GetRouteName(), _ControllerName); _Logger.Information($"{_TestContext?.TestName} completed"); } [TestMethod] public void AppSettings() { _Logger.Information("Starting Web Application"); IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider; Server.Models.AppSettings appSettings = serviceProvider.GetRequiredService(); Assert.IsNotNull(appSettings); _Logger.Information($"{_TestContext?.TestName} completed"); } [TestMethod] public void GetAppSettings() { _Logger.Information("Starting Web Application"); IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider; IAppSettingsRepository appSettingsRepository = serviceProvider.GetRequiredService(); List collection = appSettingsRepository.GetAppSettings(); Assert.IsTrue(collection is not null); _Logger.Information($"{_TestContext?.TestName} completed"); } [TestMethod] public async Task GetAppSettingsApi() { HttpClient httpClient = _WebApplicationFactory.CreateClient(); _Logger.Information("Starting Web Application"); string actionName = nameof(IAppSettingsController.Action.App); 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 result = await httpResponseMessage.Content.ReadAsStringAsync(); httpClient.Dispose(); Assert.IsNotNull(result); Assert.IsTrue(result != "[]"); _Logger.Information($"{_TestContext?.TestName} completed"); } }