< Summary

Class:Azure.Security.KeyVault.Administration.RestoreOperation
Assembly:Azure.Security.KeyVault.Administration
File(s):C:\Git\azure-sdk-for-net\sdk\keyvault\Azure.Security.KeyVault.Administration\src\RestoreOperation.cs
Covered lines:24
Uncovered lines:32
Coverable lines:56
Total lines:172
Line coverage:42.8% (24 of 56)
Covered branches:13
Total branches:26
Branch coverage:50% (13 of 26)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-0%100%
.ctor(...)-100%50%
.ctor(...)-0%0%
.ctor(...)-0%100%
get_StartTime()-0%0%
get_EndTime()-100%100%
get_Id()-100%100%
get_Value()-60%66.67%
get_HasCompleted()-100%100%
get_HasValue()-100%50%
GetRawResponse()-100%100%
UpdateStatus(...)-0%0%
UpdateStatusAsync()-100%100%
WaitForCompletionAsync(...)-100%50%
WaitForCompletionAsync(...)-0%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\keyvault\Azure.Security.KeyVault.Administration\src\RestoreOperation.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Threading;
 6using System.Threading.Tasks;
 7using Azure.Core;
 8using Azure.Security.KeyVault.Administration.Models;
 9
 10namespace Azure.Security.KeyVault.Administration
 11{
 12    /// <summary>
 13    /// A long-running operation for <see cref="KeyVaultBackupClient.StartRestore(Uri, string, string, CancellationToken
 14    /// </summary>
 15    public class RestoreOperation : Operation<Response>
 16    {
 17        /// <summary>
 18        /// The number of seconds recommended by the service to delay before checking on completion status.
 19        /// </summary>
 20        private readonly int? _retryAfterSeconds;
 21        private readonly KeyVaultBackupClient _client;
 22        private Response _response;
 23        private RestoreDetailsInternal _value;
 24        private readonly string _id;
 25
 26        /// <summary>
 27        /// Creates an instance of a RestoreOperation from a previously started operation. <see cref="UpdateStatus(Cance
 28        ///  <see cref="WaitForCompletionAsync(CancellationToken)"/>, or <see cref="WaitForCompletionAsync(TimeSpan, Can
 29        /// to re-populate the details of this operation.
 30        /// </summary>
 31        /// <param name="id">The <see cref="Id" /> from a previous <see cref="BackupOperation" />.</param>
 32        /// <param name="client">An instance of <see cref="KeyVaultBackupClient" />.</param>
 33        /// <exception cref="ArgumentNullException"><paramref name="id"/> or <paramref name="client"/> is null.</excepti
 034        public RestoreOperation(string id, KeyVaultBackupClient client)
 35        {
 036            Argument.AssertNotNull(id, nameof(id));
 037            Argument.AssertNotNull(client, nameof(client));
 38
 039            _client = client;
 040            _id = id;
 041        }
 42
 43        /// <summary>
 44        /// Initializes a new instance of a RestoreOperation.
 45        /// </summary>
 46        /// <param name="client">An instance of <see cref="KeyVaultBackupClient" />.</param>
 47        /// <param name="response">The <see cref="ResponseWithHeaders{T, THeaders}" /> returned from <see cref="KeyVault
 48        /// <exception cref="ArgumentNullException"><paramref name="client"/> or <paramref name="response"/> is null.</e
 449        internal RestoreOperation(KeyVaultBackupClient client, ResponseWithHeaders<ServiceFullRestoreOperationHeaders> r
 50        {
 451            Argument.AssertNotNull(client, nameof(client));
 452            Argument.AssertNotNull(response, nameof(response));
 53
 454            _id = response.Headers.JobId() ?? throw new InvalidOperationException("The response does not contain an Id")
 455            _client = client;
 456            _response = response.GetRawResponse();
 457            _retryAfterSeconds = response.Headers.RetryAfter;
 458        }
 59
 60        /// <summary>
 61        /// Initializes a new instance of a RestoreOperation.
 62        /// </summary>
 63        /// <param name="client">An instance of <see cref="KeyVaultBackupClient" />.</param>
 64        /// <param name="response">The <see cref="ResponseWithHeaders{T, THeaders}" /> returned from <see cref="KeyVault
 65        /// <exception cref="ArgumentNullException"><paramref name="client"/> or <paramref name="response"/> is null.</e
 066        internal RestoreOperation(KeyVaultBackupClient client, ResponseWithHeaders<ServiceSelectiveKeyRestoreOperationHe
 67        {
 068            Argument.AssertNotNull(client, nameof(client));
 069            Argument.AssertNotNull(response, nameof(response));
 70
 071            _id = response.Headers.JobId() ?? throw new InvalidOperationException("The response does not contain an Id")
 072            _client = client;
 073            _response = response.GetRawResponse();
 074            _retryAfterSeconds = response.Headers.RetryAfter;
 075        }
 76
 77        /// <summary>
 78        /// Initializes a new instance of a RestoreOperation for mocking purposes.
 79        /// </summary>
 80        /// <param name="value">The <see cref="RestoreDetailsInternal" /> that will be used to populate various properti
 81        /// <param name="response">The <see cref="Response" /> that will be returned from <see cref="GetRawResponse" />.
 82        /// <param name="client">An instance of <see cref="KeyVaultBackupClient" />.</param>
 83        /// <exception cref="ArgumentNullException"><paramref name="value"/> or <paramref name="response"/> or <paramref
 084        internal RestoreOperation(RestoreDetailsInternal value, Response response, KeyVaultBackupClient client)
 85        {
 086            Argument.AssertNotNull(value, nameof(value));
 087            Argument.AssertNotNull(response, nameof(response));
 088            Argument.AssertNotNull(client, nameof(client));
 89
 090            _client = client;
 091            _response = response;
 092            _value = value;
 093            _id = value.JobId;
 094        }
 95
 96        /// <summary>
 97        /// The start time of the restore operation.
 98        /// </summary>
 099        public DateTimeOffset? StartTime => _value?.StartTime;
 100
 101        /// <summary>
 102        /// The end time of the restore operation.
 103        /// </summary>
 36104        public DateTimeOffset? EndTime => _value?.EndTime;
 105
 106        /// <inheritdoc/>
 12107        public override string Id => _id;
 108
 109        /// <inheritdoc/>
 110        public override Response Value
 111        {
 112            get
 113            {
 114#pragma warning disable CA1065 // Do not raise exceptions in unexpected locations
 4115                if (!HasCompleted)
 116                {
 0117                    throw new InvalidOperationException("The operation is not complete.");
 118                }
 4119                if (EndTime.HasValue && _value.Error != null)
 120                {
 0121                    throw new RequestFailedException($"{_value.Error.Message}\nInnerError: {_value.Error.InnerError}\nCo
 122                }
 123#pragma warning restore CA1065 // Do not raise exceptions in unexpected locations
 4124                return _response;
 125            }
 126        }
 127
 128        /// <inheritdoc/>
 32129        public override bool HasCompleted => EndTime.HasValue;
 130
 131        /// <inheritdoc/>
 4132        public override bool HasValue => _response != null && _value?.Error == null && HasCompleted;
 133
 134        /// <inheritdoc/>
 16135        public override Response GetRawResponse() => _response;
 136
 137        /// <inheritdoc/>
 138        public override Response UpdateStatus(CancellationToken cancellationToken = default)
 139        {
 0140            if (!HasCompleted)
 141            {
 0142                Response<RestoreDetailsInternal> response = _client.GetRestoreDetails(Id, cancellationToken);
 0143                _value = response.Value;
 0144                _response = response.GetRawResponse();
 145            }
 146
 0147            return GetRawResponse();
 148        }
 149
 150        /// <inheritdoc/>
 151        public override async ValueTask<Response> UpdateStatusAsync(CancellationToken cancellationToken = default)
 152        {
 12153            if (!HasCompleted)
 154            {
 12155                Response<RestoreDetailsInternal> response = await _client.GetRestoreDetailsAsync(Id, cancellationToken).
 12156                _value = response.Value;
 12157                _response = response.GetRawResponse();
 158            }
 159
 12160            return GetRawResponse();
 12161        }
 162
 163        /// <inheritdoc/>
 164        public override ValueTask<Response<Response>> WaitForCompletionAsync(CancellationToken cancellationToken = defau
 4165            _retryAfterSeconds.HasValue ? this.DefaultWaitForCompletionAsync(TimeSpan.FromSeconds(_retryAfterSeconds.Val
 4166                this.DefaultWaitForCompletionAsync(cancellationToken);
 167
 168        /// <inheritdoc/>
 169        public override ValueTask<Response<Response>> WaitForCompletionAsync(TimeSpan pollingInterval, CancellationToken
 0170            this.DefaultWaitForCompletionAsync(pollingInterval, cancellationToken);
 171    }
 172}