public void When_Producing_A_Message_An_Audit_Message_Should_Be_Sent()
        {
            // Setup the correlation is to test
            var correlationId = Guid.NewGuid();

            // Create a serializer used to create the message bytes and visa versa
            var serializer = new JsonSerializer(new TypeNameSerializer());

            // Create a model, actually this is the bus for easynetq
            var model = Mock.Create <IModel>();

            // Arrange the basic publish to see if we actually send out a message with the correlationid, direction and producer name
            Mock.Arrange(() => model.BasicPublish(
                             "AuditExchange_TestProducer",
                             "TestProducer_audit",
                             Arg.IsAny <IBasicProperties>(),
                             Arg.Matches <byte[]>(m =>
                                                  serializer.BytesToMessage <RabbitAuditMessage>(m).Direction == Direction.Out &&
                                                  serializer.BytesToMessage <RabbitAuditMessage>(m).CorrelationId == correlationId &&
                                                  serializer.BytesToMessage <RabbitAuditMessage>(m).ProducerName == "TestProducer")))
            .Occurs(1, "No audit message was sent to the queue");

            // Let the connection return a model to publish to
            var connection = Mock.Create <IConnection>();

            Mock.Arrange(() => connection.CreateModel()).Returns(model);

            // Let the connection factory return a connection to rabbitmq
            var connectionFactory = Mock.Create <IConnectionFactory>();

            Mock.Arrange(() => connectionFactory.CreateConnection()).Returns(connection);

            // OnProduce should fire and set the correct correlation id
            var intercepter = Mock.Create <HandlerAuditIntercepter>(
                Mock.Create <ILogger>(),
                connectionFactory,
                serializer,
                Mock.Create <ITypeNameSerializer>(),
                Mock.Create <IPersistCorrelation>());

            // We need the correct consumer name and correlation id so we can verify
            Mock.Arrange(() => intercepter.ConsumerProducerName).Returns("TestProducer");
            Mock.Arrange(() => intercepter.GenerateNewCorrelationId()).Returns(correlationId);
            Mock.Arrange(() => intercepter.OnProduce(Arg.IsAny <RawMessage>())).CallOriginal();
            Mock.Arrange(() => intercepter.ShouldAudit()).Returns(true);

            var rawMessage = new RawMessage(
                new MessageProperties()
            {
                TypePresent = true,
                Type        = "MyType"
            }, Encoding.UTF8.GetBytes("my message"));

            intercepter.OnProduce(rawMessage);

            Mock.Assert(model);
        }
コード例 #2
0
        public void Should_be_able_to_republish_message()
        {
            var serializer = new JsonSerializer();
            var error      = serializer.BytesToMessage <Error>(Encoding.UTF8.GetBytes(errorMessage));

            var connectionFactory = new ConnectionFactory
            {
                HostName = "localhost",
                UserName = "******",
                Password = "******"
            };

            using (var connection = connectionFactory.CreateConnection())
                using (var model = connection.CreateModel())
                {
                    try
                    {
                        model.ExchangeDeclarePassive(error.Exchange);

                        var properties = model.CreateBasicProperties();
                        error.BasicProperties.CopyTo(properties);

                        var body = Encoding.UTF8.GetBytes(error.Message);

                        model.BasicPublish(error.Exchange, error.RoutingKey, properties, body);
                    }
                    catch (OperationInterruptedException)
                    {
                        Console.WriteLine("The exchange, '{0}', described in the error message does not exist on '{1}', '{2}'",
                                          error.Exchange, connectionFactory.HostName, connectionFactory.VirtualHost);
                    }
                }
        }
コード例 #3
0
        public void Should_be_able_to_republish_message()
        {
            var serializer = new JsonSerializer();
            var error = serializer.BytesToMessage<Error>(Encoding.UTF8.GetBytes(errorMessage));

            var connectionFactory = new ConnectionFactory
            {
                HostName = "localhost",
                UserName = "******",
                Password = "******"
            };

            using (var connection = connectionFactory.CreateConnection())
            using (var model = connection.CreateModel())
            {
                try
                {
                    model.ExchangeDeclarePassive(error.Exchange);

                    var properties = model.CreateBasicProperties();
                    error.BasicProperties.CopyTo(properties);

                    var body = Encoding.UTF8.GetBytes(error.Message);

                    model.BasicPublish(error.Exchange, error.RoutingKey, properties, body);
                }
                catch (OperationInterruptedException)
                {
                    Console.WriteLine("The exchange, '{0}', described in the error message does not exist on '{1}', '{2}'",
                        error.Exchange, connectionFactory.HostName, connectionFactory.VirtualHost);
                }
            }
        }
コード例 #4
0
        public void Should_fail_to_deseralize_some_other_random_message()
        {
            const string randomMessage = "{\"Text\":\"Hello World\"}";
            var          serializer    = new JsonSerializer();
            var          error         = serializer.BytesToMessage <Error>(Encoding.UTF8.GetBytes(randomMessage));

            error.Message.ShouldBeNull();
        }
コード例 #5
0
        public void Should_deserialise_error_message_correctly()
        {
            var serializer = new JsonSerializer();
            var error      = serializer.BytesToMessage <Error>(Encoding.UTF8.GetBytes(errorMessage));

            error.RoutingKey.ShouldEqual("originalRoutingKey");
            error.Message.ShouldEqual("{ Text:\"Hello World\"}");
        }
コード例 #6
0
 public void Should_fail_to_deseralize_some_other_random_message()
 {
     const string randomMessage = "{\"Text\":\"Hello World\"}";
     var serializer = new JsonSerializer();
     var error = serializer.BytesToMessage<Error>(Encoding.UTF8.GetBytes(randomMessage));
     error.Message.ShouldBeNull();
 }
コード例 #7
0
        public void Should_deserialise_error_message_correctly()
        {
            var serializer = new JsonSerializer();
            var error = serializer.BytesToMessage<Error>(Encoding.UTF8.GetBytes(errorMessage));

            error.RoutingKey.ShouldEqual("originalRoutingKey");
            error.Message.ShouldEqual("{ Text:\"Hello World\"}");
        }