2022-04-12 13:55:35 +00:00
|
|
|
using Elsa.Extensions;
|
2023-03-24 21:25:48 +00:00
|
|
|
using Elsa.Features.Services;
|
2023-04-18 19:28:23 +00:00
|
|
|
using Elsa.Workflows.Core.Activities;
|
2023-09-14 08:50:02 +00:00
|
|
|
using Elsa.Workflows.Core.Contracts;
|
2022-05-26 10:47:31 +00:00
|
|
|
using Elsa.Workflows.Core.Services;
|
2023-09-14 14:30:18 +00:00
|
|
|
using FluentStorage;
|
2023-04-13 20:23:07 +00:00
|
|
|
using Microsoft.Extensions.Configuration;
|
2022-04-12 13:55:35 +00:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Xunit.Abstractions;
|
|
|
|
|
|
|
|
|
|
namespace Elsa.Testing.Shared;
|
|
|
|
|
|
2023-04-17 13:58:07 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// A builder that can be used to configure an <see cref="IServiceProvider"/> for testing purposes.
|
|
|
|
|
/// </summary>
|
2022-04-18 13:48:21 +00:00
|
|
|
public class TestApplicationBuilder
|
2022-04-12 13:55:35 +00:00
|
|
|
{
|
2022-04-12 15:08:31 +00:00
|
|
|
private readonly ITestOutputHelper _testOutputHelper;
|
|
|
|
|
private readonly ServiceCollection _services;
|
2023-03-24 21:25:48 +00:00
|
|
|
private Action<IModule> _configureElsa;
|
2022-04-12 15:08:31 +00:00
|
|
|
|
2023-04-17 13:58:07 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="TestApplicationBuilder"/> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="testOutputHelper">The test output helper.</param>
|
2022-04-18 13:48:21 +00:00
|
|
|
public TestApplicationBuilder(ITestOutputHelper testOutputHelper)
|
2022-04-12 13:55:35 +00:00
|
|
|
{
|
2022-04-12 15:08:31 +00:00
|
|
|
_testOutputHelper = testOutputHelper;
|
|
|
|
|
_services = new ServiceCollection();
|
2022-04-12 13:55:35 +00:00
|
|
|
|
2022-04-12 15:08:31 +00:00
|
|
|
_services
|
|
|
|
|
.AddSingleton(testOutputHelper)
|
2023-04-13 20:23:07 +00:00
|
|
|
.AddSingleton<IConfiguration, ConfigurationManager>()
|
2022-04-12 13:55:35 +00:00
|
|
|
.AddLogging(logging => logging.AddProvider(new XunitLoggerProvider(testOutputHelper)).SetMinimumLevel(LogLevel.Debug));
|
2023-09-14 14:30:18 +00:00
|
|
|
|
2023-03-24 21:25:48 +00:00
|
|
|
_configureElsa += elsa => elsa
|
2023-04-18 19:28:23 +00:00
|
|
|
.AddActivitiesFrom<WriteLine>()
|
|
|
|
|
.UseScheduling()
|
2023-11-09 12:35:02 +00:00
|
|
|
.UseCSharp()
|
2023-04-18 19:28:23 +00:00
|
|
|
.UseJavaScript()
|
|
|
|
|
.UseLiquid()
|
2023-05-28 09:54:12 +00:00
|
|
|
.UseDsl()
|
2023-07-14 17:19:41 +00:00
|
|
|
.UseWorkflowManagement()
|
2023-03-24 21:25:48 +00:00
|
|
|
.UseWorkflows(workflows => workflows
|
|
|
|
|
.WithStandardOutStreamProvider(_ => new StandardOutStreamProvider(new XunitConsoleTextWriter(_testOutputHelper)))
|
|
|
|
|
);
|
2022-04-12 15:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
2023-04-17 13:58:07 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Builds the <see cref="IServiceProvider"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>The <see cref="IServiceProvider"/>.</returns>
|
2023-03-24 21:25:48 +00:00
|
|
|
public IServiceProvider Build()
|
|
|
|
|
{
|
|
|
|
|
_services.AddElsa(_configureElsa);
|
|
|
|
|
return _services.BuildServiceProvider();
|
|
|
|
|
}
|
2022-04-12 13:55:35 +00:00
|
|
|
|
2023-04-17 13:58:07 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Configures Elsa.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="configure">The configuration delegate.</param>
|
|
|
|
|
/// <returns>The <see cref="TestApplicationBuilder"/>.</returns>
|
2023-03-24 21:25:48 +00:00
|
|
|
public TestApplicationBuilder ConfigureElsa(Action<IModule> configure)
|
|
|
|
|
{
|
|
|
|
|
_configureElsa += configure;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2023-09-14 14:30:18 +00:00
|
|
|
|
2023-04-17 13:58:07 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Configures the service provider.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="configure">The configuration delegate.</param>
|
|
|
|
|
/// <returns>The <see cref="TestApplicationBuilder"/>.</returns>
|
2023-03-24 21:25:48 +00:00
|
|
|
public TestApplicationBuilder ConfigureServices(Action<IServiceCollection> configure)
|
2022-04-12 15:08:31 +00:00
|
|
|
{
|
|
|
|
|
configure(_services);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2022-04-12 13:55:35 +00:00
|
|
|
|
2023-04-17 13:58:07 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Configures the service provider to use a <see cref="CapturingTextWriter"/> to capture standard output.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="capturingTextWriter">The capturing text writer.</param>
|
|
|
|
|
/// <returns>The <see cref="TestApplicationBuilder"/>.</returns>
|
2022-04-18 13:48:21 +00:00
|
|
|
public TestApplicationBuilder WithCapturingTextWriter(CapturingTextWriter capturingTextWriter)
|
2022-04-12 15:08:31 +00:00
|
|
|
{
|
|
|
|
|
var combinedTextWriter = new CombinedTextWriter(capturingTextWriter, new XunitConsoleTextWriter(_testOutputHelper));
|
2023-03-24 21:25:48 +00:00
|
|
|
var provider = new StandardOutStreamProvider(combinedTextWriter);
|
|
|
|
|
|
|
|
|
|
ConfigureElsa(elsa => elsa.UseWorkflows(workflows => workflows.WithStandardOutStreamProvider(_ => provider)));
|
2022-04-12 15:08:31 +00:00
|
|
|
return this;
|
2022-04-12 13:55:35 +00:00
|
|
|
}
|
2023-09-14 08:50:02 +00:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Adds a workflow to the service provider.
|
|
|
|
|
/// </summary>
|
2023-09-14 14:30:18 +00:00
|
|
|
public TestApplicationBuilder AddWorkflow<T>() where T : IWorkflow
|
2023-09-14 08:50:02 +00:00
|
|
|
{
|
|
|
|
|
return ConfigureElsa(elsa => elsa.AddWorkflow<T>());
|
|
|
|
|
}
|
2023-09-14 14:30:18 +00:00
|
|
|
|
2023-09-22 16:45:18 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Adds activities from the assembly containing the specified type.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public TestApplicationBuilder AddActivitiesFrom<T>()
|
|
|
|
|
{
|
|
|
|
|
return ConfigureElsa(elsa => elsa.AddActivitiesFrom<T>());
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-14 14:30:18 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Add workflows from the specified relative directory.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="directory">The path segments of the directory.</param>
|
|
|
|
|
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;
|
|
|
|
|
}
|
2022-04-12 13:55:35 +00:00
|
|
|
}
|