271 lines
7.7 KiB
C#
271 lines
7.7 KiB
C#
using StellaOps.Router.Common.Enums;
|
|
using StellaOps.Router.Common.Models;
|
|
|
|
using StellaOps.TestKit;
|
|
namespace StellaOps.Router.Config.Tests;
|
|
|
|
/// <summary>
|
|
/// Unit tests for <see cref="ServiceConfig"/> and <see cref="EndpointConfig"/>.
|
|
/// </summary>
|
|
public sealed class ServiceConfigTests
|
|
{
|
|
#region ServiceConfig Default Values Tests
|
|
|
|
[Trait("Category", TestCategories.Unit)]
|
|
[Fact]
|
|
public void ServiceConfig_DefaultVersion_DefaultsToNull()
|
|
{
|
|
// Arrange & Act
|
|
var config = new ServiceConfig { ServiceName = "test" };
|
|
|
|
// Assert
|
|
config.DefaultVersion.Should().BeNull();
|
|
}
|
|
|
|
[Trait("Category", TestCategories.Unit)]
|
|
[Fact]
|
|
public void ServiceConfig_DefaultTransport_DefaultsToTcp()
|
|
{
|
|
// Arrange & Act
|
|
var config = new ServiceConfig { ServiceName = "test" };
|
|
|
|
// Assert
|
|
config.DefaultTransport.Should().Be(TransportType.Tcp);
|
|
}
|
|
|
|
[Trait("Category", TestCategories.Unit)]
|
|
[Fact]
|
|
public void ServiceConfig_Endpoints_DefaultsToEmptyList()
|
|
{
|
|
// Arrange & Act
|
|
var config = new ServiceConfig { ServiceName = "test" };
|
|
|
|
// Assert
|
|
config.Endpoints.Should().NotBeNull();
|
|
config.Endpoints.Should().BeEmpty();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region ServiceConfig Property Assignment Tests
|
|
|
|
[Trait("Category", TestCategories.Unit)]
|
|
[Fact]
|
|
public void ServiceConfig_ServiceName_CanBeSet()
|
|
{
|
|
// Arrange & Act
|
|
var config = new ServiceConfig { ServiceName = "my-service" };
|
|
|
|
// Assert
|
|
config.ServiceName.Should().Be("my-service");
|
|
}
|
|
|
|
[Trait("Category", TestCategories.Unit)]
|
|
[Fact]
|
|
public void ServiceConfig_DefaultVersion_CanBeSet()
|
|
{
|
|
// Arrange
|
|
var config = new ServiceConfig { ServiceName = "test" };
|
|
|
|
// Act
|
|
config.DefaultVersion = "1.0.0";
|
|
|
|
// Assert
|
|
config.DefaultVersion.Should().Be("1.0.0");
|
|
}
|
|
|
|
[Trait("Category", TestCategories.Unit)]
|
|
[Theory]
|
|
[InlineData(TransportType.Tcp)]
|
|
[InlineData(TransportType.Certificate)]
|
|
[InlineData(TransportType.Udp)]
|
|
[InlineData(TransportType.InMemory)]
|
|
[InlineData(TransportType.RabbitMq)]
|
|
public void ServiceConfig_DefaultTransport_CanBeSetToAllTypes(TransportType transport)
|
|
{
|
|
// Arrange
|
|
var config = new ServiceConfig { ServiceName = "test" };
|
|
|
|
// Act
|
|
config.DefaultTransport = transport;
|
|
|
|
// Assert
|
|
config.DefaultTransport.Should().Be(transport);
|
|
}
|
|
|
|
[Trait("Category", TestCategories.Unit)]
|
|
[Fact]
|
|
public void ServiceConfig_Endpoints_CanAddEndpoints()
|
|
{
|
|
// Arrange
|
|
var config = new ServiceConfig { ServiceName = "test" };
|
|
|
|
// Act
|
|
config.Endpoints.Add(new EndpointConfig { Method = "GET", Path = "/api/health" });
|
|
config.Endpoints.Add(new EndpointConfig { Method = "POST", Path = "/api/data" });
|
|
|
|
// Assert
|
|
config.Endpoints.Should().HaveCount(2);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region EndpointConfig Default Values Tests
|
|
|
|
[Trait("Category", TestCategories.Unit)]
|
|
[Fact]
|
|
public void EndpointConfig_DefaultTimeout_DefaultsToNull()
|
|
{
|
|
// Arrange & Act
|
|
var endpoint = new EndpointConfig { Method = "GET", Path = "/" };
|
|
|
|
// Assert
|
|
endpoint.DefaultTimeout.Should().BeNull();
|
|
}
|
|
|
|
[Trait("Category", TestCategories.Unit)]
|
|
[Fact]
|
|
public void EndpointConfig_SupportsStreaming_DefaultsToFalse()
|
|
{
|
|
// Arrange & Act
|
|
var endpoint = new EndpointConfig { Method = "GET", Path = "/" };
|
|
|
|
// Assert
|
|
endpoint.SupportsStreaming.Should().BeFalse();
|
|
}
|
|
|
|
[Trait("Category", TestCategories.Unit)]
|
|
[Fact]
|
|
public void EndpointConfig_RequiringClaims_DefaultsToEmptyList()
|
|
{
|
|
// Arrange & Act
|
|
var endpoint = new EndpointConfig { Method = "GET", Path = "/" };
|
|
|
|
// Assert
|
|
endpoint.RequiringClaims.Should().NotBeNull();
|
|
endpoint.RequiringClaims.Should().BeEmpty();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region EndpointConfig Property Assignment Tests
|
|
|
|
[Trait("Category", TestCategories.Unit)]
|
|
[Fact]
|
|
public void EndpointConfig_Method_CanBeSet()
|
|
{
|
|
// Arrange & Act
|
|
var endpoint = new EndpointConfig { Method = "DELETE", Path = "/" };
|
|
|
|
// Assert
|
|
endpoint.Method.Should().Be("DELETE");
|
|
}
|
|
|
|
[Trait("Category", TestCategories.Unit)]
|
|
[Fact]
|
|
public void EndpointConfig_Path_CanBeSet()
|
|
{
|
|
// Arrange & Act
|
|
var endpoint = new EndpointConfig { Method = "GET", Path = "/api/users/{id}" };
|
|
|
|
// Assert
|
|
endpoint.Path.Should().Be("/api/users/{id}");
|
|
}
|
|
|
|
[Trait("Category", TestCategories.Unit)]
|
|
[Fact]
|
|
public void EndpointConfig_DefaultTimeout_CanBeSet()
|
|
{
|
|
// Arrange
|
|
var endpoint = new EndpointConfig { Method = "GET", Path = "/" };
|
|
|
|
// Act
|
|
endpoint.DefaultTimeout = TimeSpan.FromSeconds(60);
|
|
|
|
// Assert
|
|
endpoint.DefaultTimeout.Should().Be(TimeSpan.FromSeconds(60));
|
|
}
|
|
|
|
[Trait("Category", TestCategories.Unit)]
|
|
[Fact]
|
|
public void EndpointConfig_SupportsStreaming_CanBeSet()
|
|
{
|
|
// Arrange
|
|
var endpoint = new EndpointConfig { Method = "GET", Path = "/" };
|
|
|
|
// Act
|
|
endpoint.SupportsStreaming = true;
|
|
|
|
// Assert
|
|
endpoint.SupportsStreaming.Should().BeTrue();
|
|
}
|
|
|
|
[Trait("Category", TestCategories.Unit)]
|
|
[Fact]
|
|
public void EndpointConfig_RequiringClaims_CanAddClaims()
|
|
{
|
|
// Arrange
|
|
var endpoint = new EndpointConfig { Method = "GET", Path = "/" };
|
|
|
|
// Act
|
|
endpoint.RequiringClaims.Add(new ClaimRequirement { Type = "role", Value = "admin" });
|
|
endpoint.RequiringClaims.Add(new ClaimRequirement { Type = "permission", Value = "read" });
|
|
|
|
// Assert
|
|
endpoint.RequiringClaims.Should().HaveCount(2);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Complex Configuration Tests
|
|
|
|
[Trait("Category", TestCategories.Unit)]
|
|
[Fact]
|
|
public void ServiceConfig_CompleteConfiguration_Works()
|
|
{
|
|
// Arrange & Act
|
|
var config = new ServiceConfig
|
|
{
|
|
ServiceName = "user-service",
|
|
DefaultVersion = "2.0.0",
|
|
DefaultTransport = TransportType.Certificate,
|
|
Endpoints =
|
|
[
|
|
new EndpointConfig
|
|
{
|
|
Method = "GET",
|
|
Path = "/api/users/{id}",
|
|
DefaultTimeout = TimeSpan.FromSeconds(10),
|
|
SupportsStreaming = false,
|
|
RequiringClaims = [new ClaimRequirement { Type = "role", Value = "user" }]
|
|
},
|
|
new EndpointConfig
|
|
{
|
|
Method = "POST",
|
|
Path = "/api/users",
|
|
DefaultTimeout = TimeSpan.FromSeconds(30),
|
|
SupportsStreaming = false,
|
|
RequiringClaims = [new ClaimRequirement { Type = "role", Value = "admin" }]
|
|
},
|
|
new EndpointConfig
|
|
{
|
|
Method = "GET",
|
|
Path = "/api/users/stream",
|
|
DefaultTimeout = TimeSpan.FromMinutes(5),
|
|
SupportsStreaming = true
|
|
}
|
|
]
|
|
};
|
|
|
|
// Assert
|
|
config.ServiceName.Should().Be("user-service");
|
|
config.DefaultVersion.Should().Be("2.0.0");
|
|
config.DefaultTransport.Should().Be(TransportType.Certificate);
|
|
config.Endpoints.Should().HaveCount(3);
|
|
config.Endpoints[0].RequiringClaims.Should().HaveCount(1);
|
|
config.Endpoints[2].SupportsStreaming.Should().BeTrue();
|
|
}
|
|
|
|
#endregion
|
|
}
|