예제 #1
0
        public IStrategyMatcher FindMatcher(RolloutStrategyAttribute attr)
        {
            switch (attr.Type)
            {
            case RolloutStrategyFieldType.STRING:
                return(new StringMatcher());

            case RolloutStrategyFieldType.SEMANTICVERSION:
                return(new SemanticVersionMatcher());

            case RolloutStrategyFieldType.NUMBER:
                return(new NumberMatcher());

            case RolloutStrategyFieldType.DATE:
                return(new DateMatcher());

            case RolloutStrategyFieldType.DATETIME:
                return(new DateTimeMatcher());

            case RolloutStrategyFieldType.BOOLEAN:
                return(new BooleanMatcher());

            case RolloutStrategyFieldType.IPADDRESS:
                return(new IPNetworkMatcher());

            case null:
                return(new FallthroughMatcher());

            default:
                return(new FallthroughMatcher());
            }
        }
예제 #2
0
        public bool Match(string suppliedValue, RolloutStrategyAttribute attr)
        {
            var vals = attr.Values.Where(v => v != null).Select(v => new IPNetworkProxy(v.ToString())).ToList();
            var ip   = new IPNetworkProxy(suppliedValue);

            switch (attr.Conditional)
            {
            case RolloutStrategyAttributeConditional.EQUALS:
            case RolloutStrategyAttributeConditional.INCLUDES:
                return(vals.Any(v => v.Contains(ip)));

            case RolloutStrategyAttributeConditional.NOTEQUALS:
            case RolloutStrategyAttributeConditional.EXCLUDES:
                return(!vals.Any(v => v.Contains(ip)));

            case RolloutStrategyAttributeConditional.REGEX:
                return(false);

            case null:
                return(false);

            default:
                return(false);
            }
        }
예제 #3
0
        public void BasicNumberStrategy()
        {
            // given: we have a basic number feature with two custom strategies based on age
            var feature = new FeatureState();

            feature.Key     = "num1";
            feature.Value   = 16;
            feature.Version = 1;
            feature.Type    = FeatureValueType.NUMBER;
            var over40Strategy = new RolloutStrategy("id", "name");

            over40Strategy.Value = 6;
            var ruleset1 = new RolloutStrategyAttribute();

            ruleset1.Conditional = RolloutStrategyAttributeConditional.GREATEREQUALS;
            ruleset1.Type        = RolloutStrategyFieldType.NUMBER;
            ruleset1.FieldName   = "age";
            ruleset1.Values      = new List <object> {
                40
            };

            over40Strategy.Attributes = new List <RolloutStrategyAttribute> {
                ruleset1
            };

            var over20Strategy = new RolloutStrategy("id", "name");

            over20Strategy.Value = 10;
            var ruleset2 = new RolloutStrategyAttribute();

            ruleset2.Conditional = RolloutStrategyAttributeConditional.GREATEREQUALS;
            ruleset2.Type        = RolloutStrategyFieldType.NUMBER;
            ruleset2.FieldName   = "age";
            ruleset2.Values      = new List <object> {
                20
            };
            over20Strategy.Attributes = new List <RolloutStrategyAttribute> {
                ruleset2
            };

            feature.Strategies = new List <RolloutStrategy> {
                over40Strategy, over20Strategy
            };

            // when: setup repo
            repo.Notify(new List <FeatureState> {
                feature
            });

            var age27 = new TestClientContext().Attr("age", "27");
            var age18 = new TestClientContext().Attr("age", "18");
            var age43 = new TestClientContext().Attr("age", "43");

            // then
            Assert.AreEqual(10, repo.GetFeature("num1").WithContext(age27).NumberValue);
            Assert.AreEqual(16, repo.GetFeature("num1").WithContext(age18).NumberValue);
            Assert.AreEqual(6, repo.GetFeature("num1").WithContext(age43).NumberValue);
        }
예제 #4
0
        public void DateTimeMatcher(RolloutStrategyAttributeConditional conditional, List <object> vals, string suppliedVal,
                                    bool matches)
        {
            var rsa = new RolloutStrategyAttribute();

            rsa.Conditional = conditional;
            rsa.Values      = vals.Select(v => v as object).ToList();
            rsa.Type        = RolloutStrategyFieldType.DATETIME;

            Assert.AreEqual(matches, registry.FindMatcher(rsa).Match(suppliedVal, rsa));
        }
예제 #5
0
        public void BooleanMatcher(RolloutStrategyAttributeConditional conditional, List <object> vals, string suppliedVal,
                                   bool matches)
        {
            var rsa = new RolloutStrategyAttribute();

            rsa.Conditional = conditional;
            rsa.Type        = RolloutStrategyFieldType.BOOLEAN;
            rsa.Values      = vals.Select(v => v as object).ToList();

            Assert.AreEqual(registry.FindMatcher(rsa).Match(suppliedVal?.ToString(), rsa), matches);
        }
예제 #6
0
        public void SemanticVersionMatcher(RolloutStrategyAttributeConditional conditional, List <object> vals,
                                           string suppliedVal,
                                           bool matches)
        {
            var rsa = new RolloutStrategyAttribute();

            rsa.Conditional = conditional;
            rsa.Values      = vals.Select(v => v as object).ToList();
            rsa.Type        = RolloutStrategyFieldType.SEMANTICVERSION;

            Assert.AreEqual(registry.FindMatcher(rsa).Match(suppliedVal, rsa), matches);
        }
예제 #7
0
        public bool Match(string suppliedValue, RolloutStrategyAttribute attr)
        {
            var vals = attr.Values.Where(v => v != null).Select(v => v.ToString()).ToList();

            switch (attr.Conditional)
            {
            case RolloutStrategyAttributeConditional.EQUALS:
                return(vals.Any(v => v.Equals(suppliedValue)));

            case RolloutStrategyAttributeConditional.ENDSWITH:
                return(vals.Any(suppliedValue.EndsWith));

            case RolloutStrategyAttributeConditional.STARTSWITH:
                return(vals.Any(suppliedValue.StartsWith));

            case RolloutStrategyAttributeConditional.GREATER:
                return(vals.Any(v => string.Compare(suppliedValue, v, StringComparison.Ordinal) > 0));

            case RolloutStrategyAttributeConditional.GREATEREQUALS:
                return(vals.Any(v => string.Compare(suppliedValue, v, StringComparison.Ordinal) >= 0));

            case RolloutStrategyAttributeConditional.LESS:
                return(vals.Any(v => string.Compare(suppliedValue, v, StringComparison.Ordinal) < 0));

            case RolloutStrategyAttributeConditional.LESSEQUALS:
                return(vals.Any(v => string.Compare(suppliedValue, v, StringComparison.Ordinal) <= 0));

            case RolloutStrategyAttributeConditional.NOTEQUALS:
                return(!vals.Any(v => v.Equals(suppliedValue)));

            case RolloutStrategyAttributeConditional.INCLUDES:
                return(vals.Any(suppliedValue.Contains));

            case RolloutStrategyAttributeConditional.EXCLUDES:
                return(!vals.Any(suppliedValue.Contains));

            case RolloutStrategyAttributeConditional.REGEX:
                return(vals.Any(v => Regex.IsMatch(suppliedValue, v)));

            case null:
                return(false);

            default:
                return(false);
            }
        }
예제 #8
0
        public bool Match(string suppliedValue, RolloutStrategyAttribute attr)
        {
            var vals    = attr.Values.Where(v => v != null).Select(v => new Version(v.ToString())).ToList();
            var version = new Version(suppliedValue);

            switch (attr.Conditional)
            {
            case RolloutStrategyAttributeConditional.EQUALS:
            case RolloutStrategyAttributeConditional.INCLUDES:
                return(vals.Any(v => version.Equals(v)));

            case RolloutStrategyAttributeConditional.ENDSWITH:
                break;

            case RolloutStrategyAttributeConditional.STARTSWITH:
                break;

            case RolloutStrategyAttributeConditional.GREATER:
                return(vals.Any(v => version.CompareTo(v) > 0));

            case RolloutStrategyAttributeConditional.GREATEREQUALS:
                return(vals.Any(v => version.CompareTo(v) >= 0));

            case RolloutStrategyAttributeConditional.LESS:
                return(vals.Any(v => version.CompareTo(v) < 0));

            case RolloutStrategyAttributeConditional.LESSEQUALS:
                return(vals.Any(v => version.CompareTo(v) <= 0));

            case RolloutStrategyAttributeConditional.NOTEQUALS:
            case RolloutStrategyAttributeConditional.EXCLUDES:
                return(!vals.Any(v => version.Equals(v)));

            case RolloutStrategyAttributeConditional.REGEX:
                break;

            case null:
                return(false);

            default:
                return(false);
            }

            return(false);
        }
예제 #9
0
        public bool Match(string suppliedValue, RolloutStrategyAttribute attr)
        {
            this._attr = attr;
            var dec = decimal.Parse(suppliedValue);

            switch (attr.Conditional)
            {
            case RolloutStrategyAttributeConditional.EQUALS:
            case RolloutStrategyAttributeConditional.INCLUDES:
                return(DVals.Any(v => dec.Equals(v)));

            case RolloutStrategyAttributeConditional.ENDSWITH:
                return(attr.Values.Where(v => v != null).Any(v => suppliedValue.EndsWith(v.ToString())));

            case RolloutStrategyAttributeConditional.STARTSWITH:
                return(attr.Values.Where(v => v != null).Any(v => suppliedValue.StartsWith(v.ToString())));

            case RolloutStrategyAttributeConditional.GREATER:
                return(DVals.Any(v => dec.CompareTo(v) > 0));

            case RolloutStrategyAttributeConditional.GREATEREQUALS:
                return(DVals.Any(v => dec.CompareTo(v) >= 0));

            case RolloutStrategyAttributeConditional.LESS:
                return(DVals.Any(v => dec.CompareTo(v) < 0));

            case RolloutStrategyAttributeConditional.LESSEQUALS:
                return(DVals.Any(v => dec.CompareTo(v) <= 0));

            case RolloutStrategyAttributeConditional.NOTEQUALS:
            case RolloutStrategyAttributeConditional.EXCLUDES:
                return(!DVals.Any(v => dec.Equals(v)));

            case RolloutStrategyAttributeConditional.REGEX:
                break;

            case null:
                return(false);

            default:
                return(false);
            }

            return(false);
        }
예제 #10
0
        public bool Match(string suppliedValue, RolloutStrategyAttribute attr)
        {
            var val = "true".Equals(suppliedValue);

            if (suppliedValue == null)
            {
                return(false);
            }

            if (attr.Conditional == RolloutStrategyAttributeConditional.EQUALS)
            {
                return(val == (attr.Values[0] is bool?((bool)attr.Values[0]) : bool.Parse(attr.Values[0].ToString())));
            }

            if (attr.Conditional == RolloutStrategyAttributeConditional.NOTEQUALS)
            {
                return(val != (attr.Values[0] is bool?((bool)attr.Values[0]) : bool.Parse(attr.Values[0].ToString())));
            }

            return(false);
        }
예제 #11
0
        public void BasicBooleanStrategy()
        {
            // given: we have a basic boolean feature
            var feature = new FeatureState();

            feature.Key     = "bool1";
            feature.Value   = true;
            feature.Version = 1;
            feature.Type    = FeatureValueType.BOOLEAN;
            var strategy = new RolloutStrategy("id", "name");

            strategy.Value = false;
            var ruleset = new RolloutStrategyAttribute();

            ruleset.Conditional = RolloutStrategyAttributeConditional.EQUALS;
            ruleset.Type        = RolloutStrategyFieldType.STRING;
            ruleset.FieldName   = GetEnumMemberValue(StrategyAttributeWellKnownNames.Country);
            ruleset.Values      = new List <object> {
                GetEnumMemberValue(StrategyAttributeCountryName.Turkey)
            };

            strategy.Attributes = new List <RolloutStrategyAttribute> {
                ruleset
            };
            feature.Strategies = new List <RolloutStrategy> {
                strategy
            };

            repo.Notify(new List <FeatureState> {
                feature
            });

            var matchCC   = new TestClientContext().Country(StrategyAttributeCountryName.Turkey);
            var unmatchCC = new TestClientContext().Country(StrategyAttributeCountryName.Newzealand);

            Assert.AreEqual(false, repo.GetFeature("bool1").WithContext(matchCC).BooleanValue);
            Assert.AreEqual(true, repo.GetFeature("bool1").WithContext(unmatchCC).BooleanValue);
            Assert.AreEqual(true, repo.GetFeature("bool1").BooleanValue);
        }
예제 #12
0
        private void StringTypeComparison(FeatureValueType ft)
        {
            // given: we have a basic string feature with two custom strategies based on age and platform
            var feature = new FeatureState();

            feature.Key     = "s1";
            feature.Value   = "feature";
            feature.Version = 1;
            feature.Type    = ft;
            var notMobileStrategy = new RolloutStrategy("id", "not-mobile");

            notMobileStrategy.Value = "not-mobile";
            var ruleset1 = new RolloutStrategyAttribute();

            ruleset1.Conditional = RolloutStrategyAttributeConditional.EXCLUDES;
            ruleset1.Type        = RolloutStrategyFieldType.STRING;
            ruleset1.FieldName   = GetEnumMemberValue(StrategyAttributeWellKnownNames.Platform);
            ruleset1.Values      = new List <object> {
                GetEnumMemberValue(StrategyAttributePlatformName.Android), GetEnumMemberValue(StrategyAttributePlatformName.Ios)
            };

            notMobileStrategy.Attributes = new List <RolloutStrategyAttribute> {
                ruleset1
            };

            var over20Strategy = new RolloutStrategy("id", "older-than-twenty");

            over20Strategy.Value = "older-than-twenty";
            var ruleset2 = new RolloutStrategyAttribute();

            ruleset2.Conditional = RolloutStrategyAttributeConditional.GREATEREQUALS;
            ruleset2.Type        = RolloutStrategyFieldType.NUMBER;
            ruleset2.FieldName   = "age";
            ruleset2.Values      = new List <object> {
                20
            };
            over20Strategy.Attributes = new List <RolloutStrategyAttribute> {
                ruleset2
            };

            feature.Strategies = new List <RolloutStrategy> {
                notMobileStrategy, over20Strategy
            };

            // when: setup repo
            repo.Notify(new List <FeatureState> {
                feature
            });

            var ccAge27Ios     = new TestClientContext().Platform(StrategyAttributePlatformName.Ios).Attr("age", "27");
            var ccAge18Android = new TestClientContext().Platform(StrategyAttributePlatformName.Android).Attr("age", "18");
            var ccAge43MacOS   = new TestClientContext().Platform(StrategyAttributePlatformName.Macos).Attr("age", "43");
            var ccAge18MacOS   = new TestClientContext().Platform(StrategyAttributePlatformName.Macos).Attr("age", "18");
            var ccEmpty        = new TestClientContext();

            switch (ft)
            {
            case FeatureValueType.STRING:
                // then
                Assert.AreEqual("feature", repo.GetFeature("s1").StringValue);
                Assert.AreEqual("feature", repo.GetFeature("s1").WithContext(ccEmpty).StringValue);
                Assert.AreEqual("feature", repo.GetFeature("s1").WithContext(ccAge18Android).StringValue);
                Assert.AreEqual("not-mobile", repo.GetFeature("s1").WithContext(ccAge18MacOS).StringValue);
                Assert.AreEqual("older-than-twenty", repo.GetFeature("s1").WithContext(ccAge27Ios).StringValue);
                Assert.AreEqual("not-mobile", repo.GetFeature("s1").WithContext(ccAge43MacOS).StringValue);
                break;

            case FeatureValueType.JSON:
                Assert.AreEqual("feature", repo.GetFeature("s1").JsonValue);
                Assert.AreEqual("feature", repo.GetFeature("s1").WithContext(ccEmpty).JsonValue);
                Assert.AreEqual("feature", repo.GetFeature("s1").WithContext(ccAge18Android).JsonValue);
                Assert.AreEqual("not-mobile", repo.GetFeature("s1").WithContext(ccAge18MacOS).JsonValue);
                Assert.AreEqual("older-than-twenty", repo.GetFeature("s1").WithContext(ccAge27Ios).JsonValue);
                Assert.AreEqual("not-mobile", repo.GetFeature("s1").WithContext(ccAge43MacOS).JsonValue);
                break;
            }
        }
예제 #13
0
 public bool Match(string suppliedValue, RolloutStrategyAttribute attr)
 {
     return(false);
 }