oi-metrology/Tests/UnitTestReactorController.cs
2023-01-06 08:40:35 -07:00

109 lines
4.5 KiB
C#

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<Archive.Program>? _WebApplicationFactory;
[ClassInitialize]
public static void ClassInitAsync(TestContext testContext)
{
_TestContext = testContext;
_WebApplicationFactory = new WebApplicationFactory<Archive.Program>();
}
[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<UnitTestReactorController>();
log.Information("Starting Web Application");
IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider;
Archive.Models.AppSettings appSettings = serviceProvider.GetRequiredService<Archive.Models.AppSettings>();
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<Product>;
// 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<Product>;
// 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<Product>;
// 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<Product>;
// 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<Product> GetTestProducts()
// {
// var testProducts = new List<Product>();
// 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();
}