81 lines
3.5 KiB
C#
81 lines
3.5 KiB
C#
using Microsoft.AspNetCore.Mvc.Testing;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using OI.Metrology.Shared.Models.Stateless;
|
|
using OI.Metrology.Shared.ViewModels;
|
|
|
|
namespace OI.Metrology.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;
|
|
_WebApplicationFactory = new WebApplicationFactory<Server.Program>();
|
|
IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider;
|
|
_Logger = serviceProvider.GetRequiredService<ILogger<Server.Program>>();
|
|
_ControllerName = nameof(Server.ApiControllers.ServiceShopOrderController)[..^10];
|
|
}
|
|
|
|
[TestMethod]
|
|
public void TestControllerName()
|
|
{
|
|
_Logger?.LogInformation("Starting Web Application");
|
|
Assert.AreEqual(IServiceShopOrderController<string>.GetRouteName(), _ControllerName);
|
|
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void GetServiceShopOrders()
|
|
{
|
|
if (_Logger is null)
|
|
throw new NullReferenceException(nameof(_Logger));
|
|
ServiceShopOrder[] serviceShopOrders = IServiceShopOrder.GetServiceShopOrders(null);
|
|
Assert.IsNotNull(serviceShopOrders);
|
|
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task GetAllServiceShopOrders()
|
|
{
|
|
_Logger?.LogInformation("Starting Web Application");
|
|
ServiceShopOrder[]? serviceShopOrders;
|
|
IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider;
|
|
IServiceShopOrderRepository? serviceShopOrderRepository = serviceProvider?.GetRequiredService<IServiceShopOrderRepository>();
|
|
Assert.IsTrue(serviceShopOrderRepository is not null);
|
|
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.Length != 0);
|
|
Assert.IsNotNull(serviceShopOrders[0].ToString());
|
|
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task GetAllServiceShopOrdersApi()
|
|
{
|
|
HttpClient? httpClient = _WebApplicationFactory?.CreateClient();
|
|
_Logger?.LogInformation("Starting Web Application");
|
|
Assert.IsTrue(httpClient is not null);
|
|
string actionName = nameof(IServiceShopOrderController<object>.Action.All);
|
|
string? json = await httpClient.GetStringAsync($"api/{_ControllerName}/{actionName}");
|
|
File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetAllServiceShopOrders)}.json"), json);
|
|
ServiceShopOrder[]? serviceShopOrders = System.Text.Json.JsonSerializer.Deserialize<ServiceShopOrder[]>(json);
|
|
Assert.IsNotNull(serviceShopOrders);
|
|
Assert.IsTrue(serviceShopOrders.Length != 0);
|
|
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
|
|
}
|
|
|
|
} |