| | | 1 | | using System.Dynamic; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using Elsa.Expressions.Helpers; |
| | | 4 | | using Elsa.Extensions; |
| | | 5 | | using Elsa.Workflows.Models; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Workflows; |
| | | 8 | | |
| | | 9 | | /// <inheritdoc /> |
| | 1 | 10 | | public class ActivityTestRunner( |
| | 1 | 11 | | IServiceProvider serviceProvider, |
| | 1 | 12 | | IWorkflowExecutionPipeline pipeline, |
| | 1 | 13 | | IIdentityGenerator identityGenerator) |
| | | 14 | | : IActivityTestRunner |
| | | 15 | | { |
| | | 16 | | /// <inheritdoc /> |
| | | 17 | | public async Task<ActivityExecutionContext> RunAsync(WorkflowGraph workflowGraph, IActivity activity, CancellationTo |
| | | 18 | | { |
| | 0 | 19 | | var id = identityGenerator.GenerateId(); |
| | 0 | 20 | | var workflowExecutionContext = await WorkflowExecutionContext.CreateAsync(serviceProvider, workflowGraph, id, ca |
| | 0 | 21 | | var variableTestValues = GetVariableTestValues(workflowGraph); |
| | | 22 | | |
| | 0 | 23 | | foreach (var variable in workflowGraph.Workflow.Variables) |
| | | 24 | | { |
| | 0 | 25 | | var variableValue = variableTestValues.TryGetValue(variable.Id, out var value) ? value : variable.Value; |
| | 0 | 26 | | variable.Set(workflowExecutionContext.ExpressionExecutionContext!, variableValue); |
| | | 27 | | } |
| | | 28 | | |
| | 0 | 29 | | workflowExecutionContext.ScheduleActivity(activity); |
| | 0 | 30 | | workflowExecutionContext.TransitionTo(WorkflowSubStatus.Executing); |
| | | 31 | | |
| | 0 | 32 | | await pipeline.ExecuteAsync(workflowExecutionContext); |
| | 0 | 33 | | var activityExecutionContext = workflowExecutionContext |
| | 0 | 34 | | .ActivityExecutionContexts |
| | 0 | 35 | | .First(x => x.Activity == activity); |
| | 0 | 36 | | return activityExecutionContext; |
| | 0 | 37 | | } |
| | | 38 | | |
| | | 39 | | private IDictionary<string, object?> GetVariableTestValues(WorkflowGraph workflowGraph) |
| | | 40 | | { |
| | 0 | 41 | | var variableTestValues = workflowGraph.Workflow.CustomProperties.TryGetValue("VariableTestValues", out var varia |
| | | 42 | | |
| | 0 | 43 | | if(variableTestValues is null) |
| | 0 | 44 | | return new Dictionary<string, object?>(); |
| | | 45 | | |
| | 0 | 46 | | if (variableTestValues is JsonElement jsonElement) |
| | 0 | 47 | | variableTestValues = JsonSerializer.Deserialize<ExpandoObject>(jsonElement.GetRawText())!; |
| | | 48 | | |
| | 0 | 49 | | var expandoObject = (ExpandoObject)variableTestValues; |
| | 0 | 50 | | var parsedValues = new Dictionary<string, object?>(); |
| | | 51 | | |
| | 0 | 52 | | foreach (var (key, value) in expandoObject) |
| | | 53 | | { |
| | 0 | 54 | | var variableDefinition = workflowGraph.Workflow.Variables.FirstOrDefault(x => x.Id == key); |
| | | 55 | | |
| | 0 | 56 | | if (variableDefinition == null) |
| | | 57 | | continue; |
| | | 58 | | |
| | 0 | 59 | | var variableType = variableDefinition.GetVariableType(); |
| | 0 | 60 | | var variableValue = value.ConvertTo(variableType); |
| | 0 | 61 | | parsedValues[key] = variableValue; |
| | | 62 | | } |
| | | 63 | | |
| | 0 | 64 | | return parsedValues; |
| | | 65 | | } |
| | | 66 | | } |