Forum Discussion

keanesean's avatar
keanesean
Occasional Contributor
10 years ago

Sorting XML response before assertion

Hi, 

 

I have a web service response that I want to compare to a JDBC response. I can modify the SQL query to ensure the JDBC response is sorted, but the web service response is not sorted alphabetically

How can I sort the response alphabetically?

 

E.g. sort below by ServiceCode

<AccountServices>
<ServiceCode>GTEL</ServiceCode>
<ServicePackage>defNa</ServicePackage>
<ServiceStatus>2</ServiceStatus>
<ServiceParameters/>
</AccountServices>
<AccountServices>
<ServiceCode>GPBAS</ServiceCode>
<ServicePackage>defNa</ServicePackage>
<ServiceStatus>2</ServiceStatus>
<ServiceParameters/>
</AccountServices>

3 Replies

  • Hmm., it may really depends on what data exactly you wanted to compare from jdbc response.

    For example, you get AccountServices is a list. So you can get the AccountServices if ServiceCode is GTEL and get the same data from jdbc and then compare. Then loop thru the AccountServices, that should solve the issue unless you want to compare both of the entire xmls.
    • kondasamy's avatar
      kondasamy
      Regular Contributor

      Additional option using code  -  here is a Groovy snippet which sorts the XML nodes based on our need using XmlSlurper which sorts the <book> nodes using its 'id' attribute,

       

      import groovy.xml.MarkupBuilder
      
      String xml = '''
      				<catalog>
      				   <book id="bk101">
      					  <author>Gambardella, Matthew</author>
      					  <title>XML Developer's Guide</title>
      					  <genre>Computer</genre>
      					  <price>44.95</price>
      					  <publish_date>2000-10-01</publish_date>
      					  <description>An in-depth look at creating applications 
      					  with XML.</description>
      				   </book>
      				   <book id="bk119">
      					  <author>Ralls, Kim</author>
      					  <title>Midnight Rain</title>
      					  <genre>Fantasy</genre>
      					  <price>5.95</price>
      					  <publish_date>2000-12-16</publish_date>
      					  <description>A former architect battles corporate zombies, 
      					  an evil sorceress, and her own childhood to become queen 
      					  of the world.</description>
      				   </book>
      				   <book id="bk103">
      					  <author>Corets, Eva</author>
      					  <title>Maeve Ascendant</title>
      					  <genre>Fantasy</genre>
      					  <price>5.95</price>
      					  <publish_date>2000-11-17</publish_date>
      					  <description>After the collapse of a nanotechnology 
      					  society in England, the young survivors lay the 
      					  foundation for a new society.</description>
      				   </book>
      				   <book id="bk104">
      					  <author>Corets, Eva</author>
      					  <title>Oberon's Legacy</title>
      					  <genre>Fantasy</genre>
      					  <price>5.95</price>
      					  <publish_date>2001-03-10</publish_date>
      					  <description>In post-apocalypse England, the mysterious 
      					  agent known only as Oberon helps to create a new life 
      					  for the inhabitants of London. Sequel to Maeve 
      					  Ascendant.</description>
      				   </book>
      				   <book id="bk105">
      					  <author>Corets, Eva</author>
      					  <title>The Sundered Grail</title>
      					  <genre>Fantasy</genre>
      					  <price>5.95</price>
      					  <publish_date>2001-09-10</publish_date>
      					  <description>The two daughters of Maeve, half-sisters, 
      					  battle one another for control of England. Sequel to 
      					  Oberon's Legacy.</description>
      				   </book>
      				   <book id="bk23">
      					  <author>Randall, Cynthia</author>
      					  <title>Lover Birds</title>
      					  <genre>Romance</genre>
      					  <price>4.95</price>
      					  <publish_date>2000-09-02</publish_date>
      					  <description>When Carla meets Paul at an ornithology 
      					  conference, tempers fly as feathers get ruffled.</description>
      				   </book>
      				</catalog>
      				'''
      def rootNode = new XmlParser().parseText(xml)
      rootNode.children().sort(true) {it.attribute('id')}
      new XmlNodePrinter().print(rootNode)

       

      Then you could compare this output to the output XML of JDBC Teststep.

       

      Thanks,

      Samy

    • keanesean's avatar
      keanesean
      Occasional Contributor

      Thanks for reply Rao, I want to check that the ServiceCode from XML response exists in the JDBC response, and that the ServiceStatus is the same for both. 

      Can you give me an example of what you mean when you say I should list them and loop through?