< Summary

Information
Class: Elsa.Workflows.ActivityTestRunner
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Services/ActivityTestRunner.cs
Line coverage
12%
Covered lines: 4
Uncovered lines: 28
Coverable lines: 32
Total lines: 66
Line coverage: 12.5%
Branch coverage
0%
Covered branches: 0
Total branches: 14
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
RunAsync()0%2040%
GetVariableTestValues(...)0%110100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Services/ActivityTestRunner.cs

#LineLine coverage
 1using System.Dynamic;
 2using System.Text.Json;
 3using Elsa.Expressions.Helpers;
 4using Elsa.Extensions;
 5using Elsa.Workflows.Models;
 6
 7namespace Elsa.Workflows;
 8
 9/// <inheritdoc />
 110public class ActivityTestRunner(
 111    IServiceProvider serviceProvider,
 112    IWorkflowExecutionPipeline pipeline,
 113    IIdentityGenerator identityGenerator)
 14    : IActivityTestRunner
 15{
 16    /// <inheritdoc />
 17    public async Task<ActivityExecutionContext> RunAsync(WorkflowGraph workflowGraph, IActivity activity, CancellationTo
 18    {
 019        var id = identityGenerator.GenerateId();
 020        var workflowExecutionContext = await WorkflowExecutionContext.CreateAsync(serviceProvider, workflowGraph, id, ca
 021        var variableTestValues = GetVariableTestValues(workflowGraph);
 22
 023        foreach (var variable in workflowGraph.Workflow.Variables)
 24        {
 025            var variableValue = variableTestValues.TryGetValue(variable.Id, out var value) ? value : variable.Value;
 026            variable.Set(workflowExecutionContext.ExpressionExecutionContext!, variableValue);
 27        }
 28
 029        workflowExecutionContext.ScheduleActivity(activity);
 030        workflowExecutionContext.TransitionTo(WorkflowSubStatus.Executing);
 31
 032        await pipeline.ExecuteAsync(workflowExecutionContext);
 033        var activityExecutionContext = workflowExecutionContext
 034            .ActivityExecutionContexts
 035            .First(x => x.Activity == activity);
 036        return activityExecutionContext;
 037    }
 38
 39    private IDictionary<string, object?> GetVariableTestValues(WorkflowGraph workflowGraph)
 40    {
 041        var variableTestValues = workflowGraph.Workflow.CustomProperties.TryGetValue("VariableTestValues", out var varia
 42
 043        if(variableTestValues is null)
 044            return new Dictionary<string, object?>();
 45
 046        if (variableTestValues is JsonElement jsonElement)
 047            variableTestValues = JsonSerializer.Deserialize<ExpandoObject>(jsonElement.GetRawText())!;
 48
 049        var expandoObject = (ExpandoObject)variableTestValues;
 050        var parsedValues = new Dictionary<string, object?>();
 51
 052        foreach (var (key, value) in expandoObject)
 53        {
 054            var variableDefinition = workflowGraph.Workflow.Variables.FirstOrDefault(x => x.Id == key);
 55
 056            if (variableDefinition == null)
 57                continue;
 58
 059            var variableType = variableDefinition.GetVariableType();
 060            var variableValue = value.ConvertTo(variableType);
 061            parsedValues[key] = variableValue;
 62        }
 63
 064        return parsedValues;
 65    }
 66}