product advisories, stella router improval, tests streghthening
This commit is contained in:
@@ -0,0 +1,215 @@
|
||||
using System.Text.Json;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Microsoft.Extensions.Options;
|
||||
using StellaOps.Gateway.WebService.Configuration;
|
||||
using GatewayClaimsStore = StellaOps.Gateway.WebService.Authorization.IEffectiveClaimsStore;
|
||||
using StellaOps.Gateway.WebService.Services;
|
||||
using StellaOps.Messaging;
|
||||
using StellaOps.Messaging.Abstractions;
|
||||
using StellaOps.Router.Common.Abstractions;
|
||||
using StellaOps.Router.Common.Enums;
|
||||
using StellaOps.Router.Common.Models;
|
||||
using StellaOps.Router.Transport.Messaging;
|
||||
using StellaOps.Router.Transport.Messaging.Options;
|
||||
using StellaOps.Router.Transport.Tcp;
|
||||
using StellaOps.Router.Transport.Tls;
|
||||
|
||||
namespace StellaOps.Gateway.WebService.Tests.Integration;
|
||||
|
||||
/// <summary>
|
||||
/// Unit tests for the messaging transport integration in GatewayHostedService and GatewayTransportClient.
|
||||
/// These tests verify the wiring and event handling without requiring a real Valkey instance.
|
||||
/// </summary>
|
||||
public sealed class MessagingTransportIntegrationTests
|
||||
{
|
||||
private readonly JsonSerializerOptions _jsonOptions;
|
||||
|
||||
public MessagingTransportIntegrationTests()
|
||||
{
|
||||
_jsonOptions = new JsonSerializerOptions
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
WriteIndented = false
|
||||
};
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GatewayHostedService_CanAcceptMessagingServer()
|
||||
{
|
||||
// Arrange
|
||||
var mockQueueFactory = new Mock<IMessageQueueFactory>();
|
||||
var messagingOptions = Options.Create(new MessagingTransportOptions());
|
||||
|
||||
var messagingServer = new MessagingTransportServer(
|
||||
mockQueueFactory.Object,
|
||||
messagingOptions,
|
||||
NullLogger<MessagingTransportServer>.Instance);
|
||||
|
||||
var gatewayOptions = Options.Create(new GatewayOptions());
|
||||
|
||||
var routingState = new Mock<IGlobalRoutingState>();
|
||||
var claimsStore = new Mock<GatewayClaimsStore>();
|
||||
|
||||
var tcpOptions = Options.Create(new TcpTransportOptions { Port = 29100 });
|
||||
var tlsOptions = Options.Create(new TlsTransportOptions { Port = 29443 });
|
||||
var tcpServer = new TcpTransportServer(tcpOptions, NullLogger<TcpTransportServer>.Instance);
|
||||
var tlsServer = new TlsTransportServer(tlsOptions, NullLogger<TlsTransportServer>.Instance);
|
||||
|
||||
var transportClient = new GatewayTransportClient(
|
||||
tcpServer,
|
||||
tlsServer,
|
||||
NullLogger<GatewayTransportClient>.Instance,
|
||||
messagingServer);
|
||||
|
||||
// Act & Assert - construction should succeed with messaging server
|
||||
var hostedService = new GatewayHostedService(
|
||||
tcpServer,
|
||||
tlsServer,
|
||||
routingState.Object,
|
||||
transportClient,
|
||||
claimsStore.Object,
|
||||
gatewayOptions,
|
||||
new GatewayServiceStatus(),
|
||||
NullLogger<GatewayHostedService>.Instance,
|
||||
openApiCache: null,
|
||||
messagingServer: messagingServer);
|
||||
|
||||
Assert.NotNull(hostedService);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GatewayHostedService_CanAcceptNullMessagingServer()
|
||||
{
|
||||
// Arrange
|
||||
var gatewayOptions = Options.Create(new GatewayOptions());
|
||||
|
||||
var routingState = new Mock<IGlobalRoutingState>();
|
||||
var claimsStore = new Mock<GatewayClaimsStore>();
|
||||
|
||||
var tcpOptions = Options.Create(new TcpTransportOptions { Port = 29101 });
|
||||
var tlsOptions = Options.Create(new TlsTransportOptions { Port = 29444 });
|
||||
var tcpServer = new TcpTransportServer(tcpOptions, NullLogger<TcpTransportServer>.Instance);
|
||||
var tlsServer = new TlsTransportServer(tlsOptions, NullLogger<TlsTransportServer>.Instance);
|
||||
|
||||
var transportClient = new GatewayTransportClient(
|
||||
tcpServer,
|
||||
tlsServer,
|
||||
NullLogger<GatewayTransportClient>.Instance,
|
||||
messagingServer: null);
|
||||
|
||||
// Act & Assert - construction should succeed without messaging server
|
||||
var hostedService = new GatewayHostedService(
|
||||
tcpServer,
|
||||
tlsServer,
|
||||
routingState.Object,
|
||||
transportClient,
|
||||
claimsStore.Object,
|
||||
gatewayOptions,
|
||||
new GatewayServiceStatus(),
|
||||
NullLogger<GatewayHostedService>.Instance,
|
||||
openApiCache: null,
|
||||
messagingServer: null);
|
||||
|
||||
Assert.NotNull(hostedService);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GatewayTransportClient_WithMessagingServer_CanBeConstructed()
|
||||
{
|
||||
// Arrange
|
||||
var mockQueueFactory = new Mock<IMessageQueueFactory>();
|
||||
var messagingOptions = Options.Create(new MessagingTransportOptions());
|
||||
|
||||
var messagingServer = new MessagingTransportServer(
|
||||
mockQueueFactory.Object,
|
||||
messagingOptions,
|
||||
NullLogger<MessagingTransportServer>.Instance);
|
||||
|
||||
var tcpOptions = Options.Create(new TcpTransportOptions { Port = 29102 });
|
||||
var tlsOptions = Options.Create(new TlsTransportOptions { Port = 29445 });
|
||||
var tcpServer = new TcpTransportServer(tcpOptions, NullLogger<TcpTransportServer>.Instance);
|
||||
var tlsServer = new TlsTransportServer(tlsOptions, NullLogger<TlsTransportServer>.Instance);
|
||||
|
||||
// Act
|
||||
var transportClient = new GatewayTransportClient(
|
||||
tcpServer,
|
||||
tlsServer,
|
||||
NullLogger<GatewayTransportClient>.Instance,
|
||||
messagingServer);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(transportClient);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GatewayTransportClient_SendToMessagingConnection_ThrowsWhenServerNull()
|
||||
{
|
||||
// Arrange
|
||||
var tcpOptions = Options.Create(new TcpTransportOptions { Port = 29103 });
|
||||
var tlsOptions = Options.Create(new TlsTransportOptions { Port = 29446 });
|
||||
var tcpServer = new TcpTransportServer(tcpOptions, NullLogger<TcpTransportServer>.Instance);
|
||||
var tlsServer = new TlsTransportServer(tlsOptions, NullLogger<TlsTransportServer>.Instance);
|
||||
|
||||
// No messaging server provided
|
||||
var transportClient = new GatewayTransportClient(
|
||||
tcpServer,
|
||||
tlsServer,
|
||||
NullLogger<GatewayTransportClient>.Instance,
|
||||
messagingServer: null);
|
||||
|
||||
var connection = new ConnectionState
|
||||
{
|
||||
ConnectionId = "msg-conn-001",
|
||||
Instance = new InstanceDescriptor
|
||||
{
|
||||
InstanceId = "test-001",
|
||||
ServiceName = "test-service",
|
||||
Version = "1.0.0",
|
||||
Region = "test"
|
||||
},
|
||||
TransportType = TransportType.Messaging
|
||||
};
|
||||
|
||||
var frame = new Frame
|
||||
{
|
||||
Type = FrameType.Request,
|
||||
CorrelationId = Guid.NewGuid().ToString("N"),
|
||||
Payload = new byte[] { 1, 2, 3 }
|
||||
};
|
||||
|
||||
// Act & Assert
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(async () =>
|
||||
await transportClient.SendRequestAsync(connection, frame, TimeSpan.FromSeconds(5), CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GatewayOptions_MessagingTransport_HasCorrectDefaults()
|
||||
{
|
||||
// Arrange & Act
|
||||
var options = new GatewayMessagingTransportOptions();
|
||||
|
||||
// Assert
|
||||
Assert.False(options.Enabled);
|
||||
Assert.Equal("localhost:6379", options.ConnectionString);
|
||||
Assert.Null(options.Database);
|
||||
Assert.Equal("router:requests:{service}", options.RequestQueueTemplate);
|
||||
Assert.Equal("router:responses", options.ResponseQueueName);
|
||||
Assert.Equal("router-gateway", options.ConsumerGroup);
|
||||
Assert.Equal("30s", options.RequestTimeout);
|
||||
Assert.Equal("5m", options.LeaseDuration);
|
||||
Assert.Equal(10, options.BatchSize);
|
||||
Assert.Equal("10s", options.HeartbeatInterval);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GatewayTransportOptions_IncludesMessaging()
|
||||
{
|
||||
// Arrange & Act
|
||||
var options = new GatewayTransportOptions();
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(options.Tcp);
|
||||
Assert.NotNull(options.Tls);
|
||||
Assert.NotNull(options.Messaging);
|
||||
}
|
||||
}
|
||||
@@ -29,6 +29,8 @@
|
||||
<ProjectReference Include="..\..\StellaOps.Gateway.WebService\StellaOps.Gateway.WebService.csproj" />
|
||||
<ProjectReference Include="..\..\..\__Libraries\StellaOps.Router.Gateway\StellaOps.Router.Gateway.csproj" />
|
||||
<ProjectReference Include="..\..\..\__Libraries\StellaOps.Router.Transport.InMemory\StellaOps.Router.Transport.InMemory.csproj" />
|
||||
<ProjectReference Include="..\..\..\__Libraries\StellaOps.Router.Transport.Messaging\StellaOps.Router.Transport.Messaging.csproj" />
|
||||
<ProjectReference Include="..\..\..\__Libraries\StellaOps.Messaging\StellaOps.Messaging.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user