Question

I'm trying to set up an integration test using rest-assured. In one of my test cases I have to validate some properties of an XML file with rest-assured's XmlPath which seems to use Groovy's GPath.

I have an XML document with the following structure (the ids are unique):

<rootelement>
    <someelement id="1234" type="a">
      <property key="hello" value="world" />
      <property key="name" value="a name" />
      <property key="status" value="new" />
      <child target="645823" type="a" />
      <child target="7482" type="b" />
      <child target="8942" type="c">
         <property key="pro" value="yes" />
      </child>
    </someelement>
    <someelement>
        ...
    </someelement>
<rootelement>

Ideally, given a someelement id, I want to get a map of it's properties, i.e. assuming the given someelement id is 1234 I'd like to get a map that looks like the following: {"hello": "world", "name": "a name", "status": "new"}. How would I do this? I know that there's a getMap method in XmlPath, but I couldn't figure out which expression I'd have to use.

If it's not possible to get the properties as a map, I would be content with getting a list of the keys and a list for the values. Again, I don't know which expression I have to use. I tried something like that:

xmlPath.getList("**.find {it.@id = '1234'}.property.@key", String.class)

However, it doesn't find anything.

Was it helpful?

Solution

You can do this with Groovy (assuming xml is a String containing your xml)

def map = new XmlParser().parseText( xml )
                         .someelement
                         .find { it.@id == '1234' }
                         .property
                         .collectEntries { [ it.@key, it.@value ] }

assert map == [ hello:'world', name:'a name', status:'new' ]

Never used rest-assured, so I can't be much help in that direction though :-(

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top