using Shouldly; using Xunit; using ZB.MOM.WW.OtOpcUa.Configuration.Entities; using ZB.MOM.WW.OtOpcUa.Configuration.Enums; using ZB.MOM.WW.OtOpcUa.Configuration.Validation; using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections; namespace ZB.MOM.WW.OtOpcUa.Configuration.Tests.DataConnections; [Trait("Category", "Unit")] [Trait("Feature", "GrpcData")] [Trait("Phase", "P02")] public sealed class GrpcDataMappingValidationTests { internal static DraftSnapshot ValidDraft(bool collision = false) => new() { GenerationId = 0, ClusterId = "cluster", ClusterNodes = [Node("node-a"), Node("node-b")], RawFolders = collision ? [new RawFolder { RawFolderId = "parent", ClusterId = "cluster", Name = "parent" }, new RawFolder { RawFolderId = "child", ClusterId = "cluster", ParentRawFolderId = "parent", Name = "remote" }] : [new RawFolder { RawFolderId = "parent", ClusterId = "cluster", Name = "parent" }], DataConnections = [new DataConnection { DataConnectionId = "connection", ClusterId = "cluster", Enabled = true, ConsumerNodeIds = ["node-a"], TargetMode = DataTargetMode.Node, ExpectedClusterId = "cluster", ExpectedNodeId = "node-b", Endpoints = ["https://node-b:443"], SecretReference = "secret:data/key", SecretScopeClusterId = "cluster", AllowedOperations = DataOperations.Browse | DataOperations.Read }], RemoteNamespaceMappings = [new RemoteNamespaceMapping { RemoteNamespaceMappingId = "mapping", ClusterId = "cluster", DataConnectionId = "connection", Enabled = true, ConsumerNodeIds = ["node-a"], Realm = DataRealm.Raw, RemoteRealm = DataRealm.Raw, LocalParentKind = "RawFolder", LocalParentId = "parent", BranchName = "remote", RemoteRootEntityId = "remote-root", RemoteNamespaceUri = "https://zb.com/otopcua/raw", RemoteIdentifierKind = "string", RemoteIdentifier = "root", AllowedOperations = DataOperations.Browse }] }; private static ClusterNode Node(string id) => new() { NodeId = id, ClusterId = "cluster", Host = id, ApplicationUri = "urn:" + id, CreatedBy = "test" }; [Fact] public void Mapping_requires_subset_same_realm_and_owned_parent() { var draft = ValidDraft(); var mapping = draft.RemoteNamespaceMappings[0]; mapping.ConsumerNodeIds.Add("node-b"); mapping.RemoteRealm = DataRealm.Uns; mapping.LocalParentId = "missing"; var errors = DataConnectionValidator.Validate(draft); errors.ShouldContain(e => e.Code == "DataMappingConsumerScopeInvalid"); errors.ShouldContain(e => e.Code == "DataMappingRealmMismatch"); errors.ShouldContain(e => e.Code == "DataMappingParentInvalid"); } [Fact] public void Mapping_branch_collisions_and_unsupported_operations_are_rejected() { var draft = ValidDraft(collision: true); draft.RemoteNamespaceMappings[0].AllowedOperations = DataOperations.Write; var errors = DataConnectionValidator.Validate(draft); errors.ShouldContain(e => e.Code == "DataMappingNameCollision"); errors.ShouldContain(e => e.Code == "DataMappingOperationsInvalid"); } [Fact] public void Same_cluster_mapping_cannot_mount_its_ancestor_and_unknown_types_fail() { var draft = ValidDraft(); var mapping = draft.RemoteNamespaceMappings[0]; mapping.RemoteRootEntityId = "parent"; mapping.ExpectedValueKind = (DataValueKind)999; var errors = DataConnectionValidator.Validate(draft); errors.ShouldContain(e => e.Code == "DataMappingProjectionLoop"); errors.ShouldContain(e => e.Code == "DataMappingTypeInvalid"); } [Fact] public void Branch_names_and_namespace_uri_use_canonical_realm_rules() { var draft = ValidDraft(); var mapping = draft.RemoteNamespaceMappings[0]; mapping.BranchName = "bad/segment"; mapping.RemoteNamespaceUri = "https://zb.com/otopcua/uns"; var errors = DataConnectionValidator.Validate(draft); errors.ShouldContain(e => e.Code == "DataMappingNameInvalid"); errors.ShouldContain(e => e.Code == "DataMappingRootInvalid"); } /// /// An EMPTY DisplayNameOverride counts as "no override" (the rule both compose seams apply via /// EquipmentReferenceMap.EffectiveName): the reference materialises under its tag's name, so a mapping /// branch of that name collides. A bare ?? kept the empty string and never saw the collision. /// [Theory] [InlineData("", "Speed", true)] // empty override ⇒ the reference is "Speed" ⇒ collision [InlineData(null, "Speed", true)] [InlineData("Other", "Speed", false)] // control: a real override moves the reference's name away [InlineData("", "Free", false)] // control: no sibling of that name public void Equipment_mapping_branch_collides_with_a_reference_by_its_effective_name(string? overrideName, string branch, bool collides) { var baseDraft = ValidDraft(); var draft = new DraftSnapshot { GenerationId = 0, ClusterId = "cluster", ClusterNodes = baseDraft.ClusterNodes, DataConnections = baseDraft.DataConnections, RemoteNamespaceMappings = baseDraft.RemoteNamespaceMappings, UnsAreas = [new UnsArea { UnsAreaId = "area", ClusterId = "cluster", Name = "Area" }], UnsLines = [new UnsLine { UnsLineId = "line", UnsAreaId = "area", Name = "Line" }], Equipment = [new Equipment { EquipmentId = "eq", UnsLineId = "line", Name = "Eq", MachineCode = "EQ" }], Tags = [new Tag { TagId = "tag", DeviceId = "dev", Name = "Speed", DataType = "Double", AccessLevel = TagAccessLevel.Read, TagConfig = "{}" }], UnsTagReferences = [new UnsTagReference { UnsTagReferenceId = "ref", EquipmentId = "eq", TagId = "tag", DisplayNameOverride = overrideName }], }; var mapping = draft.RemoteNamespaceMappings[0]; mapping.Realm = DataRealm.Uns; mapping.RemoteRealm = DataRealm.Uns; mapping.RemoteNamespaceUri = "https://zb.com/otopcua/uns"; mapping.LocalParentKind = "Equipment"; mapping.LocalParentId = "eq"; mapping.BranchName = branch; var errors = DataConnectionValidator.Validate(draft); if (collides) errors.ShouldContain(e => e.Code == "DataMappingNameCollision"); else errors.ShouldNotContain(e => e.Code == "DataMappingNameCollision"); // Positive control that the parent resolved (the collision rule ran against a real equipment). errors.ShouldNotContain(e => e.Code == "DataMappingParentInvalid"); } }