Template
253 lines
12 KiB
C#
253 lines
12 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using Moq;
|
|
using Snowflake.Data.Client;
|
|
using Strata.ContinuousImprovement.Biz.ClientWarehouses;
|
|
using Strata.ContinuousImprovement.Biz.DbContexts;
|
|
using Strata.ContinuousImprovement.Biz.DbContexts.Snowflake;
|
|
using Strata.ContinuousImprovement.Biz.Exploration;
|
|
using Strata.ContinuousImprovement.Biz.FiscalMonths;
|
|
using Strata.ContinuousImprovement.Biz.GMDN;
|
|
using Strata.ContinuousImprovement.Biz.Network;
|
|
using Strata.ContinuousImprovement.Biz.OpportunityComments;
|
|
using Strata.ContinuousImprovement.Biz.OpportunityTypeWorkbooks;
|
|
using Strata.ContinuousImprovement.Biz.OpportunityWorkbooks;
|
|
using Strata.ContinuousImprovement.Biz.Test.Unit.Utilities;
|
|
using Strata.ContinuousImprovement.Biz.Utilities;
|
|
using Strata.ContinuousImprovement.Client;
|
|
using Strata.ContinuousImprovement.JazzEntityFrameworkStub;
|
|
using Strata.CoreLib.Claims;
|
|
using Strata.Schema.Client;
|
|
using Strata.SqlTools.Configuration.Common.AsyncFactory;
|
|
using Strata.StrataSphereCompare.Client;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace Strata.ContinuousImprovement.Biz.Test.Integration
|
|
{
|
|
[ExcludeFromCodeCoverage]
|
|
[SetUpFixture]
|
|
public class IntegrationTestBase
|
|
{
|
|
protected VerifySettings verifyScrubbedSettings;
|
|
protected VerifySettings verifySettings;
|
|
protected VerifySettings verifySqlSettings;
|
|
protected VerifySettings verifyErrorSettings;
|
|
|
|
public const string Quick = "quick";
|
|
public const string ContinuousImprovementEndpoint = "https://continuousimprovement-api.dev.stratanetwork.net";
|
|
|
|
protected readonly Guid GmAutomationDatabase = new("cda90beb-6c2e-41af-9083-857a09fe8183"); // GM Automation (Refreshed)
|
|
|
|
protected Guid VariationOpportunityGuid { get; set; }
|
|
protected Guid LOSOpportunityGuid { get; set; }
|
|
protected ICentralDbContext CentralDbContext { get; set; }
|
|
protected JazzEntityFrameworkFactory JazzConnBuilderFactory { get; set; }
|
|
#pragma warning disable NUnit1032
|
|
protected IJazzDbContext JazzDbContext { get; set; }
|
|
protected ISnowflakeDatabaseContext SnowflakeDatabaseContext;
|
|
protected IServiceProvider ServiceProvider; //used to grab any necessary DI objects
|
|
#pragma warning restore NUnit1032
|
|
protected ILogger<IntegrationTestBase>? logger;
|
|
protected IContinuousImprovementService ContinuousImprovementService;
|
|
protected IExplorationService ExplorationService;
|
|
protected ISchemaServiceClient SchemaServiceClient;
|
|
protected IExplorationFilterService ExplorationFilterService; // ADD THIS LINE
|
|
protected ILogger<ContinuousImprovementService> ServiceLogger;
|
|
internal IClaimsPrincipalAccessor ClaimsPrincipalAccessor { get; set; }
|
|
|
|
[OneTimeSetUp]
|
|
public virtual async Task SetDbContextAndServices()
|
|
{
|
|
#if DEBUG
|
|
//When running locally sets the profile to the CI profile so you have access to resources
|
|
Environment.SetEnvironmentVariable("AWS_Profile", "sdt-continuous-improvement-service-role");
|
|
#endif
|
|
var services = ServiceCollectionFactory.Create();
|
|
services.AddScoped<ISnowflakeDatabaseContext, SnowflakeDatabaseContext>(provider =>
|
|
{
|
|
ISnowflakeDatabaseContextFactory snowflakeDatabaseContextFactory = provider.GetRequiredService<ISnowflakeDatabaseContextFactory>();
|
|
return snowflakeDatabaseContextFactory.Create($"{GmAutomationDatabase:N}");
|
|
});
|
|
ServiceProvider = services.BuildServiceProvider();
|
|
var sfConnBuilderFactory = ServiceProvider.GetRequiredService<ISnowflakeDatabaseContextFactory>();
|
|
SnowflakeDatabaseContext = sfConnBuilderFactory.Create($"{GmAutomationDatabase:N}");
|
|
|
|
try
|
|
{
|
|
var jazzConnBuilderFactory = ServiceProvider.GetRequiredService<IAsyncDbContextFactory<JazzDbContext>>();
|
|
JazzDbContext = await jazzConnBuilderFactory.CreateDbContextAsync(CancellationToken.None);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
// ILB
|
|
}
|
|
|
|
try
|
|
{
|
|
CentralDbContext = ServiceProvider.GetService<CentralDbContext>()
|
|
?? throw new NullReferenceException(nameof(DbContexts.CentralDbContext));
|
|
await ((CentralDbContext)CentralDbContext).Database.MigrateAsync(CancellationToken.None);
|
|
JazzConnBuilderFactory = new JazzEntityFrameworkFactory();
|
|
await JazzConnBuilderFactory.EnsureCreatedAsync(CentralDbContext, JazzDbContext, CancellationToken.None);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
throw;
|
|
}
|
|
|
|
#region Verifier Settings
|
|
|
|
verifySettings = TestExtensions.TestSettings();
|
|
verifySqlSettings = TestExtensions.TestSettings();
|
|
verifySqlSettings
|
|
.ScrubLinesWithReplace(_ =>
|
|
{
|
|
var regex = new Regex(@"(.*)\s+in\s+\([\d,]*\)(.*)", RegexOptions.IgnoreCase);
|
|
if (regex.IsMatch(_))
|
|
{
|
|
return regex.Replace(_, "$1 IN (...)$2");
|
|
}
|
|
var regexDate = new Regex(@"(.*)'[\d]{1,2}/[\d]{1,2}/[\d]{4}'(.*)");
|
|
if (regexDate.IsMatch(_))
|
|
{
|
|
var line = _;
|
|
while (regexDate.IsMatch(line))
|
|
{
|
|
line = regexDate.Replace(line, "$1'mm/dd/yyyy'$2");
|
|
}
|
|
return line;
|
|
}
|
|
return _;
|
|
});
|
|
verifyScrubbedSettings = TestExtensions.TestSettings();
|
|
verifyScrubbedSettings
|
|
.ScrubLinesWithReplace(replaceLine: _ => $"'{"".PadLeft(_.Length, '~')}'");
|
|
verifyScrubbedSettings
|
|
.ScrubMembersWithType<bool>();
|
|
verifyScrubbedSettings
|
|
.ScrubMembersWithType<short>();
|
|
verifyScrubbedSettings
|
|
.ScrubMembersWithType<int>();
|
|
verifyScrubbedSettings
|
|
.ScrubMembersWithType<long>();
|
|
verifyScrubbedSettings
|
|
.ScrubMembersWithType<byte>();
|
|
verifyScrubbedSettings
|
|
.ScrubMembersWithType<decimal>();
|
|
verifyScrubbedSettings
|
|
.ScrubMembersWithType<double>();
|
|
verifyErrorSettings = TestExtensions.TestSettings();
|
|
verifyErrorSettings.UseDirectory("errorSnapshots");
|
|
#endregion
|
|
|
|
SchemaServiceClient = Mock.Of<ISchemaServiceClient>();
|
|
ExplorationFilterService = new ExplorationFilterService(
|
|
ServiceProvider.GetRequiredService<IAsyncDbContextFactory<JazzDbContext>>(),
|
|
SchemaServiceClient,
|
|
Mock.Of<ILogger<ExplorationFilterService>>());
|
|
|
|
var httpClientFactory = ServiceProvider.GetRequiredService<IHttpClientFactory>();
|
|
var httpClient = httpClientFactory.CreateClient(Quick);
|
|
httpClient.BaseAddress = new Uri(ContinuousImprovementEndpoint);
|
|
ContinuousImprovementService = new ContinuousImprovementService(httpClient);
|
|
|
|
var featureFlagWrapper = Mock.Of<IFeatureFlagWrapper>();
|
|
Mock.Get(featureFlagWrapper)
|
|
.Setup(f => f.IsCiBenchmarkingEnabled(It.IsAny<bool>()))
|
|
.ReturnsAsync(true);
|
|
|
|
var strataSphereCompareService = Mock.Of<IStrataSphereCompareService>();
|
|
|
|
ExplorationService = new ExplorationService(
|
|
JazzConnBuilderFactory,
|
|
SnowflakeDatabaseContext,
|
|
Mock.Of<IFiscalMonthResolver>(),
|
|
featureFlagWrapper,
|
|
strataSphereCompareService,
|
|
ExplorationFilterService,
|
|
Mock.Of<ILogger<ExplorationService>>());
|
|
}
|
|
|
|
public static IEnumerable<TestCaseData> TestCaseSources()
|
|
{
|
|
foreach (bool value in new[] { false, true })
|
|
{
|
|
yield return new TestCaseData(nameof(ClientNetworkOpportunity), value)
|
|
.SetName(nameof(ClientNetworkOpportunity) + $" {(value ? "First" : "Count")}");
|
|
yield return new TestCaseData(nameof(ClientNetworkOpportunityLink), value)
|
|
.SetName(nameof(ClientNetworkOpportunityLink) + $" {(value ? "First" : "Count")}");
|
|
yield return new TestCaseData(nameof(ClientWarehouse), value)
|
|
.SetName(nameof(ClientWarehouse) + $" {(value ? "First" : "Count")}");
|
|
yield return new TestCaseData(nameof(Comment), value)
|
|
.SetName(nameof(Comment) + $" {(value ? "First" : "Count")}");
|
|
yield return new TestCaseData(nameof(DeviceIdentifierDimension), value)
|
|
.SetName(nameof(DeviceIdentifierDimension) + $" {(value ? "First" : "Count")}");
|
|
yield return new TestCaseData(nameof(DeviceIdentifierTerm), value)
|
|
.SetName(nameof(DeviceIdentifierTerm) + $" {(value ? "First" : "Count")}");
|
|
yield return new TestCaseData(nameof(GMDNCategory), value)
|
|
.SetName(nameof(GMDNCategory) + $" {(value ? "First" : "Count")}");
|
|
yield return new TestCaseData(nameof(GMDNTerm), value)
|
|
.SetName(nameof(GMDNTerm) + $" {(value ? "First" : "Count")}");
|
|
yield return new TestCaseData(nameof(GMDNTermCategory), value)
|
|
.SetName(nameof(GMDNTermCategory) + $" {(value ? "First" : "Count")}");
|
|
yield return new TestCaseData(nameof(GUDIDDevice), value)
|
|
.SetName(nameof(GUDIDDevice) + $" {(value ? "First" : "Count")}");
|
|
yield return new TestCaseData(nameof(GUDIDIdentifier), value)
|
|
.SetName(nameof(GUDIDIdentifier) + $" {(value ? "First" : "Count")}");
|
|
yield return new TestCaseData(nameof(GUDIDTerm), value)
|
|
.SetName(nameof(GUDIDTerm) + $" {(value ? "First" : "Count")}");
|
|
yield return new TestCaseData(nameof(NetworkOpportunity), value)
|
|
.SetName(nameof(NetworkOpportunity) + $" {(value ? "First" : "Count")}");
|
|
yield return new TestCaseData(nameof(OpportunityTypeWorkbook), value)
|
|
.SetName(nameof(OpportunityTypeWorkbook) + $" {(value ? "First" : "Count")}");
|
|
yield return new TestCaseData(nameof(OpportunityWorkbook), value)
|
|
.SetName(nameof(OpportunityWorkbook) + $" {(value ? "First" : "Count")}");
|
|
yield return new TestCaseData(nameof(TmpGUDIDIdentifier), value)
|
|
.SetName(nameof(TmpGUDIDIdentifier) + $" {(value ? "First" : "Count")}");
|
|
}
|
|
}
|
|
|
|
protected virtual async Task QueryTest<T, TI>(TI queryInfo)
|
|
where TI : class
|
|
where T : class
|
|
{
|
|
var query = Query(queryInfo);
|
|
if (string.IsNullOrEmpty(query)) { return; }
|
|
try
|
|
{
|
|
var result = await SnowflakeDatabaseContext
|
|
.QueryAsync<T>(query, nameof(QueryTest), CancellationToken.None);
|
|
await Verify(result, verifySettings)
|
|
.UseFileName(TestContext.CurrentContext.TestName(nameof(result)));
|
|
}
|
|
catch (SnowflakeDbException dbex)
|
|
{
|
|
// ILB
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
protected virtual string Query<T>(T queryInfo)
|
|
where T : class
|
|
=> "";
|
|
|
|
[OneTimeTearDown]
|
|
public async Task TearDown()
|
|
{
|
|
try
|
|
{
|
|
await ((CentralDbContext)CentralDbContext).Database.EnsureDeletedAsync(CancellationToken.None);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
}
|