96 lines
4.2 KiB
C#
96 lines
4.2 KiB
C#
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");
|
|
}
|
|
|
|
}
|