elsa-core/src/common/Elsa.Testing.Shared.Integration/ServiceProviderExtensions.cs
Sipke Schoorstra fbe9ca64f0
Fix Switch activity behavior and add testing utilities for RunWorkflowResult and improve (#7151)
* Add testing utilities for `RunWorkflowResult` and improve `Switch` activity behavior

- Introduced `RunWorkflowResultAssertions` for xUnit-based workflow testing.
- Added `RunWorkflowResultExtensions` for journal-based activity execution context handling.
- Refactored `Switch` activity to track scheduled activities and correctly complete execution when all scheduled activities finish.
- Marked `RunActivityExtensions` methods as obsolete, recommending the use of `WorkflowTestFixture`.

* Update src/common/Elsa.Testing.Shared.Integration/RunActivityExtensions.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update src/modules/Elsa.Workflows.Core/Activities/Switch.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Add XML documentation to Switch activity callback method (#7152)

* Initial plan

* Add XML documentation to OnChildActivityCompletedAsync method

Co-authored-by: sfmskywalker <938393+sfmskywalker@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: sfmskywalker <938393+sfmskywalker@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
Co-authored-by: sfmskywalker <938393+sfmskywalker@users.noreply.github.com>
2025-12-19 17:05:12 +01:00

66 lines
2.7 KiB
C#

using Elsa.Common.Models;
using Elsa.Workflows;
using Elsa.Workflows.Management;
using Elsa.Workflows.Management.Entities;
using Elsa.Workflows.Management.Models;
using Elsa.Workflows.Runtime;
using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection;
namespace Elsa.Testing.Shared;
/// <summary>
/// Provides extension methods for <see cref="IServiceProvider"/>.
/// </summary>
[PublicAPI]
public static class ServiceProviderExtensions
{
/// <param name="services">The services.</param>
extension(IServiceProvider services)
{
/// <summary>
/// Updates the registries.
/// </summary>
public Task PopulateRegistriesAsync()
{
var registriesPopulator = services.GetRequiredService<IRegistriesPopulator>();
return registriesPopulator.PopulateAsync();
}
/// <summary>
/// Imports a workflow definition from a file.
/// </summary>
/// <param name="fileName">The file name.</param>
/// <returns>The workflow definition.</returns>
public async Task<WorkflowDefinition> ImportWorkflowDefinitionAsync(string fileName)
{
var json = await File.ReadAllTextAsync(fileName);
var serializer = services.GetRequiredService<IActivitySerializer>();
var model = serializer.Deserialize<WorkflowDefinitionModel>(json);
var workflowDefinitionRequest = new SaveWorkflowDefinitionRequest
{
Model = model,
Publish = true
};
var workflowDefinitionImporter = services.GetRequiredService<IWorkflowDefinitionImporter>();
var result = await workflowDefinitionImporter.ImportAsync(workflowDefinitionRequest);
return result.WorkflowDefinition;
}
/// <summary>
/// Retrieves a workflow definition by its ID.
/// </summary>
/// <param name="workflowDefinitionId">The definition ID of the workflow definition.</param>
/// <param name="versionOptions">Options to specify the version of the workflow definition to retrieve.</param>
/// <param name="cancellationToken">A cancellation token to cancel the operation.</param>
/// <returns>The retrieved workflow definition.</returns>
public async Task<WorkflowDefinition> GetWorkflowDefinitionAsync(string workflowDefinitionId, VersionOptions versionOptions, CancellationToken cancellationToken = default)
{
var workflowDefinitionService = services.GetRequiredService<IWorkflowDefinitionService>();
var workflowDefinition = await workflowDefinitionService.FindWorkflowDefinitionAsync(workflowDefinitionId, versionOptions, cancellationToken);
return workflowDefinition!;
}
}
}