feat(commons): MxGatewayEndpointConfig validator + tests
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Types.DataConnections;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Types.Flattening;
|
||||
|
||||
namespace ZB.MOM.WW.ScadaBridge.Commons.Validators;
|
||||
|
||||
/// <summary>
|
||||
/// Pure-function validator for <see cref="MxGatewayEndpointConfig"/>. Errors carry
|
||||
/// the offending property name in <see cref="ValidationEntry.EntityName"/>
|
||||
/// (optionally prefixed, e.g. "Primary.Endpoint") so the form can render
|
||||
/// per-field messages.
|
||||
/// </summary>
|
||||
public static class MxGatewayEndpointConfigValidator
|
||||
{
|
||||
/// <summary>
|
||||
/// Validates all fields of an <see cref="MxGatewayEndpointConfig"/>, returning errors with optionally-prefixed field names.
|
||||
/// </summary>
|
||||
/// <param name="config">The MxGateway endpoint configuration to validate.</param>
|
||||
/// <param name="fieldPrefix">Optional prefix prepended to each field name in error entries (e.g., "Primary.").</param>
|
||||
public static ValidationResult Validate(MxGatewayEndpointConfig config, string fieldPrefix = "")
|
||||
{
|
||||
var errors = new List<ValidationEntry>();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(config.Endpoint))
|
||||
errors.Add(Err("Endpoint", "Endpoint URL is required."));
|
||||
else if (!Uri.TryCreate(config.Endpoint, UriKind.Absolute, out var uri)
|
||||
|| (uri.Scheme != "http" && uri.Scheme != "https")
|
||||
|| string.IsNullOrEmpty(uri.Host))
|
||||
errors.Add(Err("Endpoint", "Endpoint URL must be a valid http:// or https:// URI."));
|
||||
|
||||
if (string.IsNullOrWhiteSpace(config.ApiKey))
|
||||
errors.Add(Err("ApiKey", "API key is required."));
|
||||
|
||||
if (config.ReadTimeoutMs <= 0)
|
||||
errors.Add(Err("ReadTimeoutMs", "Must be > 0."));
|
||||
|
||||
return errors.Count == 0
|
||||
? ValidationResult.Success()
|
||||
: ValidationResult.FromErrors(errors.ToArray());
|
||||
|
||||
ValidationEntry Err(string field, string message) =>
|
||||
ValidationEntry.Error(
|
||||
ValidationCategory.ConnectionConfig,
|
||||
message,
|
||||
entityName: $"{fieldPrefix}{field}");
|
||||
}
|
||||
}
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Types.DataConnections;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Types.Flattening;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Validators;
|
||||
|
||||
namespace ZB.MOM.WW.ScadaBridge.Commons.Tests.Validators;
|
||||
|
||||
public class MxGatewayEndpointConfigValidatorTests
|
||||
{
|
||||
private static MxGatewayEndpointConfig Valid() => new()
|
||||
{
|
||||
Endpoint = "http://gw:5000",
|
||||
ApiKey = "key",
|
||||
};
|
||||
|
||||
[Fact]
|
||||
public void Validate_ValidConfig_IsValid()
|
||||
{
|
||||
var result = MxGatewayEndpointConfigValidator.Validate(Valid());
|
||||
Assert.True(result.IsValid);
|
||||
Assert.Empty(result.Errors);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_MissingEndpoint_Fails()
|
||||
{
|
||||
var c = Valid();
|
||||
c.Endpoint = "";
|
||||
var r = MxGatewayEndpointConfigValidator.Validate(c);
|
||||
Assert.False(r.IsValid);
|
||||
Assert.Contains(r.Errors, e =>
|
||||
e.EntityName == "Endpoint"
|
||||
&& e.Category == ValidationCategory.ConnectionConfig
|
||||
&& e.Message.Contains("required", StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("opc.tcp://x:4840")]
|
||||
[InlineData("ftp://x")]
|
||||
[InlineData("not a url")]
|
||||
public void Validate_BadEndpointScheme_Fails(string url)
|
||||
{
|
||||
var c = Valid();
|
||||
c.Endpoint = url;
|
||||
var r = MxGatewayEndpointConfigValidator.Validate(c);
|
||||
Assert.False(r.IsValid);
|
||||
Assert.Contains(r.Errors, e => e.EntityName == "Endpoint");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_MissingApiKey_Fails()
|
||||
{
|
||||
var c = Valid();
|
||||
c.ApiKey = "";
|
||||
var r = MxGatewayEndpointConfigValidator.Validate(c);
|
||||
Assert.False(r.IsValid);
|
||||
Assert.Contains(r.Errors, e => e.EntityName == "ApiKey");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_NonPositiveReadTimeout_Fails()
|
||||
{
|
||||
var c = Valid();
|
||||
c.ReadTimeoutMs = 0;
|
||||
var r = MxGatewayEndpointConfigValidator.Validate(c);
|
||||
Assert.False(r.IsValid);
|
||||
Assert.Contains(r.Errors, e => e.EntityName == "ReadTimeoutMs");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_PrefixedFieldNames_AppearInErrors()
|
||||
{
|
||||
var c = Valid();
|
||||
c.Endpoint = "";
|
||||
var r = MxGatewayEndpointConfigValidator.Validate(c, "Primary.");
|
||||
Assert.Contains(r.Errors, e => e.EntityName == "Primary.Endpoint");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user