using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using OI.Metrology.Shared.Models.Stateless; namespace OI.Metrology.Tests; [TestClass] public class UnitTestISpreadingResistanceProfileController { #pragma warning disable CS8618 private static ILogger? _Logger; private static string _ControllerName; private static TestContext _TestContext; private static WebApplicationFactory? _WebApplicationFactory; #pragma warning restore [ClassInitialize] public static void ClassInitAsync(TestContext testContext) { _TestContext = testContext; _WebApplicationFactory = new WebApplicationFactory(); IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider; _Logger = serviceProvider.GetRequiredService>(); _ControllerName = nameof(Server.ApiControllers.SpreadingResistanceProfileController)[..^10]; } private static void NonThrowTryCatch() { try { throw new Exception(); } catch (Exception) { } } [TestMethod] public void TestControllerName() { _Logger?.LogInformation("Starting Web Application"); Assert.AreEqual(ISpreadingResistanceProfileController.GetRouteName(), _ControllerName); _Logger?.LogInformation("{TestName} completed", _TestContext?.TestName); NonThrowTryCatch(); } [TestMethod] public void GetSpreadingResistanceProfileServiceImageBytes() { _Logger?.LogInformation("Starting Web Application"); string directory = "D:/wwwRoot"; if (!Directory.Exists(directory)) _ = Directory.CreateDirectory(directory); string file = Path.Combine(directory, "data.json"); if (File.Exists(file)) { string json = File.ReadAllText(file); IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; ISpreadingResistanceProfileService? spreadingResistanceProfileService = serviceProvider?.GetRequiredService(); byte[]? bytes = spreadingResistanceProfileService?.GetImageBytes(json); Assert.IsTrue(bytes is not null); Assert.IsTrue(bytes.Length != 0); File.WriteAllBytes(Path.Combine(directory, "srp.png"), bytes); } _Logger?.LogInformation("{TestName} completed", _TestContext?.TestName); NonThrowTryCatch(); } }