< Summary

Information
Class: Elsa.Workflows.Management.Services.WorkflowDefinitionImporter
Assembly: Elsa.Workflows.Management
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Management/Services/WorkflowDefinitionImporter.cs
Line coverage
97%
Covered lines: 40
Uncovered lines: 1
Coverable lines: 41
Total lines: 83
Line coverage: 97.5%
Branch coverage
78%
Covered branches: 22
Total branches: 28
Branch coverage: 78.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
ImportAsync()78.57%282896.96%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Management/Services/WorkflowDefinitionImporter.cs

#LineLine coverage
 1using Elsa.Common.Models;
 2using Elsa.Workflows.Activities;
 3using Elsa.Workflows.Management.Mappers;
 4using Elsa.Workflows.Management.Materializers;
 5using Elsa.Workflows.Management.Models;
 6using Elsa.Workflows.Models;
 7
 8namespace Elsa.Workflows.Management.Services
 9{
 10    /// <inheritdoc />
 11    public class WorkflowDefinitionImporter : IWorkflowDefinitionImporter
 12    {
 13        private readonly IActivitySerializer _serializer;
 14        private readonly IWorkflowDefinitionPublisher _workflowDefinitionPublisher;
 15        private readonly VariableDefinitionMapper _variableDefinitionMapper;
 16
 17        /// <summary>
 18        /// Initializes a new instance of the <see cref="WorkflowDefinitionImporter"/> class.
 19        /// </summary>
 520        public WorkflowDefinitionImporter(
 521            IActivitySerializer serializer,
 522            IWorkflowDefinitionPublisher workflowDefinitionPublisher,
 523            VariableDefinitionMapper variableDefinitionMapper)
 24        {
 525            _serializer = serializer;
 526            _workflowDefinitionPublisher = workflowDefinitionPublisher;
 527            _variableDefinitionMapper = variableDefinitionMapper;
 528        }
 29
 30        /// <inheritdoc />
 31        public async Task<ImportWorkflowResult> ImportAsync(SaveWorkflowDefinitionRequest request, CancellationToken can
 32        {
 433            var model = request.Model;
 434            var definitionId = model.DefinitionId;
 35
 36            // Get a workflow draft version.
 437            var draft = !string.IsNullOrWhiteSpace(definitionId)
 438                ? await _workflowDefinitionPublisher.GetDraftAsync(definitionId, VersionOptions.Latest, cancellationToke
 439                : null;
 40
 441            var isNew = draft == null;
 42
 43            // Create a new workflow in case no existing definition was found.
 444            if (isNew)
 45            {
 446                draft = await _workflowDefinitionPublisher.NewAsync(cancellationToken: cancellationToken);
 47
 448                if (!string.IsNullOrWhiteSpace(definitionId))
 449                    draft.DefinitionId = definitionId;
 50
 451                if (!string.IsNullOrWhiteSpace(model.Id))
 052                    draft.Id = model.Id;
 53            }
 54
 55            // Update the draft with the received model.
 456            var root = model.Root ?? new Sequence();
 457            var stringData = _serializer.Serialize(root);
 458            var variables = _variableDefinitionMapper.Map(model.Variables).ToList();
 59
 460            draft!.StringData = stringData;
 461            draft.MaterializerName = JsonWorkflowMaterializer.MaterializerName;
 462            draft.Name = model.Name?.Trim();
 463            draft.Description = model.Description?.Trim();
 464            draft.CustomProperties = model.CustomProperties ?? new Dictionary<string, object>();
 465            draft.Variables = variables;
 466            draft.Inputs = model.Inputs ?? new List<InputDefinition>();
 467            draft.Outputs = model.Outputs ?? new List<OutputDefinition>();
 468            draft.Outcomes = model.Outcomes ?? new List<string>();
 469            draft.IsReadonly = model.IsReadonly;
 470            draft.Options = model.Options ?? new WorkflowOptions();
 471            draft.ToolVersion = model.ToolVersion;
 72
 473            if (request.Publish ?? model.IsPublished)
 74            {
 275                var result = await _workflowDefinitionPublisher.PublishAsync(draft, cancellationToken);
 276                return new ImportWorkflowResult(result.Succeeded, draft, result.ValidationErrors);
 77            }
 78
 279            await _workflowDefinitionPublisher.SaveDraftAsync(draft, cancellationToken);
 280            return new ImportWorkflowResult(true, draft, new List<WorkflowValidationError>());
 481        }
 82    }
 83}