Inheritance: IPipelineEvents
        public void CanPublish()
        {
            var mockRepository = new MockRepository();
            var envelope = mockRepository.DynamicMock<IMessageEnvelope<TestMessage>>();
            var rabbitConn = mockRepository.DynamicMock<IConnection>();
            var rabbitModel = mockRepository.StrictMock<IModel>();
            var config = mockRepository.DynamicMock<IBrokerConfiguration>();
            var messageSerializer = mockRepository.DynamicMock<IMessageSerializer>();
            var callbackConsumer = mockRepository.DynamicMock<ICallbackConsumer>();
            var basicProperties = mockRepository.Stub<IBasicProperties>();
            var pipelineEvents = new PipelineEvents();
            pipelineEvents.MessageSending += (sender, args) => args.MessageEnvelope.Headers.Add("a", "b");

            using (mockRepository.Record())
            {
                SetupResult.For(config.Exchange).Return("ex");
                SetupResult.For(rabbitConn.CreateModel()).Return(rabbitModel);
                SetupResult.For(config.PipelineEvents).Return(pipelineEvents);
                SetupResult.For(envelope.Headers).Return(new Dictionary<string, string>());

                var stream = new JsonSerializer().Serialize(envelope);
                var data = new byte[stream.Length];
                stream.Read(data, 0, data.Length);

                IBasicProperties headers;
                byte[] body;
                string routingKey;
                Expect.Call(() => messageSerializer.SerializeMessage(envelope, rabbitModel, out headers, out body, out routingKey))
                    .WhenCalled(mi =>
                                    {
                                        Assert.Equal("b", ((IMessageEnvelope<object>)mi.Arguments[0]).Headers["a"]);
                                        mi.Arguments[2] = basicProperties;
                                        mi.Arguments[3] = data;
                                        mi.Arguments[4] = "routing";
                                    });
                Expect.Call(() => rabbitModel.BasicPublish("ex", "routing", basicProperties, data));
            }

            using (mockRepository.Playback())
            {
                new BrokerConnection(rabbitConn, rabbitModel, config, messageSerializer, callbackConsumer).Publish(envelope);
            }
        }
        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();
            }
        }