using Grpc.Core;
using Shouldly;
using Xunit;
using ZB.MOM.WW.HistorianGateway.Client;
namespace ZB.MOM.WW.OtOpcUa.Driver.Historian.Gateway.Tests;
///
/// lmxopcua#900 on Client 1.1.0: classifies by the client's typed
/// / /
/// . These exceptions carry the reason ONLY on the typed properties
/// (no inner , no trailer), so they pin that the typed accessor is what is read. The on-the-wire
/// path through the published client is .
///
public sealed class GatewayErrorReasonsTests
{
private static HistorianGatewayException Gw(StatusCode? code, string? reason, string? domain) =>
new("x") { StatusCode = code, ErrorReason = reason, ErrorDomain = domain };
[Fact]
public void FailedPrecondition_with_the_gateways_aggregate_reason_is_the_refusal()
{
var ex = Gw(StatusCode.FailedPrecondition, HistorianGatewayErrorReasons.AggregateNotSupportedForTag, HistorianGatewayErrorReasons.Domain);
GatewayErrorReasons.IsAggregateNotSupportedForTag(ex).ShouldBeTrue();
GatewayErrorReasons.ReasonOf(ex).ShouldBe("AGGREGATE_NOT_SUPPORTED_FOR_TAG");
}
[Fact]
public void The_refusal_is_found_when_wrapped()
{
var ex = new InvalidOperationException("outer",
Gw(StatusCode.FailedPrecondition, HistorianGatewayErrorReasons.AggregateNotSupportedForTag, HistorianGatewayErrorReasons.Domain));
GatewayErrorReasons.IsAggregateNotSupportedForTag(ex).ShouldBeTrue();
}
public static TheoryData NotTheRefusal() => new()
{
{ StatusCode.Internal, HistorianGatewayErrorReasons.AggregateNotSupportedForTag, HistorianGatewayErrorReasons.Domain },
{ null, HistorianGatewayErrorReasons.AggregateNotSupportedForTag, HistorianGatewayErrorReasons.Domain },
{ StatusCode.FailedPrecondition, null, null },
{ StatusCode.FailedPrecondition, "LIVE_WRITES_DISABLED", HistorianGatewayErrorReasons.Domain },
{ StatusCode.FailedPrecondition, HistorianGatewayErrorReasons.AggregateNotSupportedForTag, "example.com" },
{ StatusCode.FailedPrecondition, HistorianGatewayErrorReasons.AggregateNotSupportedForTag, null },
};
[Theory]
[MemberData(nameof(NotTheRefusal))]
public void Every_other_shape_is_not_the_refusal(StatusCode? code, string? reason, string? domain) =>
GatewayErrorReasons.IsAggregateNotSupportedForTag(Gw(code, reason, domain)).ShouldBeFalse();
[Fact]
public void A_foreign_domains_reason_is_no_reason() =>
GatewayErrorReasons.ReasonOf(Gw(StatusCode.FailedPrecondition, "AGGREGATE_NOT_SUPPORTED_FOR_TAG", "example.com")).ShouldBeNull();
[Fact]
public void A_non_gateway_exception_is_not_the_refusal()
{
GatewayErrorReasons.IsAggregateNotSupportedForTag(new IOException("x")).ShouldBeFalse();
GatewayErrorReasons.ReasonOf(new IOException("x")).ShouldBeNull();
}
}