Forum Discussion

Borigor's avatar
Borigor
New Contributor
11 years ago

[SOAP UI MockService] data Map usage for generating dynamic

Hello, all!

Wonder if there possibility to use Map for passing generated values and following handling in XML template in SOAP UI Mock Service.
Also curious if there some conditional structure allowed in template.

E.g., I have small piece of code:

import com.eviware.soapui.support.GroovyUtils

def myMap = new StringToObjectMap()
myMap.put('1','TRF')
myMap.put('2','BGO')
def isFour = true
myMap.put('3','MSQ')

def groovyUtils = new GroovyUtils(context)
def xmlParser = new XmlParser()
def responseContent = xmlParser.parse( groovyUtils.projectPath + "my.xml" )


and in my.xml:


<one>
<two>${myMap.'1'}</two>
<three>${myMap['2']}</three>
<if $isFour><four>${myMap['3']}</four></if>
</one>


Now response returned has empty 'two' & 'three' tags

Maybe it is possible to use some Object structure for data handling. The final aim of desired approach is generation response xml consists from rather big and reach parts of xml in cycle.

Would be grateful for useful advices.

3 Replies

  • soapug's avatar
    soapug
    New Contributor
    I would recommend that you use the approach mentioned in the SoapUI documentation section.

    Specifically, create a few XML templates using the JSP-like syntax to get to the context. SoapUI chooses the template based on the return value of the script. For example:

    Script:
    context._3 = 'MSQ'
    if (isFour) {
    context._4 = 'Whatever'
    return "Response with 4"
    }
    return "Response"

    Template "Response with 4":
    <three>${_3}</three>
    <four>${_4}</four>

    You could customize the templates and do things your way but you'll lose out on a lot of built-in functionality, like the response template name appearing in the web page for the mock service. My team finds this invaluable when actually using the mock services. This is because you can actually see what template the mock service is using.

    Alternatively, you can also just use Groovy, without using SoapUI. Creating Web Services in Groovy is pretty easy.
  • Borigor's avatar
    Borigor
    New Contributor
    soapug wrote:
    I would recommend that you use the approach mentioned in the SoapUI documentation section.

    Specifically, create a few XML templates using the JSP-like syntax to get to the context. SoapUI chooses the template based on the return value of the script. For example:

    Script:
    context._3 = 'MSQ'
    if (isFour) {
    context._4 = 'Whatever'
    return "Response with 4"
    }
    return "Response"

    Template "Response with 4":
    <three>${_3}</three>
    <four>${_4}</four>

    You could customize the templates and do things your way but you'll lose out on a lot of built-in functionality, like the response template name appearing in the web page for the mock service. My team finds this invaluable when actually using the mock services. This is because you can actually see what template the mock service is using.

    Alternatively, you can also just use Groovy, without using SoapUI. Creating Web Services in Groovy is pretty easy.


    Thank you, soapug!

    Unfortunately, this approach does not suit me, because I need to compose Response from repeated (20-50 times) almost the same complex (about 200 tags each) XML structures with different tag/attributes values. It means that I need only one template, but with different placeholder values for each iteration. That's why I need Map and conditional handling (for customizing some slight differences in composed xml's). Something like handling XML in FreeMarker or Velocity.
  • soapug's avatar
    soapug
    New Contributor
    Hi Borigor,

    Yes, I understand. You can certainly do this, but the code may not look pretty.

    Think of SoapUI as a framework leaning heavily towards ease of use. If you need to do things like FreeMarker or Velocity then just use that.

    Good luck,

    Soapug