Groovy web console

subscribe to the feed Subscribe
to this
site
test equality1 (via #groovywebconsole)
tweet this snippet Tweet
this
script

test equality1

Published 1 month ago by nmrao with tags equality
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
def response1 ='''<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Header>
      <MessageID xmlns="http://www.w3.org">uuid: ...</MessageID>
   </S:Header>
   <S:Body>
      <ns3:ResponseElement xmlns:ns2="http://www.example.com/services/common/example/xsd" xmlns:ns3="http://www.example.com/example/xsd">
         <Info>
               <Code>01234</Code>
               <Time>2017-05-28</Time>
               <SecondCode>ThirdValue</SecondCode>
               <Number>000</Number>
         </Info>
         <Info>
               <Code>56789</Code>
               <Time>2017-07-30</Time>
               <SecondCode>ThirdValue</SecondCode>
               <Number>111</Number>
         </Info>
      </ns3:ResponseElement>
   </S:Body>
</S:Envelope>
'''
def response2 ='''<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Header>
      <MessageID xmlns="http://www.w3.org">uuid: ...</MessageID>
   </S:Header>
   <S:Body>
      <ns3:ResponseElement xmlns:ns2="http://www.example.com/services/common/example/xsd" xmlns:ns3="http://www.example.com/example/xsd">
         <Info>
               <Code>56789</Code>
               <Time>2017-07-30</Time>
               <SecondCode>ThirdValue</SecondCode>
               <Number>111</Number>
         </Info>
         <Info>
               <Code>01234</Code>
               <Time>2017-05-28</Time>
               <SecondCode>ThirdValue</SecondCode>
               <Number>000</Number>
         </Info>
      </ns3:ResponseElement>
   </S:Body>
</S:Envelope>
'''
def result = { response ->
     def data = new XmlSlurper().parseText(response)
     def infos = data.'**'.findAll{it.name()=='Info'}.sort{a, b -> a.Code.text() <=> b.Code.text()}
     println infos
     infos.collect{
              element -> element.children().breadthFirst()*.name()
             .findAll { !element."$it".children().size() }
             .collectEntries{[(element."$it".name()): element."$it"] }
     }
}
def result1 = result(response1)
def result2 = result(response2)
println result1
println result2
assert result1 == result2