< Summary

Class:Microsoft.Azure.ServiceBus.RuleDescriptionExtensions
Assembly:Microsoft.Azure.ServiceBus
File(s):C:\Git\azure-sdk-for-net\sdk\servicebus\Microsoft.Azure.ServiceBus\src\Filters\RuleDescriptionExtensions.cs
Covered lines:0
Uncovered lines:67
Coverable lines:67
Total lines:153
Line coverage:0% (0 of 67)
Covered branches:0
Total branches:40
Branch coverage:0% (0 of 40)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
ValidateDescriptionName(...)-0%0%
ParseFromContent(...)-0%0%
ParseCollectionFromContent(...)-0%0%
ParseFromEntryElement(...)-0%0%
Serialize(...)-0%100%
SerializeRule(...)-0%0%

File(s)

C:\Git\azure-sdk-for-net\sdk\servicebus\Microsoft.Azure.ServiceBus\src\Filters\RuleDescriptionExtensions.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
 5{
 6    using System;
 7    using System.Collections.Generic;
 8    using System.Xml.Linq;
 9    using Microsoft.Azure.ServiceBus.Management;
 10    using Microsoft.Azure.ServiceBus.Primitives;
 11
 12    internal static class RuleDescriptionExtensions
 13    {
 14        public static void ValidateDescriptionName(this RuleDescription description)
 15        {
 016            if (string.IsNullOrWhiteSpace(description.Name))
 17            {
 018                throw Fx.Exception.ArgumentNullOrWhiteSpace(nameof(description.Name));
 19            }
 20
 021            if (description.Name.Length > Constants.RuleNameMaximumLength)
 22            {
 023                throw Fx.Exception.ArgumentOutOfRange(
 024                    nameof(description.Name),
 025                    description.Name,
 026                    Resources.EntityNameLengthExceedsLimit.FormatForUser(description.Name, Constants.RuleNameMaximumLeng
 27            }
 28
 029            if (description.Name.Contains(Constants.PathDelimiter) || description.Name.Contains(@"\"))
 30            {
 031                throw Fx.Exception.Argument(
 032                    nameof(description.Name),
 033                    Resources.InvalidCharacterInEntityName.FormatForUser(Constants.PathDelimiter, description.Name));
 34            }
 35
 036            string[] uriSchemeKeys = { "@", "?", "#" };
 037            foreach (var uriSchemeKey in uriSchemeKeys)
 38            {
 039                if (description.Name.Contains(uriSchemeKey))
 40                {
 041                    throw Fx.Exception.Argument(
 042                        nameof(description.Name),
 043                        Resources.CharacterReservedForUriScheme.FormatForUser(nameof(description.Name), uriSchemeKey));
 44                }
 45            }
 046        }
 47
 48        public static RuleDescription ParseFromContent(string xml)
 49        {
 50            try
 51            {
 052                var xDoc = XElement.Parse(xml);
 053                if (!xDoc.IsEmpty)
 54                {
 055                    if (xDoc.Name.LocalName == "entry")
 56                    {
 057                        return ParseFromEntryElement(xDoc);
 58                    }
 59                }
 060            }
 061            catch (Exception ex) when (!(ex is ServiceBusException))
 62            {
 063                throw new ServiceBusException(false, ex);
 64            }
 65
 066            throw new MessagingEntityNotFoundException("Rule was not found");
 067        }
 68
 69        public static IList<RuleDescription> ParseCollectionFromContent(string xml)
 70        {
 71            try
 72            {
 073                var xDoc = XElement.Parse(xml);
 074                if (!xDoc.IsEmpty)
 75                {
 076                    if (xDoc.Name.LocalName == "feed")
 77                    {
 078                        var rules = new List<RuleDescription>();
 79
 080                        var entryList = xDoc.Elements(XName.Get("entry", ManagementClientConstants.AtomNamespace));
 081                        foreach (var entry in entryList)
 82                        {
 083                            rules.Add(ParseFromEntryElement(entry));
 84                        }
 85
 086                        return rules;
 87                    }
 88                }
 089            }
 090            catch (Exception ex) when (!(ex is ServiceBusException))
 91            {
 092                throw new ServiceBusException(false, ex);
 93            }
 94
 095            throw new MessagingEntityNotFoundException("Rule was not found");
 096        }
 97
 98        private static RuleDescription ParseFromEntryElement(XElement xEntry)
 99        {
 0100            var name = xEntry.Element(XName.Get("title", ManagementClientConstants.AtomNamespace)).Value;
 0101            var ruleDescription = new RuleDescription();
 102
 0103            var rdXml = xEntry.Element(XName.Get("content", ManagementClientConstants.AtomNamespace))?
 0104                .Element(XName.Get("RuleDescription", ManagementClientConstants.ServiceBusNamespace));
 105
 0106            if (rdXml == null)
 107            {
 0108                throw new MessagingEntityNotFoundException("Rule was not found");
 109            }
 110
 0111            foreach (var element in rdXml.Elements())
 112            {
 0113                switch (element.Name.LocalName)
 114                {
 115                    case "Name":
 0116                        ruleDescription.Name = element.Value;
 0117                        break;
 118                    case "Filter":
 0119                        ruleDescription.Filter = FilterExtensions.ParseFromXElement(element);
 0120                        break;
 121                    case "Action":
 0122                        ruleDescription.Action = RuleActionExtensions.ParseFromXElement(element);
 0123                        break;
 124                    case "CreatedAt":
 0125                        ruleDescription.CreatedAt = DateTime.Parse(element.Value);
 126                        break;
 127                }
 128            }
 129
 0130            return ruleDescription;
 131        }
 132
 133        public static XDocument Serialize(this RuleDescription description)
 134        {
 0135            XDocument doc = new XDocument(
 0136                   new XElement(XName.Get("entry", ManagementClientConstants.AtomNamespace),
 0137                       new XElement(XName.Get("content", ManagementClientConstants.AtomNamespace),
 0138                           new XAttribute("type", "application/xml"),
 0139                           description.SerializeRule())));
 140
 0141            return doc;
 142        }
 143
 144        public static XElement SerializeRule(this RuleDescription description, string elementName = "RuleDescription")
 145        {
 0146            return new XElement(
 0147                XName.Get(elementName, ManagementClientConstants.ServiceBusNamespace),
 0148                description.Filter?.Serialize(),
 0149                description.Action?.Serialize(),
 0150                new XElement(XName.Get("Name", ManagementClientConstants.ServiceBusNamespace), description.Name));
 151        }
 152    }
 153}