using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace Elsa.Testing.Shared.Helpers
{
///
/// A helper class which 'builds' an for testing purposes.
/// The reason this is useful is that it also allows building-up callbacks to occur inside
/// the callback (which cannot be
/// done with a normal host builder alone).
///
public class ElsaHostBuilderBuilder
{
///
/// Gets a collection of the callbacks to be executed upon the of
/// the host builder as it is created.
///
/// The services callbacks
public IList> ServiceCallbacks { get; } = new List> {
services => services.AddLogging(l => l.SetMinimumLevel(LogLevel.Warning)),
};
///
/// Gets a collection of the callbacks to be executed upon the of
/// the host builder's services as it is created.
///
/// The Elsa callbacks
public IList> ElsaCallbacks { get; } = new List>();
Action ElsaConfiguration
=> ElsaCallbacks
.Where(x => !(x is null))
.Aggregate(EmptyElsaAction, (acc, next) => o => { acc(o); next(o); });
Action ServiceConfiguration
=> ServiceCallbacks
.Where(x => !(x is null))
.Aggregate(EmptyServicesAction, (acc, next) => o => { acc(o); next(o); });
static Action EmptyElsaAction => o => {};
static Action EmptyServicesAction => s => {};
///
/// Gets an configured using the
/// and .
///
/// A host builder.
public IHostBuilder GetHostBuilder()
{
return Host.CreateDefaultBuilder()
.ConfigureServices((hostBuilder, services) => {
services.AddElsa(ElsaConfiguration);
ServiceConfiguration(services);
});
}
}
}