使用SoapUI在模拟响应中更新xml

时间:2014-09-10 08:50:36

标签: groovy soapui

我正在使用SoapUI来模拟服务,并且我使用groovy脚本根据请求返回不同的存储XMl。

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)

这样可以正常工作,但现在我正在尝试更新此响应的其中一个节点,更确切地说,我想在该脚本中添加当前日期,因为xml中的节点看起来像

<?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>

在脚本中如果我这样做,就在设置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()

我会得到'eeee'

基本上我需要的是如何在设置context.content

之前将其设置为与文件本身不同的东西。

1 个答案:

答案 0 :(得分:0)

好的,以防这个混乱的问题可能会有所帮助。用于更新例如从文件系统中检索到的xml中的RequestID设置为&#39; eeee&#39;到&#39; iiii&#39;脚本应该是这个

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)

所以关键是

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' 

由于