Skip to content

Commit

Permalink
Add dependency workflows publishing tests
Browse files Browse the repository at this point in the history
This commit introduces a new integration test for the "DependencyWorkflowsPublishing" scenario. It also adds two new JSON files, "child.json" and "parent.json" to simulate the child and parent workflows accordingly. Updates are also made to the 'Elsa.IntegrationTests.csproj' and 'ServiceProviderExtensions.cs' as a part of testing setup.
  • Loading branch information
sfmskywalker committed Feb 7, 2024
1 parent cfac318 commit d878314
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/common/Elsa.Testing.Shared/ServiceProviderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,19 @@ public static async Task<RunWorkflowResult> RunActivityAsync(this IServiceProvid
var result = await workflowRunner.RunAsync(activity, options, cancellationToken);
return result;
}

/// <summary>
/// Retrieves a workflow definition by its ID.
/// </summary>
/// <param name="services">The service provider.</param>
/// <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 static async Task<WorkflowDefinition> GetWorkflowDefinitionAsync(this IServiceProvider services, string workflowDefinitionId, VersionOptions versionOptions, CancellationToken cancellationToken = default)
{
var workflowDefinitionService = services.GetRequiredService<IWorkflowDefinitionService>();
var workflowDefinition = await workflowDefinitionService.FindAsync(workflowDefinitionId, versionOptions);
return workflowDefinition!;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,11 @@
<ItemGroup>
<None Update="Serialization\**\*.json" CopyToOutputDirectory="PreserveNewest" />
<None Update="Scenarios\**\Workflows\*.json" CopyToOutputDirectory="PreserveNewest" />
<None Update="Scenarios\DependencyWorkflowsPublishing\Workflows\child.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Scenarios\DependencyWorkflowsPublishing\Workflows\parent.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using Elsa.Common.Models;
using Elsa.Extensions;
using Elsa.Testing.Shared;
using Elsa.Workflows.Contracts;
using Elsa.Workflows.Management.Activities.WorkflowDefinitionActivity;
using Elsa.Workflows.Management.Contracts;
using Elsa.Workflows.Management.Entities;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
using Xunit.Abstractions;

namespace Elsa.IntegrationTests.Scenarios.DependencyWorkflowsPublishing;

/// <summary>
/// Contains tests for the "DependencyWorkflowsPublishing" scenario.
/// </summary>
public class Tests
{
private readonly CapturingTextWriter _capturingTextWriter = new();
private readonly IServiceProvider _services;
private readonly IWorkflowDefinitionPublisher _workflowDefinitionPublisher;
private readonly IActivitySerializer _activitySerializer;
private readonly IActivityVisitor _activityVisitor;
private readonly IWorkflowDefinitionManager _workflowManager;

/// <summary>
/// Initializes a new instance of the <see cref="Tests"/> class.
/// </summary>
public Tests(ITestOutputHelper testOutputHelper)
{
_services = new TestApplicationBuilder(testOutputHelper)
.WithCapturingTextWriter(_capturingTextWriter)
.WithWorkflowsFromDirectory("Scenarios", "DependencyWorkflowsPublishing", "Workflows")
.Build();

_workflowDefinitionPublisher = _services.GetRequiredService<IWorkflowDefinitionPublisher>();
_workflowManager = _services.GetRequiredService<IWorkflowDefinitionManager>();
_activitySerializer = _services.GetRequiredService<IActivitySerializer>();
_activityVisitor = _services.GetRequiredService<IActivityVisitor>();
}

/// <summary>
/// When a dependency workflow is published, all consuming workflows are updated to point to the new version of the dependency.
/// </summary>
[Fact(DisplayName = "When a dependency workflow is published, all consuming workflows are updated to point to the new version of the dependency.")]
public async Task Test1()
{
await _services.PopulateRegistriesAsync();
var childDefinitionV1 = await _services.GetWorkflowDefinitionAsync("child", VersionOptions.Latest);
var parentDefinition = await _services.GetWorkflowDefinitionAsync("parent", VersionOptions.Latest);
var childActivityV1 = await GetChildActivityAsync(parentDefinition);

// Assert initial state.
Assert.Equal(1, childDefinitionV1.Version);
Assert.False(parentDefinition.IsPublished);
Assert.Equal(1, childActivityV1.Version);

// Create a new draft for the child workflow and publish it.
var childDefinitionV2 = (await _workflowDefinitionPublisher.GetDraftAsync(childDefinitionV1.DefinitionId, VersionOptions.Published))!;
await _workflowDefinitionPublisher.PublishAsync(childDefinitionV2);

// Update consuming workflows to point to the new version of the child workflow.
await _workflowManager.UpdateReferencesInConsumingWorkflows(childDefinitionV2);

// Assert that the parent workflow now points to the new version of the child workflow.
parentDefinition = await _services.GetWorkflowDefinitionAsync("parent", VersionOptions.Latest);
var childActivityV2 = await GetChildActivityAsync(parentDefinition);
Assert.Equal(2, childActivityV2.Version);
}

private async Task<WorkflowDefinitionActivity> GetChildActivityAsync(WorkflowDefinition parent)
{
var root = _activitySerializer.Deserialize(parent.StringData!);
var graph = await _activityVisitor.VisitAsync(root);
var flattenedList = graph.Flatten().ToList();
return (WorkflowDefinitionActivity)flattenedList.Single(x => x.Activity is WorkflowDefinitionActivity { WorkflowDefinitionId: "child" }).Activity;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"id": "child-v1",
"definitionId": "child",
"name": "Child",
"createdAt": "2024-02-07T14:57:47.115341+00:00",
"version": 1,
"toolVersion": "3.0.0.0",
"variables": [],
"inputs": [],
"outputs": [],
"outcomes": [],
"customProperties": {
"Elsa:WorkflowContextProviderTypes": []
},
"isReadonly": false,
"isLatest": true,
"isPublished": true,
"options": {
"usableAsActivity": true,
"autoUpdateConsumingWorkflows": true
},
"root": {
"type": "Elsa.Flowchart",
"version": 1,
"id": "c22918340b6cb7cd",
"nodeId": "Workflow1:c22918340b6cb7cd",
"metadata": {},
"customProperties": {
"source": "FlowchartJsonConverter.cs:45",
"notFoundConnections": [],
"canStartWorkflow": false,
"runAsynchronously": false
},
"activities": [],
"connections": []
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"id": "ffd312ccf7a2af4f",
"definitionId": "parent",
"name": "Parent",
"createdAt": "2024-02-07T14:58:12.954939+00:00",
"version": 1,
"toolVersion": "3.0.0.0",
"variables": [],
"inputs": [],
"outputs": [],
"outcomes": [],
"customProperties": {
"Elsa:WorkflowContextProviderTypes": []
},
"isReadonly": false,
"isLatest": true,
"isPublished": false,
"options": {
"autoUpdateConsumingWorkflows": false
},
"root": {
"type": "Elsa.Flowchart",
"version": 1,
"id": "3535b9598a0962f0",
"nodeId": "Workflow1:3535b9598a0962f0",
"metadata": {},
"customProperties": {
"source": "FlowchartJsonConverter.cs:45",
"notFoundConnections": [],
"canStartWorkflow": false,
"runAsynchronously": false
},
"activities": [
{
"workflowDefinitionId": "child",
"workflowDefinitionVersionId": "child-v1",
"latestAvailablePublishedVersion": 1,
"latestAvailablePublishedVersionId": "child-v1",
"id": "35525d3b78b8e518",
"nodeId": "Workflow1:3535b9598a0962f0:35525d3b78b8e518",
"name": "Child1",
"type": "Child",
"version": 1,
"customProperties": {
"canStartWorkflow": false,
"runAsynchronously": false
},
"metadata": {
"designer": {
"position": {
"x": -95.17782592773438,
"y": -137.3333282470703
},
"size": {
"width": 67.78125,
"height": 50
}
}
}
}
],
"connections": []
}
}

0 comments on commit d878314

Please sign in to comment.