< Summary

Class:Azure.Core.TestFramework.RecordSession
Assembly:Azure.Core.TestFramework
File(s):C:\Git\azure-sdk-for-net\sdk\core\Azure.Core.TestFramework\src\RecordSession.cs
Covered lines:38
Uncovered lines:15
Coverable lines:53
Total lines:138
Line coverage:71.6% (38 of 53)
Covered branches:14
Total branches:24
Branch coverage:58.3% (14 of 24)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
get_Entries()-100%100%
get_Variables()-100%100%
get_Names()-0%100%
Serialize(...)-100%100%
Deserialize(...)-64.29%64.29%
Record(...)-100%100%
Lookup(...)-100%100%
Sanitize(...)-100%100%
IsEquivalent(...)-33.33%16.67%
.ctor(...)-0%100%
Equals(...)-0%100%
GetHashCode(...)-0%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\core\Azure.Core.TestFramework\src\RecordSession.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 System.Text.Json;
 8
 9namespace Azure.Core.TestFramework
 10{
 11    public class RecordSession
 12    {
 135168813        public List<RecordEntry> Entries { get; } = new List<RecordEntry>();
 14
 5574715        public SortedDictionary<string, string> Variables { get; } = new SortedDictionary<string, string>(StringComparer
 16
 17        //Used only for deserializing track 1 session record files
 018        public Dictionary<string, Queue<string>> Names { get; set; } = new Dictionary<string, Queue<string>>();
 19
 20        public void Serialize(Utf8JsonWriter jsonWriter)
 21        {
 3022            jsonWriter.WriteStartObject();
 3023            jsonWriter.WriteStartArray(nameof(Entries));
 11624            foreach (RecordEntry record in Entries)
 25            {
 2826                record.Serialize(jsonWriter);
 27            }
 3028            jsonWriter.WriteEndArray();
 29
 3030            jsonWriter.WriteStartObject(nameof(Variables));
 17231            foreach (KeyValuePair<string, string> variable in Variables)
 32            {
 5633                jsonWriter.WriteString(variable.Key, variable.Value);
 34            }
 3035            jsonWriter.WriteEndObject();
 36
 3037            jsonWriter.WriteEndObject();
 3038        }
 39
 40        public static RecordSession Deserialize(JsonElement element)
 41        {
 913542            var session = new RecordSession();
 913543            if (element.TryGetProperty(nameof(Entries), out JsonElement property))
 44            {
 71565445                foreach (JsonElement item in property.EnumerateArray())
 46                {
 34869247                    session.Entries.Add(RecordEntry.Deserialize(item));
 48                }
 49            }
 50
 913551            if (element.TryGetProperty(nameof(Variables), out property))
 52            {
 5954253                foreach (JsonProperty item in property.EnumerateObject())
 54                {
 2063655                    session.Variables[item.Name] = item.Value.GetString();
 56                }
 57            }
 58
 913559            if (element.TryGetProperty(nameof(Names), out property))
 60            {
 061                foreach (JsonProperty item in property.EnumerateObject())
 62                {
 063                    var queue = new Queue<string>();
 064                    foreach (JsonElement subItem in item.Value.EnumerateArray())
 65                    {
 066                        queue.Enqueue(subItem.GetString());
 67                    }
 068                    session.Names[item.Name] = queue;
 69                }
 70            }
 913571            return session;
 72        }
 73
 74        public void Record(RecordEntry entry)
 75        {
 476            lock (Entries)
 77            {
 478                Entries.Add(entry);
 479            }
 480        }
 81
 82        public RecordEntry Lookup(RecordEntry requestEntry, RecordMatcher matcher, RecordedTestSanitizer sanitizer)
 83        {
 32637684            sanitizer.Sanitize(requestEntry);
 85
 32637686            lock (Entries)
 87            {
 32637688                RecordEntry entry = matcher.FindMatch(requestEntry, Entries);
 32632689                Entries.Remove(entry);
 32632690                return entry;
 91            }
 92
 32632693        }
 94
 95        public void Sanitize(RecordedTestSanitizer sanitizer)
 96        {
 697            lock (Entries)
 98            {
 699                sanitizer.Sanitize(this);
 6100            }
 6101        }
 102
 103        public bool IsEquivalent(RecordSession session, RecordMatcher matcher)
 104        {
 4105            if (session == null)
 106            {
 4107                return false;
 108            }
 109
 110            // The DateTimeOffsetNow variable is updated any time it's used so
 111            // we only care that both sessions use it or both sessions don't.
 0112            var now = TestRecording.DateTimeOffsetNowVariableKey;
 0113            return session.Variables.TryGetValue(now, out string _) == Variables.TryGetValue(now, out string _) &&
 0114                   session.Variables.Where(v => v.Key != now).SequenceEqual(Variables.Where(v => v.Key != now)) &&
 0115                   session.Entries.SequenceEqual(Entries, new EntryEquivalentComparer(matcher));
 116        }
 117
 118        private class EntryEquivalentComparer : IEqualityComparer<RecordEntry>
 119        {
 120            private readonly RecordMatcher _matcher;
 121
 0122            public EntryEquivalentComparer(RecordMatcher matcher)
 123            {
 0124                _matcher = matcher;
 0125            }
 126
 127            public bool Equals(RecordEntry x, RecordEntry y)
 128            {
 0129                return _matcher.IsEquivalentRecord(x, y);
 130            }
 131
 132            public int GetHashCode(RecordEntry obj)
 133            {
 0134                return obj.GetHashCode();
 135            }
 136        }
 137    }
 138}