public void StringLengthOnElement_ReturnsCorrectResult() { string path = "$.store.bicycle.color.length"; IReadOnlyList <JsonElement> result = JsonPath.ExecutePath(path, _jsonDocElement); Assert.Equal(3, result.Single().GetInt32()); }
public void UnrecognizedMethodName_ThrowsException() { string path = "$.store.book[?(@.price.nonExistingMethodName() > 10)]"; string input = TestDataLoader.Store(); Assert.Throws <UnrecognizedMethodNameException>(() => JsonPath.ExecutePath(path, input)); }
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 !"); }
public void ArrayLength_ReturnsCorrectResult() { string path = "$.store.book.length"; IReadOnlyList <JsonElement> result = JsonPath.ExecutePath(path, _json); Assert.Equal(4, result.Single().GetInt32()); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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()); }
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); }
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); }
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); }
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); } }
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); } }
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); }
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; } }
public void SliceOperator_ReturnsCorrectResult() { IReadOnlyList <JsonElement> result = JsonPath.ExecutePath("store.book[0:4:2]", _json); Assert.Equal(2, result.Count); }