Merge A1 keys/placements/ids into A1 core

This commit is contained in:
Joseph Doherty
2026-11-24 10:02:34 -05:00
14 changed files with 1158 additions and 9 deletions
@@ -0,0 +1,106 @@
using System.Text;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
namespace ZB.MOM.WW.OtOpcUa.Core.Abstractions.Tests.DataConnections;
/// <summary>A1 (§5.2, N4): the imported-condition identifier, its children and the condition namespace.</summary>
[Trait("Category", "Unit"), Trait("Feature", "GrpcData"), Trait("Phase", "A1")]
public sealed class GrpcDataImportedConditionIdsTests
{
// 43 base64url chars of a 32-byte value (last char low bits zero).
private static readonly string Key = Convert.ToBase64String(Enumerable.Range(1, 32).Select(x => (byte)(x * 7)).ToArray())
.TrimEnd('=').Replace('+', '-').Replace('/', '_');
private static readonly string OtherKey = Convert.ToBase64String(Enumerable.Repeat((byte)0xFB, 32).ToArray())
.TrimEnd('=').Replace('+', '-').Replace('/', '_');
[Fact]
public void Condition_namespace_is_a_third_mapped_namespace_and_not_a_realm_namespace()
{
ImportedConditionIds.NamespaceUri.ShouldBe("https://zb.com/otopcua/mapped/conditions");
ImportedConditionIds.IsConditionNamespace(ImportedConditionIds.NamespaceUri).ShouldBeTrue();
ImportedConditionIds.IsConditionNamespace(ProjectedDataIds.RawNamespaceUri).ShouldBeFalse();
ImportedConditionIds.IsConditionNamespace(ProjectedDataIds.UnsNamespaceUri).ShouldBeFalse();
ImportedConditionIds.IsConditionNamespace(null).ShouldBeFalse();
// The variable classifier must not claim it: a caller dispatches the condition namespace first (RealmOf trap).
ProjectedDataIds.IsMappedNamespace(ImportedConditionIds.NamespaceUri).ShouldBeFalse();
}
[Fact]
public void Identifier_round_trips_and_is_keyed_on_connection_and_key_only()
{
Key.Length.ShouldBe(43);
var id = ImportedConditionIds.Identifier("conn-1", Key);
id.ShouldStartWith("c1.");
id.ShouldNotContain('/');
ImportedConditionIds.TryDecodeIdentifier(id, out var connection, out var key).ShouldBeTrue();
connection.ShouldBe("conn-1");
key.ShouldBe(Key);
ImportedConditionIds.Identifier("conn-1", Key).ShouldBe(id); // deterministic
ImportedConditionIds.Identifier("conn-2", Key).ShouldNotBe(id);
ImportedConditionIds.Identifier("conn-1", OtherKey).ShouldNotBe(id);
// Disjoint from the variable scheme.
ProjectedDataIds.TryDecodeIdentifier(id, out _, out _).ShouldBeFalse();
}
[Theory]
[InlineData("EnabledState")]
[InlineData("EnabledState/Id")]
[InlineData("ShelvingState/UnshelveTime")]
public void Child_round_trips_and_splits_at_the_first_separator(string browsePath)
{
var id = ImportedConditionIds.Identifier("conn/with/slash", Key);
var child = ImportedConditionIds.ChildIdentifier(id, browsePath);
child.ShouldBe(id + "/" + browsePath);
ImportedConditionIds.TryParse(child, out var condition, out var connection, out var key, out var path).ShouldBeTrue();
condition.ShouldBe(id);
connection.ShouldBe("conn/with/slash"); // a slash inside the JSON payload never reaches the identifier text
key.ShouldBe(Key);
path.ShouldBe(browsePath);
ImportedConditionIds.TryParse(id, out condition, out _, out _, out path).ShouldBeTrue();
condition.ShouldBe(id);
path.ShouldBeNull();
ImportedConditionIds.TryDecodeIdentifier(child, out _, out _).ShouldBeFalse(); // a child is not a condition
}
[Fact]
public void Malformed_identifiers_are_rejected()
{
var id = ImportedConditionIds.Identifier("conn-1", Key);
static string B64(string json) => Convert.ToBase64String(Encoding.UTF8.GetBytes(json)).TrimEnd('=').Replace('+', '-').Replace('/', '_');
string?[] bad =
[
null, "", "c1.", "c1", "p1." + id[3..], "C1." + id[3..], id + "=", id[..^1] + "*", id + "/", id + "//X",
id + "/A//B", id + "/A/", id + "/\u0001", "c1." + B64("{}"), "c1." + B64("[1]"), "c1.!!!!",
"c1." + B64($"{{\"ConnectionId\":\"conn-1\",\"ConditionKey\":\"short\"}}"),
"c1." + B64($"{{\"ConnectionId\":\" \",\"ConditionKey\":\"{Key}\"}}"),
// non-canonical: same values, different JSON spelling (whitespace)
"c1." + B64($"{{ \"ConnectionId\":\"conn-1\",\"ConditionKey\":\"{Key}\"}}"),
"c1." + new string('A', ImportedConditionIds.MaximumIdentifierLength)
];
foreach (var candidate in bad)
{
ImportedConditionIds.TryDecodeIdentifier(candidate, out _, out _).ShouldBeFalse(candidate);
ImportedConditionIds.TryParse(candidate, out _, out _, out _, out _).ShouldBeFalse(candidate);
}
Should.Throw<ArgumentException>(() => ImportedConditionIds.ChildIdentifier("c1.bogus", "X"));
Should.Throw<ArgumentException>(() => ImportedConditionIds.ChildIdentifier(id, ""));
Should.Throw<ArgumentException>(() => ImportedConditionIds.ChildIdentifier(id, "/X"));
}
[Fact]
public void Construction_refuses_a_malformed_key_or_connection()
{
Should.Throw<ArgumentException>(() => ImportedConditionIds.Identifier("conn", "not-a-key"));
Should.Throw<ArgumentException>(() => ImportedConditionIds.Identifier("conn", Key + "A"));
// 43 chars whose trailing bits are non-zero is not a canonical 32-byte encoding.
Should.Throw<ArgumentException>(() => ImportedConditionIds.Identifier("conn", Key[..^1] + "B"));
Should.Throw<ArgumentException>(() => ImportedConditionIds.Identifier(" ", Key));
Should.Throw<ArgumentOutOfRangeException>(() => ImportedConditionIds.Identifier(new string('c', 257), Key));
Should.Throw<ArgumentOutOfRangeException>(() => ImportedConditionIds.Identifier("a\nb", Key));
ImportedConditionIds.IsWellFormedConditionKey(Key).ShouldBeTrue();
ImportedConditionIds.IsWellFormedConditionKey(OtherKey).ShouldBeTrue();
ImportedConditionIds.IsWellFormedConditionKey(Key.Replace(Key[0], '+')).ShouldBeFalse();
}
}