using System.Text.Json; using ZB.MOM.WW.ScadaBridge.Commons.Messages.Management; namespace ZB.MOM.WW.ScadaBridge.Commons.Tests.Messages; /// /// The additive optional SiteIdentifier trailing param on the /// Browse/Verify/Cert-trust management commands. Verifies backward compatibility (legacy /// JSON without the field deserializes to null) and round-trip survival when set. /// public class BrowseVerifyCertSiteIdentifierTests { private static readonly JsonSerializerOptions Options = new() { PropertyNameCaseInsensitive = true }; // ── Backward compatibility: legacy JSON (no SiteIdentifier) → null ── /// Verifies that browse node command legacy json site identifier null. [Fact] public void BrowseNodeCommand_LegacyJson_SiteIdentifierNull() { var json = """{"ConnectionName":"conn","ParentNodeId":"ns=2;s=Node","ContinuationToken":null}"""; var cmd = JsonSerializer.Deserialize(json, Options)!; Assert.Null(cmd.SiteIdentifier); Assert.Equal("conn", cmd.ConnectionName); } /// Verifies that search address space command legacy json site identifier null. [Fact] public void SearchAddressSpaceCommand_LegacyJson_SiteIdentifierNull() { var json = """{"ConnectionName":"conn","Query":"pump","MaxDepth":5,"MaxResults":100}"""; var cmd = JsonSerializer.Deserialize(json, Options)!; Assert.Null(cmd.SiteIdentifier); Assert.Equal("pump", cmd.Query); } /// Verifies that verify endpoint command legacy json site identifier null. [Fact] public void VerifyEndpointCommand_LegacyJson_SiteIdentifierNull() { var json = """{"ConnectionName":"conn","Protocol":"OpcUa","ConfigJson":"{}"}"""; var cmd = JsonSerializer.Deserialize(json, Options)!; Assert.Null(cmd.SiteIdentifier); Assert.Equal("OpcUa", cmd.Protocol); } /// Verifies that trust server cert command legacy json site identifier null. [Fact] public void TrustServerCertCommand_LegacyJson_SiteIdentifierNull() { var json = """{"ConnectionName":"conn","DerBase64":"AAAA","Thumbprint":"ABCD"}"""; var cmd = JsonSerializer.Deserialize(json, Options)!; Assert.Null(cmd.SiteIdentifier); Assert.Equal("ABCD", cmd.Thumbprint); } /// Verifies that remove server cert command legacy json site identifier null. [Fact] public void RemoveServerCertCommand_LegacyJson_SiteIdentifierNull() { var json = """{"Thumbprint":"ABCD"}"""; var cmd = JsonSerializer.Deserialize(json, Options)!; Assert.Null(cmd.SiteIdentifier); Assert.Equal("ABCD", cmd.Thumbprint); } /// Verifies that list server certs command legacy json site identifier null. [Fact] public void ListServerCertsCommand_LegacyJson_SiteIdentifierNull() { var json = "{}"; var cmd = JsonSerializer.Deserialize(json, Options)!; Assert.Null(cmd.SiteIdentifier); } // ── Round-trip: SiteIdentifier set survives serialize → deserialize ── /// Verifies that browse node command round trip site identifier survives. [Fact] public void BrowseNodeCommand_RoundTrip_SiteIdentifierSurvives() { var original = new BrowseNodeCommand("conn", "ns=2;s=Node", "tok", "SITE-01"); var json = JsonSerializer.Serialize(original, Options); var round = JsonSerializer.Deserialize(json, Options)!; Assert.Equal("SITE-01", round.SiteIdentifier); Assert.Equal(original, round); } /// Verifies that search address space command round trip site identifier survives. [Fact] public void SearchAddressSpaceCommand_RoundTrip_SiteIdentifierSurvives() { var original = new SearchAddressSpaceCommand("conn", "pump", 5, 100, "SITE-01"); var json = JsonSerializer.Serialize(original, Options); var round = JsonSerializer.Deserialize(json, Options)!; Assert.Equal("SITE-01", round.SiteIdentifier); Assert.Equal(original, round); } /// Verifies that verify endpoint command round trip site identifier survives. [Fact] public void VerifyEndpointCommand_RoundTrip_SiteIdentifierSurvives() { var original = new VerifyEndpointCommand("conn", "OpcUa", "{}", "SITE-01"); var json = JsonSerializer.Serialize(original, Options); var round = JsonSerializer.Deserialize(json, Options)!; Assert.Equal("SITE-01", round.SiteIdentifier); Assert.Equal(original, round); } /// Verifies that trust server cert command round trip site identifier survives. [Fact] public void TrustServerCertCommand_RoundTrip_SiteIdentifierSurvives() { var original = new TrustServerCertCommand("conn", "AAAA", "ABCD", "SITE-01"); var json = JsonSerializer.Serialize(original, Options); var round = JsonSerializer.Deserialize(json, Options)!; Assert.Equal("SITE-01", round.SiteIdentifier); Assert.Equal(original, round); } /// Verifies that remove server cert command round trip site identifier survives. [Fact] public void RemoveServerCertCommand_RoundTrip_SiteIdentifierSurvives() { var original = new RemoveServerCertCommand("ABCD", "SITE-01"); var json = JsonSerializer.Serialize(original, Options); var round = JsonSerializer.Deserialize(json, Options)!; Assert.Equal("SITE-01", round.SiteIdentifier); Assert.Equal(original, round); } /// Verifies that list server certs command round trip site identifier survives. [Fact] public void ListServerCertsCommand_RoundTrip_SiteIdentifierSurvives() { var original = new ListServerCertsCommand("SITE-01"); var json = JsonSerializer.Serialize(original, Options); var round = JsonSerializer.Deserialize(json, Options)!; Assert.Equal("SITE-01", round.SiteIdentifier); Assert.Equal(original, round); } }