Exemplo n.º 1
0
        private async Task <TResponse> Get <TResponse>(string endpoint, IFeatureContext context, CancellationToken token)
        {
            var request = new HttpRequestMessage(HttpMethod.Get, endpoint);

            SetHeadersFromContext(request, context);
            return(await Send <TResponse>(request, token).ConfigureAwait(false));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Can be overrided in a derived class to control how this is implemented. By default, will retrieve a feature and run a foreach with all the activation strategies
        /// </summary>
        /// <param name="feature">The feature to evaluate</param>
        /// <param name="context">Optional context</param>
        /// <returns>Boolean indicating if the feature is considered enabled</returns>
        protected virtual bool IsFeatureEnabledCore(Feature feature, IFeatureContext context)
        {
            if (feature == null || !feature.Enabled)
            {
                return(false);
            }

            if (feature.ActivationStrategies == null || !feature.ActivationStrategies.Any(kv => !string.IsNullOrWhiteSpace(kv.Key)))
            {
                return(false);
            }

            IStrategyHandler handler = null;

            foreach (var activationStrategy in feature.ActivationStrategies.Keys)
            {
                handler = GetStrategyHandler(activationStrategy);
                if (handler == null || !handler.IsEnabled(feature.ActivationStrategies[activationStrategy], context))
                {
                    return(false);
                }
            }

            return(true);
        }
Exemplo n.º 3
0
        public void IsDisabledShouldThrowWhenGivenNullFeature(IFeatureContext context)
        {
            var exception =
                Assert.Throws <ArgumentNullException>(() => FeatureContextExtensions.IsDisabled(context, null));

            Assert.Contains("feature", exception.ParamName);
        }
        public void AddsTogglersToFeatureContext(FeatureContextBuilder sut, IFeatureToggler[] togglers)
        {
            IFeatureContext context = sut.WithTogglers(togglers)
                                      .GetContext();

            Assert.Equal(togglers, context.TogglerSource.GetFeatureToggles());
        }
        public void IsDisabledShouldThrowWhenGivenNullFeature(IFeatureContext context)
        {
            var exception =
                Assert.Throws<ArgumentNullException>(() => FeatureContextExtensions.IsDisabled(context, null));

            Assert.Contains("feature", exception.ParamName);
        }
Exemplo n.º 6
0
 public HookStartedEvent(HookType hookType, IFeatureContext featureContext, IScenarioContext scenarioContext, IScenarioStepContext stepContext)
 {
     HookType        = hookType;
     FeatureContext  = featureContext;
     ScenarioContext = scenarioContext;
     StepContext     = stepContext;
 }
        public void AddsTogglerToFeatureContext(FeatureContextBuilder sut, IFeatureToggler featureToggler)
        {
            IFeatureContext context = sut.WithToggler(featureToggler)
                                      .GetContext();

            Assert.Same(featureToggler, context.TogglerSource.GetFeatureToggles().Single());
        }
Exemplo n.º 8
0
 public HookFinishedEvent(HookType hookType, IFeatureContext featureContext, IScenarioContext scenarioContext, IScenarioStepContext stepContext, Exception hookException)
 {
     HookType        = hookType;
     HookException   = hookException;
     FeatureContext  = featureContext;
     ScenarioContext = scenarioContext;
     StepContext     = stepContext;
 }
Exemplo n.º 9
0
        public async Task <bool> IsFeatureEnabledAsync(string featureId, IFeatureContext context, CancellationToken token)
        {
            if (string.IsNullOrWhiteSpace(featureId))
            {
                throw new ArgumentNullException(nameof(featureId));
            }

            return(await _httpClient.IsFeatureEnabledAsync(featureId, context, token));
        }
 public FeatureContextConfigurationUsage()
 {
     _featureContext = new FeatureContextBuilder()
                       .Togglers
                       .DependentFeatureToggler()
                       .AppSettingsToggler()
                       .DefaultValueToggler()
                       .End()
                       .GetContext();
 }
        public void ReturnsFalseWhenDependentFeatureIsEnabled(IFeatureContext featureContext, FeatureFixture feature,
                                                              IFeature dependentFeature, DependentFeatureToggler sut)
        {
            A.CallTo(() => featureContext.IsEnabled(dependentFeature))
            .Returns(true);
            feature.Traits.Add(new DependentFeatureTrait(dependentFeature));

            bool?result = sut.IsEnabled(featureContext, feature);

            Assert.Null(result);
        }
        public void IsDisabledShouldInvertFeatureContext(IFeatureContext context, IFeature feature, bool enabled)
        {
            A.CallTo(() => context.IsEnabled(feature))
                .Returns(enabled);

            bool result = FeatureContextExtensions.IsDisabled(context, feature);

            Assert.Equal(!enabled, result);
            A.CallTo(() => context.IsEnabled(feature))
                .MustHaveHappened();
        }
        public void ChecksStateOfDependentFeatures(IFeatureContext featureContext, FeatureFixture feature, IFeature dependentFeature, DependentFeatureToggler sut)
        {
            A.CallTo(() => featureContext.IsEnabled(dependentFeature))
            .Returns(true);
            feature.Traits.Add(new DependentFeatureTrait(dependentFeature));

            sut.IsEnabled(featureContext, feature);

            A.CallTo(() => featureContext.IsEnabled(dependentFeature))
            .MustHaveHappened();
        }
Exemplo n.º 14
0
        public void IsDisabledShouldInvertFeatureContext(IFeatureContext context, IFeature feature, bool enabled)
        {
            A.CallTo(() => context.IsEnabled(feature))
            .Returns(enabled);

            bool result = FeatureContextExtensions.IsDisabled(context, feature);

            Assert.Equal(!enabled, result);
            A.CallTo(() => context.IsEnabled(feature))
            .MustHaveHappened();
        }
Exemplo n.º 15
0
        /// <summary>
        /// Can be overrided in a derived class to control how this is implemented. By default, will retrieve a feature and run a foreach with all the activation strategies
        /// </summary>
        /// <param name="featureId">Required id of the feature</param>
        /// <param name="context">Optional context</param>
        /// <returns>Boolean indicating if the feature is considered enabled</returns>
        protected virtual bool IsFeatureEnabledCore(string featureId, IFeatureContext context)
        {
            if (string.IsNullOrWhiteSpace(featureId))
            {
                throw new ArgumentNullException(featureId);
            }

            var feature = _featureStore.GetFeatureById(featureId);

            return(IsFeatureEnabledCore(feature, context));
        }
Exemplo n.º 16
0
        protected virtual DesignerContext CreateDesignerContext()
        {
            IFeatureContext featureContext = this.ServiceProvider.GetService <IFeatureContext>();

            Debug.Assert(featureContext != null);
            if (featureContext != null)
            {
                return(featureContext.CreateFeature <DesignerContext>());
            }
            return(null);
        }
Exemplo n.º 17
0
        /// <summary>
        /// Checks to see if a feature is disabled.
        /// </summary>
        /// <param name="featureContext">The feature context.</param>
        /// <param name="feature">The feature.</param>
        /// <exception cref="System.ArgumentNullException"><paramref name="featureContext"/> is <see langword="null" />.</exception>
        /// <exception cref="System.ArgumentNullException"><paramref name="feature"/> is <see langword="null" />.</exception>
        /// <exception cref="Exceptions.FeatureStateNotFoundException">The given feature has no value assoicated with it.</exception>
        /// <returns><c>true</c> if the feature is disabled; otherwise, <c>false</c>.</returns>
        public static bool IsDisabled(this IFeatureContext featureContext, IFeature feature)
        {
            if (featureContext == null)
            {
                throw new ArgumentNullException(nameof(featureContext));
            }
            if (feature == null)
            {
                throw new ArgumentNullException(nameof(feature));
            }

            return(!featureContext.IsEnabled(feature));
        }
Exemplo n.º 18
0
        public IEnumerable <string> GetEnabledFeatures(IFeatureContext context)
        {
            var featureIds = new List <string>();
            var features   = _featureStore.GetEnabledFeatures();

            foreach (var feature in features)
            {
                if (IsFeatureEnabledCore(feature, context))
                {
                    featureIds.Add(feature.Id);
                }
            }
            return(featureIds);
        }
Exemplo n.º 19
0
        protected override void OnInvoke()
        {
            IFeatureContext featureContext = this.ServiceProvider.GetService <IFeatureContext>();

            Debug.Assert(featureContext != null);
            if (featureContext != null)
            {
                PanZoomWindow toolWindow = featureContext.CreateFeature <PanZoomWindow>();
                Debug.Assert(toolWindow != null);
                if (toolWindow != null)
                {
                    toolWindow.Show();
                }
            }
        }
        public void ChecksStateOfAllDependentFeatures(IFeatureContext featureContext, FeatureFixture feature,
                                                      IFeature dependentFeature, DependentFeatureToggler sut)
        {
            A.CallTo(() => featureContext.IsEnabled(dependentFeature))
            .Returns(true);
            // Hey, it works!
            feature.Traits.Add(new DependentFeatureTrait(dependentFeature));
            feature.Traits.Add(new DependentFeatureTrait(dependentFeature));
            feature.Traits.Add(new DependentFeatureTrait(dependentFeature));

            sut.IsEnabled(featureContext, feature);

            A.CallTo(() => featureContext.IsEnabled(dependentFeature))
            .MustHaveHappened(Repeated.Exactly.Times(3));
        }
Exemplo n.º 21
0
        public DependentFeatureUsage()
        {
            BaseFeature = FeatureBuilder.Create("BaseFeature")
                          .Description("This is the base feature")
                          .WithDefaultValue(false)
                          .Build();

            ChildFeature = FeatureBuilder.Create("BaseFeature.Child")
                           .Description("This is a child feature of the base feature, that should be disabled when the parent is.")
                           .WithDefaultValue(true)
                           .DependentOn(BaseFeature)
                           .Build();

            _featureContext = new FeatureContext(new FeatureTogglerSource(new DependentFeatureToggler(), new DefaultValueToggler()));
        }
Exemplo n.º 22
0
            public void ReturnsIsEnabledFromContext(IFeature feature, IFeatureContext context, bool expected)
            {
                A.CallTo(() => context.IsEnabled(feature))
                    .Returns(expected);

                bool result;
                using (ContextSwitcher.For(context))
                {
                    result = Feature.IsEnabled(feature);
                }

                Assert.Equal(expected, result);
                A.CallTo(() => context.IsEnabled(feature))
                    .MustHaveHappened();
            }
Exemplo n.º 23
0
            public void ReturnsIsEnabledFromContext(IFeature feature, IFeatureContext context, bool expected)
            {
                A.CallTo(() => context.IsEnabled(feature))
                .Returns(expected);

                bool result;

                using (ContextSwitcher.For(context))
                {
                    result = Feature.IsDisabled(feature);
                }

                Assert.Equal(!expected, result);
                A.CallTo(() => context.IsEnabled(feature))
                .MustHaveHappened();
            }
Exemplo n.º 24
0
        public async Task <IEnumerable <string> > GetEnabledFeaturesAsync(IFeatureContext context, CancellationToken token)
        {
            var featureIds = new List <string>();
            var features   = await _featureStore.GetEnabledFeaturesAsync(token);

            bool isEnanled;

            foreach (var feature in features)
            {
                isEnanled = await IsFeatureEnabledAsync(feature.Id, context, token).ConfigureAwait(false);

                if (isEnanled)
                {
                    featureIds.Add(feature.Id);
                }
            }
            return(featureIds);
        }
Exemplo n.º 25
0
        public bool?IsEnabled(IFeatureContext featureContext, IFeature feature)
        {
            if (feature == null)
            {
                throw new ArgumentNullException(nameof(feature));
            }

            string configName  = SettingsPrefix + feature.Id;
            string configValue = ConfigurationManager.AppSettings[configName];

            bool result;

            if (bool.TryParse(configValue, out result))
            {
                return(result);
            }

            return(null);
        }
Exemplo n.º 26
0
        public bool?IsEnabled(IFeatureContext featureContext, IFeature feature)
        {
            if (featureContext == null)
            {
                throw new ArgumentNullException(nameof(featureContext));
            }
            if (feature == null)
            {
                throw new ArgumentNullException(nameof(feature));
            }

            foreach (DependentFeatureTrait trait in feature.Traits.OfType <DependentFeatureTrait>())
            {
                if (!featureContext.IsEnabled(trait.DependentFeature))
                {
                    return(false);
                }
            }

            return(null);
        }
Exemplo n.º 27
0
        public bool?IsEnabled(IFeatureContext featureContext, IFeature feature)
        {
            if (feature == null)
            {
                throw new ArgumentNullException(nameof(feature));
            }

            DefaultValueTrait trait = null;

            foreach (DefaultValueTrait defaultValueTrait in feature.Traits.OfType <DefaultValueTrait>())
            {
                if (trait != null)
                {
                    throw new InvalidFeatureException($"Feature {feature.Id} contains more than one {nameof(DefaultValueTrait)}");
                }

                trait = defaultValueTrait;
            }

            return(trait?.DefaultValue);
        }
Exemplo n.º 28
0
        public bool IsEnabled(Dictionary <string, string> parameters, IFeatureContext context)
        {
            var userId = string.Empty;

            context?.Parameters?.TryGetValue(UserIdParameterName, out userId);

            if (string.IsNullOrWhiteSpace(userId))
            {
                return(false);
            }

            if (parameters.TryGetValue(AllowedUserIdsParameterName, out var allowedUserIds) && !allowedUserIds.Contains($"{Separator}{userId}{Separator}"))
            {
                return(false);
            }

            if (parameters.TryGetValue(ExcludedUserIdsParameterName, out var excludedUserIds) && excludedUserIds.Contains($"{Separator}{userId}{Separator}"))
            {
                return(false);
            }

            return(true);
        }
Exemplo n.º 29
0
 public async Task <IEnumerable <string> > GetEnabledFeaturesAsync(IFeatureContext context, CancellationToken token)
 => await _httpClient.GetEnabledFeaturesAsync(context, token);
Exemplo n.º 30
0
 public Task <bool> IsEnabledAsync(Dictionary <string, string> parameters, IFeatureContext context, CancellationToken token)
 => Task.FromResult(IsEnabled(parameters, context));
Exemplo n.º 31
0
 public static void Clear()
 {
     context = null;
 }
Exemplo n.º 32
0
 public bool IsFeatureEnabled(string featureId, IFeatureContext context)
 => IsFeatureEnabledAsync(featureId, context, CancellationToken.None).Result;
Exemplo n.º 33
0
 public Task <bool> IsFeatureEnabledAsync(string featureId, IFeatureContext context)
 => IsFeatureEnabledAsync(featureId, context, CancellationToken.None);