expose-myit/Tests/UnitTestSsaOrderController.cs

77 lines
2.8 KiB
C#

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.Http.Json;
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<Server.Program> _WebApplicationFactory;
#pragma warning restore
[ClassInitialize]
public static void ClassInitAsync(TestContext testContext)
{
_TestContext = testContext;
_Logger = Log.ForContext<UnitTestSsaOrderController>();
_WebApplicationFactory = new WebApplicationFactory<Server.Program>();
_ControllerName = nameof(Server.Controllers.SsaOrderController)[..^10];
}
[TestMethod]
public void TestControllerName()
{
_Logger.Information("Starting Web Application");
Assert.AreEqual(ISsaOrderController<string>.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<ISsaOrderRepository>();
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<object>.Action.All);
SsaOrder[]? ssaOrders = await httpClient.GetFromJsonAsync<SsaOrder[]>($"api/{_ControllerName}/{actionName}");
Assert.IsNotNull(ssaOrders);
Assert.IsTrue(ssaOrders.Any());
_Logger.Information($"{_TestContext?.TestName} completed");
}
}