Add unit tests and logging infrastructure for InMemory and RabbitMQ transports
Some checks failed
Docs CI / lint-and-preview (push) Has been cancelled

- Implemented RecordingLogger and RecordingLoggerFactory for capturing log entries in tests.
- Added unit tests for InMemoryChannel, covering constructor behavior, property assignments, channel communication, and disposal.
- Created InMemoryTransportOptionsTests to validate default values and customizable options for InMemory transport.
- Developed RabbitMqFrameProtocolTests to ensure correct parsing and property creation for RabbitMQ frames.
- Added RabbitMqTransportOptionsTests to verify default settings and customization options for RabbitMQ transport.
- Updated project files for testing libraries and dependencies.
This commit is contained in:
StellaOps Bot
2025-12-05 09:38:45 +02:00
parent 6a299d231f
commit 53508ceccb
98 changed files with 10868 additions and 663 deletions

View File

@@ -0,0 +1,253 @@
using StellaOps.Router.Common.Enums;
using StellaOps.Router.Common.Models;
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
[Fact]
public void ServiceConfig_DefaultVersion_DefaultsToNull()
{
// Arrange & Act
var config = new ServiceConfig { ServiceName = "test" };
// Assert
config.DefaultVersion.Should().BeNull();
}
[Fact]
public void ServiceConfig_DefaultTransport_DefaultsToTcp()
{
// Arrange & Act
var config = new ServiceConfig { ServiceName = "test" };
// Assert
config.DefaultTransport.Should().Be(TransportType.Tcp);
}
[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
[Fact]
public void ServiceConfig_ServiceName_CanBeSet()
{
// Arrange & Act
var config = new ServiceConfig { ServiceName = "my-service" };
// Assert
config.ServiceName.Should().Be("my-service");
}
[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");
}
[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);
}
[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
[Fact]
public void EndpointConfig_DefaultTimeout_DefaultsToNull()
{
// Arrange & Act
var endpoint = new EndpointConfig { Method = "GET", Path = "/" };
// Assert
endpoint.DefaultTimeout.Should().BeNull();
}
[Fact]
public void EndpointConfig_SupportsStreaming_DefaultsToFalse()
{
// Arrange & Act
var endpoint = new EndpointConfig { Method = "GET", Path = "/" };
// Assert
endpoint.SupportsStreaming.Should().BeFalse();
}
[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
[Fact]
public void EndpointConfig_Method_CanBeSet()
{
// Arrange & Act
var endpoint = new EndpointConfig { Method = "DELETE", Path = "/" };
// Assert
endpoint.Method.Should().Be("DELETE");
}
[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}");
}
[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));
}
[Fact]
public void EndpointConfig_SupportsStreaming_CanBeSet()
{
// Arrange
var endpoint = new EndpointConfig { Method = "GET", Path = "/" };
// Act
endpoint.SupportsStreaming = true;
// Assert
endpoint.SupportsStreaming.Should().BeTrue();
}
[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
[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
}