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.Server.Models;
using OI.Metrology.Shared.Models.Stateless;
using Serilog;
using System.Net;
namespace OI.Metrology.Tests;
@ -13,10 +13,10 @@ public class UnitTestAppSettingsController
#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 UnitTestAppSettingsController
public static void ClassInitAsync(TestContext testContext)
{
_TestContext = testContext;
_Logger = Log.ForContext<UnitTestAppSettingsController>();
_WebApplicationFactory = new WebApplicationFactory<Server.Program>();
IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider;
_Logger = serviceProvider.GetRequiredService<ILogger<Server.Program>>();
_ControllerName = nameof(Server.ApiControllers.AppSettingsController)[..^10];
}
@ -39,88 +40,90 @@ public class UnitTestAppSettingsController
[TestMethod]
public void TestControllerName()
{
_Logger.Information("Starting Web Application");
_Logger?.LogInformation("Starting Web Application");
Assert.AreEqual(IAppSettingsController<object>.GetRouteName(), _ControllerName);
_Logger.Information($"{_TestContext?.TestName} completed");
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
NonThrowTryCatch();
}
[TestMethod]
public void TestConnectionString()
{
_Logger.Information("Starting Web Application");
IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider;
IAppSettingsRepository<Server.Models.Binder.AppSettings> appSettingsRepository = serviceProvider.GetRequiredService<IAppSettingsRepository<Server.Models.Binder.AppSettings>>();
appSettingsRepository.VerifyConnectionStrings();
_Logger.Information($"{_TestContext?.TestName} completed");
_Logger?.LogInformation("Starting Web Application");
IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider;
IAppSettingsRepository<Server.Models.Binder.AppSettings>? appSettingsRepository = serviceProvider?.GetRequiredService<IAppSettingsRepository<Server.Models.Binder.AppSettings>>();
appSettingsRepository?.VerifyConnectionStrings();
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
NonThrowTryCatch();
}
[TestMethod]
public void AppSettings()
{
_Logger.Information("Starting Web Application");
IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider;
AppSettings appSettings = serviceProvider.GetRequiredService<AppSettings>();
_Logger?.LogInformation("Starting Web Application");
IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider;
AppSettings? appSettings = serviceProvider?.GetRequiredService<AppSettings>();
Assert.IsNotNull(appSettings);
_Logger.Information($"{_TestContext?.TestName} completed");
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
NonThrowTryCatch();
}
[TestMethod]
public void GetAppSettings()
{
_Logger.Information("Starting Web Application");
IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider;
IAppSettingsRepository<Server.Models.Binder.AppSettings> appSettingsRepository = serviceProvider.GetRequiredService<IAppSettingsRepository<Server.Models.Binder.AppSettings>>();
Server.Models.Binder.AppSettings appSettings = appSettingsRepository.GetAppSettings();
_Logger?.LogInformation("Starting Web Application");
IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider;
IAppSettingsRepository<Server.Models.Binder.AppSettings>? appSettingsRepository = serviceProvider?.GetRequiredService<IAppSettingsRepository<Server.Models.Binder.AppSettings>>();
Server.Models.Binder.AppSettings? appSettings = appSettingsRepository?.GetAppSettings();
Assert.IsTrue(appSettings is not null);
_Logger.Information($"{_TestContext?.TestName} completed");
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
NonThrowTryCatch();
}
[TestMethod]
public async Task GetAppSettingsApi()
{
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 actionName = nameof(IAppSettingsController<object>.Action.App);
HttpResponseMessage httpResponseMessage = await httpClient.GetAsync($"api/{_ControllerName}/{actionName}");
HttpResponseMessage? httpResponseMessage = await httpClient.GetAsync($"api/{_ControllerName}/{actionName}");
Assert.AreEqual(HttpStatusCode.OK, httpResponseMessage.StatusCode);
Assert.AreEqual("application/json; charset=utf-8", httpResponseMessage.Content.Headers.ContentType?.ToString());
string json = await httpResponseMessage.Content.ReadAsStringAsync();
File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetAppSettings)}.json"), json);
Assert.IsNotNull(json);
Assert.IsTrue(json != "[]");
_Logger.Information($"{_TestContext?.TestName} completed");
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
NonThrowTryCatch();
}
[TestMethod]
public void GetBuildNumberAndGitCommitSeven()
{
_Logger.Information("Starting Web Application");
IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider;
IAppSettingsRepository<Server.Models.Binder.AppSettings> appSettingsRepository = serviceProvider.GetRequiredService<IAppSettingsRepository<Server.Models.Binder.AppSettings>>();
string result = appSettingsRepository.GetBuildNumberAndGitCommitSeven();
_Logger?.LogInformation("Starting Web Application");
IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider;
IAppSettingsRepository<Server.Models.Binder.AppSettings>? appSettingsRepository = serviceProvider?.GetRequiredService<IAppSettingsRepository<Server.Models.Binder.AppSettings>>();
string? result = appSettingsRepository?.GetBuildNumberAndGitCommitSeven();
Assert.IsTrue(result is not null);
_Logger.Information($"{_TestContext?.TestName} completed");
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
NonThrowTryCatch();
}
[TestMethod]
public async Task GetBuildNumberAndGitCommitSevenApi()
{
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 actionName = nameof(IAppSettingsController<object>.Action.DevOps);
HttpResponseMessage httpResponseMessage = await httpClient.GetAsync($"api/{_ControllerName}/{actionName}");
HttpResponseMessage? httpResponseMessage = await httpClient.GetAsync($"api/{_ControllerName}/{actionName}");
Assert.AreEqual(HttpStatusCode.OK, httpResponseMessage.StatusCode);
Assert.AreEqual("text/plain; charset=utf-8", httpResponseMessage.Content.Headers.ContentType?.ToString());
string json = await httpResponseMessage.Content.ReadAsStringAsync();
File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetBuildNumberAndGitCommitSeven)}.json"), json);
Assert.IsNotNull(json);
_Logger.Information($"{_TestContext?.TestName} completed");
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
NonThrowTryCatch();
}