Madhusudan
Madhusudan

Reputation: 4815

Copying values from SOAP UI request into SOAP UI response using groovy

I am trying to copy some values from SOAP request XML into SOAP response XML file.

Consider following request and response XML for reference:

 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ehit="http://www.calheers.ca.gov/EHITSAWSInterfaceMessageSchema" xmlns:ehit1="http://www.calheers.ca.gov/EHITSAWSInterfaceCommonSchema" xmlns:ns="http://niem.gov/niem/structures/2.0" xmlns:ns2="http://niem.gov/niem/niem-core/2.0" xmlns:ns1="http://niem.gov/niem/domains/screening/2.1">
  <soapenv:Header/>
  <soapenv:Body>
  <ehit:DeterminationRequest>
     <ehit:MessageInformation>
        <ehit1:MessageID ns:id="?" ns:metadata="?" ns:linkMetadata="?">?   </ehit1:MessageID>
        <ehit1:MessageTimeStamp ns:id="?" ns:metadata="?" ns:linkMetadata="?">?</ehit1:MessageTimeStamp>
        <ehit1:SendingSystem ns:id="?" ns:metadata="?" ns:linkMetadata="?">?</ehit1:SendingSystem>
        <ehit1:ReceivingSystem ns:id="?" ns:metadata="?" ns:linkMetadata="?">?</ehit1:ReceivingSystem>
        <ehit1:FipsCountyCode ns:id="?" ns:metadata="?" ns:linkMetadata="?">?</ehit1:FipsCountyCode>
        <!--Optional:-->
        <ehit1:TracerID ns:id="?" ns:metadata="?" ns:linkMetadata="?">?</ehit1:TracerID>
     </ehit:MessageInformation>
     .......continued......

And Response XML is:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ehit="http://www.calheers.ca.gov/EHITSAWSInterfaceMessageSchema" xmlns:ehit1="http://www.calheers.ca.gov/EHITSAWSInterfaceCommonSchema" xmlns:ns="http://niem.gov/niem/structures/2.0">
<soapenv:Header/>
<soapenv:Body>
  <ehit:DeterminationResponse>
     <ehit1:MessageID ns:id=${id} ns:metadata=${metadata} ns:linkMetadata="?">?</ehit1:MessageID>
     <ehit1:AckTimeStamp ns:id="?" ns:metadata="?" ns:linkMetadata="?">?</ehit1:AckTimeStamp>
     <!--Optional:-->
     <ehit1:StatusCode ns:id="?" ns:metadata="?" ns:linkMetadata="?">?</ehit1:StatusCode>
     <!--Optional:-->
     <ehit1:ErrorCode ns:id="?" ns:metadata="?" ns:linkMetadata="?">?</ehit1:ErrorCode>
     <!--Optional:-->
     <ehit1:ErrorMessage ns:id="?" ns:metadata="?" ns:linkMetadata="?">?</ehit1:ErrorMessage>
  </ehit:DeterminationResponse>
</soapenv:Body>
</soapenv:Envelope>

I created two dynamic vars here ${id} and ${metadata}.

I tried to copy id and metadata attribute of .Now to set these values from request into response I am using following script:

def req = new XmlSlurper().parseText(mockRequest.requestContent)
requestContext.id = req.Body.DeterminationRequest.MessageInformation.MessageID.@id;
requestContext.metadata = req.Body.DeterminationRequest.MessageInformation.MessageID.@metadata;

But it's not returning any value. Can someone help me to figure it out what I am doing wrong here?

Also if I want to copy many (consider 50+) values from request into response then is there any simple way instead of creating 50+ variables? Because in my case I have to copy all values from request into response message.

Upvotes: 1

Views: 1365

Answers (1)

albciff
albciff

Reputation: 18507

I think that the problem is that you're not really getting the value of your id and metadata attributes since its are from a http://niem.gov/niem/structures/2.0 namespace ns:id and ns:metadata. See this related question on SO.

So try to log your expression:

log.info req.Body.DeterminationRequest.MessageInformation.MessageID.@id

And it will return empty. This is probably why you don't see the expected values in mock response.

The solution is to use the namespace to get back your attribute value, change your code for something like:

import groovy.xml.Namespace

// define namespace
def ns = new Namespace('http://niem.gov/niem/structures/2.0', 'ns')
def req = new XmlSlurper(false,true).parseText(mockRequest.requestContent)

context.id =  req.Body.DeterminationRequest.MessageInformation.MessageID[0].attributes()[ns.id.toString()]
context.metadata = req.Body.DeterminationRequest.MessageInformation.MessageID[0].attributes()[ns.metadata.toString()]

A bit more maintainable not writing each time the full path:

import groovy.xml.Namespace

// define namespace
def ns = new Namespace('http://niem.gov/niem/structures/2.0', 'ns')
def req = new XmlSlurper(false,true).parseText(mockRequest.requestContent)

def attrs = req.Body.DeterminationRequest.MessageInformation.MessageID[0].attributes()
context.id =  attrs[ns.id.toString()]
context.metadata = attrs[ns.metadata.toString()]
context.linkMetadata = attrs[ns.linkMetadata.toString()]

Upvotes: 0

Related Questions