< Summary

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

Metrics

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

File(s)

C:\Git\azure-sdk-for-net\sdk\servicebus\Microsoft.Azure.ServiceBus\src\Management\SubscriptionDescriptionExtensions.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;
 7    using System.Collections.Generic;
 8    using System.Xml;
 9    using System.Xml.Linq;
 10
 11    internal static class SubscriptionDescriptionExtensions
 12    {
 13        public static void NormalizeDescription(this SubscriptionDescription description, string baseAddress)
 14        {
 015            if (!string.IsNullOrWhiteSpace(description.ForwardTo))
 16            {
 017                description.ForwardTo = NormalizeForwardToAddress(description.ForwardTo, baseAddress);
 18            }
 19
 020            if (!string.IsNullOrWhiteSpace(description.ForwardDeadLetteredMessagesTo))
 21            {
 022                description.ForwardDeadLetteredMessagesTo = NormalizeForwardToAddress(description.ForwardDeadLetteredMes
 23            }
 024        }
 25
 26        static string NormalizeForwardToAddress(string forwardTo, string baseAddress)
 27        {
 028            if (!Uri.TryCreate(forwardTo, UriKind.Absolute, out var forwardToUri))
 29            {
 030                if (!baseAddress.EndsWith("/", StringComparison.Ordinal))
 31                {
 032                    baseAddress += "/";
 33                }
 34
 035                forwardToUri = new Uri(new Uri(baseAddress), forwardTo);
 36            }
 37
 038            return forwardToUri.AbsoluteUri;
 39        }
 40
 41        public static SubscriptionDescription ParseFromContent(string topicName, string xml)
 42        {
 43            try
 44            {
 045                var xDoc = XElement.Parse(xml);
 046                if (!xDoc.IsEmpty)
 47                {
 048                    if (xDoc.Name.LocalName == "entry")
 49                    {
 050                        return ParseFromEntryElement(topicName, xDoc);
 51                    }
 52                }
 053            }
 054            catch (Exception ex) when (!(ex is ServiceBusException))
 55            {
 056                throw new ServiceBusException(false, ex);
 57            }
 58
 059            throw new MessagingEntityNotFoundException("Subscription was not found");
 060        }
 61
 62        public static IList<SubscriptionDescription> ParseCollectionFromContent(string topicName, string xml)
 63        {
 64            try
 65            {
 066                var xDoc = XElement.Parse(xml);
 067                if (!xDoc.IsEmpty)
 68                {
 069                    if (xDoc.Name.LocalName == "feed")
 70                    {
 071                        var subscriptionList = new List<SubscriptionDescription>();
 72
 073                        var entryList = xDoc.Elements(XName.Get("entry", ManagementClientConstants.AtomNamespace));
 074                        foreach (var entry in entryList)
 75                        {
 076                            subscriptionList.Add(ParseFromEntryElement(topicName, entry));
 77                        }
 78
 079                        return subscriptionList;
 80                    }
 81                }
 082            }
 083            catch (Exception ex) when (!(ex is ServiceBusException))
 84            {
 085                throw new ServiceBusException(false, ex);
 86            }
 87
 088            throw new MessagingEntityNotFoundException("No subscriptions were found");
 089        }
 90
 91        private static SubscriptionDescription ParseFromEntryElement(string topicName, XElement xEntry)
 92        {
 093            var name = xEntry.Element(XName.Get("title", ManagementClientConstants.AtomNamespace)).Value;
 094            var subscriptionDesc = new SubscriptionDescription(topicName, name);
 95
 096            var qdXml = xEntry.Element(XName.Get("content", ManagementClientConstants.AtomNamespace))?
 097                .Element(XName.Get("SubscriptionDescription", ManagementClientConstants.ServiceBusNamespace));
 98
 099            if (qdXml == null)
 100            {
 0101                throw new MessagingEntityNotFoundException("Subscription was not found");
 102            }
 103
 0104            foreach (var element in qdXml.Elements())
 105            {
 0106                switch (element.Name.LocalName)
 107                {
 108                    case "RequiresSession":
 0109                        subscriptionDesc.RequiresSession = bool.Parse(element.Value);
 0110                        break;
 111                    case "DeadLetteringOnMessageExpiration":
 0112                        subscriptionDesc.EnableDeadLetteringOnMessageExpiration = bool.Parse(element.Value);
 0113                        break;
 114                    case "DeadLetteringOnFilterEvaluationExceptions":
 0115                        subscriptionDesc.EnableDeadLetteringOnFilterEvaluationExceptions = bool.Parse(element.Value);
 0116                        break;
 117                    case "LockDuration":
 0118                        subscriptionDesc.LockDuration = XmlConvert.ToTimeSpan(element.Value);
 0119                        break;
 120                    case "DefaultMessageTimeToLive":
 0121                        subscriptionDesc.DefaultMessageTimeToLive = XmlConvert.ToTimeSpan(element.Value);
 0122                        break;
 123                    case "MaxDeliveryCount":
 0124                        subscriptionDesc.MaxDeliveryCount = int.Parse(element.Value);
 0125                        break;
 126                    case "Status":
 0127                        subscriptionDesc.Status = (EntityStatus)Enum.Parse(typeof(EntityStatus), element.Value);
 0128                        break;
 129                    case "EnableBatchedOperations":
 0130                        subscriptionDesc.EnableBatchedOperations = bool.Parse(element.Value);
 0131                        break;
 132                    case "UserMetadata":
 0133                        subscriptionDesc.UserMetadata = element.Value;
 0134                        break;
 135                    case "AutoDeleteOnIdle":
 0136                        subscriptionDesc.AutoDeleteOnIdle = XmlConvert.ToTimeSpan(element.Value);
 0137                        break;
 138                    case "ForwardTo":
 0139                        if (!string.IsNullOrWhiteSpace(element.Value))
 140                        {
 0141                            subscriptionDesc.ForwardTo = element.Value;
 142                        }
 0143                        break;
 144                    case "ForwardDeadLetteredMessagesTo":
 0145                        if (!string.IsNullOrWhiteSpace(element.Value))
 146                        {
 0147                            subscriptionDesc.ForwardDeadLetteredMessagesTo = element.Value;
 148                        }
 0149                        break;
 150                    case "AccessedAt":
 151                    case "CreatedAt":
 152                    case "MessageCount":
 153                    case "SizeInBytes":
 154                    case "UpdatedAt":
 155                    case "CountDetails":
 156                        // Ignore known properties
 157                        // Do nothing
 158                        break;
 159                    default:
 160                        // For unknown properties, keep them as-is for forward proof.
 0161                        if (subscriptionDesc.UnknownProperties == null)
 162                        {
 0163                            subscriptionDesc.UnknownProperties = new List<object>();
 164                        }
 165
 0166                        subscriptionDesc.UnknownProperties.Add(element);
 167                        break;
 168                }
 169            }
 170
 0171            return subscriptionDesc;
 172        }
 173
 174        public static XDocument Serialize(this SubscriptionDescription description)
 175        {
 0176            var subscriptionDescriptionElements = new List<object>()
 0177            {
 0178                new XElement(XName.Get("LockDuration", ManagementClientConstants.ServiceBusNamespace), XmlConvert.ToStri
 0179                new XElement(XName.Get("RequiresSession", ManagementClientConstants.ServiceBusNamespace), XmlConvert.ToS
 0180                description.DefaultMessageTimeToLive != TimeSpan.MaxValue ? new XElement(XName.Get("DefaultMessageTimeTo
 0181                new XElement(XName.Get("DeadLetteringOnMessageExpiration", ManagementClientConstants.ServiceBusNamespace
 0182                new XElement(XName.Get("DeadLetteringOnFilterEvaluationExceptions", ManagementClientConstants.ServiceBus
 0183                description.DefaultRuleDescription != null ? description.DefaultRuleDescription.SerializeRule("DefaultRu
 0184                new XElement(XName.Get("MaxDeliveryCount", ManagementClientConstants.ServiceBusNamespace), XmlConvert.To
 0185                new XElement(XName.Get("EnableBatchedOperations", ManagementClientConstants.ServiceBusNamespace), XmlCon
 0186                new XElement(XName.Get("Status", ManagementClientConstants.ServiceBusNamespace), description.Status.ToSt
 0187                description.ForwardTo != null ? new XElement(XName.Get("ForwardTo", ManagementClientConstants.ServiceBus
 0188                description.UserMetadata != null ? new XElement(XName.Get("UserMetadata", ManagementClientConstants.Serv
 0189                description.ForwardDeadLetteredMessagesTo != null ? new XElement(XName.Get("ForwardDeadLetteredMessagesT
 0190                description.AutoDeleteOnIdle != TimeSpan.MaxValue ? new XElement(XName.Get("AutoDeleteOnIdle", Managemen
 0191            };
 192
 0193            if (description.UnknownProperties != null)
 194            {
 0195                subscriptionDescriptionElements.AddRange(description.UnknownProperties);
 196            }
 197
 0198            return new XDocument(
 0199                new XElement(XName.Get("entry", ManagementClientConstants.AtomNamespace),
 0200                    new XElement(XName.Get("content", ManagementClientConstants.AtomNamespace),
 0201                        new XAttribute("type", "application/xml"),
 0202                        new XElement(XName.Get("SubscriptionDescription", ManagementClientConstants.ServiceBusNamespace)
 0203                            subscriptionDescriptionElements
 0204                        ))
 0205                ));
 206        }
 207    }
 208}