< Summary

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

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
ParseFromXElement(...)-0%0%
ParseFromXElementSqlRuleAction(...)-0%0%
Serialize(...)-0%0%

File(s)

C:\Git\azure-sdk-for-net\sdk\servicebus\Microsoft.Azure.ServiceBus\src\Filters\RuleActionExtensions.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.Xml.Linq;
 8    using Microsoft.Azure.ServiceBus.Filters;
 9    using Microsoft.Azure.ServiceBus.Management;
 10
 11    internal static class RuleActionExtensions
 12    {
 13        internal static RuleAction ParseFromXElement(XElement xElement)
 14        {
 015            var attribute = xElement.Attribute(XName.Get("type", ManagementClientConstants.XmlSchemaInstanceNamespace));
 016            if (attribute == null)
 17            {
 018                return null;
 19            }
 20
 021            switch (attribute.Value)
 22            {
 23                case "SqlRuleAction":
 024                    return ParseFromXElementSqlRuleAction(xElement);
 25
 26                case "EmptyRuleAction":
 027                    return null;
 28
 29                default:
 030                    MessagingEventSource.Log.ManagementSerializationException(
 031                        $"{nameof(RuleActionExtensions)}_{nameof(ParseFromXElement)}",
 032                        xElement.ToString());
 033                    return null;
 34            }
 35        }
 36
 37
 38        static RuleAction ParseFromXElementSqlRuleAction(XElement xElement)
 39        {
 040            var expression = xElement.Element(XName.Get("SqlExpression", ManagementClientConstants.ServiceBusNamespace))
 041            if (string.IsNullOrWhiteSpace(expression))
 42            {
 043                return null;
 44            }
 45
 046            var action = new SqlRuleAction(expression);
 47
 048            var parameters = xElement.Element(XName.Get("Parameters", ManagementClientConstants.ServiceBusNamespace));
 049            if (parameters != null && parameters.HasElements)
 50            {
 051                foreach (var param in parameters.Elements(XName.Get("KeyValueOfstringanyType", ManagementClientConstants
 52                {
 053                    var key = param.Element(XName.Get("Key", ManagementClientConstants.ServiceBusNamespace))?.Value;
 054                    var value = XmlObjectConvertor.ParseValueObject(param.Element(XName.Get("Value", ManagementClientCon
 055                    action.Parameters.Add(key, value);
 56                }
 57            }
 58
 059            return action;
 60        }
 61
 62        public static XElement Serialize(this RuleAction action)
 63        {
 064            if (action is SqlRuleAction sqlRuleAction)
 65            {
 066                XElement parameterElement = null;
 067                if (sqlRuleAction.parameters != null)
 68                {
 069                    parameterElement = new XElement(XName.Get("Parameters", ManagementClientConstants.ServiceBusNamespac
 070                    foreach (var param in sqlRuleAction.Parameters)
 71                    {
 072                        parameterElement.Add(
 073                            new XElement(XName.Get("KeyValueOfstringanyType", ManagementClientConstants.ServiceBusNamesp
 074                                new XElement(XName.Get("Key", ManagementClientConstants.ServiceBusNamespace), param.Key)
 075                                XmlObjectConvertor.SerializeObject(param.Value)));
 76                    }
 77                }
 78
 079                return new XElement(
 080                        XName.Get("Action", ManagementClientConstants.ServiceBusNamespace),
 081                        new XAttribute(XName.Get("type", ManagementClientConstants.XmlSchemaInstanceNamespace), nameof(S
 082                        new XElement(XName.Get("SqlExpression", ManagementClientConstants.ServiceBusNamespace), sqlRuleA
 083                        parameterElement);
 84            }
 85            else
 86            {
 087                throw new NotImplementedException($"Rule action of type {action.GetType().Name} is not implemented");
 88            }
 89        }
 90    }
 91}