| | | 1 | | using AsiBackbone.Core.Audit; |
| | | 2 | | |
| | | 3 | | namespace AsiBackbone.Testing.Contracts; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Reusable contract fixture for <see cref="IAsiBackboneAuditSink" /> implementations. |
| | | 7 | | /// </summary> |
| | | 8 | | public abstract class AsiBackboneAuditSinkContract |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Creates the audit sink implementation under test. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <returns>The audit sink implementation to validate.</returns> |
| | | 14 | | protected abstract IAsiBackboneAuditSink CreateAuditSink(); |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Creates the audit residue supplied to the audit sink implementation under test. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <returns>The audit residue to write.</returns> |
| | | 20 | | protected abstract IAsiBackboneAuditResidue CreateAuditResidue(); |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Verifies that an audit sink accepts a valid audit residue value without weakening the residue shape. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <param name="cancellationToken">A token that can cancel the contract validation.</param> |
| | | 26 | | /// <returns>A task that completes when the contract validation succeeds.</returns> |
| | | 27 | | public async ValueTask VerifyAuditSinkAcceptsValidResidueAsync(CancellationToken cancellationToken = default) |
| | | 28 | | { |
| | 12 | 29 | | IAsiBackboneAuditSink auditSink = CreateAuditSink() |
| | 12 | 30 | | ?? throw new AsiBackboneContractViolationException("Audit sink contract must provide an audit sink instance. |
| | 10 | 31 | | IAsiBackboneAuditResidue residue = CreateAuditResidue() |
| | 10 | 32 | | ?? throw new AsiBackboneContractViolationException("Audit sink contract must provide audit residue."); |
| | | 33 | | |
| | 8 | 34 | | _ = AsiBackboneDecisionContract.VerifyAuditResidue(residue, "Audit sink residue"); |
| | | 35 | | |
| | | 36 | | try |
| | | 37 | | { |
| | 8 | 38 | | await auditSink.WriteAsync(residue, cancellationToken).ConfigureAwait(false); |
| | 2 | 39 | | } |
| | 2 | 40 | | catch (OperationCanceledException) |
| | | 41 | | { |
| | 2 | 42 | | throw; |
| | | 43 | | } |
| | 2 | 44 | | catch (AsiBackboneContractViolationException) |
| | | 45 | | { |
| | 2 | 46 | | throw; |
| | | 47 | | } |
| | 2 | 48 | | catch (Exception exception) |
| | | 49 | | { |
| | 2 | 50 | | throw new AsiBackboneContractViolationException( |
| | 2 | 51 | | "Audit sink implementations must accept valid audit residue during normal contract validation or documen |
| | 2 | 52 | | exception); |
| | | 53 | | } |
| | 2 | 54 | | } |
| | | 55 | | } |