Skip to content

CAMEL-22865: Add JSON comparison with unordered elements to MockValueBuilder#20961

Merged
gnodet merged 1 commit intoapache:mainfrom
gnodet:feature/CAMEL-22865
Jan 23, 2026
Merged

CAMEL-22865: Add JSON comparison with unordered elements to MockValueBuilder#20961
gnodet merged 1 commit intoapache:mainfrom
gnodet:feature/CAMEL-22865

Conversation

@gnodet
Copy link
Contributor

@gnodet gnodet commented Jan 21, 2026

Fixes CAMEL-22865

This commit implements a new feature for the camel-mock component that allows JSON comparison while ignoring element order in arrays and objects. This is particularly useful when testing Camel routes with JSON payloads where the order of elements can vary.

Key changes:

  • Added jsonEquals(Object expected) method to MockValueBuilder that defaults to ignoring array element order
  • Added jsonEquals(Object expected, boolean ignoreOrder) method for more control over comparison behavior
  • Implemented deep JSON comparison using camel-util-json library
  • Supports various input types: String, byte[], InputStream, JsonObject, JsonArray
  • Handles nested JSON structures (objects and arrays)
  • Provides semantic comparison that ignores key order in objects
  • Can optionally enforce strict array element order when needed

Implementation details:

  • Uses Jsoner.deserialize() from camel-util-json for JSON parsing
  • Implements recursive comparison algorithm for nested structures
  • For unordered array comparison, uses a matching algorithm that finds corresponding elements
  • Handles primitives, nulls, booleans, numbers, and strings correctly

…Builder

This commit implements a new feature for the camel-mock component that allows
JSON comparison while ignoring element order in arrays and objects. This is
particularly useful when testing Camel routes with JSON payloads where the
order of elements can vary.

Key changes:
- Added jsonEquals(Object expected) method to MockValueBuilder that defaults
  to ignoring array element order
- Added jsonEquals(Object expected, boolean ignoreOrder) method for more
  control over comparison behavior
- Implemented deep JSON comparison using camel-util-json library
- Supports various input types: String, byte[], InputStream, JsonObject,
  JsonArray
- Handles nested JSON structures (objects and arrays)
- Provides semantic comparison that ignores key order in objects
- Can optionally enforce strict array element order when needed

Implementation details:
- Uses Jsoner.deserialize() from camel-util-json for JSON parsing
- Implements recursive comparison algorithm for nested structures
- For unordered array comparison, uses a matching algorithm that finds
  corresponding elements
- Handles primitives, nulls, booleans, numbers, and strings correctly

Testing:
- Added comprehensive test suite with 20 test cases covering:
  * JSON objects with different key orders
  * JSON arrays with different element orders (both strict and lenient)
  * Nested JSON structures
  * Mixed type arrays
  * Edge cases (empty objects/arrays, nulls, whitespace)
  * Error cases (mismatches, different structures)
  * Different input types (String, byte[])

All tests pass successfully.
@github-actions
Copy link
Contributor

🌟 Thank you for your contribution to the Apache Camel project! 🌟

🤖 CI automation will test this PR automatically.

🐫 Apache Camel Committers, please review the following items:

  • First-time contributors require MANUAL approval for the GitHub Actions to run

  • You can use the command /component-test (camel-)component-name1 (camel-)component-name2.. to request a test from the test bot.

  • You can label PRs using build-all, build-dependents, skip-tests and test-dependents to fine-tune the checks executed by this PR.

  • Build and test logs are available in the Summary page. Only Apache Camel committers have access to the summary.

  • ⚠️ Be careful when sharing logs. Review their contents before sharing them publicly.

@davsclaus
Copy link
Contributor

That is a great idea to use camel-tooling-json and the JSooner parser we have so this is included out of the box in camel-mock

@gnodet gnodet marked this pull request as ready for review January 22, 2026 15:59
@gnodet gnodet merged commit 5b03b03 into apache:main Jan 23, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants