2

I have an XML response as below:

<ns:Envelope xmlns:tns="http://schemas.xmlsoap.org/soap/envelope/">
 <ns:Body>
    <ns:response xmlns:svc="http://...serviceNameSpace" 
                xmlns:ent="http://....entitiesNameSpace">
        <ns:customer>
            <ns:contact>
                <ns:type>firstclass</ns:type>
                <ns:email>kevin@....com</ns:email>
                <ns:details>
                    <ns:name>Kevin</ns:name>
                    <ns:area>Networking</ns:area>
                </ns:details>
                <ns:address>
                    <ns:code>39343</ns:code>
                    <ns:country>US</ns:country>
                </ns:address>
             </ns:contact>
            <ns:contact>
                <ns:type>secondclass</ns:type>
                <ns:email>john@...com</ns:email>
                <ns:details>
                    <ns:name>John</ns:name>
                    <ns:area>Development</ns:area>
                <ns:address>
                    <ns:code>23445</ns:code>
                    <ns:country>US</ns:country>
             </ns:contact>                 
        </ns:customer>
    </ns:response >
</ns:Body>

I am trying this to iterate childnodes details and address to validate the response with the request properties. But I could assert email but couldn't go into details (name and area) and address (code and country). Below is the code I am using

import groovy.xml.*

def envelope = new XmlSlurper().parseText(messageExchange.responseContentAsXml)
def type = 'secondclass'
def emailAddress= ${properties#emailAddress}

envelope.'**'
.findAll { it.name() == 'contact' }
.findAll { it.type.text().contains(type) }
.each {
        assert emailAddress== it.emailAddress.text()
    }

Please help me in iterating the nodes details(name and area) and address (code and country) for assertion

Kumar
  • 23
  • 1
  • 3
  • You mean to say if you receive the same data in the response as that was sent in the request? Then, please add your request as well by editing the post. – Rao Feb 24 '17 at 08:48
  • Nope request is not same as response data. That is different, I am setting properties seperately with the values. And I want to assert it by validating the xml data with already set properties. – Kumar Feb 24 '17 at 15:01

1 Answers1

1

First of all, it seems that your xml is slightly broken with missing closing tags. I took the liberty of fixing that in the example below.

Conceptually, when you are navigating through the xml with expressions like xml.Envelope.Body.response you are navigating through xml nodes. Note the distinction here between xml nodes (i.e. elements) and the actual data or text within the nodes.

The xml nodes returned from XmlSlurper are represented as descendants of the groovy GPathResult class. These descendants include NodeChild, NodeChildren, NoChildren, and Attribute, all of which can be returned by an xml.Envelope.Body.Response type of query depending on how the query and the xml looks. To retrieve the actual text data within a node you need to call node.text().

With the xml fixed and the above in mind, the following code:

def str = '''\
<ns:Envelope xmlns:ns="http://schemas.xmlsoap.org/soap/envelope/">
<ns:Body>
    <ns:response xmlns:svc="http://...serviceNameSpace" xmlns:ent="http://....entitiesNameSpace">
        <ns:customer>
            <ns:contact>
                <ns:type>firstclass</ns:type>
                <ns:email>kevin@....com</ns:email>
                <ns:details>
                    <ns:name>Kevin</ns:name>
                    <ns:area>Networking</ns:area>
                </ns:details>
                <ns:address>
                    <ns:code>39343</ns:code>
                    <ns:country>US</ns:country>
                </ns:address>
             </ns:contact>
            <ns:contact>
                <ns:type>secondclass</ns:type>
                <ns:email>john@...com</ns:email>
                <ns:details>
                    <ns:name>John</ns:name>
                    <ns:area>Development</ns:area>
                </ns:details>
                <ns:address>
                    <ns:code>23445</ns:code>
                    <ns:country>US</ns:country>
                </ns:address>
             </ns:contact>                 
        </ns:customer>
    </ns:response >
</ns:Body>
</ns:Envelope>
'''

def xml = new XmlSlurper(false, true).parseText(str)

def contactNodes = xml.Body.response.customer.contact

assert contactNodes.first().email               == 'kevin@....com'
assert contactNodes.first().details.name.text() == "Kevin"
assert contactNodes.first().details.area.text() == "Networking"

assert contactNodes.last().email               == 'john@...com'
assert contactNodes.last().details.name.text() == "John"
assert contactNodes.last().details.area.text() == "Development"

runs and all the assertions succeed.

The contactNodes variable is a groovy NodeChildren object and can for all intents and purposes be treated as a List of nodes (i.e. you can call methods like .each {}, .every {}, .any {}, ... on it).

edit in response to comment: To iterate only over contact nodes with specific properties, you can do:

xml.Body.response.customer.contact.findAll { contactNode ->
    contactNode.type.text() == 'firstclass' 
}.each { firstClassContactNode -> 
    assert firstClassContactNode.email.text() == "kevin@....com"
}
Matias Bjarland
  • 4,124
  • 1
  • 13
  • 21
  • Thanks for your answer, can you please tell me if I want to iterate the xml when the type is firstclass . I want to iterate those xml data and assert it – Kumar Feb 24 '17 at 14:59
  • Thanks Matias, I could already assert email address for that type can you please help me in asserting details and address child nodes for that type – Kumar Feb 24 '17 at 17:03
  • I tried with this. .each { details -> assert details.name.text()== "Kevin"} Getting groovy.util.slurpersupport.NodeChildren@1e46e7a (to.String()=="") message – Kumar Feb 24 '17 at 17:26
  • Thank you Matias. I got it worked. Appreciate you help. – Kumar Feb 24 '17 at 18:35
  • Glad it worked out in the end. If you feel that the answer solved your problem satisfactorily, feel free to mark it as an accepted answer. – Matias Bjarland Feb 24 '17 at 18:38