expose-myit/Tests/UnitTestServiceShopOrderController.cs

81 lines
3.3 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;
namespace Expose.MyIT.Tests;
[TestClass]
public class UnitTestServiceShopOrderController
{
#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<UnitTestServiceShopOrderController>();
_WebApplicationFactory = new WebApplicationFactory<Server.Program>();
_ControllerName = nameof(Server.Controllers.ServiceShopOrderController)[..^10];
}
[TestMethod]
public void TestControllerName()
{
_Logger.Information("Starting Web Application");
Assert.AreEqual(IServiceShopOrderController<string>.GetRouteName(), _ControllerName);
_Logger.Information($"{_TestContext?.TestName} completed");
}
[TestMethod]
public void GetServiceShopOrders()
{
if (_Logger is null)
throw new NullReferenceException(nameof(_Logger));
ServiceShopOrder[] serviceShopOrders = IServiceShopOrder.GetServiceShopOrders(null);
Assert.IsNotNull(serviceShopOrders);
_Logger.Information($"{_TestContext?.TestName} completed");
}
[TestMethod]
public async Task GetAllServiceShopOrders()
{
_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 GetAllServiceShopOrdersApi()
{
HttpClient httpClient = _WebApplicationFactory.CreateClient();
_Logger.Information("Starting Web Application");
string actionName = nameof(IServiceShopOrderController<object>.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");
}
}