Eviware Forum

soapUI => soapUI => Topic started by: Adam White on May 17, 2008, 12:54:48 am



Title: Help getting node value with groovy script
Post by: Adam White on May 17, 2008, 12:54:48 am
Hi,

I'm having trouble accessing a node value, I keep getting null returned.  I'm using the free version of SoapUI to prove what we need to do can be done, but I'm having trouble getting it to work.

The groovy script below is for a Mockservice.  I'm trying to access a value from the request and send back a response based on a node value.

The XML Request is:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
   <soap:Header>
      <wsse:Security xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/07/secext" soap:mustUnderstand="0">
         <wsse:UsernameToken>
            <wsse:Username>ccmsUser</wsse:Username>
            <wsse:Password Type="wsse:passwordText">ccmsPassword</wsse:Password>
         </wsse:UsernameToken>
      </wsse:Security>
      <ProcessingInstructions xmlns="http://website.com/interfaces">
         <IncludeEmptyElements>Y</IncludeEmptyElements>
      </ProcessingInstructions>
   </soap:Header>
   <soap:Body>
      <FaCSIAEnrolmentInterface_QueryEnrolments_Input xmlns="http://website.com/interfaces">
         <EndDateEmpty xsi:nil="1"/>
         <ServiceProviderEnrolmentReference>2</ServiceProviderEnrolmentReference>
         <PageNumber>1</PageNumber>
         <Status xsi:nil="1"/>
         <StartDateFrom xsi:nil="1"/>
         <EndDateFrom xsi:nil="1"/>
         <PageSize>10</PageSize>
         <SourceSystemCode>1-I6TS</SourceSystemCode>
         <Type xsi:nil="1"/>
         <CCBApprovalId>232432</CCBApprovalId>
         <StartDateTo xsi:nil="1"/>
         <EndDateTo xsi:nil="1"/>
         <FACSIAEnrolmentId xsi:nil="1"/>
         <TransactionId>27E1460B-A5B9-4082-9968-235AAE728C22</TransactionId>
         <UpdatedSince xsi:nil="1"/>
      </FaCSIAEnrolmentInterface_QueryEnrolments_Input>
   </soap:Body>
</soap:Envelope>

My groovy script is:
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder( mockRequest.requestContent )

//return holder.getNodeValue( "//wsse:Username" )
return holder.getNodeValue( "//ServiceProviderEnrolmentReference" )
def sper = holder.getNodeValue( "//ServiceProviderEnrolmentReference" )


if (sper == "1")
   return "Response 1"
else if (sper == "2")
   return "Response 2"
else
return "Response 1"

The line return holder.getNodeValue( "//wsse:Username" ) works and returns a response.  But holder.getNodeValue( "//ServiceProviderEnrolmentReference" ) returns null.  If I change the node to anything contained within the <soap:body> tags I get null.

Can you tell me what I'm doing wrong?

Thanks,
Adam


Title: Re: Help getting node value with groovy script
Post by: omatzura on May 17, 2008, 10:04:17 pm
Hi Adam,

this is probably namespace-related; please try first associating the required namespace with the holder:

holder.namespaces["ns"] = "http://website.com/interfaces"
log.info( holder["//ns:ServiceProviderEnrolmentReference"] )

does that help?

regards!

/Ole
eviware.com


Title: Re: Help getting node value with groovy script
Post by: Adam White on May 17, 2008, 11:57:46 pm
Awesome, that worked.  A lot better than my hacked way below of getting it.

def requestString = request.toString()
def length = requestString.size()
def iStart = requestString.indexOf('ServiceProviderEnrolmentReference')
iStart = iStart + 34
def iEnd = requestString.lastIndexOf('ServiceProviderEnrolmentReference')
iEnd = iEnd - 2
def enrolmentId = requestString.substring(iStart, iEnd)

Thanks.


Title: Re: Help getting node value with groovy script
Post by: zxsoap on June 18, 2008, 10:00:02 pm
Hi OLE,

How can I get a value node (ALL account_ID) which's occuring more then one time (see below respone)? Ex, my response return more than one account_id. Now I'm only get just the first accountID on xml response. But, I need to get all? I want to return all account_id (14,68,22,41).

Here's what I used:

roovyUtils = new com.eviware.soapui.support.GroovyUtils(context )
holder = groovyUtils.getXmlHolder ("getAccountPricingGroupsByPriceGroupID#Response")

accid = holder.getNodeValue("//account_id")
log.info (accid)


Here's the response

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <SOAP-ENV:Header/>
   <SOAP-ENV:Body>
      <ser-root:getIDResponse xmlns:ser-root="http://......com/amAccountDataServices.services">
         <getAccountPricingGroupsByPriceGroupIDRes>
            <responseStatus>
               <code>Success</code>
            </responseStatus>
            <pricingGroupAccount>
               <ArrayOfpricingGroupAccountItem>
                  <account_id>14</account_id>
                  <account_number>3819178372</account_number>
                  <account_type_code>123</account_type_code>
                 .......
                  <state_code>68</state_code>
               </ArrayOfpricingGroupAccountItem>
               <ArrayOfpricingGroupAccountItem>
                  <account_id>17</account_id>
                  <account_number>6099471829</account_number>
                  <account_type_code>123</account_type_code>
                  .......
               </ArrayOfpricingGroupAccountItem>
               <ArrayOfpricingGroupAccountItem>
                  <account_id>22</account_id>
                  <account_number>6092357899</account_number>
                  <account_type_code>123</account_type_code>
                  ....
               </ArrayOfpricingGroupAccountItem>
               <ArrayOfpricingGroupAccountItem>
                  <account_id>41</account_id>
                  <account_number>0451471007</account_number>
                  .......
               </ArrayOfpricingGroupAccountItem>
            </pricingGroupAccount>
         </getAccountPricingGroupsByPriceGroupIDRes>
      </ser-root:getIDResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Thanks,
ZX


Title: Re: Help getting node value with groovy script
Post by: omatzura on June 18, 2008, 10:21:41 pm
Hi!

please try the

holder.getNodeValues( ..xpath.. )

method instead, which returns an array of strings, one for each node matched by the specified xpath

regards!

/Ole
eviware.com