Add RabbitMQ integration tests and URL parsing logic
Enhanced test coverage for RabbitMQ integration by adding: - New package references for dependency injection, logging, and RabbitMQ. - `EmailSenderCollection` and `EmailSenderFixture` for managing static client lifecycle in integration tests. - Integration tests for `EmailSender` and `OutgoingEmailPublisher` to verify connection management, message publishing, and serialization. - `EmailSenderUrlOverloadTests` to validate URL-based connection overload behavior. - `RabbitMqTestConfig` for centralized RabbitMQ test configuration. - Unit tests for URL parsing logic in `EmailSenderUrlParsingTests`. These changes improve reliability, maintainability, and test coverage for the messaging service.
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
using DigitalData.MessagingService.Client.DependencyInjection;
|
||||
using DigitalData.MessagingService.Publisher.Abstraction;
|
||||
|
||||
namespace DigitalData.MessagingService.Tests.Integration;
|
||||
|
||||
/// <summary>
|
||||
/// Integration tests for the URL-based <see cref="EmailSender.ConnectRabbitMq(string, string, string, OnReconnect)"/>
|
||||
/// overload. All tests run inside <see cref="EmailSenderCollection"/> so they share the already-established
|
||||
/// static connection. The URL overload is exercised via <see cref="OnReconnect.Ignore"/> and
|
||||
/// <see cref="OnReconnect.ThrowException"/> to verify its delegation and guard behavior.
|
||||
/// </summary>
|
||||
[Collection(EmailSenderCollection.Name)]
|
||||
public sealed class EmailSenderUrlOverloadTests
|
||||
{
|
||||
private static readonly string ValidUrl = $"amqp://{RabbitMqTestConfig.HostName}:{RabbitMqTestConfig.Port}";
|
||||
private static readonly string ValidUrlWithVHost = $"amqp://{RabbitMqTestConfig.HostName}:{RabbitMqTestConfig.Port}/myvhost";
|
||||
|
||||
[Fact]
|
||||
public void ConnectRabbitMq_UrlOverload_WhenAlreadyConnected_WithIgnore_DoesNotThrow()
|
||||
{
|
||||
var exception = Record.Exception(() =>
|
||||
EmailSender.ConnectRabbitMq(
|
||||
ValidUrl,
|
||||
RabbitMqTestConfig.UserName,
|
||||
RabbitMqTestConfig.Password,
|
||||
OnReconnect.Ignore));
|
||||
|
||||
Assert.Null(exception);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ConnectRabbitMq_UrlOverload_WhenAlreadyConnected_WithThrowException_Throws()
|
||||
{
|
||||
Assert.Throws<InvalidOperationException>(() =>
|
||||
EmailSender.ConnectRabbitMq(
|
||||
ValidUrl,
|
||||
RabbitMqTestConfig.UserName,
|
||||
RabbitMqTestConfig.Password,
|
||||
OnReconnect.ThrowException));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ConnectRabbitMq_UrlOverload_WithVHostInPath_WithIgnore_DoesNotThrow()
|
||||
{
|
||||
var exception = Record.Exception(() =>
|
||||
EmailSender.ConnectRabbitMq(
|
||||
ValidUrlWithVHost,
|
||||
RabbitMqTestConfig.UserName,
|
||||
RabbitMqTestConfig.Password,
|
||||
OnReconnect.Ignore));
|
||||
|
||||
Assert.Null(exception);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Send_AfterUrlOverloadConnection_DoesNotThrow()
|
||||
{
|
||||
var email = new OutgoingEmailEvent
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Recipient = "url-overload-test@example.com",
|
||||
Subject = "URL Overload Integration Test",
|
||||
Body = "<p>Sent after URL-based connection.</p>",
|
||||
IsHtml = true,
|
||||
QueuedAt = DateTime.Now
|
||||
};
|
||||
|
||||
// Connection was established via Action<> overload in the fixture;
|
||||
// Send should work regardless of which overload was used to connect.
|
||||
var exception = Record.Exception(() => EmailSender.Send(email));
|
||||
|
||||
Assert.Null(exception);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user