Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataLocalExportCatalogTests.cs
T
2026-11-23 09:55:03 -05:00

81 lines
5.0 KiB
C#

using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
using ZB.MOM.WW.OtOpcUa.Runtime.DataConnections;
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
using ZB.MOM.WW.OtOpcUa.Configuration.Enums;
using ZB.MOM.WW.OtOpcUa.Configuration.Validation;
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.DataConnections;
[Trait("Category", "Unit"), Trait("Feature", "GrpcData"), Trait("Phase", "P04")]
public sealed class GrpcDataLocalExportCatalogTests
{
[Fact]
public void Deployed_origin_catalog_uses_actual_Uns_folder_ids_and_one_backing_source_for_both_realms()
{
using var fixture = new LocalDataFixture();
var snapshot = new DraftSnapshot
{
GenerationId = 1, ClusterId = "cluster",
ServerClusters = [new() { ClusterId = "cluster", Name = "Cluster", Enterprise = "Enterprise", Site = "Site", CreatedBy = "test", RedundancyMode = RedundancyMode.None }],
RawFolders = [new() { RawFolderId = "folder", ClusterId = "cluster", Name = "Plant" }],
DriverInstances = [new() { DriverInstanceId = "driver", ClusterId = "cluster", RawFolderId = "folder", Name = "Driver", DriverType = "ModbusTcp", DriverConfig = "{}" }],
Devices = [new() { DeviceId = "device", DriverInstanceId = "driver", Name = "Device", DeviceConfig = "{}" }],
Tags = [new() { TagId = "tag", DeviceId = "device", Name = "Speed", DataType = "Int32", AccessLevel = TagAccessLevel.ReadWrite, TagConfig = "{}" }],
UnsAreas = [new() { UnsAreaId = "area-id", ClusterId = "cluster", Name = "Area" }],
UnsLines = [new() { UnsLineId = "line-id", UnsAreaId = "area-id", Name = "Line" }],
Equipment = [new() { EquipmentId = "equipment-id", UnsLineId = "line-id", Name = "Equipment", MachineCode = "E" }],
UnsTagReferences = [new() { UnsTagReferenceId = "reference", EquipmentId = "equipment-id", TagId = "tag" }],
DataExports = [new() { DataExportId = "raw", ClusterId = "cluster", Enabled = true, Realm = DataRealm.Raw,
RootKind = "Cluster", RootEntityId = "cluster", PrincipalId = "peer", AllowedOperations = DataOperations.Browse | DataOperations.Metadata | DataOperations.Read },
new() { DataExportId = "uns", ClusterId = "cluster", Enabled = true, Realm = DataRealm.Uns,
RootKind = "Cluster", RootEntityId = "cluster", PrincipalId = "peer", AllowedOperations = DataOperations.Browse | DataOperations.Metadata | DataOperations.Read }]
};
var catalog = LocalExportCatalog.Compile(snapshot, fixture.Serving, fixture.Activation.CaptureAuthority());
var raw = catalog.Nodes.Single(x => x.EntityKind == "RawTag");
var uns = catalog.Nodes.Single(x => x.EntityKind == "UnsTagReference");
raw.Source.ShouldBe(uns.Source);
raw.Source.ShouldBe(SourceKey.ForDriver("cluster", "driver", "Plant/Driver/Device/Speed", "node-a", "serving-1"));
var area = catalog.Nodes.Single(x => x.EntityKind == "UnsArea");
area.Metadata.View.Address.Identifier.ShouldBe("area-id");
area.Scope.Segments.ShouldBe(["Area"]);
uns.Metadata.View.Address.Identifier.ShouldBe("Area/Line/Equipment/Speed");
catalog.Nodes.ShouldNotContain(x => x.EntityKind == "RemoteNamespaceMapping");
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void Catalog_rejects_projected_or_foreign_nodes_as_origin_local_exports(bool projected)
{
using var fixture = new LocalDataFixture();
var valid = fixture.Catalog.Nodes.First();
var invalidView = projected
? valid.Metadata.View with { MappingId = "remote", MappingRevision = 1 }
: valid.Metadata.View with { Address = valid.Metadata.View.Address with { OriginClusterId = "foreign" } };
var invalid = valid with { Metadata = valid.Metadata with { View = invalidView } };
Should.Throw<ArgumentException>(() => new LocalExportCatalog("cluster", fixture.Serving,
fixture.Activation.CaptureAuthority(), [invalid], []));
}
[Fact]
public void Catalog_token_is_invalidated_by_a_new_desired_revision()
{
using var fixture = new LocalDataFixture();
fixture.Catalog.IsActive.ShouldBeTrue();
fixture.Activation.Begin("next");
fixture.Catalog.IsActive.ShouldBeFalse();
}
[Fact]
public async Task Catalog_metadata_and_read_batch_limits_are_checked_before_response_allocation()
{
using var fixture = new LocalDataFixture();
await using var connection = fixture.Connection();
var budget = LocalDataFixture.Budget with { MaxItems = 1 };
await Should.ThrowAsync<ArgumentOutOfRangeException>(() => connection.GetMetadataAsync(new([fixture.Raw, fixture.Uns], budget), TestContext.Current.CancellationToken));
await Should.ThrowAsync<ArgumentOutOfRangeException>(() => connection.ReadAsync(new([new("a", fixture.Raw, null), new("b", fixture.Uns, null)], budget), TestContext.Current.CancellationToken));
}
}