oi-metrology/Tests/UnitTestReactorController.cs

85 lines
3.1 KiB
C#

using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.DependencyInjection;
using OI.Metrology.Shared.DataModels;
using OI.Metrology.Shared.Models.Stateless;
using Serilog;
namespace OI.Metrology.Tests;
[TestClass]
public class UnitTestReactorController
{
#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<UnitTestReactorController>();
_WebApplicationFactory = new WebApplicationFactory<Server.Program>();
_ControllerName = nameof(Server.ApiControllers.ReactorsController)[..^10];
}
private static void NonThrowTryCatch()
{
try
{ throw new Exception(); }
catch (Exception) { }
}
[TestMethod]
public void TestControllerName()
{
_Logger.Information("Starting Web Application");
Assert.AreEqual(IReactorsController<string>.GetRouteName(), _ControllerName);
_Logger.Information($"{_TestContext?.TestName} completed");
NonThrowTryCatch();
}
[TestMethod]
public void GetReactors()
{
_Logger.Information("Starting Web Application");
IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider;
IReactorsRepository reactorsRepository = serviceProvider.GetRequiredService<IReactorsRepository>();
Result<int[]> result = reactorsRepository.EvenReactors();
Assert.IsNotNull(result?.Results);
_Logger.Information($"{_TestContext?.TestName} completed");
NonThrowTryCatch();
}
[TestMethod]
public async Task GetReactorsApi()
{
HttpClient httpClient = _WebApplicationFactory.CreateClient();
_Logger.Information("Starting Web Application");
string? json = await httpClient.GetStringAsync($"api/{_ControllerName}/true/");
File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetReactors)}.json"), json);
Result<int[]>? result = System.Text.Json.JsonSerializer.Deserialize<Result<int[]>>(json);
Assert.IsNotNull(result?.Results);
_Logger.Information($"{_TestContext?.TestName} completed");
NonThrowTryCatch();
}
[TestMethod]
public void GetKey()
{
_Logger.Information("Starting Web Application");
IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider;
IReactorsRepository reactorsRepository = serviceProvider.GetRequiredService<IReactorsRepository>();
WorkMaterialOut workMaterialOut = new() { RunDataSheet = "123456", Username = "phares" };
string? result = reactorsRepository.GetKey(workMaterialOut, save: false);
Assert.IsNotNull(result);
_Logger.Information($"{_TestContext?.TestName} completed");
NonThrowTryCatch();
}
}