Add unit tests for RabbitMq and Udp transport servers and clients
Some checks failed
Docs CI / lint-and-preview (push) Has been cancelled
Some checks failed
Docs CI / lint-and-preview (push) Has been cancelled
- Implemented comprehensive unit tests for RabbitMqTransportServer, covering constructor, disposal, connection management, event handlers, and exception handling. - Added configuration tests for RabbitMqTransportServer to validate SSL, durable queues, auto-recovery, and custom virtual host options. - Created unit tests for UdpFrameProtocol, including frame parsing and serialization, header size validation, and round-trip data preservation. - Developed tests for UdpTransportClient, focusing on connection handling, event subscriptions, and exception scenarios. - Established tests for UdpTransportServer, ensuring proper start/stop behavior, connection state management, and event handling. - Included tests for UdpTransportOptions to verify default values and modification capabilities. - Enhanced service registration tests for Udp transport services in the dependency injection container.
This commit is contained in:
@@ -0,0 +1,197 @@
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Moq;
|
||||
using StellaOps.Router.Common.Models;
|
||||
|
||||
namespace StellaOps.Microservice.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Unit tests for <see cref="EndpointDiscoveryService"/>.
|
||||
/// </summary>
|
||||
public sealed class EndpointDiscoveryServiceTests
|
||||
{
|
||||
private readonly Mock<IEndpointDiscoveryProvider> _discoveryProviderMock;
|
||||
private readonly Mock<IMicroserviceYamlLoader> _yamlLoaderMock;
|
||||
private readonly Mock<IEndpointOverrideMerger> _mergerMock;
|
||||
|
||||
public EndpointDiscoveryServiceTests()
|
||||
{
|
||||
_discoveryProviderMock = new Mock<IEndpointDiscoveryProvider>();
|
||||
_yamlLoaderMock = new Mock<IMicroserviceYamlLoader>();
|
||||
_mergerMock = new Mock<IEndpointOverrideMerger>();
|
||||
|
||||
// Default setups
|
||||
_discoveryProviderMock.Setup(d => d.DiscoverEndpoints())
|
||||
.Returns(new List<EndpointDescriptor>());
|
||||
_yamlLoaderMock.Setup(l => l.Load())
|
||||
.Returns((MicroserviceYamlConfig?)null);
|
||||
_mergerMock.Setup(m => m.Merge(It.IsAny<IReadOnlyList<EndpointDescriptor>>(), It.IsAny<MicroserviceYamlConfig?>()))
|
||||
.Returns<IReadOnlyList<EndpointDescriptor>, MicroserviceYamlConfig?>((e, _) => e);
|
||||
}
|
||||
|
||||
private EndpointDiscoveryService CreateService()
|
||||
{
|
||||
return new EndpointDiscoveryService(
|
||||
_discoveryProviderMock.Object,
|
||||
_yamlLoaderMock.Object,
|
||||
_mergerMock.Object,
|
||||
NullLogger<EndpointDiscoveryService>.Instance);
|
||||
}
|
||||
|
||||
#region DiscoverEndpoints Tests
|
||||
|
||||
[Fact]
|
||||
public void DiscoverEndpoints_CallsDiscoveryProvider()
|
||||
{
|
||||
// Arrange
|
||||
var service = CreateService();
|
||||
|
||||
// Act
|
||||
service.DiscoverEndpoints();
|
||||
|
||||
// Assert
|
||||
_discoveryProviderMock.Verify(d => d.DiscoverEndpoints(), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiscoverEndpoints_LoadsYamlConfig()
|
||||
{
|
||||
// Arrange
|
||||
var service = CreateService();
|
||||
|
||||
// Act
|
||||
service.DiscoverEndpoints();
|
||||
|
||||
// Assert
|
||||
_yamlLoaderMock.Verify(l => l.Load(), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiscoverEndpoints_MergesCodeAndYaml()
|
||||
{
|
||||
// Arrange
|
||||
var codeEndpoints = new List<EndpointDescriptor>
|
||||
{
|
||||
new() { ServiceName = "test", Version = "1.0", Method = "GET", Path = "/api/users" }
|
||||
};
|
||||
_discoveryProviderMock.Setup(d => d.DiscoverEndpoints()).Returns(codeEndpoints);
|
||||
|
||||
var yamlConfig = new MicroserviceYamlConfig
|
||||
{
|
||||
Endpoints =
|
||||
[
|
||||
new EndpointOverrideConfig { Method = "GET", Path = "/api/users", DefaultTimeout = "30s" }
|
||||
]
|
||||
};
|
||||
_yamlLoaderMock.Setup(l => l.Load()).Returns(yamlConfig);
|
||||
|
||||
var service = CreateService();
|
||||
|
||||
// Act
|
||||
service.DiscoverEndpoints();
|
||||
|
||||
// Assert
|
||||
_mergerMock.Verify(m => m.Merge(codeEndpoints, yamlConfig), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiscoverEndpoints_ReturnsMergedEndpoints()
|
||||
{
|
||||
// Arrange
|
||||
var codeEndpoints = new List<EndpointDescriptor>
|
||||
{
|
||||
new() { ServiceName = "test", Version = "1.0", Method = "GET", Path = "/api/users" }
|
||||
};
|
||||
_discoveryProviderMock.Setup(d => d.DiscoverEndpoints()).Returns(codeEndpoints);
|
||||
|
||||
var mergedEndpoints = new List<EndpointDescriptor>
|
||||
{
|
||||
new() { ServiceName = "test", Version = "1.0", Method = "GET", Path = "/api/users", DefaultTimeout = TimeSpan.FromSeconds(30) }
|
||||
};
|
||||
_mergerMock.Setup(m => m.Merge(It.IsAny<IReadOnlyList<EndpointDescriptor>>(), It.IsAny<MicroserviceYamlConfig?>()))
|
||||
.Returns(mergedEndpoints);
|
||||
|
||||
var service = CreateService();
|
||||
|
||||
// Act
|
||||
var result = service.DiscoverEndpoints();
|
||||
|
||||
// Assert
|
||||
result.Should().BeSameAs(mergedEndpoints);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiscoverEndpoints_WhenYamlLoadFails_UsesCodeEndpointsOnly()
|
||||
{
|
||||
// Arrange
|
||||
var codeEndpoints = new List<EndpointDescriptor>
|
||||
{
|
||||
new() { ServiceName = "test", Version = "1.0", Method = "GET", Path = "/api/users" }
|
||||
};
|
||||
_discoveryProviderMock.Setup(d => d.DiscoverEndpoints()).Returns(codeEndpoints);
|
||||
_yamlLoaderMock.Setup(l => l.Load()).Throws(new FileNotFoundException("YAML not found"));
|
||||
|
||||
var service = CreateService();
|
||||
|
||||
// Act
|
||||
service.DiscoverEndpoints();
|
||||
|
||||
// Assert - merger should be called with null config
|
||||
_mergerMock.Verify(m => m.Merge(codeEndpoints, null), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiscoverEndpoints_WithMultipleEndpoints_ReturnsAll()
|
||||
{
|
||||
// Arrange
|
||||
var endpoints = new List<EndpointDescriptor>
|
||||
{
|
||||
new() { ServiceName = "test", Version = "1.0", Method = "GET", Path = "/api/users" },
|
||||
new() { ServiceName = "test", Version = "1.0", Method = "POST", Path = "/api/users" },
|
||||
new() { ServiceName = "test", Version = "1.0", Method = "GET", Path = "/api/users/{id}" },
|
||||
new() { ServiceName = "test", Version = "1.0", Method = "DELETE", Path = "/api/users/{id}" }
|
||||
};
|
||||
_discoveryProviderMock.Setup(d => d.DiscoverEndpoints()).Returns(endpoints);
|
||||
_mergerMock.Setup(m => m.Merge(endpoints, null)).Returns(endpoints);
|
||||
|
||||
var service = CreateService();
|
||||
|
||||
// Act
|
||||
var result = service.DiscoverEndpoints();
|
||||
|
||||
// Assert
|
||||
result.Should().HaveCount(4);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiscoverEndpoints_EmptyEndpoints_ReturnsEmptyList()
|
||||
{
|
||||
// Arrange
|
||||
_discoveryProviderMock.Setup(d => d.DiscoverEndpoints()).Returns(new List<EndpointDescriptor>());
|
||||
_mergerMock.Setup(m => m.Merge(It.IsAny<IReadOnlyList<EndpointDescriptor>>(), null))
|
||||
.Returns(new List<EndpointDescriptor>());
|
||||
|
||||
var service = CreateService();
|
||||
|
||||
// Act
|
||||
var result = service.DiscoverEndpoints();
|
||||
|
||||
// Assert
|
||||
result.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiscoverEndpoints_CanBeCalledMultipleTimes()
|
||||
{
|
||||
// Arrange
|
||||
var service = CreateService();
|
||||
|
||||
// Act
|
||||
var result1 = service.DiscoverEndpoints();
|
||||
var result2 = service.DiscoverEndpoints();
|
||||
|
||||
// Assert
|
||||
_discoveryProviderMock.Verify(d => d.DiscoverEndpoints(), Times.Exactly(2));
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
Reference in New Issue
Block a user