[SOLVED] How to compare json response with jdbc response using groovy

Issue

This Content is from Stack Overflow. Question asked by Bob Jones

I know this question has been asked earlier. The previous response was comparison of two xml responses.
Here I need to compare json response which with jdbc’s xml response. I have made necessary changes as per my json response and jdbc response. I am still getting error that the assertion has failed. I have been struggling with this for couple of weeks. Please advise.

json response

   "meta": {"api_response_datetime_utc": "2022-09-19T04:11:42.013964Z"},
   "warnings":    [
            {
         "id": "ABC23879",
         "issue_datetime_utc": "2022-03-07T19:21:31Z",
         "expires_datetime_utc": "2025-03-14T19:21:24Z",
         "title": "",
         "sub_title": "",
         "area_summary":          [
            "North Central",
            "North East",
            "Central",
            "West and South Gippsland and East Gippsland forecast districts"
         ],
         "phenomena_summary": null,
         "severity_code": ["STD"],
         "issue_type": "Update",
         "type": "WFR",
         "area_state_code": "VIC",
         "link_details": null
      },
            {
         "id": "BAC22000",
         "issue_datetime_utc": "2022-03-07T19:21:29Z",
         "expires_datetime_utc": "2025-03-14T19:21:24Z",
         "title": "Fire Weather",
         "sub_title": "Mallee, Northern Country, North Central and North East forecast districts",
         "area_summary":          [
            "Mallee",
            "Northern Country",
            "North Central and North East"
         ],
         "phenomena_summary": null,
         "severity_code": ["SEV"],
         "issue_type": "Alert",
         "type": "WFW",
         "area_state_code": "VIC",
         "link_details": null
      }
   ]
}

jdbc response

<Results>
    <ResultSet fetchSize="0">
        <Row rowNumber="1">
            <WARNINGS_VW.PRODUCT_IDENT_NUMBER>ABC23879</WARNINGS_VW.PRODUCT_IDENT_NUMBER>
            <WARNINGS_VW.DMP_ID>34434</WARNINGS_VW.DMP_ID>
            <WARNINGS_VW.WARNING_TYPE>WFW</WARNINGS_VW.WARNING_TYPE>
            <WARNINGS_VW.GEOCODE_TYPE>aac:fire_district</WARNINGS_VW.GEOCODE_TYPE>
            <WARNINGS_VW.GEOCODE_CODE>VIC_FW008</WARNINGS_VW.GEOCODE_CODE>
            <WARNINGS_VW.AREA_SUMMARY>Mallee, Northern Country, North Central and North East</WARNINGS_VW.AREA_SUMMARY>
            <WARNINGS_VW.WARNING_AREA_TYPE>geocode</WARNINGS_VW.WARNING_AREA_TYPE>
            <WARNINGS_VW.SEARCH_POLYGON/>
        </Row>
        <Row rowNumber="2">
            <WARNINGS_VW.PRODUCT_IDENT_NUMBER>BAC23000</WARNINGS_VW.PRODUCT_IDENT_NUMBER>
            <WARNINGS_VW.DMP_ID>34439</WARNINGS_VW.DMP_ID>
            <WARNINGS_VW.WARNING_TYPE>WFR</WARNINGS_VW.WARNING_TYPE>
            <WARNINGS_VW.GEOCODE_TYPE>aac:public_district</WARNINGS_VW.GEOCODE_TYPE>
            <WARNINGS_VW.GEOCODE_CODE>VIC_PW008</WARNINGS_VW.GEOCODE_CODE>
            <WARNINGS_VW.AREA_SUMMARY>North Central, North East, Central, West and South Gippsland and East Gippsland forecast districts</WARNINGS_VW.AREA_SUMMARY>
            <WARNINGS_VW.WARNING_AREA_TYPE>geocode</WARNINGS_VW.WARNING_AREA_TYPE>
            <WARNINGS_VW.SEARCH_POLYGON/>
        </Row>
    </ResultSet>
</Results>

My Groovy Script

import groovy.json.*
import groovy.xml.*

@groovy.transform.Canonical
class Model {
    def id
    def warningType
    def geocodeType
    def geocodeCode
    def areaSummary
    def warningAreaType
    def searchPolygon    

  
    def buildJdbcData(row) {
        row.with {
            id = WARNINGS_VW.DMP_ID
            warningType= WARNINGS_VW.WARNING_TYPE
          
           
        }
    }
  
  def buildJsonData(warnings){
       
    id = warnings.id
    warningType = warnings.type
    
  
    }
}
 def jdbcResponse = testRunner.testCase.testSteps["JDBC Request"].testRequest.response.contentAsString

 
 def jsonResponse = testRunner.testCase.testSteps["List1-Request1"].testRequest.response.contentAsString



//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 JsonSlurper().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()



assert jdbcDataObjects == jsonDataObjects, "Comparison of Jdbc XML data and Json data has failed"



Solution

I’ve modified how the 2 x data sources are being fed into the app but the following works for me.

import groovy.json.*
import groovy.transform.Canonical
import groovy.xml.*

def jsonTxt =
        '''{ "meta": {"api_response_datetime_utc": "2022-09-19T04:11:42.013964Z"},
   "warnings":    [
            {
         "id": "ABC23879",
         "issue_datetime_utc": "2022-03-07T19:21:31Z",
         "expires_datetime_utc": "2025-03-14T19:21:24Z",
         "title": "",
         "sub_title": "",
         "area_summary":          [
            "North Central",
            "North East",
            "Central",
            "West and South Gippsland and East Gippsland forecast districts"
         ],
         "phenomena_summary": null,
         "severity_code": ["STD"],
         "issue_type": "Update",
         "type": "WFR",
         "area_state_code": "VIC",
         "link_details": null
      },
            {
         "id": "BAC22000",
         "issue_datetime_utc": "2022-03-07T19:21:29Z",
         "expires_datetime_utc": "2025-03-14T19:21:24Z",
         "title": "Fire Weather",
         "sub_title": "Mallee, Northern Country, North Central and North East forecast districts",
         "area_summary":          [
            "Mallee",
            "Northern Country",
            "North Central and North East"
         ],
         "phenomena_summary": null,
         "severity_code": ["SEV"],
         "issue_type": "Alert",
         "type": "WFW",
         "area_state_code": "VIC",
         "link_details": null
      }
   ]
}'''

def jdbcTxt =
        '''<Results>
    <ResultSet fetchSize="0">
        <Row rowNumber="1">
            <WARNINGS_VW.PRODUCT_IDENT_NUMBER>ABC23879</WARNINGS_VW.PRODUCT_IDENT_NUMBER>
            <WARNINGS_VW.DMP_ID>34434</WARNINGS_VW.DMP_ID>
            <WARNINGS_VW.WARNING_TYPE>WFW</WARNINGS_VW.WARNING_TYPE>
            <WARNINGS_VW.GEOCODE_TYPE>aac:fire_district</WARNINGS_VW.GEOCODE_TYPE>
            <WARNINGS_VW.GEOCODE_CODE>VIC_FW008</WARNINGS_VW.GEOCODE_CODE>
            <WARNINGS_VW.AREA_SUMMARY>Mallee, Northern Country, North Central and North East</WARNINGS_VW.AREA_SUMMARY>
            <WARNINGS_VW.WARNING_AREA_TYPE>geocode</WARNINGS_VW.WARNING_AREA_TYPE>
            <WARNINGS_VW.SEARCH_POLYGON/>
        </Row>
        <Row rowNumber="2">
            <WARNINGS_VW.PRODUCT_IDENT_NUMBER>BAC23000</WARNINGS_VW.PRODUCT_IDENT_NUMBER>
            <WARNINGS_VW.DMP_ID>34439</WARNINGS_VW.DMP_ID>
            <WARNINGS_VW.WARNING_TYPE>WFR</WARNINGS_VW.WARNING_TYPE>
            <WARNINGS_VW.GEOCODE_TYPE>aac:public_district</WARNINGS_VW.GEOCODE_TYPE>
            <WARNINGS_VW.GEOCODE_CODE>VIC_PW008</WARNINGS_VW.GEOCODE_CODE>
            <WARNINGS_VW.AREA_SUMMARY>North Central, North East, Central, West and South Gippsland and East Gippsland forecast districts</WARNINGS_VW.AREA_SUMMARY>
            <WARNINGS_VW.WARNING_AREA_TYPE>geocode</WARNINGS_VW.WARNING_AREA_TYPE>
            <WARNINGS_VW.SEARCH_POLYGON/>
        </Row>
    </ResultSet>
</Results>'''



@Canonical
class Model {
    def id
    def warningType
    def geocodeType
    def geocodeCode
    def areaSummary
    def warningAreaType
    def searchPolygon


    def buildJdbcData(row) {
        row.with {
            id = it."WARNINGS_VW.PRODUCT_IDENT_NUMBER"
            warningType= it."WARNINGS_VW.WARNING_TYPE"
        }
    }

    def buildJsonData(warnings){
        id = warnings.id
        warningType = warnings.type
    }
}
//def jdbcResponse = testRunner.testCase.testSteps["JDBC Request"].testRequest.response.contentAsString


//def jsonResponse = testRunner.testCase.testSteps["List1-Request1"].testRequest.response.contentAsString



//Parsing the jdbc and build the jdbc model object list
def results = new XmlSlurper().parseText(jdbcTxt)
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 JsonSlurper().parseText(jsonTxt)
def jsonDataObjects = []
arrayOfTagInfo.warnings.each { tagInfo ->
    jsonDataObjects.add(new Model().buildJsonData(tagInfo))

}

//sorting the Data before checking for equality
jdbcDataObjects.sort()
jsonDataObjects.sort()

assert jdbcDataObjects == jsonDataObjects, "Comparison of Jdbc XML data and Json data has failed"


This Question was asked in StackOverflow by Bob Jones and Answered by Mike W It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?