< Summary

Class:Azure.Messaging.ServiceBus.Management.QueuePropertiesExtensions
Assembly:Azure.Messaging.ServiceBus
File(s):C:\Git\azure-sdk-for-net\sdk\servicebus\Azure.Messaging.ServiceBus\src\Management\QueuePropertiesExtensions.cs
Covered lines:89
Uncovered lines:17
Coverable lines:106
Total lines:224
Line coverage:83.9% (89 of 106)
Covered branches:132
Total branches:148
Branch coverage:89.1% (132 of 148)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
Serialize(...)-100%83.33%
ParseFromContent(...)-88.89%100%
ParseFromEntryElement(...)-86.96%93.86%
ParseCollectionFromContent(...)-69.23%66.67%
NormalizeDescription(...)-60%50%
NormalizeForwardToAddress(...)-0%0%

File(s)

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

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System.Xml.Linq;
 5using System;
 6using System.Collections.Generic;
 7using System.Xml;
 8using System.Globalization;
 9
 10namespace Azure.Messaging.ServiceBus.Management
 11{
 12    internal static class QueuePropertiesExtensions
 13    {
 14        public static XDocument Serialize(this QueueProperties description)
 15        {
 2816            var queueDescriptionElements = new List<object>()
 2817            {
 2818                new XElement(XName.Get("LockDuration", ManagementClientConstants.ServiceBusNamespace), XmlConvert.ToStri
 2819                new XElement(XName.Get("MaxSizeInMegabytes", ManagementClientConstants.ServiceBusNamespace), XmlConvert.
 2820                new XElement(XName.Get("RequiresDuplicateDetection", ManagementClientConstants.ServiceBusNamespace), Xml
 2821                new XElement(XName.Get("RequiresSession", ManagementClientConstants.ServiceBusNamespace), XmlConvert.ToS
 2822                description.DefaultMessageTimeToLive != TimeSpan.MaxValue ? new XElement(XName.Get("DefaultMessageTimeTo
 2823                new XElement(XName.Get("DeadLetteringOnMessageExpiration", ManagementClientConstants.ServiceBusNamespace
 2824                description.RequiresDuplicateDetection && description.DuplicateDetectionHistoryTimeWindow != default ?
 2825                    new XElement(XName.Get("DuplicateDetectionHistoryTimeWindow", ManagementClientConstants.ServiceBusNa
 2826                    : null,
 2827                new XElement(XName.Get("MaxDeliveryCount", ManagementClientConstants.ServiceBusNamespace), XmlConvert.To
 2828                new XElement(XName.Get("EnableBatchedOperations", ManagementClientConstants.ServiceBusNamespace), XmlCon
 2829                description.AuthorizationRules?.Serialize(),
 2830                new XElement(XName.Get("Status", ManagementClientConstants.ServiceBusNamespace), description.Status.ToSt
 2831                description.ForwardTo != null ? new XElement(XName.Get("ForwardTo", ManagementClientConstants.ServiceBus
 2832                description.UserMetadata != null ? new XElement(XName.Get("UserMetadata", ManagementClientConstants.Serv
 2833                description.AutoDeleteOnIdle != TimeSpan.MaxValue ? new XElement(XName.Get("AutoDeleteOnIdle", Managemen
 2834                new XElement(XName.Get("EnablePartitioning", ManagementClientConstants.ServiceBusNamespace), XmlConvert.
 2835                description.ForwardDeadLetteredMessagesTo != null ? new XElement(XName.Get("ForwardDeadLetteredMessagesT
 2836            };
 37
 2838            if (description.UnknownProperties != null)
 39            {
 440                queueDescriptionElements.AddRange(description.UnknownProperties);
 41            }
 42
 2843            return new XDocument(
 2844                new XElement(XName.Get("entry", ManagementClientConstants.AtomNamespace),
 2845                    new XElement(XName.Get("content", ManagementClientConstants.AtomNamespace),
 2846                        new XAttribute("type", "application/xml"),
 2847                        new XElement(XName.Get("QueueDescription", ManagementClientConstants.ServiceBusNamespace),
 2848                            queueDescriptionElements.ToArray()))));
 49        }
 50
 51        /// <summary>
 52        ///
 53        /// </summary>
 54        public static QueueProperties ParseFromContent(string xml)
 55        {
 56            try
 57            {
 4858                var xDoc = XElement.Parse(xml);
 4859                if (!xDoc.IsEmpty)
 60                {
 4861                    if (xDoc.Name.LocalName == "entry")
 62                    {
 3263                        return ParseFromEntryElement(xDoc);
 64                    }
 65                }
 1666            }
 467            catch (Exception ex) when (!(ex is ServiceBusException))
 68            {
 069                throw new ServiceBusException(false, ex.Message);
 70            }
 71
 1672            throw new ServiceBusException("Queue was not found", ServiceBusFailureReason.MessagingEntityNotFound);
 2873        }
 74
 75        private static QueueProperties ParseFromEntryElement(XElement xEntry)
 76        {
 3677            var name = xEntry.Element(XName.Get("title", ManagementClientConstants.AtomNamespace)).Value;
 3678            var properties = new QueueProperties(name);
 79
 3680            var qdXml = xEntry.Element(XName.Get("content", ManagementClientConstants.AtomNamespace))?
 3681                .Element(XName.Get("QueueDescription", ManagementClientConstants.ServiceBusNamespace));
 82
 3683            if (qdXml == null)
 84            {
 485                throw new ServiceBusException("Queue was not found", ServiceBusFailureReason.MessagingEntityNotFound);
 86            }
 87
 149688            foreach (var element in qdXml.Elements())
 89            {
 71690                switch (element.Name.LocalName)
 91                {
 92                    case "MaxSizeInMegabytes":
 3293                        properties.MaxSizeInMegabytes = int.Parse(element.Value, CultureInfo.InvariantCulture);
 3294                        break;
 95                    case "RequiresDuplicateDetection":
 3296                        properties.RequiresDuplicateDetection = bool.Parse(element.Value);
 3297                        break;
 98                    case "RequiresSession":
 3299                        properties.RequiresSession = bool.Parse(element.Value);
 32100                        break;
 101                    case "DeadLetteringOnMessageExpiration":
 32102                        properties.DeadLetteringOnMessageExpiration = bool.Parse(element.Value);
 32103                        break;
 104                    case "DuplicateDetectionHistoryTimeWindow":
 32105                        properties.DuplicateDetectionHistoryTimeWindow = XmlConvert.ToTimeSpan(element.Value);
 32106                        break;
 107                    case "LockDuration":
 32108                        properties.LockDuration = XmlConvert.ToTimeSpan(element.Value);
 32109                        break;
 110                    case "DefaultMessageTimeToLive":
 32111                        properties.DefaultMessageTimeToLive = XmlConvert.ToTimeSpan(element.Value);
 32112                        break;
 113                    case "MaxDeliveryCount":
 32114                        properties.MaxDeliveryCount = int.Parse(element.Value, CultureInfo.InvariantCulture);
 32115                        break;
 116                    case "EnableBatchedOperations":
 32117                        properties.EnableBatchedOperations = bool.Parse(element.Value);
 32118                        break;
 119                    case "Status":
 32120                        properties.Status = element.Value;
 32121                        break;
 122                    case "AutoDeleteOnIdle":
 32123                        properties.AutoDeleteOnIdle = XmlConvert.ToTimeSpan(element.Value);
 32124                        break;
 125                    case "EnablePartitioning":
 32126                        properties.EnablePartitioning = bool.Parse(element.Value);
 32127                        break;
 128                    case "UserMetadata":
 20129                        properties.UserMetadata = element.Value;
 20130                        break;
 131                    case "ForwardTo":
 0132                        if (!string.IsNullOrWhiteSpace(element.Value))
 133                        {
 0134                            properties.ForwardTo = element.Value;
 135                        }
 0136                        break;
 137                    case "ForwardDeadLetteredMessagesTo":
 0138                        if (!string.IsNullOrWhiteSpace(element.Value))
 139                        {
 0140                            properties.ForwardDeadLetteredMessagesTo = element.Value;
 141                        }
 0142                        break;
 143                    case "AuthorizationRules":
 32144                        properties.AuthorizationRules = AuthorizationRules.ParseFromXElement(element);
 32145                        break;
 146                    case "AccessedAt":
 147                    case "CreatedAt":
 148                    case "MessageCount":
 149                    case "SizeInBytes":
 150                    case "UpdatedAt":
 151                    case "CountDetails":
 152                        // Ignore known properties
 153                        // Do nothing
 154                        break;
 155                    default:
 156                        // For unknown properties, keep them as-is for forward proof.
 144157                        if (properties.UnknownProperties == null)
 158                        {
 32159                            properties.UnknownProperties = new List<object>();
 160                        }
 161
 144162                        properties.UnknownProperties.Add(element);
 163                        break;
 164                }
 165            }
 166
 32167            return properties;
 168        }
 169
 170        public static List<QueueProperties> ParseCollectionFromContent(string xml)
 171        {
 172            try
 173            {
 4174                var xDoc = XElement.Parse(xml);
 4175                if (!xDoc.IsEmpty)
 176                {
 4177                    if (xDoc.Name.LocalName == "feed")
 178                    {
 4179                        var queueList = new List<QueueProperties>();
 180
 4181                        var entryList = xDoc.Elements(XName.Get("entry", ManagementClientConstants.AtomNamespace));
 16182                        foreach (var entry in entryList)
 183                        {
 4184                            queueList.Add(ParseFromEntryElement(entry));
 185                        }
 186
 4187                        return queueList;
 188                    }
 189                }
 0190            }
 0191            catch (Exception ex) when (!(ex is ServiceBusException))
 192            {
 0193                throw new ServiceBusException(false, ex.Message);
 194            }
 195
 0196            throw new ServiceBusException("No queues were found", ServiceBusFailureReason.MessagingEntityNotFound);
 4197        }
 198
 199        public static void NormalizeDescription(this QueueProperties description, string baseAddress)
 200        {
 28201            if (!string.IsNullOrWhiteSpace(description.ForwardTo))
 202            {
 0203                description.ForwardTo = NormalizeForwardToAddress(description.ForwardTo, baseAddress);
 204            }
 205
 28206            if (!string.IsNullOrWhiteSpace(description.ForwardDeadLetteredMessagesTo))
 207            {
 0208                description.ForwardDeadLetteredMessagesTo = NormalizeForwardToAddress(description.ForwardDeadLetteredMes
 209            }
 28210        }
 211
 212        private static string NormalizeForwardToAddress(string forwardTo, string baseAddress)
 213        {
 0214            baseAddress = new UriBuilder(baseAddress).Uri.ToString();
 215
 0216            if (!Uri.TryCreate(forwardTo, UriKind.Absolute, out Uri forwardToUri))
 217            {
 0218                forwardToUri = new Uri(new Uri(baseAddress), forwardTo);
 219            }
 220
 0221            return forwardToUri.AbsoluteUri;
 222        }
 223    }
 224}