0
votes

I am trying to write a groovy script to update some of the CDATA section of a SoapUI request.

I am a total novice with respect to both SoapUI and groovy.

I have successfully managed to do what I need by using property transfers, however, since I need to do the same processing in almost all my testcases, I would rather do it in a script that can be called from wherever it's needed.

I have tried to mimick the property transfer steps in groovy, but have had no success as I can't seem to access and parse the CDATA section.

Any help on how to parse and update the values using groovy will be much appreciated.

An example of the request format is shown below

<soapenv:Envelope xmlns:soapenv="soappath" xmlns:myns="mynamespacepath">
   <soapenv:Header/>
       <soapenv:Body>
           <myns:CMSXMLWebImport>
           <myns:xmlIn><![CDATA[<CMSCardRequest xmlns="http://mypath2">
           <Group>
            <Name>Finance</Name>
                <OrgUnit>OU=Finance,OU=Enterprise,DC=DummyDomain</OrgUnit>
                <Parent>Enterprise</Parent>
                <User>
                <Personal>
                        <FirstName>xxx</FirstName>
                        <LastName>xxx</LastName>
                        <Email>[email protected]</Email>
            </Personal>
                </User>
            </Group>
            </CMSCardRequest>]]></myns:xmlIn>
            </myns:CMSXMLWebImport>
        </soapenv:Body>
</soapenv:Envelope>
1

1 Answers

0
votes

The following code:

import groovy.xml.*

def data = '''
<soapenv:Envelope xmlns:soapenv="soappath" xmlns:myns="mynamespacepath">
 <soapenv:Header/>
 <soapenv:Body>
   <myns:CMSXMLWebImport>
     <myns:xmlIn><![CDATA[
        <CMSCardRequest xmlns="http://mypath2">
          <Group>
            <Name>Finance</Name>
            <OrgUnit>OU=Finance,OU=Enterprise,DC=DummyDomain</OrgUnit>
            <Parent>Enterprise</Parent>
            <User>
              <Personal>
                <FirstName>xxx</FirstName>
                <LastName>xxx</LastName>
                <Email>[email protected]</Email>
              </Personal>
            </User>
          </Group>
        </CMSCardRequest>]]>
      </myns:xmlIn>
    </myns:CMSXMLWebImport>
  </soapenv:Body>
</soapenv:Envelope>
'''

def parser = new XmlSlurper()
def xml = parser.parseText(data)
def cardXml = parser.parseText(xml.Body.CMSXMLWebImport.xmlIn.text())

println "name:       ${cardXml.Group.Name.text()}"
println "org unit:   ${cardXml.Group.OrgUnit.text()}"
println "first name: ${cardXml.Group.User.Personal.FirstName.text()}"

// or for a somewhat more succinct access pattern 
println "---------"
cardXml.Group.with { 
  println "name:       ${Name.text()}"
  println "org unit:   ${OrgUnit.text()}"
  println "first name: ${User.Personal.FirstName.text()}"
}

will result in:

~> groovy solution.groovy 
name:       Finance
org unit:   OU=Finance,OU=Enterprise,DC=DummyDomain
first name: xxx
---------
name:       Finance
org unit:   OU=Finance,OU=Enterprise,DC=DummyDomain
first name: xxx

~> 

when run. The trick here is to pull the CDATA section out as a string and then parse that part separately.

I also use the groovy with method in the second example for a somewhat shorter way of accessing the same data.