From 6508ab0b6a71e702d9e2cf3f338243918d68f1ca Mon Sep 17 00:00:00 2001 From: Yalin Li Date: Tue, 6 Jul 2021 14:55:18 -0700 Subject: [PATCH] Add SubscriptionContainer tests (#22348) --- .../ResourcesCreateOrUpdateOperation.cs | 73 ------------- .../src/Generated/ResourcesUpdateOperation.cs | 73 ------------- .../src/Generated/SubscriptionContainer.cs | 38 ++++--- .../Scenario/SubscriptionContainerTests.cs | 16 ++- .../SubscriptionContainerTests/Get().json | 99 +++++++++++++++++ .../Get()Async.json | 100 ++++++++++++++++++ 6 files changed, 235 insertions(+), 164 deletions(-) delete mode 100644 sdk/resourcemanager/Azure.ResourceManager.Core/src/Generated/ResourcesCreateOrUpdateOperation.cs delete mode 100644 sdk/resourcemanager/Azure.ResourceManager.Core/src/Generated/ResourcesUpdateOperation.cs create mode 100644 sdk/resourcemanager/Azure.ResourceManager.Core/tests/SessionRecords/SubscriptionContainerTests/Get().json create mode 100644 sdk/resourcemanager/Azure.ResourceManager.Core/tests/SessionRecords/SubscriptionContainerTests/Get()Async.json diff --git a/sdk/resourcemanager/Azure.ResourceManager.Core/src/Generated/ResourcesCreateOrUpdateOperation.cs b/sdk/resourcemanager/Azure.ResourceManager.Core/src/Generated/ResourcesCreateOrUpdateOperation.cs deleted file mode 100644 index 9d2570183d132..0000000000000 --- a/sdk/resourcemanager/Azure.ResourceManager.Core/src/Generated/ResourcesCreateOrUpdateOperation.cs +++ /dev/null @@ -1,73 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -// - -#nullable disable - -using System; -using System.Text.Json; -using System.Threading; -using System.Threading.Tasks; -using Azure; -using Azure.Core; -using Azure.Core.Pipeline; - -namespace Azure.ResourceManager.Core -{ - /// Creates a resource. - public partial class ResourcesCreateOrUpdateOperation : Operation, IOperationSource - { - private readonly OperationOrResponseInternals _operation; - private readonly OperationsBase _parentOperation; - - /// Initializes a new instance of ResourcesCreateOrUpdateOperation for mocking. - protected ResourcesCreateOrUpdateOperation() - { - } - - internal ResourcesCreateOrUpdateOperation(OperationsBase parentOperation, ClientDiagnostics clientDiagnostics, HttpPipeline pipeline, Request request, Response response) - { - _operation = new OperationOrResponseInternals(this, clientDiagnostics, pipeline, request, response, OperationFinalStateVia.Location, "ResourcesCreateOrUpdateOperation"); - _parentOperation = parentOperation; - } - /// - public override string Id => ""; - - /// - public override GenericResource Value => _operation.Value; - - /// - public override bool HasCompleted => _operation.HasCompleted; - - /// - public override bool HasValue => _operation.HasValue; - - /// - public override Response GetRawResponse() => _operation.GetRawResponse(); - - /// - public override Response UpdateStatus(CancellationToken cancellationToken = default) => _operation.UpdateStatus(cancellationToken); - - /// - public override ValueTask UpdateStatusAsync(CancellationToken cancellationToken = default) => _operation.UpdateStatusAsync(cancellationToken); - - /// - public override ValueTask> WaitForCompletionAsync(CancellationToken cancellationToken = default) => _operation.WaitForCompletionAsync(cancellationToken); - - /// - public override ValueTask> WaitForCompletionAsync(TimeSpan pollingInterval, CancellationToken cancellationToken = default) => _operation.WaitForCompletionAsync(pollingInterval, cancellationToken); - - GenericResource IOperationSource.CreateResult(Response response, CancellationToken cancellationToken) - { - using var document = JsonDocument.Parse(response.ContentStream); - return new GenericResource(_parentOperation, GenericResourceData.DeserializeGenericResource(document.RootElement)); - } - - async ValueTask IOperationSource.CreateResultAsync(Response response, CancellationToken cancellationToken) - { - using var document = await JsonDocument.ParseAsync(response.ContentStream, default, cancellationToken).ConfigureAwait(false); - return new GenericResource(_parentOperation, GenericResourceData.DeserializeGenericResource(document.RootElement)); - } - } -} diff --git a/sdk/resourcemanager/Azure.ResourceManager.Core/src/Generated/ResourcesUpdateOperation.cs b/sdk/resourcemanager/Azure.ResourceManager.Core/src/Generated/ResourcesUpdateOperation.cs deleted file mode 100644 index 469b2138262c2..0000000000000 --- a/sdk/resourcemanager/Azure.ResourceManager.Core/src/Generated/ResourcesUpdateOperation.cs +++ /dev/null @@ -1,73 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -// - -#nullable disable - -using System; -using System.Text.Json; -using System.Threading; -using System.Threading.Tasks; -using Azure; -using Azure.Core; -using Azure.Core.Pipeline; - -namespace Azure.ResourceManager.Core -{ - /// Updates a resource. - public partial class ResourcesUpdateOperation : Operation, IOperationSource - { - private readonly OperationOrResponseInternals _operation; - private readonly OperationsBase _parentOperation; - - /// Initializes a new instance of ResourcesUpdateOperation for mocking. - protected ResourcesUpdateOperation() - { - } - - internal ResourcesUpdateOperation(OperationsBase parentOperation, ClientDiagnostics clientDiagnostics, HttpPipeline pipeline, Request request, Response response) - { - _operation = new OperationOrResponseInternals(this, clientDiagnostics, pipeline, request, response, OperationFinalStateVia.Location, "ResourcesUpdateOperation"); - _parentOperation = parentOperation; - } - /// - public override string Id => ""; - - /// - public override GenericResource Value => _operation.Value; - - /// - public override bool HasCompleted => _operation.HasCompleted; - - /// - public override bool HasValue => _operation.HasValue; - - /// - public override Response GetRawResponse() => _operation.GetRawResponse(); - - /// - public override Response UpdateStatus(CancellationToken cancellationToken = default) => _operation.UpdateStatus(cancellationToken); - - /// - public override ValueTask UpdateStatusAsync(CancellationToken cancellationToken = default) => _operation.UpdateStatusAsync(cancellationToken); - - /// - public override ValueTask> WaitForCompletionAsync(CancellationToken cancellationToken = default) => _operation.WaitForCompletionAsync(cancellationToken); - - /// - public override ValueTask> WaitForCompletionAsync(TimeSpan pollingInterval, CancellationToken cancellationToken = default) => _operation.WaitForCompletionAsync(pollingInterval, cancellationToken); - - GenericResource IOperationSource.CreateResult(Response response, CancellationToken cancellationToken) - { - using var document = JsonDocument.Parse(response.ContentStream); - return new GenericResource(_parentOperation, GenericResourceData.DeserializeGenericResource(document.RootElement)); - } - - async ValueTask IOperationSource.CreateResultAsync(Response response, CancellationToken cancellationToken) - { - using var document = await JsonDocument.ParseAsync(response.ContentStream, default, cancellationToken).ConfigureAwait(false); - return new GenericResource(_parentOperation, GenericResourceData.DeserializeGenericResource(document.RootElement)); - } - } -} diff --git a/sdk/resourcemanager/Azure.ResourceManager.Core/src/Generated/SubscriptionContainer.cs b/sdk/resourcemanager/Azure.ResourceManager.Core/src/Generated/SubscriptionContainer.cs index 6bb1bc63ed203..0ff4c6e1a6fd2 100644 --- a/sdk/resourcemanager/Azure.ResourceManager.Core/src/Generated/SubscriptionContainer.cs +++ b/sdk/resourcemanager/Azure.ResourceManager.Core/src/Generated/SubscriptionContainer.cs @@ -144,9 +144,18 @@ protected override void Validate(ResourceIdentifier identifier) /// subscriptionGuid cannot be null or a whitespace. public Response Get(string subscriptionGuid, CancellationToken cancellationToken = default) { - return new SubscriptionOperations( - new ClientContext(ClientOptions, Credential, BaseUri, Pipeline), - subscriptionGuid).Get(cancellationToken); + using var scope = Diagnostics.CreateScope("SubscriptionContainer.Get"); + scope.Start(); + try + { + var response = RestClient.Get(subscriptionGuid, cancellationToken); + return Response.FromValue(new Subscription(this, response.Value), response.GetRawResponse()); + } + catch (Exception e) + { + scope.Failed(e); + throw; + } } /// @@ -156,11 +165,20 @@ public Response Get(string subscriptionGuid, CancellationToken can /// A token to allow the caller to cancel the call to the service. The default value is . /// A that on completion returns a response with the operation for this subscription. /// subscriptionGuid cannot be null or a whitespace. - public virtual Task> GetAsync(string subscriptionGuid, CancellationToken cancellationToken = default) + public virtual async Task> GetAsync(string subscriptionGuid, CancellationToken cancellationToken = default) { - return new SubscriptionOperations( - new ClientContext(ClientOptions, Credential, BaseUri, Pipeline), - subscriptionGuid).GetAsync(cancellationToken); + using var scope = Diagnostics.CreateScope("SubscriptionContainer.Get"); + scope.Start(); + try + { + var response = await RestClient.GetAsync(subscriptionGuid, cancellationToken).ConfigureAwait(false); + return Response.FromValue(new Subscription(this, response.Value), response.GetRawResponse()); + } + catch (Exception e) + { + scope.Failed(e); + throw; + } } /// @@ -172,11 +190,5 @@ protected override ResourceOperationsBase Converter() - { - return s => new Subscription(new SubscriptionOperations(new ClientContext(ClientOptions, Credential, BaseUri, Pipeline), s.Id), s); - } } } diff --git a/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Scenario/SubscriptionContainerTests.cs b/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Scenario/SubscriptionContainerTests.cs index 1ffff268e8c43..aef5b79a0c5ad 100644 --- a/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Scenario/SubscriptionContainerTests.cs +++ b/sdk/resourcemanager/Azure.ResourceManager.Core/tests/Scenario/SubscriptionContainerTests.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading.Tasks; using Azure.Core.TestFramework; using NUnit.Framework; @@ -31,7 +28,7 @@ public async Task List() [RecordedTest] public async Task TryGet() { - var foo = await Client.GetSubscriptions().TryGetAsync("/subscriptions/"+ new Guid().ToString()).ConfigureAwait(false); + var foo = await Client.GetSubscriptions().TryGetAsync("/subscriptions/" + new Guid().ToString()).ConfigureAwait(false); Assert.IsNull(foo); string subscriptionId = Client.DefaultSubscription.Id.SubscriptionId; var subscription = await Client.GetSubscriptions().TryGetAsync("/subscriptions/" + subscriptionId).ConfigureAwait(false); @@ -41,7 +38,16 @@ public async Task TryGet() [TestCase] [RecordedTest] - public async Task DoesExist() + public async Task Get() + { + string subscriptionId = Client.DefaultSubscription.Id.SubscriptionId; + Subscription result = await Client.GetSubscriptions().GetAsync(subscriptionId).ConfigureAwait(false); + Assert.AreEqual(subscriptionId, result.Id.SubscriptionId); + } + + [TestCase] + [RecordedTest] + public async Task DoesExist() { var expectFalse = await Client.GetSubscriptions().DoesExistAsync("/subscriptions/" + new Guid().ToString()).ConfigureAwait(false); Assert.IsFalse(expectFalse); diff --git a/sdk/resourcemanager/Azure.ResourceManager.Core/tests/SessionRecords/SubscriptionContainerTests/Get().json b/sdk/resourcemanager/Azure.ResourceManager.Core/tests/SessionRecords/SubscriptionContainerTests/Get().json new file mode 100644 index 0000000000000..60bf8fa1ad4b2 --- /dev/null +++ b/sdk/resourcemanager/Azure.ResourceManager.Core/tests/SessionRecords/SubscriptionContainerTests/Get().json @@ -0,0 +1,99 @@ +{ + "Entries": [ + { + "RequestUri": "https://management.azure.com/subscriptions/db1ab6f0-4769-4b27-930e-01e2ef9c123c?api-version=2019-11-01", + "RequestMethod": "GET", + "RequestHeaders": { + "Accept": "application/json", + "Authorization": "Sanitized", + "User-Agent": "azsdk-net-ResourceManager.Core/1.0.0-alpha.20210706.1 (.NET Framework 4.8.4300.0; Microsoft Windows 10.0.19043 )", + "x-ms-client-request-id": "3e85fc12538b48dd9a8b265a4e852d47", + "x-ms-return-client-request-id": "true" + }, + "RequestBody": null, + "StatusCode": 200, + "ResponseHeaders": { + "Cache-Control": "no-cache", + "Content-Length": "450", + "Content-Type": "application/json; charset=utf-8", + "Date": "Tue, 06 Jul 2021 20:32:33 GMT", + "Expires": "-1", + "Pragma": "no-cache", + "Strict-Transport-Security": "max-age=31536000; includeSubDomains", + "X-Content-Type-Options": "nosniff", + "x-ms-correlation-request-id": "52feaa40-89e0-406c-99bc-bcc862ee002b", + "x-ms-ratelimit-remaining-subscription-reads": "11996", + "x-ms-request-id": "52feaa40-89e0-406c-99bc-bcc862ee002b", + "x-ms-routing-request-id": "WESTUS2:20210706T203233Z:52feaa40-89e0-406c-99bc-bcc862ee002b" + }, + "ResponseBody": { + "id": "/subscriptions/db1ab6f0-4769-4b27-930e-01e2ef9c123c", + "authorizationSource": "RoleBased", + "managedByTenants": [], + "tags": { + "tagKey1": "tagValue1", + "tagKey2": "tagValue2" + }, + "subscriptionId": "db1ab6f0-4769-4b27-930e-01e2ef9c123c", + "tenantId": "72f988bf-86f1-41af-91ab-2d7cd011db47", + "displayName": "Azure SDK sandbox", + "state": "Enabled", + "subscriptionPolicies": { + "locationPlacementId": "Internal_2014-09-01", + "quotaId": "Internal_2014-09-01", + "spendingLimit": "Off" + } + } + }, + { + "RequestUri": "https://management.azure.com/subscriptions/db1ab6f0-4769-4b27-930e-01e2ef9c123c?api-version=2019-11-01", + "RequestMethod": "GET", + "RequestHeaders": { + "Accept": "application/json", + "Authorization": "Sanitized", + "traceparent": "00-8b782cd7fb0c134582e38486a9413367-27454a08c81d0a4b-00", + "User-Agent": "azsdk-net-ResourceManager.Core/1.0.0-alpha.20210706.1 (.NET Framework 4.8.4300.0; Microsoft Windows 10.0.19043 )", + "x-ms-client-request-id": "35d84112dc513ac26fb053d476335d71", + "x-ms-return-client-request-id": "true" + }, + "RequestBody": null, + "StatusCode": 200, + "ResponseHeaders": { + "Cache-Control": "no-cache", + "Content-Length": "450", + "Content-Type": "application/json; charset=utf-8", + "Date": "Tue, 06 Jul 2021 20:32:33 GMT", + "Expires": "-1", + "Pragma": "no-cache", + "Strict-Transport-Security": "max-age=31536000; includeSubDomains", + "X-Content-Type-Options": "nosniff", + "x-ms-correlation-request-id": "3fe18db1-28e3-4fed-9d67-f209c7c00f6d", + "x-ms-ratelimit-remaining-subscription-reads": "11995", + "x-ms-request-id": "3fe18db1-28e3-4fed-9d67-f209c7c00f6d", + "x-ms-routing-request-id": "WESTUS2:20210706T203233Z:3fe18db1-28e3-4fed-9d67-f209c7c00f6d" + }, + "ResponseBody": { + "id": "/subscriptions/db1ab6f0-4769-4b27-930e-01e2ef9c123c", + "authorizationSource": "RoleBased", + "managedByTenants": [], + "tags": { + "tagKey1": "tagValue1", + "tagKey2": "tagValue2" + }, + "subscriptionId": "db1ab6f0-4769-4b27-930e-01e2ef9c123c", + "tenantId": "72f988bf-86f1-41af-91ab-2d7cd011db47", + "displayName": "Azure SDK sandbox", + "state": "Enabled", + "subscriptionPolicies": { + "locationPlacementId": "Internal_2014-09-01", + "quotaId": "Internal_2014-09-01", + "spendingLimit": "Off" + } + } + } + ], + "Variables": { + "RandomSeed": "1163362173", + "SUBSCRIPTION_ID": "db1ab6f0-4769-4b27-930e-01e2ef9c123c" + } +} \ No newline at end of file diff --git a/sdk/resourcemanager/Azure.ResourceManager.Core/tests/SessionRecords/SubscriptionContainerTests/Get()Async.json b/sdk/resourcemanager/Azure.ResourceManager.Core/tests/SessionRecords/SubscriptionContainerTests/Get()Async.json new file mode 100644 index 0000000000000..23e8a94738e8b --- /dev/null +++ b/sdk/resourcemanager/Azure.ResourceManager.Core/tests/SessionRecords/SubscriptionContainerTests/Get()Async.json @@ -0,0 +1,100 @@ +{ + "Entries": [ + { + "RequestUri": "https://management.azure.com/subscriptions/db1ab6f0-4769-4b27-930e-01e2ef9c123c?api-version=2019-11-01", + "RequestMethod": "GET", + "RequestHeaders": { + "Accept": "application/json", + "Authorization": "Sanitized", + "Request-Id": "|a07f6a94-4bec862981a82377.", + "User-Agent": "azsdk-net-ResourceManager.Core/1.0.0-alpha.20210706.1 (.NET Framework 4.8.4300.0; Microsoft Windows 10.0.19043 )", + "x-ms-client-request-id": "3e85fc12538b48dd9a8b265a4e852d47", + "x-ms-return-client-request-id": "true" + }, + "RequestBody": null, + "StatusCode": 200, + "ResponseHeaders": { + "Cache-Control": "no-cache", + "Content-Length": "450", + "Content-Type": "application/json; charset=utf-8", + "Date": "Tue, 06 Jul 2021 20:32:33 GMT", + "Expires": "-1", + "Pragma": "no-cache", + "Strict-Transport-Security": "max-age=31536000; includeSubDomains", + "X-Content-Type-Options": "nosniff", + "x-ms-correlation-request-id": "0c54e7dd-6472-4747-8f86-04c9fd0fa8f9", + "x-ms-ratelimit-remaining-subscription-reads": "11996", + "x-ms-request-id": "0c54e7dd-6472-4747-8f86-04c9fd0fa8f9", + "x-ms-routing-request-id": "WESTUS2:20210706T203233Z:0c54e7dd-6472-4747-8f86-04c9fd0fa8f9" + }, + "ResponseBody": { + "id": "/subscriptions/db1ab6f0-4769-4b27-930e-01e2ef9c123c", + "authorizationSource": "RoleBased", + "managedByTenants": [], + "tags": { + "tagKey1": "tagValue1", + "tagKey2": "tagValue2" + }, + "subscriptionId": "db1ab6f0-4769-4b27-930e-01e2ef9c123c", + "tenantId": "72f988bf-86f1-41af-91ab-2d7cd011db47", + "displayName": "Azure SDK sandbox", + "state": "Enabled", + "subscriptionPolicies": { + "locationPlacementId": "Internal_2014-09-01", + "quotaId": "Internal_2014-09-01", + "spendingLimit": "Off" + } + } + }, + { + "RequestUri": "https://management.azure.com/subscriptions/db1ab6f0-4769-4b27-930e-01e2ef9c123c?api-version=2019-11-01", + "RequestMethod": "GET", + "RequestHeaders": { + "Accept": "application/json", + "Authorization": "Sanitized", + "traceparent": "00-00f818ed75bea24f8ab2decf15c5f7b7-daae7cdc04674e40-00", + "User-Agent": "azsdk-net-ResourceManager.Core/1.0.0-alpha.20210706.1 (.NET Framework 4.8.4300.0; Microsoft Windows 10.0.19043 )", + "x-ms-client-request-id": "35d84112dc513ac26fb053d476335d71", + "x-ms-return-client-request-id": "true" + }, + "RequestBody": null, + "StatusCode": 200, + "ResponseHeaders": { + "Cache-Control": "no-cache", + "Content-Length": "450", + "Content-Type": "application/json; charset=utf-8", + "Date": "Tue, 06 Jul 2021 20:32:33 GMT", + "Expires": "-1", + "Pragma": "no-cache", + "Strict-Transport-Security": "max-age=31536000; includeSubDomains", + "X-Content-Type-Options": "nosniff", + "x-ms-correlation-request-id": "567e1055-d76b-4205-9b39-a945c56d87de", + "x-ms-ratelimit-remaining-subscription-reads": "11994", + "x-ms-request-id": "567e1055-d76b-4205-9b39-a945c56d87de", + "x-ms-routing-request-id": "WESTUS2:20210706T203233Z:567e1055-d76b-4205-9b39-a945c56d87de" + }, + "ResponseBody": { + "id": "/subscriptions/db1ab6f0-4769-4b27-930e-01e2ef9c123c", + "authorizationSource": "RoleBased", + "managedByTenants": [], + "tags": { + "tagKey1": "tagValue1", + "tagKey2": "tagValue2" + }, + "subscriptionId": "db1ab6f0-4769-4b27-930e-01e2ef9c123c", + "tenantId": "72f988bf-86f1-41af-91ab-2d7cd011db47", + "displayName": "Azure SDK sandbox", + "state": "Enabled", + "subscriptionPolicies": { + "locationPlacementId": "Internal_2014-09-01", + "quotaId": "Internal_2014-09-01", + "spendingLimit": "Off" + } + } + } + ], + "Variables": { + "RandomSeed": "1163362173", + "SUBSCRIPTION_ID": "db1ab6f0-4769-4b27-930e-01e2ef9c123c" + } +} \ No newline at end of file