Ejemplo n.º 1
0
        public async Task WithMessageHandlerWithDefaultContext_WithMessageBodySerializerImplementationFactoryWithMessageBodyFilter_UsesSerializer(bool matches)
        {
            // Arrange
            var services        = new MessageHandlerCollection(new ServiceCollection());
            var expectedBody    = $"test-message-body-{Guid.NewGuid()}";
            var expectedMessage = new TestMessage();
            var serializer      = new TestMessageBodySerializer(expectedBody, expectedMessage);

            // Act
            services.WithMessageHandler <DefaultTestMessageHandler, TestMessage>(
                messageBodySerializerImplementationFactory: serviceProvider => serializer,
                messageBodyFilter: body =>
            {
                Assert.Same(expectedMessage, body);
                return(matches);
            });

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

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

            Assert.NotNull(handler);
            bool actual = handler.CanProcessMessageBasedOnMessage(expectedMessage);

            Assert.Equal(matches, actual);
            MessageResult result = await handler.TryCustomDeserializeMessageAsync(expectedBody);

            Assert.NotNull(result);
            Assert.Same(expectedMessage, result.DeserializedMessage);
        }
Ejemplo n.º 2
0
        public async Task WithServiceBusRouting_WithMessageHandlerMessageBodySerializerSubType_DeserializesCustomMessage()
        {
            // Arrange
            var services       = new ServiceCollection();
            var collection     = new ServiceBusMessageHandlerCollection(services);
            var ignoredHandler = new StubServiceBusMessageHandler <TestMessage>();
            var spyHandler     = new StubServiceBusMessageHandler <Order>();

            var expectedMessage = new TestMessage {
                TestProperty = "Some value"
            };
            string expectedBody = JsonConvert.SerializeObject(expectedMessage);
            var    serializer   = new TestMessageBodySerializer(expectedBody, new SubOrder());

            collection.WithServiceBusMessageHandler <StubServiceBusMessageHandler <Order>, Order>(messageBodySerializer: serializer, implementationFactory: serviceProvider => spyHandler)
            .WithServiceBusMessageHandler <StubServiceBusMessageHandler <TestMessage>, TestMessage>(implementationFactory: 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");

            ServiceBusReceivedMessage message = expectedMessage.AsServiceBusReceivedMessage();
            await router.RouteMessageAsync(message, context, correlationInfo, CancellationToken.None);

            Assert.True(spyHandler.IsProcessed);
            Assert.False(ignoredHandler.IsProcessed);
        }
        public async Task WithMessageHandlerWithDefaultContext_WithMessageBodySerializerWithImplementationFactory_UsesSerializer()
        {
            // Arrange
            var services        = new MessageHandlerCollection(new ServiceCollection());
            var expectedBody    = $"test-message-body-{Guid.NewGuid()}";
            var expectedMessage = new TestMessage();
            var serializer      = new TestMessageBodySerializer(expectedBody, expectedMessage);
            var expectedHandler = new DefaultTestMessageHandler();

            // Act
            services.WithMessageHandler <DefaultTestMessageHandler, TestMessage>(
                messageBodySerializer: serializer, implementationFactory: serviceProvider => expectedHandler);

            // Assert
            IServiceProvider             provider = services.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);
            MessageResult result = await handler.TryCustomDeserializeMessageAsync(expectedBody);

            Assert.NotNull(result);
            Assert.Same(expectedMessage, result.DeserializedMessage);
        }
Ejemplo n.º 4
0
        public async Task WithMessageHandler_WithMessageContextFilterWithMessageBodySerializer_UsesSerializer(bool matches)
        {
            // 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);

            // Act
            collection.WithServiceBusMessageHandler <TestServiceBusMessageHandler, TestMessage>(
                messageContextFilter: context =>
            {
                Assert.Same(expectedContext, context);
                return(matches);
            },
                messageBodySerializer: serializer);

            // 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);
            bool actual = handler.CanProcessMessageBasedOnContext(expectedContext);

            Assert.Equal(matches, actual);
            MessageResult result = await handler.TryCustomDeserializeMessageAsync(expectedBody);

            Assert.NotNull(result);
            Assert.Same(expectedMessage, result.DeserializedMessage);
        }
        public async Task WithServiceBusRouting_WithMessageHandlerCanHandleAllFiltersAtOnce_AndStillFindsTheRightMessageHandler()
        {
            // Arrange
            var services        = new ServiceCollection();
            var collection      = new ServiceBusMessageHandlerCollection(services);
            var ignoredHandler1 = new StubServiceBusMessageHandler <TestMessage>();
            var ignoredHandler2 = new StubServiceBusMessageHandler <TestMessage>();
            var ignoredHandler3 = new StubServiceBusMessageHandler <Order>();
            var spyHandler      = new StubServiceBusMessageHandler <Order>();

            AzureServiceBusMessageContext context = AzureServiceBusMessageContextFactory.Generate();
            var expectedMessage = new TestMessage {
                TestProperty = "Some value"
            };
            string expectedBody = JsonConvert.SerializeObject(expectedMessage);
            var    serializer   = new TestMessageBodySerializer(expectedBody, OrderGenerator.Generate());

            collection
            .WithServiceBusMessageHandler <StubServiceBusMessageHandler <Order>, Order>(
                messageContextFilter: ctx => ctx != null,
                messageBodyFilter: body => body != null,
                messageBodySerializer: new TestMessageBodySerializer(expectedBody, new Customer()),
                implementationFactory: serviceProvider => ignoredHandler3)
            .WithServiceBusMessageHandler <StubServiceBusMessageHandler <TestMessage>, TestMessage>(
                messageBodyFilter: body => body is null,
                implementationFactory: serviceProvider => ignoredHandler2)
            .WithServiceBusMessageHandler <StubServiceBusMessageHandler <Order>, Order>(
                messageBodySerializer: serializer,
                messageBodyFilter: body => body.Customer != null,
                messageContextFilter: ctx => ctx.MessageId.StartsWith("message-id"),
                implementationFactory: serviceProvider => spyHandler)
            .WithServiceBusMessageHandler <StubServiceBusMessageHandler <TestMessage>, TestMessage>()
            .WithServiceBusMessageHandler <StubServiceBusMessageHandler <TestMessage>, TestMessage>(
                implementationFactory: serviceProvider => ignoredHandler1);

            // Act
            services.AddServiceBusMessageRouting();

            // Assert
            IServiceProvider provider = services.BuildServiceProvider();
            var router = provider.GetRequiredService <IAzureServiceBusMessageRouter>();

            var correlationInfo = new MessageCorrelationInfo("operation-id", "transaction-id");
            ServiceBusReceivedMessage message = expectedMessage.AsServiceBusReceivedMessage();
            await router.RouteMessageAsync(message, context, correlationInfo, CancellationToken.None);

            Assert.True(spyHandler.IsProcessed);
            Assert.False(ignoredHandler1.IsProcessed);
            Assert.False(ignoredHandler2.IsProcessed);
            Assert.False(ignoredHandler3.IsProcessed);
        }
Ejemplo n.º 6
0
        public async Task WithMessageRouting_WithMessageHandlerMessageBodySerializer_GoesThroughRegisteredMessageHandlers()
        {
            // Arrange
            var services               = new ServiceCollection();
            var collection             = new MessageHandlerCollection(services);
            var spyHandler             = new StubTestMessageHandler <TestMessage, TestMessageContext>();
            var ignoredSameTypeHandler = new StubTestMessageHandler <TestMessage, TestMessageContext>();
            var ignoredWrongDeserializedTypeHandler = new StubTestMessageHandler <Order, TestMessageContext>();
            var ignoredDefaultHandler = new DefaultTestMessageHandler();
            var ignoredHandler        = new TestMessageHandler();

            var expectedMessage = new TestMessage {
                TestProperty = "Some value"
            };
            string expectedBody = JsonConvert.SerializeObject(expectedMessage);
            var    serializer   = new TestMessageBodySerializer(expectedBody, expectedMessage);

            collection.WithMessageHandler <DefaultTestMessageHandler, TestMessage>(serviceProvider => ignoredDefaultHandler)
            .WithMessageHandler <StubTestMessageHandler <Order, TestMessageContext>, Order, TestMessageContext>(
                messageBodySerializer: new TestMessageBodySerializer(expectedBody, new Customer()),
                implementationFactory: serviceProvider => ignoredWrongDeserializedTypeHandler)
            .WithMessageHandler <StubTestMessageHandler <TestMessage, TestMessageContext>, TestMessage, TestMessageContext>(
                messageContextFilter: ctx => false, implementationFactory: serviceProvider => ignoredSameTypeHandler)
            .WithMessageHandler <StubTestMessageHandler <TestMessage, TestMessageContext>, TestMessage, TestMessageContext>(
                messageBodySerializer: serializer,
                implementationFactory: serviceProvider => spyHandler)
            .WithMessageHandler <TestMessageHandler, TestMessage, TestMessageContext>(serviceProvider => ignoredHandler);

            // Act
            services.AddMessageRouting();

            // Assert
            IServiceProvider provider = services.BuildServiceProvider();
            var router = provider.GetRequiredService <IMessageRouter>();

            var correlationInfo = new MessageCorrelationInfo("operation-id", "transaction-id");
            var context         = TestMessageContext.Generate();
            await router.RouteMessageAsync(expectedBody, context, correlationInfo, CancellationToken.None);

            Assert.True(spyHandler.IsProcessed);
            Assert.False(ignoredSameTypeHandler.IsProcessed);
            Assert.False(ignoredDefaultHandler.IsProcessed);
            Assert.False(ignoredWrongDeserializedTypeHandler.IsProcessed);
            Assert.False(ignoredHandler.IsProcessed);
        }
Ejemplo n.º 7
0
        public async Task WithMessageHandlerCustomContext_WithContextFilterWithMessageBodySerializerWithMessageBodyFilterWithMessageHandlerImplementationFactory_UsesFilter(bool matchesContext, bool matchesBody)
        {
            // Arrange
            var services        = new MessageHandlerCollection(new ServiceCollection());
            var expectedBody    = $"test-message-body-{Guid.NewGuid()}";
            var expectedMessage = new TestMessage();
            var expectedContext = TestMessageContext.Generate();
            var serializer      = new TestMessageBodySerializer(expectedBody, expectedMessage);
            var expectedHandler = new TestMessageHandler();

            // Act
            services.WithMessageHandler <TestMessageHandler, TestMessage, TestMessageContext>(
                messageContextFilter: context =>
            {
                Assert.Same(expectedContext, context);
                return(matchesContext);
            },
                messageBodySerializer: serializer,
                messageBodyFilter: body =>
            {
                Assert.Same(expectedMessage, body);
                return(matchesBody);
            },
                implementationFactory: serviceProvider => expectedHandler);

            // Assert
            IServiceProvider             provider = services.Services.BuildServiceProvider();
            IEnumerable <MessageHandler> handlers = MessageHandler.SubtractFrom(provider, NullLogger.Instance);
            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);
        }
        public async Task WithMessageHandler_WithMessageBodySerializer_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);

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

            // 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);
            MessageResult result = await handler.TryCustomDeserializeMessageAsync(expectedBody);

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