< Summary

Class:Azure.Messaging.ServiceBus.Management.SubscriptionPropertiesExtensions
Assembly:Azure.Messaging.ServiceBus
File(s):C:\Git\azure-sdk-for-net\sdk\servicebus\Azure.Messaging.ServiceBus\src\Management\SubscriptionPropertiesExtensions.cs
Covered lines:76
Uncovered lines:19
Coverable lines:95
Total lines:206
Line coverage:80% (76 of 95)
Covered branches:111
Total branches:128
Branch coverage:86.7% (111 of 128)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
NormalizeDescription(...)-60%50%
NormalizeForwardToAddress(...)-0%0%
ParseFromContent(...)-77.78%100%
ParseCollectionFromContent(...)-69.23%66.67%
ParseFromEntryElement(...)-81.58%90.82%
Serialize(...)-100%85.71%

File(s)

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

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Collections.Generic;
 6using System.Globalization;
 7using System.Xml;
 8using System.Xml.Linq;
 9
 10namespace Azure.Messaging.ServiceBus.Management
 11{
 12    internal static class SubscriptionPropertiesExtensions
 13    {
 14        public static void NormalizeDescription(this SubscriptionProperties description, string baseAddress)
 15        {
 3616            if (!string.IsNullOrWhiteSpace(description.ForwardTo))
 17            {
 018                description.ForwardTo = NormalizeForwardToAddress(description.ForwardTo, baseAddress);
 19            }
 20
 3621            if (!string.IsNullOrWhiteSpace(description.ForwardDeadLetteredMessagesTo))
 22            {
 023                description.ForwardDeadLetteredMessagesTo = NormalizeForwardToAddress(description.ForwardDeadLetteredMes
 24            }
 3625        }
 26
 27        public static string NormalizeForwardToAddress(string forwardTo, string baseAddress)
 28        {
 029            baseAddress = new UriBuilder(baseAddress).Uri.ToString();
 30
 031            if (!Uri.TryCreate(forwardTo, UriKind.Absolute, out Uri forwardToUri))
 32            {
 033                forwardToUri = new Uri(new Uri(baseAddress), forwardTo);
 34            }
 35
 036            return forwardToUri.AbsoluteUri;
 37        }
 38
 39        public static SubscriptionProperties ParseFromContent(string topicName, string xml)
 40        {
 41            try
 42            {
 4843                var xDoc = XElement.Parse(xml);
 4844                if (!xDoc.IsEmpty)
 45                {
 4846                    if (xDoc.Name.LocalName == "entry")
 47                    {
 4048                        return ParseFromEntryElement(topicName, xDoc);
 49                    }
 50                }
 851            }
 052            catch (Exception ex) when (!(ex is ServiceBusException))
 53            {
 054                throw new ServiceBusException(false, ex.Message);
 55            }
 56
 857            throw new ServiceBusException("Subscription was not found", ServiceBusFailureReason.MessagingEntityNotFound)
 4058        }
 59
 60        public static List<SubscriptionProperties> ParseCollectionFromContent(string topicName, string xml)
 61        {
 62            try
 63            {
 464                var xDoc = XElement.Parse(xml);
 465                if (!xDoc.IsEmpty)
 66                {
 467                    if (xDoc.Name.LocalName == "feed")
 68                    {
 469                        var subscriptionList = new List<SubscriptionProperties>();
 70
 471                        var entryList = xDoc.Elements(XName.Get("entry", ManagementClientConstants.AtomNamespace));
 1672                        foreach (var entry in entryList)
 73                        {
 474                            subscriptionList.Add(ParseFromEntryElement(topicName, entry));
 75                        }
 76
 477                        return subscriptionList;
 78                    }
 79                }
 080            }
 081            catch (Exception ex) when (!(ex is ServiceBusException))
 82            {
 083                throw new ServiceBusException(false, ex.Message);
 84            }
 85
 086            throw new ServiceBusException("No subscriptions were found", ServiceBusFailureReason.MessagingEntityNotFound
 487        }
 88
 89        private static SubscriptionProperties ParseFromEntryElement(string topicName, XElement xEntry)
 90        {
 4491            var name = xEntry.Element(XName.Get("title", ManagementClientConstants.AtomNamespace)).Value;
 4492            var subscriptionDesc = new SubscriptionProperties(topicName, name);
 93
 4494            var qdXml = xEntry.Element(XName.Get("content", ManagementClientConstants.AtomNamespace))?
 4495                .Element(XName.Get("SubscriptionDescription", ManagementClientConstants.ServiceBusNamespace));
 96
 4497            if (qdXml == null)
 98            {
 099                throw new ServiceBusException("Subscription was not found", ServiceBusFailureReason.MessagingEntityNotFo
 100            }
 101
 1392102            foreach (var element in qdXml.Elements())
 103            {
 652104                switch (element.Name.LocalName)
 105                {
 106                    case "RequiresSession":
 44107                        subscriptionDesc.RequiresSession = bool.Parse(element.Value);
 44108                        break;
 109                    case "DeadLetteringOnMessageExpiration":
 44110                        subscriptionDesc.DeadLetteringOnMessageExpiration = bool.Parse(element.Value);
 44111                        break;
 112                    case "DeadLetteringOnFilterEvaluationExceptions":
 44113                        subscriptionDesc.EnableDeadLetteringOnFilterEvaluationExceptions = bool.Parse(element.Value);
 44114                        break;
 115                    case "LockDuration":
 44116                        subscriptionDesc.LockDuration = XmlConvert.ToTimeSpan(element.Value);
 44117                        break;
 118                    case "DefaultMessageTimeToLive":
 44119                        subscriptionDesc.DefaultMessageTimeToLive = XmlConvert.ToTimeSpan(element.Value);
 44120                        break;
 121                    case "MaxDeliveryCount":
 44122                        subscriptionDesc.MaxDeliveryCount = int.Parse(element.Value, CultureInfo.InvariantCulture);
 44123                        break;
 124                    case "Status":
 44125                        subscriptionDesc.Status = element.Value;
 44126                        break;
 127                    case "EnableBatchedOperations":
 44128                        subscriptionDesc.EnableBatchedOperations = bool.Parse(element.Value);
 44129                        break;
 130                    case "UserMetadata":
 20131                        subscriptionDesc.UserMetadata = element.Value;
 20132                        break;
 133                    case "AutoDeleteOnIdle":
 44134                        subscriptionDesc.AutoDeleteOnIdle = XmlConvert.ToTimeSpan(element.Value);
 44135                        break;
 136                    case "ForwardTo":
 0137                        if (!string.IsNullOrWhiteSpace(element.Value))
 138                        {
 0139                            subscriptionDesc.ForwardTo = element.Value;
 140                        }
 0141                        break;
 142                    case "ForwardDeadLetteredMessagesTo":
 0143                        if (!string.IsNullOrWhiteSpace(element.Value))
 144                        {
 0145                            subscriptionDesc.ForwardDeadLetteredMessagesTo = element.Value;
 146                        }
 0147                        break;
 148                    case "AccessedAt":
 149                    case "CreatedAt":
 150                    case "MessageCount":
 151                    case "SizeInBytes":
 152                    case "UpdatedAt":
 153                    case "CountDetails":
 154                        // Ignore known properties
 155                        // Do nothing
 156                        break;
 157                    default:
 158                        // For unknown properties, keep them as-is for forward proof.
 44159                        if (subscriptionDesc.UnknownProperties == null)
 160                        {
 44161                            subscriptionDesc.UnknownProperties = new List<object>();
 162                        }
 163
 44164                        subscriptionDesc.UnknownProperties.Add(element);
 165                        break;
 166                }
 167            }
 168
 44169            return subscriptionDesc;
 170        }
 171
 172        public static XDocument Serialize(this SubscriptionProperties description)
 173        {
 36174            var subscriptionDescriptionElements = new List<object>()
 36175            {
 36176                new XElement(XName.Get("LockDuration", ManagementClientConstants.ServiceBusNamespace), XmlConvert.ToStri
 36177                new XElement(XName.Get("RequiresSession", ManagementClientConstants.ServiceBusNamespace), XmlConvert.ToS
 36178                description.DefaultMessageTimeToLive != TimeSpan.MaxValue ? new XElement(XName.Get("DefaultMessageTimeTo
 36179                new XElement(XName.Get("DeadLetteringOnMessageExpiration", ManagementClientConstants.ServiceBusNamespace
 36180                new XElement(XName.Get("DeadLetteringOnFilterEvaluationExceptions", ManagementClientConstants.ServiceBus
 36181                description.Rule != null ? description.Rule.SerializeRule("DefaultRuleDescription") : null,
 36182                new XElement(XName.Get("MaxDeliveryCount", ManagementClientConstants.ServiceBusNamespace), XmlConvert.To
 36183                new XElement(XName.Get("EnableBatchedOperations", ManagementClientConstants.ServiceBusNamespace), XmlCon
 36184                new XElement(XName.Get("Status", ManagementClientConstants.ServiceBusNamespace), description.Status.ToSt
 36185                description.ForwardTo != null ? new XElement(XName.Get("ForwardTo", ManagementClientConstants.ServiceBus
 36186                description.UserMetadata != null ? new XElement(XName.Get("UserMetadata", ManagementClientConstants.Serv
 36187                description.ForwardDeadLetteredMessagesTo != null ? new XElement(XName.Get("ForwardDeadLetteredMessagesT
 36188                description.AutoDeleteOnIdle != TimeSpan.MaxValue ? new XElement(XName.Get("AutoDeleteOnIdle", Managemen
 36189            };
 190
 36191            if (description.UnknownProperties != null)
 192            {
 4193                subscriptionDescriptionElements.AddRange(description.UnknownProperties);
 194            }
 195
 36196            return new XDocument(
 36197                new XElement(XName.Get("entry", ManagementClientConstants.AtomNamespace),
 36198                    new XElement(XName.Get("content", ManagementClientConstants.AtomNamespace),
 36199                        new XAttribute("type", "application/xml"),
 36200                        new XElement(XName.Get("SubscriptionDescription", ManagementClientConstants.ServiceBusNamespace)
 36201                            subscriptionDescriptionElements
 36202                        ))
 36203                ));
 204        }
 205    }
 206}