comparing json response with jdbc response
I am trying to compare json response to jdbc response. The script seems to be comparing only the last object. How do I get it to compare all the objects. Please see my code below
import groovy.json.*
import groovy.xml.*
import net.sf.json.JSON
import net.sf.json.JSONSerializer
import net.sf.json.xml.XMLSerializer
@groovy.transform.Canonical
class Model {
def name
def id
def buildJdbcData(row) {
row.with {
name = it."PLACES_MASTER.NAME"
id = it."PLACES_MASTER.PLACE_ID"
}
}
def buildJsonData(place){
name = place.name
id = place.id
}
}
def jdbcResponse = testRunner.testCase.testSteps["JDBC Request"].testRequest.response.contentAsString
def jsonResponse = testRunner.testCase.testSteps["JSON_Request"].testRequest.response.contentAsString
JSON json = JSONSerializer.toJSON( jsonResponse )
XMLSerializer xmlSerializer = new XMLSerializer()
xmlSerializer.setTypeHintsCompatibility( true )
String xml = xmlSerializer.write( json )
//log.info(xml)
//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))}
log.info(jdbcDataObjects)
//Parsing the json and build the json model object list
def arrayOfTagInfo = new XmlSlurper().parseText(xml)
def jsonDataObjects = []
arrayOfTagInfo.place.each { tagInfo ->
jsonDataObjects.add(new Model().buildJsonData(tagInfo))
}
log.info(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"
Here is my json response
{"place": {
"elevation": 0,
"id": "TAS_RS098",
"coordinate": {
"longitude": 147.0036,
"latitude": -42.3811
},
"gridcells": {"forecast": {
"x": 587,
"y": 52
}},
"name": "River Clyde at Bothwell",
"postcode": {
"name": "7030",
"description": "APSLEY"
},
"timezone": "Australia/Hobart",
"type": "river_site",
"location_hierarchy": {
"region": [
{
"aac": "AUS_FA001",
"description": "Australia"
},
{
"aac": "TAS_FA001",
"description": "Tasmania"
}
],
"public_district": {
"aac": "TAS_PW008",
"description": "Central Plateau"
},
"coast": {
"aac": null,
"description": null
},
"fire_district": {
"aac": "TAS_FW008",
"description": "Central Plateau"
},
"flood_watch_area": [ {
"aac": "TAS_FL022",
"description": "Derwent River"
}],
"metropolitan": [ {
"aac": "",
"description": ""
}],
"coast_warn": [ {
"aac": "",
"description": "",
"startPoint": null,
"endPoint": null
}],
"tidal_location": {
"aac": null,
"description": null
},
"nearest": {
"elevation": 350,
"id": "TAS_RO0029",
"coordinate": {
"longitude": 147.0036,
"latitude": -42.3811
},
"gridcells": {"forecast": {
"x": 587,
"y": 52
}},
"name": "River Clyde at Bothwell",
"state": "TAS",
"timezone": "Australia/Hobart",
"postcode": {
"name": "7030",
"description": "APSLEY"
},
"type": "river_obs_point",
"distance": 0,
"feature_code": "river_obs_point"
},
"nearby": [
{
"elevation": 337,
"id": "TAS_PT124",
"coordinate": {
"longitude": 147.000005,
"latitude": -42.380001
},
"gridcells": {"forecast": {
"x": 587,
"y": 52
}},
"name": "Bothwell",
"state": "TAS",
"timezone": "Australia/Hobart",
"postcode": {
"name": "7030",
"description": "APSLEY"
},
"type": "bom_poi",
"distance": 319.57495,
"feature_code": "bom_poi"
},
{
"elevation": 0,
"id": "232818",
"coordinate": {
"longitude": 147,
"latitude": -42.380001
},
"gridcells": {"forecast": {
"x": 587,
"y": 52
}},
"name": "Bothwell",
"state": "TAS",
"timezone": "Australia/Hobart",
"postcode": {
"name": "7030",
"description": "APSLEY"
},
"type": "place",
"distance": 319.9567,
"feature_code": "SUB"
},
{
"elevation": 0,
"id": "242455",
"coordinate": {
"longitude": 147,
"latitude": -42.380001
},
"gridcells": {"forecast": {
"x": 587,
"y": 52
}},
"name": "Queens Park",
"state": "TAS",
"timezone": "Australia/Hobart",
"postcode": {
"name": "7030",
"description": "APSLEY"
},
"type": "place",
"distance": 319.9567,
"feature_code": "RESV"
},
{
"elevation": 0,
"id": "35009",
"coordinate": {
"longitude": 147.008316,
"latitude": -42.384769
},
"gridcells": {"forecast": {
"x": 587,
"y": 52
}},
"name": "Bothwell",
"state": "TAS",
"timezone": "Australia/Hobart",
"postcode": {
"name": "7030",
"description": "APSLEY"
},
"type": "place",
"distance": 562.5672,
"feature_code": "POPL"
},
{
"elevation": 0,
"id": "248514",
"coordinate": {
"longitude": 147.039993,
"latitude": -42.380001
},
"gridcells": {"forecast": {
"x": 588,
"y": 52
}},
"name": "Pub Corner",
"state": "TAS",
"timezone": "Australia/Hobart",
"postcode": {
"name": "7030",
"description": "APSLEY"
},
"type": "place",
"distance": 2991.7427,
"feature_code": "LOCU"
}
],
"national_landscape": null,
"capital_area": {
"name": "6RTAS",
"description": "Rest of Tas."
},
"post_code": {
"name": "7030",
"description": "APSLEY"
},
"suburb": {
"name": "60063",
"description": "Bothwell"
},
"lga": {
"name": "61010",
"description": "Central Highlands (M) (Tas.)"
},
"state": {
"abbrev": "TAS",
"name": "TASMANIA",
"aac": "TAS_ST001"
},
"country": {
"aac": "AUS_CO001",
"name": "Australia"
}
},
"feature_code": "river_site"
}}
Here is my jdbc response
<Results>
<ResultSet fetchSize="0">
<Row rowNumber="1">
<PLACES_MASTER.DMP_ID>2259</PLACES_MASTER.DMP_ID>
<PLACES_MASTER.PLACE_ID>TAS_RS098</PLACES_MASTER.PLACE_ID>
<PLACES_MASTER.STATE_ABBREV>TAS</PLACES_MASTER.STATE_ABBREV>
<PLACES_MASTER.NAME>River Clyde at Bothwell</PLACES_MASTER.NAME>
<PLACES_MASTER.FEATURE_CO>river_site</PLACES_MASTER.FEATURE_CO>
<PLACES_MASTER.RECORD_ID>N/A</PLACES_MASTER.RECORD_ID>
<PLACES_MASTER.ELEVATION>0.0</PLACES_MASTER.ELEVATION>
<PLACES_MASTER.FORECAST_POINT>TAS_RS098</PLACES_MASTER.FORECAST_POINT>
<PLACES_MASTER.REGION>AUS_FA001|TAS_FA001</PLACES_MASTER.REGION>
<PLACES_MASTER.REGION_NAME>Australia|Tasmania</PLACES_MASTER.REGION_NAME>
<PLACES_MASTER.SUB_REGION/>
<PLACES_MASTER.SUB_REGION_NAME/>
<PLACES_MASTER.PUBLIC_DISTRICT>TAS_PW008</PLACES_MASTER.PUBLIC_DISTRICT>
<PLACES_MASTER.PUBLIC_DISTRICT_NAME>Central Plateau</PLACES_MASTER.PUBLIC_DISTRICT_NAME>
<PLACES_MASTER.FIRE_AREA>TAS_FW008</PLACES_MASTER.FIRE_AREA>
<PLACES_MASTER.FIRE_AREA_NAME>Central Plateau</PLACES_MASTER.FIRE_AREA_NAME>
<PLACES_MASTER.MARINE_WARNING_AREA/>
<PLACES_MASTER.MARINE_WARNING_AREA_NAME/>
<PLACES_MASTER.MARINE_COAST/>
<PLACES_MASTER.MARINE_COAST_NAME/>
<PLACES_MASTER.METROPOLITAN_AREA/>
<PLACES_MASTER.METROPOLITAN_AREA_NAME/>
<PLACES_MASTER.COASTAL_AREA/>
<PLACES_MASTER.COASTAL_AREA_NAME/>
<PLACES_MASTER.CATCHMENT>TAS_RC006|TAS_RC042</PLACES_MASTER.CATCHMENT>
<PLACES_MASTER.CATCHMENT_NAME>River Derwent|River Clyde</PLACES_MASTER.CATCHMENT_NAME>
<PLACES_MASTER.FLOOD_WATCH_AREA>TAS_FL022</PLACES_MASTER.FLOOD_WATCH_AREA>
<PLACES_MASTER.FLOOD_WATCH_AREA_NAME>Derwent River</PLACES_MASTER.FLOOD_WATCH_AREA_NAME>
<PLACES_MASTER.NATIONAL_LANDSCAPE/>
<PLACES_MASTER.FLAG_MAR>f</PLACES_MASTER.FLAG_MAR>
<PLACES_MASTER.FLAG_TERR>t</PLACES_MASTER.FLAG_TERR>
<PLACES_MASTER.SSC_CODE>60063</PLACES_MASTER.SSC_CODE>
<PLACES_MASTER.SSC_CODE_NAME>Bothwell</PLACES_MASTER.SSC_CODE_NAME>
<PLACES_MASTER.LGA_CODE>61010</PLACES_MASTER.LGA_CODE>
<PLACES_MASTER.LGA_CODE_NAME>Central Highlands (M) (Tas.)</PLACES_MASTER.LGA_CODE_NAME>
<PLACES_MASTER.GCC_CODE>6RTAS</PLACES_MASTER.GCC_CODE>
<PLACES_MASTER.GCC_CODE_NAME>Rest of Tas.</PLACES_MASTER.GCC_CODE_NAME>
<PLACES_MASTER.POST_CODE>7030</PLACES_MASTER.POST_CODE>
<PLACES_MASTER.POST_CODE_NAME>APSLEY</PLACES_MASTER.POST_CODE_NAME>
<PLACES_MASTER.TIDAL_LOCATION/>
<PLACES_MASTER.TIDAL_LOCATION_NAME/>
<PLACES_MASTER.SOURCE_TYPE>river_site</PLACES_MASTER.SOURCE_TYPE>
<PLACES_MASTER.TZ_CODE>Australia/Hobart</PLACES_MASTER.TZ_CODE>
<PLACES_MASTER.GEOM>0101000020E61000008048BF7D1D606240B84082E2C73045C0</PLACES_MASTER.GEOM>
<PLACES_MASTER.LONGITUDE>147.003600000000006</PLACES_MASTER.LONGITUDE>
<PLACES_MASTER.LATITUDE>-42.3811000000000035</PLACES_MASTER.LATITUDE>
<PLACES_MASTER.GRIDCELL_LONG_INDEX>587</PLACES_MASTER.GRIDCELL_LONG_INDEX>
<PLACES_MASTER.GRIDCELL_LAT_INDEX>52</PLACES_MASTER.GRIDCELL_LAT_INDEX>
<PLACES_MASTER.CAPABILITY>N/A</PLACES_MASTER.CAPABILITY>
<PLACES_MASTER.STATION_COMMON_NAME/>
<PLACES_MASTER.TIDAL_LOCATION_AREA/>
<PLACES_MASTER.TIDAL_LOCATION_AREA_NAME/>
<PLACES_MASTER.PRIMARY_SORT>101</PLACES_MASTER.PRIMARY_SORT>
<PLACES_MASTER.TERTIARY_SORT>6</PLACES_MASTER.TERTIARY_SORT>
<PLACES_MASTER.STATE_AREA>TAS_ST001</PLACES_MASTER.STATE_AREA>
<PLACES_MASTER.STATE_AREA_NAME>TASMANIA</PLACES_MASTER.STATE_AREA_NAME>
<PLACES_MASTER.COUNTRY_AREA>AUS_CO001</PLACES_MASTER.COUNTRY_AREA>
<PLACES_MASTER.COUNTRY_AREA_NAME>Australia</PLACES_MASTER.COUNTRY_AREA_NAME>
<PLACES_MASTER.LOCALITY_FCST/>
<PLACES_MASTER.LOCALITY_FCST_NAME/>
<PLACES_MASTER.PRECIS_FCST>TAS_PT124</PLACES_MASTER.PRECIS_FCST>
<PLACES_MASTER.PRECIS_FCST_NAME>Bothwell</PLACES_MASTER.PRECIS_FCST_NAME>
</Row>
</ResultSet>
</Results>