* Remove unused using directives in test classes The using directives for System, System.Collections.Generic, System.IO, and others were not necessary in many of the test classes. These unused directives have been removed to enhance code readability and maintainability. * Update source port retrieval in ConnectionJsonConverter The source port retrieval code in ConnectionJsonConverter.cs has been updated. It now uses TryGetProperty instead of GetProperty, enabling it to handle cases where the "port" property may not exist. This enhances error handling and resilience in the activities module. * Refactor InputJsonConverter for proper variable expressions handling The InputJsonConverter in the Elsa.Workflows.Serialization has been expanded for efficient handling of variable expressions during the JSON conversion process. The refactor ensures appropriate extraction and assignment of values to variables. This adds robustness to the serialization process, maintaining variable types after serialization. * Add tests for variable expressions serialization A new `Tests.cs` file has been added under the `Elsa.IntegrationTests/Serialization/VariableExpressions` directory. This file contains tests for ensuring the serialization of variable expressions works correctly. A corresponding `SampleWorkflow` file has also been established, providing the workflows to be used in the tests. * Refactor workflow builder extension class The WorkflowDefinitionBuilderExtensions class has been deleted and replaced with WorkflowBuilderExtensions in Elsa.Workflows.Core. This new class retains similar functionality but includes more detailed comments and dynamic member access capabilities in its method definition.
90 lines
4 KiB
C#
90 lines
4 KiB
C#
using Elsa.Extensions;
|
|
using Elsa.IntegrationTests.Scenarios.BlockingAndBreaking.Workflows;
|
|
using Elsa.Testing.Shared;
|
|
using Elsa.Workflows.Contracts;
|
|
using Elsa.Workflows.Options;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Xunit;
|
|
using Xunit.Abstractions;
|
|
|
|
namespace Elsa.IntegrationTests.Scenarios.BlockingAndBreaking;
|
|
|
|
public class Tests
|
|
{
|
|
private readonly IWorkflowRunner _workflowRunner;
|
|
private readonly CapturingTextWriter _capturingTextWriter = new();
|
|
private readonly IWorkflowBuilderFactory _workflowBuilderFactory;
|
|
private readonly IServiceProvider _services;
|
|
|
|
public Tests(ITestOutputHelper testOutputHelper)
|
|
{
|
|
_services = new TestApplicationBuilder(testOutputHelper).WithCapturingTextWriter(_capturingTextWriter).Build();
|
|
_workflowBuilderFactory = _services.GetRequiredService<IWorkflowBuilderFactory>();
|
|
_workflowRunner = _services.GetRequiredService<IWorkflowRunner>();
|
|
}
|
|
|
|
[Fact(DisplayName = "Fork completes after all branches complete.")]
|
|
public async Task Test1()
|
|
{
|
|
await _services.PopulateRegistriesAsync();
|
|
var workflow = await _workflowBuilderFactory.CreateBuilder().BuildWorkflowAsync<WaitAllForkWorkflow>();
|
|
|
|
// Start workflow.
|
|
var result1 = await _workflowRunner.RunAsync(workflow);
|
|
|
|
// Resume the first branch.
|
|
var bookmark = result1.WorkflowState.Bookmarks.FirstOrDefault(x => x.ActivityId == "Branch 1");
|
|
var runOptions = new RunWorkflowOptions { BookmarkId = bookmark!.Id };
|
|
var result2 = await _workflowRunner.RunAsync(workflow, result1.WorkflowState, runOptions);
|
|
|
|
// Resume the second branch.
|
|
bookmark = result2.WorkflowState.Bookmarks.FirstOrDefault(x => x.ActivityId == "Branch 2");
|
|
runOptions = new RunWorkflowOptions { BookmarkId = bookmark!.Id };
|
|
var result3 = await _workflowRunner.RunAsync(workflow, result2.WorkflowState, runOptions);
|
|
|
|
// Verify expected output.
|
|
var lines = _capturingTextWriter.Lines.ToList();
|
|
Assert.Equal(new[] { "Start", "Branch 1", "Branch 2", "Branch 1 - Resumed", "Branch 2 - Resumed", "End" }, lines);
|
|
}
|
|
|
|
[Fact(DisplayName = "Fork completes after any branch completes.")]
|
|
public async Task Test2()
|
|
{
|
|
await _services.PopulateRegistriesAsync();
|
|
var workflow = await _workflowBuilderFactory.CreateBuilder().BuildWorkflowAsync<WaitAnyForkWorkflow>();
|
|
|
|
// Start workflow.
|
|
var result1 = await _workflowRunner.RunAsync(workflow);
|
|
|
|
// Resume the first branch.
|
|
var bookmark = result1.WorkflowState.Bookmarks.FirstOrDefault(x => x.ActivityId == "Branch 1");
|
|
var runOptions = new RunWorkflowOptions { BookmarkId = bookmark!.Id };
|
|
var result2 = await _workflowRunner.RunAsync(workflow, result1.WorkflowState, runOptions);
|
|
|
|
// Verify expected output.
|
|
var lines = _capturingTextWriter.Lines.ToList();
|
|
Assert.Equal(new[] { "Start", "Branch 1", "Branch 2", "Branch 1 - Resumed", "End" }, lines);
|
|
}
|
|
|
|
[Fact(DisplayName = "Break breaks out of While loop.")]
|
|
public async Task Test3()
|
|
{
|
|
await _services.PopulateRegistriesAsync();
|
|
var workflow = await _workflowBuilderFactory.CreateBuilder().BuildWorkflowAsync<BreakWhileFromForkWorkflow>();
|
|
|
|
// Start workflow.
|
|
var result1 = await _workflowRunner.RunAsync(workflow);
|
|
|
|
// Resume the first branch.
|
|
var bookmark = result1.WorkflowState.Bookmarks.FirstOrDefault(x => x.ActivityId == "Branch 1");
|
|
var runOptions = new RunWorkflowOptions { BookmarkId = bookmark!.Id };
|
|
var result2 = await _workflowRunner.RunAsync(workflow, result1.WorkflowState, runOptions);
|
|
|
|
// There should be no bookmarks left, since the while loop was broken out of.
|
|
Assert.Empty(result2.WorkflowState.Bookmarks);
|
|
|
|
// Verify expected output.
|
|
var lines = _capturingTextWriter.Lines.ToList();
|
|
Assert.Equal(new[] { "Start", "Branch 1", "Branch 2", "Branch 1 - Resumed", "End" }, lines);
|
|
}
|
|
} |