Class IsActualEqualToExpectedJsonValueComparer

All Implemented Interfaces:
ValueComparer

public class IsActualEqualToExpectedJsonValueComparer extends ValueComparerTemplateBase
ValueComparer implementation that verifies the actual value is semantically equal to the expected value by parsing both as JSON and comparing the resulting document trees, instead of comparing their raw text.

Databases that store native JSON (for example MySQL JSON, PostgreSQL json/jsonb, or H2 JSON) commonly reformat the text on storage: insignificant whitespace is stripped and object keys may be reordered. A plain string or DataType.compare(Object, Object) comparison then fails even when the expected and actual documents are equivalent. This comparer instead treats JSON object member order as insignificant while still treating JSON array element order as significant, matching JSON's own equality semantics. Special case: if both values are null, they match.

Requires the optional jackson-databind dependency (the same one used by JsonDataSet) on the classpath. Deliberately not exposed as a constant on ValueComparers, because that class eagerly instantiates every constant it declares; doing so here would force the optional dependency onto every consumer of ValueComparers, not only those comparing JSON columns. Construct this comparer directly instead.

Since:
3.5.0
Author:
Jeff Jensen
  • Constructor Details

    • IsActualEqualToExpectedJsonValueComparer

      public IsActualEqualToExpectedJsonValueComparer()
  • Method Details

    • isExpected

      protected boolean isExpected(ITable expectedTable, ITable actualTable, int rowNum, String columnName, DataType dataType, Object expectedValue, Object actualValue) throws DatabaseUnitException
      Description copied from class: ValueComparerTemplateBase
      Determines whether the actual value compares as expected against the expected value.
      Specified by:
      isExpected in class ValueComparerTemplateBase
      Parameters:
      expectedTable - Table containing all expected results.
      actualTable - Table containing all actual results.
      rowNum - The current row number comparing.
      columnName - The name of the current column comparing.
      dataType - The DataType for the current column comparing.
      expectedValue - The current expected value for the column.
      actualValue - The current actual value for the column.
      Returns:
      true if comparing actual to expected is as expected.
      Throws:
      DatabaseUnitException - if the comparison cannot be performed.
    • isJsonEqual

      protected boolean isJsonEqual(int rowNum, String columnName, Object expectedValue, Object actualValue) throws DatabaseUnitException
      Returns whether the expected and actual values parse as structurally equal JSON documents.
      Parameters:
      rowNum - the current row number comparing, used only to identify a parse failure.
      columnName - the name of the current column comparing, used only to identify a parse failure.
      expectedValue - the expected value.
      actualValue - the actual value.
      Returns:
      true if both values parse as JSON and their document trees are equal.
      Throws:
      DatabaseUnitException - if either value cannot be converted to a string or parsed as JSON.
    • getFailPhrase

      protected String getFailPhrase()
      Description copied from class: ValueComparerTemplateBase
      Returns the text snippet for substitution in ValueComparerBase.BASE_FAIL_MSG.
      Specified by:
      getFailPhrase in class ValueComparerTemplateBase
      Returns:
      The text snippet for substitution in ValueComparerBase.BASE_FAIL_MSG.