Search code examples
groovysoapuiassertions

Asserting the unique value from the SOAP respose in Script Assertion


I´m new to testing with SoapUI, so hopefully I can summarize what I want to do.
I send a request for a web service and in the response a tag is used several times.
I want to check if the values of this tag are unique over a Script Assertion (SoapUI 5.0) in this response. But I wasn´t able to find anything regarding exactly this on the web.
When I execute my script its always passed.

// from script assertion get the response 
def response = messageExchange.getResponseContent()
// parse the XML
def xml = new XmlSlurper().parseText(response)
// get all KampagnenID
def KampagnenID = xml.'**'.findAll { it.name() == 'KampagnenID' }
// check that each KampagnenID aka Kundenansprache Source Code is unique
assert KampagnenID.each.unique:true 

The response contains this so one value is twice but this won´t be recognized:

              <Kampagne>
                 <KampagnenID>NH_B_PoC_Lounge_01</KampagnenID>
              </Kampagne>                     
              <Kampagne>
                 <KampagnenID>NH_NBO_KS_01</KampagnenID>
              <Kampagne>                     
              <Kampagne>
                 <KampagnenID>NH_B_PoC_Lounge_01</KampagnenID>
              </Kampagne> 

Solution

  • You can just check the size of the unique ids vs total ids.

    def xml = new XmlSlurper().parseText(xml)
    def result = xml.'**'.findAll { it.name() == 'KampagnenID' }*.text()
    assert result.size() == result.unique().size(), 'There are duplicate ids'