feat(client-cli): add ack/confirm/shelve alarm commands with service layer

Adds ConfirmAlarmAsync and ShelveAlarmAsync to IOpcUaClientService and
OpcUaClientService (mirroring the AcknowledgeAlarmAsync pattern: same
CallMethodAsync/ServiceResultException/StatusCode contract). Adds ShelveKind
enum (OneShot/Timed/Unshelve). Adds three new CLI commands — ack, confirm,
shelve — with hex EventId input and per-command argument validation.
Updates both fakes (CLI + UI) to implement the new interface members and
record calls. Adds 16 unit tests covering argument mapping, invalid-input
CommandException paths, bad-status output, and disconnect-in-finally.
This commit is contained in:
Joseph Doherty
2026-06-11 05:46:25 -04:00
parent a6fed85ac9
commit 1f172e55f7
11 changed files with 819 additions and 1 deletions
@@ -0,0 +1,106 @@
using CliFx.Exceptions;
using Opc.Ua;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Client.CLI.Commands;
using ZB.MOM.WW.OtOpcUa.Client.CLI.Tests.Fakes;
namespace ZB.MOM.WW.OtOpcUa.Client.CLI.Tests;
public class AcknowledgeCommandTests
{
private static string HexOf(byte[] bytes) => Convert.ToHexString(bytes);
/// <summary>Verifies that a successful ack prints a success message.</summary>
[Fact]
public async Task Execute_Success_PrintsSuccessMessage()
{
var fakeService = new FakeOpcUaClientService
{
AcknowledgeAlarmResult = StatusCodes.Good
};
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var eventId = new byte[] { 0x01, 0x02, 0x03 };
var command = new AcknowledgeCommand(factory)
{
Url = "opc.tcp://localhost:4840",
NodeId = "ns=2;s=MyAlarm",
EventId = HexOf(eventId),
Comment = "test comment"
};
using var console = TestConsoleHelper.CreateConsole();
await command.ExecuteAsync(console);
var output = TestConsoleHelper.GetOutput(console);
output.ShouldContain("Acknowledge successful");
fakeService.AcknowledgeAlarmCalls.Count.ShouldBe(1);
fakeService.AcknowledgeAlarmCalls[0].ConditionNodeId.ShouldBe("ns=2;s=MyAlarm");
fakeService.AcknowledgeAlarmCalls[0].EventId.ShouldBe(eventId);
fakeService.AcknowledgeAlarmCalls[0].Comment.ShouldBe("test comment");
}
/// <summary>Verifies that a bad StatusCode from the service prints a failure message.</summary>
[Fact]
public async Task Execute_BadStatus_PrintsFailureMessage()
{
var fakeService = new FakeOpcUaClientService
{
AcknowledgeAlarmResult = StatusCodes.BadEventNotAcknowledgeable
};
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new AcknowledgeCommand(factory)
{
Url = "opc.tcp://localhost:4840",
NodeId = "ns=2;s=MyAlarm",
EventId = HexOf(new byte[] { 0xAB }),
Comment = ""
};
using var console = TestConsoleHelper.CreateConsole();
await command.ExecuteAsync(console);
var output = TestConsoleHelper.GetOutput(console);
output.ShouldContain("Acknowledge failed");
}
/// <summary>Verifies that an invalid hex event-id throws CommandException.</summary>
[Fact]
public async Task Execute_InvalidEventId_ThrowsCommandException()
{
var fakeService = new FakeOpcUaClientService();
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new AcknowledgeCommand(factory)
{
Url = "opc.tcp://localhost:4840",
NodeId = "ns=2;s=MyAlarm",
EventId = "not-hex!",
Comment = ""
};
using var console = TestConsoleHelper.CreateConsole();
var ex = await Should.ThrowAsync<CommandException>(async () => await command.ExecuteAsync(console));
ex.Message.ShouldContain("event-id", Case.Insensitive);
}
/// <summary>Verifies that the command disconnects in the finally block.</summary>
[Fact]
public async Task Execute_DisconnectsInFinally()
{
var fakeService = new FakeOpcUaClientService();
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new AcknowledgeCommand(factory)
{
Url = "opc.tcp://localhost:4840",
NodeId = "ns=2;s=MyAlarm",
EventId = HexOf(new byte[] { 0x01 }),
Comment = ""
};
using var console = TestConsoleHelper.CreateConsole();
await command.ExecuteAsync(console);
fakeService.DisconnectCalled.ShouldBeTrue();
fakeService.DisposeCalled.ShouldBeTrue();
}
}
@@ -0,0 +1,106 @@
using CliFx.Exceptions;
using Opc.Ua;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Client.CLI.Commands;
using ZB.MOM.WW.OtOpcUa.Client.CLI.Tests.Fakes;
namespace ZB.MOM.WW.OtOpcUa.Client.CLI.Tests;
public class ConfirmCommandTests
{
private static string HexOf(byte[] bytes) => Convert.ToHexString(bytes);
/// <summary>Verifies that a successful confirm prints a success message and passes correct arguments to the service.</summary>
[Fact]
public async Task Execute_Success_PrintsSuccessMessage()
{
var fakeService = new FakeOpcUaClientService
{
ConfirmAlarmResult = StatusCodes.Good
};
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var eventId = new byte[] { 0x0A, 0x1B, 0x2C };
var command = new ConfirmCommand(factory)
{
Url = "opc.tcp://localhost:4840",
NodeId = "ns=2;s=MyAlarm",
EventId = HexOf(eventId),
Comment = "confirmed by operator"
};
using var console = TestConsoleHelper.CreateConsole();
await command.ExecuteAsync(console);
var output = TestConsoleHelper.GetOutput(console);
output.ShouldContain("Confirm successful");
fakeService.ConfirmAlarmCalls.Count.ShouldBe(1);
fakeService.ConfirmAlarmCalls[0].ConditionNodeId.ShouldBe("ns=2;s=MyAlarm");
fakeService.ConfirmAlarmCalls[0].EventId.ShouldBe(eventId);
fakeService.ConfirmAlarmCalls[0].Comment.ShouldBe("confirmed by operator");
}
/// <summary>Verifies that a bad StatusCode from the service prints a failure message.</summary>
[Fact]
public async Task Execute_BadStatus_PrintsFailureMessage()
{
var fakeService = new FakeOpcUaClientService
{
ConfirmAlarmResult = StatusCodes.BadConditionBranchAlreadyConfirmed
};
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new ConfirmCommand(factory)
{
Url = "opc.tcp://localhost:4840",
NodeId = "ns=2;s=MyAlarm",
EventId = HexOf(new byte[] { 0xFF }),
Comment = ""
};
using var console = TestConsoleHelper.CreateConsole();
await command.ExecuteAsync(console);
var output = TestConsoleHelper.GetOutput(console);
output.ShouldContain("Confirm failed");
}
/// <summary>Verifies that an invalid hex event-id throws CommandException.</summary>
[Fact]
public async Task Execute_InvalidEventId_ThrowsCommandException()
{
var fakeService = new FakeOpcUaClientService();
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new ConfirmCommand(factory)
{
Url = "opc.tcp://localhost:4840",
NodeId = "ns=2;s=MyAlarm",
EventId = "ZZZ-not-hex",
Comment = ""
};
using var console = TestConsoleHelper.CreateConsole();
var ex = await Should.ThrowAsync<CommandException>(async () => await command.ExecuteAsync(console));
ex.Message.ShouldContain("event-id", Case.Insensitive);
}
/// <summary>Verifies that the command disconnects in the finally block.</summary>
[Fact]
public async Task Execute_DisconnectsInFinally()
{
var fakeService = new FakeOpcUaClientService();
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new ConfirmCommand(factory)
{
Url = "opc.tcp://localhost:4840",
NodeId = "ns=2;s=MyAlarm",
EventId = HexOf(new byte[] { 0x01 }),
Comment = ""
};
using var console = TestConsoleHelper.CreateConsole();
await command.ExecuteAsync(console);
fakeService.DisconnectCalled.ShouldBeTrue();
fakeService.DisposeCalled.ShouldBeTrue();
}
}
@@ -220,11 +220,46 @@ public sealed class FakeOpcUaClientService : IOpcUaClientService
return Task.CompletedTask;
}
/// <summary>Gets the list of (conditionNodeId, eventId, comment) tuples from AcknowledgeAlarmAsync calls.</summary>
public List<(string ConditionNodeId, byte[] EventId, string Comment)> AcknowledgeAlarmCalls { get; } = [];
/// <summary>Gets or sets the status code returned by AcknowledgeAlarmAsync.</summary>
public StatusCode AcknowledgeAlarmResult { get; set; } = StatusCodes.Good;
/// <inheritdoc />
public Task<StatusCode> AcknowledgeAlarmAsync(string conditionNodeId, byte[] eventId, string comment,
CancellationToken ct = default)
{
return Task.FromResult(new StatusCode(StatusCodes.Good));
AcknowledgeAlarmCalls.Add((conditionNodeId, eventId, comment));
return Task.FromResult(AcknowledgeAlarmResult);
}
/// <summary>Gets the list of (conditionNodeId, eventId, comment) tuples from ConfirmAlarmAsync calls.</summary>
public List<(string ConditionNodeId, byte[] EventId, string Comment)> ConfirmAlarmCalls { get; } = [];
/// <summary>Gets or sets the status code returned by ConfirmAlarmAsync.</summary>
public StatusCode ConfirmAlarmResult { get; set; } = StatusCodes.Good;
/// <inheritdoc />
public Task<StatusCode> ConfirmAlarmAsync(string conditionNodeId, byte[] eventId, string comment,
CancellationToken ct = default)
{
ConfirmAlarmCalls.Add((conditionNodeId, eventId, comment));
return Task.FromResult(ConfirmAlarmResult);
}
/// <summary>Gets the list of (conditionNodeId, kind, durationSeconds) tuples from ShelveAlarmAsync calls.</summary>
public List<(string ConditionNodeId, ShelveKind Kind, double DurationSeconds)> ShelveAlarmCalls { get; } = [];
/// <summary>Gets or sets the status code returned by ShelveAlarmAsync.</summary>
public StatusCode ShelveAlarmResult { get; set; } = StatusCodes.Good;
/// <inheritdoc />
public Task<StatusCode> ShelveAlarmAsync(string conditionNodeId, ShelveKind kind, double shelvingTimeSeconds = 0,
CancellationToken ct = default)
{
ShelveAlarmCalls.Add((conditionNodeId, kind, shelvingTimeSeconds));
return Task.FromResult(ShelveAlarmResult);
}
/// <inheritdoc />
@@ -0,0 +1,181 @@
using CliFx.Exceptions;
using Opc.Ua;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Client.CLI.Commands;
using ZB.MOM.WW.OtOpcUa.Client.CLI.Tests.Fakes;
using ZB.MOM.WW.OtOpcUa.Client.Shared.Models;
namespace ZB.MOM.WW.OtOpcUa.Client.CLI.Tests;
public class ShelveCommandTests
{
/// <summary>Verifies that OneShot shelving produces the correct kind and zero duration.</summary>
[Fact]
public async Task Execute_OneShotKind_PassesOneShotToService()
{
var fakeService = new FakeOpcUaClientService { ShelveAlarmResult = StatusCodes.Good };
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new ShelveCommand(factory)
{
Url = "opc.tcp://localhost:4840",
NodeId = "ns=2;s=MyAlarm",
Kind = "OneShot",
DurationSeconds = 0
};
using var console = TestConsoleHelper.CreateConsole();
await command.ExecuteAsync(console);
fakeService.ShelveAlarmCalls.Count.ShouldBe(1);
fakeService.ShelveAlarmCalls[0].Kind.ShouldBe(ShelveKind.OneShot);
fakeService.ShelveAlarmCalls[0].DurationSeconds.ShouldBe(0);
fakeService.ShelveAlarmCalls[0].ConditionNodeId.ShouldBe("ns=2;s=MyAlarm");
var output = TestConsoleHelper.GetOutput(console);
output.ShouldContain("successful");
}
/// <summary>Verifies that Timed shelving passes the correct duration to the service.</summary>
[Fact]
public async Task Execute_TimedKind_PassesDurationToService()
{
var fakeService = new FakeOpcUaClientService { ShelveAlarmResult = StatusCodes.Good };
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new ShelveCommand(factory)
{
Url = "opc.tcp://localhost:4840",
NodeId = "ns=2;s=MyAlarm",
Kind = "Timed",
DurationSeconds = 300
};
using var console = TestConsoleHelper.CreateConsole();
await command.ExecuteAsync(console);
fakeService.ShelveAlarmCalls.Count.ShouldBe(1);
fakeService.ShelveAlarmCalls[0].Kind.ShouldBe(ShelveKind.Timed);
fakeService.ShelveAlarmCalls[0].DurationSeconds.ShouldBe(300);
}
/// <summary>Verifies that Unshelve produces the Unshelve kind.</summary>
[Fact]
public async Task Execute_UnshelveKind_PassesUnshelveToService()
{
var fakeService = new FakeOpcUaClientService { ShelveAlarmResult = StatusCodes.Good };
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new ShelveCommand(factory)
{
Url = "opc.tcp://localhost:4840",
NodeId = "ns=2;s=MyAlarm",
Kind = "Unshelve",
DurationSeconds = 0
};
using var console = TestConsoleHelper.CreateConsole();
await command.ExecuteAsync(console);
fakeService.ShelveAlarmCalls.Count.ShouldBe(1);
fakeService.ShelveAlarmCalls[0].Kind.ShouldBe(ShelveKind.Unshelve);
}
/// <summary>Verifies that kind parsing is case-insensitive (e.g. "oneshot" works).</summary>
[Fact]
public async Task Execute_KindCaseInsensitive_ParsesCorrectly()
{
var fakeService = new FakeOpcUaClientService { ShelveAlarmResult = StatusCodes.Good };
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new ShelveCommand(factory)
{
Url = "opc.tcp://localhost:4840",
NodeId = "ns=2;s=MyAlarm",
Kind = "oneshot",
DurationSeconds = 0
};
using var console = TestConsoleHelper.CreateConsole();
await command.ExecuteAsync(console);
fakeService.ShelveAlarmCalls.Count.ShouldBe(1);
fakeService.ShelveAlarmCalls[0].Kind.ShouldBe(ShelveKind.OneShot);
}
/// <summary>Verifies that an invalid Kind value throws CommandException.</summary>
[Fact]
public async Task Execute_InvalidKind_ThrowsCommandException()
{
var fakeService = new FakeOpcUaClientService();
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new ShelveCommand(factory)
{
Url = "opc.tcp://localhost:4840",
NodeId = "ns=2;s=MyAlarm",
Kind = "SuperShelve",
DurationSeconds = 0
};
using var console = TestConsoleHelper.CreateConsole();
var ex = await Should.ThrowAsync<CommandException>(async () => await command.ExecuteAsync(console));
ex.Message.ShouldContain("kind", Case.Insensitive);
}
/// <summary>Verifies that Timed with zero duration throws CommandException (missing --duration).</summary>
[Fact]
public async Task Execute_TimedWithZeroDuration_ThrowsCommandException()
{
var fakeService = new FakeOpcUaClientService();
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new ShelveCommand(factory)
{
Url = "opc.tcp://localhost:4840",
NodeId = "ns=2;s=MyAlarm",
Kind = "Timed",
DurationSeconds = 0
};
using var console = TestConsoleHelper.CreateConsole();
var ex = await Should.ThrowAsync<CommandException>(async () => await command.ExecuteAsync(console));
ex.Message.ShouldContain("duration", Case.Insensitive);
}
/// <summary>Verifies that a bad status code from the service prints a failure message.</summary>
[Fact]
public async Task Execute_BadStatus_PrintsFailureMessage()
{
var fakeService = new FakeOpcUaClientService { ShelveAlarmResult = StatusCodes.BadNotSupported };
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new ShelveCommand(factory)
{
Url = "opc.tcp://localhost:4840",
NodeId = "ns=2;s=MyAlarm",
Kind = "OneShot",
DurationSeconds = 0
};
using var console = TestConsoleHelper.CreateConsole();
await command.ExecuteAsync(console);
var output = TestConsoleHelper.GetOutput(console);
output.ShouldContain("failed");
}
/// <summary>Verifies that the command disconnects in the finally block.</summary>
[Fact]
public async Task Execute_DisconnectsInFinally()
{
var fakeService = new FakeOpcUaClientService();
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new ShelveCommand(factory)
{
Url = "opc.tcp://localhost:4840",
NodeId = "ns=2;s=MyAlarm",
Kind = "OneShot",
DurationSeconds = 0
};
using var console = TestConsoleHelper.CreateConsole();
await command.ExecuteAsync(console);
fakeService.DisconnectCalled.ShouldBeTrue();
fakeService.DisposeCalled.ShouldBeTrue();
}
}
@@ -263,6 +263,26 @@ public sealed class FakeOpcUaClientService : IOpcUaClientService
return Task.FromResult(AcknowledgeResult);
}
/// <summary>Gets or sets the status code returned by confirmation operations in UI tests.</summary>
public StatusCode ConfirmResult { get; set; } = StatusCodes.Good;
/// <inheritdoc />
public Task<StatusCode> ConfirmAlarmAsync(string conditionNodeId, byte[] eventId, string comment,
CancellationToken ct = default)
{
return Task.FromResult(ConfirmResult);
}
/// <summary>Gets or sets the status code returned by shelve operations in UI tests.</summary>
public StatusCode ShelveResult { get; set; } = StatusCodes.Good;
/// <inheritdoc />
public Task<StatusCode> ShelveAlarmAsync(string conditionNodeId, ShelveKind kind, double shelvingTimeSeconds = 0,
CancellationToken ct = default)
{
return Task.FromResult(ShelveResult);
}
/// <inheritdoc />
public Task<IReadOnlyList<DataValue>> HistoryReadRawAsync(NodeId nodeId, DateTime startTime, DateTime endTime,
int maxValues = 1000, CancellationToken ct = default)