expose-myit/Tests/UnitTestSsaOrderController.cs
2022-12-10 12:06:16 -07:00

96 lines
4.0 KiB
C#

using Expose.MyIT.Shared.Models.Methods;
using Expose.MyIT.Shared.Models.Stateless.Methods;
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 : ISsaOrder
{
private static ILogger? _Logger;
private static string? _ControllerName;
private static TestContext? _TestContext;
private static WebApplicationFactory<Server.Program>? _WebApplicationFactory;
[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()
{
if (_Logger is null)
throw new NullReferenceException(nameof(_Logger));
if (_ControllerName is null)
throw new NullReferenceException(nameof(_ControllerName));
if (_WebApplicationFactory is null)
throw new NullReferenceException(nameof(_WebApplicationFactory));
_Logger.Information("Starting Web Application");
Assert.AreEqual(ISsaOrderController.GetRouteName(), _ControllerName);
_Logger.Information($"{_TestContext?.TestName} completed");
}
[TestMethod]
public void TestStatic_GetSsaOrders()
{
if (_Logger is null)
throw new NullReferenceException(nameof(_Logger));
ISsaOrder ssaOrder = this;
SsaOrder[] ssaOrders = ssaOrder.TestStatic_GetSsaOrders(null);
Assert.IsNotNull(ssaOrders);
_Logger.Information($"{_TestContext?.TestName} completed");
}
[TestMethod]
public async Task GetAllSsaOrders_ShouldReturn()
{
if (_Logger is null)
throw new NullReferenceException(nameof(_Logger));
if (_ControllerName is null)
throw new NullReferenceException(nameof(_ControllerName));
if (_WebApplicationFactory is null)
throw new NullReferenceException(nameof(_WebApplicationFactory));
_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 GetAllSsaOrders_API_ShouldReturn()
{
if (_Logger is null)
throw new NullReferenceException(nameof(_Logger));
if (_ControllerName is null)
throw new NullReferenceException(nameof(_ControllerName));
if (_WebApplicationFactory is null)
throw new NullReferenceException(nameof(_WebApplicationFactory));
HttpClient httpClient = _WebApplicationFactory.CreateClient();
_Logger.Information("Starting Web Application");
string actionName = nameof(Server.Controllers.SsaOrderController.GetAllSsaOrders)[3..];
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();
Assert.IsNotNull(result);
httpClient.Dispose();
_Logger.Information($"{_TestContext?.TestName} completed");
}
}