< Summary

Class:Microsoft.Azure.ServiceBus.Management.QueueDescriptionExtensions
Assembly:Microsoft.Azure.ServiceBus
File(s):C:\Git\azure-sdk-for-net\sdk\servicebus\Microsoft.Azure.ServiceBus\src\Management\QueueDescriptionExtensions.cs
Covered lines:0
Uncovered lines:107
Coverable lines:107
Total lines:223
Line coverage:0% (0 of 107)
Covered branches:0
Total branches:150
Branch coverage:0% (0 of 150)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
Serialize(...)-0%0%
ParseFromContent(...)-0%0%
ParseFromEntryElement(...)-0%0%
ParseCollectionFromContent(...)-0%0%
NormalizeDescription(...)-0%0%
NormalizeForwardToAddress(...)-0%0%

File(s)

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

#LineLine coverage
 1// Copyright (c) Microsoft. All rights reserved.
 2// Licensed under the MIT license. See LICENSE file in the project root for full license information.
 3
 4namespace Microsoft.Azure.ServiceBus.Management
 5{
 6    using System.Xml.Linq;
 7    using System;
 8    using System.Collections.Generic;
 9    using System.Xml;
 10
 11    internal static class QueueDescriptionExtensions
 12    {
 13        public static XDocument Serialize(this QueueDescription description)
 14        {
 015            var queueDescriptionElements = new List<object>()
 016            {
 017                new XElement(XName.Get("LockDuration", ManagementClientConstants.ServiceBusNamespace), XmlConvert.ToStri
 018                new XElement(XName.Get("MaxSizeInMegabytes", ManagementClientConstants.ServiceBusNamespace), XmlConvert.
 019                new XElement(XName.Get("RequiresDuplicateDetection", ManagementClientConstants.ServiceBusNamespace), Xml
 020                new XElement(XName.Get("RequiresSession", ManagementClientConstants.ServiceBusNamespace), XmlConvert.ToS
 021                description.DefaultMessageTimeToLive != TimeSpan.MaxValue ? new XElement(XName.Get("DefaultMessageTimeTo
 022                new XElement(XName.Get("DeadLetteringOnMessageExpiration", ManagementClientConstants.ServiceBusNamespace
 023                description.RequiresDuplicateDetection && description.DuplicateDetectionHistoryTimeWindow != default ?
 024                    new XElement(XName.Get("DuplicateDetectionHistoryTimeWindow", ManagementClientConstants.ServiceBusNa
 025                    : null,
 026                new XElement(XName.Get("MaxDeliveryCount", ManagementClientConstants.ServiceBusNamespace), XmlConvert.To
 027                new XElement(XName.Get("EnableBatchedOperations", ManagementClientConstants.ServiceBusNamespace), XmlCon
 028                description.AuthorizationRules?.Serialize(),
 029                new XElement(XName.Get("Status", ManagementClientConstants.ServiceBusNamespace), description.Status.ToSt
 030                description.ForwardTo != null ? new XElement(XName.Get("ForwardTo", ManagementClientConstants.ServiceBus
 031                description.UserMetadata != null ? new XElement(XName.Get("UserMetadata", ManagementClientConstants.Serv
 032                description.AutoDeleteOnIdle != TimeSpan.MaxValue ? new XElement(XName.Get("AutoDeleteOnIdle", Managemen
 033                new XElement(XName.Get("EnablePartitioning", ManagementClientConstants.ServiceBusNamespace), XmlConvert.
 034                description.ForwardDeadLetteredMessagesTo != null ? new XElement(XName.Get("ForwardDeadLetteredMessagesT
 035            };
 36
 037            if (description.UnknownProperties != null)
 38            {
 039                queueDescriptionElements.AddRange(description.UnknownProperties);
 40            }
 41
 042            return new XDocument(
 043                new XElement(XName.Get("entry", ManagementClientConstants.AtomNamespace),
 044                    new XElement(XName.Get("content", ManagementClientConstants.AtomNamespace),
 045                        new XAttribute("type", "application/xml"),
 046                        new XElement(XName.Get("QueueDescription", ManagementClientConstants.ServiceBusNamespace),
 047                            queueDescriptionElements.ToArray()))));
 48        }
 49
 50        public static QueueDescription ParseFromContent(string xml)
 51        {
 52            try
 53            {
 054                var xDoc = XElement.Parse(xml);
 055                if (!xDoc.IsEmpty)
 56                {
 057                    if (xDoc.Name.LocalName == "entry")
 58                    {
 059                        return ParseFromEntryElement(xDoc);
 60                    }
 61                }
 062            }
 063            catch (Exception ex) when (!(ex is ServiceBusException))
 64            {
 065                throw new ServiceBusException(false, ex);
 66            }
 67
 068            throw new MessagingEntityNotFoundException("Queue was not found");
 069        }
 70
 71        private static QueueDescription ParseFromEntryElement(XElement xEntry)
 72        {
 073            var name = xEntry.Element(XName.Get("title", ManagementClientConstants.AtomNamespace)).Value;
 074            var qd = new QueueDescription(name);
 75
 076            var qdXml = xEntry.Element(XName.Get("content", ManagementClientConstants.AtomNamespace))?
 077                .Element(XName.Get("QueueDescription", ManagementClientConstants.ServiceBusNamespace));
 78
 079            if (qdXml == null)
 80            {
 081                throw new MessagingEntityNotFoundException("Queue was not found");
 82            }
 83
 084            foreach (var element in qdXml.Elements())
 85            {
 086                switch (element.Name.LocalName)
 87                {
 88                    case "MaxSizeInMegabytes":
 089                        qd.MaxSizeInMB = Int64.Parse(element.Value);
 090                        break;
 91                    case "RequiresDuplicateDetection":
 092                        qd.RequiresDuplicateDetection = Boolean.Parse(element.Value);
 093                        break;
 94                    case "RequiresSession":
 095                        qd.RequiresSession = Boolean.Parse(element.Value);
 096                        break;
 97                    case "DeadLetteringOnMessageExpiration":
 098                        qd.EnableDeadLetteringOnMessageExpiration = Boolean.Parse(element.Value);
 099                        break;
 100                    case "DuplicateDetectionHistoryTimeWindow":
 0101                        qd.duplicateDetectionHistoryTimeWindow = XmlConvert.ToTimeSpan(element.Value);
 0102                        break;
 103                    case "LockDuration":
 0104                        qd.LockDuration = XmlConvert.ToTimeSpan(element.Value);
 0105                        break;
 106                    case "DefaultMessageTimeToLive":
 0107                        qd.DefaultMessageTimeToLive = XmlConvert.ToTimeSpan(element.Value);
 0108                        break;
 109                    case "MaxDeliveryCount":
 0110                        qd.MaxDeliveryCount = Int32.Parse(element.Value);
 0111                        break;
 112                    case "EnableBatchedOperations":
 0113                        qd.EnableBatchedOperations = Boolean.Parse(element.Value);
 0114                        break;
 115                    case "Status":
 0116                        qd.Status = (EntityStatus)Enum.Parse(typeof(EntityStatus), element.Value);
 0117                        break;
 118                    case "AutoDeleteOnIdle":
 0119                        qd.AutoDeleteOnIdle = XmlConvert.ToTimeSpan(element.Value);
 0120                        break;
 121                    case "EnablePartitioning":
 0122                        qd.EnablePartitioning = bool.Parse(element.Value);
 0123                        break;
 124                    case "UserMetadata":
 0125                        qd.UserMetadata = element.Value;
 0126                        break;
 127                    case "ForwardTo":
 0128                        if (!string.IsNullOrWhiteSpace(element.Value))
 129                        {
 0130                            qd.ForwardTo = element.Value;
 131                        }
 0132                        break;
 133                    case "ForwardDeadLetteredMessagesTo":
 0134                        if (!string.IsNullOrWhiteSpace(element.Value))
 135                        {
 0136                            qd.ForwardDeadLetteredMessagesTo = element.Value;
 137                        }
 0138                        break;
 139                    case "AuthorizationRules":
 0140                        qd.AuthorizationRules = AuthorizationRules.ParseFromXElement(element);
 0141                        break;
 142                    case "AccessedAt":
 143                    case "CreatedAt":
 144                    case "MessageCount":
 145                    case "SizeInBytes":
 146                    case "UpdatedAt":
 147                    case "CountDetails":
 148                        // Ignore known properties
 149                        // Do nothing
 150                        break;
 151                    default:
 152                        // For unknown properties, keep them as-is for forward proof.
 0153                        if (qd.UnknownProperties == null)
 154                        {
 0155                            qd.UnknownProperties = new List<object>();
 156                        }
 157
 0158                        qd.UnknownProperties.Add(element);
 159                        break;
 160                }
 161            }
 162
 0163            return qd;
 164        }
 165
 166        public static IList<QueueDescription> ParseCollectionFromContent(string xml)
 167        {
 168            try
 169            {
 0170                var xDoc = XElement.Parse(xml);
 0171                if (!xDoc.IsEmpty)
 172                {
 0173                    if (xDoc.Name.LocalName == "feed")
 174                    {
 0175                        var queueList = new List<QueueDescription>();
 176
 0177                        var entryList = xDoc.Elements(XName.Get("entry", ManagementClientConstants.AtomNamespace));
 0178                        foreach (var entry in entryList)
 179                        {
 0180                            queueList.Add(ParseFromEntryElement(entry));
 181                        }
 182
 0183                        return queueList;
 184                    }
 185                }
 0186            }
 0187            catch (Exception ex) when (!(ex is ServiceBusException))
 188            {
 0189                throw new ServiceBusException(false, ex);
 190            }
 191
 0192            throw new MessagingEntityNotFoundException("No queues were found");
 0193        }
 194
 195        public static void NormalizeDescription(this QueueDescription description, string baseAddress)
 196        {
 0197            if (!string.IsNullOrWhiteSpace(description.ForwardTo))
 198            {
 0199                description.ForwardTo = NormalizeForwardToAddress(description.ForwardTo, baseAddress);
 200            }
 201
 0202            if (!string.IsNullOrWhiteSpace(description.ForwardDeadLetteredMessagesTo))
 203            {
 0204                description.ForwardDeadLetteredMessagesTo = NormalizeForwardToAddress(description.ForwardDeadLetteredMes
 205            }
 0206        }
 207
 208        private static string NormalizeForwardToAddress(string forwardTo, string baseAddress)
 209        {
 0210            if (!Uri.TryCreate(forwardTo, UriKind.Absolute, out var forwardToUri))
 211            {
 0212                if (!baseAddress.EndsWith("/", StringComparison.OrdinalIgnoreCase))
 213                {
 0214                    baseAddress += "/";
 215                }
 216
 0217                forwardToUri = new Uri(new Uri(baseAddress), forwardTo);
 218            }
 219
 0220            return forwardToUri.AbsoluteUri;
 221        }
 222    }
 223}