Files
lmxopcua/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Historian.Gateway.Tests/Mapping/EventMapperTests.cs
T
Joseph Doherty 3a312ffdd1
v2-ci / unit-tests (push) Has been cancelled
fix(history): a folder's event history reads the Galaxy objects behind its native conditions (#886)
A notifier folder read one event source, its NodeId identifier (a RawPath or an EquipmentId). AVEVA stores a
Galaxy object's A&E history under the object's tag_name (Source_Object), so AVEVA's own alarm and event
history reached no production folder. Its lower-case property keys were also missed, so every row read
Severity 1 with no ConditionName.

Sources. A folder's event history now reads its own source plus the Galaxy object of every native condition
that fires on it live: those parented under it and those WireAlarmNotifiers wired to it. Live and history use
the same membership. The object comes from the condition's Galaxy alarm reference, its raw tag's attributeRef,
up to the first '.':
- The composer reads it with the driver's own parser (GalaxyAttributeRef, shared with GalaxyDriver), only for
  a GalaxyMxGateway tag with an alarm (RawTagPlan.NativeAlarmReference, part of plan equality).
- The applier binds it after materialising the condition (IOpcUaAddressSpaceSink.BindNativeAlarmHistory,
  forwarded by the deferred sink). A rebuild or the condition's removal forgets it.
Each object is read with the #882 rule: its own events and <Object>.<Attribute> rows, never a prefix sibling.

Fan-out. historiangw's ReadEvents filter is a single predicate, so there is one request per source; the wire
contract is unchanged. EventSourceFanOut merges the reads as one source:
- order is (time, source position, arrival), reversed for a reverse read; ties are never re-sorted by EventId,
  so the boundary skip stays stable;
- duplicates by EventId are dropped and the merge is cut to the cap;
- a truncated source bounds the page at the time it stopped.
Paging, continuation points (bound to the whole source set), the walked-back reverse read and the tie-cluster
over-fetch are unchanged. A folder with one source is read exactly as before.

ACL. Every source is read inside the folder's own HistoryRead, after AuthorizeHistoryRead (#826). A denied
folder reads nothing and fails closed.

Mapping (EventMapper, property names now case-insensitive):
- Severity: an AVEVA row's priority goes through GalaxyAlarmPriority, the table live Galaxy alarms use
  (MxAccessSeverityMapper now delegates to it), so history matches live. Otherwise severity (OtOpcUa's own
  rows, which AVEVA stores lower-cased), clamped. AVEVA's own 1-4 severity is ignored. The table ranks a
  higher priority as more severe, which inverts AVEVA's convention: filed as #887.
- An AVEVA row whose source_conditionvariable names a native condition that fires on the folder takes that
  condition's SourceNode and live ConditionName. Otherwise ConditionName is alarmname or the condition
  variable's attribute.
- alarm_id/alarmid map to AlarmId.

Tests:
- GalaxyEquipmentEventHistoryWireTests (4, real SDK session, production Gateway backend, AVEVA-shaped fake
  gateway): equipment EqA backed by objects Oven_007 + OvenDoor_07. Covers object and attribute events from
  both objects, the prefix sibling excluded, cross-object and in-object ties, forward paging at 1-4 a page,
  reverse whole and paged, SourceNode/ConditionName/Severity, the raw device folder, ACL deny with no backend
  call plus a granted positive control, and a rebuild forgetting the bindings.
- EventSourceFanOutTests (8), EventMapper AVEVA cases (11), GalaxyAttributeRefTests (16), a composer test and
  an applier ordering test.
- Mutation-checked, 17 mutants, all killed. Covered: parent-only membership, no scope, a case-sensitive
  condition match, no condition rewrite, no truncation bound, EventId tie order, no sort, no cap, no stop at
  the bound, unreversed source order, no dedupe, no '.' boundary, priority ignored, case-sensitive keys, no
  applier bind, no deferred forward, and a composer that ignores the driver type. The forget-on-rematerialise
  guard survives as belt-and-braces: the rebuild clear covers it.
- Live, read-only, no WinRM, vd03 HistorianGateway ea9f21c: equipment folder EQ-886-ZipsenLeft (objects
  DevWinPlatform + Left_001). It read 0 events before and 92 now, equal to the unfiltered read's rows for both
  objects. 1/7-event pages and reverse whole/5-event pages agree, and 73 rows resolve to their live condition.
  diff_runs vs the #882 run: 221/226 identical. The 5 that differ are expected: DevWinPlatform Severity
  1 -> 750/1000, and #881 capabilities.

Claude-Session: https://claude.ai/code/session_01GT67ACmpMR7b9LGRPLijMi
2026-11-12 11:45:48 -05:00

150 lines
7.2 KiB
C#

using Google.Protobuf.WellKnownTypes;
using Xunit;
using ZB.MOM.WW.HistorianGateway.Contracts.Grpc;
using ZB.MOM.WW.OtOpcUa.Driver.Historian.Gateway.Mapping;
namespace ZB.MOM.WW.OtOpcUa.Driver.Historian.Gateway.Tests.Mapping;
public sealed class EventMapperTests
{
[Fact]
public void Carries_the_alarm_type_condition_name_and_alarm_id_the_writer_sent()
{
// The round trip of AlarmEventMapper's shape: Type = AlarmTypeName, Properties AlarmName / AlarmId.
var sent = AlarmEventMapper.ToHistorianEvent(new ZB.MOM.WW.OtOpcUa.Core.AlarmHistorian.AlarmHistorianEvent(
"Plant/Modbus/dev1/temp_hi", "Area/Line/Eq", "HiTemp", "LimitAlarm", ZB.MOM.WW.OtOpcUa.Core.Abstractions.AlarmSeverity.High,
"Activated", "high", "u", null, new DateTime(2026, 1, 1, 0, 0, 0, DateTimeKind.Utc)));
var h = EventMapper.ToHistoricalEvent(sent);
Assert.Equal("LimitAlarm", h.EventTypeName);
Assert.Equal("HiTemp", h.ConditionName);
Assert.Equal("Plant/Modbus/dev1/temp_hi", h.AlarmId);
// A gateway row without them (e.g. an event another writer produced) maps to nulls, never throws.
var bare = new HistorianEvent { Id = "E", EventTime = Ts(2026, 1, 1, 0, 0, 0), ReceivedTime = Ts(2026, 1, 1, 0, 0, 0) };
var b = EventMapper.ToHistoricalEvent(bare);
Assert.Null(b.EventTypeName);
Assert.Null(b.ConditionName);
Assert.Null(b.AlarmId);
}
[Fact]
public void Maps_core_fields_and_times()
{
var e = new HistorianEvent { Id = "E1", SourceName = "Pump1",
EventTime = Ts(2026, 1, 1, 0, 0, 0), ReceivedTime = Ts(2026, 1, 1, 0, 0, 5) };
e.Properties["Message"] = "High temp";
e.Properties["Severity"] = "700";
var h = EventMapper.ToHistoricalEvent(e);
Assert.Equal("E1", h.EventId);
Assert.Equal("Pump1", h.SourceName);
Assert.Equal("High temp", h.Message);
Assert.Equal((ushort)700, h.Severity);
Assert.Equal(DateTimeKind.Utc, h.EventTimeUtc.Kind);
}
[Theory]
[InlineData("Priority", "999", 1000)] // a Galaxy priority maps through the live table (#886), not clamped as a severity
[InlineData("Severity", "0", 1)] // clamp to OPC UA min 1
[InlineData("Severity", "5000", 1000)] // clamp to OPC UA max 1000
[InlineData(null, null, 1)] // missing → default min severity
public void Severity_parsed_and_clamped(string? key, string? val, int expected)
{
var e = new HistorianEvent { Id = "E", EventTime = Ts(2026, 1, 1, 0, 0, 0), ReceivedTime = Ts(2026, 1, 1, 0, 0, 0) };
if (key is not null) e.Properties[key] = val!;
Assert.Equal((ushort)expected, EventMapper.ToHistoricalEvent(e).Severity);
}
// ---- Gitea #886: AVEVA-shaped rows (lower-case keys) -------------------------------------------------------------
/// <summary>An AVEVA A&amp;E alarm row as vd03's HistorianGateway returns it (2026-11-12 probe, trimmed).</summary>
private static HistorianEvent AvevaAlarmRow(string priority = "500")
{
var e = new HistorianEvent
{
Id = "001c0040-0025-0004-5b00-74003f005400", Type = "Alarm.Set", SourceName = "Oven_007.Temp",
Namespace = "Application Server", EventTime = Ts(2026, 11, 5, 15, 19, 52), ReceivedTime = Ts(2026, 11, 5, 15, 19, 53),
};
e.Properties["alarm_id"] = "001c0040-0025-0004-5b00-74003f005400";
e.Properties["alarm_state"] = "UNACK_ALM";
e.Properties["comment"] = "Oven 7 temperature";
e.Properties["priority"] = priority;
e.Properties["severity"] = "2"; // AVEVA's own 1..4 scale: never the OPC UA Severity
e.Properties["source_conditionvariable"] = "Oven_007.TempHiAlarm";
e.Properties["source_object"] = "Oven_007";
e.Properties["source_processvariable"] = "Oven_007.Temp";
return e;
}
[Fact]
public void An_aveva_alarm_row_maps_priority_alarm_id_and_its_condition_reference()
{
var h = EventMapper.ToHistoricalEvent(AvevaAlarmRow());
Assert.Equal((ushort)750, h.Severity); // priority 500 → the live Galaxy table's High bucket, not AVEVA's severity "2"
Assert.Equal("001c0040-0025-0004-5b00-74003f005400", h.AlarmId);
Assert.Equal("Oven_007.TempHiAlarm", h.ConditionReference);
Assert.Equal("TempHiAlarm", h.ConditionName); // the AVEVA alarm's name until the server resolves a live condition
Assert.Equal("Alarm.Set", h.Message); // no message property: the event type, as before
Assert.Equal("Oven_007.Temp", h.SourceName);
}
[Theory]
[InlineData("1", 250)]
[InlineData("249", 250)]
[InlineData("250", 500)]
[InlineData("499", 500)]
[InlineData("500", 750)]
[InlineData("749", 750)]
[InlineData("750", 1000)]
[InlineData("999", 1000)] // a System.Stop/Start row (priority 999, AVEVA severity 0) — lmxopcua#887 tracks the direction
public void An_aveva_priority_maps_through_the_live_galaxy_table(string priority, int expected)
{
var h = EventMapper.ToHistoricalEvent(AvevaAlarmRow(priority));
Assert.Equal((ushort)expected, h.Severity);
// The live Galaxy path maps the same raw value through the same table, so history and live agree.
Assert.Equal(expected, ZB.MOM.WW.OtOpcUa.Core.Abstractions.GalaxyAlarmPriority.Map(int.Parse(priority)).OpcUaSeverity);
}
[Fact]
public void OtOpcUas_own_event_read_back_from_aveva_with_lower_case_keys_keeps_its_fields()
{
// AVEVA lower-cases the keys OtOpcUa writes (AlarmName → alarmname, …): seen on vd03 2026-11-12.
var e = new HistorianEvent { Id = "E", Type = "LimitAlarm", SourceName = "Area/Line/Eq",
EventTime = Ts(2026, 6, 27, 3, 43, 44), ReceivedTime = Ts(2026, 6, 27, 3, 43, 44) };
e.Properties["alarmid"] = "Plant/Modbus/dev1/temp_hi";
e.Properties["alarmname"] = "HiTemp";
e.Properties["eventkind"] = "Activated";
e.Properties["message"] = "Temperature high";
e.Properties["severity"] = "700";
var h = EventMapper.ToHistoricalEvent(e);
Assert.Equal("Plant/Modbus/dev1/temp_hi", h.AlarmId);
Assert.Equal("HiTemp", h.ConditionName);
Assert.Equal("Temperature high", h.Message);
Assert.Equal((ushort)700, h.Severity);
Assert.Null(h.ConditionReference);
}
[Fact]
public void An_aveva_system_event_has_no_condition()
{
var e = new HistorianEvent { Id = "S", Type = "System.Stop", SourceName = "Oven_007",
EventTime = Ts(2026, 11, 10, 11, 32, 35), ReceivedTime = Ts(2026, 11, 10, 11, 32, 35) };
e.Properties["priority"] = "999";
e.Properties["severity"] = "0";
e.Properties["source_object"] = "Oven_007";
var h = EventMapper.ToHistoricalEvent(e);
Assert.Null(h.ConditionName);
Assert.Null(h.ConditionReference);
Assert.Null(h.AlarmId);
Assert.Equal("System.Stop", h.Message);
}
// Ts(...) builds a Google.Protobuf.WellKnownTypes.Timestamp from UTC parts.
private static Timestamp Ts(int y, int mo, int d, int h, int mi, int s)
=> Timestamp.FromDateTime(new DateTime(y, mo, d, h, mi, s, DateTimeKind.Utc));
}