SoapUI 模拟服务传输多个节点

SoapUI Mock Service Transfer Multiple Nodes

我很为难。 我觉得有一个简单的解决方案可以实现我想要实现的目标,但我不知道如何去做。

基本上,我正在建立一些模拟 Soap 服务。 我想用传入的更新调用回显。

我的请求是这样的:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"/>
   <soap:Body>
      <ns2:setEvents xmlns:ns2="http://example.com/eventingservices" xmlns:ns3="http://example.com/eventing/sdo">
         <setEventsRequest>
            <SystemCode>ABC</SystemCode>
            <Event>
               <EventTypeCode>01</EventTypeCode>
            </Event>
            <Event>
               <EventTypeCode>04</EventTypeCode>
            </Event>
         </setEventsRequest>
      </ns2:SetEvents>
   </soap:Body>
</soap:Envelope>

然后我想简单地将事件列表传输到响应中。它们具有与请求相同的属性。

一个典型的回应,看起来像这样:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"/>
   <soapenv:Body>
      <qu:setEventsResponse xmlns:qu="http:/example.com/eventingServices">
         <setEventsResponse>
            <Event>
               <EventTypeCode>01</EventTypeCode>
            </Event>
            <Event>
               <EventTypeCode>04</EventTypeCode>
            </Event>
         </setEventsResponse>
      </qu:setEventsResponse>
   </soapenv:Body>
</soapenv:Envelope>

我尝试使用以下 Groovy 脚本,将响应中的事件替换为 ${events}:

def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def holder = groovyUtils.getXmlHolder(mockRequest.requestContent)
def events = String.valueOf(holder.getNodeValues("//Event"))

context.setProperty("events", events);

我也试过上面没有做的字符串。无果。

请帮助我。 如果我能让这该死的东西工作,我会请你喝啤酒!

我假设您在 SOAPUI 中有一个模拟服务,配置如下。

操作的响应类似于:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"/>
   <soapenv:Body>
      <setEventsResponse xmlns="http:/example.com/eventingServices">
         <setEventsResponse>
            ${events}
         </setEventsResponse>
      </setEventsResponse>
   </soapenv:Body>
</soapenv:Envelope>

并且在 mockService 的 onRequestScript 选项卡上,您希望拥有必要的脚本以从 request 获取 <Event> 节点并将其放入 response使用 ${events} 属性。为此,我建议您可以使用 XmlSlurper,如下所示:

import groovy.xml.StreamingMarkupBuilder

// parse the request
def reqSlurper = new XmlSlurper().parseText(mockRequest.requestContent)
// find all 'Event' nodes from request
def events = reqSlurper.depthFirst().findAll { it.name() == 'Event' }

// builder to convert the nodes to string
def smb = new StreamingMarkupBuilder() 

// concatenate in the string all "<Event>" nodes from request
def eventAsString = ''
events.each{
    eventAsString += smb.bindNode(it)
}

// set the property to use in the response
context.setProperty("events", eventAsString);

这就是让它工作的全部内容 :)

另外请注意,您的 xml 中存在一些错误。您问题中的请求格式不正确:</ns2:SetEvents> not close <ns2:setEvents>(注意大写),如果您希望 xmlns:ns2="http://example.com/eventingservices" 命名空间适用于 <SetEvents> 的所有子项,您有将 ns2 添加到所有子节点或删除 ns2 以使其成为该子树的默认值:<SetEvents xmlns="http://example.com/eventingservices">`(这也适用于您的回复)

希望对您有所帮助,