ngoyal
7 years agoOccasional Contributor
How to pick minimum value of an attribute from soap response and pick next node value based on it
From below response I am trying to pick minimum value of AmountAfterTax and also value of corresponding HotelCode and ChainCode.
Response XML -
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"/>
<soap:Body>
<ns2:OTA_HotelAvailRS AltLangID="en" EchoToken="1234" PrimaryLangID="en" RetransmissionIndicator="false" Target="Test">
<ns2:Success/>
<ns2:RoomStays>
<ns2:RoomStay>
<ns2:RoomRates>
<ns2:RoomRate>
<ns2:Rates>
<ns2:Rate RateMode="1">
<ns2:Base AmountAfterTax="193.81" AmountBeforeTax="169.98" CurrencyCode="USD" DecimalPlaces="2"/>
</ns2:Rate>
<ns2:Rate RateMode="2">
<ns2:Base AmountAfterTax="296.43" AmountBeforeTax="259.98" CurrencyCode="USD" DecimalPlaces="2"/>
</ns2:Rate>
</ns2:Rates>
</ns2:RoomRate>
</ns2:RoomRates>
<ns2:BasicPropertyInfo BrandCode="BEST" ChainCode="ABC" HotelCode="1234"/>
<ns2:TPA_Extensions>
<ns2:ExchangeRate Percent="1"/>
</ns2:TPA_Extensions>
</ns2:RoomStay>
<ns2:RoomStay>
<ns2:RoomRates>
<ns2:RoomRate>
<ns2:Rates>
<ns2:Rate RateMode="1">
<ns2:Base AmountAfterTax="296.42" AmountBeforeTax="268.20" CurrencyCode="USD" DecimalPlaces="2"/>
</ns2:Rate>
<ns2:Rate RateMode="2">
<ns2:Base AmountAfterTax="373.56" AmountBeforeTax="338.00" CurrencyCode="USD" DecimalPlaces="2"/>
</ns2:Rate>
</ns2:Rates>
</ns2:RoomRate>
</ns2:RoomRates>
<ns2:BasicPropertyInfo BrandCode="PLUS" ChainCode="ABC" HotelCode="3456"/>
<ns2:TPA_Extensions>
<ns2:ExchangeRate Percent="1"/>
</ns2:TPA_Extensions>
</ns2:RoomStay>
<ns2:RoomStay>
<ns2:RoomRates>
<ns2:RoomRate>
<ns2:Rates>
<ns2:Rate RateMode="1">
<ns2:Base AmountAfterTax="213.61" AmountBeforeTax="191.23" CurrencyCode="USD" DecimalPlaces="2"/>
</ns2:Rate>
<ns2:Rate RateMode="2">
<ns2:Base AmountAfterTax="452.36" AmountBeforeTax="404.98" CurrencyCode="USD" DecimalPlaces="2"/>
</ns2:Rate>
</ns2:Rates>
</ns2:RoomRate>
</ns2:RoomRates>
<ns2:BasicPropertyInfo BrandCode="BEST" ChainCode="ABC" HotelCode="03154"/>
<ns2:TPA_Extensions>
<ns2:ExchangeRate Percent="1"/>
</ns2:TPA_Extensions>
</ns2:RoomStay>
<ns2:RoomStay>
<ns2:RoomRates>
<ns2:RoomRate>
<ns2:Rates>
<ns2:Rate RateMode="1">
<ns2:Base AmountAfterTax="410.08" AmountBeforeTax="359.98" CurrencyCode="USD" DecimalPlaces="2"/>
</ns2:Rate>
<ns2:Rate RateMode="2">
<ns2:Base AmountAfterTax="797.42" AmountBeforeTax="699.98" CurrencyCode="USD" DecimalPlaces="2"/>
</ns2:Rate>
</ns2:Rates>
</ns2:RoomRate>
</ns2:RoomRates>
<ns2:BasicPropertyInfo BrandCode="PLUS" ChainCode="ABC" HotelCode="03147"/>
<ns2:TPA_Extensions>
<ns2:ExchangeRate Percent="1"/>
</ns2:TPA_Extensions>
</ns2:RoomStay>
<ns2:RoomStay>
<ns2:RoomRates>
<ns2:RoomRate>
<ns2:Rates>
<ns2:Rate RateMode="1">
<ns2:Base AmountAfterTax="368.42" AmountBeforeTax="322.98" CurrencyCode="USD" DecimalPlaces="2"/>
</ns2:Rate>
<ns2:Rate RateMode="2">
<ns2:Base AmountAfterTax="479.08" AmountBeforeTax="419.98" CurrencyCode="USD" DecimalPlaces="2"/>
</ns2:Rate>
</ns2:Rates>
</ns2:RoomRate>
</ns2:RoomRates>
<ns2:BasicPropertyInfo BrandCode="BEST" ChainCode="ABC" HotelCode="03133"/>
<ns2:TPA_Extensions>
<ns2:ExchangeRate Percent="1"/>
</ns2:TPA_Extensions>
</ns2:RoomStay>
<ns2:RoomStay>
<ns2:RoomRates>
<ns2:RoomRate>
<ns2:Rates>
<ns2:Rate RateMode="1">
<ns2:Base AmountAfterTax="246.86" AmountBeforeTax="219.30" CurrencyCode="USD" DecimalPlaces="2"/>
</ns2:Rate>
<ns2:Rate RateMode="2">
<ns2:Base AmountAfterTax="403.00" AmountBeforeTax="358.00" CurrencyCode="USD" DecimalPlaces="2"/>
</ns2:Rate>
</ns2:Rates>
</ns2:RoomRate>
</ns2:RoomRates>
<ns2:BasicPropertyInfo BrandCode="BEST" ChainCode="ABC" HotelCode="03092"/>
<ns2:TPA_Extensions>
<ns2:ExchangeRate Percent="1"/>
</ns2:TPA_Extensions>
</ns2:RoomStay>
<ns2:RoomStay>
<ns2:RoomRates>
<ns2:RoomRate>
<ns2:Rates>
<ns2:Rate RateMode="1">
<ns2:Base AmountAfterTax="144.06" AmountBeforeTax="127.98" CurrencyCode="USD" DecimalPlaces="2"/>
</ns2:Rate>
<ns2:Rate RateMode="2">
<ns2:Base AmountAfterTax="236.38" AmountBeforeTax="209.98" CurrencyCode="USD" DecimalPlaces="2"/>
</ns2:Rate>
</ns2:Rates>
</ns2:RoomRate>
</ns2:RoomRates>
<ns2:BasicPropertyInfo BrandCode="BEST" ChainCode="ABC" HotelCode="03062"/>
<ns2:TPA_Extensions>
<ns2:ExchangeRate Percent="1"/>
</ns2:TPA_Extensions>
</ns2:RoomStay>
</ns2:RoomStays>
</ns2:OTA_HotelAvailRS>
</soap:Body>
</soap:Envelope>
Below is the code by which I am able to get all the values for AmountAfterTax but sort method is not working and I am unable to fetch the corresponding HotelCode and ChainCode.
import groovy.xml.*;
def response = testRunner.testCase.getTestStepByName("AreaAvail_2011B").getProperty("Response").getValue()
def parsedXml = new XmlSlurper().parseText(response)
def amountAfterTax = parsedXml.'**'.findAll {'Base' == it.name()}*.@AmountAfterTax
Arrays.sort(amountAfterTax);
def minimumAmount = amountAfterTax[0]
Thanks in advance for your suggestions.
//Using this expectedStayInfo just for testing def expectedStayInfo = [AmountAfterTax: 144.06, HotelCode: '03062', ChainCode: 'ABC' ] def xml = new XmlSlurper().parseText(response) def stays = xml.'**'.findAll {it.name() == 'RoomStay'} def actualStayInfo = [AmountAfterTax: 0, HotelCode: null, ChainCode: null] def updateActualStayInfo = { node, rate -> def isUpdate = false if ((0 == actualStayInfo.AmountAfterTax) || (actualStayInfo.AmountAfterTax > rate)) { isUpdate = true } if (isUpdate) { def info = node.'**'.find{'BasicPropertyInfo' == it.name()} actualStayInfo.AmountAfterTax = rate actualStayInfo.HotelCode = info.@HotelCode.text() actualStayInfo.ChainCode = info.@ChainCode.text() } }
stays.each { stay -> def tempMin = stay.'**'.findAll{it.name() == 'Base'}*.@AmountAfterTax.collect{it.toBigDecimal()}.min() updateActualStayInfo(stay, tempMin) } //test if the actual value of xml is matching assert actualStayInfo == expectedStayInfo