< Summary

Class:Azure.Storage.Queues.Specialized.Models.EncryptedMessageSerializer
Assembly:Azure.Storage.Queues
File(s):C:\Git\azure-sdk-for-net\sdk\storage\Azure.Storage.Queues\src\Models\EncryptedMessageSerializer.cs
Covered lines:36
Uncovered lines:1
Coverable lines:37
Total lines:107
Line coverage:97.2% (36 of 37)
Covered branches:8
Total branches:10
Branch coverage:80% (8 of 10)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
Serialize(...)-100%100%
SerializeEncryptedMessage(...)-100%100%
WriteEncryptedMessage(...)-100%100%
TryDeserialize(...)-100%100%
Deserialize(...)-100%100%
DeserializeEncryptedMessage(...)-100%100%
ReadEncryptionData(...)-83.33%66.67%
ReadPropertyValue(...)-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\storage\Azure.Storage.Queues\src\Models\EncryptedMessageSerializer.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Text;
 6using System.Text.Json;
 7using Azure.Storage.Cryptography.Models;
 8
 9namespace Azure.Storage.Queues.Specialized.Models
 10{
 11    internal static class EncryptedMessageSerializer
 12    {
 13        private const string EncryptedMessage_EncryptedMessageTextName = "EncryptedMessageContents";
 14
 15        #region Serialize
 16        public static string Serialize(EncryptedMessage data)
 17        {
 618            return Encoding.UTF8.GetString(SerializeEncryptedMessage(data).ToArray());
 19        }
 20
 21        public static ReadOnlyMemory<byte> SerializeEncryptedMessage(EncryptedMessage message)
 22        {
 623            var writer = new Core.ArrayBufferWriter<byte>();
 624            using var json = new Utf8JsonWriter(writer);
 25
 626            json.WriteStartObject();
 627            WriteEncryptedMessage(json, message);
 628            json.WriteEndObject();
 29
 630            json.Flush();
 631            return writer.WrittenMemory;
 632        }
 33
 34        public static void WriteEncryptedMessage(Utf8JsonWriter json, EncryptedMessage message)
 35        {
 636            json.WriteString(EncryptedMessage_EncryptedMessageTextName, message.EncryptedMessageText);
 37
 638            json.WriteStartObject(nameof(message.EncryptionData));
 639            EncryptionDataSerializer.WriteEncryptionData(json, message.EncryptionData);
 640            json.WriteEndObject();
 641        }
 42        #endregion
 43
 44        #region Deserialize
 45        public static bool TryDeserialize(string serializedData, out EncryptedMessage encryptedMessage)
 46        {
 47            try
 48            {
 1649                encryptedMessage = Deserialize(serializedData);
 250                return true;
 51            }
 52            // JsonException does not actually cover everything. InvalidOperationException can be thrown
 53            // on some string inputs, as we can't assume input is even JSON.
 1454            catch (Exception)
 55            {
 1456                encryptedMessage = default;
 1457                return false;
 58            }
 1659        }
 60
 61        public static EncryptedMessage Deserialize(string serializedData)
 62        {
 1863            var reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(serializedData));
 1864            return DeserializeEncryptedMessage(ref reader);
 65        }
 66
 67        public static EncryptedMessage DeserializeEncryptedMessage(ref Utf8JsonReader reader)
 68        {
 1869            using JsonDocument json = JsonDocument.ParseValue(ref reader);
 1270            JsonElement root = json.RootElement;
 1271            return ReadEncryptionData(root);
 472        }
 73
 74        private static EncryptedMessage ReadEncryptionData(JsonElement root)
 75        {
 1276            var data = new EncryptedMessage();
 5277            foreach (var property in root.EnumerateObject())
 78            {
 1879                ReadPropertyValue(data, property);
 80            }
 81
 482            if (data.EncryptionData == default || data.EncryptedMessageText == default)
 83            {
 084                throw new FormatException($"Failed to find non-optional properties while deserializing `{typeof(Encrypte
 85            }
 86
 487            return data;
 88        }
 89
 90        private static void ReadPropertyValue(EncryptedMessage data, JsonProperty property)
 91        {
 1892            if (property.NameEquals(EncryptedMessage_EncryptedMessageTextName))
 93            {
 694                data.EncryptedMessageText = property.Value.GetString();
 95            }
 1296            else if (property.NameEquals(nameof(data.EncryptionData)))
 97            {
 698                data.EncryptionData = EncryptionDataSerializer.ReadEncryptionData(property.Value);
 99            }
 100            else
 101            {
 6102                throw new FormatException($"Failed to deserialize `{typeof(EncryptedMessage).FullName}`. Unrecognized pr
 103            }
 104        }
 105        #endregion
 106    }
 107}