Forum Discussion

azhar25's avatar
azhar25
New Contributor
6 years ago

Re: Compare JSON & JDBC response using groovy

Hi

I am having the similar issue, but script returns empty result for the model object. I am not sure if I my class model is defined correctly. I am trying to compare some json response values with jdbc response values. 

 

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 id
def createdTime
def updatedTime

/**
* this will accept the jdbc row
* Param row
* @return
*/
Model buildJdbcData(row) {
row.with {
id = row.GDE_REQUEST_ID
createdTime = row.CREATED_TIMESTAMP
updatedTime = row.UPDATED_TIMESTAMP
}
}

/**
* this will accept the json TagInfo
* Param tagInfo
* @return
*/
def buildJsonData(tagInfo){
id = tagInfo.eligibilityId
createdTime = tagInfo.eligibilityRequestCreated
updatedTime = tagInfo.eligibilityRequestUpdated
}
}

/**
* def jdbcResponse = context.expand('${JdbcStepName#Response}')
*/
def jdbcResponse = context.expand( '${JDBC Request1#ResponseAsXML#//Results/ResultSet/Row}')

//log.info jdbcResponse

/**
* 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 jsonResponse = context.expand( '${LocationList 1 - Request 1#ResponseAsXML}')
//log.info jsonResponse


//Parsing the jdbc and build the jdbc model object list
def results = new XmlSlurper().parseText(jdbcResponse)
def jdbcObjects = []
def jdbcDataObjects = []
results.ResultSet.Row.each { Row->
jdbcDataObjects.add(new Model().buildJdbcData(Row))
}
log.info "JDBC Response -> "+ results
log.info "JDBC "+ jdbcDataObjects

//Parsing the json and build the json model object list
def arrayOfTagInfo = new XmlSlurper().parseText(context.responseAsXml)

//def arrayOfTagInfo = new JsonSlurper().parseText(jsonResponse)
def jsonDataObjects = []
arrayOfTagInfo.TagInfo.each { tagInfo ->
jsonDataObjects.add(new Model().buildJsonData(tagInfo))
}
log.info arrayOfTagInfo
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"