All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.twitter.finatra.json.utils.JsonDiffResult.scala Maven / Gradle / Ivy

There is a newer version: 2.1.6
Show newest version
package com.twitter.finatra.json.utils

import com.fasterxml.jackson.databind.JsonNode
import com.twitter.finatra.json.FinatraObjectMapper
import org.apache.commons.lang.StringUtils

object JsonDiffResult {

  def create(
    mapper: FinatraObjectMapper,
    expected: JsonNode,
    received: JsonNode): JsonDiffResult = {

    JsonDiffResult(
      expected = expected,
      expectedPrettyString = mapper.writePrettyString(expected),
      received = received,
      receivedPrettyString = mapper.writePrettyString(received))
  }
}

case class JsonDiffResult(
  expected: JsonNode,
  expectedPrettyString: String,
  received: JsonNode,
  receivedPrettyString: String) {

  lazy val toMessage: String = {
    val expectedJsonSorted = JsonDiffUtil.sortedString(expected)
    val receivedJsonSorted = JsonDiffUtil.sortedString(received)

    val expectedHeader = "Expected: "
    val diffStartIdx = StringUtils.indexOfDifference(receivedJsonSorted, expectedJsonSorted)

    val message = new StringBuilder
    message.append(" " * (expectedHeader.length + diffStartIdx) + "*\n")
    message.append(s"Received: $receivedJsonSorted\n")
    message.append(expectedHeader + expectedJsonSorted)
    message.toString()
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy