|
| 1 | +namespace SWEN3.Paperless.RabbitMq.Tests.Unit; |
| 2 | + |
| 3 | +public class RabbitMqExtensionsTests |
| 4 | +{ |
| 5 | + [Fact] |
| 6 | + public void AddPaperlessRabbitMq_WithValidConfig_ShouldRegisterAllServicesWithoutConnecting() |
| 7 | + { |
| 8 | + var services = new ServiceCollection(); |
| 9 | + var connectionFactoryMock = new Mock<IConnectionFactory>(); |
| 10 | + var configuration = new ConfigurationBuilder() |
| 11 | + .AddInMemoryCollection(new Dictionary<string, string?> |
| 12 | + { |
| 13 | + ["RabbitMQ:Uri"] = "amqp://guest:guest@localhost:5672" |
| 14 | + }) |
| 15 | + .Build(); |
| 16 | + |
| 17 | + services.AddLogging(); |
| 18 | + services.AddPaperlessRabbitMq(configuration, includeOcrResultStream: false, includeGenAiResultStream: false); |
| 19 | + services.AddSingleton<IConnectionFactory>(_ => connectionFactoryMock.Object); |
| 20 | + services.AddSingleton<IConnection>(_ => Mock.Of<IConnection>()); |
| 21 | + services.AddSingleton<IChannel>(_ => Mock.Of<IChannel>()); |
| 22 | + |
| 23 | + var provider = services.BuildServiceProvider(); |
| 24 | + provider.GetRequiredService<IConnection>().Should().NotBeNull(); |
| 25 | + provider.GetRequiredService<IRabbitMqPublisher>().Should().NotBeNull(); |
| 26 | + provider.GetRequiredService<IRabbitMqConsumerFactory>().Should().NotBeNull(); |
| 27 | + provider.GetServices<IHostedService>().Should().ContainSingle(s => s is RabbitMqTopologySetup); |
| 28 | + provider.GetService<ISseStream<OcrEvent>>().Should().BeNull(); |
| 29 | + provider.GetService<ISseStream<GenAIEvent>>().Should().BeNull(); |
| 30 | + } |
| 31 | + |
| 32 | + [Fact] |
| 33 | + public void AddPaperlessRabbitMq_WithMissingUri_ShouldThrow() |
| 34 | + { |
| 35 | + var services = new ServiceCollection(); |
| 36 | + var emptyConfig = new ConfigurationBuilder().Build(); |
| 37 | + |
| 38 | + var act = () => services.AddPaperlessRabbitMq(emptyConfig); |
| 39 | + |
| 40 | + act.Should().Throw<InvalidOperationException>().WithMessage("Configuration value 'RabbitMQ:Uri' is missing"); |
| 41 | + } |
| 42 | + |
| 43 | + [Fact] |
| 44 | + public void AddPaperlessRabbitMq_WithSseStreamsEnabled_ShouldRegisterStreams() |
| 45 | + { |
| 46 | + var services = new ServiceCollection(); |
| 47 | + var configuration = new ConfigurationBuilder() |
| 48 | + .AddInMemoryCollection(new Dictionary<string, string?> |
| 49 | + { |
| 50 | + ["RabbitMQ:Uri"] = "amqp://guest:guest@localhost:5672" |
| 51 | + }) |
| 52 | + .Build(); |
| 53 | + |
| 54 | + services.AddPaperlessRabbitMq(configuration, includeOcrResultStream: true, includeGenAiResultStream: true); |
| 55 | + |
| 56 | + var provider = services.BuildServiceProvider(); |
| 57 | + provider.GetService<ISseStream<OcrEvent>>().Should().NotBeNull(); |
| 58 | + provider.GetService<ISseStream<GenAIEvent>>().Should().NotBeNull(); |
| 59 | + } |
| 60 | +} |
0 commit comments