< 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: 115
Uncovered lines: 0
Coverable lines: 115
Total lines: 391
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
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%44100%
AddMetadata(...)100%11100%
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;
 419    private readonly List<string> reasonCodes = [];
 420    private readonly Dictionary<string, string> metadata = new(StringComparer.Ordinal);
 21
 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
 445    private AuditResidueBuilder(
 446        IAsiBackboneActorContext actor,
 447        string operationName,
 448        string outcome,
 449        IEnumerable<string>? reasonCodes = null)
 50    {
 451        ArgumentNullException.ThrowIfNull(actor);
 52
 453        this.actor = actor;
 454        this.operationName = operationName;
 455        this.outcome = outcome;
 456        _ = WithReasonCodes(reasonCodes);
 457    }
 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    {
 267        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    {
 278        ArgumentNullException.ThrowIfNull(decision);
 79
 180        return new AuditResidueBuilder(
 181            actor,
 182            operationName,
 183            decision.Outcome.ToString(),
 184            decision.ReasonCodes)
 185            .WithCorrelationId(decision.CorrelationId)
 186            .WithTraceId(decision.TraceId)
 187            .WithPolicyVersion(decision.PolicyVersion)
 188            .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    {
 299        ArgumentNullException.ThrowIfNull(constraintResult);
 100
 1101        return new AuditResidueBuilder(
 1102            actor,
 1103            operationName,
 1104            constraintResult.Outcome.ToString(),
 1105            constraintResult.ReasonCodes);
 106    }
 107
 108    /// <summary>
 109    /// Sets the audit event identifier.
 110    /// </summary>
 111    public AuditResidueBuilder WithEventId(string? value)
 112    {
 3113        eventId = value;
 3114        return this;
 115    }
 116
 117    /// <summary>
 118    /// Sets the event occurrence timestamp.
 119    /// </summary>
 120    public AuditResidueBuilder WithOccurredUtc(DateTimeOffset? value)
 121    {
 3122        occurredUtc = value;
 3123        return this;
 124    }
 125
 126    /// <summary>
 127    /// Replaces the reason-code collection.
 128    /// </summary>
 129    public AuditResidueBuilder WithReasonCodes(IEnumerable<string>? values)
 130    {
 4131        reasonCodes.Clear();
 132
 4133        if (values is not null)
 134        {
 2135            reasonCodes.AddRange(values);
 136        }
 137
 4138        return this;
 139    }
 140
 141    /// <summary>
 142    /// Adds one reason code to the builder.
 143    /// </summary>
 144    public AuditResidueBuilder AddReasonCode(string value)
 145    {
 4146        reasonCodes.Add(value);
 4147        return this;
 148    }
 149
 150    /// <summary>
 151    /// Sets the correlation identifier.
 152    /// </summary>
 153    public AuditResidueBuilder WithCorrelationId(string? value)
 154    {
 3155        correlationId = value;
 3156        return this;
 157    }
 158
 159    /// <summary>
 160    /// Sets the trace identifier.
 161    /// </summary>
 162    public AuditResidueBuilder WithTraceId(string? value)
 163    {
 3164        traceId = value;
 3165        return this;
 166    }
 167
 168    /// <summary>
 169    /// Sets the policy version.
 170    /// </summary>
 171    public AuditResidueBuilder WithPolicyVersion(string? value)
 172    {
 3173        policyVersion = value;
 3174        return this;
 175    }
 176
 177    /// <summary>
 178    /// Sets the policy hash.
 179    /// </summary>
 180    public AuditResidueBuilder WithPolicyHash(string? value)
 181    {
 3182        policyHash = value;
 3183        return this;
 184    }
 185
 186    /// <summary>
 187    /// Replaces the metadata collection.
 188    /// </summary>
 189    public AuditResidueBuilder WithMetadata(IReadOnlyDictionary<string, string>? values)
 190    {
 2191        metadata.Clear();
 192
 2193        if (values is not null)
 194        {
 10195            foreach (KeyValuePair<string, string> item in values)
 196            {
 3197                metadata[item.Key] = item.Value;
 198            }
 199        }
 200
 2201        return this;
 202    }
 203
 204    /// <summary>
 205    /// Adds or replaces one metadata value.
 206    /// </summary>
 207    public AuditResidueBuilder AddMetadata(string key, string value)
 208    {
 3209        metadata[key] = value;
 3210        return this;
 211    }
 212
 213    /// <summary>
 214    /// Sets the stable audit residue identifier.
 215    /// </summary>
 216    public AuditResidueBuilder WithAuditResidueId(string? value)
 217    {
 2218        auditResidueId = value;
 2219        return this;
 220    }
 221
 222    /// <summary>
 223    /// Sets the span identifier.
 224    /// </summary>
 225    public AuditResidueBuilder WithSpanId(string? value)
 226    {
 2227        spanId = value;
 2228        return this;
 229    }
 230
 231    /// <summary>
 232    /// Sets the parent span identifier.
 233    /// </summary>
 234    public AuditResidueBuilder WithParentSpanId(string? value)
 235    {
 2236        parentSpanId = value;
 2237        return this;
 238    }
 239
 240    /// <summary>
 241    /// Sets the decision latency in milliseconds.
 242    /// </summary>
 243    public AuditResidueBuilder WithDecisionLatencyMs(long? value)
 244    {
 2245        decisionLatencyMs = value;
 2246        return this;
 247    }
 248
 249    /// <summary>
 250    /// Sets the evaluated constraint-set hash.
 251    /// </summary>
 252    public AuditResidueBuilder WithConstraintSetHash(string? value)
 253    {
 2254        constraintSetHash = value;
 2255        return this;
 256    }
 257
 258    /// <summary>
 259    /// Sets the evaluated constraint count.
 260    /// </summary>
 261    public AuditResidueBuilder WithConstraintCount(int? value)
 262    {
 2263        constraintCount = value;
 2264        return this;
 265    }
 266
 267    /// <summary>
 268    /// Sets the host-defined risk score.
 269    /// </summary>
 270    public AuditResidueBuilder WithRiskScore(double? value)
 271    {
 2272        riskScore = value;
 2273        return this;
 274    }
 275
 276    /// <summary>
 277    /// Sets the host-defined policy scope.
 278    /// </summary>
 279    public AuditResidueBuilder WithPolicyScope(string? value)
 280    {
 2281        policyScope = value;
 2282        return this;
 283    }
 284
 285    /// <summary>
 286    /// Sets the privacy-preserving tenant hash.
 287    /// </summary>
 288    public AuditResidueBuilder WithTenantHash(string? value)
 289    {
 2290        tenantHash = value;
 2291        return this;
 292    }
 293
 294    /// <summary>
 295    /// Sets the privacy-preserving organization hash.
 296    /// </summary>
 297    public AuditResidueBuilder WithOrganizationHash(string? value)
 298    {
 2299        organizationHash = value;
 2300        return this;
 301    }
 302
 303    /// <summary>
 304    /// Sets the provider-neutral emitter status.
 305    /// </summary>
 306    public AuditResidueBuilder WithEmitterStatus(string? value)
 307    {
 2308        emitterStatus = value;
 2309        return this;
 310    }
 311
 312    /// <summary>
 313    /// Sets the provider-neutral emitter provider name.
 314    /// </summary>
 315    public AuditResidueBuilder WithEmitterProvider(string? value)
 316    {
 2317        emitterProvider = value;
 2318        return this;
 319    }
 320
 321    /// <summary>
 322    /// Sets the outbox sequence.
 323    /// </summary>
 324    public AuditResidueBuilder WithOutboxSequence(long? value)
 325    {
 2326        outboxSequence = value;
 2327        return this;
 328    }
 329
 330    /// <summary>
 331    /// Sets the gateway execution identifier.
 332    /// </summary>
 333    public AuditResidueBuilder WithGatewayExecutionId(string? value)
 334    {
 2335        gatewayExecutionId = value;
 2336        return this;
 337    }
 338
 339    /// <summary>
 340    /// Sets the provider-neutral decision stage.
 341    /// </summary>
 342    public AuditResidueBuilder WithDecisionStage(string? value)
 343    {
 2344        decisionStage = value;
 2345        return this;
 346    }
 347
 348    /// <summary>
 349    /// Sets the audit residue schema version.
 350    /// </summary>
 351    public AuditResidueBuilder WithSchemaVersion(string? value)
 352    {
 2353        schemaVersion = value;
 2354        return this;
 355    }
 356
 357    /// <summary>
 358    /// Builds an immutable audit residue value.
 359    /// </summary>
 360    public AuditResidue Build()
 361    {
 5362        return AuditResidue.Create(
 5363            actor,
 5364            operationName,
 5365            outcome,
 5366            reasonCodes,
 5367            eventId,
 5368            occurredUtc,
 5369            correlationId,
 5370            traceId,
 5371            policyVersion,
 5372            policyHash,
 5373            metadata,
 5374            auditResidueId,
 5375            spanId,
 5376            parentSpanId,
 5377            decisionLatencyMs,
 5378            constraintSetHash,
 5379            constraintCount,
 5380            riskScore,
 5381            policyScope,
 5382            tenantHash,
 5383            organizationHash,
 5384            emitterStatus,
 5385            emitterProvider,
 5386            outboxSequence,
 5387            gatewayExecutionId,
 5388            decisionStage,
 5389            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()