Service Virtualization

  • 1.  REST call with dynamic URL

    Posted Jun 12, 2016 12:21 PM


    Hi!

    For a test that I'm developing uing DevTest 91: I have a situation where the first thing I do is that I create something in SUT (I send in key1) and that will give me a response(salesID). Based on that response I later (not just after) need  to make another  REST call bases on the return value. I have inserted a JSR-223 script that builds a hash  = saleryMap[key1,salesID].

    I would then like to make a REST call like:

    api/v1/salesopportunity/{{salaryMap[1004]}}/offer that I hoped would work. Instead I get "ERROR - Could not evaluate expression '=salaryMap[1004]'"

    I'm not at all used to working in workstation (nor DevTest), is there perhaps something I need to do to "export" my hasmap from the JSR-223 step so all consequent steps is aware of the map??

     

    Kind regards,

    Fredrik



  • 2.  Re: REST call with dynamic URL

    Broadcom Employee
    Posted Jun 12, 2016 12:29 PM

    Your map is deleted when the JSR-223 step completes. One way to save a map is to use a SharedModelMap.

    DevTest 8.0 - Scripting Guide - V1.1.pdf contains an explanation.



  • 3.  Re: REST call with dynamic URL

    Posted Jun 13, 2016 04:52 AM

    Based on your explanation my take is you only have a single key that you want to substitute dynamically in the REST URL. If that's correct then you can leverage testExec for this purpose ( testExec - specifies the current test execution environment).

     

    In JSR-223 step you can set the key like this:

    testExec.setStateValue("salarymapkey","salesID1004");

    Later in any subsequent steps you can access the value of salarymapkey in REST URL like this

    api/v1/salesopportunity/{{salarymapkey}}/offer

     

    The value of property salarymapkey won't change until the next cycle of test execution till you fetch new salesID.

     

    Assuming there are multiple key values in that case you should look at leveraging SharedModelMap as Rick explained earlier.

     

    Populate the map in JSR-223 step as follows (sample code snippet)

    import com.itko.lisa.vse.SharedModelMap;

     

    if (!SharedModelMap.containsKey(salarymapkey)) {

      SharedModelMap.put(salarymapkey,value);

    }

    REST URL should be constructed like this

    api/v1/salesopportunity/{{=com.itko.lisa.vse.SharedModelMap.get(salarymapkey);}}/offer



  • 4.  Re: REST call with dynamic URL
    Best Answer

    Posted Jun 13, 2016 08:26 AM

    You need to save the hash pointer into the DT variable table.  (setStateObject or the shareable api),

    and then either use that in the variable substitution syntax.. OR, just use the groovy syntax, to construct the string on the fly.

    hash = testexec.getStateObject(hashkename)

    url  = "api/v1/salesopportunity/" + hash.get(transkey) + "/offer"



  • 5.  Re: REST call with dynamic URL

    Posted Jun 14, 2016 02:29 PM

    Thanks!

    I used this approach, and was happy with it!