Exemple #1
0
        private byte[] GetEnveloppeDataForCommand(string publisher, string content)
        {
            var evt = new RabbitCommand {
                Data = content
            };
            var ev = new Enveloppe(JsonConvert.SerializeObject(evt), typeof(RabbitCommand), publisher);

            return(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(ev)));
        }
        public async Task DispatchCommandAsync_Should_AddItToExchange()
        {
            var appId     = Guid.NewGuid();
            var queueName = "test_queue_commands_1";

            try
            {
                _channel.ExchangeDeclare(appId + "_commands", ExchangeType.Topic, true, false);
                _channel.QueueDeclare(queueName, true, true, false);
                _channel.QueueBind(queueName, appId + "_commands", "CQELight");

                var evt = new RabbitCommand
                {
                    Data = "testData"
                };

                var b = new RabbitMQCommandBus(
                    new JsonDispatcherSerializer(),
                    new RabbitMQClientBusConfiguration(appId.ToString(), _testConfiguration["host"], _testConfiguration["user"], _testConfiguration["password"]));
                var allCalled = false;

                await b.DispatchAsync(evt).ContinueWith(t =>
                {
                    t.Result.IsSuccess.Should().BeTrue();
                    var result = _channel.BasicGet(queueName, true);
                    result.Should().NotBeNull();
                    var enveloppeAsStr = Encoding.UTF8.GetString(result.Body);
                    enveloppeAsStr.Should().NotBeNullOrWhiteSpace();

                    var receivedEnveloppe = enveloppeAsStr.FromJson <Enveloppe>();
                    receivedEnveloppe.Should().NotBeNull();

                    var type = Type.GetType(receivedEnveloppe.AssemblyQualifiedDataType);
                    var evet = receivedEnveloppe.Data.FromJson(type);
                    evet.Should().BeOfType <RabbitCommand>();
                    evet.As <RabbitCommand>().Data.Should().Be("testData");
                    allCalled = true;
                }).ConfigureAwait(false);

                allCalled.Should().BeTrue();
            }
            finally
            {
                _channel.QueueDelete(queueName);
                _channel.ExchangeDelete(appId + "_commands");
            }
        }