public void WithServiceBusMessageHandler_WithMessageBodyFilterWithImplementationFactory_UsesFilter(bool matchesBody)
        {
            // Arrange
            var collection      = new ServiceBusMessageHandlerCollection(new ServiceCollection());
            var expectedMessage = new TestMessage();
            var expectedHandler = new TestServiceBusMessageHandler();

            // Act
            collection.WithServiceBusMessageHandler <TestServiceBusMessageHandler, TestMessage>(
                messageBodyFilter: body =>
            {
                Assert.Same(expectedMessage, body);
                return(matchesBody);
            },
                implementationFactory: serviceProvider => expectedHandler);

            // Assert
            IServiceProvider             provider = collection.Services.BuildServiceProvider();
            IEnumerable <MessageHandler> handlers = MessageHandler.SubtractFrom(provider, NullLogger.Instance);

            Assert.NotNull(handlers);
            MessageHandler handler = Assert.Single(handlers);

            Assert.NotNull(handler);
            Assert.Same(expectedHandler, handler.GetMessageHandlerInstance());
            Assert.Equal(matchesBody, handler.CanProcessMessageBasedOnMessage(expectedMessage));
        }
Esempio n. 2
0
        public async Task WithServiceBusMessageRouting_WithMessageHandler_GoesThroughRegisteredMessageHandlers()
        {
            // Arrange
            var services       = new ServiceCollection();
            var collection     = new ServiceBusMessageHandlerCollection(services);
            var ignoredHandler = new TestServiceBusMessageHandler();
            var spyHandler     = new StubServiceBusMessageHandler <Order>();

            collection.WithServiceBusMessageHandler <StubServiceBusMessageHandler <Order>, Order>(serviceProvider => spyHandler)
            .WithServiceBusMessageHandler <TestServiceBusMessageHandler, TestMessage>(serviceProvider => ignoredHandler);

            // Act
            services.AddServiceBusMessageRouting();

            // Assert
            IServiceProvider provider = services.BuildServiceProvider();
            var router = provider.GetRequiredService <IAzureServiceBusMessageRouter>();
            AzureServiceBusMessageContext context = AzureServiceBusMessageContextFactory.Generate();
            var correlationInfo = new MessageCorrelationInfo("operation-id", "transaction-id");

            Order order = OrderGenerator.Generate();
            ServiceBusReceivedMessage message = order.AsServiceBusReceivedMessage();

            await router.RouteMessageAsync(message, context, correlationInfo, CancellationToken.None);

            Assert.True(spyHandler.IsProcessed);
            Assert.False(ignoredHandler.IsProcessed);
        }
Esempio n. 3
0
        public void WithServiceBusMessageHandler_WithMessageContextFilterWithMessageBodyFilter_UsesSerializer(bool matchesContext, bool matchesBody)
        {
            // Arrange
            var collection      = new ServiceBusMessageHandlerCollection(new ServiceCollection());
            var expectedMessage = new TestMessage();
            var expectedContext = AzureServiceBusMessageContextFactory.Generate();
            var expectedHandler = new TestServiceBusMessageHandler();

            // Act
            collection.WithServiceBusMessageHandler <TestServiceBusMessageHandler, TestMessage>(
                messageContextFilter: context =>
            {
                Assert.Same(expectedContext, context);
                return(matchesContext);
            },
                messageBodyFilter: body =>
            {
                Assert.Same(expectedMessage, body);
                return(matchesBody);
            });

            // Assert
            IServiceProvider             provider = collection.Services.BuildServiceProvider();
            IEnumerable <MessageHandler> handlers = MessageHandler.SubtractFrom(provider, NullLogger.Instance);

            Assert.NotNull(handlers);
            MessageHandler handler = Assert.Single(handlers);

            Assert.NotNull(handler);
            Assert.NotSame(expectedHandler, handler.GetMessageHandlerInstance());
            Assert.Equal(matchesContext, handler.CanProcessMessageBasedOnContext(expectedContext));
            Assert.Equal(matchesBody, handler.CanProcessMessageBasedOnMessage(expectedMessage));
        }
        public async Task WithMessageHandler_WithMessageBodySerializerWithMessageHandlerImplementationFactory_UsesSerializer()
        {
            // Arrange
            var collection      = new ServiceBusMessageHandlerCollection(new ServiceCollection());
            var expectedBody    = $"test-message-body-{Guid.NewGuid()}";
            var expectedMessage = new TestMessage();
            var serializer      = new TestMessageBodySerializer(expectedBody, expectedMessage);
            var expectedHandler = new TestServiceBusMessageHandler();

            // Act
            collection.WithServiceBusMessageHandler <TestServiceBusMessageHandler, TestMessage>(
                messageBodySerializer: serializer,
                implementationFactory: serviceProvider => expectedHandler);

            // Assert
            IServiceProvider             provider = collection.Services.BuildServiceProvider();
            IEnumerable <MessageHandler> handlers = MessageHandler.SubtractFrom(provider, NullLogger.Instance);

            Assert.NotNull(handlers);
            MessageHandler handler = Assert.Single(handlers);

            Assert.NotNull(handler);
            Assert.Same(expectedHandler, handler.GetMessageHandlerInstance());
            MessageResult result = await handler.TryCustomDeserializeMessageAsync(expectedBody);

            Assert.NotNull(result);
            Assert.Same(expectedMessage, result.DeserializedMessage);
        }
        public async Task WithServiceBusMessageHandler_WithContextFilterWithMessageBodySerializerImplementationFactoryWithMessageBodyFilterWithMessageHandlerImplementationFactory_UsesAll(
            bool matchesContext,
            bool matchesBody)
        {
            // Arrange
            var collection      = new ServiceBusMessageHandlerCollection(new ServiceCollection());
            var expectedBody    = $"test-message-body-{Guid.NewGuid()}";
            var expectedMessage = new TestMessage();
            var expectedContext = AzureServiceBusMessageContextFactory.Generate();
            var serializer      = new TestMessageBodySerializer(expectedBody, expectedMessage);
            var expectedHandler = new TestServiceBusMessageHandler();

            // Act
            collection.WithServiceBusMessageHandler <TestServiceBusMessageHandler, TestMessage>(
                messageContextFilter: context =>
            {
                Assert.Same(expectedContext, context);
                return(matchesContext);
            },
                messageBodySerializerImplementationFactory: serviceProvider => serializer,
                messageBodyFilter: body =>
            {
                Assert.Same(expectedMessage, body);
                return(matchesBody);
            },
                messageHandlerImplementationFactory: serviceProvider => expectedHandler);

            // Assert
            IServiceProvider             provider = collection.Services.BuildServiceProvider();
            IEnumerable <MessageHandler> handlers = MessageHandler.SubtractFrom(provider, NullLogger.Instance);

            Assert.NotNull(handlers);
            MessageHandler handler = Assert.Single(handlers);

            Assert.NotNull(handler);
            Assert.Same(expectedHandler, handler.GetMessageHandlerInstance());
            Assert.Equal(matchesBody, handler.CanProcessMessageBasedOnMessage(expectedMessage));
            Assert.Equal(matchesContext, handler.CanProcessMessageBasedOnContext(expectedContext));
            MessageResult result = await handler.TryCustomDeserializeMessageAsync(expectedBody);

            Assert.NotNull(result);
            Assert.Same(expectedMessage, result.DeserializedMessage);
        }