Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.Communication.Tests/Grpc/ProtoContractTests.cs
T
Joseph Doherty 513fa1aa16 docs(tests): fix CommentChecker findings across tests/ — 13,392 → 0 (repo-wide scan clean)
Comment/XML-doc changes only:
- add summaries to ~10k test methods, fixtures, fakes and helpers
  (+ <param> on theories, <returns> on async Task methods,
  <inheritdoc/> on interface implementations the checker can resolve)
- strip ~2.6k project-tracking references (WP*, P*-*, row-NN, Task N,
  #NN, Component-0NN) from comments, keeping the explanatory text;
  reword false positives in comments only
- real docs on fakes implementing BCL/library interfaces
  (IDisposable, ILogger, IOptionsMonitor, IAsyncLifetime)
- reword the last 5 src/ false positives (CWE-1236, failures-1,
  year-0001, M365)

Only non-comment edits: four one-line nested classes split across lines
so members can carry docs. Build 0 errors / 0 warnings.
2026-10-07 15:05:49 -04:00

123 lines
4.8 KiB
C#

using Google.Protobuf.WellKnownTypes;
using ZB.MOM.WW.ScadaBridge.Communication.Grpc;
namespace ZB.MOM.WW.ScadaBridge.Communication.Tests.Grpc;
/// <summary>
/// Guardrail tests that verify all oneof variants in SiteStreamEvent have
/// corresponding conversion handlers. Adding a new proto field without
/// implementing the conversion will cause these tests to fail.
/// </summary>
public class ProtoContractTests
{
/// <summary>
/// The set of EventOneofCase values we handle in ConvertToDomainEvent.
/// Update this array when adding a new oneof variant.
/// </summary>
private static readonly SiteStreamEvent.EventOneofCase[] HandledCases =
[
SiteStreamEvent.EventOneofCase.AttributeChanged,
SiteStreamEvent.EventOneofCase.AlarmChanged
];
/// <summary>
/// Oneof variants that are NOT per-event payloads and so are deliberately absent from
/// <see cref="HandledCases"/>. <c>Batch</c> (R2) is a framing envelope: it is unpacked
/// by <see cref="SiteStreamGrpcClient.ForEachEvent"/> into the per-event cases above
/// BEFORE conversion, and never reaches <c>ConvertToDomainEvent</c> as a whole frame.
/// </summary>
private static readonly SiteStreamEvent.EventOneofCase[] FramingCases =
[
SiteStreamEvent.EventOneofCase.Batch
];
/// <summary>Verifies that every oneof variant has a conversion handler.</summary>
[Fact]
public void AllOneofVariants_HaveConversionHandlers()
{
var allCases = System.Enum.GetValues<SiteStreamEvent.EventOneofCase>()
.Where(c => c != SiteStreamEvent.EventOneofCase.None)
.ToArray();
var accountedFor = HandledCases.Concat(FramingCases).ToArray();
Assert.Equal(allCases.Length, accountedFor.Length);
foreach (var c in allCases)
Assert.Contains(c, accountedFor);
}
/// <summary>Verifies that a batch frame is unpacked into per-event cases rather than converted whole.</summary>
[Fact]
public void BatchFrame_IsUnpackedIntoPerEventCases_NotConvertedWhole()
{
// The framing case's contract: ForEachEvent hands the per-event cases to the
// handler in order, and ConvertToDomainEvent is never asked to make sense of the
// envelope itself (it would return null, silently dropping the whole batch).
var inner = new[]
{
CreateTestEvent(SiteStreamEvent.EventOneofCase.AttributeChanged),
CreateTestEvent(SiteStreamEvent.EventOneofCase.AlarmChanged)
};
var frame = new SiteStreamEvent
{
CorrelationId = "test",
Batch = new SiteStreamEventBatch { Events = { inner } }
};
Assert.Null(SiteStreamGrpcClient.ConvertToDomainEvent(frame));
var seen = new List<SiteStreamEvent.EventOneofCase>();
SiteStreamGrpcClient.ForEachEvent(frame, e => seen.Add(e.EventCase));
Assert.Equal(
[SiteStreamEvent.EventOneofCase.AttributeChanged, SiteStreamEvent.EventOneofCase.AlarmChanged],
seen);
}
/// <summary>Verifies that ConvertToDomainEvent handles every oneof variant.</summary>
/// <param name="eventCase">The event case.</param>
[Theory]
[InlineData(SiteStreamEvent.EventOneofCase.AttributeChanged)]
[InlineData(SiteStreamEvent.EventOneofCase.AlarmChanged)]
public void ConvertToDomainEvent_HandlesAllOneofVariants(SiteStreamEvent.EventOneofCase eventCase)
{
var evt = CreateTestEvent(eventCase);
var result = SiteStreamGrpcClient.ConvertToDomainEvent(evt);
Assert.NotNull(result);
}
private static SiteStreamEvent CreateTestEvent(SiteStreamEvent.EventOneofCase eventCase)
{
var ts = Timestamp.FromDateTimeOffset(DateTimeOffset.UtcNow);
return eventCase switch
{
SiteStreamEvent.EventOneofCase.AttributeChanged => new SiteStreamEvent
{
CorrelationId = "test",
AttributeChanged = new AttributeValueUpdate
{
InstanceUniqueName = "Site1.Inst1",
AttributePath = "Path",
AttributeName = "Attr",
Value = "42",
Quality = Quality.Good,
Timestamp = ts
}
},
SiteStreamEvent.EventOneofCase.AlarmChanged => new SiteStreamEvent
{
CorrelationId = "test",
AlarmChanged = new AlarmStateUpdate
{
InstanceUniqueName = "Site1.Inst1",
AlarmName = "HighTemp",
State = AlarmStateEnum.AlarmStateActive,
Priority = 1,
Timestamp = ts
}
},
_ => throw new ArgumentOutOfRangeException(nameof(eventCase), eventCase, "Unhandled event case")
};
}
}