using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using ZB.MOM.WW.ScadaBridge.Commons.Types.Alarms; using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums; using ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Adapters; namespace ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Tests; /// /// Task 28: live smoke test for the OPC UA Alarms & Conditions adapter /// ( as IAlarmSubscribableConnection). /// /// Round-trips against an alarm-capable OPC UA endpoint /// (opc.tcp://localhost:50000 — the infra OPC PLC server, see /// infra/docker-compose.yml). Marked [SkippableFact] so it reports /// Skipped — not failed — when the endpoint is unreachable OR does not expose /// Alarms & Conditions (no ConditionRefresh snapshot arrives). The OPC PLC /// simulator does not reliably expose A&C; see /// docs/test_infra/test_infra.md for the alarm-capable-server requirement. /// /// The pure field→transition mapping is covered without a server by /// ; this test proves the live /// event-subscription + ConditionRefresh path end to end when infra supports it. /// [Trait("Category", "RequiresOpcUa")] public class OpcUaAlarmLiveSmokeTests { private const string EndpointUrl = "opc.tcp://localhost:50000"; [SkippableFact] public async Task SubscribeAlarms_DeliversConditionRefreshSnapshot() { using var loggerFactory = LoggerFactory.Create(_ => { }); var clientFactory = new RealOpcUaClientFactory(new OpcUaGlobalOptions(), loggerFactory); var adapter = new OpcUaDataConnection(clientFactory, NullLogger.Instance); // Probe the endpoint. An unreachable infra server surfaces a socket/timeout // error from deep in the OPC Foundation SDK — treat as "infra not available". try { await adapter.ConnectAsync(new Dictionary { ["EndpointUrl"] = EndpointUrl }); } catch (Exception ex) { Skip.If(true, $"OPC UA test server not reachable on {EndpointUrl}: {ex.Message}"); return; } try { var snapshotComplete = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); void OnTransition(NativeAlarmTransition t) { if (t.Kind == AlarmTransitionKind.SnapshotComplete) { snapshotComplete.TrySetResult(true); } } // Empty source reference = mirror every condition under the server's // event notifier; ConditionRefresh replays the active set then a // SnapshotComplete sentinel. await adapter.SubscribeAlarmsAsync(sourceReference: string.Empty, conditionFilter: null, OnTransition); var done = await Task.WhenAny(snapshotComplete.Task, Task.Delay(TimeSpan.FromSeconds(10))); // Reachable but no A&C snapshot within the window → the server does not // expose Alarms & Conditions. Skip rather than fail. Skip.IfNot(done == snapshotComplete.Task, $"OPC UA endpoint {EndpointUrl} reachable but delivered no A&C ConditionRefresh snapshot — " + "server likely does not expose Alarms & Conditions."); Assert.True(snapshotComplete.Task.IsCompletedSuccessfully); } finally { await adapter.DisconnectAsync(); } } }