Path: redb-route/tests/redb.Route.Tests/Expressions/JsonPathExpressionTests.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/tests/redb.Route.Tests/Expressions/JsonPathExpressionTests.cs
----
using AwesomeAssertions;
using System.Text.Json.Nodes;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Expressions;
namespace redb.Route.Tests.Expressions;
///
/// Tests for JsonPath functionality: JsonPathExpression, TypedJsonPathExpression,
/// CompiledJPathExpression, jpath() DSL helpers, and ApplyJPath via ExpressionResolver.
///
[Collection("ExpressionResolver")]
public class JsonPathExpressionTests : IDisposable
{
private const string SampleJson = """
{
"id": "DRV-001",
"firstName": "John",
"lastName": "Doe",
"isHired": true,
"dismissed": false,
"age": 35,
"salary": 75000.50,
"tags": ["driver", "active", "verified"],
"scores": [90, 85, 92],
"address": {
"city": "Moscow",
"country": "Russia"
},
"contacts": [
{ "type": "email", "value": "john@example.com" },
{ "type": "phone", "value": "+7-999-123-45-67" }
]
}
""";
public JsonPathExpressionTests()
{
ExpressionResolver.ClearAllCaches();
}
public void Dispose()
{
ExpressionResolver.ClearAllCaches();
}
private static IExchange CreateJsonExchange(string json = SampleJson)
=> new Exchange(new Message(JsonNode.Parse(json)!));
private static IExchange CreatePocoExchange()
=> new Exchange(new Message(new { Name = "Alice", Age = 30, Active = true }));
// ── JsonPathExpression: basic property access ──
[Fact]
public void Evaluate_StringProperty_ReturnsString()
{
var expr = new JsonPathExpression("$.firstName");
var result = expr.Evaluate(CreateJsonExchange());
result.Should().Be("John");
}
[Fact]
public void Evaluate_BoolProperty_ReturnsBool()
{
var expr = new JsonPathExpression("$.isHired");
var result = expr.Evaluate(CreateJsonExchange());
result.Should().BeTrue();
}
[Fact]
public void Evaluate_IntProperty_ReturnsInt()
{
var expr = new JsonPathExpression("$.age");
var result = expr.Evaluate(CreateJsonExchange());
result.Should().Be(35);
}
[Fact]
public void Evaluate_DoubleProperty_ReturnsDouble()
{
var expr = new JsonPathExpression("$.salary");
var result = expr.Evaluate(CreateJsonExchange());
result.Should().BeApproximately(75000.50, 0.01);
}
[Fact]
public void Evaluate_NestedProperty_ReturnsValue()
{
var expr = new JsonPathExpression("$.address.city");
var result = expr.Evaluate(CreateJsonExchange());
result.Should().Be("Moscow");
}
[Fact]
public void Evaluate_RootPath_ReturnsWholeDocument()
{
var expr = new JsonPathExpression("$");
var result = expr.Evaluate