using Shouldly; using Xunit; using ZB.MOM.WW.OtOpcUa.Commons.DataConnections; using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections; namespace ZB.MOM.WW.OtOpcUa.Commons.Tests.DataConnections; [Trait("Feature", "GrpcData"), Trait("Phase", "P01"), Trait("Category", "Unit")] public sealed class GrpcDataCompactCodecTests { [Fact] public void Sample_preserves_status_timestamps_and_provenance() { using var sample = new DataSample( DataValue.FromScalar(DataValueKind.UInt64, ulong.MaxValue), 0x81234567, null, DateTimeOffset.UnixEpoch, new ServingIdentity("n", "i", "primary", 4, 5), 8 ); using var decoded = DataProtoMapper.DecodeSample(DataProtoMapper.EncodeSample(sample)); decoded.StatusCode.ShouldBe(sample.StatusCode); decoded.SourceTimestampUtc.ShouldBeNull(); decoded.ServerTimestampUtc.ShouldBe(sample.ServerTimestampUtc); decoded.Serving.ShouldBe(sample.Serving); decoded.Version.ShouldBe(8); } [Fact] public void Compact_and_fallback_preserve_explicit_absence_and_reject_stale_handles() { var serving = new ServingIdentity("n", "i", "primary", 1, 2); var h1 = new DataItemHandle("s", 1, 1, 1); var h2 = new DataItemHandle("s", 1, 2, 1); using var frame = new DataSubscriptionValues( "s", 1, 1, false, new[] { new DataSubscriptionValue( h1, new DataSample(DataValue.Null(), 12, DateTimeOffset.UnixEpoch, null, serving, 2) ), new DataSubscriptionValue( h2, new DataSample( DataValue.FromBytes(Array.Empty()), 13, null, null, serving, 3 ) ), } ); var handles = new Dictionary { [1] = h1, [2] = h2 }; using var compact = DataProtoMapper.DecodeValues( DataProtoMapper.EncodeValues(frame, true), handles, 10, 10000, serving ); using var fallback = DataProtoMapper.DecodeValues( DataProtoMapper.EncodeValues(frame, false), handles, 10, 10000, serving ); compact.Items[1].Sample.SourceTimestampUtc.ShouldBeNull(); compact.Items[1].Sample.StatusCode.ShouldBe(fallback.Items[1].Sample.StatusCode); compact.Items[1].Sample.Value.Kind.ShouldBe(fallback.Items[1].Sample.Value.Kind); handles[2] = h2 with { ItemRevision = 2 }; Should.Throw(() => DataProtoMapper.DecodeValues( DataProtoMapper.EncodeValues(frame, true), handles, 10, 10000, serving ) ); } [Fact] public void Compact_batch_rejects_mixed_provenance() { var first = new ServingIdentity("a", "i", "primary", 1, 2); var second = first with { NodeId = "b" }; using var frame = new DataSubscriptionValues( "s", 1, 1, false, new[] { new DataSubscriptionValue( new DataItemHandle("s", 1, 1, 1), new DataSample(DataValue.Null(), 0, null, null, first, 1) ), new DataSubscriptionValue( new DataItemHandle("s", 1, 2, 1), new DataSample(DataValue.Null(), 0, null, null, second, 1) ), } ); Should.Throw(() => DataProtoMapper.EncodeValues(frame, true)); } [Fact] public void Control_and_ack_preserve_ordering_epochs_and_revisions() { var control = new DataSubscriptionControl( "s", 3, 9, DataStreamControlKind.CatalogInvalidated, new[] { new PartitionWatermark(2, 7) }, 4, 5, null ); var decoded = (DataSubscriptionControl) DataProtoMapper.DecodeControlOrAck(DataProtoMapper.EncodeControlOrAck(control)); decoded.Kind.ShouldBe(control.Kind); decoded.Watermarks.Single().ShouldBe(control.Watermarks.Single()); var ack = new DataSubscriptionAck( "s", 3, 10, "cmd", new[] { new DataSubscriptionItemAck( "item", DataItemStatus.Good, new DataItemHandle("s", 3, 7, 2), null, null ), } ); var decodedAck = (DataSubscriptionAck) DataProtoMapper.DecodeControlOrAck(DataProtoMapper.EncodeControlOrAck(ack)); decodedAck.Items.Single().Handle.ShouldBe(ack.Items.Single().Handle); } [Fact] public void One_item_can_contain_many_array_elements_and_serving_is_authenticated() { var serving = new ServingIdentity("n", "i", "primary", 1, 2); var handle = new DataItemHandle("s", 1, 1, 1); using var frame = new DataSubscriptionValues( "s", 1, 1, false, new[] { new DataSubscriptionValue( handle, new DataSample( DataValue.FromArray( DataValueKind.Int32, Enumerable.Range(0, 100).Cast().ToArray(), new[] { 100 } ), 0, null, null, serving, 1 ) ), } ); var handles = new Dictionary { [1] = handle }; foreach (var compact in new[] { false, true }) { var wire = DataProtoMapper.EncodeValues(frame, compact); using var decoded = DataProtoMapper.DecodeValues( wire, handles, 1, 100000, serving, 1000 ); decoded.Items.Single().Sample.Value.GetArray().Count.ShouldBe(100); Should.Throw(() => DataProtoMapper.DecodeValues( wire, handles, 1, 100000, serving with { Incarnation = "other", }, 1000 ) ); } } [Fact] public void Fallback_mixed_provenance_and_ack_other_epoch_are_rejected() { var serving = new ServingIdentity("n", "i", "primary", 1, 2); var handle = new DataItemHandle("s", 1, 1, 1); using var frame = new DataSubscriptionValues( "s", 1, 1, false, new[] { new DataSubscriptionValue( handle, new DataSample(DataValue.Null(), 0, null, null, serving, 1) ), } ); var wire = DataProtoMapper.EncodeValues(frame, false); wire.Values.Items[0].Sample.Serving.NodeId = "other"; var handles = new Dictionary { [1] = handle }; Should.Throw(() => DataProtoMapper.DecodeValues(wire, handles, 1, 10000, serving) ); var ack = new DataSubscriptionAck( "s", 1, 2, "command", new[] { new DataSubscriptionItemAck("x", DataItemStatus.Good, handle, null, null) } ); var ackWire = DataProtoMapper.EncodeControlOrAck(ack); ackWire.Ack.Items[0].Handle.StreamEpoch = 2; Should.Throw(() => DataProtoMapper.DecodeControlOrAck(ackWire)); } [Fact] public void Control_and_ack_obey_receiver_item_limits() { var control = new DataSubscriptionControl( "s", 1, 1, DataStreamControlKind.SnapshotEnd, new[] { new PartitionWatermark(1, 1), new PartitionWatermark(2, 1) }, 1, 1, null ); Should.Throw(() => DataProtoMapper.DecodeControlOrAck( DataProtoMapper.EncodeControlOrAck(control), new DataDecodeLimits(1, 10000, 100) ) ); var ack = new DataSubscriptionAck( "s", 1, 2, "command", new[] { new DataSubscriptionItemAck("x", DataItemStatus.Good, null, null, null), new DataSubscriptionItemAck("y", DataItemStatus.Good, null, null, null), } ); Should.Throw(() => DataProtoMapper.DecodeControlOrAck( DataProtoMapper.EncodeControlOrAck(ack), new DataDecodeLimits(1, 10000, 100) ) ); } [Fact] public void Invalid_control_generations_and_cross_stream_removals_are_rejected() { var control = new DataSubscriptionControl( "s", 1, 1, DataStreamControlKind.SnapshotEnd, new[] { new PartitionWatermark(1, 1) }, 1, 1, null ); var wire = DataProtoMapper.EncodeControlOrAck(control); wire.Control.CatalogEpoch = -1; Should.Throw(() => DataProtoMapper.DecodeControlOrAck(wire)); wire.Control.CatalogEpoch = 1; wire.Control.Watermarks[0].PartitionId = -1; Should.Throw(() => DataProtoMapper.DecodeControlOrAck(wire)); wire.Control.Watermarks[0].PartitionId = 1; wire.Control.Watermarks[0].Version = -1; Should.Throw(() => DataProtoMapper.DecodeControlOrAck(wire)); var command = new RemoveSubscriptionItems( "s", 1, "command", TimeSpan.FromSeconds(1), new[] { new DataItemHandle("s", 1, 1, 1) } ); var removal = DataProtoMapper.EncodeCommand(command); removal.Remove.Items[0].StreamEpoch = 2; Should.Throw(() => DataProtoMapper.DecodeCommand(removal)); } }