Forum Discussion

Gkm's avatar
Gkm
Contributor
8 years ago

how to compare JSON response with JDBC request, when JDBC request contains CDATA string

 

import com.eviware.soapui.support.XmlHolder
import groovy.json.JsonSlurper

def JSONResponse = ''' "{\"Unit\":{\"Profile\":12,\"Devices\":[{\"Id\":1,\"DetailJSON\":\"[{\\\"Name\\\":\\\"ALPR_1\\\",\\\"Profile\\\":{\\\"ID\\\":1,\\\"BitRate\\\":0},\\\"Index\\\":-1}]\",\"PIPDetailJSON\":[\"[{\\\"Name\\\":\\\"ALPR_2\\\",\\\"Profile\\\":{\\\"ID\\\":2,\\\"BitRate\\\":0,\\\"Mode\\\":\\\"pip\\\"}]\"]}],\"UnitID\":12}" '''

// replace "(starting&ending), [](starting&ending) and \ with space from response
def TrimResponse =JSONResponse.replaceAll('^\"|\"$','').replaceAll('\\\\','').replaceAll('\"\\[','')replaceAll('\\]\"','')
def json = new JsonSlurper().parseText(TrimResponse)

def response = ''' <Results>
  <ResultSet fetchSize="128">
    <Row rowNumber="1">
      <CONFIGURATIONDETAILXML> <![CDATA[
		<ROOT>
		  <Profile>12</Profile>
		  <BitRate>3072</BitRate>
	      <VideoDevice>
			<Id>1</Id>
			<Devices>[{"Name":"ALPR_1","Profile":{"ID":1,"BitRate":0},"Index":-1}]</Devices>
			<PIPDevices>
			  <Devices>[{"Name":"ALPR_2","Profile":{"ID":2,"BitRate":0},"Mode":"pip"}]</Devices>
			</PIPDevices>
		  </VideoDevice>
		</ROOT> ]]>
      </CONFIGURATIONDETAILXML>			
        <UNITID>12</UNITID>
        </Row>
    </ResultSet>
</Results> '''

def holder = new XmlHolder(response)

//Get ConfigurationDetailXML Node value using Xpath from holder object, which will retrive CDATA
def configurationXmlCdata =  holder.getNodeValue('//*:CONFIGURATIONDETAILXML')

def cdataHolder = new XmlHolder(configurationXmlCdata)

I'm able to compare each node like this

 

def id = json.Unit.Devices.Id.toString().replaceAll('\\[','').replaceAll('\\]','')
def jsonpip = json.Unit.Devices.PIPDetailJSON.toString().replaceAll('\\[','').replaceAll('\\]','')

def dbid = cdataHolder.getNodeValue('//ROOT/VideoDevice/Id')
def pip = cdataHolder.getNodeValue('//ROOT/VideoDevice/PIPDevices/Devices')
def dbpip = new JsonSlurper().parseText(pip).toString().replaceAll('\\[','').replaceAll('\\]','')

def error = []
if (id != dbid) {error.add("id: $id is not equal to dbid: $dbid ")}
if (jsonpip != dbpip) {error.add("jsonpip: $jsonpip is not equal to dbpip: $dbpip ")}

assert error.size() == 0," $error " 
if(error.size() == 0){ log.info "Script Passed" } 

But it is not a good way as I have so many values to compare :(

 

18 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3
    Is the actual problem with comparison or showing differences if any?
    • Gkm's avatar
      Gkm
      Contributor

      First compare and if any difference then print the difference, but I don't want to do this node by node .