| | | 1 | | using Elsa.Common; |
| | | 2 | | using Elsa.Expressions.Contracts; |
| | | 3 | | using Elsa.Expressions.Services; |
| | | 4 | | using Elsa.Extensions; |
| | | 5 | | using Elsa.Mediator.Contracts; |
| | | 6 | | using Elsa.Workflows; |
| | | 7 | | using Elsa.Workflows.Activities; |
| | | 8 | | using Elsa.Workflows.CommitStates; |
| | | 9 | | using Elsa.Workflows.Management.Providers; |
| | | 10 | | using Elsa.Workflows.Management.Services; |
| | | 11 | | using Elsa.Workflows.Memory; |
| | | 12 | | using Elsa.Workflows.PortResolvers; |
| | | 13 | | using JetBrains.Annotations; |
| | | 14 | | using Microsoft.Extensions.DependencyInjection; |
| | | 15 | | using NSubstitute; |
| | | 16 | | |
| | | 17 | | namespace Elsa.Testing.Shared; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// A test fixture for unit testing activities in isolation. |
| | | 21 | | /// Provides a fluent API to configure services, variables, and execution context. |
| | | 22 | | /// </summary> |
| | | 23 | | public class ActivityTestFixture |
| | | 24 | | { |
| | | 25 | | private Action<ActivityExecutionContext>? _configureContextAction; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Initializes a new instance of the <see cref="ActivityTestFixture"/> class. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <param name="activity">The activity to test</param> |
| | 0 | 31 | | public ActivityTestFixture(IActivity activity) |
| | | 32 | | { |
| | 0 | 33 | | Activity = activity; |
| | 0 | 34 | | Services = new ServiceCollection(); |
| | 0 | 35 | | AddCoreWorkflowServices(Services); |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Represents the activity being tested within the context of the activity test fixture. |
| | | 40 | | /// Provides access to the activity for configuration, execution, and validation purposes. |
| | | 41 | | /// </summary> |
| | 0 | 42 | | public IActivity Activity { get; } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Gets the service collection for registering additional services. |
| | | 46 | | /// Use this to add services required by the activity under test. |
| | | 47 | | /// </summary> |
| | | 48 | | [UsedImplicitly] |
| | 0 | 49 | | public IServiceCollection Services { get; private set; } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Configures the service collection using a fluent action. |
| | | 53 | | /// </summary> |
| | | 54 | | /// <param name="configure">Action to configure the service collection</param> |
| | | 55 | | /// <returns>The fixture instance for method chaining</returns> |
| | | 56 | | public ActivityTestFixture ConfigureServices(Action<IServiceCollection> configure) |
| | | 57 | | { |
| | 0 | 58 | | configure(Services); |
| | 0 | 59 | | return this; |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Configures the activity execution context before execution. |
| | | 64 | | /// Multiple calls to this method will chain the configuration actions together. |
| | | 65 | | /// </summary> |
| | | 66 | | /// <param name="configure">Action to configure the activity execution context</param> |
| | | 67 | | /// <returns>The fixture instance for method chaining</returns> |
| | | 68 | | [UsedImplicitly] |
| | | 69 | | public ActivityTestFixture ConfigureContext(Action<ActivityExecutionContext> configure) |
| | | 70 | | { |
| | 0 | 71 | | _configureContextAction += configure; |
| | 0 | 72 | | return this; |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Executes the activity and returns the execution context. |
| | | 77 | | /// </summary> |
| | | 78 | | /// <returns>The ActivityExecutionContext after execution</returns> |
| | | 79 | | public async Task<ActivityExecutionContext> ExecuteAsync() |
| | | 80 | | { |
| | 0 | 81 | | var context = await BuildAsync(); |
| | 0 | 82 | | return await ExecuteAsync(context); |
| | 0 | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Executes the activity using a pre-built <see cref="ActivityExecutionContext"/>. |
| | | 87 | | /// Useful when you need to customize the context before execution, such as setting initial workflow state or overri |
| | | 88 | | /// </summary> |
| | | 89 | | /// <param name="context">The pre-built context to execute</param> |
| | | 90 | | /// <returns>The <see cref="ActivityExecutionContext"/> after execution</returns> |
| | | 91 | | public async Task<ActivityExecutionContext> ExecuteAsync(ActivityExecutionContext context) |
| | | 92 | | { |
| | | 93 | | // Set up variables and inputs, then execute the activity |
| | 0 | 94 | | await SetupExistingVariablesAsync(Activity, context); |
| | 0 | 95 | | await context.EvaluateInputPropertiesAsync(); |
| | 0 | 96 | | context.TransitionTo(ActivityStatus.Running); |
| | 0 | 97 | | await Activity.ExecuteAsync(context); |
| | | 98 | | |
| | 0 | 99 | | return context; |
| | 0 | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Builds the ActivityExecutionContext without executing the activity. |
| | | 104 | | /// </summary> |
| | | 105 | | public async Task<ActivityExecutionContext> BuildAsync() |
| | | 106 | | { |
| | 0 | 107 | | var serviceProvider = Services.BuildServiceProvider(); |
| | 0 | 108 | | var activityRegistry = serviceProvider.GetRequiredService<IActivityRegistry>(); |
| | 0 | 109 | | var workflowGraphBuilder = serviceProvider.GetRequiredService<IWorkflowGraphBuilder>(); |
| | | 110 | | |
| | 0 | 111 | | await activityRegistry.RegisterAsync(Activity.GetType()); |
| | | 112 | | |
| | 0 | 113 | | var workflow = Workflow.FromActivity(Activity); |
| | 0 | 114 | | var workflowGraph = await workflowGraphBuilder.BuildAsync(workflow); |
| | | 115 | | |
| | | 116 | | // Create workflow execution context using the static factory method |
| | 0 | 117 | | var workflowExecutionContext = await WorkflowExecutionContext.CreateAsync( |
| | 0 | 118 | | serviceProvider, |
| | 0 | 119 | | workflowGraph, |
| | 0 | 120 | | $"test-instance-{Guid.NewGuid()}", |
| | 0 | 121 | | CancellationToken.None |
| | 0 | 122 | | ); |
| | | 123 | | |
| | | 124 | | // Create ActivityExecutionContext for the actual activity we want to test |
| | 0 | 125 | | var context = await workflowExecutionContext.CreateActivityExecutionContextAsync(Activity); |
| | | 126 | | |
| | | 127 | | // Apply any context configuration action |
| | 0 | 128 | | _configureContextAction?.Invoke(context); |
| | | 129 | | |
| | 0 | 130 | | return context; |
| | 0 | 131 | | } |
| | | 132 | | |
| | | 133 | | /// <summary> |
| | | 134 | | /// Sets up existing variables found on the activity in the execution context. |
| | | 135 | | /// This is necessary because in unit tests, variables need to be initialized. |
| | | 136 | | /// </summary> |
| | | 137 | | private static Task SetupExistingVariablesAsync(IActivity activity, ActivityExecutionContext context) |
| | | 138 | | { |
| | 0 | 139 | | var activityType = activity.GetType(); |
| | 0 | 140 | | var variableProperties = activityType.GetProperties() |
| | 0 | 141 | | .Where(p => typeof(Variable).IsAssignableFrom(p.PropertyType)) |
| | 0 | 142 | | .ToList(); |
| | | 143 | | |
| | 0 | 144 | | foreach (var variable in variableProperties.Select(property => (Variable?)property.GetValue(activity))) |
| | | 145 | | { |
| | 0 | 146 | | if(variable == null) |
| | | 147 | | continue; |
| | | 148 | | |
| | 0 | 149 | | context.WorkflowExecutionContext.MemoryRegister.Declare(variable); |
| | 0 | 150 | | variable.Set(context.ExpressionExecutionContext, variable.Value); |
| | | 151 | | } |
| | | 152 | | |
| | 0 | 153 | | return Task.CompletedTask; |
| | | 154 | | } |
| | | 155 | | |
| | | 156 | | private static void AddCoreWorkflowServices(IServiceCollection services) |
| | | 157 | | { |
| | 0 | 158 | | services.AddLogging(); |
| | 0 | 159 | | services.AddSingleton<ISystemClock>(_ => Substitute.For<ISystemClock>()); |
| | 0 | 160 | | services.AddSingleton<INotificationSender>(_ => Substitute.For<INotificationSender>()); |
| | 0 | 161 | | services.AddSingleton<IActivityVisitor, ActivityVisitor>(); |
| | 0 | 162 | | services.AddScoped<IExpressionEvaluator, ExpressionEvaluator>(); |
| | 0 | 163 | | services.AddSingleton<IWellKnownTypeRegistry, WellKnownTypeRegistry>(); |
| | 0 | 164 | | services.AddSingleton<IActivityDescriber, ActivityDescriber>(); |
| | 0 | 165 | | services.AddSingleton<IPropertyDefaultValueResolver, PropertyDefaultValueResolver>(); |
| | 0 | 166 | | services.AddSingleton<IPropertyUIHandlerResolver, PropertyUIHandlerResolver>(); |
| | 0 | 167 | | services.AddSingleton<IActivityRegistry, ActivityRegistry>(); |
| | 0 | 168 | | services.AddScoped<IActivityRegistryLookupService, ActivityRegistryLookupService>(); |
| | 0 | 169 | | services.AddScoped<IIdentityGraphService, IdentityGraphService>(); |
| | 0 | 170 | | services.AddScoped<IWorkflowGraphBuilder, WorkflowGraphBuilder>(); |
| | 0 | 171 | | services.AddScoped<IActivityResolver, PropertyBasedActivityResolver>(); |
| | 0 | 172 | | services.AddScoped<IActivityResolver, SwitchActivityResolver>(); |
| | 0 | 173 | | services.AddScoped<DefaultActivityInputEvaluator>(); |
| | 0 | 174 | | services.AddSingleton<IExpressionDescriptorProvider, DefaultExpressionDescriptorProvider>(); |
| | 0 | 175 | | services.AddSingleton<IExpressionDescriptorRegistry, ExpressionDescriptorRegistry>(); |
| | 0 | 176 | | services.AddSingleton<IIdentityGenerator>(_ => Substitute.For<IIdentityGenerator>()); |
| | 0 | 177 | | services.AddSingleton<IHasher>(_ => Substitute.For<IHasher>()); |
| | 0 | 178 | | services.AddSingleton<IStimulusHasher, StimulusHasher>(); |
| | 0 | 179 | | services.AddSingleton<ICommitStateHandler>(_ => Substitute.For<ICommitStateHandler>()); |
| | 0 | 180 | | services.AddSingleton<IActivitySchedulerFactory, ActivitySchedulerFactory>(); |
| | 0 | 181 | | services.AddSingleton<IWorkflowExecutionContextSchedulerStrategy, FakeWorkflowExecutionContextSchedulerStrategy> |
| | 0 | 182 | | services.AddSingleton<IActivityExecutionContextSchedulerStrategy, FakeActivityExecutionContextSchedulerStrategy> |
| | 0 | 183 | | } |
| | | 184 | | } |