public void SetNSGOnNicByNameNoPreviousNSG()
        {
            // Setup
            cmdlet = new SetAzureNetworkSecurityGroupAssociation
            {
                Name                 = NetworkSecurityGroupName,
                ServiceName          = ServiceName,
                RoleName             = RoleName,
                NetworkInterfaceName = NetworkInterfaceName,
                CommandRuntime       = mockCommandRuntime,
                Client               = this.client
            };
            cmdlet.SetParameterSet(SetAzureNetworkSecurityGroupAssociation.AddNetworkSecurityGroupAssociationToPaaSRole);

            // Action
            cmdlet.ExecuteCmdlet();

            // Assert

            #region Never called
            networkingClientMock.Verify(
                c => c.NetworkSecurityGroups.AddToRoleAsync(
                    It.IsAny <string>(),
                    It.IsAny <string>(),
                    It.IsAny <string>(),
                    It.IsAny <NetworkSecurityGroupAddAssociationParameters>(),
                    It.IsAny <CancellationToken>()),
                Times.Never);

            networkingClientMock.Verify(
                c => c.NetworkSecurityGroups.AddToSubnetAsync(
                    It.IsAny <string>(),
                    It.IsAny <string>(),
                    It.IsAny <NetworkSecurityGroupAddAssociationParameters>(),
                    It.IsAny <CancellationToken>()),
                Times.Never);
            #endregion

            computeClientMock.Verify(
                c => c.Deployments.GetBySlotAsync(
                    ServiceName,
                    DeploymentSlot.Production,
                    It.IsAny <CancellationToken>()),
                Times.Once);

            networkingClientMock.Verify(
                c => c.NetworkSecurityGroups.AddToNetworkInterfaceAsync(
                    ServiceName,
                    DeploymentName,
                    RoleName,
                    NetworkInterfaceName,
                    It.Is <NetworkSecurityGroupAddAssociationParameters>(
                        parameters => string.Equals(NetworkSecurityGroupName, parameters.Name)),
                    It.IsAny <CancellationToken>()),
                Times.Once);

            Assert.Equal(0, mockCommandRuntime.OutputPipeline.Count);
        }
        public void SetNSGOnNicByNameWithPreviousNSG()
        {
            // Setup

            this.networkingClientMock
            .Setup(c =>
                   c.NetworkSecurityGroups.AddToNetworkInterfaceAsync(
                       ServiceName,
                       DeploymentName,
                       RoleName,
                       NetworkInterfaceName,
                       It.IsAny <NetworkSecurityGroupAddAssociationParameters>(),
                       It.IsAny <CancellationToken>()))
            .ThrowsAsync(AlreadySetException);

            this.networkingClientMock
            .Setup(c =>
                   c.NetworkSecurityGroups.RemoveFromNetworkInterfaceAsync(
                       ServiceName,
                       DeploymentName,
                       RoleName,
                       NetworkInterfaceName,
                       CurrentNetworkSecurityGroup,
                       It.IsAny <CancellationToken>()))
            .Returns(() =>
            {
                // after we remove the current association, Set shouldn't throw an exception any more
                this.networkingClientMock
                .Setup(c =>
                       c.NetworkSecurityGroups.AddToNetworkInterfaceAsync(
                           ServiceName,
                           DeploymentName,
                           RoleName,
                           NetworkInterfaceName,
                           It.IsAny <NetworkSecurityGroupAddAssociationParameters>(),
                           It.IsAny <CancellationToken>()))
                .Returns(Task.Factory.StartNew(() => new Azure.OperationStatusResponse()));

                return(Task.Factory.StartNew(() => new Azure.OperationStatusResponse()));
            });

            cmdlet = new SetAzureNetworkSecurityGroupAssociation
            {
                ServiceName          = ServiceName,
                RoleName             = RoleName,
                NetworkInterfaceName = NetworkInterfaceName,
                Name           = NetworkSecurityGroupName,
                CommandRuntime = mockCommandRuntime,
                Client         = this.client
            };
            cmdlet.SetParameterSet(SetAzureNetworkSecurityGroupAssociation.AddNetworkSecurityGroupAssociationToPaaSRole);

            // Action
            cmdlet.ExecuteCmdlet();

            // Assert
            #region Never called
            networkingClientMock.Verify(
                c => c.NetworkSecurityGroups.AddToSubnetAsync(
                    It.IsAny <string>(),
                    It.IsAny <string>(),
                    It.IsAny <NetworkSecurityGroupAddAssociationParameters>(),
                    It.IsAny <CancellationToken>()),
                Times.Never);

            networkingClientMock.Verify(
                c => c.NetworkSecurityGroups.AddToRoleAsync(
                    It.IsAny <string>(),
                    It.IsAny <string>(),
                    It.IsAny <string>(),
                    It.IsAny <NetworkSecurityGroupAddAssociationParameters>(),
                    It.IsAny <CancellationToken>()),
                Times.Never);
            #endregion

            networkingClientMock.Verify(
                c => c.NetworkSecurityGroups.AddToNetworkInterfaceAsync(
                    cmdlet.ServiceName,
                    DeploymentName,
                    RoleName,
                    NetworkInterfaceName,
                    It.Is <NetworkSecurityGroupAddAssociationParameters>(
                        parameters => string.Equals(NetworkSecurityGroupName, parameters.Name)),
                    It.IsAny <CancellationToken>()),
                Times.Exactly(2));

            networkingClientMock.Verify(
                c => c.NetworkSecurityGroups.RemoveFromNetworkInterfaceAsync(
                    cmdlet.ServiceName,
                    DeploymentName,
                    RoleName,
                    NetworkInterfaceName,
                    CurrentNetworkSecurityGroup,
                    It.IsAny <CancellationToken>()),
                Times.Once);

            networkingClientMock.Verify(
                c => c.NetworkSecurityGroups.GetForNetworkInterfaceAsync(
                    cmdlet.ServiceName,
                    DeploymentName,
                    RoleName,
                    NetworkInterfaceName,
                    It.IsAny <CancellationToken>()),
                Times.Once);

            computeClientMock.Verify(
                c => c.Deployments.GetBySlotAsync(
                    It.IsAny <string>(),
                    It.IsAny <DeploymentSlot>(),
                    It.IsAny <CancellationToken>()),
                Times.Once);

            Assert.Equal(0, mockCommandRuntime.OutputPipeline.Count);
        }