< Summary

Class:Azure.Messaging.ServiceBus.EntityNameFormatter
Assembly:Azure.Messaging.ServiceBus
File(s):C:\Git\azure-sdk-for-net\sdk\servicebus\Azure.Messaging.ServiceBus\src\EntityNameFormatter.cs
Covered lines:29
Uncovered lines:7
Coverable lines:36
Total lines:147
Line coverage:80.5% (29 of 36)
Covered branches:15
Total branches:20
Branch coverage:75% (15 of 20)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
FormatDeadLetterPath(...)-0%100%
FormatSubQueuePath(...)-0%100%
FormatSubscriptionPath(...)-100%100%
FormatRulePath(...)-100%100%
FormatTransferDeadLetterPath(...)-0%100%
CheckValidQueueName(...)-100%100%
CheckValidTopicName(...)-100%100%
CheckValidSubscriptionName(...)-100%100%
CheckValidRuleName(...)-100%100%
CheckValidEntityName(...)-71.43%68.75%
GetPathWithoutBaseUri(...)-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\servicebus\Azure.Messaging.ServiceBus\src\EntityNameFormatter.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using Azure.Messaging.ServiceBus.Management;
 6
 7namespace Azure.Messaging.ServiceBus
 8{
 9    /// <summary>
 10    /// This class can be used to format the path for different Service Bus entity types.
 11    /// </summary>
 12    public static class EntityNameFormatter
 13    {
 14        private const string PathDelimiter = @"/";
 15        private const string SubscriptionsSubPath = "Subscriptions";
 16        private const string RulesSubPath = "Rules";
 17        private const string SubQueuePrefix = "$";
 18        private const string DeadLetterQueueSuffix = "DeadLetterQueue";
 19        private const string DeadLetterQueueName = SubQueuePrefix + DeadLetterQueueSuffix;
 20        private const string Transfer = "Transfer";
 21        private const string TransferDeadLetterQueueName = SubQueuePrefix + Transfer + PathDelimiter + DeadLetterQueueNa
 22
 23        /// <summary>
 24        /// Formats the dead letter path for either a queue, or a subscription.
 25        /// </summary>
 26        /// <param name="entityPath">The name of the queue, or path of the subscription.</param>
 27        /// <returns>The path as a string of the dead letter entity.</returns>
 28        public static string FormatDeadLetterPath(string entityPath)
 29        {
 030            return EntityNameFormatter.FormatSubQueuePath(entityPath, EntityNameFormatter.DeadLetterQueueName);
 31        }
 32
 33        /// <summary>
 34        /// Formats the subqueue path for either a queue, or a subscription.
 35        /// </summary>
 36        /// <param name="entityPath">The name of the queue, or path of the subscription.</param>
 37        /// <param name="subQueueName"></param>
 38        /// <returns>The path as a string of the subqueue entity.</returns>
 39        public static string FormatSubQueuePath(string entityPath, string subQueueName)
 40        {
 041            return string.Concat(entityPath, EntityNameFormatter.PathDelimiter, subQueueName);
 42        }
 43
 44        /// <summary>
 45        /// Formats the subscription path, based on the topic path and subscription name.
 46        /// </summary>
 47        /// <param name="topicPath">The name of the topic, including slashes.</param>
 48        /// <param name="subscriptionName">The name of the subscription.</param>
 49        public static string FormatSubscriptionPath(string topicPath, string subscriptionName)
 50        {
 6851            return string.Concat(topicPath, PathDelimiter, SubscriptionsSubPath, PathDelimiter, subscriptionName);
 52        }
 53
 54        /// <summary>
 55        /// Formats the rule path, based on the topic path, subscription name and rule name.
 56        /// </summary>
 57        /// <param name="topicPath">The name of the topic, including slashes.</param>
 58        /// <param name="subscriptionName">The name of the subscription.</param>
 59        /// <param name="ruleName">The name of the rule</param>
 60        public static string FormatRulePath(string topicPath, string subscriptionName, string ruleName)
 61        {
 4862            return string.Concat(
 4863                topicPath, PathDelimiter,
 4864                SubscriptionsSubPath, PathDelimiter,
 4865                subscriptionName, PathDelimiter,
 4866                RulesSubPath, PathDelimiter, ruleName);
 67        }
 68
 69        /// <summary>
 70        /// Utility method that creates the name for the transfer dead letter receiver, specified by <paramref name="ent
 71        /// </summary>
 72        public static string Format​Transfer​Dead​Letter​Path(string entityPath)
 73        {
 074            return string.Concat(entityPath, PathDelimiter, TransferDeadLetterQueueName);
 75        }
 76
 77        internal static void CheckValidQueueName(string queueName, string paramName = "queuePath")
 78        {
 12879            CheckValidEntityName(GetPathWithoutBaseUri(queueName), ManagementClientConstants.QueueNameMaximumLength, tru
 10880        }
 81
 82        internal static void CheckValidTopicName(string topicName, string paramName = "topicPath")
 83        {
 44484            CheckValidEntityName(topicName, ManagementClientConstants.TopicNameMaximumLength, true, paramName);
 44485        }
 86
 87        internal static void CheckValidSubscriptionName(string subscriptionName, string paramName = "subscriptionName")
 88        {
 21689            CheckValidEntityName(subscriptionName, ManagementClientConstants.SubscriptionNameMaximumLength, false, param
 21690        }
 91
 92        internal static void CheckValidRuleName(string ruleName, string paramName = "ruleName")
 93        {
 2094            CheckValidEntityName(ruleName, ManagementClientConstants.RuleNameMaximumLength, false, paramName);
 2095        }
 96
 97        private static void CheckValidEntityName(string entityName, int maxEntityNameLength, bool allowSeparator, string
 98        {
 80899            if (string.IsNullOrWhiteSpace(entityName))
 100            {
 0101                throw new ArgumentNullException(paramName);
 102            }
 103
 104            // and "\" will be converted to "/" on the REST path anyway. Gateway/REST do not
 105            // have to worry about the begin/end slash problem, so this is purely a client side check.
 808106            var tmpName = entityName.Replace(@"\", Constants.PathDelimiter);
 808107            if (tmpName.Length > maxEntityNameLength)
 108            {
 20109                throw new ArgumentOutOfRangeException(paramName, $@"Entity path '{entityName}' exceeds the '{maxEntityNa
 110            }
 111
 788112            if (tmpName.StartsWith(Constants.PathDelimiter, StringComparison.OrdinalIgnoreCase) ||
 788113                tmpName.EndsWith(Constants.PathDelimiter, StringComparison.OrdinalIgnoreCase))
 114            {
 0115                throw new ArgumentException($@"The entity name/path cannot contain '/' as prefix or suffix. The supplied
 116            }
 117
 788118            if (!allowSeparator && tmpName.Contains(Constants.PathDelimiter))
 119            {
 0120                throw new ArgumentException($@"The entity name/path contains an invalid character '{Constants.PathDelimi
 121            }
 122
 7880123            foreach (var uriSchemeKey in ManagementClientConstants.InvalidEntityPathCharacters)
 124            {
 3152125                if (entityName.IndexOf(uriSchemeKey) >= 0)
 126                {
 0127                    throw new ArgumentException($@"'{entityName}' contains character '{uriSchemeKey}' which is not allow
 128                }
 129            }
 788130        }
 131
 132        private static string GetPathWithoutBaseUri(string entityName)
 133        {
 134            // Note: on Linux/macOS, "/path" URLs are treated as valid absolute file URLs.
 135            // To ensure relative queue paths are correctly rejected on these platforms,
 136            // an additional check using IsWellFormedOriginalString() is made here.
 137            // See https://github.com/dotnet/corefx/issues/22098 for more information.
 128138            if (Uri.TryCreate(entityName, UriKind.Absolute, out Uri uriValue) &&
 128139                uriValue.IsWellFormedOriginalString())
 140            {
 24141                entityName = uriValue.PathAndQuery;
 24142                return entityName.TrimStart('/');
 143            }
 104144            return entityName;
 145        }
 146    }
 147}