< Summary

Class:Azure.Messaging.ServiceBus.Amqp.AmqpMessageExtensions
Assembly:Azure.Messaging.ServiceBus
File(s):C:\Git\azure-sdk-for-net\sdk\servicebus\Azure.Messaging.ServiceBus\src\Amqp\AmqpMessageExtensions.cs
Covered lines:23
Uncovered lines:12
Coverable lines:35
Total lines:99
Line coverage:65.7% (23 of 35)
Covered branches:21
Total branches:30
Branch coverage:70% (21 of 30)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
ToAmqpMessage(...)-100%100%
GetByteArray(...)-30.77%62.5%
GetDataViaDataBody()-100%83.33%
ConvertAndFlattenData(...)-75%50%
CreateAmqpDataMessage(...)-100%50%
ToServiceBusReceivedMessage(...)-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\servicebus\Azure.Messaging.ServiceBus\src\Amqp\AmqpMessageExtensions.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.Linq;
 7using Azure.Core;
 8using Microsoft.Azure.Amqp;
 9using Microsoft.Azure.Amqp.Framing;
 10
 11namespace Azure.Messaging.ServiceBus.Amqp
 12{
 13    internal static class AmqpMessageExtensions
 14    {
 15        public static AmqpMessage ToAmqpMessage(this ServiceBusMessage message) =>
 7416            AmqpMessage.Create(new Data { Value = new ArraySegment<byte>(message.Body.Bytes.IsEmpty ? Array.Empty<byte>(
 17
 18        private static byte[] GetByteArray(this Data data)
 19        {
 420            switch (data.Value)
 21            {
 22                case byte[] byteArray:
 223                    return byteArray;
 224                case ArraySegment<byte> arraySegment when arraySegment.Count == arraySegment.Array?.Length:
 225                    return arraySegment.Array;
 26                case ArraySegment<byte> arraySegment:
 027                    var bytes = new byte[arraySegment.Count];
 028                    Array.ConstrainedCopy(
 029                        sourceArray: arraySegment.Array,
 030                        sourceIndex: arraySegment.Offset,
 031                        destinationArray: bytes,
 032                        destinationIndex: 0,
 033                        length: arraySegment.Count);
 034                    return bytes;
 35                default:
 036                    return null;
 37            }
 38        }
 39
 40        private static IEnumerable<byte[]> GetDataViaDataBody(AmqpMessage message)
 41        {
 1642            foreach (Data data in (message.DataBody ?? Enumerable.Empty<Data>()))
 43            {
 444                byte[] bytes = data.GetByteArray();
 445                if (bytes != null)
 46                {
 447                    yield return bytes;
 48                }
 49            }
 450        }
 51
 52        // Returns via the out parameter the flattened collection of bytes.
 53        // A majority of the time, data will only contain 1 element.
 54        // The method is optimized for this situation to return the pre-existing array.
 55        private static byte[] ConvertAndFlattenData(IEnumerable<byte[]> data)
 56        {
 457            byte[] flattened = null;
 458            List<byte> flattenedList = null;
 459            var dataCount = 0;
 1660            foreach (byte[] byteArray in data)
 61            {
 62                // Only the first array is needed if it is the only valid array.
 63                // This should be the case 99% of the time.
 464                if (dataCount == 0)
 65                {
 466                    flattened = byteArray;
 67                }
 68                else
 69                {
 70                    // We defer creating this list since this case will rarely happen.
 071                    flattenedList ??= new List<byte>(flattened!);
 072                    flattenedList.AddRange(byteArray);
 73                }
 74
 475                dataCount++;
 76            }
 77
 478            if (dataCount > 1)
 79            {
 080                flattened = flattenedList!.ToArray();
 81            }
 82
 483            return flattened;
 84        }
 85
 86        private static ServiceBusMessage CreateAmqpDataMessage(IEnumerable<byte[]> data) =>
 487            new ServiceBusMessage(BinaryData.FromMemory(ConvertAndFlattenData(data) ?? ReadOnlyMemory<byte>.Empty));
 88
 89        public static ServiceBusReceivedMessage ToServiceBusReceivedMessage(this AmqpMessage message)
 90        {
 891            if ((message.BodyType & SectionFlag.Data) != 0 && message.DataBody != null)
 92            {
 493                return new ServiceBusReceivedMessage { SentMessage = CreateAmqpDataMessage(GetDataViaDataBody(message)) 
 94            }
 95
 496            return new ServiceBusReceivedMessage();
 97        }
 98    }
 99}