using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.Extensions.DependencyInjection; using Serilog; using System.Net; namespace OI.Metrology.Tests; [TestClass] public class UnitTestReactorController { private static TestContext? _TestContext; private static WebApplicationFactory? _WebApplicationFactory; [ClassInitialize] public static void ClassInitAsync(TestContext testContext) { _TestContext = testContext; _WebApplicationFactory = new WebApplicationFactory(); } [TestMethod] public async Task GetReactors_ShouldReturnAllReactorsAsync() { HttpResponseMessage httpResponseMessage; if (_WebApplicationFactory is null) throw new NullReferenceException(nameof(_WebApplicationFactory)); HttpClient httpClient = _WebApplicationFactory.CreateClient(); ILogger log = Log.ForContext(); log.Information("Starting Web Application"); IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider; Archive.Models.AppSettings appSettings = serviceProvider.GetRequiredService(); httpResponseMessage = await httpClient.GetAsync($"api/{nameof(Archive.ApiContollers.ReactorsController)[..^10]}/true"); Assert.AreEqual(HttpStatusCode.OK, httpResponseMessage.StatusCode); Assert.AreEqual("application/json; charset=utf-8", httpResponseMessage.Content.Headers.ContentType?.ToString()); httpResponseMessage = await httpClient.GetAsync($"api/{nameof(Archive.ApiContollers.ReactorsController)[..^10]}/false"); Assert.AreEqual(HttpStatusCode.OK, httpResponseMessage.StatusCode); // string result = await httpResponseMessage.Content.ReadAsStringAsync(); // Assert.AreEqual("\"Sample Name 1\"", result); httpClient.Dispose(); log.Information($"{_TestContext?.TestName} completed"); } // [TestMethod] // public void GetAllProducts_ShouldReturnAllProducts() // { // var testProducts = GetTestProducts(); // var controller = new OI.Metrology.Archive.ApiContollers.ReactorsController(testProducts); // var result = controller.GetAllProducts() as List; // Assert.AreEqual(testProducts.Count, result.Count); // } // [TestMethod] // public async Task GetAllProductsAsync_ShouldReturnAllProducts() // { // var testProducts = GetTestProducts(); // var controller = new OI.Metrology.Archive.ApiContollers.ReactorsController(testProducts); // var result = await controller.GetAllProductsAsync() as List; // Assert.AreEqual(testProducts.Count, result.Count); // } // [TestMethod] // public void GetProduct_ShouldReturnCorrectProduct() // { // var testProducts = GetTestProducts(); // var controller = new OI.Metrology.Archive.ApiContollers.ReactorsController(testProducts); // var result = controller.GetProduct(4) as OkNegotiatedContentResult; // Assert.IsNotNull(result); // Assert.AreEqual(testProducts[3].Name, result.Content.Name); // } // [TestMethod] // public async Task GetProductAsync_ShouldReturnCorrectProduct() // { // var testProducts = GetTestProducts(); // var controller = new OI.Metrology.Archive.ApiContollers.ReactorsController(testProducts); // var result = await controller.GetProductAsync(4) as OkNegotiatedContentResult; // Assert.IsNotNull(result); // Assert.AreEqual(testProducts[3].Name, result.Content.Name); // } // [TestMethod] // public void GetProduct_ShouldNotFindProduct() // { // var controller = new OI.Metrology.Archive.ApiContollers.ReactorsController(GetTestProducts()); // var result = controller.GetProduct(999); // Assert.IsInstanceOfType(result, typeof(NotFoundResult)); // } // private List GetTestProducts() // { // var testProducts = new List(); // testProducts.Add(new Product { Id = 1, Name = "Demo1", Price = 1 }); // testProducts.Add(new Product { Id = 2, Name = "Demo2", Price = 3.75M }); // testProducts.Add(new Product { Id = 3, Name = "Demo3", Price = 16.99M }); // testProducts.Add(new Product { Id = 4, Name = "Demo4", Price = 11.00M }); // return testProducts; // } [ClassCleanup] public static void ClassCleanup() => _WebApplicationFactory?.Dispose(); }