using Elsa.Extensions;
using Elsa.Features.Services;
using Elsa.Workflows;
using Elsa.Workflows.Activities;
using FluentStorage;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Xunit.Abstractions;
namespace Elsa.Testing.Shared;
///
/// A builder that can be used to configure an for testing purposes.
///
public class TestApplicationBuilder
{
private readonly ITestOutputHelper _testOutputHelper;
private readonly ServiceCollection _services;
private Action _configureElsa;
///
/// Initializes a new instance of the class.
///
/// The test output helper.
public TestApplicationBuilder(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
_services = new();
_services
.AddSingleton(testOutputHelper)
.AddSingleton()
.AddLogging(logging => logging.AddProvider(new XunitLoggerProvider(testOutputHelper)).SetMinimumLevel(LogLevel.Debug));
_configureElsa += elsa => elsa
.AddActivitiesFrom()
.UseScheduling()
.UseCSharp(options => options.AllowHostCodeExecution = true)
.UseJavaScript()
.UseLiquid()
.UseWorkflowManagement()
.UseWorkflows(workflows => workflows
.WithStandardOutStreamProvider(_ => new StandardOutStreamProvider(new XunitConsoleTextWriter(_testOutputHelper)))
);
}
///
/// Builds the .
///
/// The .
public IServiceProvider Build()
{
_services.AddElsa(_configureElsa);
return _services.BuildServiceProvider();
}
///
/// Configures Elsa.
///
/// The configuration delegate.
/// The .
public TestApplicationBuilder ConfigureElsa(Action configure)
{
_configureElsa += configure;
return this;
}
///
/// Configures the service provider.
///
/// The configuration delegate.
/// The .
public TestApplicationBuilder ConfigureServices(Action configure)
{
configure(_services);
return this;
}
///
/// Configures the service provider to use a to capture standard output.
///
/// The capturing text writer.
/// The .
public TestApplicationBuilder WithCapturingTextWriter(CapturingTextWriter capturingTextWriter)
{
var combinedTextWriter = new CombinedTextWriter(capturingTextWriter, new XunitConsoleTextWriter(_testOutputHelper));
var provider = new StandardOutStreamProvider(combinedTextWriter);
ConfigureElsa(elsa => elsa.UseWorkflows(workflows => workflows.WithStandardOutStreamProvider(_ => provider)));
return this;
}
///
/// Adds a workflow to the service provider.
///
public TestApplicationBuilder AddWorkflow() where T : IWorkflow
{
return ConfigureElsa(elsa => elsa.AddWorkflow());
}
///
/// Adds activities from the assembly containing the specified type.
///
public TestApplicationBuilder AddActivitiesFrom()
{
return ConfigureElsa(elsa => elsa.AddActivitiesFrom());
}
///
/// Add workflows from the specified relative directory.
///
/// The path segments of the directory.
public TestApplicationBuilder WithWorkflowsFromDirectory(params string[] directory)
{
var assemblyLocation = System.Reflection.Assembly.GetExecutingAssembly().Location;
var assemblyDirectory = Path.GetDirectoryName(assemblyLocation)!;
var workflowsDirectory = directory.Prepend(assemblyDirectory).ToArray();
_configureElsa += elsa => elsa.UseFluentStorageProvider(storage => storage.BlobStorage = sp => StorageFactory.Blobs.DirectoryFiles(Path.Combine(workflowsDirectory)));
return this;
}
}