< Summary

Information
Class: Elsa.ExternalAuthentication.Services.AdapterSettingsMigrationService
Assembly: Elsa.ExternalAuthentication
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.ExternalAuthentication/Services/AdapterSettingsMigrationService.cs
Line coverage
85%
Covered lines: 30
Uncovered lines: 5
Coverable lines: 35
Total lines: 66
Line coverage: 85.7%
Branch coverage
73%
Covered branches: 19
Total branches: 26
Branch coverage: 73%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
MigrateAsync()77.77%191886.95%
BuildMigrationIndex(...)62.5%9871.42%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.ExternalAuthentication/Services/AdapterSettingsMigrationService.cs

#LineLine coverage
 1using System.Text.Json;
 2using Elsa.ExternalAuthentication.Contracts;
 3using Elsa.ExternalAuthentication.Models;
 4
 5namespace Elsa.ExternalAuthentication.Services;
 6
 7/// <summary>
 8/// Orchestrates adapter-owned, forward-only settings migrations while preserving
 9/// the protocol-neutral connection envelope.
 10/// </summary>
 211public sealed class AdapterSettingsMigrationService(
 212    IExternalAuthenticationAdapterRegistry adapters,
 213    IEnumerable<IAdapterSettingsMigration> migrations) : IAdapterSettingsMigrationService
 14{
 215    private readonly IReadOnlyDictionary<(string AdapterType, int FromVersion), IAdapterSettingsMigration> _migrations =
 216        BuildMigrationIndex(migrations);
 17
 18    public async ValueTask<AdapterSettingsMigrationResult> MigrateAsync(
 19        string adapterType,
 20        int settingsVersion,
 21        JsonElement settings,
 22        CancellationToken cancellationToken = default)
 23    {
 524        cancellationToken.ThrowIfCancellationRequested();
 525        if (!adapters.TryGet(adapterType, out var adapter))
 126            throw new InvalidOperationException($"The adapter type '{adapterType}' is not installed or deployment-allowe
 27
 428        var currentVersion = adapter.Describe().SettingsVersion;
 429        if (settingsVersion <= 0 || settingsVersion > currentVersion)
 130            throw new InvalidOperationException($"Settings version {settingsVersion} is not compatible with adapter '{ad
 331        if (settingsVersion == currentVersion)
 132            return new AdapterSettingsMigrationResult(currentVersion, settings.Clone(), false);
 33
 234        var migrated = settings.Clone();
 235        var version = settingsVersion;
 236        var stepCount = 0;
 537        while (version < currentVersion)
 38        {
 339            if (!_migrations.TryGetValue((adapterType, version), out var migration))
 040                throw new InvalidOperationException($"Adapter '{adapterType}' does not provide a settings migration from
 341            if (migration.ToVersion <= version || migration.ToVersion > currentVersion)
 042                throw new InvalidOperationException($"Adapter '{adapterType}' has an invalid settings migration from ver
 343            if (++stepCount > 64)
 044                throw new InvalidOperationException($"Adapter '{adapterType}' settings migration contains a cycle.");
 45
 346            migrated = (await migration.MigrateAsync(migrated, cancellationToken)).Clone();
 347            version = migration.ToVersion;
 348        }
 49
 250        return new AdapterSettingsMigrationResult(version, migrated, true);
 351    }
 52
 53    private static IReadOnlyDictionary<(string AdapterType, int FromVersion), IAdapterSettingsMigration> BuildMigrationI
 54        IEnumerable<IAdapterSettingsMigration> migrations)
 55    {
 256        var result = new Dictionary<(string AdapterType, int FromVersion), IAdapterSettingsMigration>();
 1057        foreach (var migration in migrations)
 58        {
 359            if (string.IsNullOrWhiteSpace(migration.AdapterType) || migration.FromVersion <= 0)
 060                throw new InvalidOperationException("Adapter settings migrations must define an adapter type and a posit
 361            if (!result.TryAdd((migration.AdapterType, migration.FromVersion), migration))
 062                throw new InvalidOperationException($"Adapter '{migration.AdapterType}' registers more than one migratio
 63        }
 264        return result;
 65    }
 66}