feat(cli): add enable/disable condition commands (H4 client path)

This commit is contained in:
Joseph Doherty
2026-06-19 01:48:21 -04:00
parent eb328e5761
commit 88d5ba45bf
10 changed files with 488 additions and 0 deletions
@@ -0,0 +1,75 @@
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 DisableCommandTests
{
/// <summary>Verifies that a successful disable prints a success message and targets the node.</summary>
[Fact]
public async Task Execute_Success_PrintsSuccessMessage()
{
var fakeService = new FakeOpcUaClientService
{
DisableResult = StatusCodes.Good
};
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new DisableCommand(factory)
{
Url = "opc.tcp://localhost:4840",
NodeId = "ns=2;s=MyAlarm"
};
using var console = TestConsoleHelper.CreateConsole();
await command.ExecuteAsync(console);
var output = TestConsoleHelper.GetOutput(console);
output.ShouldContain("Disable successful");
fakeService.DisableCalls.Count.ShouldBe(1);
fakeService.DisableCalls[0].ShouldBe("ns=2;s=MyAlarm");
}
/// <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
{
DisableResult = StatusCodes.BadConditionAlreadyDisabled
};
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new DisableCommand(factory)
{
Url = "opc.tcp://localhost:4840",
NodeId = "ns=2;s=MyAlarm"
};
using var console = TestConsoleHelper.CreateConsole();
await command.ExecuteAsync(console);
var output = TestConsoleHelper.GetOutput(console);
output.ShouldContain("Disable failed");
}
/// <summary>Verifies that the command disconnects and disposes in the finally block.</summary>
[Fact]
public async Task Execute_DisconnectsInFinally()
{
var fakeService = new FakeOpcUaClientService();
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new DisableCommand(factory)
{
Url = "opc.tcp://localhost:4840",
NodeId = "ns=2;s=MyAlarm"
};
using var console = TestConsoleHelper.CreateConsole();
await command.ExecuteAsync(console);
fakeService.DisconnectCalled.ShouldBeTrue();
fakeService.DisposeCalled.ShouldBeTrue();
}
}
@@ -0,0 +1,75 @@
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 EnableCommandTests
{
/// <summary>Verifies that a successful enable prints a success message and targets the node.</summary>
[Fact]
public async Task Execute_Success_PrintsSuccessMessage()
{
var fakeService = new FakeOpcUaClientService
{
EnableResult = StatusCodes.Good
};
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new EnableCommand(factory)
{
Url = "opc.tcp://localhost:4840",
NodeId = "ns=2;s=MyAlarm"
};
using var console = TestConsoleHelper.CreateConsole();
await command.ExecuteAsync(console);
var output = TestConsoleHelper.GetOutput(console);
output.ShouldContain("Enable successful");
fakeService.EnableCalls.Count.ShouldBe(1);
fakeService.EnableCalls[0].ShouldBe("ns=2;s=MyAlarm");
}
/// <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
{
EnableResult = StatusCodes.BadConditionAlreadyEnabled
};
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new EnableCommand(factory)
{
Url = "opc.tcp://localhost:4840",
NodeId = "ns=2;s=MyAlarm"
};
using var console = TestConsoleHelper.CreateConsole();
await command.ExecuteAsync(console);
var output = TestConsoleHelper.GetOutput(console);
output.ShouldContain("Enable failed");
}
/// <summary>Verifies that the command disconnects and disposes in the finally block.</summary>
[Fact]
public async Task Execute_DisconnectsInFinally()
{
var fakeService = new FakeOpcUaClientService();
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new EnableCommand(factory)
{
Url = "opc.tcp://localhost:4840",
NodeId = "ns=2;s=MyAlarm"
};
using var console = TestConsoleHelper.CreateConsole();
await command.ExecuteAsync(console);
fakeService.DisconnectCalled.ShouldBeTrue();
fakeService.DisposeCalled.ShouldBeTrue();
}
}
@@ -262,6 +262,32 @@ public sealed class FakeOpcUaClientService : IOpcUaClientService
return Task.FromResult(ShelveAlarmResult);
}
/// <summary>Gets the list of condition node IDs passed to EnableAsync calls.</summary>
public List<string> EnableCalls { get; } = [];
/// <summary>Gets or sets the status code returned by EnableAsync.</summary>
public StatusCode EnableResult { get; set; } = StatusCodes.Good;
/// <inheritdoc />
public Task<StatusCode> EnableAsync(string conditionNodeId, CancellationToken ct = default)
{
EnableCalls.Add(conditionNodeId);
return Task.FromResult(EnableResult);
}
/// <summary>Gets the list of condition node IDs passed to DisableAsync calls.</summary>
public List<string> DisableCalls { get; } = [];
/// <summary>Gets or sets the status code returned by DisableAsync.</summary>
public StatusCode DisableResult { get; set; } = StatusCodes.Good;
/// <inheritdoc />
public Task<StatusCode> DisableAsync(string conditionNodeId, CancellationToken ct = default)
{
DisableCalls.Add(conditionNodeId);
return Task.FromResult(DisableResult);
}
/// <inheritdoc />
public Task<IReadOnlyList<DataValue>> HistoryReadRawAsync(
NodeId nodeId, DateTime startTime, DateTime endTime, int maxValues = 1000, CancellationToken ct = default)
@@ -208,11 +208,25 @@ internal sealed class FakeSessionAdapter : ISessionAdapter
/// </summary>
public List<object[]> CallMethodInputArgs { get; } = [];
/// <summary>
/// Records the object node id passed to each <see cref="CallMethodAsync"/> invocation,
/// so tests can assert which condition object a method was invoked on.
/// </summary>
public List<NodeId> CallMethodObjectIds { get; } = [];
/// <summary>
/// Records the method node id passed to each <see cref="CallMethodAsync"/> invocation,
/// so tests can assert the correct Part 9 method (e.g. Enable/Disable) was invoked.
/// </summary>
public List<NodeId> CallMethodMethodIds { get; } = [];
/// <inheritdoc />
public Task<IList<object>?> CallMethodAsync(NodeId objectId, NodeId methodId, object[] inputArguments,
CancellationToken ct = default)
{
CallMethodCount++;
CallMethodObjectIds.Add(objectId);
CallMethodMethodIds.Add(methodId);
CallMethodInputArgs.Add(inputArguments);
if (CallMethodException != null)
throw CallMethodException;
@@ -1174,6 +1174,88 @@ public class OpcUaClientServiceTests : IDisposable
result.Code.ShouldBe(StatusCodes.BadConditionAlreadyShelved);
}
// --- Enable / Disable tests (H4 client path) ---
/// <summary>
/// Verifies that EnableAsync invokes the ConditionType_Enable method with no input
/// arguments and returns <see cref="StatusCodes.Good"/> on success.
/// </summary>
[Fact]
public async Task EnableAsync_OnSuccess_CallsEnableMethodWithNoArgs()
{
var session = new FakeSessionAdapter();
_sessionFactory.EnqueueSession(session);
await _service.ConnectAsync(ValidSettings());
var result = await _service.EnableAsync("ns=2;s=Cond");
result.ShouldBe(StatusCodes.Good);
session.CallMethodCount.ShouldBe(1);
session.CallMethodMethodIds[0].ShouldBe(MethodIds.ConditionType_Enable);
session.CallMethodInputArgs[0].ShouldBeEmpty();
}
/// <summary>
/// Verifies that DisableAsync invokes the ConditionType_Disable method with no input
/// arguments and returns <see cref="StatusCodes.Good"/> on success.
/// </summary>
[Fact]
public async Task DisableAsync_OnSuccess_CallsDisableMethodWithNoArgs()
{
var session = new FakeSessionAdapter();
_sessionFactory.EnqueueSession(session);
await _service.ConnectAsync(ValidSettings());
var result = await _service.DisableAsync("ns=2;s=Cond");
result.ShouldBe(StatusCodes.Good);
session.CallMethodCount.ShouldBe(1);
session.CallMethodMethodIds[0].ShouldBe(MethodIds.ConditionType_Disable);
session.CallMethodInputArgs[0].ShouldBeEmpty();
}
/// <summary>
/// Verifies that a <see cref="ServiceResultException"/> from the session is captured and
/// returned as a bad <see cref="StatusCode"/> rather than propagating to the caller.
/// </summary>
[Fact]
public async Task EnableAsync_OnServiceResultException_ReturnsBadStatusCode()
{
var session = new FakeSessionAdapter
{
CallMethodException = new ServiceResultException(
StatusCodes.BadConditionAlreadyEnabled, "already enabled")
};
_sessionFactory.EnqueueSession(session);
await _service.ConnectAsync(ValidSettings());
var result = await _service.EnableAsync("ns=2;s=Cond");
StatusCode.IsBad(result).ShouldBeTrue();
result.Code.ShouldBe(StatusCodes.BadConditionAlreadyEnabled);
}
/// <summary>
/// Verifies that a <see cref="ServiceResultException"/> from the session is captured and
/// returned as a bad <see cref="StatusCode"/> rather than propagating to the caller.
/// </summary>
[Fact]
public async Task DisableAsync_OnServiceResultException_ReturnsBadStatusCode()
{
var session = new FakeSessionAdapter
{
CallMethodException = new ServiceResultException(
StatusCodes.BadConditionAlreadyDisabled, "already disabled")
};
_sessionFactory.EnqueueSession(session);
await _service.ConnectAsync(ValidSettings());
var result = await _service.DisableAsync("ns=2;s=Cond");
StatusCode.IsBad(result).ShouldBeTrue();
result.Code.ShouldBe(StatusCodes.BadConditionAlreadyDisabled);
}
// --- Alarm fallback path (Client.Shared-011) ---
/// <summary>
@@ -301,6 +301,42 @@ public sealed class FakeOpcUaClientService : IOpcUaClientService
return Task.FromResult(ShelveResult);
}
/// <summary>Gets or sets the status code returned by enable operations in UI tests.</summary>
public StatusCode EnableResult { get; set; } = StatusCodes.Good;
/// <summary>Gets or sets the exception thrown to simulate alarm enable failures in the UI.</summary>
public Exception? EnableException { get; set; }
/// <summary>Gets the number of times EnableAsync has been called.</summary>
public int EnableCallCount { get; private set; }
/// <summary>Gets the conditionNodeId of the last EnableAsync call.</summary>
public string? LastEnableCall { get; private set; }
/// <inheritdoc />
public Task<StatusCode> EnableAsync(string conditionNodeId, CancellationToken ct = default)
{
EnableCallCount++;
LastEnableCall = conditionNodeId;
if (EnableException != null) throw EnableException;
return Task.FromResult(EnableResult);
}
/// <summary>Gets or sets the status code returned by disable operations in UI tests.</summary>
public StatusCode DisableResult { get; set; } = StatusCodes.Good;
/// <summary>Gets or sets the exception thrown to simulate alarm disable failures in the UI.</summary>
public Exception? DisableException { get; set; }
/// <summary>Gets the number of times DisableAsync has been called.</summary>
public int DisableCallCount { get; private set; }
/// <summary>Gets the conditionNodeId of the last DisableAsync call.</summary>
public string? LastDisableCall { get; private set; }
/// <inheritdoc />
public Task<StatusCode> DisableAsync(string conditionNodeId, CancellationToken ct = default)
{
DisableCallCount++;
LastDisableCall = conditionNodeId;
if (DisableException != null) throw DisableException;
return Task.FromResult(DisableResult);
}
/// <inheritdoc />
public Task<IReadOnlyList<DataValue>> HistoryReadRawAsync(NodeId nodeId, DateTime startTime, DateTime endTime,
int maxValues = 1000, CancellationToken ct = default)