285 lines
14 KiB
C#
285 lines
14 KiB
C#
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 OI.Metrology.Shared.Services;
|
|
using System.Data;
|
|
using System.Net.Http.Json;
|
|
|
|
namespace OI.Metrology.Tests;
|
|
|
|
[TestClass]
|
|
public class UnitTestToolTypesController
|
|
{
|
|
|
|
#pragma warning disable CS8618
|
|
|
|
private static ILogger? _Logger;
|
|
private static string _ControllerName;
|
|
private static TestContext _TestContext;
|
|
private static WebApplicationFactory<Server.Program>? _WebApplicationFactory;
|
|
|
|
#pragma warning restore
|
|
|
|
[ClassInitialize]
|
|
public static void ClassInitAsync(TestContext testContext)
|
|
{
|
|
_TestContext = testContext;
|
|
_WebApplicationFactory = new WebApplicationFactory<Server.Program>();
|
|
IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider;
|
|
_Logger = serviceProvider.GetRequiredService<ILogger<Server.Program>>();
|
|
_ControllerName = nameof(Server.ApiControllers.ToolTypesController)[..^10];
|
|
}
|
|
|
|
private static void NonThrowTryCatch()
|
|
{
|
|
try
|
|
{ throw new Exception(); }
|
|
catch (Exception) { }
|
|
}
|
|
|
|
[TestMethod]
|
|
public void TestControllerName()
|
|
{
|
|
_Logger?.LogInformation("Starting Web Application");
|
|
Assert.AreEqual(IToolTypesController<string>.GetRouteName(), _ControllerName);
|
|
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Index()
|
|
{
|
|
_Logger?.LogInformation("Starting Web Application");
|
|
IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider;
|
|
IMetrologyRepository? metrologyRepository = serviceProvider?.GetRequiredService<IMetrologyRepository>();
|
|
IToolTypesRepository? toolTypesRepository = serviceProvider?.GetRequiredService<IToolTypesRepository>();
|
|
Assert.IsTrue(metrologyRepository is not null);
|
|
Result<ToolTypeNameId[]>? result = toolTypesRepository?.Index(metrologyRepository);
|
|
Assert.IsNotNull(result?.Results);
|
|
Assert.IsTrue(result.Results.Length != 0);
|
|
Assert.IsFalse(result.Results.All(l => l.ID == 0));
|
|
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task IndexApi()
|
|
{
|
|
HttpClient? httpClient = _WebApplicationFactory?.CreateClient();
|
|
_Logger?.LogInformation("Starting Web Application");
|
|
Assert.IsTrue(httpClient is not null);
|
|
string? json = await httpClient.GetStringAsync($"api/{_ControllerName}");
|
|
File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(Index)}.json"), json);
|
|
Result<ToolTypeNameId[]>? result = System.Text.Json.JsonSerializer.Deserialize<Result<ToolTypeNameId[]>>(json);
|
|
Assert.IsNotNull(result?.Results);
|
|
Assert.IsTrue(result.Results.Length != 0);
|
|
Assert.IsFalse(result.Results.All(l => l.ID == 0));
|
|
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void GetToolTypeMetadata()
|
|
{
|
|
_Logger?.LogInformation("Starting Web Application");
|
|
IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider;
|
|
IMetrologyRepository? metrologyRepository = serviceProvider?.GetRequiredService<IMetrologyRepository>();
|
|
IToolTypesRepository? toolTypesRepository = serviceProvider?.GetRequiredService<IToolTypesRepository>();
|
|
Assert.IsTrue(metrologyRepository is not null);
|
|
Result<ToolTypeMetadataResult>? result = toolTypesRepository?.GetToolTypeMetadata(metrologyRepository, id: 1, sortby: string.Empty);
|
|
Assert.IsNotNull(result?.Results);
|
|
Assert.IsNotNull(result.Results.Metadata);
|
|
Assert.IsTrue(result.Results.Metadata.Length != 0);
|
|
Assert.IsFalse(result.Results.Metadata.All(l => l.ToolTypeID == 0));
|
|
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task GetToolTypeMetadataApi()
|
|
{
|
|
HttpClient? httpClient = _WebApplicationFactory?.CreateClient();
|
|
_Logger?.LogInformation("Starting Web Application");
|
|
Assert.IsTrue(httpClient is not null);
|
|
string? json = await httpClient.GetStringAsync($"api/{_ControllerName}/1");
|
|
File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetToolTypeMetadata)}.json"), json);
|
|
Result<ToolTypeMetadataResult>? result = System.Text.Json.JsonSerializer.Deserialize<Result<ToolTypeMetadataResult>>(json);
|
|
Assert.IsNotNull(result?.Results);
|
|
Assert.IsNotNull(result.Results.Metadata);
|
|
Assert.IsTrue(result.Results.Metadata.Length != 0);
|
|
Assert.IsFalse(result.Results.Metadata.All(l => l.ToolTypeID == 0));
|
|
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void GetHeaders()
|
|
{
|
|
_Logger?.LogInformation("Starting Web Application");
|
|
IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider;
|
|
IMetrologyRepository? metrologyRepository = serviceProvider?.GetRequiredService<IMetrologyRepository>();
|
|
IToolTypesRepository? toolTypesRepository = serviceProvider?.GetRequiredService<IToolTypesRepository>();
|
|
Assert.IsTrue(metrologyRepository is not null);
|
|
Result<DataTable>? result = toolTypesRepository?.GetHeaders(metrologyRepository, id: 1, datebegin: null, dateend: null, page: null, pagesize: null, headerid: null);
|
|
Assert.IsNotNull(result?.Results);
|
|
Assert.IsNotNull(result.Results.Rows.Count > 0);
|
|
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task GetHeadersApi()
|
|
{
|
|
HttpClient? httpClient = _WebApplicationFactory?.CreateClient();
|
|
_Logger?.LogInformation("Starting Web Application");
|
|
Assert.IsTrue(httpClient is not null);
|
|
string? json = await httpClient.GetStringAsync($"api/{_ControllerName}/1/headers");
|
|
File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetHeaders)}.json"), json);
|
|
Result<DataTable>? result = Newtonsoft.Json.JsonConvert.DeserializeObject<Result<DataTable>>(json);
|
|
Assert.IsNotNull(result?.Results);
|
|
Assert.IsNotNull(result.Results.Rows.Count > 0);
|
|
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void GetHeaderTitles()
|
|
{
|
|
_Logger?.LogInformation("Starting Web Application");
|
|
IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider;
|
|
IMetrologyRepository? metrologyRepository = serviceProvider?.GetRequiredService<IMetrologyRepository>();
|
|
IToolTypesRepository? toolTypesRepository = serviceProvider?.GetRequiredService<IToolTypesRepository>();
|
|
Assert.IsTrue(metrologyRepository is not null);
|
|
Result<HeaderCommon[]>? result = toolTypesRepository?.GetHeaderTitles(metrologyRepository, id: -1, page: null, pagesize: null);
|
|
Assert.IsNotNull(result?.Results);
|
|
Assert.IsTrue(result?.Results.Length != 0);
|
|
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task GetHeaderTitlesApi()
|
|
{
|
|
HttpClient? httpClient = _WebApplicationFactory?.CreateClient();
|
|
_Logger?.LogInformation("Starting Web Application");
|
|
Assert.IsTrue(httpClient is not null);
|
|
string? json = await httpClient.GetStringAsync($"api/{_ControllerName}/-1/headertitles");
|
|
File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetHeaderTitles)}.json"), json);
|
|
Result<HeaderCommon[]>? result = System.Text.Json.JsonSerializer.Deserialize<Result<HeaderCommon[]>>(json);
|
|
Assert.IsNotNull(result?.Results);
|
|
Assert.IsTrue(result.Results.Length != 0);
|
|
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void GetHeaderFields()
|
|
{
|
|
_Logger?.LogInformation("Starting Web Application");
|
|
IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider;
|
|
IMetrologyRepository? metrologyRepository = serviceProvider?.GetRequiredService<IMetrologyRepository>();
|
|
IToolTypesRepository? toolTypesRepository = serviceProvider?.GetRequiredService<IToolTypesRepository>();
|
|
Assert.IsTrue(metrologyRepository is not null);
|
|
Result<ColumnValue[]>? result = toolTypesRepository?.GetHeaderFields(metrologyRepository, id: 1, headerid: 1);
|
|
Assert.IsNotNull(result?.Results);
|
|
Assert.IsTrue(result.Results.Length != 0);
|
|
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task GetHeaderFieldsApi()
|
|
{
|
|
HttpClient? httpClient = _WebApplicationFactory?.CreateClient();
|
|
_Logger?.LogInformation("Starting Web Application");
|
|
Assert.IsTrue(httpClient is not null);
|
|
string? json = await httpClient.GetStringAsync($"api/{_ControllerName}/1/headers/1/fields");
|
|
File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetHeaderFields)}.json"), json);
|
|
Result<ColumnValue[]>? result = System.Text.Json.JsonSerializer.Deserialize<Result<ColumnValue[]>>(json);
|
|
Assert.IsNotNull(result?.Results);
|
|
Assert.IsTrue(result.Results.Length != 0);
|
|
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void GetData()
|
|
{
|
|
_Logger?.LogInformation("Starting Web Application");
|
|
IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider;
|
|
IMetrologyRepository? metrologyRepository = serviceProvider?.GetRequiredService<IMetrologyRepository>();
|
|
IToolTypesRepository? toolTypesRepository = serviceProvider?.GetRequiredService<IToolTypesRepository>();
|
|
Assert.IsTrue(metrologyRepository is not null);
|
|
Result<DataTable>? result = toolTypesRepository?.GetData(metrologyRepository, id: 1, headerid: 1);
|
|
Assert.IsNotNull(result?.Results);
|
|
Assert.IsNotNull(result.Results.Rows.Count > 0);
|
|
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task GetDataApi()
|
|
{
|
|
HttpClient? httpClient = _WebApplicationFactory?.CreateClient();
|
|
_Logger?.LogInformation("Starting Web Application");
|
|
Assert.IsTrue(httpClient is not null);
|
|
string? json = await httpClient.GetStringAsync($"api/{_ControllerName}/1/headers/1/data");
|
|
File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetData)}.json"), json);
|
|
Result<DataTable>? result = Newtonsoft.Json.JsonConvert.DeserializeObject<Result<DataTable>>(json);
|
|
Assert.IsNotNull(result?.Results);
|
|
Assert.IsNotNull(result.Results.Rows.Count > 0);
|
|
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
|
|
}
|
|
|
|
[Ignore]
|
|
[TestMethod]
|
|
public void GetAttachment()
|
|
{
|
|
_Logger?.LogInformation("Starting Web Application");
|
|
int toolTypeId = 1;
|
|
string tabletype = "data";
|
|
string filename = "data.txt";
|
|
string attachmentId = "ffdf5410-ca19-4097-bfa4-b398e236d07e";
|
|
IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider;
|
|
IAttachmentsService? attachmentsService = serviceProvider?.GetRequiredService<IAttachmentsService>();
|
|
IMetrologyRepository? metrologyRepository = serviceProvider?.GetRequiredService<IMetrologyRepository>();
|
|
IToolTypesRepository? toolTypesRepository = serviceProvider?.GetRequiredService<IToolTypesRepository>();
|
|
Assert.IsTrue(attachmentsService is not null);
|
|
Assert.IsTrue(metrologyRepository is not null);
|
|
Assert.IsTrue(toolTypesRepository is not null);
|
|
(string? message, string? _, Stream? _) = toolTypesRepository.GetAttachment(metrologyRepository, attachmentsService, toolTypeId, tabletype, attachmentId, filename);
|
|
Assert.IsTrue(message is null);
|
|
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
|
|
}
|
|
|
|
[Ignore]
|
|
[TestMethod]
|
|
public async Task GetAttachmentApi()
|
|
{
|
|
HttpClient? httpClient = _WebApplicationFactory?.CreateClient();
|
|
_Logger?.LogInformation("Starting Web Application");
|
|
Assert.IsTrue(httpClient is not null);
|
|
_ = await httpClient.GetFromJsonAsync<object>($"api/{_ControllerName}/1/data/files/ffdf5410-ca19-4097-bfa4-b398e236d07e/data.txt");
|
|
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
|
|
}
|
|
|
|
// [Ignore]
|
|
[TestMethod]
|
|
public void OIExport()
|
|
{
|
|
_Logger?.LogInformation("Starting Web Application");
|
|
IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider;
|
|
IAttachmentsService? attachmentsService = serviceProvider?.GetRequiredService<IAttachmentsService>();
|
|
IMetrologyRepository? metrologyRepository = serviceProvider?.GetRequiredService<IMetrologyRepository>();
|
|
IToolTypesRepository? toolTypesRepository = serviceProvider?.GetRequiredService<IToolTypesRepository>();
|
|
Server.Models.AppSettings? appSettings = serviceProvider?.GetRequiredService<Server.Models.AppSettings>();
|
|
Assert.IsTrue(appSettings is not null);
|
|
Assert.IsTrue(attachmentsService is not null);
|
|
Assert.IsTrue(metrologyRepository is not null);
|
|
string? message = toolTypesRepository?.OIExport(metrologyRepository, attachmentsService, toolTypeId: 1, headerid: 1);
|
|
Assert.IsTrue(message is null);
|
|
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
|
|
}
|
|
|
|
[Ignore]
|
|
[TestMethod]
|
|
public async Task OIExportApi()
|
|
{
|
|
HttpClient? httpClient = _WebApplicationFactory?.CreateClient();
|
|
_Logger?.LogInformation("Starting Web Application");
|
|
Assert.IsTrue(httpClient is not null);
|
|
_ = await httpClient.GetFromJsonAsync<object>($"api/{_ControllerName}/1/headers/1/export");
|
|
_Logger?.LogInformation("{TestName} completed", _TestContext?.TestName);
|
|
}
|
|
|
|
} |