/// <summary>
 /// Checks if a feature is registered with the current subscription
 /// </summary>
 /// <param name="feature">The feature</param>
 private bool IsFeatureRegistered(FeatureResponse feature)
 {
     return string.Equals(feature.Properties.State, ProviderFeatureClient.RegisteredStateName, StringComparison.InvariantCultureIgnoreCase);
 }
        public void GetProviderFeatureTests()
        {
            // setup return values
            const string Provider1Namespace = "Providers.Test1";
            const string Feature1Name = "feature1";

            const string Provider2Namespace = "Providers.Test2";
            const string Feature2Name = "feature2";

            var provider1RegisteredFeature = new FeatureResponse
            {
                Id = "featureId1",
                Name = Provider1Namespace + "/" + Feature1Name,
                Properties = new FeatureProperties
                {
                    State = ProviderFeatureClient.RegisteredStateName,
                },
                RequestId = "requestId",
                StatusCode = HttpStatusCode.OK,
                Type = "Microsoft.Features/feature"
            };

            var provider1UnregisteredFeature = new FeatureResponse
            {
                Id = "featureId1",
                Name = Provider1Namespace + "/" + Feature2Name,
                Properties = new FeatureProperties
                {
                    State = "Unregistered",
                },
                RequestId = "requestId",
                StatusCode = HttpStatusCode.OK,
                Type = "Microsoft.Features/feature"
            };

            var provider2UnregisteredFeature = new FeatureResponse
            {
                Id = "featureId2",
                Name = Provider2Namespace + "/" + Feature1Name,
                Properties = new FeatureProperties
                {
                    State = "Unregistered",
                },
                RequestId = "requestId",
                StatusCode = HttpStatusCode.OK,
                Type = "Microsoft.Features/feature"
            };

            var listResult = new FeatureOperationsListResult
            {
                Features = new[] { provider1RegisteredFeature, provider1UnregisteredFeature, provider2UnregisteredFeature },
                NextLink = null,
                RequestId = "requestId",
                StatusCode = HttpStatusCode.OK
            };

            this.featureOperationsMock
                .Setup(f => f.ListAllAsync(It.IsAny<CancellationToken>()))
                .Returns(() => Task.FromResult(listResult));

            // 1. List only registered features of providers
            this.commandRuntimeMock
                .Setup(m => m.WriteObject(It.IsAny<object>()))
                .Callback((object obj) =>
                {
                    Assert.IsType<PSProviderFeature[]>(obj);

                    var features = (PSProviderFeature[])obj;
                    Assert.Equal(1, features.Length);

                    var provider = features.Single();
                    Assert.Equal(Provider1Namespace, provider.ProviderName, StringComparer.InvariantCultureIgnoreCase);
                    Assert.Equal(Feature1Name, provider.FeatureName, StringComparer.InvariantCultureIgnoreCase);
                    Assert.Equal(ProviderFeatureClient.RegisteredStateName, provider.RegistrationState, StringComparer.InvariantCultureIgnoreCase);
                });

            this.cmdlet.ParameterSetOverride = GetAzureProviderFeatureCmdlet.ListAvailableParameterSet;

            this.cmdlet.ExecuteCmdlet();

            this.VerifyListAllCallPatternAndReset();

            // 2. List all features of all providers
            this.cmdlet.ListAvailable = true;

            this.commandRuntimeMock
              .Setup(m => m.WriteObject(It.IsAny<object>()))
              .Callback((object obj) =>
              {
                  Assert.IsType<PSProviderFeature[]>(obj);
                  var features = (PSProviderFeature[])obj;
                  Assert.Equal(listResult.Features.Count, features.Length);
              });

            this.cmdlet.ExecuteCmdlet();

            this.VerifyListAllCallPatternAndReset();

            // 3.a. List only registered features of a particular provider - and they exist
            string providerOfChoice = Provider1Namespace;
            this.cmdlet.ListAvailable = false;
            this.cmdlet.ProviderNamespace = providerOfChoice;
            listResult.Features = new[] { provider1RegisteredFeature, provider1UnregisteredFeature };

            this.featureOperationsMock
                .Setup(f => f.ListAsync(It.IsAny<string>(), It.IsAny<CancellationToken>()))
                .Callback((string providerName, CancellationToken ignored) => Assert.Equal(providerOfChoice, providerName, StringComparer.InvariantCultureIgnoreCase))
                .Returns(() => Task.FromResult(listResult));

            this.commandRuntimeMock
                .Setup(m => m.WriteObject(It.IsAny<object>()))
                .Callback((object obj) =>
                {
                    Assert.IsType<PSProviderFeature[]>(obj);

                    var features = (PSProviderFeature[])obj;
                    Assert.Equal(1, features.Length);

                    var provider = features.Single();
                    Assert.Equal(Provider1Namespace, provider.ProviderName, StringComparer.InvariantCultureIgnoreCase);
                    Assert.Equal(Feature1Name, provider.FeatureName, StringComparer.InvariantCultureIgnoreCase);
                    Assert.Equal(ProviderFeatureClient.RegisteredStateName, provider.RegistrationState, StringComparer.InvariantCultureIgnoreCase);
                });

            this.cmdlet.ParameterSetOverride = GetAzureProviderFeatureCmdlet.GetFeatureParameterSet;

            this.cmdlet.ExecuteCmdlet();

            this.VerifyListProviderFeaturesCallPatternAndReset();

            // 3.b. List only registered features of a particular provider - and they do not exist
            providerOfChoice = Provider2Namespace;
            this.cmdlet.ListAvailable = false;
            this.cmdlet.ProviderNamespace = providerOfChoice;
            listResult.Features = new[] { provider2UnregisteredFeature };

            this.commandRuntimeMock
                .Setup(m => m.WriteObject(It.IsAny<object>()))
                .Callback((object obj) =>
                {
                    Assert.IsType<PSProviderFeature[]>(obj);

                    var features = (PSProviderFeature[])obj;
                    Assert.Equal(0, features.Length);
                });

            this.cmdlet.ExecuteCmdlet();

            this.VerifyListProviderFeaturesCallPatternAndReset();

            // 4. List all features of a particular provider
            providerOfChoice = Provider1Namespace;
            this.cmdlet.ProviderNamespace = providerOfChoice;
            this.cmdlet.ListAvailable = true;
            listResult.Features = new[] { provider1RegisteredFeature, provider1UnregisteredFeature };

            this.commandRuntimeMock
              .Setup(m => m.WriteObject(It.IsAny<object>()))
              .Callback((object obj) =>
              {
                  Assert.IsType<PSProviderFeature[]>(obj);
                  var features = (PSProviderFeature[])obj;
                  Assert.Equal(2, features.Length);
                  Assert.True(features.Any(feature => string.Equals(feature.FeatureName, Feature1Name, StringComparison.InvariantCultureIgnoreCase)));
                  Assert.True(features.Any(feature => string.Equals(feature.FeatureName, Feature2Name, StringComparison.InvariantCultureIgnoreCase)));
                  Assert.True(features.All(feature => string.Equals(feature.ProviderName, Provider1Namespace, StringComparison.InvariantCultureIgnoreCase)));
              });

            this.cmdlet.ParameterSetOverride = GetAzureProviderFeatureCmdlet.ListAvailableParameterSet;

            this.cmdlet.ExecuteCmdlet();

            this.VerifyListProviderFeaturesCallPatternAndReset();

            // 5. get a single provider feature by name
            this.cmdlet.ProviderNamespace = Provider2Namespace;
            this.cmdlet.FeatureName = Feature1Name;
            this.cmdlet.ListAvailable = false;

            this.featureOperationsMock
              .Setup(f => f.GetAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
              .Callback((string providerName, string featureName, CancellationToken ignored) =>
              {
                  Assert.Equal(Provider2Namespace, providerName, StringComparer.InvariantCultureIgnoreCase);
                  Assert.Equal(Feature1Name, featureName, StringComparer.InvariantCultureIgnoreCase);
              })
              .Returns(() => Task.FromResult(provider2UnregisteredFeature));

            this.commandRuntimeMock
                .Setup(m => m.WriteObject(It.IsAny<object>()))
                .Callback((object obj) =>
                {
                    Assert.IsType<PSProviderFeature[]>(obj);
                    var features = (PSProviderFeature[])obj;
                    Assert.Equal(1, features.Length);
                    var feature = features.Single();
                    Assert.Equal(Provider2Namespace, feature.ProviderName, StringComparer.InvariantCultureIgnoreCase);
                    Assert.Equal(Feature1Name, feature.FeatureName, StringComparer.InvariantCultureIgnoreCase);
                    Assert.Equal("Unregistered", feature.RegistrationState, StringComparer.InvariantCultureIgnoreCase);
                });

            this.cmdlet.ParameterSetOverride = GetAzureProviderFeatureCmdlet.GetFeatureParameterSet;

            this.cmdlet.ExecuteCmdlet();

            this.VerifyGetCallPatternAndReset();
        }
        public void RegisterResourceProviderFeatureTests()
        {
            const string ProviderName = "Providers.Test";
            const string FeatureName = "Feature1";

            var registeredFeature = new FeatureResponse
            {
                Id = "featureId1",
                Name = ProviderName + "/" + FeatureName,
                Properties = new FeatureProperties
                {
                    State = ProviderFeatureClient.RegisteredStateName,
                },
                RequestId = "requestId",
                StatusCode = HttpStatusCode.OK,
                Type = "Microsoft.Features/feature"
            };

            this.featureOperationsMock
                .Setup(client => client.RegisterAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
                .Callback((string providerName, string featureName, CancellationToken ignored) =>
                {
                    Assert.Equal(ProviderName, providerName, StringComparer.InvariantCultureIgnoreCase);
                    Assert.Equal(FeatureName, featureName, StringComparer.InvariantCultureIgnoreCase);
                })
                .Returns(() => Task.FromResult(registeredFeature));

            this.cmdlet.Force = true;
            this.cmdlet.ProviderNamespace = ProviderName;
            this.cmdlet.FeatureName = FeatureName;

            this.commandRuntimeMock
                .Setup(m => m.WriteObject(It.IsAny<object>()))
                .Callback((object obj) =>
                {
                    Assert.IsType<PSProviderFeature>(obj);
                    var feature = (PSProviderFeature)obj;
                    Assert.Equal(ProviderName, feature.ProviderName, StringComparer.InvariantCultureIgnoreCase);
                    Assert.Equal(FeatureName, feature.FeatureName, StringComparer.InvariantCultureIgnoreCase);
                });

            this.cmdlet.ExecuteCmdlet();

            this.VerifyCallPatternAndReset(succeeded: true);
        }