Tests passed using Mock

This commit is contained in:
2023-11-01 08:27:25 -07:00
parent f684c4b7ef
commit 3dd4034a84
81 changed files with 171126 additions and 139391 deletions

View File

@ -1,8 +1,8 @@
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 Serilog;
using System.Text;
namespace OI.Metrology.Tests;
@ -13,10 +13,10 @@ public class UnitTestExportController
#pragma warning disable CS8618
private static ILogger _Logger;
private static ILogger? _Logger;
private static string _ControllerName;
private static TestContext _TestContext;
private static WebApplicationFactory<Server.Program> _WebApplicationFactory;
private static WebApplicationFactory<Server.Program>? _WebApplicationFactory;
#pragma warning restore
@ -24,8 +24,9 @@ public class UnitTestExportController
public static void ClassInitAsync(TestContext testContext)
{
_TestContext = testContext;
_Logger = Log.ForContext<UnitTestExportController>();
_WebApplicationFactory = new WebApplicationFactory<Server.Program>();
IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider;
_Logger = serviceProvider.GetRequiredService<ILogger<Server.Program>>();
_ControllerName = nameof(Server.ApiControllers.ExportController)[..^10];
}
@ -39,9 +40,9 @@ public class UnitTestExportController
[TestMethod]
public void TestControllerName()
{
_Logger.Information("Starting Web Application");
_Logger?.LogInformation("Starting Web Application");
Assert.AreEqual(IExportController<string>.GetRouteName(), _ControllerName);
_Logger.Information($"{_TestContext?.TestName} completed");
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
NonThrowTryCatch();
}
@ -54,142 +55,150 @@ public class UnitTestExportController
[TestMethod]
public void GetExport()
{
_Logger.Information("Starting Web Application");
IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider;
IExportRepository exportRepository = serviceProvider.GetRequiredService<IExportRepository>();
string result = exportRepository.GetExport(GetHeaderCommon());
_Logger?.LogInformation("Starting Web Application");
IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider;
IExportRepository? exportRepository = serviceProvider?.GetRequiredService<IExportRepository>();
string? result = exportRepository?.GetExport(GetHeaderCommon());
Assert.IsNotNull(result);
_Logger.Information($"{_TestContext?.TestName} completed");
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
NonThrowTryCatch();
}
[TestMethod]
public async Task GetExportApi()
{
HttpClient httpClient = _WebApplicationFactory.CreateClient();
_Logger.Information("Starting Web Application");
HttpClient? httpClient = _WebApplicationFactory?.CreateClient();
_Logger?.LogInformation("Starting Web Application");
Assert.IsTrue(httpClient is not null);
string? result = await httpClient.GetStringAsync($"api/{_ControllerName}/export");
File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetExport)}.txt"), result);
Assert.IsNotNull(result);
_Logger.Information($"{_TestContext?.TestName} completed");
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
NonThrowTryCatch();
}
[TestMethod]
public async Task PostExportApi()
{
HttpClient httpClient = _WebApplicationFactory.CreateClient();
_Logger.Information("Starting Web Application");
HttpClient? httpClient = _WebApplicationFactory?.CreateClient();
_Logger?.LogInformation("Starting Web Application");
Assert.IsTrue(httpClient is not null);
HttpResponseMessage httpResponseMessage = await httpClient.PostAsync($"api/{_ControllerName}/export", GetStringContent());
Assert.IsTrue(httpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK);
_Logger.Information($"{_TestContext?.TestName} completed");
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
NonThrowTryCatch();
}
[TestMethod]
public void GetHeaders()
{
_Logger.Information("Starting Web Application");
IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider;
IExportRepository exportRepository = serviceProvider.GetRequiredService<IExportRepository>();
Result<HeaderCommon[]> result = exportRepository.GetHeaders(GetHeaderCommon());
_Logger?.LogInformation("Starting Web Application");
IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider;
IExportRepository? exportRepository = serviceProvider?.GetRequiredService<IExportRepository>();
Result<HeaderCommon[]>? result = exportRepository?.GetHeaders(GetHeaderCommon());
Assert.IsNotNull(result?.Results);
_Logger.Information($"{_TestContext?.TestName} completed");
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
NonThrowTryCatch();
}
[TestMethod]
public async Task GetHeadersApi()
{
HttpClient httpClient = _WebApplicationFactory.CreateClient();
_Logger.Information("Starting Web Application");
HttpClient? httpClient = _WebApplicationFactory?.CreateClient();
_Logger?.LogInformation("Starting Web Application");
Assert.IsTrue(httpClient is not null);
string? json = await httpClient.GetStringAsync($"api/{_ControllerName}/headers");
File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetHeaders)}.json"), json);
Result<HeaderCommon[]>? result = System.Text.Json.JsonSerializer.Deserialize<Result<HeaderCommon[]>>(json);
Assert.IsNotNull(result?.Results);
_Logger.Information($"{_TestContext?.TestName} completed");
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
NonThrowTryCatch();
}
[TestMethod]
public async Task PostHeadersApi()
{
HttpClient httpClient = _WebApplicationFactory.CreateClient();
_Logger.Information("Starting Web Application");
HttpClient? httpClient = _WebApplicationFactory?.CreateClient();
_Logger?.LogInformation("Starting Web Application");
Assert.IsTrue(httpClient is not null);
HttpResponseMessage httpResponseMessage = await httpClient.PostAsync($"api/{_ControllerName}/headers", GetStringContent());
Assert.IsTrue(httpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK);
_Logger.Information($"{_TestContext?.TestName} completed");
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
NonThrowTryCatch();
}
[TestMethod]
public void GetLogistics()
{
_Logger.Information("Starting Web Application");
IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider;
IExportRepository exportRepository = serviceProvider.GetRequiredService<IExportRepository>();
Result<HeaderCommon[]> result = exportRepository.GetLogistics(GetHeaderCommon());
_Logger?.LogInformation("Starting Web Application");
IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider;
IExportRepository? exportRepository = serviceProvider?.GetRequiredService<IExportRepository>();
Result<HeaderCommon[]>? result = exportRepository?.GetLogistics(GetHeaderCommon());
Assert.IsNotNull(result?.Results);
_Logger.Information($"{_TestContext?.TestName} completed");
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
NonThrowTryCatch();
}
[TestMethod]
public async Task GetLogisticsApi()
{
HttpClient httpClient = _WebApplicationFactory.CreateClient();
_Logger.Information("Starting Web Application");
HttpClient? httpClient = _WebApplicationFactory?.CreateClient();
_Logger?.LogInformation("Starting Web Application");
Assert.IsTrue(httpClient is not null);
string? json = await httpClient.GetStringAsync($"api/{_ControllerName}/logistics");
File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetLogistics)}.json"), json);
Result<HeaderCommon[]>? result = System.Text.Json.JsonSerializer.Deserialize<Result<HeaderCommon[]>>(json);
Assert.IsNotNull(result?.Results);
_Logger.Information($"{_TestContext?.TestName} completed");
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
NonThrowTryCatch();
}
[TestMethod]
public async Task PostLogisticsApi()
{
HttpClient httpClient = _WebApplicationFactory.CreateClient();
_Logger.Information("Starting Web Application");
HttpClient? httpClient = _WebApplicationFactory?.CreateClient();
_Logger?.LogInformation("Starting Web Application");
Assert.IsTrue(httpClient is not null);
HttpResponseMessage httpResponseMessage = await httpClient.PostAsync($"api/{_ControllerName}/logistics", GetStringContent());
Assert.IsTrue(httpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK);
_Logger.Information($"{_TestContext?.TestName} completed");
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
NonThrowTryCatch();
}
[TestMethod]
public void GetProcessDataStandardFormat()
{
_Logger.Information("Starting Web Application");
IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider;
IExportRepository exportRepository = serviceProvider.GetRequiredService<IExportRepository>();
string result = exportRepository.GetProcessDataStandardFormat(GetHeaderCommon());
_Logger?.LogInformation("Starting Web Application");
IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider;
IExportRepository? exportRepository = serviceProvider?.GetRequiredService<IExportRepository>();
string? result = exportRepository?.GetProcessDataStandardFormat(GetHeaderCommon());
Assert.IsNotNull(result);
_Logger.Information($"{_TestContext?.TestName} completed");
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
NonThrowTryCatch();
}
[TestMethod]
public async Task GetProcessDataStandardFormatApi()
{
HttpClient httpClient = _WebApplicationFactory.CreateClient();
_Logger.Information("Starting Web Application");
HttpClient? httpClient = _WebApplicationFactory?.CreateClient();
_Logger?.LogInformation("Starting Web Application");
Assert.IsTrue(httpClient is not null);
string? result = await httpClient.GetStringAsync($"api/{_ControllerName}/processDataStandardFormat");
File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetProcessDataStandardFormat)}.pdsf"), result);
Assert.IsNotNull(result);
_Logger.Information($"{_TestContext?.TestName} completed");
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
NonThrowTryCatch();
}
[TestMethod]
public async Task PostProcessDataStandardFormatApi()
{
HttpClient httpClient = _WebApplicationFactory.CreateClient();
_Logger.Information("Starting Web Application");
HttpClient? httpClient = _WebApplicationFactory?.CreateClient();
_Logger?.LogInformation("Starting Web Application");
Assert.IsTrue(httpClient is not null);
HttpResponseMessage httpResponseMessage = await httpClient.PostAsync($"api/{_ControllerName}/processDataStandardFormat", GetStringContent());
Assert.IsTrue(httpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK);
_Logger.Information($"{_TestContext?.TestName} completed");
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
NonThrowTryCatch();
}