0
votes

I'm using SoapUI to mock a service, and I'm returning different stored XMl's based on the request using a groovy script.

import com.eviware.soapui.support.GroovyUtils
import groovy.xml.XmlUtil

def groovyUtils = new GroovyUtils(context)

def xmlParser = new XmlParser()
def responseContent

def requestXmlHolder = groovyUtils.getXmlHolder(mockRequest.getRequestContent())
requestXmlHolder.declareNamespace("v01", "http://www.zoot.com/data_type/ZE_Messaging/v01")
def email = (requestXmlHolder.getNodeValue("//v01:Request[1]/v01:Email[1]"))

def responsePath = "C:/MockService/Responses"

switch(email){
    case ~/(?i).*acceptall.*/:
        responseContent = xmlParser.parse( responsePath + "/acceptall-response.xml" )
        break
    case ~/(?i).*acceptspl.*/:
        responseContent = xmlParser.parse( responsePath + "/acceptspl-response.xml" )
        break
    case ~/(?i).*acceptmpl.*/:
        responseContent = xmlParser.parse( responsePath  + "/acceptmpl-response.xml" )
        break
    case ~/(?i).*decline.*/:
        responseContent = xmlParser.parse( responsePath + "/decline-response.xml" )
        break
    default:
        responseContent = xmlParser.parse( responsePath + "/custom-response.xml" )
        break
}

context.content = XmlUtil.serialize(responseContent)

This works fine, but now I'm trying to update one of the nodes of this response before, more precisely I'd like to add the current date in that script as there's a node in the xml which looks like

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v01="http://www.whatever.com/data_type/ZE_Messaging/v01">
   <soapenv:Header/>
   <soapenv:Body>
      <v01:Response>
         <v01:RequestID>eeee</v01:RequestID>
         <v01:TransactionID>?</v01:TransactionID>
         <v01:CostumerID>?</v01:CostumerID>
         <v01:TransactionDateTime>?</v01:TransactionDateTime>
         <v01:FirstName>?</v01:FirstName>
         <v01:MiddleName>?</v01:MiddleName>
         <v01:Name>?</v01:Name>
         <v01:MaidenName>?</v01:MaidenName>
         <v01:Gender>?</v01:Gender>
         <v01:DateOfBirth>?</v01:DateOfBirth>
         <v01:Decision>ACCEPTALL</v01:Decision>
         <v01:DecisionText>?</v01:DecisionText>
         <v01:DecisionReasonCodes>?</v01:DecisionReasonCodes>
         <v01:ErrorCode>?</v01:ErrorCode>
         <v01:ErrorDescription>?</v01:ErrorDescription>
         <v01:AdditionalDetails>
            <v01:Data category="?" attribute="?">?</v01:Data>
         </v01:AdditionalDetails>
      </v01:Response>
   </soapenv:Body>
</soapenv:Envelope>

In the script if I do this, just before setting the context.content

def soapenv =  new groovy.xml.Namespace("http://schemas.xmlsoap.org/soap/envelope/")
def v01 = new groovy.xml.Namespace("http://www.whatever.com/data_type/ZE_Messaging/v01")

log.info responseContent[soapenv.Body][v01.Response][v01.RequestID].text()

I will get the 'eeee'

So basically what I need is how to set that to a different thing from what I getting from the file itself before setting that context.content

1
It's unclear what You'd like to do. - Opal
@Opal, I would like to update the xml I get from my file systeme, and put the current date in the TransactionDateTime before. I update the question - mitomed
Can You please add full response, e.g. with namespaces, that is a valid XML file? - Opal
Yes, @Opal, it's a valid XML file that I add to the question - mitomed

1 Answers

0
votes

Ok, just in case this messy question could be of some help. for updating for example RequestID that comes in the xml retrieved from the file system set to 'eeee' to 'iiii' the script should be this one

import com.eviware.soapui.support.GroovyUtils
import groovy.xml.XmlUtil

def groovyUtils = new GroovyUtils(context)

def xmlParser = new XmlParser()
def responseContent

def requestXmlHolder = groovyUtils.getXmlHolder(mockRequest.getRequestContent())
requestXmlHolder.declareNamespace("v01", "http://www.whatever.com/data_type/ZE_Messaging/v01")
def email = (requestXmlHolder.getNodeValue("//v01:Request[1]/v01:Email[1]"))

def responsePath = "C:/inetpub/ZootMock/Responses"

switch(email){
    case ~/(?i).*acceptall.*/:
        responseContent = xmlParser.parse( responsePath + "/acceptall-response.xml" )
        break
    case ~/(?i).*acceptspl.*/:
        responseContent = xmlParser.parse( responsePath + "/acceptspl-response.xml" )
        break
    case ~/(?i).*acceptmpl.*/:
        responseContent = xmlParser.parse( responsePath  + "/acceptmpl-response.xml" )
        break
    case ~/(?i).*decline.*/:
        responseContent = xmlParser.parse( responsePath + "/decline-response.xml" )
        break
    default:
        responseContent = xmlParser.parse( responsePath + "/custom-response.xml" )
        break
}
responseContent = xmlParser.parse( responsePath + "/acceptall-response.xml" )

def soapenv =  new groovy.xml.Namespace("http://schemas.xmlsoap.org/soap/envelope/")
def v01 = new groovy.xml.Namespace("http://www.whatever.com/data_type/ZE_Messaging/v01")

responseContent[soapenv.Body][v01.Response][v01.RequestID][0].value = 'iii' 

context.content = XmlUtil.serialize(responseContent)

So the key thing was

def soapenv =  new groovy.xml.Namespace("http://schemas.xmlsoap.org/soap/envelope/")
def v01 = new groovy.xml.Namespace("http://www.whatever.com/data_type/ZE_Messaging/v01")

responseContent[soapenv.Body][v01.Response][v01.RequestID][0].value = 'iii' 

Thanks