Path: redb-route/redb.Route/Components/MockExpectation.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Components/MockExpectation.cs ---- using System.Globalization; using redb.Route.Abstractions; using redb.Route.Core; namespace redb.Route.Components; /// /// What a captured when a message arrived: the live exchange plus the /// body, headers and properties as they were at that moment. Built-in expectations compare /// against the captured values, so a step that mutates the exchange after the mock cannot change /// what the mock "saw" (Apache Camel's mock copies the exchange for the same reason). /// internal sealed class MockReceived { public required IExchange Exchange { get; init; } public required object? Body { get; init; } public required IReadOnlyDictionary Headers { get; init; } public required IReadOnlyDictionary Properties { get; init; } public static MockReceived Capture(IExchange exchange) => new() { Exchange = exchange, Body = exchange.In.Body, Headers = new Dictionary(exchange.In.Headers, StringComparer.OrdinalIgnoreCase), Properties = new Dictionary(exchange.Properties), }; } /// /// One expectation registered on a . Evaluated against the captured /// messages; returns null when satisfied, otherwise one human-readable failure line in the /// Apache Camel style (Received message count. Expected: 1 but was: 0). /// internal abstract class MockExpectation { public abstract ValueTask EvaluateAsync(IReadOnlyList received); } /// Exact or minimum number of received messages. internal sealed class MessageCountExpectation(int expected, bool minimum) : MockExpectation { public bool IsMinimum => minimum; public override ValueTask EvaluateAsync(IReadOnlyList received) { var ok = minimum ? received.Count >= expected : received.Count == expected; if (ok) return ValueTask.FromResult(null); var kind = minimum ? "Expected at least" : "Expected"; return ValueTask.FromResult($"Received message count. {kind}: {expected} but was: {received.Count}"); } } /// Bodies received, in order or in any order. internal sealed class BodiesExpectation(object?[] bodies, bool anyOrder) : MockExpectation { public override ValueTask EvaluateAsync(IReadOnlyList received) { if (received.Count != bodies.Length) return ValueTask.FromResult( $"Received message count. Expected: {bodies.Length} but was: {received.Count}"); if (!anyOrder) { for (var i = 0; i < bodies.Length; i++) { if (!MockValues.AreEqual(bodies[i], received[i].Body)) return ValueTask.FromResult( $"Body of message {i + 1}. Expected: {MockValues.Describe(bodies[i])} but was: {MockValues.Describe(received[i].Body)}"); } return ValueTask.FromResult(null); } var remaining = received.Select(r => r.Body).ToList(); foreach (var expected in bodies) { var index = remaining.FindIndex(actual => MockValues.AreEqual(expected, actual)); if (index < 0) return ValueTask.FromResult( $"Body {MockValues.Describe(expected)} expected in any order but was not received"); remaining.RemoveAt(index); } return ValueTask.FromResult(null); } } /// A header (or exchange property) with a given value on every message, or on at least one. internal sealed class KeyValueExpectation(string kind, string key, object? value, bool every) : MockExpectation { public override ValueTask EvaluateAsync(IReadOnlyList received) { if (received.Count == 0) return ValueTask.FromResult($"{kind} '{key}'. Expected: {MockValues.Describe(value)} but no message was received"); for (var i = 0; i < received.Count; i++) { var store = kind == "Property" ? received[i].Properties : received[i].Headers; var present = store.TryGetValue(key, out var actual); var matches = present && MockValues.AreEqual(value, actual); if (matches && !every) return ValueTask.FromResult(null); if (!matches && every) return ValueTask.FromResult( $"{kind} '{key}' on message {i + 1}. Expected: {MockValues.Describe(value)} but was: {(present ? MockValues.Describe(actual) : "")}"); } return ValueTask.FromResult(every ? null : $"{kind} '{key}'. Expected: {MockValues.Describe(value)} on at least one message but none matched"); } } /// An arbitrary predicate holding on every message (evaluated against the live exchange). internal sealed class PredicateExpectation(Func> predicate, string description) : MockExpectation { public override async ValueTask EvaluateAsync(IReadOnlyList received) { if (received.Count == 0) return $"Predicate {description}. Expected to hold but no message was received"; for (var i = 0; i < received.Count; i++) { if (!await predicate(received[i].Exchange).ConfigureAwait(false)) return $"Predicate {description} failed on message {i + 1}: {MockValues.Describe(received[i].Body)}"; } return null; } } /// Value comparison and formatting shared by the expectations. internal static class MockValues { /// /// plus two conveniences that make expectations /// read naturally: numbers compare across CLR types (1 equals 1L), and byte arrays /// compare by content. /// public static bool AreEqual(object? expected, object? actual) { if (Equals(expected, actual)) return true; if (expected is null || actual is null) return false; if (IsNumeric(expected) && IsNumeric(actual)) { try { return RouteDecimal.From(expected) == RouteDecimal.From(actual); } catch (OverflowException) { return false; } } if (expected is byte[] eb && actual is byte[] ab) return eb.AsSpan().SequenceEqual(ab); return false; } public static string Describe(object? value) => value switch { null => "", string s => $"\"{s}\"", byte[] b => $"byte[{b.Length}]", IFormattable f => f.ToString(null, CultureInfo.InvariantCulture), _ => value.ToString() ?? value.GetType().Name, }; private static bool IsNumeric(object value) => value is sbyte or byte or short or ushort or int or uint or long or ulong or float or double or decimal; }