using Opc.Ua;
using Shouldly;
using Xunit;

namespace ZB.MOM.WW.OtOpcUa.Driver.OpcUaClient.Tests;

[Trait("Category", "Unit")]
public sealed class OpcUaClientSecurityPolicyTests
{
    /// <summary>Verifies that every security policy enum value maps to a known non-empty URI.</summary>
    /// <param name="policy">The OPC UA security policy to map.</param>
    [Theory]
    [InlineData(OpcUaSecurityPolicy.None)]
    [InlineData(OpcUaSecurityPolicy.Basic128Rsa15)]
    [InlineData(OpcUaSecurityPolicy.Basic256)]
    [InlineData(OpcUaSecurityPolicy.Basic256Sha256)]
    [InlineData(OpcUaSecurityPolicy.Aes128_Sha256_RsaOaep)]
    [InlineData(OpcUaSecurityPolicy.Aes256_Sha256_RsaPss)]
    public void MapSecurityPolicy_returns_known_non_empty_uri_for_every_enum_value(OpcUaSecurityPolicy policy)
    {
        var uri = OpcUaClientDriver.MapSecurityPolicy(policy);
        uri.ShouldNotBeNullOrEmpty();
        // Each URI should end in the enum name (for the non-None policies) so a driver
        // operator reading logs can correlate the URI back to the config value.
        if (policy != OpcUaSecurityPolicy.None)
            uri.ShouldContain(policy.ToString());
    }

    /// <summary>Verifies that None security policy maps to the SDK None URI.</summary>
    [Fact]
    public void MapSecurityPolicy_None_matches_SDK_None_URI()
    {
        OpcUaClientDriver.MapSecurityPolicy(OpcUaSecurityPolicy.None)
            .ShouldBe(SecurityPolicies.None);
    }

    /// <summary>Verifies that Basic256Sha256 policy maps to the SDK URI.</summary>
    [Fact]
    public void MapSecurityPolicy_Basic256Sha256_matches_SDK_URI()
    {
        OpcUaClientDriver.MapSecurityPolicy(OpcUaSecurityPolicy.Basic256Sha256)
            .ShouldBe(SecurityPolicies.Basic256Sha256);
    }

    /// <summary>Verifies that Aes256_Sha256_RsaPss policy maps to the SDK URI.</summary>
    [Fact]
    public void MapSecurityPolicy_Aes256_Sha256_RsaPss_matches_SDK_URI()
    {
        OpcUaClientDriver.MapSecurityPolicy(OpcUaSecurityPolicy.Aes256_Sha256_RsaPss)
            .ShouldBe(SecurityPolicies.Aes256_Sha256_RsaPss);
    }

    /// <summary>Verifies that every security policy enum value has a mapping.</summary>
    [Fact]
    public void Every_enum_value_has_a_mapping()
    {
        foreach (OpcUaSecurityPolicy p in Enum.GetValues<OpcUaSecurityPolicy>())
            Should.NotThrow(() => OpcUaClientDriver.MapSecurityPolicy(p));
    }
}
