< Summary

Information
Class: AsiBackbone.Core.Audit.AuditResidueBuilder
Assembly: AsiBackbone.Core
File(s): /home/runner/work/AsiBackbone/AsiBackbone/src/AsiBackbone.Core/Audit/AuditResidueBuilder.cs
Line coverage
100%
Covered lines: 114
Uncovered lines: 0
Coverable lines: 114
Total lines: 391
Line coverage: 100%
Branch coverage
100%
Covered branches: 10
Total branches: 10
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Create(...)100%11100%
FromDecision(...)100%11100%
FromConstraint(...)100%11100%
WithEventId(...)100%11100%
WithOccurredUtc(...)100%11100%
WithReasonCodes(...)100%22100%
AddReasonCode(...)100%11100%
WithCorrelationId(...)100%11100%
WithTraceId(...)100%11100%
WithPolicyVersion(...)100%11100%
WithPolicyHash(...)100%11100%
WithMetadata(...)100%66100%
AddMetadata(...)100%22100%
WithAuditResidueId(...)100%11100%
WithSpanId(...)100%11100%
WithParentSpanId(...)100%11100%
WithDecisionLatencyMs(...)100%11100%
WithConstraintSetHash(...)100%11100%
WithConstraintCount(...)100%11100%
WithRiskScore(...)100%11100%
WithPolicyScope(...)100%11100%
WithTenantHash(...)100%11100%
WithOrganizationHash(...)100%11100%
WithEmitterStatus(...)100%11100%
WithEmitterProvider(...)100%11100%
WithOutboxSequence(...)100%11100%
WithGatewayExecutionId(...)100%11100%
WithDecisionStage(...)100%11100%
WithSchemaVersion(...)100%11100%
Build()100%11100%

File(s)

/home/runner/work/AsiBackbone/AsiBackbone/src/AsiBackbone.Core/Audit/AuditResidueBuilder.cs

#LineLine coverage
 1using AsiBackbone.Core.Actors;
 2using AsiBackbone.Core.Constraints;
 3using AsiBackbone.Core.Decisions;
 4
 5namespace AsiBackbone.Core.Audit;
 6
 7/// <summary>
 8/// Provides a fluent construction path for complex <see cref="AuditResidue" /> values.
 9/// </summary>
 10/// <remarks>
 11/// The builder is intended for ergonomic object creation in host code, samples, and tests. It preserves the immutable
 12/// <see cref="AuditResidue" /> value model by producing a new residue when <see cref="Build" /> is called.
 13/// </remarks>
 14public sealed class AuditResidueBuilder
 15{
 16    private readonly IAsiBackboneActorContext actor;
 17    private readonly string operationName;
 18    private readonly string outcome;
 2019    private readonly List<string> reasonCodes = [];
 20
 21    private Dictionary<string, string>? metadata;
 22    private string? eventId;
 23    private DateTimeOffset? occurredUtc;
 24    private string? correlationId;
 25    private string? traceId;
 26    private string? policyVersion;
 27    private string? policyHash;
 28    private string? auditResidueId;
 29    private string? spanId;
 30    private string? parentSpanId;
 31    private long? decisionLatencyMs;
 32    private string? constraintSetHash;
 33    private int? constraintCount;
 34    private double? riskScore;
 35    private string? policyScope;
 36    private string? tenantHash;
 37    private string? organizationHash;
 38    private string? emitterStatus;
 39    private string? emitterProvider;
 40    private long? outboxSequence;
 41    private string? gatewayExecutionId;
 42    private string? decisionStage;
 43    private string? schemaVersion;
 44
 2045    private AuditResidueBuilder(
 2046        IAsiBackboneActorContext actor,
 2047        string operationName,
 2048        string outcome,
 2049        IEnumerable<string>? reasonCodes = null)
 50    {
 2051        ArgumentNullException.ThrowIfNull(actor);
 52
 2053        this.actor = actor;
 2054        this.operationName = operationName;
 2055        this.outcome = outcome;
 2056        _ = WithReasonCodes(reasonCodes);
 2057    }
 58
 59    /// <summary>
 60    /// Creates a builder for a host-defined audit outcome.
 61    /// </summary>
 62    public static AuditResidueBuilder Create(
 63        IAsiBackboneActorContext actor,
 64        string operationName,
 65        string outcome)
 66    {
 1667        return new AuditResidueBuilder(actor, operationName, outcome);
 68    }
 69
 70    /// <summary>
 71    /// Creates a builder initialized from a governance decision.
 72    /// </summary>
 73    public static AuditResidueBuilder FromDecision(
 74        IAsiBackboneActorContext actor,
 75        string operationName,
 76        GovernanceDecision decision)
 77    {
 478        ArgumentNullException.ThrowIfNull(decision);
 79
 280        return new AuditResidueBuilder(
 281            actor,
 282            operationName,
 283            decision.Outcome.ToString(),
 284            decision.ReasonCodes)
 285            .WithCorrelationId(decision.CorrelationId)
 286            .WithTraceId(decision.TraceId)
 287            .WithPolicyVersion(decision.PolicyVersion)
 288            .WithPolicyHash(decision.PolicyHash);
 89    }
 90
 91    /// <summary>
 92    /// Creates a builder initialized from a constraint evaluation result.
 93    /// </summary>
 94    public static AuditResidueBuilder FromConstraint(
 95        IAsiBackboneActorContext actor,
 96        string operationName,
 97        ConstraintEvaluationResult constraintResult)
 98    {
 499        ArgumentNullException.ThrowIfNull(constraintResult);
 100
 2101        return new AuditResidueBuilder(
 2102            actor,
 2103            operationName,
 2104            constraintResult.Outcome.ToString(),
 2105            constraintResult.ReasonCodes);
 106    }
 107
 108    /// <summary>
 109    /// Sets the audit event identifier.
 110    /// </summary>
 111    public AuditResidueBuilder WithEventId(string? value)
 112    {
 6113        eventId = value;
 6114        return this;
 115    }
 116
 117    /// <summary>
 118    /// Sets the event occurrence timestamp.
 119    /// </summary>
 120    public AuditResidueBuilder WithOccurredUtc(DateTimeOffset? value)
 121    {
 6122        occurredUtc = value;
 6123        return this;
 124    }
 125
 126    /// <summary>
 127    /// Replaces the reason-code collection.
 128    /// </summary>
 129    public AuditResidueBuilder WithReasonCodes(IEnumerable<string>? values)
 130    {
 20131        reasonCodes.Clear();
 132
 20133        if (values is not null)
 134        {
 4135            reasonCodes.AddRange(values);
 136        }
 137
 20138        return this;
 139    }
 140
 141    /// <summary>
 142    /// Adds one reason code to the builder.
 143    /// </summary>
 144    public AuditResidueBuilder AddReasonCode(string value)
 145    {
 8146        reasonCodes.Add(value);
 8147        return this;
 148    }
 149
 150    /// <summary>
 151    /// Sets the correlation identifier.
 152    /// </summary>
 153    public AuditResidueBuilder WithCorrelationId(string? value)
 154    {
 6155        correlationId = value;
 6156        return this;
 157    }
 158
 159    /// <summary>
 160    /// Sets the trace identifier.
 161    /// </summary>
 162    public AuditResidueBuilder WithTraceId(string? value)
 163    {
 6164        traceId = value;
 6165        return this;
 166    }
 167
 168    /// <summary>
 169    /// Sets the policy version.
 170    /// </summary>
 171    public AuditResidueBuilder WithPolicyVersion(string? value)
 172    {
 6173        policyVersion = value;
 6174        return this;
 175    }
 176
 177    /// <summary>
 178    /// Sets the policy hash.
 179    /// </summary>
 180    public AuditResidueBuilder WithPolicyHash(string? value)
 181    {
 6182        policyHash = value;
 6183        return this;
 184    }
 185
 186    /// <summary>
 187    /// Replaces the metadata collection.
 188    /// </summary>
 189    public AuditResidueBuilder WithMetadata(IReadOnlyDictionary<string, string>? values)
 190    {
 12191        metadata = null;
 192
 12193        if (values is not null)
 194        {
 44195            foreach (KeyValuePair<string, string> item in values)
 196            {
 12197                (metadata ??= new Dictionary<string, string>(StringComparer.Ordinal))[item.Key] = item.Value;
 198            }
 199        }
 200
 12201        return this;
 202    }
 203
 204    /// <summary>
 205    /// Adds or replaces one metadata value.
 206    /// </summary>
 207    public AuditResidueBuilder AddMetadata(string key, string value)
 208    {
 16209        (metadata ??= new Dictionary<string, string>(StringComparer.Ordinal))[key] = value;
 16210        return this;
 211    }
 212
 213    /// <summary>
 214    /// Sets the stable audit residue identifier.
 215    /// </summary>
 216    public AuditResidueBuilder WithAuditResidueId(string? value)
 217    {
 4218        auditResidueId = value;
 4219        return this;
 220    }
 221
 222    /// <summary>
 223    /// Sets the span identifier.
 224    /// </summary>
 225    public AuditResidueBuilder WithSpanId(string? value)
 226    {
 4227        spanId = value;
 4228        return this;
 229    }
 230
 231    /// <summary>
 232    /// Sets the parent span identifier.
 233    /// </summary>
 234    public AuditResidueBuilder WithParentSpanId(string? value)
 235    {
 4236        parentSpanId = value;
 4237        return this;
 238    }
 239
 240    /// <summary>
 241    /// Sets the decision latency in milliseconds.
 242    /// </summary>
 243    public AuditResidueBuilder WithDecisionLatencyMs(long? value)
 244    {
 4245        decisionLatencyMs = value;
 4246        return this;
 247    }
 248
 249    /// <summary>
 250    /// Sets the evaluated constraint-set hash.
 251    /// </summary>
 252    public AuditResidueBuilder WithConstraintSetHash(string? value)
 253    {
 4254        constraintSetHash = value;
 4255        return this;
 256    }
 257
 258    /// <summary>
 259    /// Sets the evaluated constraint count.
 260    /// </summary>
 261    public AuditResidueBuilder WithConstraintCount(int? value)
 262    {
 4263        constraintCount = value;
 4264        return this;
 265    }
 266
 267    /// <summary>
 268    /// Sets the host-defined risk score.
 269    /// </summary>
 270    public AuditResidueBuilder WithRiskScore(double? value)
 271    {
 4272        riskScore = value;
 4273        return this;
 274    }
 275
 276    /// <summary>
 277    /// Sets the host-defined policy scope.
 278    /// </summary>
 279    public AuditResidueBuilder WithPolicyScope(string? value)
 280    {
 4281        policyScope = value;
 4282        return this;
 283    }
 284
 285    /// <summary>
 286    /// Sets the privacy-preserving tenant hash.
 287    /// </summary>
 288    public AuditResidueBuilder WithTenantHash(string? value)
 289    {
 4290        tenantHash = value;
 4291        return this;
 292    }
 293
 294    /// <summary>
 295    /// Sets the privacy-preserving organization hash.
 296    /// </summary>
 297    public AuditResidueBuilder WithOrganizationHash(string? value)
 298    {
 4299        organizationHash = value;
 4300        return this;
 301    }
 302
 303    /// <summary>
 304    /// Sets the provider-neutral emitter status.
 305    /// </summary>
 306    public AuditResidueBuilder WithEmitterStatus(string? value)
 307    {
 4308        emitterStatus = value;
 4309        return this;
 310    }
 311
 312    /// <summary>
 313    /// Sets the provider-neutral emitter provider name.
 314    /// </summary>
 315    public AuditResidueBuilder WithEmitterProvider(string? value)
 316    {
 4317        emitterProvider = value;
 4318        return this;
 319    }
 320
 321    /// <summary>
 322    /// Sets the outbox sequence.
 323    /// </summary>
 324    public AuditResidueBuilder WithOutboxSequence(long? value)
 325    {
 4326        outboxSequence = value;
 4327        return this;
 328    }
 329
 330    /// <summary>
 331    /// Sets the gateway execution identifier.
 332    /// </summary>
 333    public AuditResidueBuilder WithGatewayExecutionId(string? value)
 334    {
 4335        gatewayExecutionId = value;
 4336        return this;
 337    }
 338
 339    /// <summary>
 340    /// Sets the provider-neutral decision stage.
 341    /// </summary>
 342    public AuditResidueBuilder WithDecisionStage(string? value)
 343    {
 4344        decisionStage = value;
 4345        return this;
 346    }
 347
 348    /// <summary>
 349    /// Sets the audit residue schema version.
 350    /// </summary>
 351    public AuditResidueBuilder WithSchemaVersion(string? value)
 352    {
 4353        schemaVersion = value;
 4354        return this;
 355    }
 356
 357    /// <summary>
 358    /// Builds an immutable audit residue value.
 359    /// </summary>
 360    public AuditResidue Build()
 361    {
 24362        return AuditResidue.Create(
 24363            actor,
 24364            operationName,
 24365            outcome,
 24366            reasonCodes,
 24367            eventId,
 24368            occurredUtc,
 24369            correlationId,
 24370            traceId,
 24371            policyVersion,
 24372            policyHash,
 24373            metadata,
 24374            auditResidueId,
 24375            spanId,
 24376            parentSpanId,
 24377            decisionLatencyMs,
 24378            constraintSetHash,
 24379            constraintCount,
 24380            riskScore,
 24381            policyScope,
 24382            tenantHash,
 24383            organizationHash,
 24384            emitterStatus,
 24385            emitterProvider,
 24386            outboxSequence,
 24387            gatewayExecutionId,
 24388            decisionStage,
 24389            schemaVersion);
 390    }
 391}

Methods/Properties

.ctor(AsiBackbone.Core.Actors.IAsiBackboneActorContext,System.String,System.String,System.Collections.Generic.IEnumerable`1<System.String>)
Create(AsiBackbone.Core.Actors.IAsiBackboneActorContext,System.String,System.String)
FromDecision(AsiBackbone.Core.Actors.IAsiBackboneActorContext,System.String,AsiBackbone.Core.Decisions.GovernanceDecision)
FromConstraint(AsiBackbone.Core.Actors.IAsiBackboneActorContext,System.String,AsiBackbone.Core.Constraints.ConstraintEvaluationResult)
WithEventId(System.String)
WithOccurredUtc(System.Nullable`1<System.DateTimeOffset>)
WithReasonCodes(System.Collections.Generic.IEnumerable`1<System.String>)
AddReasonCode(System.String)
WithCorrelationId(System.String)
WithTraceId(System.String)
WithPolicyVersion(System.String)
WithPolicyHash(System.String)
WithMetadata(System.Collections.Generic.IReadOnlyDictionary`2<System.String,System.String>)
AddMetadata(System.String,System.String)
WithAuditResidueId(System.String)
WithSpanId(System.String)
WithParentSpanId(System.String)
WithDecisionLatencyMs(System.Nullable`1<System.Int64>)
WithConstraintSetHash(System.String)
WithConstraintCount(System.Nullable`1<System.Int32>)
WithRiskScore(System.Nullable`1<System.Double>)
WithPolicyScope(System.String)
WithTenantHash(System.String)
WithOrganizationHash(System.String)
WithEmitterStatus(System.String)
WithEmitterProvider(System.String)
WithOutboxSequence(System.Nullable`1<System.Int64>)
WithGatewayExecutionId(System.String)
WithDecisionStage(System.String)
WithSchemaVersion(System.String)
Build()