Compare JDBC Response to an XML Response where number of nodes vary and order could change?

谁都会走 提交于 2019-12-02 07:02:57

Here is the script which I corrected to make it work.

The problems in the script you made and addressed as below -

  • xml elements are not read properly
  • made the methods static
  • Canonical annotation added to class so that it allows to comparable

Groovy Script

//Class to model the different sources of data and create object
//so that those are comparable
@groovy.transform.Canonical
class Model {
    String campaignSysKey
    String campaignName
    String startDate
    String endDate
    String campaignCode

    static def buildJdbcData(row) {
        def obj = new Model()
        row.with {
            obj.campaignSysKey = UPGRADETYPE
            obj.campaignName = SOURCEDESC
            obj.startDate = STARTDATE
            obj.endDate = ENDDATE
            obj.campaignCode = SOURCECODE
        }
        obj
    }

    static def buildXMLData(cmpgn) {
        def obj = new Model()
        obj.campaignSysKey = cmpgn.CampaignSysKey as String
        obj.campaignName = cmpgn.CampaignName as String
        obj.startDate = cmpgn.StartDate as String
        obj.endDate = cmpgn.EndDate as String
        obj.campaignCode = cmpgn.CampaignCode as String
        obj
    }
}

//Read the jdbc response from its step 
def jdbcResponse = context.expand('${Validation#ResponseAsXml}')

//Read the xml response from its step
def xmlResponse = context.expand('${OfferHistoryRequest#Response}')

//Read & Create objects for jdbc response
def results = new XmlSlurper().parseText(jdbcResponse)
def jdbcDataObjects = []
results.ResultSet.Row.each { row ->
    jdbcDataObjects.add(Model.buildJdbcData(row))
}

//Read & Create objects for xml response
def parsedXml = new XmlSlurper().parseText(xmlResponse)
def campaigns = parsedXml.'**'.findAll{it.name() == 'Campaign'}
def xmlDataObjects = []
campaigns.each { cmpgn ->
    xmlDataObjects.add(Model.buildXMLData(cmpgn))
}

//Log both responses
log.info "Jdbc response records: ${jdbcDataObjects.size()}"
log.info "Xml response records: ${xmlDataObjects.size()}"


if (jdbcDataObjects.size() != xmlDataObjects.size()) {
    log.info("Jdbc resultset size is : ${jdbcDataObjects.size()} and XML result size is : ${xmlDataObjects.size()}") 
}

//Check if both object sizes are equal before comparing one to one
assert jdbcDataObjects.size() == xmlDataObjects.size(), "Both responses have not matched element count"

//Log both the objects
log.info jdbcDataObjects.toString()
log.info xmlDataObjects.toString()

//Compare now both the responses and show the difference
if (jdbcDataObjects != xmlDataObjects){
    log.info "Both responses do not match"
    for(int index=0;index<jdbcDataObjects.size();index++){
        assert jdbcDataObjects[index] == xmlDataObjects[index], "Responses @ ${index+1} position do not match"
    }
} else {
    log.info "Both responses matches"
}

By the way, the responses you posted have difference.

If you want to quickly check online demo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!