Esempio n. 1
0
        private ITriggerBinding GetMqttTriggerBinding(ParameterInfo parameter, MqttTriggerAttribute mqttTriggerAttribute)
        {
            var topicFilters = new List <TopicFilter>();

            var mqttConnection = _connectionFactory.GetMqttConnection(mqttTriggerAttribute);

            try
            {
                if (mqttTriggerAttribute.TopicFilter != null)
                {
                    topicFilters.Add(mqttTriggerAttribute.TopicFilter);
                }
                else
                {
                    topicFilters.AddRange(mqttTriggerAttribute.TopicStrings.Select(t =>
                    {
                        var topicString = (mqttTriggerAttribute.MqttConfigCreatorType != null) ? _nameResolver.ResolveWholeString(t) : t;
                        return(new TopicFilter()
                        {
                            Topic = topicString, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce
                        });
                    }));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message, ex);
                throw;
            }

            return(new MqttTriggerBinding(parameter, mqttConnection, topicFilters.ToArray(), _logger));
        }
        public void BrokenCustomConfigCreatorThrowsException()
        {
            // Arrange
            var mqttTriggerAttribute = new MqttTriggerAttribute(typeof(BrokenTestMqttConfigProvider));

            var attributeToConfigConverter = new AttributeToConfigConverter(mqttTriggerAttribute, _resolver, _mockLogger.Object);

            // Act & Assert
            var ex = Assert.Throws <InvalidCustomConfigCreatorException>(() => attributeToConfigConverter.GetMqttConfiguration());
        }
Esempio n. 3
0
        public void BrokenCustomConfigCreatorThrowsException()
        {
            // Arrange
            var mqttTriggerAttribute = new MqttTriggerAttribute(typeof(BrokenTestMqttConfigProvider));

            var mqttConfigurationParser = new MqttConfigurationParser(_resolver, _mockLoggerFactory.Object);

            // Act & Assert
            Assert.Throws <InvalidCustomConfigCreatorException>(() => mqttConfigurationParser.Parse(mqttTriggerAttribute));
        }
Esempio n. 4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AttributeToConfigConverter"/> class.
        /// </summary>
        /// <param name="source">The trigger attribute.</param>
        /// <param name="nameResolver">The name resolver.</param>
        /// <param name="logger">The logger.</param>
        public AttributeToConfigConverter(MqttTriggerAttribute source, INameResolver nameResolver, ILogger logger)
        {
            _connectionString = new DbConnectionStringBuilder()
            {
                ConnectionString = nameResolver.Resolve(source.ConnectionString)
            };

            _mqttTriggerAttribute = source;
            _nameResolver         = nameResolver;
            _logger = logger;
        }
        public void NoServernameProvidedResultsInException()
        {
            // Arrange
            var mqttTriggerAttribute = new MqttTriggerAttribute(new[] { "testTopic" })
            {
                ConnectionString = "Server=;Port=1883;Username=UserName;Password=Password;ClientId="
            };

            var attributeToConfigConverter = new AttributeToConfigConverter(mqttTriggerAttribute, _resolver, _mockLogger.Object);

            // Act & Assert
            var ex = Assert.Throws <Exception>(() => attributeToConfigConverter.GetMqttConfiguration());
        }
        public void InvalidPortThrowsException()
        {
            // Arrange
            var mqttTriggerAttribute = new MqttTriggerAttribute("testTopic")
            {
                ConnectionString = "Server=ServerName;Port=ByeWorld;Username=UserName;Password=Password"
            };

            var attributeToConfigConverter = new AttributeToConfigConverter(mqttTriggerAttribute, _resolver, _mockLogger.Object);

            // Act & Assert
            var ex = Assert.Throws <FormatException>(() => attributeToConfigConverter.GetMqttConfiguration());
        }
Esempio n. 7
0
        public void CustomConfigProviderIsInvoked()
        {
            // Arrange
            var mqttTriggerAttribute = new MqttTriggerAttribute(typeof(TestMqttConfigProvider));

            var mqttConfigurationParser = new MqttConfigurationParser(_resolver, _mockLoggerFactory.Object);

            // Act
            var result = mqttConfigurationParser.Parse(mqttTriggerAttribute);

            // Assert
            Assert.NotNull(result);
        }
Esempio n. 8
0
        public void NoServernameProvidedResultsInException()
        {
            // Arrange
            var mqttTriggerAttribute = new MqttTriggerAttribute(new[] { "testTopic" })
            {
                ConnectionString = "Server=;Port=1883;Username=UserName;Password=Password;ClientId="
            };

            var mqttConfigurationParser = new MqttConfigurationParser(_resolver, _mockLoggerFactory.Object);

            // Act & Assert
            Assert.Throws <Exception>(() => mqttConfigurationParser.Parse(mqttTriggerAttribute));
        }
Esempio n. 9
0
        public void InvalidPortThrowsException()
        {
            // Arrange
            var mqttTriggerAttribute = new MqttTriggerAttribute("testTopic")
            {
                ConnectionString = "Server=ServerName;Port=ByeWorld;Username=UserName;Password=Password"
            };

            var mqttConfigurationParser = new MqttConfigurationParser(_resolver, _mockLoggerFactory.Object);

            // Act & Assert
            Assert.Throws <FormatException>(() => mqttConfigurationParser.Parse(mqttTriggerAttribute));
        }
        public void CustomConfigProviderIsInvoked()
        {
            // Arrange
            var mqttTriggerAttribute = new MqttTriggerAttribute(typeof(TestMqttConfigProvider));

            var attributeToConfigConverter = new AttributeToConfigConverter(mqttTriggerAttribute, _resolver, _mockLogger.Object);

            // Act
            var result = attributeToConfigConverter.GetMqttConfiguration();

            // Assert
            Assert.NotNull(result);
            //Assert.Contains(result.Topics.Select(x => x.Topic), (x) => x == "Test");
        }
Esempio n. 11
0
        public void ValidConfigurationIsMappedCorrect()
        {
            // Arrange
            var mqttTriggerAttribute = new MqttTriggerAttribute("testTopic")
            {
                ConnectionString = "Server=ServerName;Port=1883;Username=UserName;Password=Password;ClientId=TestClientId"
            };

            var mqttConfigurationParser = new MqttConfigurationParser(_resolver, _mockLoggerFactory.Object);

            // Act
            var result = mqttConfigurationParser.Parse(mqttTriggerAttribute);

            // Assert
            Assert.Equal("TestClientId", result.Options.ClientOptions.ClientId);
        }
        private MqttTriggerBinding GetMqttTriggerBinding(ParameterInfo parameter, MqttTriggerAttribute mqttTriggerAttribute)
        {
            TopicFilter[] topics;
            var           mqttConnection = _connectionFactory.GetMqttConnection(mqttTriggerAttribute);

            try
            {
                topics = mqttTriggerAttribute.Topics.Select(t => new TopicFilter(t, MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce)).ToArray();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message, ex);
                throw;
            }

            return(new MqttTriggerBinding(parameter, mqttConnection, topics, _logger));
        }
        public void NoClientIdGuidBasedClientIdIsGenerated()
        {
            // Arrange
            var mqttTriggerAttribute = new MqttTriggerAttribute(new[] { "testTopic" })
            {
                ConnectionString = "Server=ServerName;Port=1883;Username=UserName;Password=Password;ClientId="
            };

            var attributeToConfigConverter = new AttributeToConfigConverter(mqttTriggerAttribute, _resolver, _mockLogger.Object);

            // Act
            var result = attributeToConfigConverter.GetMqttConfiguration();

            // Assert
            Assert.NotNull(result.Options.ClientOptions.ClientId);
            Assert.True(Guid.TryParse(result.Options.ClientOptions.ClientId, out var guid));
        }
        public void ValidConfigurationIsMappedCorrect()
        {
            // Arrange
            var mqttTriggerAttribute = new MqttTriggerAttribute("testTopic")
            {
                ConnectionString = "Server=ServerName;Port=1883;Username=UserName;Password=Password;ClientId=TestClientId"
            };

            var attributeToConfigConverter = new AttributeToConfigConverter(mqttTriggerAttribute, _resolver, _mockLogger.Object);

            // Act
            var result = attributeToConfigConverter.GetMqttConfiguration();

            // Assert
            //Assert.Equal(mqttTriggerAttribute.Topics, result.Topics.Select(x => x.Topic));
            Assert.Equal("TestClientId", result.Options.ClientOptions.ClientId);
        }
        private MqttTriggerBinding GetMqttTriggerBinding(ParameterInfo parameter, MqttTriggerAttribute mqttTriggerAttribute)
        {
            var attributeToConfigConverter = new AttributeToConfigConverter(mqttTriggerAttribute, _nameResolver, _logger);

            MqttConfiguration mqttConfiguration;

            try
            {
                mqttConfiguration = attributeToConfigConverter.GetMqttConfiguration();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message, ex);
                throw;
            }

            var mqttFactory = new MqttFactory();

            return(new MqttTriggerBinding(parameter, mqttFactory, mqttConfiguration, _logger));
        }