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

96 lines
4.3 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 UnitTestServiceShopOrderController : IServiceShopOrder
{
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<UnitTestServiceShopOrderController>();
_WebApplicationFactory = new WebApplicationFactory<Server.Program>();
_ControllerName = nameof(Server.Controllers.ServiceShopOrderController)[..^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(IServiceShopOrderController.GetRouteName(), _ControllerName);
_Logger.Information($"{_TestContext?.TestName} completed");
}
[TestMethod]
public void TestStatic_GetServiceShopOrders()
{
if (_Logger is null)
throw new NullReferenceException(nameof(_Logger));
IServiceShopOrder serviceShopOrder = this;
ServiceShopOrder[] serviceShopOrders = serviceShopOrder.TestStatic_GetServiceShopOrders(null);
Assert.IsNotNull(serviceShopOrders);
_Logger.Information($"{_TestContext?.TestName} completed");
}
[TestMethod]
public async Task GetAllServiceShopOrders_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");
ServiceShopOrder[] serviceShopOrders;
IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider;
IServiceShopOrderRepository ServiceShopOrderRepository = serviceProvider.GetRequiredService<IServiceShopOrderRepository>();
serviceShopOrders = await ServiceShopOrderRepository.GetAllServiceShopOrders();
Assert.IsTrue(serviceShopOrders is not null);
serviceShopOrders = await ServiceShopOrderRepository.GetServiceShopOrders("23188d3d-9b75-ed11-ab8b-0050568f2fc3");
Assert.IsTrue(serviceShopOrders is not null && serviceShopOrders.Any());
Assert.IsNotNull(serviceShopOrders[0].ToString());
_Logger.Information($"{_TestContext?.TestName} completed");
}
[TestMethod]
public async Task GetAllServiceShopOrders_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.ServiceShopOrderController.GetAllServiceShopOrders)[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");
}
}