2009-06-03 6 views
22

Questo è probabilmente un molto facile uno per tutti i clienti abituali SoapUI.SoapUI parametri ricevendo richiesta a script di servizio finto

In uno script di risposta del servizio finto SoapUI, come faccio a estrarre il valore all'interno della richiesta che sto rispondendo a?

Diciamo che la richiesta in ingresso ha

<ns1:foo> 
    <ns3:data> 
    <ns3:CustomerNumber>1234</ns3:CustomerNumber> 
    </ns3:data> 
</ns1:foo> 

Come faccio ad avere il "1234" in una variabile Groovy? Ho provato con un xmlHolder ma mi sembra di avere l'XPath sbagliato.

(So come impostare una proprietà e integrare il suo valore nella risposta già.)

risposta

28

Se si desidera accedere richiesta SOAP e fare un po 'di elaborazione XPath, c'è un modo più semplice per farlo in SoapUI grazie alla la potenza di GPath e XmlSlurper.

Ecco come si dovrebbe accedere al codice cliente:

def req = new XmlSlurper().parseText(mockRequest.requestContent) 
log.info "Customer #${req.foo.data.CustomerNumber}" 

Come di Groovy 1.6.3 (che viene utilizzato in soapUI 2.5 e oltre), viene eseguito in XmlSlurper namespace-aware e la modalità non-validante per default quindi non c'è nient'altro che devi fare.

Cheers!
Shonzilla

+0

Un sacco di XML-Q/A qui, ma se si dispone di una richiesta JSON, Funziona in modo simile usando 'def requestJson = new groovy.json.JsonSlurper(). parseText (mockRequest.requestContent)' e per esempio 'log.info" $ {requestJson.'CustomerNumber '} "' – fheub

22

Un altro esempio:

def request = new XmlSlurper().parseText(mockRequest.requestContent) 
def a = request.Body.Add.x.toDouble() 
def b = request.Body.Add.y.toDouble() 
context.result = a + b 

In questo esempio abbiamo ottenere due parametri dalla richiesta e convertirli in doppie. In questo modo possiamo eseguire calcoli sui parametri. La risposta SoapUI di esempio per questo esempio è:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:typ="http://example.org/math/types/"> 
    <soapenv:Header/> 
    <soapenv:Body> 
     <typ:AddResponse> 
     <result>${result}</result> 
     </typ:AddResponse> 
    </soapenv:Body> 
</soapenv:Envelope> 

Si può vedere come i calcoli risultato è passato nuovamente alla risposta.

+0

Grazie per il tuo esempio! Mi piacciono sia le risposte di Shonzilla che te! – Thorsten79

0

In un Java puro (se non utilizzano SoapUI) si sarebbe solo creare un contesto dei nomi personalizzato come questo:

import java.util.Iterator; 
import java.util.List; 

import javax.xml.XMLConstants; 
import javax.xml.namespace.NamespaceContext; 

class WSNamespaceContext implements NamespaceContext 
{ 
    public String getNamespaceURI(String prefix) 
    { 
     if (prefix.equals("ns3")) 
      return "http://www.mysite.com/services/taxservice"; 
     else if (prefix.equals("soapenv")) 
      return "http://schemas.xmlsoap.org/soap/envelope/"; 
     else 
      return XMLConstants.NULL_NS_URI; 
    } 

    public String getPrefix(String namespace) 
    { 
     if (namespace.equals("http://www.mysite.com/services/taxservice")) 
      return "ns3"; 
     else if (namespace.equals("http://schemas.xmlsoap.org/soap/envelope/")) 
      return "soapenv"; 
     else 
      return null; 
    } 

    public Iterator<List<String>> getPrefixes(String namespace) 
    { 
     return null; 
    } 
} 

Poi, analizzarlo in questo modo:

XPathFactory factory = XPathFactory.newInstance(); 
XPath xp = factory.newXPath(); 
xp.setNamespaceContext(nsc); 
XPathExpression xpexpr = xp.compile("//ns3:CustomerNumber/text()"); 
NodeList nodes = (NodeList)xpexpr.evaluate(doc, XPathConstants.NODESET); 
for (int i = 0; i < nodes.getLength(); i++) { 
    String val = nodes.item(i).getNodeValue(); 
    System.out.println("Value: " + val ); 
} 
Problemi correlati