diff --git a/src/ZB.MOM.WW.MxGateway.Worker.Tests/Conversion/MxStatusDetailTextTests.cs b/src/ZB.MOM.WW.MxGateway.Worker.Tests/Conversion/MxStatusDetailTextTests.cs new file mode 100644 index 00000000..484c103c --- /dev/null +++ b/src/ZB.MOM.WW.MxGateway.Worker.Tests/Conversion/MxStatusDetailTextTests.cs @@ -0,0 +1,133 @@ +using System.Collections.Generic; +using System.Linq; +using ZB.MOM.WW.MxGateway.Worker.Conversion; + +namespace ZB.MOM.WW.MxGateway.Worker.Tests.Conversion; + +/// +/// Pins the table: the text each known MXAccess +/// MXSTATUS_PROXY.detail code maps to, and that every other code maps to the empty +/// string. The text reaches clients as MxStatusProxy.DiagnosticText, so an accidental +/// edit, a dropped row or a duplicate code is a visible wire change (mxaccessgw#178). +/// +public sealed class MxStatusDetailTextTests +{ + /// Every known detail code with its expected text. + public static TheoryData KnownDetails => new() + { + { 16, "Request timed out" }, + { 17, "Platform communication error" }, + { 18, "Invalid platform ID" }, + { 19, "Invalid engine ID" }, + { 20, "Engine communication error" }, + { 21, "Invalid reference" }, + { 22, "No Galaxy Repository" }, + { 23, "Invalid object ID" }, + { 24, "Object signature mismatch" }, + { 25, "Invalid primitive ID" }, + { 26, "Invalid attribute ID" }, + { 27, "Invalid property ID" }, + { 28, "Index out of range" }, + { 29, "Data out of range" }, + { 30, "Incorrect data type" }, + { 31, "Attribute not readable" }, + { 32, "Attribute not writeable" }, + { 33, "Write access denied" }, + { 34, "Unknown error" }, + { 36, "Wrong data type" }, + { 37, "Wrong number of dimensions" }, + { 38, "Invalid index" }, + { 39, "Index out of order" }, + { 40, "Dimension does not exist" }, + { 41, "Conversion not supported" }, + { 42, "Unable to convert string" }, + { 43, "Overflow" }, + { 44, "Attribute signature mismatch" }, + { 47, "Nmx version mismatch" }, + { 48, "Nmx command not valid" }, + { 49, "Lmx version mismatch" }, + { 50, "Lmx command not valid" }, + { 56, "Secured Write" }, + { 57, "Verified Write" }, + { 60, "User did not have the necessary permissions to write" }, + { 61, "Verifier did not have the necessary permissions to verify" }, + { 541, "Conversion to intended data type is not supported" }, + { 542, "Unable to convert the input string to intended data type" }, + { 8017, "Object must be offscan to modify attributes that have an MxSecurityConfigure security classification" }, + }; + + // Upper bound of the exhaustive sweep. It covers every code in the table (the largest is + // 8017) with headroom, so a row added anywhere in that range fails the set-equality test + // until it is added to KnownDetails as well. + private const int SweepUpperBound = 20_000; + + /// Verifies each known code maps to its documented text. + /// MXAccess status detail code. + /// Expected description. + [Theory] + [MemberData(nameof(KnownDetails))] + public void Lookup_WithKnownDetail_ReturnsText(int detail, string expected) + { + Assert.Equal(expected, MxStatusDetailText.Lookup(detail)); + } + + /// Verifies codes outside the table, including the gaps inside it, map to empty. + /// MXAccess status detail code with no entry. + [Theory] + [InlineData(0)] + [InlineData(1)] + [InlineData(15)] + [InlineData(35)] + [InlineData(45)] + [InlineData(46)] + [InlineData(51)] + [InlineData(55)] + [InlineData(58)] + [InlineData(59)] + [InlineData(62)] + [InlineData(540)] + [InlineData(543)] + [InlineData(8016)] + [InlineData(8018)] + [InlineData(-1)] + [InlineData(int.MinValue)] + [InlineData(int.MaxValue)] + public void Lookup_WithUnknownDetail_ReturnsEmpty(int detail) + { + Assert.Equal(string.Empty, MxStatusDetailText.Lookup(detail)); + } + + /// + /// Verifies the table holds exactly the pinned codes: sweeping the code range finds no + /// entry missing from and none extra. + /// + [Fact] + public void Lookup_AcrossCodeRange_ResolvesExactlyThePinnedCodes() + { + HashSet pinned = new(KnownDetails.Select(row => (int)row[0])); + List resolved = new(); + for (int detail = -16; detail <= SweepUpperBound; detail++) + { + if (MxStatusDetailText.Lookup(detail).Length > 0) + { + resolved.Add(detail); + } + } + + Assert.Equal(pinned.OrderBy(code => code), resolved); + } + + /// Verifies no entry is blank or padded, and no two codes share a text. + [Fact] + public void KnownDetails_HaveDistinctTrimmedNonEmptyText() + { + List texts = KnownDetails.Select(row => MxStatusDetailText.Lookup((int)row[0])).ToList(); + + Assert.All(texts, text => + { + Assert.False(string.IsNullOrWhiteSpace(text)); + Assert.Equal(text.Trim(), text); + }); + Assert.Equal(texts.Count, texts.Distinct().Count()); + } +}