Path: redb-route/tests/redb.Route.Tests.Xml/BeanStepXmlTests.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/BeanStepXmlTests.cs
----
using System.Xml.Linq;
using AwesomeAssertions;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Definitions;
using redb.Route.Xml;
namespace redb.Route.Tests.Xml;
///
/// The step <bean ref|beanType method/> (amended 2026-11-05): the structured endpoint child of
/// <to> without the wrapper. It normalizes through the same path into the same bean: endpoint;
/// the name bean keeps its other two places (the declaration under <routes>/<context>
/// and the endpoint child of <to>) exactly as before.
///
public class BeanStepXmlTests : IAsyncLifetime
{
private readonly RouteContext _context = new();
public Task InitializeAsync() => Task.CompletedTask;
public async Task DisposeAsync()
{
await _context.DisposeAsync();
GC.SuppressFinalize(this);
}
private static string Greeter => $"{typeof(XmlGreeter).FullName}, {typeof(XmlGreeter).Assembly.GetName().Name}";
private string? StepUri(string stepXml, out List errors)
{
var ctx = new XmlParseContext(_context, ElementRegistry.CreateDefault(), "probe.xml");
var uri = StructuredEndpoint.ResolveBeanStep(XElement.Parse(stepXml, LoadOptions.SetLineInfo), ctx);
errors = [.. ctx.Errors];
return uri;
}
private async Task StartAndProducer(string fromUri)
{
await _context.Start();
var producer = _context.GetEndpoint(fromUri).CreateProducer();
await producer.Start();
return producer;
}
private static string CodeOf(string xml)
=> XmlCodeGenerator.Generate(XDocument.Parse(xml), "Beans", "Tests.Generated", sourceName: "");
// ── normalization ────────────────────────────────────────────────────────
[Theory]
[InlineData("""""", "bean://#greeter?method=Greet")]
[InlineData("""""", "bean://#greeter?method=Greet")]
[InlineData("""""", "bean://#greeter")]
[InlineData("""""",
"bean://Acme.Pricing, Acme?method=Gross(${header.qty})&Prefix=x")]
[InlineData("""""", "bean://#greeter?method=Greet")]
public void Step_NormalizesIntoTheBeanUri(string step, string expected)
{
StepUri(step, out var errors).Should().Be(expected);
errors.Should().BeEmpty();
}
[Fact]
public void Step_AndTheToChild_MeetAtOneEndpointKey()
{
var step = StepUri("""""", out _)!;
var ctx = new XmlParseContext(_context, ElementRegistry.CreateDefault(), "probe.xml");
var child = StructuredEndpoint.Resolve(
XElement.Parse(""""""), ctx, out _)!;
EndpointUriParser.Parse(step).NormalizedKey.Should().Be(EndpointUriParser.Parse(child).NormalizedKey);
EndpointUriParser.Parse(step).NormalizedKey
.Should().Be(EndpointUriParser.Parse("bean:#greeter?method=Greet").NormalizedKey);
}
[Theory]
[InlineData("""""", "exactly one of ref=")]
[InlineData("""""", "exactly one of ref=")]
[InlineData("""""", "not path=")]
[InlineData("""""", "is a declaration")]
[InlineData("""""", "attributes only")]
[InlineData("""""", "contains '?'")]
public void Step_Malformed_IsAPositionedError(string step, string message)
{
StepUri(step, out var errors).Should().BeNull();
errors.Should().ContainSingle().Which.Should().Contain(message);
}
// ── the three places of ───────────────────────────────────────────
[Fact]
public async Task AllThreePlaces_ParseAndRun_InOneDocument()
{
_context.AddXmlRoutesFromContent($$"""
""");
var producer = await StartAndProducer("direct://xml-bean-step");
var exchange = new Exchange(new Message("bob"));
exchange.In.Headers["go"] = "yes";
await producer.Process(exchange);
exchange.In.Headers["first"].Should().Be("decl:bob");
exchange.In.Body.Should().Be("decl:decl:decl:bob", "the step, the bean: URI and the step in a scope each called it");
}
[Fact]
public async Task BeanTypeStep_CreatesTheInstance_WithItsProperties()
{
_context.AddXmlRoutesFromContent($$"""
""");
var producer = await StartAndProducer("direct://xml-bean-type-step");
var exchange = new Exchange(new Message("ann"));
await producer.Process(exchange);
exchange.In.Body.Should().Be("typed:ann");
}
[Fact]
public void StepAtTheRoutesLevel_IsReadAsADeclaration_AndRefused()
{
// At the container level a is a declaration: the step attributes do not make it a step.
var act = () => _context.AddXmlRoutesFromContent("""
""");
act.Should().Throw().WithMessage("*is a step: it belongs inside a *");
}
[Fact]
public void DeclarationInsideARoute_IsRefused_PointingToTheContainerLevel()
{
var act = () => _context.AddXmlRoutesFromContent($$"""
""");
act.Should().Throw().WithMessage("*is a declaration* or *");
}
// ── schema ───────────────────────────────────────────────────────────────
private static string ThreePlaces => $$"""
""";
[Fact]
public void CatalogSchema_TakesAllThreePlaces()
{
var catalog = ComponentCatalog.Build([new Components.Bean.BeanComponent()]);
XmlRouteSchema.Validate(XDocument.Parse(ThreePlaces, LoadOptions.SetLineInfo),
ElementRegistry.CreateDefault(), catalog)
.Should().BeEmpty();
}
[Fact]
public void CoreSchema_TakesAllThreePlaces()
{
XmlRouteSchema.Validate(XDocument.Parse(ThreePlaces, LoadOptions.SetLineInfo), ElementRegistry.CreateDefault())
.Should().BeEmpty();
}
[Fact]
public void Schema_TakesTheStep_AndKeepsTheDeclaration()
{
var problems = XmlRouteSchema.Validate(XDocument.Parse($$"""
""", LoadOptions.SetLineInfo), ElementRegistry.CreateDefault());
problems.Should().BeEmpty();
}
[Fact]
public void Schema_RefusesADeclarationChildOnTheStep()
{
var problems = XmlRouteSchema.Validate(XDocument.Parse("""
""", LoadOptions.SetLineInfo), ElementRegistry.CreateDefault());
problems.Should().NotBeEmpty("the step takes attributes only; belongs to a declaration");
}
// ── code generation ──────────────────────────────────────────────────────
[Fact]
public void Print_IsTheBeanSugar_OrTheUriWhenTheStepSetsProperties()
{
var code = CodeOf($$"""
""");
code.Should().Contain(".Bean(\"#greeter\", \"Greet\")");
code.Should().Contain(".Bean(\"#greeter\")");
code.Should().Contain($".To(\"bean://{Greeter}?method=Greet&Prefix=x\")");
}
[Fact]
public void Print_ProducesTheSameUri_TheLoaderBuilds()
{
const string xml = """
""";
_context.AddXmlRoutesFromContent(xml);
var builder = _context.RouteBuilders.Single();
if (!builder.IsBuilt)
builder.InternalBuild(_context);
var loaded = builder.Definitions.Single().Outputs.OfType().Single().Uri;
EndpointUriParser.Parse(loaded).NormalizedKey
.Should().Be(EndpointUriParser.Parse(Components.Bean.BeanComponent.UriFor("#greeter", "Greet")).NormalizedKey);
CodeOf(xml).Should().Contain(".Bean(\"#greeter\", \"Greet\")");
}
}