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

前端 未结 1 1418
长情又很酷
长情又很酷 2021-01-25 06:03

I am very new to coding and am having some trouble when attempting to compare the results from a JDBC query to the results in an XML response.

I am using groovy rather

相关标签:
1条回答
  • 2021-01-25 06:47

    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

    0 讨论(0)
提交回复
热议问题