< Summary

Class:Azure.Messaging.ServiceBus.Management.XmlObjectConvertor
Assembly:Azure.Messaging.ServiceBus
File(s):C:\Git\azure-sdk-for-net\sdk\servicebus\Azure.Messaging.ServiceBus\src\Management\Rules\XmlObjectConvertor.cs
Covered lines:35
Uncovered lines:12
Coverable lines:47
Total lines:107
Line coverage:74.4% (35 of 47)
Covered branches:38
Total branches:52
Branch coverage:73% (38 of 52)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
ParseValueObject(...)-70%65.79%
SerializeObject(...)-77.78%92.86%

File(s)

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

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Xml;
 6using System.Xml.Linq;
 7using Azure.Messaging.ServiceBus.Diagnostics;
 8
 9namespace Azure.Messaging.ServiceBus.Management
 10{
 11    internal class XmlObjectConvertor
 12    {
 13        internal static object ParseValueObject(XElement element)
 14        {
 11215            var serializedPrefix = element.GetPrefixOfNamespace(XNamespace.Get(ManagementClientConstants.SerializationNa
 11216            var type = element.Attribute(XName.Get("type", ManagementClientConstants.XmlSchemaInstanceNamespace)).Value;
 17
 11218            if (!string.IsNullOrEmpty(serializedPrefix))
 19            {
 1220                if (type.Substring(serializedPrefix.Length + 1) == "duration")
 21                {
 1222                    return XmlConvert.ToTimeSpan(element.Value);
 23                }
 24            }
 25
 10026            var prefix = element.GetPrefixOfNamespace(XNamespace.Get(ManagementClientConstants.XmlSchemaNamespace));
 10027            if (string.IsNullOrWhiteSpace(prefix))
 28            {
 029                return element.Value;
 30            }
 31
 10032            switch (type.Substring(prefix.Length + 1))
 33            {
 34                case "string":
 4035                    return element.Value;
 36                case "int":
 2037                    return XmlConvert.ToInt32(element.Value);
 38                case "long":
 1639                    return XmlConvert.ToInt64(element.Value);
 40                case "boolean":
 441                    return XmlConvert.ToBoolean(element.Value);
 42                case "double":
 443                    return XmlConvert.ToDouble(element.Value);
 44                case "dateTime":
 1645                    return XmlConvert.ToDateTime(element.Value, XmlDateTimeSerializationMode.Utc);
 46                case "duration":
 047                    return XmlConvert.ToTimeSpan(element.Value);
 48                default:
 049                    ServiceBusEventSource.Log.ManagementSerializationException(
 050                            $"{nameof(XmlObjectConvertor)}_{nameof(ParseValueObject)}",
 051                            element.ToString());
 052                    return element.Value;
 53            }
 54        }
 55
 56        internal static XElement SerializeObject(object value)
 57        {
 6458            var prefix = "l28";
 6459            string type = prefix + ':';
 6460            if (value is string)
 61            {
 2462                type += "string";
 63            }
 4064            else if (value is int)
 65            {
 1266                type += "int";
 67            }
 2868            else if (value is long)
 69            {
 870                type += "long";
 71            }
 2072            else if (value is bool)
 73            {
 474                type += "boolean";
 75            }
 1676            else if (value is double)
 77            {
 478                type += "double";
 79            }
 1280            else if (value is DateTime)
 81            {
 882                type += "dateTime";
 83            }
 484            else if (value is TimeSpan)
 85            {
 486                type += "duration";
 87            }
 88            else
 89            {
 090                var unknownType = value.GetType().Name;
 091                ServiceBusEventSource.Log.ManagementSerializationException(
 092                            $"{nameof(XmlObjectConvertor)}_{nameof(SerializeObject)}",
 093                            unknownType);
 94
 095                throw new ServiceBusException(false, "Object is not of supported type: " + unknownType + ". " +
 096                    "Only following types are supported through HTTP: string,int,long,bool,double,DateTime");
 97            }
 98
 6499            var element = new XElement(XName.Get("Value", ManagementClientConstants.ServiceBusNamespace),
 64100                new XAttribute(XName.Get("type", ManagementClientConstants.XmlSchemaInstanceNamespace), type),
 64101                new XAttribute(XNamespace.Xmlns + prefix, ManagementClientConstants.XmlSchemaNamespace),
 64102                value);
 103
 64104            return element;
 105        }
 106    }
 107}