예제 #1
0
        public void StringLengthOnElement_ReturnsCorrectResult()
        {
            string path = "$.store.bicycle.color.length";
            IReadOnlyList <JsonElement> result = JsonPath.ExecutePath(path, _jsonDocElement);

            Assert.Equal(3, result.Single().GetInt32());
        }
예제 #2
0
        public void UnrecognizedMethodName_ThrowsException()
        {
            string path  = "$.store.book[?(@.price.nonExistingMethodName() > 10)]";
            string input = TestDataLoader.Store();

            Assert.Throws <UnrecognizedMethodNameException>(() => JsonPath.ExecutePath(path, input));
        }
예제 #3
0
        public override string Execute(NatsService natsService, ExecutorService executorService)
        {
            if (executorService.Message == null)
            {
                throw new ScriptCommandException("No received message !");
            }
            if (executorService.Message.Data == null)
            {
                throw new ScriptCommandException("No data in message !");
            }

            string data = executorService.Message.Data;
            IReadOnlyList <JsonElement> resultJson = JsonPath.ExecutePath(Param1, data);
            var result = JsonSerializer.Serialize(resultJson, new JsonSerializerOptions {
                WriteIndented = true
            });

            if (result == Param2)
            {
                return("Ok.");
            }

            Logger.Error($"Pattern: {Param1}");
            Logger.Error($"Data: {data}");
            Logger.Error($"Result: {result}");
            Logger.Error($"Expected: {Param2}");
            throw new ScriptCommandException("Result and Expected values are different !");
        }
예제 #4
0
        public void ArrayLength_ReturnsCorrectResult()
        {
            string path = "$.store.book.length";
            IReadOnlyList <JsonElement> result = JsonPath.ExecutePath(path, _json);

            Assert.Equal(4, result.Single().GetInt32());
        }
예제 #5
0
        public void WildcardOperator_ReturnsCorrectResult()
        {
            IReadOnlyList <JsonElement> result = JsonPath.ExecutePath("$.store.bicycle.*", _json);

            Assert.Equal(2, result.Count);
            Assert.Contains(result, x => x.ValueKind == JsonValueKind.String && x.GetString() == "red");
            Assert.Contains(result, x => x.ValueKind == JsonValueKind.Number && x.GetDouble() == 19.95);
        }
예제 #6
0
        public void PropertyAndEscapedProperty_ReturnsSameResult()
        {
            IReadOnlyList <JsonElement> result1 = JsonPath.ExecutePath("$.store.bicycle.price", _json);
            IReadOnlyList <JsonElement> result2 = JsonPath.ExecutePath("$[\"store\"][\"bicycle\"][\"price\"]", _json);
            IReadOnlyList <JsonElement> result3 = JsonPath.ExecutePath("$['store']['bicycle']['price']", _json);

            Assert.Equal(result1.Single(), result2.Single(), JsonElementEqualityComparer.Default);
            Assert.Equal(result2.Single(), result2.Single(), JsonElementEqualityComparer.Default);
        }
예제 #7
0
        public void AllProperties_ReturnsValidResult(string path, string expected)
        {
            string json = TestDataLoader.Store();

            IReadOnlyList <JsonElement> result = JsonPath.ExecutePath(path, json);
            string resultJson = JsonSerializer.Serialize(result).RemoveWhiteSpace();

            Assert.Equal(expected, resultJson);
        }
예제 #8
0
        public void PathWithSlice_ReturnsExpectedResult(string path, string expectedJson)
        {
            expectedJson = expectedJson.Replace("`", "\"");
            string json = TestDataLoader.AbcArray();

            IReadOnlyList <JsonElement> result = JsonPath.ExecutePath(path, json);
            string resultJson = JsonSerializer.Serialize(result);

            Assert.Equal(expectedJson, resultJson);
        }
예제 #9
0
        public void StringMethodToUpper_ReturnsCorrectResult()
        {
            string path = "$.store.book[?(@.author.contains(\"tolkien\", true))]";
            IReadOnlyList <JsonElement> result = JsonPath.ExecutePath(path, _json);

            Assert.Single(result);
            string resultString = JsonSerializer.Serialize(result.Single());

            Assert.Equal(_lotr, resultString);
        }
예제 #10
0
        public void FilterOnNonExistingPropertyEqualNull_ReturnsCorrectResult()
        {
            string input = TestDataLoader.BooksWithNulls();

            ExpressionList expression = ExpressionList.TokenizeAndParse("$.books[?(@.plumbus == null)]");

            IReadOnlyList <JsonElement> result = JsonPath.ExecutePath(expression, input);

            Assert.Equal(4, result.Count);
        }
예제 #11
0
        public void StringLengthInFilter_ReturnsCorrectResult()
        {
            string path = "$.store.book[?(@.title.length == 21)]";
            IReadOnlyList <JsonElement> result = JsonPath.ExecutePath(path, _json);

            Assert.Single(result);
            string resultString = JsonSerializer.Serialize(result.Single());

            Assert.Equal(_lotr, resultString);
        }
예제 #12
0
        public void FilterWithWildcardPropertyFilter_ReturnsCorrectResult()
        {
            string path  = "$.store.bicycle.*";
            string input = TestDataLoader.Store();
            IReadOnlyList <JsonElement> result = JsonPath.ExecutePath(path, input);

            Assert.Equal(2, result.Count);

            Assert.Contains(result, x => x.ValueKind == JsonValueKind.String && x.GetString() == "red");
            Assert.Contains(result, x => x.ValueKind == JsonValueKind.Number && x.GetDouble() == 19.95);

            path   = "store.*";
            result = JsonPath.ExecutePath(path, input);

            string resultJson = JsonSerializer.Serialize(result).RemoveWhiteSpace();

            string expected = @"
                [
                  [
                    {
                      `category`: `reference`,
                      `author`: `Nigel Rees`,
                      `title`: `Sayings of the Century`,
                      `price`: 8.95
                    },
                    {
                      `category`: `fiction`,
                      `author`: `Evelyn Waugh`,
                      `title`: `Sword of Honour`,
                      `price`: 12.99
                    },
                    {
                      `category`: `fiction`,
                      `author`: `Herman Melville`,
                      `title`: `Moby Dick`,
                      `isbn`: `0-553-21311-3`,
                      `price`: 8.99
                    },
                    {
                      `category`: `fiction`,
                      `author`: `J. R. R. Tolkien`,
                      `title`: `The Lord of the Rings`,
                      `isbn`: `0-395-19395-8`,
                      `price`: 22.99
                    }
                  ],
                  {
                    `color`: `red`,
                    `price`: 19.95
                  }
                ]"
                              .Replace("`", "\"").RemoveWhiteSpace();

            Assert.Equal(expected, resultJson);
        }
예제 #13
0
        public void Expression_UsedMultipleTimes_ReturnsSameResult()
        {
            string         path       = "$.store.bicycle.color.length";
            ExpressionList expression = JsonPathExpression.Parse(path);
            JsonDocument   doc        = JsonDocument.Parse(_json);

            JsonElement result1 = JsonPath.ExecutePath(expression, doc).Single();
            JsonElement result2 = JsonPath.ExecutePath(expression, doc).Single();

            Assert.Equal(result1, result2, JsonElementEqualityComparer.Default);
        }
예제 #14
0
        public void RecursiveOperator_ReturnsCorrectResult()
        {
            IReadOnlyList <JsonElement> result = JsonPath.ExecutePath("$.store.book..", _json);

            Assert.Equal(5, result.Count);

            string expected = "[[{`category`:`reference`,`author`:`Nigel Rees`,`title`:`Sayings of the Century`,`price`:8.95},{`category`:`fiction`,`author`:`Evelyn Waugh`,`title`:`Sword of Honour`,`price`:12.99},{`category`:`fiction`,`author`:`Herman Melville`,`title`:`Moby Dick`,`isbn`:`0-553-21311-3`,`price`:8.99},{`category`:`fiction`,`author`:`J. R. R. Tolkien`,`title`:`The Lord of the Rings`,`isbn`:`0-395-19395-8`,`price`:22.99}],{`category`:`reference`,`author`:`Nigel Rees`,`title`:`Sayings of the Century`,`price`:8.95},{`category`:`fiction`,`author`:`Evelyn Waugh`,`title`:`Sword of Honour`,`price`:12.99},{`category`:`fiction`,`author`:`Herman Melville`,`title`:`Moby Dick`,`isbn`:`0-553-21311-3`,`price`:8.99},{`category`:`fiction`,`author`:`J. R. R. Tolkien`,`title`:`The Lord of the Rings`,`isbn`:`0-395-19395-8`,`price`:22.99}]"
                              .Replace("`", "\"");
            string resultJson = JsonSerializer.Serialize(result);

            Assert.Equal(expected, resultJson);
        }
예제 #15
0
        public void FilterOnNumberEqualNull_ReturnsCorrectResult()
        {
            string input = TestDataLoader.BooksWithNulls();

            ExpressionList expression = ExpressionList.TokenizeAndParse("$.books[?(@.price== null)]");

            IReadOnlyList <JsonElement> result = JsonPath.ExecutePath(expression, input);

            Assert.Equal(1, result.Count);
            Assert.Equal(JsonValueKind.Object, result[0].ValueKind);
            Assert.Equal("Sayings of the Century", result[0].EnumerateObject().FirstOrDefault(x => x.Name == "title").Value.GetString());
        }
예제 #16
0
        public void FitlerOperatorOnPrice_ReturnsCorrectResult()
        {
            IReadOnlyList <JsonElement> result = JsonPath.ExecutePath("$.store.book[?(@.price > 10)]", _json);

            Assert.Equal(2, result.Count);

            string resultJson   = JsonSerializer.Serialize(result);
            string expectedJson = "[{`category`:`fiction`,`author`:`Evelyn Waugh`,`title`:`Sword of Honour`,`price`:12.99},{`category`:`fiction`,`author`:`J. R. R. Tolkien`,`title`:`The Lord of the Rings`,`isbn`:`0-395-19395-8`,`price`:22.99}]"
                                  .Replace("`", "\"");

            Assert.Equal(expectedJson, resultJson);
        }
예제 #17
0
        public void FitlerTruthyIsbnOperator_ReturnsCorrectResult()
        {
            IReadOnlyList <JsonElement> result = JsonPath.ExecutePath("$.store.book[?(@.isbn)]", _json);

            Assert.Equal(2, result.Count);

            string resultJson   = JsonSerializer.Serialize(result);
            string expectedJson = "[{`category`:`fiction`,`author`:`Herman Melville`,`title`:`Moby Dick`,`isbn`:`0-553-21311-3`,`price`:8.99},{`category`:`fiction`,`author`:`J. R. R. Tolkien`,`title`:`The Lord of the Rings`,`isbn`:`0-395-19395-8`,`price`:22.99}]"
                                  .Replace("`", "\"");

            Assert.Equal(expectedJson, resultJson);
        }
예제 #18
0
        public void PathWithAllElements_ReturnsExpectedResult()
        {
            string path = "[*]";
            string json = TestDataLoader.AbcArray();

            IReadOnlyList <JsonElement> result = JsonPath.ExecutePath(path, json);
            string resultJson = JsonSerializer.Serialize(result);

            json = new string(json.Where(x => !char.IsWhiteSpace(x)).ToArray());

            Assert.Equal(json, resultJson);
        }
예제 #19
0
        public void FilterOnBooks_ReturnsCorrectResult(string path, params string[] expected)
        {
            string input = TestDataLoader.Store();
            IReadOnlyList <JsonElement> result = JsonPath.ExecutePath(path, input);

            Assert.Equal(expected.Length, result.Count);
            string[] resultJsons = result.Select(x => JsonSerializer.Serialize(x)).Select(x => x.RemoveWhiteSpace()).ToArray();

            foreach (string e in expected)
            {
                Assert.Contains(_bookJsons[e], resultJsons);
            }
        }
예제 #20
0
        public void RecursiveFilterAndWildcardArrayFilter_ReturnsCorrectResult(string path, params string[] expected)
        {
            string input = TestDataLoader.Store();
            IReadOnlyList <JsonElement> result = JsonPath.ExecutePath(path, input);

            Assert.Equal(expected.Length, result.Count);

            foreach (JsonElement r in result)
            {
                Assert.Equal(JsonValueKind.String, r.ValueKind);
                string rString = r.GetString();
                Assert.Contains(rString, expected);
            }
        }
예제 #21
0
        public void LessThanOrEqualFilterOnIntWithDecimal_ReturnsCorrectResult()
        {
            string input = @"
                {
                    ""object"": {
                        ""arrays"": [ { ""amount"": 10 } ]
                    }
                }
            ";

            ExpressionList expression = ExpressionList.TokenizeAndParse("$.object.arrays[?(@.amount >= 10.0)]");

            IReadOnlyList <JsonElement> result = JsonPath.ExecutePath(expression, input);

            Assert.Equal(1, result.Count);
        }
예제 #22
0
 private void TestJson()
 {
     try
     {
         IReadOnlyList <JsonElement> result = JsonPath.ExecutePath(Model.Expression, Model.Data);
         Model.Result = JsonSerializer.Serialize(result, new JsonSerializerOptions {
             WriteIndented = true
         });
     }
     catch (Exception e)
     {
         var msg = $"Failed to apply Json path: {e.Message}";
         Logger.Error(msg);
         Model.Result = msg;
     }
 }
예제 #23
0
        public void SliceOperator_ReturnsCorrectResult()
        {
            IReadOnlyList <JsonElement> result = JsonPath.ExecutePath("store.book[0:4:2]", _json);

            Assert.Equal(2, result.Count);
        }