|
| 1 | +// ---------------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// Copyright Microsoft Corporation |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | +// ---------------------------------------------------------------------------------- |
| 14 | + |
| 15 | +using System; |
| 16 | +using System.Collections.Generic; |
| 17 | +using System.Linq; |
| 18 | +using System.Net; |
| 19 | +using System.Net.Http; |
| 20 | +using System.Text; |
| 21 | +using System.Threading; |
| 22 | +using System.Threading.Tasks; |
| 23 | +using Hyak.Common.TransientFaultHandling; |
| 24 | +using Microsoft.Azure.Management.Resources; |
| 25 | +using Microsoft.Azure.Management.Resources.Models; |
| 26 | +using Microsoft.WindowsAzure.Commands.Utilities.Common; |
| 27 | +using Moq; |
| 28 | + |
| 29 | +namespace Microsoft.Azure.Commands.ScenarioTest.Mocks |
| 30 | +{ |
| 31 | + public class MockDeploymentClientFactory |
| 32 | + { |
| 33 | + public static ResourceManagementClient GetResourceClient(ResourceManagementClient wrapped) |
| 34 | + { |
| 35 | + var deployment = new Mock<IDeploymentOperations>(); |
| 36 | + deployment.Setup(d => d.ValidateAsync(It.IsAny<string>(), It.IsAny<string>(), |
| 37 | + It.IsAny<Deployment>(), It.IsAny<CancellationToken>())).Returns( |
| 38 | + (string resourceGroupName, string deploymentName, Deployment props, CancellationToken token) => |
| 39 | + Task.FromResult(new DeploymentValidateResponse |
| 40 | + { |
| 41 | + Error = null, |
| 42 | + IsValid = true, |
| 43 | + Properties = new DeploymentPropertiesExtended |
| 44 | + { |
| 45 | + CorrelationId = Guid.NewGuid().ToString(), |
| 46 | + DebugSetting = props.Properties.DebugSetting, |
| 47 | + DebugSettingResponse = props.Properties.DebugSetting, |
| 48 | + Dependencies = new List<Dependency>(), |
| 49 | + Duration = TimeSpan.FromSeconds(1), |
| 50 | + Mode = props.Properties.Mode, |
| 51 | + Outputs = string.Empty, |
| 52 | + Parameters = props.Properties.Parameters, |
| 53 | + ParametersLink = props.Properties.ParametersLink, |
| 54 | + Providers = new List<Provider> |
| 55 | + { |
| 56 | + new Provider |
| 57 | + { |
| 58 | + Id = Guid.NewGuid().ToString(), |
| 59 | + Namespace = "Microsoft.Compute", |
| 60 | + RegistrationState = "Registered", |
| 61 | + RequestId = Guid.NewGuid().ToString(), |
| 62 | + ResourceTypes = null, |
| 63 | + StatusCode = HttpStatusCode.OK |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + }, |
| 68 | + RequestId = Guid.NewGuid().ToString(), |
| 69 | + StatusCode = HttpStatusCode.OK |
| 70 | + })); |
| 71 | + deployment.Setup( |
| 72 | + d => d.BeginDeletingAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>())) |
| 73 | + .Returns( |
| 74 | + (string resourceGroupName, string deploymentName, CancellationToken token) => |
| 75 | + wrapped.Deployments.BeginDeletingAsync(resourceGroupName, deploymentName, token)); |
| 76 | + deployment.Setup( |
| 77 | + d => d.CancelAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>())) |
| 78 | + .Returns( |
| 79 | + (string resourceGroupName, string deploymentName, CancellationToken token) => |
| 80 | + wrapped.Deployments.CancelAsync(resourceGroupName, deploymentName, token)); |
| 81 | + deployment.Setup( |
| 82 | + d => d.CheckExistenceAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>())) |
| 83 | + .Returns( |
| 84 | + (string resourceGroupName, string deploymentName, CancellationToken token) => |
| 85 | + wrapped.Deployments.CheckExistenceAsync(resourceGroupName, deploymentName, token)); |
| 86 | + deployment.Setup( |
| 87 | + d => |
| 88 | + d.CreateOrUpdateAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Deployment>(), |
| 89 | + It.IsAny<CancellationToken>())) |
| 90 | + .Returns( |
| 91 | + (string resourceGroupName, string deploymentName, Deployment depl, CancellationToken token) => |
| 92 | + wrapped.Deployments.CreateOrUpdateAsync(resourceGroupName, deploymentName, depl, token)); |
| 93 | + deployment.Setup( |
| 94 | + d => d.DeleteAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>())) |
| 95 | + .Returns( |
| 96 | + (string resourceGroupName, string deploymentName, CancellationToken token) => |
| 97 | + wrapped.Deployments.DeleteAsync(resourceGroupName, deploymentName, token)); |
| 98 | + deployment.Setup( |
| 99 | + d => d.GetAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>())) |
| 100 | + .Returns( |
| 101 | + (string resourceGroupName, string deploymentName, CancellationToken token) => |
| 102 | + wrapped.Deployments.GetAsync(resourceGroupName, deploymentName, token)); |
| 103 | + deployment.Setup( |
| 104 | + d => |
| 105 | + d.ListAsync(It.IsAny<string>(), It.IsAny<DeploymentListParameters>(), |
| 106 | + It.IsAny<CancellationToken>())) |
| 107 | + .Returns( |
| 108 | + (string resourceGroupName, DeploymentListParameters list, CancellationToken token) => |
| 109 | + wrapped.Deployments.ListAsync(resourceGroupName, list, token)); |
| 110 | + deployment.Setup( |
| 111 | + d => d.ListNextAsync(It.IsAny<string>(), It.IsAny<CancellationToken>())) |
| 112 | + .Returns( |
| 113 | + (string nextLink, CancellationToken token) => |
| 114 | + wrapped.Deployments.ListNextAsync(nextLink, token)); |
| 115 | + var mockResource = new Mock<ResourceManagementClient>(); |
| 116 | + mockResource.SetupGet(r => r.Deployments).Returns(deployment.Object); |
| 117 | + mockResource.SetupGet(r => r.Resources).Returns(wrapped.Resources); |
| 118 | + mockResource.SetupGet(r => r.DeploymentOperations).Returns(wrapped.DeploymentOperations); |
| 119 | + mockResource.SetupGet(r => r.ProviderOperationsMetadata).Returns(wrapped.ProviderOperationsMetadata); |
| 120 | + mockResource.SetupGet(r => r.Providers).Returns(wrapped.Providers); |
| 121 | + mockResource.SetupGet(r => r.ResourceGroups).Returns(wrapped.ResourceGroups); |
| 122 | + mockResource.SetupGet(r => r.ResourceProviderOperationDetails) |
| 123 | + .Returns(wrapped.ResourceProviderOperationDetails); |
| 124 | + mockResource.SetupGet(r => r.Tags).Returns(wrapped.Tags); |
| 125 | + |
| 126 | + return mockResource.Object; |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments