Path: redb-route/tests/redb.Route.Tests.Xml/XmlJsonMarkupTests.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/XmlJsonMarkupTests.cs
----
using System.Text;
using System.Text.Json.Nodes;
using System.Xml.Linq;
using AwesomeAssertions;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.DataFormats.XmlJson;
using redb.Route.Xml;
namespace redb.Route.Tests.Xml;
///
/// The xmljson format in markup and in the DSL (step S7): the default instance under the name xmljson, a named
/// instance with its options in context.xml, the per-step type= form, both directions, a schema path
/// resolved through the context's resource resolver (the package root), and the content types it writes.
///
public sealed class XmlJsonMarkupTests : IAsyncLifetime
{
private static readonly string FormatType =
$"{typeof(XmlJsonDataFormat).FullName}, {typeof(XmlJsonDataFormat).Assembly.GetName().Name}";
private readonly string _package = Path.Combine(Path.GetTempPath(), "redb-xmljson-pkg-" + Guid.NewGuid().ToString("N"));
private readonly RouteContext _context = new();
public Task InitializeAsync()
{
Directory.CreateDirectory(Path.Combine(_package, "schemas"));
File.WriteAllText(Path.Combine(_package, "schemas", "order.xsd"), """
""");
// The package root only the context's resolver knows, as a Tsak package has it.
_context.AddService(typeof(IRouteResourceResolver), new RouteResourceResolver { ResourceRoot = _package });
return Task.CompletedTask;
}
public async Task DisposeAsync()
{
await _context.DisposeAsync();
Directory.Delete(_package, recursive: true);
}
private void LoadContext(string inner)
=> new XmlContextLoader(_context).Load(XDocument.Parse($"""
{inner}
""", LoadOptions.SetLineInfo), "context.xml");
private async Task Run(string step, object body)
{
IMessage? seen = null;
_context.AddRoutes(r => r.From("direct://xj-out").Process(e => seen = e.In));
_context.AddXmlRoutesFromContent($"""
{step}
""");
await _context.Start();
var producer = _context.GetEndpoint("direct://xj-in").CreateProducer();
await producer.Start();
await producer.Process(new Exchange(new Message(body)));
return seen!;
}
private static string Text(object? body) => body is byte[] bytes ? Encoding.UTF8.GetString(bytes) : body?.ToString() ?? "";
[Fact]
public async Task TheDefaultInstance_AnswersToXmljson()
{
_context.AddXmlJsonDataFormat();
var message = await Run("""""", "12");
Text(message.Body).Should().Be("""{"o":{"i":["1","2"]}}""");
message.ContentType.Should().Be("application/json");
}
[Fact]
public async Task ANamedInstanceInContextXml_ReadsItsSchemaFromThePackage()
{
LoadContext($"""""");
var message = await Run("""""", "7a");
Text(message.Body).Should().Be("""{"Order":{"Id":7,"Tag":["a"]}}""", "the schema says Id is an int and Tag repeats");
}
[Fact]
public async Task ThePerStepForm_TakesItsOptions()
{
var message = await Run($"""""",
"""- 1
t""");
Text(message.Body).Should().Be("""{"r":{"Items":{"Item":["1"]},"Tag":["t"]}}""");
}
[Fact]
public async Task Unmarshal_JsonToXml_WithRootAndPrefixes()
{
var message = await Run($"""""", """{"Id":"1","Tag":["a","b"]}""");
Text(message.Body).Should().Be("""1ab""");
message.ContentType.Should().Be("application/xml");
}
[Fact]
public async Task Unmarshal_XmlToTheJsonTree()
{
var message = await Run($"""""", "1");
message.Body.Should().BeAssignableTo().Which["o"]!["a"]!.GetValue().Should().Be("1");
}
[Fact]
public async Task TheDsl_BothDirections()
{
string? json = null, xml = null;
_context.AddRoutes(r => r.From("direct://dsl").MarshalXmlJson(o => o.Arrays.Add("//i"))
.Process(e => json = Text(e.In.Body))
.UnmarshalXmlJson()
.Process(e => xml = Text(e.In.Body)));
await _context.Start();
var producer = _context.GetEndpoint("direct://dsl").CreateProducer();
await producer.Start();
await producer.Process(new Exchange(new Message("1")));
json.Should().Be("""{"o":{"i":["1"]}}""");
xml.Should().Be("""1""");
}
[Fact]
public void AMalformedPrefixesOption_IsAnErrorOfTheDocument()
{
var act = () => LoadContext($"""""");
act.Should().Throw().Where(e => e.ToString().Contains("is not prefix=uri"));
}
[Fact]
public void TheSerializerOptions_RoundTripTheirStringForms()
{
var format = new XmlJsonDataFormat { Arrays = " //Item , /Order/Tag ", Prefixes = "o=urn:orders, x = urn:x" };
format.Options.Arrays.Should().Equal("//Item", "/Order/Tag");
format.Options.Prefixes.Should().BeEquivalentTo(new Dictionary { ["o"] = "urn:orders", ["x"] = "urn:x" });
format.Arrays.Should().Be("//Item,/Order/Tag");
}
}