< Summary

Class:Azure.Messaging.ServiceBus.Management.RuleDescriptionExtensions
Assembly:Azure.Messaging.ServiceBus
File(s):C:\Git\azure-sdk-for-net\sdk\servicebus\Azure.Messaging.ServiceBus\src\Management\Rules\RuleDescriptionExtensions.cs
Covered lines:40
Uncovered lines:21
Coverable lines:61
Total lines:149
Line coverage:65.5% (40 of 61)
Covered branches:20
Total branches:34
Branch coverage:58.8% (20 of 34)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
ValidateDescriptionName(...)-0%0%
ParseFromContent(...)-55.56%50%
ParseCollectionFromContent(...)-69.23%66.67%
ParseFromEntryElement(...)-100%91.67%
Serialize(...)-100%100%
SerializeRule(...)-100%75%

File(s)

C:\Git\azure-sdk-for-net\sdk\servicebus\Azure.Messaging.ServiceBus\src\Management\Rules\RuleDescriptionExtensions.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.Linq;
 8using Azure.Core;
 9using Azure.Messaging.ServiceBus.Primitives;
 10
 11namespace Azure.Messaging.ServiceBus.Management
 12{
 13    internal static class RuleDescriptionExtensions
 14    {
 15        public static void ValidateDescriptionName(this RuleProperties description)
 16        {
 017            Argument.AssertNotNullOrWhiteSpace(description.Name, nameof(description.Name));
 018            Argument.AssertNotTooLong(description.Name, Constants.RuleNameMaximumLength, nameof(description.Name));
 19
 020            if (description.Name.Contains(Constants.PathDelimiter) || description.Name.Contains(@"\"))
 21            {
 22#pragma warning disable CA2208 // Instantiate argument exceptions correctly. Specifying the Name property
 23                               // is more intuitive, than just description.
 024                throw new ArgumentException(
 025                    Resources.InvalidCharacterInEntityName.FormatForUser(Constants.PathDelimiter, description.Name),
 026                    nameof(description.Name));
 27#pragma warning restore CA2208 // Instantiate argument exceptions correctly
 28            }
 29
 030            string[] uriSchemeKeys = { "@", "?", "#" };
 031            foreach (var uriSchemeKey in uriSchemeKeys)
 32            {
 033                if (description.Name.Contains(uriSchemeKey))
 34                {
 35#pragma warning disable CA2208 // Instantiate argument exceptions correctly
 036                    throw new ArgumentException(
 037                        Resources.CharacterReservedForUriScheme.FormatForUser(nameof(description.Name), uriSchemeKey),
 038                        nameof(description.Name));
 39#pragma warning restore CA2208 // Instantiate argument exceptions correctly
 40                }
 41            }
 042        }
 43
 44        public static RuleProperties ParseFromContent(string xml)
 45        {
 46            try
 47            {
 3648                var xDoc = XElement.Parse(xml);
 3649                if (!xDoc.IsEmpty)
 50                {
 3651                    if (xDoc.Name.LocalName == "entry")
 52                    {
 3653                        return ParseFromEntryElement(xDoc);
 54                    }
 55                }
 056            }
 057            catch (Exception ex) when (!(ex is ServiceBusException))
 58            {
 59                //throw new ServiceBusException(false, ex);
 060            }
 061            return null;
 62            //throw new MessagingEntityNotFoundException("Rule was not found");
 3663        }
 64
 65        public static List<RuleProperties> ParseCollectionFromContent(string xml)
 66        {
 67            try
 68            {
 469                var xDoc = XElement.Parse(xml);
 470                if (!xDoc.IsEmpty)
 71                {
 472                    if (xDoc.Name.LocalName == "feed")
 73                    {
 474                        var rules = new List<RuleProperties>();
 75
 476                        var entryList = xDoc.Elements(XName.Get("entry", ManagementClientConstants.AtomNamespace));
 3277                        foreach (var entry in entryList)
 78                        {
 1279                            rules.Add(ParseFromEntryElement(entry));
 80                        }
 81
 482                        return rules;
 83                    }
 84                }
 085            }
 086            catch (Exception ex) when (!(ex is ServiceBusException))
 87            {
 88                //throw new ServiceBusException(false, ex);
 089            }
 090            return null;
 91            //throw new MessagingEntityNotFoundException("Rule was not found");
 492        }
 93
 94        private static RuleProperties ParseFromEntryElement(XElement xEntry)
 95        {
 4896            var name = xEntry.Element(XName.Get("title", ManagementClientConstants.AtomNamespace)).Value;
 4897            var ruleDescription = new RuleProperties();
 98
 4899            var rdXml = xEntry.Element(XName.Get("content", ManagementClientConstants.AtomNamespace))?
 48100                .Element(XName.Get("RuleDescription", ManagementClientConstants.ServiceBusNamespace));
 101
 48102            if (rdXml == null)
 103            {
 104                //throw new MessagingEntityNotFoundException("Rule was not found");
 105            }
 106
 480107            foreach (var element in rdXml.Elements())
 108            {
 192109                switch (element.Name.LocalName)
 110                {
 111                    case "Name":
 48112                        ruleDescription.Name = element.Value;
 48113                        break;
 114                    case "Filter":
 48115                        ruleDescription.Filter = RuleFilterExtensions.ParseFromXElement(element);
 48116                        break;
 117                    case "Action":
 48118                        ruleDescription.Action = RuleActionExtensions.ParseFromXElement(element);
 48119                        break;
 120                    case "CreatedAt":
 48121                        ruleDescription.CreatedAt = DateTime.Parse(element.Value, CultureInfo.InvariantCulture);
 122                        break;
 123                }
 124            }
 125
 48126            return ruleDescription;
 127        }
 128
 129        public static XDocument Serialize(this RuleProperties description)
 130        {
 20131            XDocument doc = new XDocument(
 20132                   new XElement(XName.Get("entry", ManagementClientConstants.AtomNamespace),
 20133                       new XElement(XName.Get("content", ManagementClientConstants.AtomNamespace),
 20134                           new XAttribute("type", "application/xml"),
 20135                           description.SerializeRule())));
 136
 20137            return doc;
 138        }
 139
 140        public static XElement SerializeRule(this RuleProperties description, string elementName = "RuleDescription")
 141        {
 48142            return new XElement(
 48143                XName.Get(elementName, ManagementClientConstants.ServiceBusNamespace),
 48144                description.Filter?.Serialize(),
 48145                description.Action?.Serialize(),
 48146                new XElement(XName.Get("Name", ManagementClientConstants.ServiceBusNamespace), description.Name));
 147        }
 148    }
 149}