| | | 1 | | using Elsa.Abstractions; |
| | | 2 | | using Elsa.Workflows.Management; |
| | | 3 | | using Elsa.Workflows.Models; |
| | | 4 | | using Elsa.Workflows.Runtime; |
| | | 5 | | using Elsa.Workflows.State; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Workflows.Api.Endpoints.Tests.Activities; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Represents an endpoint for testing activities in workflows. This endpoint is responsible for handling requests |
| | | 11 | | /// that test the execution of a specific activity in a workflow and returning the results. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <remarks> |
| | | 14 | | /// This endpoint is used to perform operations such as: |
| | | 15 | | /// - Finding a workflow graph based on a provided workflow definition handle. |
| | | 16 | | /// - Locating and executing a specific activity within the workflow graph. |
| | | 17 | | /// - Capturing the execution results and returning them as a response. |
| | | 18 | | /// </remarks> |
| | | 19 | | internal class Endpoint( |
| | | 20 | | IWorkflowDefinitionService workflowDefinitionService, |
| | | 21 | | IActivityTestRunner activityTestRunner, |
| | | 22 | | IActivityExecutionMapper activityExecutionMapper) |
| | | 23 | | : ElsaEndpoint<Request> |
| | | 24 | | { |
| | | 25 | | /// <inheritdoc /> |
| | | 26 | | public override void Configure() |
| | | 27 | | { |
| | | 28 | | Post("/tests/activities"); |
| | | 29 | | ConfigurePermissions("exec:tests"); |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <inheritdoc /> |
| | | 33 | | public override async Task HandleAsync(Request request, CancellationToken cancellationToken) |
| | | 34 | | { |
| | | 35 | | var workflowGraph = await workflowDefinitionService.FindWorkflowGraphAsync(request.WorkflowDefinitionHandle, can |
| | | 36 | | |
| | | 37 | | if (workflowGraph == null) |
| | | 38 | | { |
| | | 39 | | AddError("Workflow definition not found."); |
| | | 40 | | await Send.ErrorsAsync(cancellation: cancellationToken); |
| | | 41 | | return; |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | var activity = workflowGraph.FindActivity(request.ActivityHandle); |
| | | 45 | | |
| | | 46 | | if (activity == null) |
| | | 47 | | { |
| | | 48 | | AddError("Activity not found."); |
| | | 49 | | await Send.ErrorsAsync(cancellation: cancellationToken); |
| | | 50 | | return; |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | var activityExecutionContext = await activityTestRunner.RunAsync(workflowGraph, activity, cancellationToken); |
| | | 54 | | var record = await activityExecutionMapper.MapAsync(activityExecutionContext); |
| | | 55 | | var activityState = record.ActivityState ?? new Dictionary<string, object?>(); |
| | | 56 | | |
| | | 57 | | var response = new Response |
| | | 58 | | { |
| | | 59 | | ActivityState = activityState, |
| | | 60 | | Outputs = record.Outputs, |
| | | 61 | | Payload = record.Payload, |
| | | 62 | | Exception = record.Exception, |
| | | 63 | | Status = record.Status |
| | | 64 | | }; |
| | | 65 | | |
| | | 66 | | await Send.OkAsync(response, cancellationToken); |
| | | 67 | | } |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | public class Request |
| | | 71 | | { |
| | 0 | 72 | | public WorkflowDefinitionHandle WorkflowDefinitionHandle { get; set; } = null!; |
| | 0 | 73 | | public ActivityHandle ActivityHandle { get; set; } = null!; |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | public class Response |
| | | 77 | | { |
| | | 78 | | public IDictionary<string, object?> ActivityState { get; set; } = null!; |
| | | 79 | | public IDictionary<string, object?>? Outputs { get; set; } |
| | | 80 | | public IDictionary<string, object>? Payload { get; set; } |
| | | 81 | | public ExceptionState? Exception { get; set; } |
| | | 82 | | public ActivityStatus Status { get; set; } |
| | | 83 | | } |