in util-jackson/src/main/scala/com/twitter/util/jackson/JsonDiff.scala [71:196]
def diff[T](expected: T, actual: T): Option[Result] =
diff[T](expected, actual, None)
/**
* Computes the diff for two snippets of json both of expected type `T`.
* If a difference is detected a [[Result]] is returned otherwise a None.
*
* @param expected the expected json
* @param actual the actual or received json
* @param normalizeFn a function to apply to the actual json in order to "normalize" values.
*
* @return if a difference is detected a [[Result]]
* is returned otherwise a None.
*
* ==Usage==
* {{{
*
* private def normalize(jsonNode: JsonNode): JsonNode = jsonNode match {
* case on: ObjectNode => on.put("time", "1970-01-01T00:00:00Z")
* case _ => jsonNode
* }
*
* val expected = """{"foo": "bar", "time": ""1970-01-01T00:00:00Z"}"""
* val actual = ??? ({"foo": "bar", "time": ""2021-05-14T00:00:00Z"})
* val result: Option[JsonDiff.Result] = JsonDiff.diff(expected, actual, normalize)
* }}}
*/
def diff[T](
expected: T,
actual: T,
normalizeFn: JsonNode => JsonNode
): Option[Result] =
diff[T](expected, actual, Option(normalizeFn))
/**
* Asserts that the actual equals the expected. Will throw an [[AssertionError]] with details
* printed to [[System.out]].
*
* @param expected the expected json
* @param actual the actual or received json
*
* @throws AssertionError - when the expected does not match the actual.
*/
@throws[AssertionError]
def assertDiff[T](expected: T, actual: T): Unit =
assert[T](expected, actual, None, System.out)
/**
* Asserts that the actual equals the expected. Will throw an [[AssertionError]] with details
* printed to [[System.out]] using the given normalizeFn to normalize the actual contents.
*
* @param expected the expected json
* @param actual the actual or received json
* @param normalizeFn a function to apply to the actual json in order to "normalize" values.
*
* @throws AssertionError - when the expected does not match the actual.
*/
@throws[AssertionError]
def assertDiff[T](
expected: T,
actual: T,
normalizeFn: JsonNode => JsonNode
): Unit = assert[T](expected, actual, Option(normalizeFn), System.out)
/**
* Asserts that the actual equals the expected. Will throw an [[AssertionError]] with details
* printed to the given [[PrintStream]].
*
* @param expected the expected json
* @param actual the actual or received json
* @param p the [[PrintStream]] for reporting details
*
* @throws AssertionError - when the expected does not match the actual.
*/
@throws[AssertionError]
def assertDiff[T](expected: T, actual: T, p: PrintStream): Unit =
assert[T](expected, actual, None, p)
/**
* Asserts that the actual equals the expected. Will throw an [[AssertionError]] with details
* printed to the given [[PrintStream]] using the given normalizeFn to normalize the actual contents.
*
* @param expected the expected json
* @param actual the actual or received json
* @param p the [[PrintStream]] for reporting details
* @param normalizeFn a function to apply to the actual json in order to "normalize" values.
*
* @throws AssertionError - when the expected does not match the actual.
*/
@throws[AssertionError]
def assertDiff[T](
expected: T,
actual: T,
normalizeFn: JsonNode => JsonNode,
p: PrintStream
): Unit = assert[T](expected, actual, Option(normalizeFn), p)
/* Private */
private[this] def diff[T](
expected: T,
actual: T,
normalizer: Option[JsonNode => JsonNode]
): Option[Result] = {
val actualJson = jsonString(actual)
val expectedJson = jsonString(expected)
val actualJsonNode: JsonNode = {
val jsonNode = tryJsonNodeParse(actualJson)
normalizer match {
case Some(normalizerFn) => normalizerFn(jsonNode)
case _ => jsonNode
}
}
val expectedJsonNode: JsonNode = tryJsonNodeParse(expectedJson)
if (actualJsonNode != expectedJsonNode) {
val result = Result(
expected = expectedJsonNode,
expectedPrettyString = mapper.writePrettyString(expectedJsonNode),
actual = actualJsonNode,
actualPrettyString = mapper.writePrettyString(actualJsonNode)
)
Some(result)
} else None
}