Path: redb-route/tests/redb.Route.Tests.Xml/CatalogContributionsTests.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/tests/redb.Route.Tests.Xml/CatalogContributionsTests.cs
----
using AwesomeAssertions;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Xml;
namespace redb.Route.Tests.Xml;
/// A body type for the unmarshal/ofType tests.
public class CatalogOrder
{
public string? Id { get; set; }
public int Amount { get; set; }
}
///
/// Catalog batch: the rest of the element set — leaves, scopes, branch
/// children and the schema errors the format promises. Behavioral spot-checks plus one
/// anti-drift document that exercises every element at once.
///
public class CatalogContributionsTests : IAsyncLifetime
{
private readonly RouteContext _context = new();
public Task InitializeAsync() => Task.CompletedTask;
public async Task DisposeAsync()
{
await _context.DisposeAsync();
GC.SuppressFinalize(this);
}
private async Task StartAndProducer(string fromUri)
{
await _context.Start();
var producer = _context.GetEndpoint(fromUri).CreateProducer();
await producer.Start();
return producer;
}
private void Load(string xml, string? sourceName = null)
=> _context.AddXmlRoutesFromContent(xml, sourceName ?? "");
// ── behavior ─────────────────────────────────────────────────────────────
[Fact]
public async Task SetHeaders_MixesConstantsAndExpressions()
{
Load("""
""");
var producer = await StartAndProducer("direct://cat-sh-in");
var exchange = new Exchange(new Message("x"));
await producer.Process(exchange);
exchange.In.Headers["fixed"].Should().Be("A");
exchange.In.Headers["derived"].Should().Be("x-tail");
}
[Fact]
public async Task SetProperties_MixesConstantsAndExpressions_InOrder()
{
Load("""
""");
var producer = await StartAndProducer("direct://cat-sp-in");
var exchange = new Exchange(new Message("x"));
await producer.Process(exchange);
exchange.Properties["fixed"].Should().Be("A");
exchange.Properties["derived"].Should().Be("x-tail");
exchange.Properties["chained"].Should().Be("A+");
}
[Fact]
public void SetProperties_AcceptsOnlyPropertyChildren()
{
var foreignChild = () => Load("""
""");
foreignChild.Should().Throw().WithMessage("* accepts only children*");
}
[Fact]
public async Task TryCatch_CatchHandles_FinallyAlwaysRuns()
{
Load("""
""");
var producer = await StartAndProducer("direct://cat-tc-in");
var exchange = new Exchange(new Message("x"));
await producer.Process(exchange);
// Engine semantics: a matched catch marks the exception handled; the record stays.
exchange.ExceptionHandled.Should().BeTrue();
exchange.In.Headers["caught"].Should().Be("yes");
exchange.In.Headers["final"].Should().Be("yes");
}
[Fact]
public async Task Loop_Count_RepeatsTheBody()
{
var hits = 0;
_context.AddRoutes(r => r.From("direct://cat-loop-out").Process(_ => hits++));
Load("""
""");
var producer = await StartAndProducer("direct://cat-loop-in");
await producer.Process(new Exchange(new Message("x")));
hits.Should().Be(3);
}
[Fact]
public async Task Aggregate_CompletionSize_WithNamedStrategy_Merges()
{
object? merged = null;
_context.AddRoutes(r => r.From("direct://cat-agg-out").Process(e => merged = e.In.Body));
Load("""
""");
var producer = await StartAndProducer("direct://cat-agg-in");
await producer.Process(Msg("a", ("group", "g")));
await producer.Process(Msg("b", ("group", "g")));
merged.Should().Be("a+b");
}
[Fact]
public async Task Multicast_DeliversToEveryBranchStep()
{
// Multicast runs its branches in parallel by default — the sink must be thread-safe.
var seen = new System.Collections.Concurrent.ConcurrentBag();
_context.AddRoutes(r =>
{
r.From("direct://cat-mc-1").Process(_ => seen.Add("one"));
r.From("direct://cat-mc-2").Process(_ => seen.Add("two"));
});
Load("""
""");
var producer = await StartAndProducer("direct://cat-mc-in");
await producer.Process(new Exchange(new Message("x")));
seen.Should().BeEquivalentTo("one", "two");
}
[Fact]
public async Task Split_TokenizeLines_SplitsTheBody()
{
var parts = new List