Domanda

Questo è probabilmente un molto facile uno per tutti i clienti abituali SoapUI.

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 in una variabile Groovy "1234"? Ho provato con un xmlHolder, ma mi sembra di avere la XPath sbagliata.

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

È stato utile?

Soluzione

Se si desidera accedere richiesta SOAP e fare un po 'di elaborazione XPath, c'è un modo più semplice per farlo in SoapUI grazie alla 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}"

A partire dal Groovy 1.6.3 (che viene utilizzato in soapUI 2.5 e oltre), XmlSlurper corre nel namespace-aware e la modalità non-validante per default quindi non c'è niente altro che devi fare.

Cheers!
Shonzilla

Altri suggerimenti

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 siamo in grado di eseguire calcoli sui parametri. La risposta SoapUI di esempio per questo esempio è il seguente:

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

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  ); 
}

http://www.soapui.org/soap-mocking /creating-dynamic-mockservices.html e sulla base di http://www.soapui.org/apidocs/com/eviware/soapui/support/xmlholder.html sono arrivato fino a questo:

// Create XmlHolder for request content
def holder = new com.eviware.soapui.support.XmlHolder( mockRequest.requestContent )
holder.namespaces["ns3"] = "ns3"

// Get arguments
def custNo = holder.getNodeValue("//ns3:CustomerNumber")
context.setProperty("custNo", custNo)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top