2014-05-22 167 views
0

我试图根据请求做模拟响应。 有了这样的请求:在soapui中重复模拟响应

<soapenv:Body> 
    <con:person> 
    <person> 
     <name>John</name> 
     <age>18</age> 
    </person> 
    </con:person> 
</soapenv:Body> 

,并像

<soapenv:Body> 
    <con:result> 
    <person> 
     <name>?</name> 
     <age>?</age> 
     <country>?</country> 
     <city>?</city> 
    </person> 
    </con:result> 
</soapenv:Body> 

我可以使用从请求元素,采取什么样的,我想在数据库中,并创建响应效应初探。 但是,当我与许多人一样,

<soapenv:Body> 
    <con:person> 
    <person> 
     <name>John</name> 
     <age>18</age> 
    </person> 
    <person> 
     <name>Doe</name> 
     <age>50</age> 
    </person> 
    </con:person> 
</soapenv:Body> 

的请求,我不知道我怎样才能从请求采取的所有数据,以及如何我可以使用它们来创建一个这样的回应:

<soapenv:Body> 
    <con:result> 
    <person> 
     <name>John</name> 
     <age>18</age> 
     <country>France</country> 
     <city>Paris</city> 
    </person> 
    <person> 
     <name>Doe</name> 
     <age>50</age> 
     <country>Spain</country> 
     <city>Madrid</city> 
    </person> 
    </con:result> 
</soapenv:Body> 

请求中和响应中的人数相同。

我希望我清楚,我感谢你的答案。

回答

2

我设法做类似的事情。首先,我定义我的模拟响应对象为:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:stac="stackOverflow.question"> 
    <soapenv:Header/> 
    <soapenv:Body> 
     <stac:result> 
     ${personElements} 
     </stac:result> 
    </soapenv:Body> 
</soapenv:Envelope> 

然后我用这个Groovy脚本创建的内容${personElements}

import groovy.xml.MarkupBuilder 

// An array from which county and city will be drawn randomly 
def countryArray = [["Australia", "Perth"], 
    ["Spain", "Madrid"], 
    ["England","London"], 
    ["Brazil", "Rio"]] 

def random = new Random() 

// create XmlHolder for request content 
def holder = new com.eviware.soapui.support.XmlHolder(mockRequest.requestContent) 

// Get the name and age values from the request 
def requestItems = holder.getNodeValues("//*:person/*:person/descendant::*") 

def writer = new StringWriter() 
def personElements = new MarkupBuilder(writer) 

// Build the response elements 
for (int index = 0; index < requestItems.size() - 1; index += 2) { 

    personElements.'stac:person'() { 

     'stac:name'(requestItems[index]) 
     'stac:age'(requestItems[index+1]) 

     // Choose a random county and city from the array 
     def randomIndex = random.nextInt(countryArray.size()) 
     'stac:country'(countryArray[randomIndex][0]) 
     'stac:city'(countryArray[randomIndex][1]) 
    } 
} 

// Add the newly created elements to the response 
context.personElements = writer.toString() 

这给了我这样的响应:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:stac="stackOverflow.question"> 
    <soapenv:Header/> 
    <soapenv:Body> 
     <stac:result> 
     <stac:person> 
      <stac:name>Dolly Bonkers</stac:name> 
      <stac:age>42</stac:age> 
      <stac:country>Brazil</stac:country> 
      <stac:city>Rio</stac:city> 
     </stac:person> 
     <stac:person> 
      <stac:name>Mary Poppins</stac:name> 
      <stac:age>82</stac:age> 
      <stac:country>England</stac:country> 
      <stac:city>London</stac:city> 
     </stac:person> 
     <stac:person> 
      <stac:name>Bilbo Baggins</stac:name> 
      <stac:age>102</stac:age> 
      <stac:country>Australia</stac:country> 
      <stac:city>Perth</stac:city> 
     </stac:person> 
     <stac:person> 
      <stac:name>Johnny Hardcase</stac:name> 
      <stac:age>22</stac:age> 
      <stac:country>Spain</stac:country> 
      <stac:city>Madrid</stac:city> 
     </stac:person> 
     </stac:result> 
    </soapenv:Body> 
</soapenv:Envelope> 

该脚本只需从一个小阵列中随机抽取城市和国家的值,但如果您想要对某些名称进行一致的回应,则可以提出更好的建议。