< Summary

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

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
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\TopicDescriptionExtensions.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 TopicDescriptionExtensions
 12    {
 13        public static TopicDescription ParseFromContent(string xml)
 14        {
 15            try
 16            {
 017                var xDoc = XElement.Parse(xml);
 018                if (!xDoc.IsEmpty)
 19                {
 020                    if (xDoc.Name.LocalName == "entry")
 21                    {
 022                        return ParseFromEntryElement(xDoc);
 23                    }
 24                }
 025            }
 026            catch (Exception ex) when (!(ex is ServiceBusException))
 27            {
 028                throw new ServiceBusException(false, ex);
 29            }
 30
 031            throw new MessagingEntityNotFoundException("Topic was not found");
 032        }
 33
 34        public static IList<TopicDescription> ParseCollectionFromContent(string xml)
 35        {
 36            try
 37            {
 038                var xDoc = XElement.Parse(xml);
 039                if (!xDoc.IsEmpty)
 40                {
 041                    if (xDoc.Name.LocalName == "feed")
 42                    {
 043                        var topicList = new List<TopicDescription>();
 44
 045                        var entryList = xDoc.Elements(XName.Get("entry", ManagementClientConstants.AtomNamespace));
 046                        foreach (var entry in entryList)
 47                        {
 048                            topicList.Add(ParseFromEntryElement(entry));
 49                        }
 50
 051                        return topicList;
 52                    }
 53                }
 054            }
 055            catch (Exception ex) when (!(ex is ServiceBusException))
 56            {
 057                throw new ServiceBusException(false, ex);
 58            }
 59
 060            throw new MessagingEntityNotFoundException("No topics were found");
 061        }
 62
 63        private static TopicDescription ParseFromEntryElement(XElement xEntry)
 64        {
 065            var name = xEntry.Element(XName.Get("title", ManagementClientConstants.AtomNamespace)).Value;
 066            var topicDesc = new TopicDescription(name);
 67
 068            var qdXml = xEntry.Element(XName.Get("content", ManagementClientConstants.AtomNamespace))?
 069                .Element(XName.Get("TopicDescription", ManagementClientConstants.ServiceBusNamespace));
 70
 071            if (qdXml == null)
 72            {
 073                throw new MessagingEntityNotFoundException("Topic was not found");
 74            }
 75
 076            foreach (var element in qdXml.Elements())
 77            {
 078                switch (element.Name.LocalName)
 79                {
 80                    case "MaxSizeInMegabytes":
 081                        topicDesc.MaxSizeInMB = long.Parse(element.Value);
 082                        break;
 83                    case "RequiresDuplicateDetection":
 084                        topicDesc.RequiresDuplicateDetection = bool.Parse(element.Value);
 085                        break;
 86                    case "DuplicateDetectionHistoryTimeWindow":
 087                        topicDesc.duplicateDetectionHistoryTimeWindow = XmlConvert.ToTimeSpan(element.Value);
 088                        break;
 89                    case "DefaultMessageTimeToLive":
 090                        topicDesc.DefaultMessageTimeToLive = XmlConvert.ToTimeSpan(element.Value);
 091                        break;
 92                    case "EnableBatchedOperations":
 093                        topicDesc.EnableBatchedOperations = bool.Parse(element.Value);
 094                        break;
 95                    case "Status":
 096                        topicDesc.Status = (EntityStatus)Enum.Parse(typeof(EntityStatus), element.Value);
 097                        break;
 98                    case "UserMetadata":
 099                        topicDesc.UserMetadata = element.Value;
 0100                        break;
 101                    case "AutoDeleteOnIdle":
 0102                        topicDesc.AutoDeleteOnIdle = XmlConvert.ToTimeSpan(element.Value);
 0103                        break;
 104                    case "EnablePartitioning":
 0105                        topicDesc.EnablePartitioning = bool.Parse(element.Value);
 0106                        break;
 107                    case "SupportOrdering":
 0108                        topicDesc.SupportOrdering = bool.Parse(element.Value);
 0109                        break;
 110                    case "AuthorizationRules":
 0111                        topicDesc.AuthorizationRules = AuthorizationRules.ParseFromXElement(element);
 0112                        break;
 113                    case "AccessedAt":
 114                    case "CreatedAt":
 115                    case "MessageCount":
 116                    case "SizeInBytes":
 117                    case "UpdatedAt":
 118                    case "CountDetails":
 119                    case "SubscriptionCount":
 120                        // Ignore known properties
 121                        // Do nothing
 122                        break;
 123                    default:
 124                        // For unknown properties, keep them as-is for forward proof.
 0125                        if (topicDesc.UnknownProperties == null)
 126                        {
 0127                            topicDesc.UnknownProperties = new List<object>();
 128                        }
 129
 0130                        topicDesc.UnknownProperties.Add(element);
 131                        break;
 132                }
 133            }
 134
 0135            return topicDesc;
 136        }
 137
 138        public static XDocument Serialize(this TopicDescription description)
 139        {
 0140            var topicDescriptionElements = new List<object>
 0141            {
 0142                description.DefaultMessageTimeToLive != TimeSpan.MaxValue ? new XElement(XName.Get("DefaultMessageTimeTo
 0143                new XElement(XName.Get("MaxSizeInMegabytes", ManagementClientConstants.ServiceBusNamespace), XmlConvert.
 0144                new XElement(XName.Get("RequiresDuplicateDetection", ManagementClientConstants.ServiceBusNamespace), Xml
 0145                description.RequiresDuplicateDetection && description.DuplicateDetectionHistoryTimeWindow != default ?
 0146                    new XElement(XName.Get("DuplicateDetectionHistoryTimeWindow", ManagementClientConstants.ServiceBusNa
 0147                    : null,
 0148                new XElement(XName.Get("EnableBatchedOperations", ManagementClientConstants.ServiceBusNamespace), XmlCon
 0149                description.AuthorizationRules?.Serialize(),
 0150                new XElement(XName.Get("Status", ManagementClientConstants.ServiceBusNamespace), description.Status.ToSt
 0151                description.UserMetadata != null ? new XElement(XName.Get("UserMetadata", ManagementClientConstants.Serv
 0152                new XElement(XName.Get("SupportOrdering", ManagementClientConstants.ServiceBusNamespace), XmlConvert.ToS
 0153                description.AutoDeleteOnIdle != TimeSpan.MaxValue ? new XElement(XName.Get("AutoDeleteOnIdle", Managemen
 0154                new XElement(XName.Get("EnablePartitioning", ManagementClientConstants.ServiceBusNamespace), XmlConvert.
 0155            };
 156
 0157            if (description.UnknownProperties != null)
 158            {
 0159                topicDescriptionElements.AddRange(description.UnknownProperties);
 160            }
 161
 0162            XDocument doc = new XDocument(
 0163                new XElement(XName.Get("entry", ManagementClientConstants.AtomNamespace),
 0164                    new XElement(XName.Get("content", ManagementClientConstants.AtomNamespace),
 0165                        new XAttribute("type", "application/xml"),
 0166                        new XElement(XName.Get("TopicDescription", ManagementClientConstants.ServiceBusNamespace),
 0167                            topicDescriptionElements
 0168                        ))
 0169                    ));
 170
 0171            return doc;
 172        }
 173    }
 174}