Template
chore: deploy initial code base
This commit is contained in:
+38
@@ -0,0 +1,38 @@
|
||||
using Strata.ContinuousImprovement.Biz.DbContexts;
|
||||
using Strata.ContinuousImprovement.Api.Test.Integration.Setup;
|
||||
using Strata.ContinuousImprovement.Api.Test.Integration.Tests;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
[ExcludeFromCodeCoverage]
|
||||
[SetUpFixture]
|
||||
public class ClientIntegrationTestFixture
|
||||
{
|
||||
[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 clientService = new ApiTestBase();
|
||||
await clientService.ClientValidation();
|
||||
var services = ServiceCollectionFactory.Create();
|
||||
await services.SetupServiceProperties(clientService.StrataId);
|
||||
}
|
||||
|
||||
[OneTimeTearDown]
|
||||
public async Task TearDown()
|
||||
{
|
||||
try
|
||||
{
|
||||
var centralDbContext = ServiceProperties.CentralDbContext as CentralDbContext;
|
||||
if (centralDbContext == null) { return; }
|
||||
_ = await centralDbContext.Database.EnsureDeletedAsync(CancellationToken.None);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Strata.ApiLib.Core.Cors.Extensions;
|
||||
using Strata.ApiLib.Core.StrataAuthentication.Bootstrappers;
|
||||
using Strata.Configuration.Client.DependencyInjection.ConsoleApps;
|
||||
using Strata.ContinuousImprovement.Biz.Configurations;
|
||||
using Strata.ContinuousImprovement.Biz.DbContexts;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace Strata.ContinuousImprovement.Api.Test.Integration.Setup
|
||||
{
|
||||
[ExcludeFromCodeCoverage]
|
||||
public static class ServiceCollectionFactory
|
||||
{
|
||||
// dev master
|
||||
private static readonly Guid DatabaseGuid = new("54409f55-5ddf-4748-8222-9ccfb04fb4f5");
|
||||
private static readonly string IntegrationTesting = $"ci_api_{DateTime.Now:yyyyMMdd}_{Guid.NewGuid():N}";
|
||||
|
||||
public static IServiceCollection Create()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
try
|
||||
{
|
||||
var configuration = StrataConfigurationFactory.GetConfiguration();
|
||||
services.AddSingleton(configuration);
|
||||
services.AddStrataAuthentication(configuration);
|
||||
services.AddStrataCors(configuration);
|
||||
services.AddContinuousImprovementServices(configuration, IntegrationTesting);
|
||||
services.AddAsyncDbContextFactory<JazzDbContext>(options =>
|
||||
{
|
||||
options
|
||||
.WithConnectionString((provider, token) => provider.GetConnectionStringFromSmc(DatabaseGuid, token))
|
||||
.WithDbContextOptions((connectionString, provider, builder) =>
|
||||
builder.UseSqlServer(connectionString, options => { options.CommandTimeout(300); }));
|
||||
});
|
||||
services.AddLogging();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Moq;
|
||||
using Strata.Analytics.Client;
|
||||
using Strata.Analytics.Models.Workbooks;
|
||||
using Strata.ContinuousImprovement.Api.Test.Integration.Info;
|
||||
using Strata.ContinuousImprovement.Biz.DbContexts;
|
||||
using Strata.ContinuousImprovement.Biz.Generics;
|
||||
using Strata.ContinuousImprovement.Biz.OpportunityTypeWorkbooks;
|
||||
using Strata.ContinuousImprovement.Biz.OpportunityWorkbooks;
|
||||
using Strata.ContinuousImprovement.JazzEntityFrameworkStub;
|
||||
using Strata.SqlTools.Configuration.Common.AsyncFactory;
|
||||
|
||||
namespace Strata.ContinuousImprovement.Api.Test.Integration.Setup
|
||||
{
|
||||
public static class ServiceProperties
|
||||
{
|
||||
// dev master
|
||||
internal static readonly Guid DatabaseGuid = new("54409f55-5ddf-4748-8222-9ccfb04fb4f5");
|
||||
|
||||
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
|
||||
public static ICentralDbContext CentralDbContext { get; set; }
|
||||
public static JazzEntityFrameworkFactory JazzConnBuilderFactory { get; set; }
|
||||
public static IJazzDbContext JazzDbContext { get; set; }
|
||||
public static Biz.DbContexts.Snowflake.ISnowflakeDatabaseContext SnowflakeDatabaseContext { get; set; }
|
||||
public static IServiceCollection Services { get; set; }
|
||||
public static IServiceProvider ServiceProvider { get; set; }
|
||||
public static IAnalyticsService AnalyticsService { get; set; }
|
||||
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
|
||||
|
||||
public static string BaseAddress { get; set; } = string.Empty;
|
||||
|
||||
private static int WorkbookId = 9;
|
||||
|
||||
public static async Task SetupServiceProperties(this IServiceCollection services, int strataId = 0)
|
||||
{
|
||||
#if DEBUG
|
||||
Environment.SetEnvironmentVariable("AWS_Profile", "sdt-continuous-improvement-service-role");
|
||||
#endif
|
||||
services.AddOptions<AddressInfo>().BindConfiguration("address").ValidateDataAnnotations(); //bind the options for aws and validates for missing values
|
||||
|
||||
Services = services;
|
||||
var serviceProvider = services.BuildServiceProvider();
|
||||
ServiceProvider = serviceProvider;
|
||||
|
||||
try
|
||||
{
|
||||
var sfConnBuilderFactory = serviceProvider.GetRequiredService<Biz.DbContexts.Snowflake.ISnowflakeDatabaseContextFactory>();
|
||||
SnowflakeDatabaseContext = sfConnBuilderFactory.Create($"{DatabaseGuid:N}");
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// ILB
|
||||
}
|
||||
var sectionAccessor = serviceProvider.GetRequiredService<IOptions<AddressInfo>>();
|
||||
var addressInfo = sectionAccessor.Value;
|
||||
if (addressInfo != null && !string.IsNullOrEmpty(addressInfo.BaseAddress))
|
||||
{
|
||||
BaseAddress = addressInfo.BaseAddress;
|
||||
}
|
||||
try
|
||||
{
|
||||
var jazzConnBuilderFactory = serviceProvider.GetRequiredService<IAsyncDbContextFactory<JazzDbContext>>();
|
||||
JazzDbContext = await jazzConnBuilderFactory.CreateDbContextAsync(CancellationToken.None);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// ILB
|
||||
}
|
||||
|
||||
await BuildPostgresDb(strataId);
|
||||
|
||||
BuildAnalyticsService(strataId);
|
||||
}
|
||||
|
||||
private static async Task BuildPostgresDb(int strataId)
|
||||
{
|
||||
try
|
||||
{
|
||||
CentralDbContext = ServiceProvider.GetService<CentralDbContext>() as CentralDbContext
|
||||
?? throw new NullReferenceException(nameof(CentralDbContext));
|
||||
var centralDbContext = CentralDbContext as CentralDbContext;
|
||||
if (centralDbContext == null) { throw new NullReferenceException(nameof(centralDbContext)); }
|
||||
|
||||
await centralDbContext.Database.MigrateAsync(CancellationToken.None);
|
||||
Assert.That(centralDbContext, Is.Not.Null);
|
||||
|
||||
JazzConnBuilderFactory = new JazzEntityFrameworkFactory();
|
||||
await JazzConnBuilderFactory.EnsureCreatedAsync(centralDbContext, JazzDbContext, CancellationToken.None);
|
||||
var workbook = new OpportunityWorkbook
|
||||
{
|
||||
StrataId = strataId,
|
||||
WorkbookId = WorkbookId,
|
||||
OpportunityGuid = Guid.NewGuid(),
|
||||
Name = "Test",
|
||||
};
|
||||
if (!centralDbContext.OpportunityWorkbooks.Any(ow => ow.StrataId == workbook.StrataId
|
||||
&& ow.WorkbookId == workbook.WorkbookId))
|
||||
{
|
||||
await centralDbContext.OpportunityWorkbooks.AddAsync(workbook, CancellationToken.None);
|
||||
}
|
||||
var oppTypeWorkbook = new OpportunityTypeWorkbook
|
||||
{
|
||||
StrataId = strataId,
|
||||
WorkbookId = WorkbookId,
|
||||
OpportunityType = (int)OpportunityTypes.ChargeCode,
|
||||
Name = "Test",
|
||||
};
|
||||
if (!centralDbContext.OpportunityTypeWorkbooks.Any(otw => otw.StrataId == oppTypeWorkbook.StrataId
|
||||
&& otw.WorkbookId == oppTypeWorkbook.WorkbookId))
|
||||
{
|
||||
await centralDbContext.OpportunityTypeWorkbooks.AddAsync(oppTypeWorkbook, CancellationToken.None);
|
||||
}
|
||||
|
||||
await centralDbContext.SaveChangesAsync(CancellationToken.None);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static void BuildAnalyticsService(int strataId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var analyticsService = Mock.Of<IAnalyticsService>();
|
||||
var workbook = new Workbook
|
||||
{
|
||||
Name = "Test",
|
||||
WorkbookId = WorkbookId,
|
||||
IsHidden = false,
|
||||
StrataId = strataId,
|
||||
};
|
||||
var workbooks = new List<Workbook> { workbook };
|
||||
workbooks.AddRange(Enumerable.Range(1, 1000).Where(i => i != WorkbookId).Select(i => new Workbook
|
||||
{
|
||||
Name = $"Test {i}",
|
||||
WorkbookId = i,
|
||||
IsHidden = (i % 2 == 0),
|
||||
StrataId = (i % 3 == 0) ? 9 : strataId,
|
||||
}));
|
||||
//GetWorkbooksAsync
|
||||
Mock.Get(analyticsService)
|
||||
.Setup(s => s.GetWorkbookAsync(It.IsAny<int>(), It.IsAny<CancellationToken>()))
|
||||
.Returns<int, CancellationToken>((id, token)
|
||||
=> Task.FromResult(id == WorkbookId ? workbook : null));
|
||||
Mock.Get(analyticsService)
|
||||
.Setup(s => s.GetWorkbooksAsync(It.IsAny<bool>(), It.IsAny<CancellationToken>()))
|
||||
.Returns<bool, CancellationToken>((isHidden, token)
|
||||
=> Task.FromResult(workbooks.Where(w => w.IsHidden == isHidden)));
|
||||
AnalyticsService = analyticsService;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace Strata.ContinuousImprovement.Api.Test.Integration.Setup
|
||||
{
|
||||
public class VaultResponse
|
||||
{
|
||||
public string UserName { get; set; }
|
||||
public string Password { get; set; }
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user