2025-08-03 14:10:49 +02:00
|
|
|
using System.Diagnostics;
|
|
|
|
|
using Autofac;
|
|
|
|
|
using Microsoft.ApplicationInsights;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using SWP.Core.Configurations;
|
|
|
|
|
using SWP.Core.Configurations.JsonConfigProvider;
|
|
|
|
|
using SWP.Core.Database.ModuleRegistry;
|
|
|
|
|
using SWP.Core.ModuleRegistry;
|
|
|
|
|
using SWP.Core.SeqLogging;
|
|
|
|
|
|
|
|
|
|
namespace SWP.Core.X.TDD;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Act as base class for tests. Avoids duplication of test setup code
|
|
|
|
|
/// </summary>
|
|
|
|
|
[TestClass]
|
|
|
|
|
public abstract class TestFixture
|
|
|
|
|
{
|
|
|
|
|
private readonly string _configurationFilePath;
|
|
|
|
|
|
|
|
|
|
protected TestFixture() : this(null)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TestFixture(string configurationFilePath)
|
|
|
|
|
{
|
|
|
|
|
if (configurationFilePath is not null)
|
|
|
|
|
_configurationFilePath = configurationFilePath?.TrimEnd('/') + "/";
|
|
|
|
|
|
|
|
|
|
CreateContainerBuilder();
|
|
|
|
|
Container = ContainerBuilder.Build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected IContainer Container { get; private set; }
|
|
|
|
|
protected ContainerBuilder ContainerBuilder { get; private set; }
|
|
|
|
|
|
|
|
|
|
public virtual IConfigurationRoot Configuration()
|
|
|
|
|
{
|
|
|
|
|
var configuration = new ConfigurationBuilder()
|
|
|
|
|
.AddJsonFile($"{_configurationFilePath}appconfiguration.dev.json")
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
return configuration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual void CreateContainerBuilder()
|
|
|
|
|
{
|
|
|
|
|
var configuration = Configuration();
|
|
|
|
|
var builder = new ContainerBuilder();
|
|
|
|
|
|
|
|
|
|
builder.RegisterGeneric(typeof(Logger<>))
|
|
|
|
|
.As(typeof(ILogger<>))
|
|
|
|
|
.SingleInstance();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
builder.RegisterModule(new DbPostgreSqlModule
|
|
|
|
|
{
|
|
|
|
|
ConnectionString = configuration.GetConnectionString("DefaultConnection")
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(configuration.GetSection("ApplicationInsights").ToObject<TelemetryConfig>().ConnectionString))
|
|
|
|
|
builder.RegisterModule(new TelemetryModule
|
|
|
|
|
{
|
|
|
|
|
TelemetryConfig = configuration.GetSection("ApplicationInsights").ToObject<TelemetryConfig>()
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(configuration.GetSection("SeqConfiguration").ToObject<SeqConfiguration>().IngestionEndpoint))
|
|
|
|
|
builder.RegisterModule(new SeqLoggingModule
|
|
|
|
|
{
|
|
|
|
|
SeqConfiguration = configuration.GetSection("SeqConfiguration").ToObject<SeqConfiguration>()
|
|
|
|
|
});
|
|
|
|
|
|
2025-08-02 22:16:39 +02:00
|
|
|
builder.RegisterModule<SecurityModule>();
|
|
|
|
|
|
2025-08-03 14:10:49 +02:00
|
|
|
ContainerBuilder = builder;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestCleanup]
|
|
|
|
|
public void CleanUp()
|
|
|
|
|
{
|
|
|
|
|
Trace.Flush();
|
|
|
|
|
if (Container.IsRegistered<TelemetryClient>())
|
|
|
|
|
{
|
|
|
|
|
var telemetryClient = Container.Resolve<TelemetryClient>();
|
|
|
|
|
telemetryClient.Flush();
|
|
|
|
|
}
|
|
|
|
|
if (Container is null) return;
|
|
|
|
|
|
|
|
|
|
Container.Dispose();
|
|
|
|
|
Container = null;
|
|
|
|
|
}
|
|
|
|
|
}
|