< Summary

Information
Class: AsiBackbone.AspNetCore.Results.GovernanceHttpResultMappingOptions
Assembly: AsiBackbone.AspNetCore
File(s): /home/runner/work/AsiBackbone/AsiBackbone/src/AsiBackbone.AspNetCore/Results/GovernanceHttpResultMappingOptions.cs
Line coverage
100%
Covered lines: 16
Uncovered lines: 0
Coverable lines: 16
Total lines: 103
Line coverage: 100%
Branch coverage
90%
Covered branches: 9
Total branches: 10
Branch coverage: 90%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
Validate()100%44100%
ValidateStatusCode(...)83.33%66100%

File(s)

/home/runner/work/AsiBackbone/AsiBackbone/src/AsiBackbone.AspNetCore/Results/GovernanceHttpResultMappingOptions.cs

#LineLine coverage
 1using Microsoft.AspNetCore.Http;
 2
 3namespace AsiBackbone.AspNetCore.Results;
 4
 5/// <summary>
 6/// Provides host-overridable defaults for mapping AsiBackbone Core outcomes into HTTP responses.
 7/// </summary>
 8public sealed class GovernanceHttpResultMappingOptions
 9{
 10    /// <summary>
 11    /// Gets or sets the status code returned for allowed governance decisions and successful operation results.
 12    /// </summary>
 13    public int SuccessStatusCode { get; set; } = StatusCodes.Status200OK;
 14
 15    /// <summary>
 16    /// Gets or sets the status code returned for warning governance decisions.
 17    /// </summary>
 18    public int WarningStatusCode { get; set; } = StatusCodes.Status200OK;
 19
 20    /// <summary>
 21    /// Gets or sets the status code returned for denied governance decisions.
 22    /// </summary>
 23    public int DeniedStatusCode { get; set; } = StatusCodes.Status403Forbidden;
 24
 25    /// <summary>
 26    /// Gets or sets the status code returned for deferred governance decisions.
 27    /// </summary>
 28    public int DeferredStatusCode { get; set; } = StatusCodes.Status202Accepted;
 29
 30    /// <summary>
 31    /// Gets or sets the status code returned for acknowledgment-required governance decisions.
 32    /// </summary>
 33    public int AcknowledgmentRequiredStatusCode { get; set; } = StatusCodes.Status428PreconditionRequired;
 34
 35    /// <summary>
 36    /// Gets or sets the status code returned for escalation-recommended governance decisions.
 37    /// </summary>
 38    public int EscalationRecommendedStatusCode { get; set; } = StatusCodes.Status409Conflict;
 39
 40    /// <summary>
 41    /// Gets or sets the status code returned for failed operation results.
 42    /// </summary>
 43    public int OperationFailureStatusCode { get; set; } = StatusCodes.Status400BadRequest;
 44
 45    /// <summary>
 46    /// Gets or sets the safe user-facing message used for non-success governance decisions.
 47    /// </summary>
 48    public string GovernanceDecisionNotAllowedMessage { get; set; } =
 5049        "The governance decision did not allow immediate execution.";
 50
 51    /// <summary>
 52    /// Gets or sets the safe user-facing message used for failed operation results.
 53    /// </summary>
 54    public string OperationFailureMessage { get; set; } = "The operation did not complete successfully.";
 55
 56    /// <summary>
 57    /// Gets or sets a value indicating whether reason messages may be exposed in HTTP responses.
 58    /// </summary>
 59    public bool IncludeReasonMessages { get; set; }
 60
 61    /// <summary>
 62    /// Gets or sets a value indicating whether trace identifiers may be exposed in HTTP responses.
 63    /// </summary>
 64    public bool IncludeTraceId { get; set; }
 65
 66    /// <summary>
 67    /// Gets or sets a value indicating whether policy version and policy hash values may be exposed in HTTP responses.
 68    /// </summary>
 69    public bool IncludePolicyMetadata { get; set; }
 70
 71    /// <summary>
 72    /// Validates the configured mapping options.
 73    /// </summary>
 74    /// <exception cref="InvalidOperationException">Thrown when a configured status code or message is invalid.</excepti
 75    public void Validate()
 76    {
 7377        ValidateStatusCode(SuccessStatusCode, nameof(SuccessStatusCode));
 7278        ValidateStatusCode(WarningStatusCode, nameof(WarningStatusCode));
 7179        ValidateStatusCode(DeniedStatusCode, nameof(DeniedStatusCode));
 6980        ValidateStatusCode(DeferredStatusCode, nameof(DeferredStatusCode));
 6881        ValidateStatusCode(AcknowledgmentRequiredStatusCode, nameof(AcknowledgmentRequiredStatusCode));
 6782        ValidateStatusCode(EscalationRecommendedStatusCode, nameof(EscalationRecommendedStatusCode));
 6683        ValidateStatusCode(OperationFailureStatusCode, nameof(OperationFailureStatusCode));
 84
 6585        if (string.IsNullOrWhiteSpace(GovernanceDecisionNotAllowedMessage))
 86        {
 187            throw new InvalidOperationException("A safe governance decision response message must be configured.");
 88        }
 89
 6490        if (string.IsNullOrWhiteSpace(OperationFailureMessage))
 91        {
 192            throw new InvalidOperationException("A safe operation failure response message must be configured.");
 93        }
 6394    }
 95
 96    private static void ValidateStatusCode(int statusCode, string propertyName)
 97    {
 48698        if (statusCode is < 100 or > 599)
 99        {
 8100            throw new InvalidOperationException($"{propertyName} must be a valid HTTP status code.");
 101        }
 478102    }
 103}