Path: redb-route/tests/redb.Route.Tests.Xml/XmlPackageContributionsTests.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/XmlPackageContributionsTests.cs ---- using System.Xml.Linq; using AwesomeAssertions; using redb.Route.Abstractions; using redb.Route.Cache; using redb.Route.Core; using redb.Route.JsonTransform; using redb.Route.Templates; using redb.Route.Xml; namespace redb.Route.Tests.Xml; /// /// The first REAL package contributions through the extension channel — `redb.Route.Cache`, /// `redb.Route.JsonTransform`, `redb.Route.Templates` each ship their element (parse + schema /// shape + C# printing in one class) and a host registers them via /// . This closes the last "package elements" rows /// of the element table reachable before Tsak (`<rest>` waits for the Http package to be /// free, `redb.Route.Core` waits for the owner). /// public class XmlPackageContributionsTests : IAsyncLifetime { private static readonly XmlRouteLoaderOptions Options = new() { Extensions = [new CacheXmlContribution(), new JsonTransformXmlContribution(), new PayloadXmlContribution()], }; 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; } [Fact] public async Task Cache_Element_CachesTheScopeResult() { _context.UseCache(); var inner = 0; _context.AddRoutes(r => r.From("direct://cache-inner").Process(e => { inner++; e.In.Body = $"computed-{e.In.Headers["k"]}"; })); _context.AddXmlRoutesFromContent(""" """, options: Options); var producer = await StartAndProducer("direct://pkg-cache-in"); var first = Msg("a"); await producer.Process(first); var second = Msg("a"); await producer.Process(second); var other = Msg("b"); await producer.Process(other); inner.Should().Be(2, "the second send with the same key is a cache hit"); second.In.Body.Should().Be("computed-a"); other.In.Body.Should().Be("computed-b"); } [Fact] public async Task TransformJson_Element_RunsInlineJsonata() { _context.UseJsonTransform(); _context.AddXmlRoutesFromContent(""" """, options: Options); var producer = await StartAndProducer("direct://pkg-json-in"); var exchange = new Exchange(new Message("""{"name":"bob"}""")); await producer.Process(exchange); exchange.In.Body.Should().BeOfType().Which.Should().Contain("\"who\"").And.Contain("BOB"); } [Fact] public async Task Payload_Element_RendersTheTemplate_WithArgs() { _context.UseTemplates(); _context.AddXmlRoutesFromContent(""" """, options: Options); var producer = await StartAndProducer("direct://pkg-payload-in"); var exchange = Msg("x", ("user", "rinat")); await producer.Process(exchange); exchange.In.Headers["greeting"].Should().Be("hello rinat"); } // A constant key is one entry for every message; a key over header.k is one entry per value of k. [Theory] [InlineData("orders-all", 1)] [InlineData("header.k", 2)] [InlineData("${header.k}-x", 2)] public async Task CacheKey_InMarkup_FollowsTheKeyRule(string key, int misses) { _context.UseCache(); var keys = new List(); _context.AddRoutes(r => r.From("direct://cache-key-inner").Process(e => keys.Add("miss"))); _context.AddXmlRoutesFromContent($""" """, options: Options); var producer = await StartAndProducer("direct://pkg-cache-key-in"); await producer.Process(Msg("a")); await producer.Process(Msg("b")); await producer.Process(Msg("a")); keys.Should().HaveCount(misses); } [Fact] public void CacheKey_BrokenArithmeticOverARoot_FailsTheLoad() { var act = () => _context.AddXmlRoutesFromContent(""" """, options: Options); act.Should().Throw().Where(e => e.ToString().Contains("is not a value")); } [Fact] public void PackageElements_ValidateAgainstTheSchema_ThroughTheirSpecs() { // The schema pass is ON: these load only because each contribution declares its shape. var act = () => _context.AddXmlRoutesFromContent(""" """, options: Options); act.Should().Throw().WithMessage("*[schema]*provder*", "a typo in a package element's attribute is caught by the generated schema"); } [Fact] public void PackageElements_PrintThroughTheGenerator() { var code = XmlCodeGenerator.Generate(XDocument.Parse(""" """, LoadOptions.SetLineInfo), "PkgRoutes", "Demo", options: Options); code.Should().Contain(".Cache(\"${header.k}\", TimeSpan.FromSeconds(60));") .And.Contain("Distributed()") .And.Contain("TransformJson(\"specs/normalize.jsonata\")") .And.Contain("SetBodyTemplate(TextSource.Inline("); } // ── : the container-level contribution of the Http package ───────── private static readonly XmlRouteLoaderOptions RestOptions = new() { Extensions = [new Route.Http.Rest.RestXmlContribution()], }; [Fact] public void Rest_SpawnsTheHttpRoutes_ForToAndInlineVerbs() { _context.AddComponent(new KafkaStubComponent()); // registers "kafka" for the to= check _context.AddXmlRoutesFromContent(""" """, options: RestOptions); var ids = Examples.ExampleHarness.Definitions(_context) .OfType().Select(r => r.GetRouteId()).ToList(); ids.Should().Contain("orders-get", "id= names the verb's route"); ids.Should().Contain(id => id != null && id.StartsWith("rest-POST-api-orders-") && !id.EndsWith("-handler"), "the inline verb spawns its consumer route"); ids.Should().Contain(id => id != null && id.StartsWith("rest-POST-api-orders-") && id.EndsWith("-handler"), "inline steps live in their own handler route"); } [Fact] public void Rest_VerbWithBothToAndSteps_IsASchemaError() { var act = () => _context.AddXmlRoutesFromContent(""" """, options: RestOptions); act.Should().Throw() .WithMessage("*to= or with inline steps*not both*"); } [Fact] public void Rest_InsideARoute_IsRefusedWithAClearMessage() { var act = () => _context.AddXmlRoutesFromContent(""" """, options: RestOptions); act.Should().Throw() .WithMessage("*container-level element*beside *"); } [Fact] public void Rest_PrintsThroughTheGenerator() { var code = XmlCodeGenerator.Generate(XDocument.Parse(""" """, LoadOptions.SetLineInfo), "RestRoutes", "Demo", options: RestOptions); code.Should().Contain("Rest(\"/api\", options => { options.Port = 5099; })") .And.Contain(".Get(\"/{id}\").To(\"direct://x\");") .And.Contain(".Post(\"\").Route()"); } private static IExchange Msg(object? body, params (string Name, object? Value)[] headers) { var exchange = new Exchange(new Message(body)); foreach (var (name, value) in headers) exchange.In.Headers[name] = value; return exchange; } private static IExchange Msg(string key) { var exchange = new Exchange(new Message("payload")); exchange.In.Headers["k"] = key; return exchange; } }