|
| 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.Collections.Generic; |
| 16 | +using Microsoft.Rest.Azure; |
| 17 | +using Microsoft.WindowsAzure.Commands.ScenarioTest; |
| 18 | +using Moq; |
| 19 | +using System.Management.Automation; |
| 20 | +using System.Threading; |
| 21 | +using System.Threading.Tasks; |
| 22 | +using Xunit; |
| 23 | +using Microsoft.Azure.Commands.ScenarioTest; |
| 24 | +using Microsoft.Azure.Commands.Insights.ActivityLogAlert; |
| 25 | +using Microsoft.Azure.Management.Monitor.Management; |
| 26 | +using Microsoft.Azure.Management.Monitor.Management.Models; |
| 27 | + |
| 28 | +namespace Microsoft.Azure.Commands.Insights.Test.ActivityLogAlerts |
| 29 | +{ |
| 30 | + public class DisableAzureRmActivityLogAlertTests |
| 31 | + { |
| 32 | + private readonly DisableAzureRmActivityLogAlertCommand cmdlet; |
| 33 | + private readonly Mock<MonitorManagementClient> monitorClientMock; |
| 34 | + private readonly Mock<IActivityLogAlertsOperations> insightsOperationsMock; |
| 35 | + private Mock<ICommandRuntime> commandRuntimeMock; |
| 36 | + private AzureOperationResponse<ActivityLogAlertResource> response; |
| 37 | + private string resourceGroup; |
| 38 | + private string name; |
| 39 | + private ActivityLogAlertPatchBody body; |
| 40 | + |
| 41 | + public DisableAzureRmActivityLogAlertTests(Xunit.Abstractions.ITestOutputHelper output) |
| 42 | + { |
| 43 | + TestExecutionHelpers.SetUpSessionAndProfile(); |
| 44 | + //ServiceManagemenet.Common.Models.XunitTracingInterceptor.AddToContext(new ServiceManagemenet.Common.Models.XunitTracingInterceptor(output)); |
| 45 | + insightsOperationsMock = new Mock<IActivityLogAlertsOperations>(); |
| 46 | + monitorClientMock = new Mock<MonitorManagementClient>(); |
| 47 | + commandRuntimeMock = new Mock<ICommandRuntime>(); |
| 48 | + cmdlet = new DisableAzureRmActivityLogAlertCommand() |
| 49 | + { |
| 50 | + CommandRuntime = commandRuntimeMock.Object, |
| 51 | + MonitorManagementClient = monitorClientMock.Object |
| 52 | + }; |
| 53 | + |
| 54 | + response = new AzureOperationResponse<ActivityLogAlertResource>() |
| 55 | + { |
| 56 | + Body = ActivityLogAlertsUtilities.CreateActivityLogAlertResource(location: "westus", name: "alert1") |
| 57 | + }; |
| 58 | + |
| 59 | + insightsOperationsMock.Setup(f => f.UpdateWithHttpMessagesAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<ActivityLogAlertPatchBody>(), It.IsAny<Dictionary<string, List<string>>>(), It.IsAny<CancellationToken>())) |
| 60 | + .Returns(Task.FromResult<AzureOperationResponse<ActivityLogAlertResource>>(response)) |
| 61 | + .Callback((string r, string n, ActivityLogAlertPatchBody b, Dictionary<string, List<string>> headers, CancellationToken t) => |
| 62 | + { |
| 63 | + this.resourceGroup = r; |
| 64 | + this.name = n; |
| 65 | + this.body = b; |
| 66 | + }); |
| 67 | + |
| 68 | + monitorClientMock.SetupGet(f => f.ActivityLogAlerts).Returns(this.insightsOperationsMock.Object); |
| 69 | + |
| 70 | + // Setup Confirmation |
| 71 | + commandRuntimeMock.Setup(f => f.ShouldProcess(It.IsAny<string>())).Returns(true); |
| 72 | + commandRuntimeMock.Setup(f => f.ShouldProcess(It.IsAny<string>(), It.IsAny<string>())).Returns(true); |
| 73 | + commandRuntimeMock.Setup(f => f.ShouldProcess(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns(true); |
| 74 | + commandRuntimeMock.Setup(f => f.ShouldContinue(It.IsAny<string>(), It.IsAny<string>())).Returns(true); |
| 75 | + } |
| 76 | + |
| 77 | + [Fact] |
| 78 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 79 | + public void DisableActivityLogAlertCommandParametersProcessing() |
| 80 | + { |
| 81 | + cmdlet.ResourceGroupName = Utilities.ResourceGroup; |
| 82 | + cmdlet.Name = "alert1"; |
| 83 | + cmdlet.Tag = new Dictionary<string, string> { { "key2", "value2" } }; |
| 84 | + cmdlet.ExecuteCmdlet(); |
| 85 | + |
| 86 | + Assert.Equal(Utilities.ResourceGroup, this.resourceGroup); |
| 87 | + Assert.Equal("alert1", this.name); |
| 88 | + Assert.NotNull(this.body); |
| 89 | + Assert.False(this.body.Enabled); |
| 90 | + Assert.NotNull(this.body.Tags); |
| 91 | + |
| 92 | + cmdlet.Tag = null; |
| 93 | + cmdlet.ExecuteCmdlet(); |
| 94 | + |
| 95 | + Assert.NotNull(this.body); |
| 96 | + Assert.False(this.body.Enabled); |
| 97 | + Assert.Null(this.body.Tags); |
| 98 | + |
| 99 | + ActivityLogAlertResource resource = new ActivityLogAlertResource(location: "Global", scopes: null, condition: null, name: "alert1", actions: null, id: "//subscriptions/32323/resourcegroups/" + Utilities.ResourceGroup) |
| 100 | + { |
| 101 | + Enabled = true |
| 102 | + }; |
| 103 | + |
| 104 | + cmdlet.InputObject = new OutputClasses.PSActivityLogAlertResource(resource); |
| 105 | + cmdlet.ExecuteCmdlet(); |
| 106 | + |
| 107 | + Assert.NotNull(this.body); |
| 108 | + Assert.Equal(Utilities.ResourceGroup, this.resourceGroup); |
| 109 | + Assert.Equal("alert1", this.name); |
| 110 | + Assert.False(this.body.Enabled); |
| 111 | + Assert.Null(this.body.Tags); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments