2014-05-22 165 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> 

該腳本只需從一個小陣列中隨機抽取城市和國家的值,但如果您想要對某些名稱進行一致的迴應,則可以提出更好的建議。