|
| 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 | +namespace Microsoft.Azure.Commands.Resources.Test |
| 16 | +{ |
| 17 | + using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation; |
| 18 | + using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkClient; |
| 19 | + using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels; |
| 20 | + using Microsoft.Azure.Management.ResourceManager; |
| 21 | + using Microsoft.Azure.Management.ResourceManager.Models; |
| 22 | + using Microsoft.Rest.Azure; |
| 23 | + using Microsoft.WindowsAzure.Commands.Common.Test.Mocks; |
| 24 | + using Microsoft.WindowsAzure.Commands.ScenarioTest; |
| 25 | + using Moq; |
| 26 | + using System; |
| 27 | + using System.Collections.Generic; |
| 28 | + using System.Management.Automation; |
| 29 | + using System.Threading; |
| 30 | + using System.Threading.Tasks; |
| 31 | + using WindowsAzure.Commands.Test.Utilities.Common; |
| 32 | + using Xunit; |
| 33 | + using Xunit.Abstractions; |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Tests the Azure Provider Feature cmdlets |
| 37 | + /// </summary> |
| 38 | + public class UnregisterProviderFeatureCmdletTests : RMTestBase |
| 39 | + { |
| 40 | + /// <summary> |
| 41 | + /// An instance of the cmdlet |
| 42 | + /// </summary> |
| 43 | + private readonly UnregisterAzureProviderFeatureCmdlet cmdlet; |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// A mock of the client |
| 47 | + /// </summary> |
| 48 | + private readonly Mock<IFeaturesOperations> featureOperationsMock; |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// A mock of the command runtime |
| 52 | + /// </summary> |
| 53 | + private readonly Mock<ICommandRuntime> commandRuntimeMock; |
| 54 | + private MockCommandRuntime mockRuntime; |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Initializes a new instance of the <see cref="UnregisterProviderFeatureCmdletTests"/> class. |
| 58 | + /// </summary> |
| 59 | + public UnregisterProviderFeatureCmdletTests(ITestOutputHelper output) |
| 60 | + { |
| 61 | + this.featureOperationsMock = new Mock<IFeaturesOperations>(); |
| 62 | + var featureClient = new Mock<IFeatureClient>(); |
| 63 | + |
| 64 | + featureClient |
| 65 | + .SetupGet(client => client.Features) |
| 66 | + .Returns(() => this.featureOperationsMock.Object); |
| 67 | + |
| 68 | + this.commandRuntimeMock = new Mock<ICommandRuntime>(); |
| 69 | + |
| 70 | + this.commandRuntimeMock |
| 71 | + .Setup(m => m.ShouldProcess(It.IsAny<string>(), It.IsAny<string>())) |
| 72 | + .Returns(() => true); |
| 73 | + |
| 74 | + this.cmdlet = new UnregisterAzureProviderFeatureCmdlet() |
| 75 | + { |
| 76 | + ProviderFeatureClient = new ProviderFeatureClient |
| 77 | + { |
| 78 | + FeaturesManagementClient = featureClient.Object |
| 79 | + } |
| 80 | + }; |
| 81 | + PSCmdletExtensions.SetCommandRuntimeMock(cmdlet, commandRuntimeMock.Object); |
| 82 | + mockRuntime = new MockCommandRuntime(); |
| 83 | + commandRuntimeMock.Setup(f => f.Host).Returns(mockRuntime.Host); |
| 84 | + } |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// Validates all Unregister-AzureRmResourceProvider scenarios |
| 88 | + /// </summary> |
| 89 | + [Fact] |
| 90 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 91 | + public void UnregisterResourceProviderFeatureTests() |
| 92 | + { |
| 93 | + const string ProviderName = "Providers.Test"; |
| 94 | + const string FeatureName = "Feature1"; |
| 95 | + |
| 96 | + var registeredFeature = new FeatureResult |
| 97 | + { |
| 98 | + Id = "featureId1", |
| 99 | + Name = ProviderName + "/" + FeatureName, |
| 100 | + Properties = new FeatureProperties |
| 101 | + { |
| 102 | + State = ProviderFeatureClient.RegisteredStateName, |
| 103 | + }, |
| 104 | + Type = "Microsoft.Features/feature" |
| 105 | + }; |
| 106 | + |
| 107 | + this.featureOperationsMock |
| 108 | + .Setup(client => client.UnregisterWithHttpMessagesAsync(It.IsAny<string>(), It.IsAny<string>(), null, It.IsAny<CancellationToken>())) |
| 109 | + .Callback((string providerName, string featureName, Dictionary<string, List<string>> customHeaders, CancellationToken ignored) => |
| 110 | + { |
| 111 | + Assert.Equal(ProviderName, providerName, StringComparer.OrdinalIgnoreCase); |
| 112 | + Assert.Equal(FeatureName, featureName, StringComparer.OrdinalIgnoreCase); |
| 113 | + }) |
| 114 | + .Returns(() => Task.FromResult(new AzureOperationResponse<FeatureResult>() |
| 115 | + { |
| 116 | + Body = registeredFeature |
| 117 | + })); |
| 118 | + |
| 119 | + this.cmdlet.ProviderNamespace = ProviderName; |
| 120 | + this.cmdlet.FeatureName = FeatureName; |
| 121 | + |
| 122 | + this.commandRuntimeMock |
| 123 | + .Setup(m => m.WriteObject(It.IsAny<object>())) |
| 124 | + .Callback((object obj) => |
| 125 | + { |
| 126 | + Assert.IsType<PSProviderFeature>(obj); |
| 127 | + var feature = (PSProviderFeature)obj; |
| 128 | + Assert.Equal(ProviderName, feature.ProviderName, StringComparer.OrdinalIgnoreCase); |
| 129 | + Assert.Equal(FeatureName, feature.FeatureName, StringComparer.OrdinalIgnoreCase); |
| 130 | + }); |
| 131 | + |
| 132 | + this.cmdlet.ExecuteCmdlet(); |
| 133 | + |
| 134 | + this.VerifyCallPatternAndReset(succeeded: true); |
| 135 | + } |
| 136 | + |
| 137 | + /// <summary> |
| 138 | + /// Verifies the right call patterns are made |
| 139 | + /// </summary> |
| 140 | + private void VerifyCallPatternAndReset(bool succeeded) |
| 141 | + { |
| 142 | + this.featureOperationsMock.Verify(f => f.UnregisterWithHttpMessagesAsync(It.IsAny<string>(), |
| 143 | + It.IsAny<string>(), null, It.IsAny<CancellationToken>()), Times.Once()); |
| 144 | + this.commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny<object>()), succeeded ? Times.Once() : Times.Never()); |
| 145 | + |
| 146 | + this.featureOperationsMock.ResetCalls(); |
| 147 | + this.commandRuntimeMock.ResetCalls(); |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments