Beispiel #1
0
        public async Task RuleUsingTimeBasedOperators()
        {
            contexts = ContextCreator.Merge(
                ContextCreator.Create("device", "1", Tuple.Create("birthday", JsonValue.NewString(DateTime.UtcNow.AddDays(-2).ToString("u")))),
                ContextCreator.Create("device", "2", Tuple.Create("birthday", JsonValue.NewString(DateTime.UtcNow.AddDays(-5).ToString("u")))));

            paths = new[] { "abc/somepath" };
            rules = new Dictionary <string, RuleDefinition>()
            {
                ["abc/somepath"] =
                    JPadGenerator.New()
                    .AddSingleVariantRule(JsonConvert.SerializeObject(new Dictionary <string, object>()
                {
                    { "Device.birthday", new Dictionary <string, object>()
                      {
                          { "$withinTime", "3d" }
                      } }
                }), value: "true")
                    .AddSingleVariantRule(JsonConvert.SerializeObject(new {}), value: "false")
                    .Generate()
            };

            await Run(async (tweek, context) =>
            {
                var val = await tweek.GetContextAndCalculate("abc/_", new HashSet <Identity> {
                    new Identity("device", "1")
                }, context);
                Assert.Equal("true", val["abc/somepath"].Value.AsString());

                val = await tweek.GetContextAndCalculate("abc/_", new HashSet <Identity> {
                    new Identity("device", "2")
                }, context);
                Assert.Equal("false", val["abc/somepath"].Value.AsString());
            });
        }
Beispiel #2
0
        public async Task CalculateWithFixedValue()
        {
            contexts = ContextCreator.Merge(ContextCreator.Create("device", "1", Tuple.Create("@fixed:abc/somepath", JsonValue.NewString("FixedValue"))),
                                            ContextCreator.Create("device", "2", Tuple.Create("SomeDeviceProp", JsonValue.NewNumber(5))),
                                            ContextCreator.Create("device", "3", Tuple.Create("SomeDeviceProp", JsonValue.NewNumber(5)), Tuple.Create("@fixed:abc/somepath", JsonValue.NewString("FixedValue"))));

            paths = new[] { "abc/somepath" };
            rules = new Dictionary <string, RuleDefinition>()
            {
                ["abc/somepath"] = JPadGenerator.New().AddSingleVariantRule(matcher: JsonConvert.SerializeObject(new Dictionary <string, object>()
                {
                    { "device.SomeDeviceProp", 5 }
                }), value: "RuleBasedValue").Generate()
            };


            await Run(async (tweek, context) =>
            {
                var val = await tweek.GetContextAndCalculate("abc/_", new HashSet <Identity> {
                    new Identity("device", "1")
                }, context);
                Assert.Equal("FixedValue", val["abc/somepath"].Value.AsString());

                val = await tweek.GetContextAndCalculate("abc/_", new HashSet <Identity> {
                    new Identity("device", "2")
                }, context);
                Assert.Equal("RuleBasedValue", val["abc/somepath"].Value.AsString());

                val = await tweek.GetContextAndCalculate("abc/_", new HashSet <Identity> {
                    new Identity("device", "3")
                }, context);
                Assert.Equal("FixedValue", val["abc/somepath"].Value.AsString());
            });
        }
Beispiel #3
0
        public async Task CalculateFilterByMatcherWithMultiIdentities()
        {
            contexts = ContextCreator.Merge(
                ContextCreator.Create("user", "1", Tuple.Create("SomeUserProp", JsonValue.NewNumber(10))),
                ContextCreator.Create("device", "1", Tuple.Create("SomeDeviceProp", JsonValue.NewNumber(5))));
            paths = new[] { "abc/somepath" };
            rules = new Dictionary <string, RuleDefinition>
            {
                ["abc/somepath"] = JPadGenerator.New().AddSingleVariantRule(matcher: JsonConvert.SerializeObject(new MatcherData
                {
                    ["device.SomeDeviceProp"] = 5,
                    ["user.SomeUserProp"]     = 10
                }), value: "SomeValue").Generate()
            };

            await Run(async (tweek, context) =>
            {
                var val = await tweek.GetContextAndCalculate("abc/_", new HashSet <Identity> {
                    new Identity("device", "1")
                }, context);
                Assert.Equal(0, val.Count);

                val = await tweek.GetContextAndCalculate("abc/_", new HashSet <Identity> {
                    new Identity("user", "1")
                }, context);
                Assert.Equal(0, val.Count);

                val = await tweek.GetContextAndCalculate("abc/_", new HashSet <Identity> {
                    new Identity("device", "1"), new Identity("user", "1")
                }, context);
                Assert.Equal("SomeValue", val["abc/somepath"].Value.AsString());
            });
        }
Beispiel #4
0
        public async Task EmptyFixedKeyIsIgnoredInScan()
        {
            contexts = ContextCreator.Merge(ContextCreator.Create("device", "1", Tuple.Create("@fixed:", JsonValue.NewString("FixedValue"))));

            paths = new[] { "abc/somepath" };
            rules = rules = new Dictionary <string, RuleDefinition>
            {
                ["abc/somepath"] = JPadGenerator.New().AddSingleVariantRule(matcher: "{}", value: "SomeValue").Generate()
            };


            await Run(async (tweek, context) =>
            {
                var val = await tweek.GetContextAndCalculate("_", new HashSet <Identity> {
                    new Identity("device", "1")
                }, context);
                Assert.Equal("SomeValue", val["abc/somepath"].Value.AsString());
            });
        }
Beispiel #5
0
        public async Task CalculateWithRecursiveMatcher()
        {
            contexts = ContextCreator.Merge(
                ContextCreator.Create("device", "1", Tuple.Create("SomeDeviceProp", JsonValue.NewNumber(5))),
                ContextCreator.Create("device", "2", Tuple.Create("@fixed:abc/dep_path2", JsonValue.NewBoolean(true)), Tuple.Create("SomeDeviceProp", JsonValue.NewNumber(5)))
                );

            paths = new[] { "abc/somepath", "abc/dep_path1", "abc/dep_path2" };

            rules = new Dictionary <string, RuleDefinition>()
            {
                ["abc/dep_path1"] = JPadGenerator.New().AddSingleVariantRule(matcher: JsonConvert.SerializeObject(new Dictionary <string, object>()
                {
                    { "device.SomeDeviceProp", 5 }
                }), value: true).Generate(),
                ["abc/somepath"] = JPadGenerator.New().AddSingleVariantRule(matcher: JsonConvert.SerializeObject(new Dictionary <string, object>()
                {
                    { "@@key:abc/dep_path1", true },
                    { "keys.abc/dep_path2", true }
                }),
                                                                            value: true).Generate()
            };

            await Run(async (tweek, context) =>
            {
                var val = await tweek.GetContextAndCalculate("abc/_", new HashSet <Identity> {
                    new Identity("device", "1")
                }, context);
                Assert.Equal(1, val.Count);
                Assert.Equal("true", val["abc/dep_path1"].Value.AsString());

                val = await tweek.GetContextAndCalculate("abc/_", new HashSet <Identity> {
                    new Identity("device", "2")
                }, context);
                Assert.Equal(3, val.Count);
                Assert.Equal("true", val["abc/dep_path1"].Value.AsString());
                Assert.Equal("true", val["abc/dep_path2"].Value.AsString());
                Assert.Equal("true", val["abc/somepath"].Value.AsString());
            });
        }