< Summary

Class:Azure.Messaging.ServiceBus.Management.RuleActionExtensions
Assembly:Azure.Messaging.ServiceBus
File(s):C:\Git\azure-sdk-for-net\sdk\servicebus\Azure.Messaging.ServiceBus\src\Management\Rules\RuleActionExtensions.cs
Covered lines:21
Uncovered lines:16
Coverable lines:37
Total lines:87
Line coverage:56.7% (21 of 37)
Covered branches:11
Total branches:22
Branch coverage:50% (11 of 22)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
ParseFromXElement(...)-50%66.67%
ParseFromXElementSqlRuleAction(...)-54.55%41.67%
Serialize(...)-62.5%50%

File(s)

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

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Xml.Linq;
 6using Azure.Messaging.ServiceBus.Diagnostics;
 7
 8namespace Azure.Messaging.ServiceBus.Management
 9{
 10    internal static class RuleActionExtensions
 11    {
 12        internal static RuleAction ParseFromXElement(XElement xElement)
 13        {
 4814            var attribute = xElement.Attribute(XName.Get("type", ManagementClientConstants.XmlSchemaInstanceNamespace));
 4815            if (attribute == null)
 16            {
 017                return null;
 18            }
 19
 4820            switch (attribute.Value)
 21            {
 22                case "SqlRuleAction":
 1223                    return ParseFromXElementSqlRuleAction(xElement);
 24
 25                case "EmptyRuleAction":
 3626                    return null;
 27
 28                default:
 029                    ServiceBusEventSource.Log.ManagementSerializationException(
 030                        $"{nameof(RuleActionExtensions)}_{nameof(ParseFromXElement)}",
 031                        xElement.ToString());
 032                    return null;
 33            }
 34        }
 35
 36        private static RuleAction ParseFromXElementSqlRuleAction(XElement xElement)
 37        {
 1238            var expression = xElement.Element(XName.Get("SqlExpression", ManagementClientConstants.ServiceBusNamespace))
 1239            if (string.IsNullOrWhiteSpace(expression))
 40            {
 041                return null;
 42            }
 43
 1244            var action = new SqlRuleAction(expression);
 45
 1246            var parameters = xElement.Element(XName.Get("Parameters", ManagementClientConstants.ServiceBusNamespace));
 1247            if (parameters != null && parameters.HasElements)
 48            {
 049                foreach (var param in parameters.Elements(XName.Get("KeyValueOfstringanyType", ManagementClientConstants
 50                {
 051                    var key = param.Element(XName.Get("Key", ManagementClientConstants.ServiceBusNamespace))?.Value;
 052                    var value = XmlObjectConvertor.ParseValueObject(param.Element(XName.Get("Value", ManagementClientCon
 053                    action.Parameters.Add(key, value);
 54                }
 55            }
 1256            return action;
 57        }
 58
 59        public static XElement Serialize(this RuleAction action)
 60        {
 461            if (action is SqlRuleAction sqlRuleAction)
 62            {
 463                XElement parameterElement = new XElement(
 464                    XName.Get(
 465                        "Parameters",
 466                        ManagementClientConstants.ServiceBusNamespace));
 067                foreach (var param in sqlRuleAction.Parameters)
 68                {
 069                    parameterElement.Add(
 070                        new XElement(XName.Get("KeyValueOfstringanyType", ManagementClientConstants.ServiceBusNamespace)
 071                            new XElement(XName.Get("Key", ManagementClientConstants.ServiceBusNamespace), param.Key),
 072                            XmlObjectConvertor.SerializeObject(param.Value)));
 73                }
 74
 475                return new XElement(
 476                        XName.Get("Action", ManagementClientConstants.ServiceBusNamespace),
 477                        new XAttribute(XName.Get("type", ManagementClientConstants.XmlSchemaInstanceNamespace), nameof(S
 478                        new XElement(XName.Get("SqlExpression", ManagementClientConstants.ServiceBusNamespace), sqlRuleA
 479                        parameterElement);
 80            }
 81            else
 82            {
 083                throw new NotImplementedException($"Rule action of type {action.GetType().Name} is not implemented");
 84            }
 85        }
 86    }
 87}