Compare JSON & JDBC response using groovy
I am trying to compare json response with jdbc response. Please find screenshot for the responses. I am using below groovy to compare value by value, but its only picking the latest attribute from json response instead of comparing all.
As of now I am doing it only for 2 attributes - Transaction Id & Date.
import com.eviware.soapui.support.XmlHolder
import groovy.xml.XmlUtil
import groovy.util.XmlSlurper
import groovy.json.JsonSlurper
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
/**
* Model object for comparing
*/
@groovy.transform.Canonical
class Model {
def transactionId
def transactionDate
//def storeId
//def marketingName
//def storeVisitToCity
//def transactionChanel
//def transactionValue
//def totalDiscount
/**
* this will acception jdbc row
* Param row
* @return
*/
def buildJdbcData(row) {
row.with {
transactionId = TRANSACTION_ID
transactionDate = TRANSACTION_DATE_KEY
//storeId = STORE_ID
//marketingName = MARKETING_NAME
//storeVisitToCity = STORE_VISIT_TO_CITY
//transactionChanel = TRANSACTION_CHANNEL
//transactionValue = TRANSACTION_VALUE
//totalDiscount = TOTAL_DISCOUNT
// log.info transactionId
}
}
/**
* this will accept the json TagInfo
* Param tagInfo
* @return
*/
def buildJsonData(tagInfo){
transactionId = tagInfo.transactionId
//log.info transactionId
transactionDate = tagInfo.transactionDate
// storeId = tagInfo.@storeId
// marketingName = tagInfo.@marketingName
// storeVisitToCity = tagInfo.@storeVisitToCity
// transactionChanel = tagInfo.@transactionChanel
// transactionValue = tagInfo.@transactionValue
// totalDiscount = tagInfo.@totalDiscount
}
}
/**
* Creating the jdbcResponse from the response received, using fixed value for testing
* If you want, you can assign the response received directly using below instead of current and make sure you replace the step name correctly
* def jdbcResponse = context.expand('${JdbcStepName#Response}')
*/
def jdbcResponse = context.expand( '${JDBC#ResponseAsXML}' )
//log.info jdbcResponse
//Parsing the jdbc and build the jdbc model object list
def results = new XmlSlurper().parseText(jdbcResponse)
//log.info results.ResultSet.Row[0]
def jdbcObjects = []
def jdbcDataObjects = []
results.ResultSet.Row.each { Row ->
jdbcDataObjects.add(new Model().buildJdbcData(Row))
}
log.info "JDBC Response -> "+ results
log.info "JDBC "+ jdbcDataObjects
/**
* Creating the jsonResponse from the response received, using fixed value for testing
* If you want, you can assign the response received directly using below instead of current and make sure you replace the step name correctly
* def jsonResponse = context.expand('${JsonStepName#Response}')
*/
def restResponse = context.expand('${Request#Response}')
//log.info restResponse
//Parsing the json and build the json model object list
def arrayOfTagInfo = new JsonSlurper().parseText(restResponse)
//log.info arrayOfTagInfo
def jsonDataObjects = []
log.info "JSON Response -> "+arrayOfTagInfo
arrayOfTagInfo.transactions.each{ tagInfo ->
jsonDataObjects.add(new Model().buildJsonData(tagInfo))
}
log.info "JSON "+jsonDataObjects
//sorting the Data before checking for equality
//jdbcDataObjects.sort()
//jsonDataObjects.sort()
if (jdbcDataObjects.size() != jsonDataObjects.size()) {
System.err.println("Jdbc resultset size is : ${jdbcDataObjects.size()} and Json result size is : ${jsonDataObjects.size()}")
}
assert jdbcDataObjects == jsonDataObjects, "Comparison of Jdbc and Json data is failed"
On running the step, it gives me the result , please see attached image.
it only compares the transaction_date and not transaction id.
- Try using utf-8 encoding. See below thread for setting it up.
https://stackoverflow.com/questions/44436512/how-to-handle-utf-8-character-encoding-in-http-log-in-soapui/44437807#44437807