< Summary

Information
Class: AsiBackbone.Testing.Contracts.GovernanceDecisionPolicyContract<T>
Assembly: AsiBackbone.Testing
File(s): /home/runner/work/AsiBackbone/AsiBackbone/src/AsiBackbone.Testing/Contracts/GovernanceDecisionPolicyContract.cs
Line coverage
100%
Covered lines: 26
Uncovered lines: 0
Coverable lines: 26
Total lines: 87
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
CreateComposedDecision(...)100%11100%
CreateConstraintResults()100%11100%
VerifyDecisionPolicyReturnsSafeDecisionAsync()100%88100%

File(s)

/home/runner/work/AsiBackbone/AsiBackbone/src/AsiBackbone.Testing/Contracts/GovernanceDecisionPolicyContract.cs

#LineLine coverage
 1using AsiBackbone.Core.Constraints;
 2using AsiBackbone.Core.Decisions;
 3using AsiBackbone.Core.Evaluation;
 4
 5namespace AsiBackbone.Testing.Contracts;
 6
 7/// <summary>
 8/// Reusable contract fixture for <see cref="IGovernanceDecisionPolicy{TContext}" /> implementations.
 9/// </summary>
 10/// <typeparam name="TContext">The framework-neutral evaluation context type.</typeparam>
 11public abstract class GovernanceDecisionPolicyContract<TContext>
 12    where TContext : IGovernanceEvaluationContext
 13{
 14    /// <summary>
 15    /// Creates the decision-policy implementation under test.
 16    /// </summary>
 17    /// <returns>The decision-policy implementation to validate.</returns>
 18    protected abstract IGovernanceDecisionPolicy<TContext> CreateDecisionPolicy();
 19
 20    /// <summary>
 21    /// Creates the context supplied to the decision-policy implementation under test.
 22    /// </summary>
 23    /// <returns>The evaluation context to validate with.</returns>
 24    protected abstract TContext CreateEvaluationContext();
 25
 26    /// <summary>
 27    /// Creates the composed decision supplied to the decision-policy implementation under test.
 28    /// </summary>
 29    /// <param name="context">The evaluation context created for the contract run.</param>
 30    /// <returns>The composed decision supplied to the decision policy.</returns>
 31    protected virtual GovernanceDecision CreateComposedDecision(TContext context)
 32    {
 633        ArgumentNullException.ThrowIfNull(context);
 34
 635        return GovernanceDecision.Allow(
 636            context.CorrelationId,
 637            traceId: null,
 638            context.PolicyVersion,
 639            context.PolicyHash);
 40    }
 41
 42    /// <summary>
 43    /// Creates the constraint-result collection supplied to the decision-policy implementation under test.
 44    /// </summary>
 45    /// <returns>The constraint-result collection supplied to the decision policy.</returns>
 46    protected virtual IReadOnlyList<ConstraintEvaluationResult> CreateConstraintResults()
 47    {
 548        return Array.Empty<ConstraintEvaluationResult>();
 49    }
 50
 51    /// <summary>
 52    /// Verifies that the decision policy returns a safe, non-null decision and preserves supplied decision telemetry.
 53    /// </summary>
 54    /// <param name="cancellationToken">A token that can cancel the contract validation.</param>
 55    /// <returns>The verified governance decision.</returns>
 56    public async ValueTask<GovernanceDecision> VerifyDecisionPolicyReturnsSafeDecisionAsync(CancellationToken cancellati
 57    {
 958        IGovernanceDecisionPolicy<TContext> decisionPolicy = CreateDecisionPolicy()
 959            ?? throw new GovernanceContractViolationException("Decision-policy contract must provide a decision policy i
 860        TContext context = CreateEvaluationContext()
 861            ?? throw new GovernanceContractViolationException("Decision-policy contract must provide an evaluation conte
 762        GovernanceDecision composedDecision = CreateComposedDecision(context)
 763            ?? throw new GovernanceContractViolationException("Decision-policy contract must provide a composed decision
 664        IReadOnlyList<ConstraintEvaluationResult> constraintResults = CreateConstraintResults()
 665            ?? throw new GovernanceContractViolationException("Decision-policy contract must provide a constraint-result
 66
 67        try
 68        {
 569            GovernanceDecision decision = await decisionPolicy.ApplyAsync(context, composedDecision, constraintResults, 
 170            return GovernanceDecisionContract.VerifyTelemetryFromContext(decision, context, "Decision-policy decision");
 71        }
 172        catch (OperationCanceledException)
 73        {
 174            throw;
 75        }
 176        catch (GovernanceContractViolationException)
 77        {
 178            throw;
 79        }
 280        catch (Exception exception)
 81        {
 282            throw new GovernanceContractViolationException(
 283                "Decision-policy implementations must return safe decisions instead of bypassing the policy pipeline by 
 284                exception);
 85        }
 186    }
 87}