示例#1
0
        public void CanPublish()
        {
            var mockRepository = new MockRepository();
            var config = mockRepository.DynamicMock<IBrokerConfiguration>();
            var connFactory = mockRepository.DynamicMock<IBrokerConnectionFactory>();
            var conn = mockRepository.DynamicMock<IBrokerConnection>();
            var consumer = mockRepository.DynamicMock<IRegisteredConsumer>();
            var message = new TestMessage();

            using (mockRepository.Record())
            {
                SetupResult.For(consumer.MessageType).Return(typeof(TestMessage));
                SetupResult.For(connFactory.CreateConnection()).Return(conn);
                SetupResult.For(config.Exchange).Return("ex");
                SetupResult.For(config.ConnectionFactory).Return(connFactory);
                SetupResult.For(config.RegisteredConsumers).Return(new Dictionary<Type, IList<IRegisteredConsumer>> { { typeof(TestMessage), new List<IRegisteredConsumer> { consumer } } }); ;
                SetupResult.For(conn.IsOpen).Return(true);

                Expect.Call(() => conn.Publish<TestMessage>(null))
                    .IgnoreArguments()
                    .WhenCalled(mi =>
                            {
                                var envelope = mi.Arguments[0];

                                Assert.IsAssignableFrom(typeof(IMessageEnvelope<TestMessage>), envelope);
                                Assert.Same(message, ((IMessageEnvelope<TestMessage>)envelope).Message);
                            });
            }

            using (mockRepository.Playback())
            {
                var bus = new Bus(config);
                bus.Publish(message);
            }
        }
示例#2
0
        public void ShouldBeAbleToBuildWithFactory(FilterCategory category, Type resultType)
        {
            var testMessage = new TestMessage().AsFilterMessage(category, resultType);

            Assert.Equal(category, testMessage.Category);
            Assert.Equal(resultType, testMessage.ResultType);
        }
示例#3
0
        public void ShouldBeAbleToBuildWithFactory(Type executedType, MethodInfo executedMethod)
        {
            var testMessage = new TestMessage().AsSourceMessage(executedType, executedMethod);

            Assert.Equal(executedType, testMessage.ExecutedType);
            Assert.Equal(executedMethod, testMessage.ExecutedMethod); 
        }
示例#4
0
        public void ShouldBeAbleToBuildWithFactoryUsingActionDescriptor(ActionDescriptor descriptor)
        { 
            var testMessage = new TestMessage().AsActionMessage(descriptor);

            Assert.Equal(descriptor.ActionName, testMessage.ActionName);
            Assert.Equal(descriptor.ControllerDescriptor.ControllerName, testMessage.ControllerName);
        }
 public void GetInstance_NullInstanceContext()
 {
     var data = new ServiceImplementationData();
     var container = new ContainerBuilder().Build();
     var provider = new AutofacInstanceProvider(container, data);
     var message = new TestMessage();
     Assert.Throws<ArgumentNullException>(() => provider.GetInstance(null, message));
 }
        public void ShouldBeAbleToBuildWithFactory(TimerResult timerResult, string eventName, TimelineCategoryItem eventCategory, string eventSubText)
        {
            var testMessage = new TestMessage().AsTimelineMessage(eventName, eventCategory, eventSubText);

            Assert.Equal(eventName, testMessage.EventName);
            Assert.Equal(eventCategory, testMessage.EventCategory);
            Assert.Equal(eventSubText, testMessage.EventSubText);
        }
示例#7
0
        public void ShouldBeAbleToBuildWithFactory(TimerResult timerResult)
        {
            var testMessage = new TestMessage().AsTimedMessage(timerResult);

            Assert.Equal(timerResult.Offset, testMessage.Offset);
            Assert.Equal(timerResult.Duration, testMessage.Duration);
            Assert.Equal(timerResult.StartTime, testMessage.StartTime);
        }
示例#8
0
        public void ShouldBeAbleToBuildWithFactoryUsingControllerContext(ControllerContext controllerContext, string controllerName, string actionName)
        {
            controllerContext.Controller.ValueProvider.Setup(x => x.GetValue("controller")).Returns(new ValueProviderResult(controllerName, null, null));
            controllerContext.Controller.ValueProvider.Setup(x => x.GetValue("name")).Returns(new ValueProviderResult(actionName, null, null));

            var testMessage = new TestMessage().AsActionMessage(controllerContext);

            Assert.Equal(controllerName, testMessage.ControllerName);
            Assert.Equal(actionName, testMessage.ActionName);
        }
 public void SendGeneric_With_StringDestination_And_MessageBuilder_Sends()
 {
     var message = new TestMessage { Body = "Body" };
     var serviceBusMock = new Mock<IServiceBus>();
     serviceBusMock.Setup(x => x.CreateMessage<TestMessage>(It.IsAny<Action<TestMessage>>()))
         .Returns(message);
     var serviceBus = serviceBusMock.Object;
     //
     serviceBus.Send<TestMessage>("dest", x => x.Body = "...");
     //
     serviceBusMock.Verify(x => x.Send(It.IsAny<LiteralServiceBusEndpoint>(), It.Is<object[]>(y => y[0] == message)));
 }
 public void SendGeneric_With_MessageBuilder_Sends()
 {
     var message = new TestMessage { Body = "Body" };
     var serviceBusMock = new Mock<IServiceBus>();
     serviceBusMock.Setup(x => x.CreateMessage<TestMessage>(It.IsAny<Action<TestMessage>>()))
         .Returns(message);
     var serviceBus = serviceBusMock.Object;
     //
     serviceBus.Send<TestMessage>(x => x.Body = "...");
     //
     serviceBusMock.Verify(x => x.Send(null, It.Is<object[]>(y => y[0] == message)));
 }
            public void SerializesMessage()
            {
                Mock<IMessageSender> mockMessageSender = new Mock<IMessageSender>();

                Mock<IMessageSerializer> mockMessageSerializer = new Mock<IMessageSerializer>();

                Mock<IMessagePropertyProvider<TestMessage>> mockMessagePropertyProvider = new Mock<IMessagePropertyProvider<TestMessage>>();
                
                MessagePublisher<TestMessage> messagePublisher = new MessagePublisher<TestMessage>(mockMessageSender.Object, mockMessageSerializer.Object, mockMessagePropertyProvider.Object);

                TestMessage message = new TestMessage();

                messagePublisher.PublishAsync(message);

                mockMessageSerializer.Verify(ms => ms.Serialize(It.IsAny<Stream>(), It.Is<TestMessage>(it => Object.ReferenceEquals(it, message))), Times.Once());
            }
            public async Task ReceivesAndDeserializesSingleMessage()
            {
                Mock<IMessageDeserializer<TestMessage>> mockTestMessageDeserializer = new Mock<IMessageDeserializer<TestMessage>>();
                mockTestMessageDeserializer.Setup(md => md.GetTypeName())
                    .Returns(typeof(TestMessage).Name);

                TestMessage testMessage = new TestMessage();

                mockTestMessageDeserializer.Setup(md => md.Deserialize(It.IsAny<Stream>()))
                    .Returns(testMessage);

                BrokeredMessage testBrokeredMessage = new BrokeredMessage()
                {
                    Properties =
                    {
                        { MessagePropertyNames.TypeName, typeof(TestMessage).Name }
                    }
                };

                IObservable<BrokeredMessage> brokeredMessages = Observable.Create<BrokeredMessage>(o =>
                    {
                        o.OnNext(testBrokeredMessage);

                        o.OnCompleted();

                        return Disposable.Empty;
                    });

                MessageSource<TestMessage> messageSource = new MessageSource<TestMessage>(brokeredMessages, new[] { mockTestMessageDeserializer.Object });

                TestMessage message = await messageSource.Messages.SingleOrDefaultAsync();

                message.ShouldBeEquivalentTo(testMessage);

                // NOTE: Would be great to be able to verify that testBrokeredMessage.CompleteAsync() was called here, but I would have to build abstraction around BrokeredMessage for that because it can't be mocked (since it's sealed)

                mockTestMessageDeserializer.Verify(md => md.Deserialize(It.IsAny<Stream>()), Times.Once());
            }
示例#13
0
        public void CreatesNewConnectionIfConnectionClosedWhenPublishing()
        {
            var mockRepository = new MockRepository();
            var config = mockRepository.DynamicMock<IBrokerConfiguration>();
            var connFactory = mockRepository.DynamicMock<IBrokerConnectionFactory>();
            var conn = mockRepository.DynamicMock<IBrokerConnection>();
            var consumer = mockRepository.DynamicMock<IRegisteredConsumer>();
            var message = new TestMessage();

            using (mockRepository.Record())
            {
                SetupResult.For(consumer.MessageType).Return(typeof (TestMessage));
                SetupResult.For(config.Exchange).Return("ex");
                SetupResult.For(config.ConnectionFactory).Return(connFactory);
                SetupResult.For(config.RegisteredConsumers).Return(new Dictionary<Type, IList<IRegisteredConsumer>> { { typeof(TestMessage), new List<IRegisteredConsumer> { consumer } } }); ;
                SetupResult.For(conn.IsOpen).Return(false);

                Expect.Call(connFactory.CreateConnection()).Repeat.Times(4).Return(conn);
            }

            using (mockRepository.Playback())
            {
                var bus = new Bus(config);
                bus.Publish(message);
                bus.Publish(message);
            }
        }
            public async Task GetsMessagePropertiesFromPropertyProviderAndAppliesThemToTheBrokeredMessage()
            {
                Mock<IMessageSender> mockMessageSender = new Mock<IMessageSender>();
                BrokeredMessage brokeredMessageSent = null;

                mockMessageSender.Setup(ms => ms.SendAsync(It.IsAny<BrokeredMessage>()))
                    .Callback<BrokeredMessage>(bm =>
                    {
                        brokeredMessageSent = bm;
                    })
                    .Returns(Task.FromResult<object>(null));

                Mock<IMessageSerializer> mockMessageSerializer = new Mock<IMessageSerializer>();

                Mock<IMessagePropertyProvider<TestMessage>> mockMessagePropertyProvider = new Mock<IMessagePropertyProvider<TestMessage>>();

                KeyValuePair<string, object>[] properties = new[]
                {
                    new KeyValuePair<string, object>("Prop1", 1),
                    new KeyValuePair<string, object>("Prop2", "two"),
                };

                mockMessagePropertyProvider.Setup(mpp => mpp.GetProperties(It.IsAny<TestMessage>()))
                    .Returns(properties);

                MessagePublisher<TestMessage> messagePublisher = new MessagePublisher<TestMessage>(mockMessageSender.Object, mockMessageSerializer.Object, mockMessagePropertyProvider.Object);

                TestMessage message = new TestMessage();

                await messagePublisher.PublishAsync(message);

                mockMessagePropertyProvider.Verify(mpp => mpp.GetProperties(It.Is<TestMessage>(it => Object.ReferenceEquals(it, message))), Times.Once);

                brokeredMessageSent.Should().NotBeNull();

                // TODO: should be able to do this cleaner
                brokeredMessageSent.Properties.Should().ContainKeys(properties.Select(kvp => kvp.Key), "Should have translated all properties provided by the property provider to the BrokeredMessage.");
                brokeredMessageSent.Properties.Should().ContainValues(properties.Select(kvp => kvp.Value), "Should have translated all properties provided by the property provider to the BrokeredMessage.");
            }
            public async Task SendsMessage()
            {
                Mock<IMessageSender> mockMessageSender = new Mock<IMessageSender>();

                Mock<IMessageSerializer> mockMessageSerializer = new Mock<IMessageSerializer>();

                Mock<IMessagePropertyProvider<TestMessage>> mockMessagePropertyProvider = new Mock<IMessagePropertyProvider<TestMessage>>();

                MessagePublisher<TestMessage> messagePublisher = new MessagePublisher<TestMessage>(mockMessageSender.Object, mockMessageSerializer.Object, mockMessagePropertyProvider.Object);

                TestMessage message = new TestMessage();

                await messagePublisher.PublishAsync(message);

                mockMessageSender.Verify(ms => ms.SendAsync(It.IsAny<BrokeredMessage>()), Times.Once());
            }
            public async Task AppliesMessageTypeNamePropertyToTheBrokeredMessage()
            {
                Mock<IMessageSender> mockMessageSender = new Mock<IMessageSender>();
                BrokeredMessage brokeredMessageSent = null;

                mockMessageSender.Setup(ms => ms.SendAsync(It.IsAny<BrokeredMessage>()))
                    .Callback<BrokeredMessage>(bm =>
                    {
                        brokeredMessageSent = bm;
                    })
                    .Returns(Task.FromResult<object>(null));

                Mock<IMessageSerializer> mockMessageSerializer = new Mock<IMessageSerializer>();

                Mock<IMessagePropertyProvider<TestMessage>> mockMessagePropertyProvider = new Mock<IMessagePropertyProvider<TestMessage>>();

                MessagePublisher<TestMessage> messagePublisher = new MessagePublisher<TestMessage>(mockMessageSender.Object, mockMessageSerializer.Object, mockMessagePropertyProvider.Object);

                TestMessage message = new TestMessage();

                await messagePublisher.PublishAsync(message);

                brokeredMessageSent.Should().NotBeNull();

                brokeredMessageSent.Properties.Should().Contain(new KeyValuePair<string,object>(MessagePropertyNames.TypeName, typeof(TestMessage).Name), "Should have applied the mesage type name property to the BrokeredMessage.");
            }
        public void SetsPrincipalOnDelivery()
        {
            var mockRepository = new MockRepository();
            var rabbitModel = mockRepository.DynamicMock<IModel>();
            var messageSerializer = mockRepository.DynamicMock<IMessageSerializer>();
            var registeredConsumer = mockRepository.DynamicMock<IRegisteredConsumer>();
            var properties = mockRepository.Stub<IBasicProperties>();
            var envelope = mockRepository.DynamicMock<IMessageEnvelope<TestMessage>>();
            var message = new TestMessage();
            var config = mockRepository.DynamicMock<IBrokerConfiguration>();
            var pipelineEvents = new PipelineEvents();
            pipelineEvents.ResolvePrincipal +=
                (sender, args) => args.Principal = new GenericPrincipal(new GenericIdentity(args.MessageEnvelope.UserName + "set"), new string[0]);

            using (mockRepository.Record())
            {
                SetupResult.For(envelope.UserName).Return("user1");
                SetupResult.For(registeredConsumer.MessageType).Return(typeof(TestMessage));
                SetupResult.For(registeredConsumer.Queue).Return("q");
                SetupResult.For(registeredConsumer.ConsumerType).Return(typeof(TestConsumer));
                SetupResult.For(envelope.Message).Return(message);
                SetupResult.For(messageSerializer.DeserializeMessage(null)).IgnoreArguments().Return(envelope);
                SetupResult.For(config.PipelineEvents).Return(pipelineEvents);

                Expect.Call(() => registeredConsumer.Invoke(message)).WhenCalled(mi => Assert.Equal("user1set", Thread.CurrentPrincipal.Identity.Name));
            }

            using (mockRepository.Playback())
            {
                var callbackConsumer = new GroupedCallbackConsumer(rabbitModel, messageSerializer, config);
                callbackConsumer.ConsumeQueue(registeredConsumer);
                callbackConsumer.QueueConsumers["q"].Consumer.HandleBasicDeliver("ct1", 1, false, "ex", typeof(TestMessage).ToRoutingKey(), properties, new byte[0]);

                callbackConsumer.Close();
                callbackConsumer.Dispose();
            }
        }
示例#18
0
        public void ShouldBeAbleToBuildWithFactoryUsingActionDescriptor(ControllerBase controller)
        {
            var testMessage = new TestMessage().AsChildActionMessage(controller);

            Assert.Equal(controller.ControllerContext.IsChildAction, testMessage.IsChildAction); 
        } 
        public void InvokesOnDelivery()
        {
            var mockRepository = new MockRepository();
            var rabbitModel = mockRepository.DynamicMock<IModel>();
            var messageSerializer = mockRepository.DynamicMock<IMessageSerializer>();
            var registeredConsumer = mockRepository.DynamicMock<IRegisteredConsumer>();
            var properties = mockRepository.Stub<IBasicProperties>();
            var envelope = mockRepository.DynamicMock<IMessageEnvelope<TestMessage>>();
            var message = new TestMessage();
            var config = mockRepository.DynamicMock<IBrokerConfiguration>();

            using (mockRepository.Record())
            {
                SetupResult.For(registeredConsumer.MessageType).Return(typeof(TestMessage));
                SetupResult.For(registeredConsumer.Queue).Return("q");
                SetupResult.For(registeredConsumer.ConsumerType).Return(typeof(TestConsumer));
                SetupResult.For(envelope.Message).Return(message);
                SetupResult.For(messageSerializer.DeserializeMessage(null)).IgnoreArguments().Return(envelope);
                SetupResult.For(config.PipelineEvents).Return(new PipelineEvents());

                Expect.Call(() => registeredConsumer.Invoke(message));
            }

            using (mockRepository.Playback())
            {
                var callbackConsumer = new GroupedCallbackConsumer(rabbitModel, messageSerializer, config);
                callbackConsumer.ConsumeQueue(registeredConsumer);
                callbackConsumer.QueueConsumers["q"].Consumer.HandleBasicDeliver("ct1", 1, false, "ex", typeof(TestMessage).ToRoutingKey(), properties, new byte[0]);

                callbackConsumer.Close();
                callbackConsumer.Dispose();
            }
        }
        public void ShouldBeAbleToBuildWithFactory(FilterBounds bounds)
        {
            var testMessage = new TestMessage().AsBoundedFilterMessage(bounds);

            Assert.Equal(bounds, testMessage.Bounds); 
        }