< Summary

Class:Azure.Messaging.ServiceBus.Management.TopicPropertiesExtensions
Assembly:Azure.Messaging.ServiceBus
File(s):C:\Git\azure-sdk-for-net\sdk\servicebus\Azure.Messaging.ServiceBus\src\Management\TopicPropertiesExtensions.cs
Covered lines:78
Uncovered lines:5
Coverable lines:83
Total lines:175
Line coverage:93.9% (78 of 83)
Covered branches:113
Total branches:118
Branch coverage:95.7% (113 of 118)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
ParseFromContent(...)-88.89%100%
ParseCollectionFromContent(...)-69.23%66.67%
ParseFromEntryElement(...)-100%97.87%
Serialize(...)-100%92.86%

File(s)

C:\Git\azure-sdk-for-net\sdk\servicebus\Azure.Messaging.ServiceBus\src\Management\TopicPropertiesExtensions.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 TopicPropertiesExtensions
 13    {
 14        public static TopicProperties ParseFromContent(string xml)
 15        {
 16            try
 17            {
 6818                var xDoc = XElement.Parse(xml);
 6819                if (!xDoc.IsEmpty)
 20                {
 6821                    if (xDoc.Name.LocalName == "entry")
 22                    {
 6023                        return ParseFromEntryElement(xDoc);
 24                    }
 25                }
 826            }
 427            catch (Exception ex) when (!(ex is ServiceBusException))
 28            {
 029                throw new ServiceBusException(false, ex.Message);
 30            }
 31
 832            throw new ServiceBusException("Topic was not found", ServiceBusFailureReason.MessagingEntityNotFound);
 5633        }
 34
 35        public static List<TopicProperties> ParseCollectionFromContent(string xml)
 36        {
 37            try
 38            {
 439                var xDoc = XElement.Parse(xml);
 440                if (!xDoc.IsEmpty)
 41                {
 442                    if (xDoc.Name.LocalName == "feed")
 43                    {
 444                        var topicList = new List<TopicProperties>();
 45
 446                        var entryList = xDoc.Elements(XName.Get("entry", ManagementClientConstants.AtomNamespace));
 1647                        foreach (var entry in entryList)
 48                        {
 449                            topicList.Add(ParseFromEntryElement(entry));
 50                        }
 51
 452                        return topicList;
 53                    }
 54                }
 055            }
 056            catch (Exception ex) when (!(ex is ServiceBusException))
 57            {
 058                throw new ServiceBusException(false, ex.Message);
 59            }
 60
 061            throw new ServiceBusException("No topics were found", ServiceBusFailureReason.MessagingEntityNotFound);
 462        }
 63
 64        private static TopicProperties ParseFromEntryElement(XElement xEntry)
 65        {
 6466            var name = xEntry.Element(XName.Get("title", ManagementClientConstants.AtomNamespace)).Value;
 6467            var topicDesc = new TopicProperties(name);
 68
 6469            var qdXml = xEntry.Element(XName.Get("content", ManagementClientConstants.AtomNamespace))?
 6470                .Element(XName.Get("TopicDescription", ManagementClientConstants.ServiceBusNamespace));
 71
 6472            if (qdXml == null)
 73            {
 474                throw new ServiceBusException("Topic was not found", ServiceBusFailureReason.MessagingEntityNotFound);
 75            }
 76
 238477            foreach (var element in qdXml.Elements())
 78            {
 113279                switch (element.Name.LocalName)
 80                {
 81                    case "MaxSizeInMegabytes":
 6082                        topicDesc.MaxSizeInMegabytes = long.Parse(element.Value, CultureInfo.InvariantCulture);
 6083                        break;
 84                    case "RequiresDuplicateDetection":
 6085                        topicDesc.RequiresDuplicateDetection = bool.Parse(element.Value);
 6086                        break;
 87                    case "DuplicateDetectionHistoryTimeWindow":
 5688                        topicDesc.DuplicateDetectionHistoryTimeWindow = XmlConvert.ToTimeSpan(element.Value);
 5689                        break;
 90                    case "DefaultMessageTimeToLive":
 5691                        topicDesc.DefaultMessageTimeToLive = XmlConvert.ToTimeSpan(element.Value);
 5692                        break;
 93                    case "EnableBatchedOperations":
 6094                        topicDesc.EnableBatchedOperations = bool.Parse(element.Value);
 6095                        break;
 96                    case "Status":
 6097                        topicDesc.Status = element.Value;
 6098                        break;
 99                    case "UserMetadata":
 20100                        topicDesc.UserMetadata = element.Value;
 20101                        break;
 102                    case "AutoDeleteOnIdle":
 60103                        topicDesc.AutoDeleteOnIdle = XmlConvert.ToTimeSpan(element.Value);
 60104                        break;
 105                    case "EnablePartitioning":
 60106                        topicDesc.EnablePartitioning = bool.Parse(element.Value);
 60107                        break;
 108                    case "SupportOrdering":
 60109                        topicDesc.SupportOrdering = bool.Parse(element.Value);
 60110                        break;
 111                    case "AuthorizationRules":
 60112                        topicDesc.AuthorizationRules = AuthorizationRules.ParseFromXElement(element);
 60113                        break;
 114                    case "AccessedAt":
 115                    case "CreatedAt":
 116                    case "MessageCount":
 117                    case "SizeInBytes":
 118                    case "UpdatedAt":
 119                    case "CountDetails":
 120                    case "SubscriptionCount":
 121                        // Ignore known properties
 122                        // Do nothing
 123                        break;
 124                    default:
 125                        // For unknown properties, keep them as-is for forward proof.
 316126                        if (topicDesc.UnknownProperties == null)
 127                        {
 60128                            topicDesc.UnknownProperties = new List<object>();
 129                        }
 130
 316131                        topicDesc.UnknownProperties.Add(element);
 132                        break;
 133                }
 134            }
 135
 60136            return topicDesc;
 137        }
 138
 139        public static XDocument Serialize(this TopicProperties description)
 140        {
 52141            var topicDescriptionElements = new List<object>
 52142            {
 52143                description.DefaultMessageTimeToLive != TimeSpan.MaxValue ? new XElement(XName.Get("DefaultMessageTimeTo
 52144                new XElement(XName.Get("MaxSizeInMegabytes", ManagementClientConstants.ServiceBusNamespace), XmlConvert.
 52145                new XElement(XName.Get("RequiresDuplicateDetection", ManagementClientConstants.ServiceBusNamespace), Xml
 52146                description.RequiresDuplicateDetection && description.DuplicateDetectionHistoryTimeWindow != default ?
 52147                    new XElement(XName.Get("DuplicateDetectionHistoryTimeWindow", ManagementClientConstants.ServiceBusNa
 52148                    : null,
 52149                new XElement(XName.Get("EnableBatchedOperations", ManagementClientConstants.ServiceBusNamespace), XmlCon
 52150                description.AuthorizationRules?.Serialize(),
 52151                new XElement(XName.Get("Status", ManagementClientConstants.ServiceBusNamespace), description.Status.ToSt
 52152                description.UserMetadata != null ? new XElement(XName.Get("UserMetadata", ManagementClientConstants.Serv
 52153                new XElement(XName.Get("SupportOrdering", ManagementClientConstants.ServiceBusNamespace), XmlConvert.ToS
 52154                description.AutoDeleteOnIdle != TimeSpan.MaxValue ? new XElement(XName.Get("AutoDeleteOnIdle", Managemen
 52155                new XElement(XName.Get("EnablePartitioning", ManagementClientConstants.ServiceBusNamespace), XmlConvert.
 52156            };
 157
 52158            if (description.UnknownProperties != null)
 159            {
 8160                topicDescriptionElements.AddRange(description.UnknownProperties);
 161            }
 162
 52163            XDocument doc = new XDocument(
 52164                new XElement(XName.Get("entry", ManagementClientConstants.AtomNamespace),
 52165                    new XElement(XName.Get("content", ManagementClientConstants.AtomNamespace),
 52166                        new XAttribute("type", "application/xml"),
 52167                        new XElement(XName.Get("TopicDescription", ManagementClientConstants.ServiceBusNamespace),
 52168                            topicDescriptionElements
 52169                        ))
 52170                    ));
 171
 52172            return doc;
 173        }
 174    }
 175}