oi-metrology/Tests/UnitInboundController.cs
Mike Phares 6f52566fc2 Dynamic 5 and 14 point copy
AppSettings alignment with other projects

_TestContextTestName over _TestContext
2025-05-02 15:22:18 -07:00

79 lines
2.8 KiB
C#

using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using OI.Metrology.Shared.DataModels;
using OI.Metrology.Shared.Models.Stateless;
using System.Text;
namespace OI.Metrology.Tests;
[TestClass]
public class UnitInboundController
{
#pragma warning disable CS8618
private static ILogger? _Logger;
private static string _ControllerName;
private static string? _TestContextTestName;
private static WebApplicationFactory<Server.Program>? _WebApplicationFactory;
#pragma warning restore
[ClassInitialize]
public static void ClassInitAsync(TestContext testContext)
{
_TestContextTestName = testContext.TestName;
_WebApplicationFactory = new WebApplicationFactory<Server.Program>();
IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider;
_Logger = serviceProvider.GetRequiredService<ILogger<Server.Program>>();
_ControllerName = nameof(Server.ApiControllers.InboundController)[..^10];
}
private static void NonThrowTryCatch()
{
try
{ throw new Exception(); }
catch (Exception) { }
}
[TestMethod]
public void TestControllerName()
{
_Logger?.LogInformation("Starting Web Application");
Assert.AreEqual(IInboundController<string>.GetRouteName(), _ControllerName);
_Logger?.LogInformation("{TestName} completed", _TestContextTestName);
NonThrowTryCatch();
}
private static InboundCommon GetInboundCommon() =>
new() { PSN = "5008", Reactor = "61", RDS = "579487", Id = 1678209360, ProcessDataStandardFormat = @"\\server\share\file.ext" };
private static StringContent GetStringContent() =>
new(System.Text.Json.JsonSerializer.Serialize(GetInboundCommon()), Encoding.UTF8, "application/json");
[Ignore]
[TestMethod]
public async Task DataApi()
{
HttpClient? httpClient = _WebApplicationFactory?.CreateClient();
_Logger?.LogInformation("Starting Web Application");
Assert.IsTrue(httpClient is not null);
_ = await httpClient.PostAsync($"api/{_ControllerName}/BioRad", GetStringContent());
_Logger?.LogInformation("{TestName} completed", _TestContextTestName);
NonThrowTryCatch();
}
[Ignore]
[TestMethod]
public async Task AttachFileApi()
{
HttpClient? httpClient = _WebApplicationFactory?.CreateClient();
_Logger?.LogInformation("Starting Web Application");
Assert.IsTrue(httpClient is not null);
_ = await httpClient.PostAsync($"api/{_ControllerName}/BioRad/attachment", GetStringContent());
_Logger?.LogInformation("{TestName} completed", _TestContextTestName);
NonThrowTryCatch();
}
}