0
我使用SoapUI來模擬服務,並根據請求使用groovy腳本返回不同的存儲XMl。使用SoapUI更新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'
前10所以,基本上我需要的是如何設置它與我從文件本身獲得的不同之處,然後再設置上下文。content
目前尚不清楚你想要做什麼。 – Opal 2014-09-10 10:25:37
@Opal,我想更新從我的文件系統中獲得的xml,並將當前日期放在TransactionDateTime中。我更新了問題 – mitomed 2014-09-10 10:29:29
您可以添加完整的回覆嗎?與命名空間,這是一個有效的XML文件? – Opal 2014-09-10 10:52:51