- Add JoeAppEngine folder to OPC UA nodes.json (BTCS, AlarmCntsBySeverity, Scheduler/ScanTime) - Fix DataConnectionActor: capture Self in PreStart for use from non-actor threads, preventing Self.Tell failure in Disconnected event handler - Implement InstanceActor.HandleConnectionQualityChanged to mark attributes Bad on disconnect - Fix LmxFakeProxy TagMapper to serialize arrays as JSON instead of "System.Int32[]" - Allow DataType and DataSourceReference updates in TemplateService.UpdateAttributeAsync - Update test_infra_opcua.md with JoeAppEngine documentation
132 lines
4.3 KiB
C#
132 lines
4.3 KiB
C#
using ScadaLink.CLI.Commands;
|
|
using ScadaLink.Commons.Messages.Management;
|
|
|
|
namespace ScadaLink.CLI.Tests;
|
|
|
|
public class CommandHelpersTests
|
|
{
|
|
[Fact]
|
|
public void HandleResponse_ManagementSuccess_JsonFormat_ReturnsZero()
|
|
{
|
|
var writer = new StringWriter();
|
|
Console.SetOut(writer);
|
|
|
|
var response = new ManagementSuccess("corr-1", "{\"id\":1,\"name\":\"test\"}");
|
|
var exitCode = CommandHelpers.HandleResponse(response, "json");
|
|
|
|
Assert.Equal(0, exitCode);
|
|
Assert.Contains("{\"id\":1,\"name\":\"test\"}", writer.ToString());
|
|
|
|
Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true });
|
|
}
|
|
|
|
[Fact]
|
|
public void HandleResponse_ManagementSuccess_TableFormat_ArrayData_ReturnsZero()
|
|
{
|
|
var writer = new StringWriter();
|
|
Console.SetOut(writer);
|
|
|
|
var json = "[{\"Id\":1,\"Name\":\"Alpha\"},{\"Id\":2,\"Name\":\"Beta\"}]";
|
|
var response = new ManagementSuccess("corr-1", json);
|
|
var exitCode = CommandHelpers.HandleResponse(response, "table");
|
|
|
|
Assert.Equal(0, exitCode);
|
|
var output = writer.ToString();
|
|
Assert.Contains("Id", output);
|
|
Assert.Contains("Name", output);
|
|
Assert.Contains("Alpha", output);
|
|
Assert.Contains("Beta", output);
|
|
|
|
Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true });
|
|
}
|
|
|
|
[Fact]
|
|
public void HandleResponse_ManagementSuccess_TableFormat_ObjectData_ReturnsZero()
|
|
{
|
|
var writer = new StringWriter();
|
|
Console.SetOut(writer);
|
|
|
|
var json = "{\"Id\":1,\"Name\":\"Alpha\",\"Status\":\"Active\"}";
|
|
var response = new ManagementSuccess("corr-1", json);
|
|
var exitCode = CommandHelpers.HandleResponse(response, "table");
|
|
|
|
Assert.Equal(0, exitCode);
|
|
var output = writer.ToString();
|
|
Assert.Contains("Property", output);
|
|
Assert.Contains("Value", output);
|
|
Assert.Contains("Id", output);
|
|
Assert.Contains("Alpha", output);
|
|
|
|
Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true });
|
|
}
|
|
|
|
[Fact]
|
|
public void HandleResponse_ManagementSuccess_TableFormat_EmptyArray_ShowsNoResults()
|
|
{
|
|
var writer = new StringWriter();
|
|
Console.SetOut(writer);
|
|
|
|
var response = new ManagementSuccess("corr-1", "[]");
|
|
var exitCode = CommandHelpers.HandleResponse(response, "table");
|
|
|
|
Assert.Equal(0, exitCode);
|
|
Assert.Contains("(no results)", writer.ToString());
|
|
|
|
Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true });
|
|
}
|
|
|
|
[Fact]
|
|
public void HandleResponse_ManagementError_ReturnsOne()
|
|
{
|
|
var errWriter = new StringWriter();
|
|
Console.SetError(errWriter);
|
|
|
|
var response = new ManagementError("corr-1", "Something failed", "FAIL_CODE");
|
|
var exitCode = CommandHelpers.HandleResponse(response, "json");
|
|
|
|
Assert.Equal(1, exitCode);
|
|
Assert.Contains("Something failed", errWriter.ToString());
|
|
|
|
Console.SetError(new StreamWriter(Console.OpenStandardError()) { AutoFlush = true });
|
|
}
|
|
|
|
[Fact]
|
|
public void HandleResponse_ManagementUnauthorized_ReturnsTwo()
|
|
{
|
|
var errWriter = new StringWriter();
|
|
Console.SetError(errWriter);
|
|
|
|
var response = new ManagementUnauthorized("corr-1", "Access denied");
|
|
var exitCode = CommandHelpers.HandleResponse(response, "json");
|
|
|
|
Assert.Equal(2, exitCode);
|
|
Assert.Contains("Access denied", errWriter.ToString());
|
|
|
|
Console.SetError(new StreamWriter(Console.OpenStandardError()) { AutoFlush = true });
|
|
}
|
|
|
|
[Fact]
|
|
public void HandleResponse_UnexpectedType_ReturnsOne()
|
|
{
|
|
var errWriter = new StringWriter();
|
|
Console.SetError(errWriter);
|
|
|
|
var exitCode = CommandHelpers.HandleResponse("unexpected", "json");
|
|
|
|
Assert.Equal(1, exitCode);
|
|
Assert.Contains("Unexpected response type", errWriter.ToString());
|
|
|
|
Console.SetError(new StreamWriter(Console.OpenStandardError()) { AutoFlush = true });
|
|
}
|
|
|
|
[Fact]
|
|
public void NewCorrelationId_ReturnsNonEmpty32CharHex()
|
|
{
|
|
var id = CommandHelpers.NewCorrelationId();
|
|
|
|
Assert.NotNull(id);
|
|
Assert.Equal(32, id.Length);
|
|
Assert.True(id.All(c => "0123456789abcdef".Contains(c)));
|
|
}
|
|
}
|