public void ValidateTopic_OnValidTopic()
        {
            // Arrange
            var validRawTopic = TestUtils.GenerateValidTopic();

            // Act
            Action validatingMinimalTopic = () =>
                                            TopicValidator.ValidateTopic(validRawTopic);

            // Assert
            validatingMinimalTopic.Should()
            .NotThrow <MqttBaseException>("because the topic is correctly built");
        }
        public void ValidateTopic_OnNonUtf8Topic()
        {
            // Arrange
            const string nonUtf8Topic = "non/🌱/utf/🛠/8/🐛";

            // Act
            Action validatingNonUtf8Topic = () =>
                                            TopicValidator.ValidateTopic(nonUtf8Topic);

            // Assert
            validatingNonUtf8Topic.Should()
            .Throw <InvalidTopicException>();
        }
        public void ValidateTopic_OnMinimalTopic()
        {
            // Arrange
            var minimalValidTopic = Mqtt.Topic.Separator.ToString();

            // Act
            Action validatingTopic = () =>
                                     TopicValidator.ValidateTopic(minimalValidTopic);

            // Assert
            validatingTopic.Should()
            .NotThrow <MqttBaseException>("because the topic is minimal yet allowed");
        }
        public void ValidateTopic_OnBlankTopic()
        {
            // Arrange
            var blankTopic = string.Empty;

            // Act
            Action validatingBlankTopic = () =>
                                          TopicValidator.ValidateTopic(blankTopic);

            // Assert
            validatingBlankTopic.Should()
            .Throw <EmptyTopicException>("because a topic can not be empty");
        }
        public void ValidateTopic_OnMultiLevelWildcardsBeforeTopicEnd()
        {
            // Arrange
            var topicWithMultiLevelWildcardBeforeEnd = $"{Mqtt.Topic.Separator}{Mqtt.Wildcard.MultiLevel}";

            topicWithMultiLevelWildcardBeforeEnd += $"{Mqtt.Topic.Separator}{TestUtils.GenerateValidTopic()}";

            // Act
            Action validatingTopicWithMultiLevelWildcardsBeforeEnd = () =>
                                                                     TopicValidator.ValidateTopic(topicWithMultiLevelWildcardBeforeEnd);

            // Assert
            validatingTopicWithMultiLevelWildcardsBeforeEnd.Should()
            .Throw <IllegalTopicConstructionException>()
            .Where(_ =>
                   _.Message.Contains(ExceptionMessages.TopicAfterWildcard),
                   "because a multi-level wildcard may only be used anywhere but at the end of a topic");
        }
        public void ValidateTopic_OnMultiLevelWildcardsTopic()
        {
            // Arrange
            var multiLevelWildcardsTopic = TestUtils.GenerateValidTopic(
                Mqtt.Topic.MaximumAllowedLevels - 2);

            // Appending '/#/#'
            multiLevelWildcardsTopic += string.Concat(
                Enumerable.Repeat(
                    $"{Mqtt.Topic.Separator}{Mqtt.Wildcard.MultiLevel}", 2));

            // Act
            Action validatingMultiLevelWildcardTopic = () =>
                                                       TopicValidator.ValidateTopic(multiLevelWildcardsTopic);

            // Assert
            validatingMultiLevelWildcardTopic.Should()
            .Throw <IllegalTopicConstructionException>();
        }
Exemple #7
0
        /// <summary>
        /// Create a new MQTT Topic from a raw string
        /// <para>
        /// If <paramref name="rawTopic"/> is empty, the minimal topic will be created
        /// (made only of a single <see cref="Mqtt.Topic.Separator"/>)
        /// </para>
        /// </summary>
        /// <param name="rawTopic">Raw MQTT topic</param>
        /// <remarks>
        /// Any trailing separator will be removed
        /// </remarks>
        public Topic(string rawTopic)
        {
            // Create minimal topic on empty string or already minimal raw string
            if (string.IsNullOrEmpty(rawTopic) ||
                rawTopic == Mqtt.Topic.Separator.ToString())
            {
                Value = Mqtt.Topic.Separator.ToString();
                return;
            }

            TopicValidator.ValidateTopic(rawTopic);

            // Remove trailing "/" if any
            if (rawTopic.Last() == Mqtt.Topic.Separator)
            {
                rawTopic = rawTopic.Remove(rawTopic.Length - 1);
            }

            Value = rawTopic;
        }