internal RabbitMQContext CreateContext(RabbitMQAttribute attribute)
        {
            string connectionString = Utility.FirstOrDefault(attribute.ConnectionStringSetting, _options.Value.ConnectionString);
            string hostName         = Utility.FirstOrDefault(attribute.HostName, _options.Value.HostName);
            string exchangeName     = Utility.FirstOrDefault(attribute.ExchangeName, _options.Value.ExchangeName) ?? string.Empty;
            string routingKey       = Utility.FirstOrDefault(attribute.RoutingKey, _options.Value.RoutingKey) ?? string.Empty;
            string userName         = Utility.FirstOrDefault(attribute.UserName, _options.Value.UserName);
            string password         = Utility.FirstOrDefault(attribute.Password, _options.Value.Password);
            int    port             = Utility.FirstOrDefault(attribute.Port, _options.Value.Port);

            RabbitMQAttribute resolvedAttribute;
            IRabbitMQService  service;

            resolvedAttribute = new RabbitMQAttribute
            {
                ConnectionStringSetting = connectionString,
                HostName     = hostName,
                ExchangeName = exchangeName,
                RoutingKey   = routingKey,
                UserName     = userName,
                Password     = password,
                Port         = port,
            };

            service = GetService(connectionString, hostName, exchangeName, routingKey, userName, password, port);

            return(new RabbitMQContext
            {
                ResolvedAttribute = resolvedAttribute,
                Service = service,
            });
        }
        internal RabbitMQContext CreateContext(RabbitMQAttribute attribute)
        {
            string connectionString = Utility.FirstOrDefault(attribute.ConnectionStringSetting, _options.Value.ConnectionString);
            string hostName         = Utility.FirstOrDefault(attribute.HostName, _options.Value.HostName);
            string queueName        = Utility.FirstOrDefault(attribute.QueueName, _options.Value.QueueName);
            string userName         = Utility.FirstOrDefault(attribute.UserName, _options.Value.UserName);
            string password         = Utility.FirstOrDefault(attribute.Password, _options.Value.Password);
            int    port             = Utility.FirstOrDefault(attribute.Port, _options.Value.Port);


            RabbitMQAttribute resolvedAttribute;
            IRabbitMQService  service;

            resolvedAttribute = new RabbitMQAttribute
            {
                ConnectionStringSetting = connectionString,
                HostName  = hostName,
                QueueName = queueName,
                UserName  = userName,
                Password  = password,
                Port      = port,
            };

            service = GetService(connectionString, hostName, queueName, userName, password, port);

            return(new RabbitMQContext
            {
                ResolvedAttribute = resolvedAttribute,
                Service = service,
            });
        }
        public void ValidateBinding(RabbitMQAttribute attribute, Type type)
        {
            string connectionString = Utility.FirstOrDefault(attribute.ConnectionStringSetting, _options.Value.ConnectionString);
            string hostName         = Utility.FirstOrDefault(attribute.HostName, _options.Value.HostName) ?? Constants.LocalHost;

            _logger.LogInformation("Setting hostName to localhost since it was not specified");

            string userName = Utility.FirstOrDefault(attribute.UserName, _options.Value.UserName);
            string password = Utility.FirstOrDefault(attribute.Password, _options.Value.Password);

            if (string.IsNullOrEmpty(connectionString) && !Utility.ValidateUserNamePassword(userName, password, hostName))
            {
                throw new InvalidOperationException("RabbitMQ username and password required if not connecting to localhost");
            }

            string routingKey   = Utility.FirstOrDefault(attribute.RoutingKey, _options.Value.RoutingKey);
            string exchangeName = Utility.FirstOrDefault(attribute.ExchangeName, _options.Value.ExchangeName);

            if (string.IsNullOrEmpty(routingKey) && string.IsNullOrEmpty(exchangeName))
            {
                throw new InvalidOperationException("One of routingKey or exchangeName should be provided");
            }

            _logger.LogInformation($"RoutingKey {routingKey} and Exchange {exchangeName}");
        }
Beispiel #4
0
        public void Opens_Connection()
        {
            var options = new OptionsWrapper <RabbitMQOptions>(new RabbitMQOptions {
                HostName = Constants.LocalHost
            });
            var loggerFactory      = new LoggerFactory();
            var mockServiceFactory = new Mock <IRabbitMQServiceFactory>();
            var mockNameResolver   = new Mock <INameResolver>();
            var config             = new RabbitMQExtensionConfigProvider(options, mockNameResolver.Object, mockServiceFactory.Object, loggerFactory, _emptyConfig);
            var mockService        = new Mock <IRabbitMQService>();

            mockServiceFactory.Setup(m => m.CreateService(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <int>())).Returns(mockService.Object);

            RabbitMQAttribute attr = new RabbitMQAttribute
            {
                ConnectionStringSetting = string.Empty,
                HostName = Constants.LocalHost,
                UserName = "******",
                Password = "******",
                Port     = 5672
            };

            RabbitMQClientBuilder clientBuilder = new RabbitMQClientBuilder(config, options);

            var model = clientBuilder.Convert(attr);

            mockServiceFactory.Verify(m => m.CreateService(It.IsAny <string>(), Constants.LocalHost, "guest", "guest", 5672), Times.Exactly(1));
        }
Beispiel #5
0
        public async Task AddAsync_AddsMessagesToQueue()
        {
            var mockRabbitMQService = new Mock <IRabbitMQService>(MockBehavior.Strict);
            var mockBatch           = new Mock <IBasicPublishBatch>();

            mockRabbitMQService.Setup(m => m.BasicPublishBatch).Returns(mockBatch.Object);

            var attribute = new RabbitMQAttribute
            {
                HostName  = Constants.LocalHost,
                QueueName = "queue",
            };

            var context = new RabbitMQContext
            {
                ResolvedAttribute = attribute,
                Service           = mockRabbitMQService.Object
            };

            ILoggerFactory loggerFactory = new LoggerFactory();
            ILogger        logger        = loggerFactory.CreateLogger(LogCategories.CreateTriggerCategory(Constants.RabbitMQ));
            var            collector     = new RabbitMQAsyncCollector(context, logger);

            byte[] body = Encoding.UTF8.GetBytes("hi");
            await collector.AddAsync(body);

            mockBatch.Verify(m => m.Add(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <bool>(), It.IsAny <IBasicProperties>(), body), Times.Exactly(1));
        }
Beispiel #6
0
        public void Creates_Context_Correctly()
        {
            var options = new RabbitMQOptions {
                HostName = Constants.LocalHost, RoutingKey = "hello"
            };
            var loggerFactory      = new LoggerFactory();
            var mockServiceFactory = new Mock <IRabbitMQServiceFactory>();
            var mockNameResolver   = new Mock <INameResolver>();
            var config             = new RabbitMQExtensionConfigProvider(new OptionsWrapper <RabbitMQOptions>(options), mockNameResolver.Object, mockServiceFactory.Object, (ILoggerFactory)loggerFactory, _emptyConfig);
            var attribute          = new RabbitMQAttribute {
                HostName = "131.107.174.10", RoutingKey = "route"
            };

            var actualContext = config.CreateContext(attribute);

            RabbitMQAttribute attr = new RabbitMQAttribute
            {
                HostName   = "131.107.174.10",
                RoutingKey = "route",
            };

            RabbitMQContext expectedContext = new RabbitMQContext
            {
                ResolvedAttribute = attr,
            };

            Assert.Equal(actualContext.ResolvedAttribute.HostName, expectedContext.ResolvedAttribute.HostName);
            Assert.Equal(actualContext.ResolvedAttribute.RoutingKey, expectedContext.ResolvedAttribute.RoutingKey);
        }
Beispiel #7
0
        public static RabbitMQAttribute GetRabbitMQAttribute <T>()
        {
            if (_rabbitMQAttribute.IsNullOrEmpty())
            {
                var typeOfT = typeof(T);
                _rabbitMQAttribute = typeOfT.GetCustomAttribute <RabbitMQAttribute>();
            }

            return(_rabbitMQAttribute);
        }
        public void ValidateBinding(RabbitMQAttribute attribute, Type type)
        {
            string connectionString = Utility.FirstOrDefault(attribute.ConnectionStringSetting, _options.Value.ConnectionString);
            string hostName         = Utility.FirstOrDefault(attribute.HostName, _options.Value.HostName) ?? Constants.LocalHost;

            _logger.LogInformation("Setting hostName to localhost since it was not specified");

            string userName = Utility.FirstOrDefault(attribute.UserName, _options.Value.UserName);
            string password = Utility.FirstOrDefault(attribute.Password, _options.Value.Password);

            if (string.IsNullOrEmpty(connectionString) && !Utility.ValidateUserNamePassword(userName, password, hostName))
            {
                throw new InvalidOperationException("RabbitMQ username and password required if not connecting to localhost");
            }
        }
        public void ValidateBinding(RabbitMQAttribute attribute, Type type)
        {
            string hostName  = Utility.FirstOrDefault(attribute.HostName, _options.Value.HostName);
            string queueName = Utility.FirstOrDefault(attribute.QueueName, _options.Value.QueueName);

            if (string.IsNullOrEmpty(hostName))
            {
                throw new InvalidOperationException("RabbitMQ host name is missing");
            }

            if (string.IsNullOrEmpty(queueName))
            {
                throw new InvalidOperationException("RabbitMQ queue name is missing");
            }
        }
        public void TestWhetherConnectionIsPooled()
        {
            var options = new OptionsWrapper <RabbitMQOptions>(new RabbitMQOptions {
                HostName = Constants.LocalHost
            });
            var mockServiceFactory = new Mock <IRabbitMQServiceFactory>();

            mockServiceFactory.SetupSequence(m => m.CreateService(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <int>()))
            .Returns(GetRabbitMQService())
            .Returns(GetRabbitMQService());
            var config             = new RabbitMQExtensionConfigProvider(options, new Mock <INameResolver>().Object, mockServiceFactory.Object, new LoggerFactory(), _emptyConfig);
            RabbitMQAttribute attr = GetTestAttribute();

            RabbitMQClientBuilder clientBuilder = new RabbitMQClientBuilder(config, options);

            var model  = clientBuilder.Convert(attr);
            var model2 = clientBuilder.Convert(attr);

            Assert.Equal(model, model2);
        }
        internal RabbitMQContext CreateContext(RabbitMQAttribute attribute)
        {
            string           hostname   = Utility.FirstOrDefault(attribute.HostName, _options.Value.HostName);
            string           queuename  = Utility.FirstOrDefault(attribute.QueueName, _options.Value.QueueName);
            string           exchange   = Utility.FirstOrDefault(attribute.Exchange, _options.Value.Exchange) ?? string.Empty;
            IBasicProperties properties = attribute.Properties;

            var resolvedAttribute = new RabbitMQAttribute
            {
                HostName   = hostname,
                QueueName  = queuename,
                Exchange   = exchange,
                Properties = properties,
            };

            IRabbitMQService service = GetService(hostname, queuename);

            return(new RabbitMQContext
            {
                ResolvedAttribute = resolvedAttribute,
                Service = service,
            });
        }
Beispiel #12
0
        public void Handles_Null_Attributes_And_Options(string attrHostname, string attrQueueName, string optHostname, string optQueueName)
        {
            RabbitMQAttribute attr = new RabbitMQAttribute
            {
                HostName   = attrHostname,
                RoutingKey = attrQueueName,
            };

            RabbitMQOptions opt = new RabbitMQOptions
            {
                HostName   = optHostname,
                RoutingKey = optQueueName,
            };

            var loggerFactory      = new LoggerFactory();
            var mockServiceFactory = new Mock <IRabbitMQServiceFactory>();
            var mockNameResolver   = new Mock <INameResolver>();
            var config             = new RabbitMQExtensionConfigProvider(new OptionsWrapper <RabbitMQOptions>(opt), mockNameResolver.Object, mockServiceFactory.Object, (ILoggerFactory)loggerFactory, _emptyConfig);
            var actualContext      = config.CreateContext(attr);

            if (optHostname == null && optQueueName == null)
            {
                Assert.Equal(actualContext.ResolvedAttribute.HostName, attrHostname);
                Assert.Equal(actualContext.ResolvedAttribute.RoutingKey, attrQueueName);
            }
            else if (attrHostname == null && optQueueName == null)
            {
                Assert.Equal(actualContext.ResolvedAttribute.HostName, optHostname);
                Assert.Equal(actualContext.ResolvedAttribute.RoutingKey, attrQueueName);
            }
            else
            {
                Assert.Equal(actualContext.ResolvedAttribute.HostName, optHostname);
                Assert.Equal(actualContext.ResolvedAttribute.RoutingKey, optQueueName);
            }
        }