~nmrao
I was following your suggestion in the stackoverflow post & running into an issue with the groovy script. I have ReadyAPI Pro, and like the original person who posted, was trying to compare 2 datasources, so tried using your script to solve the issue.
My test case consists of:
- Jdbc step called JDBCRequest (retrieving the BrandGroupID from the db which should match the data in step 2). Should this step really be the JDBC response parsed to a Datasource?
Sample of response data:
<Row rowNumber="1">
<BRANDGROUPID>2</BRANDGROUPID>
</Row>
<Row rowNumber="2">
<BRANDGROUPID>4</BRANDGROUPID>
</Row>
2. Json step called GetAllBrandGroups (returns many data points but the only one i'm interested in is the BrandGroupID ). Should this step really be the Json response parsed to a Datasource?
Sample of response data:
{
"UniqueID": "g6s6c7A072Lm2thUjFKdqk8uAE4E41107",
"IsUniqueIDEncrypted": true,
"BrandGroupID": 2,
"Brands": [ {
"Name": "WrapperUnitTest_171_17_44_17_87672_628",
"BrandUniqueID": "RLHlzWV072rBuAPIFfp2tE2I4QE4E41107",
"BrandID": 18685
}],
"Name": "WrapperUnitTest_171_17_44_17_884",
"Owner": "Wrapper.Unit.Test",
"OwnerType": 1
}
3. Groovy script step
Now here is the script (below) that I fashioned after yours.
Few questions :
- In the object model, do I have to specify all the json datapoint/tags, even though I'm only interested in the BrandGroupID one?
- I've added a log.info to see if the data is writing out on the jdbc, json - jdbc is not returning data. Is this syntax correct for a JDBC step: def jdbcResponse = context.expand('${JDBCRequest#Response}')
-When I try to run the script, I'm receiving: org.xml.sax.SAXParseException; Premature end of file. error at line: 42, which is the script line: def results = new XmlSlurper().parseText(jdbcResponse).
This is where I'm stuck.
Script:
/*
* Model object for comparing
*/
@groovy.transform.Canonical
class Model {
def BrandGroupID
//jdbc
def buildJdbcData(row) {
row.with {
BrandGroupID = BRANDGROUPID
}
}
//json
def buildJsonData(tagInfo){
BrandGroupID = tagInfo.@BrandGroupID
}
}
/*
* 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('${JDBCRequest#Response}')
log.info("jdbcResponse: " + jdbcResponse) //not returning any data
/*
* 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 jsonResponse = context.expand('${GetAllBrandGroups#Response}')
log.info("jsonResponse: " + jsonResponse) //returning data
//Parsing the jdbc and build the jdbc model object list
def results = new XmlSlurper().parseText(jdbcResponse)
def jdbcDataObjects = []
results.ResultSet.Row.each { row ->
jdbcDataObjects.add(new Model().buildJdbcData(row))
}
//Parsing the json and build the json model object list
def arrayOfTagInfo = new XmlSlurper().parseText(jsonResponse)
def jsonDataObjects = []
arrayOfTagInfo.TagInfo.each { tagInfo ->
jsonDataObjects.add(new Model().buildJsonData(tagInfo))
}
//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 has failed"