79 lines
2.7 KiB
C#
79 lines
2.7 KiB
C#
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Drivers;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Resilience;
|
|
|
|
public class ResilienceFormModelTests
|
|
{
|
|
[Fact]
|
|
public void Blank_form_serializes_to_null()
|
|
=> new ResilienceFormModel().ToJson().ShouldBeNull();
|
|
|
|
[Fact]
|
|
public void Partial_override_round_trips()
|
|
{
|
|
var m = new ResilienceFormModel { RecycleIntervalSeconds = 3600 };
|
|
m.Policies["Read"].TimeoutSeconds = 5;
|
|
m.Policies["Read"].RetryCount = 5;
|
|
|
|
var json = m.ToJson();
|
|
json.ShouldNotBeNull();
|
|
|
|
var back = ResilienceFormModel.FromJson(json);
|
|
back.RecycleIntervalSeconds.ShouldBe(3600);
|
|
back.Policies["Read"].TimeoutSeconds.ShouldBe(5);
|
|
back.Policies["Write"].IsEmpty.ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void Malformed_json_yields_empty_model()
|
|
{
|
|
var m = ResilienceFormModel.FromJson("{ not valid json");
|
|
m.RecycleIntervalSeconds.ShouldBeNull();
|
|
m.Policies["Read"].IsEmpty.ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void Validate_flags_out_of_range_values_with_capability_and_range()
|
|
{
|
|
var m = new ResilienceFormModel();
|
|
m.Policies["Read"].TimeoutSeconds = 0; // < MinTimeoutSeconds
|
|
m.Policies["Read"].RetryCount = -1; // < MinRetryCount
|
|
m.Policies["Read"].BreakerFailureThreshold = 1; // below Polly MinimumThroughput floor
|
|
|
|
var msgs = m.Validate();
|
|
|
|
msgs.Count.ShouldBe(3);
|
|
msgs.ShouldContain(s => s.Contains("Read") && s.Contains("timeout"));
|
|
msgs.ShouldContain(s => s.Contains("Read") && s.Contains("retries"));
|
|
msgs.ShouldContain(s => s.Contains("Read") && s.Contains("breaker"));
|
|
}
|
|
|
|
[Fact]
|
|
public void Validate_passes_in_range_and_blank()
|
|
{
|
|
var blank = new ResilienceFormModel();
|
|
blank.Validate().ShouldBeEmpty();
|
|
|
|
var inRange = new ResilienceFormModel();
|
|
inRange.Policies["Read"].TimeoutSeconds = 5;
|
|
inRange.Policies["Read"].RetryCount = 3;
|
|
inRange.Policies["Read"].BreakerFailureThreshold = 4;
|
|
inRange.Policies["Write"].BreakerFailureThreshold = 0; // 0 = breaker off, legal
|
|
inRange.Validate().ShouldBeEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public void Emitted_json_is_consumable_by_the_runtime_parser()
|
|
{
|
|
var m = new ResilienceFormModel();
|
|
m.Policies["Read"].TimeoutSeconds = 7;
|
|
|
|
var opts = DriverResilienceOptionsParser.ParseOrDefaults(DriverTier.B, m.ToJson(), out var diag);
|
|
diag.ShouldBeNull();
|
|
opts.Resolve(DriverCapability.Read).TimeoutSeconds.ShouldBe(7);
|
|
opts.Resolve(DriverCapability.Write).RetryCount.ShouldBe(0);
|
|
}
|
|
}
|