Path: redb-route/tests/redb.Route.Tests.DataFormats/XmlJsonJsonToXmlSchemaTests.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.DataFormats/XmlJsonJsonToXmlSchemaTests.cs
----
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;
using AwesomeAssertions;
using redb.Route.DataFormats.XmlJson;
using redb.Route.DataFormats.XmlJson.Internal;
namespace redb.Route.Tests.DataFormats;
///
/// xmljson, JSON to XML by a schema (step S6): sequence order whatever the key order, choice branches, all, the
/// schema's namespaces and prefixes, lexical forms, strict errors with the JSON path, a wrapper only where the schema
/// has one. Every document written is validated against the schema.
///
public sealed class XmlJsonJsonToXmlSchemaTests
{
private const string Orders = """
""";
private static XmlSchemaSet Set(string xsd)
{
var set = new XmlSchemaSet();
using var reader = XmlReader.Create(new StringReader(xsd));
set.Add(null, reader);
set.Compile();
return set;
}
private static XDocument Convert(string json, string xsd = Orders, Action? configure = null)
{
var set = Set(xsd);
var options = new XmlJsonOptions { SchemaSet = set };
configure?.Invoke(options);
return new JsonToXml(options).Convert(Encoding.UTF8.GetBytes(json));
}
private static string Valid(XDocument document, string xsd = Orders)
{
var errors = new List();
document.Validate(Set(xsd), (_, e) => errors.Add(e.Message));
errors.Should().BeEmpty();
return document.Root!.ToString(SaveOptions.DisableFormatting);
}
private const string Shuffled = """
{"Order":{"Items":{"Item":[{"Qty":2,"Sku":"pen"}]},"Cash":"yes","Total":1e3,"@count":2,"Paid":true,"Id":7,"@channel":"web"}}
""";
[Fact]
public void SequenceOrder_NamespacesAndLexicalForms_FromTheSchema()
{
Valid(Convert(Shuffled)).Should().Be(
"""7true1000yespen2""");
}
[Fact]
public void Prefixes_FromTheOptions_OrFromTheJson()
{
Valid(Convert(Shuffled, configure: o => o.Prefixes["o"] = "urn:orders")).Should().StartWith("");
}
[Fact]
public void Root_NamesTheGlobalElement()
{
Valid(Convert("""{"Id":1,"Paid":false,"Total":1,"Card":"x","Items":{"Item":[{"Sku":"a","Qty":1}]}}""", configure: o => o.Root = "Order"))
.Should().StartWith("a1");
}
[Fact]
public void Nillable_IsXsiNil()
{
Valid(Convert("""{"Order":{"Id":1,"Paid":false,"Total":1,"Card":"x","Note":null,"Items":{"Item":[{"Sku":"a","Qty":1}]}}}"""))
.Should().Contain("").And.Contain("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
}
[Fact]
public void AStringStaysAsWritten()
{
Valid(Convert("""{"Order":{"Id":1,"Paid":false,"Total":"12.50","Card":"x","Code":"007","Items":{"Item":[{"Sku":"a","Qty":1}]}}}"""))
.Should().Contain("12.50").And.Contain("007");
}
[Theory]
[InlineData("""{"Order":{"Id":1,"Card":"x","Cash":"y"}}""", "xmljson: /Order has both 'Card' and 'Cash', two branches of one choice in the schema.")]
[InlineData("""{"Order":{"Id":1,"Extra":"x"}}""", "xmljson: /Order/Extra: the content of /Order in the schema has no place for 'Extra'.")]
[InlineData("""{"Order":{"@size":"1"}}""", "xmljson: /Order/@size: the schema declares no attribute 'size' here.")]
[InlineData("""{"Order":{"Id":[1,2]}}""", "xmljson: /Order/Id is an array of 2, and the schema allows one 'Id' here.")]
[InlineData("""{"Order":{"Id":null}}""", "xmljson: /Order/Id is null, and the schema does not declare it nillable.")]
[InlineData("""{"Order":{"Items":"x"}}""", "xmljson: /Order/Items holds a value, and the schema declares element content: an object is expected.")]
[InlineData("""{"Other":{}}""", "xmljson: the root 'Other' is not a global element of the schema.")]
public void WhatTheSchemaDoesNotAllow_IsAnError(string json, string message)
{
var act = () => Convert(json);
act.Should().Throw().WithMessage(message);
}
[Fact]
public void UnqualifiedLocalElements_HaveNoNamespace()
{
const string xsd = """
""";
Valid(Convert("""{"r":{"a":5}}""", xsd), xsd).Should().Be("""5""");
}
[Fact]
public void UnderAWildcard_ThePlainReverseApplies()
{
const string xsd = """
""";
Valid(Convert("""{"r":{"free":{"x":["1","2"]},"id":3}}""", xsd), xsd).Should().Be("312");
}
[Fact]
public void XmlToJsonToXml_RoundTripsByTheSchema()
{
const string xml = """7true12.50va1""";
var options = new XmlJsonOptions { SchemaSet = Set(Orders) };
var toJson = new XmlToJson(options);
var json = toJson.Write(toJson.Convert(Encoding.UTF8.GetBytes(xml)));
Valid(new JsonToXml(options).Convert(json)).Should().Be(xml);
}
}