Matt Matt - 3 years ago 237
Groovy Question

SOAPUI picking up values from JDBC response into an array for assert

I have spent a couple of hours on this and after reading some articles I'm unsure if I'm taking the right approach. More on that question at the end.

In my previous post it was found that I was using an array when I shouldn't have been. My latest problem is the reverse of this in that I want to grab data from my jdbc response into an array to assert against a SOAP response which is also in an array.

I have the actual (EDITED) results from the SOAP response in array form working fine but the JDBC response is only grabbing the first value so assert fails.

Here is a snip from my JDBC response:

<Results>
<ResultSet fetchSize="64">
<Row rowNumber="1">
<TW070_VALIDATION.CODE>APP</TW070_VALIDATION.CODE>
<TW070_VALIDATION.VALID_DATA/>
</Row>
<Row rowNumber="2">
<TW070_VALIDATION.CODE>CHI</TW070_VALIDATION.CODE>
<TW070_VALIDATION.VALID_DATA>1</TW070_VALIDATION.VALID_DATA>
</Row>
<Row rowNumber="3">
<TW070_VALIDATION.CODE>DEN</TW070_VALIDATION.CODE>
<TW070_VALIDATION.VALID_DATA>1</TW070_VALIDATION.VALID_DATA>
</Row>
</ResultSet>
</Results>


I would like to pick up both values for each row returned. So in this example I am looking to get the following picked up to use for my expected result:

APP= ,CHI=1,DEN=1

My assert script currently looks like this:

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder( messageExchange.responseContent )
def pxml = new XmlSlurper().parseText(context.response)

//grab the expected result from jdbc response
def expectedCodes = context.expand( '${JDBC Request for expected results#ResponseAsXml#//*:TW070_VALIDATION.CODE}' )

//grab the actual result from the SOAP response
def actualCodes = pxml.'**'.findAll{it.name() == 'ReportAssessment'}.collectEntries{[(it.ReportAssessmentGroup.text()):it.Ranking.text()]}

log.info expectedCodes
log.info actualCodes

assert expectedCodes == actualCodes


EDIT: Adding in a picture of my test structure.enter image description here

EDIT2: Adding in a sample from my SOAP response (Step 4)

<ns2:ReportAssessment>
<ns2:ReportAssessmentGroup>APP</ns2:ReportAssessmentGroup>
<ns2:Ranking>0</ns2:Ranking>
<ns2:ReportAssessmentGroupDescription>APPLIANCES</ns2:ReportAssessmentGroupDescription>
</ns2:ReportAssessment>
<ns2:ReportAssessment>
<ns2:ReportAssessmentGroup>CHI</ns2:ReportAssessmentGroup>
<ns2:Ranking>1</ns2:Ranking>
<ns2:ReportAssessmentGroupDescription>CHIROPRACTIC</ns2:ReportAssessmentGroupDescription>
</ns2:ReportAssessment>
<ns2:ReportAssessment>
<ns2:ReportAssessmentGroup>DEN</ns2:ReportAssessmentGroup>
<ns2:Ranking>1</ns2:Ranking>
<ns2:ReportAssessmentGroupDescription>DENTAL</ns2:ReportAssessmentGroupDescription>


I have been reading about loop approaches so unsure if I can address this in the groovy script or if I need a different test structure to contain a loop.

Rao Rao
Answer Source

Here you go:

It appears that, you have an issue becuase the element name has .. So, it needs to be enclosed between the quotes as shown below:

First find all Rows and create map out of each row.

Here is the Script Assertion

//Define the expected map (key value pairs)
def expected = [APP: '', CHI: '1', DEN: '1']

//Read the xml and create map
def xml = new XmlSlurper().parseText(context.response)
def actual = xml.'**'.findAll{it.name() == 'Row'}.collectEntries{ [(it.'TW070_VALIDATION.CODE'.text()): it.'TW070_VALIDATION.VALID_DATA'.text() ]}
log.info actual
assert expected == actual

EDIT: based on the OP comments and chat

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder( messageExchange.responseContent )
def pxml = new XmlSlurper().parseText(context.response)

//grab the expected result from jdbc response
def jdbcResponse = context.expand( '${JDBC Request for expected results#ResponseAsXml}')
def xml = new XmlSlurper().parseText(jdbcResponse)
def expectedCodes = xml.'**'.findAll{it.name() == 'Row'}.collectEntries{ [(it.'TW070_VALIDATION.CODE'.text()):it.'TW070_VALIDATION.VALID_DATA'.text() ]}

//grab the actual result from the SOAP response
def actualCodes = pxml.'**'.findAll{it.name() == 'ReportAssessment'}.collectEntries{[(it.ReportAssessmentGroup.text()):it.Ranking.text()]}

log.info expectedCodes
log.info actualCodes

assert expectedCodes == actualCodes

You can quickly try the online demo

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download