【问题标题】:Copying values from SOAP UI request into SOAP UI response using groovy使用 groovy 将 SOAP UI 请求中的值复制到 SOAP UI 响应中
【发布时间】:2015-10-29 15:28:24
【问题描述】:

我正在尝试将一些值从 SOAP 请求 XML 复制到 SOAP 响应 XML 文件中。

考虑以下请求和响应 XML 以供参考:

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

而响应 XML 是:

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

我在这里创建了两个动态变量 ${id} 和 ${metadata}。

我试图复制 .Now 的 id 和 metadata 属性,以将这些值从请求设置为响应,我正在使用以下脚本:

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

但它没有返回任何值。有人可以帮我弄清楚我在这里做错了什么吗?

另外,如果我想将请求中的许多(考虑 50 多个)值复制到响应中,那么有什么简单的方法可以代替创建 50 多个变量吗?因为就我而言,我必须将请求中的所有值复制到响应消息中。

【问题讨论】:

  • 您是否在模拟服务中的 onRequest script 上使用此脚本?
  • 是的,我正在为soap ui中的模拟服务编写这个脚本

标签: xml web-services groovy soapui


【解决方案1】:

我认为问题在于您并没有真正获得idmetadata 属性的值,因为它们来自http://niem.gov/niem/structures/2.0 命名空间ns:idns:metadata。见this related question on SO

所以试着记录下你的表情:

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

它会返回空。这可能是您在模拟响应中看不到预期值的原因。

解决方案是使用命名空间来取回您的属性值,将您的代码更改为:

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()]

不用每次都写完整路径更易于维护:

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()]

【讨论】:

    猜你喜欢
    • 2017-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多