Forum Discussion

vpachpute1's avatar
vpachpute1
Frequent Contributor
8 years ago

Groovy : XML Comparison: Dynamic XML Attributes are not ignored whie comparison.

Hi

 

I have one Groovy utility to compare current response with external response kept in xml file.

 

But while comparison, I am facing issue if parameter contains space. e.g.

1. If parameter is present as , subscription id .Then it is not ignored

2. If parameter is present as, subscription-id. Then it is ignored while comparison. Working file.

 

My case:

Actually in my case, subscription id will be dynamic. So while comparison , I added it in ignore list. So that next time this will be ignored while comparison.

 

Attached my groovy Utility and sample external response file.

 

Regards

Vishal

 

  • nmrao's avatar
    nmrao
    8 years ago

    vpachpute1,

     

    The data provided is not well-formed. Fixed a bit to make it well-formed.

     

    Here is the script:

     

    //assign xml string data for the below two statements, not assigned here to reduce the size of the script
    
    def expected = 
    def actual = 
    
    //Don't have to modify beyond this point
    
    //If required, add more ignore nodes or attributes(node: attribute format) in the below appropriately
    def ignoreNodes = ['purchase-date', 'expiry-date']
    def ignoreAttributes = ['subscription':'id']
    
    //Closure to update the xml with a fixed value REPLACED in place of dynamic value, so that comparison is done successfully
    def updateIgnoreElements = { data, fixedVal = 'REPLACED' ->
        def parsedData = new XmlSlurper().parseText(data)
        ignoreAttributes.each { k,v -> parsedData.'**'.findAll { it.name() == k}.collect{it.@"$v" = fixedVal} }
        ignoreNodes.each { element ->  parsedData.'**'.findAll { it.name() == element}.collect{it.replaceBody fixedVal} }
        groovy.xml.XmlUtil.serialize(parsedData) 
    }
    
    assert updateIgnoreElements(expected) == updateIgnoreElements(actual), 'Both are not matching'

    You can quickly try online demo and the same is available at github

  • vpachpute1's avatar
    vpachpute1
    Frequent Contributor

    Hi

     

    Can anyone please suggest any solution on this . Any help on this will be really appreciated.

     

    Actually I am doubtful about following part.

    If you look at other ignore elements like transaction-id, refund-enlargement-date etc, there are parameters. And if you look at subscription id, in this case, id is attribute.

    So basically need to ignore this id attribute.

     

    def elelist = ["subscription id", "refund-enlargement-date", "transaction-id"]

    for (i in 0..(elelist.size())-1)
    {
            varelement = elelist[i].toString()

            p = xml1.'**'."$varelement"
            p.each{ it.value = "This element is intentionally ignored for xml comparison" }

            p1 = xml.'**'."$varelement"
            p1.each{ it.value = "This element is intentionally ignored for xml comparison" }
          

    // I have added this to ignore the id attribute. but not worked out.
            p2 = xml.'**'."$varelement"
            p2.each{ it.attributes().value = "This element is intentionally ignored for xml comparison" }

    }

     

    Regards

    Vishal

      • vpachpute1's avatar
        vpachpute1
        Frequent Contributor

        Hi

         

        Thanks very much for your reply.

         

        Can you please let me know how should I ignore <subscription id> in my following code while comparison.

         

        Code for ignoring parameters while comparison

         

        def elelist = ["id", "refund-enlargement-date", "transaction-id"]

        for (i in 0..(elelist.size())-1)
        {
                varelement = elelist[i].toString()

                p = xml1.'**'."$varelement"
                p.each{ it.value = "This element is intentionally ignored for xml comparison" }

                p1 = xml.'**'."$varelement"
                p1.each{ it.value = "This element is intentionally ignored for xml comparison" }

        }

         

        Thanks for your support.

         

        Regards

        Vishal

         

         

  • nmrao's avatar
    nmrao
    Champion Level 3
    Based on the attachment, you seemed to ignore all the elements. what is left out there to compare?
    • vpachpute1's avatar
      vpachpute1
      Frequent Contributor

      Hi

       

      Actually the response xml is edited one. There are other parameters also.

       

      From my response

      def elelist = ["subscription id", "purchase-date", "expiry-date"]

       

      Actually I have to ignore id attribute from subscription element. As id is dynamic.

       

      Can you please let me know how to ignore attributes while comparison. (Not elements)

       

      Regards

      Vishal

      • vpachpute1's avatar
        vpachpute1
        Frequent Contributor

        Hi

         

        Can anyone please help me on this.

         

        Regards

        Vishal