using Expose.MyIT.Shared.Models.Stateless; using Expose.MyIT.Shared.ViewModels; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.Extensions.DependencyInjection; using Serilog; using System.Net; namespace Expose.MyIT.Tests; [TestClass] public class UnitTestSsaOrderController { #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.SsaOrderController)[..^10]; } [TestMethod] public void TestControllerName() { _Logger.Information("Starting Web Application"); Assert.AreEqual(ISsaOrderController.GetRouteName(), _ControllerName); _Logger.Information($"{_TestContext?.TestName} completed"); } [TestMethod] public void GetSsaOrders() { if (_Logger is null) throw new NullReferenceException(nameof(_Logger)); SsaOrder[] ssaOrders = ISsaOrder.GetSsaOrders(null); Assert.IsNotNull(ssaOrders); _Logger.Information($"{_TestContext?.TestName} completed"); } [TestMethod] public async Task GetAllSsaOrders() { _Logger.Information("Starting Web Application"); SsaOrder[] ssaOrders; IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider; ISsaOrderRepository ssaOrderRepository = serviceProvider.GetRequiredService(); ssaOrders = await ssaOrderRepository.GetAllSsaOrders(); Assert.IsTrue(ssaOrders is not null); ssaOrders = await ssaOrderRepository.GetSsaOrders("f0998fc8-c724-ed11-a98b-0050568f497f"); Assert.IsTrue(ssaOrders is not null && ssaOrders.Any()); Assert.IsNotNull(ssaOrders[0].ToString()); _Logger.Information($"{_TestContext?.TestName} completed"); } [TestMethod] public async Task GetAllSsaOrdersApi() { HttpClient httpClient = _WebApplicationFactory.CreateClient(); _Logger.Information("Starting Web Application"); string actionName = nameof(ISsaOrderController.Action.All); 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"); } }