Service Virtualization

Expand all | Collapse all

Extract data and value from REST

  • 1.  Extract data and value from REST

    Posted Apr 06, 2017 06:48 AM

    Hi Team,

    I have below request coming in lisa.vse.request and the requirement is to extract Argument e.g.URLPARAM0, metaData properties e.g. Postman-Token unique to a transaction (assuming this doesn't change) and if it matches certain value send it to virtual invocation else continue with live. This is little urgent appreciate if you can look into this and help me.

     

    Possible solution I could think is to write assertion script in listener step to capture the incoming data and set execution method to Live.

     

    lisa.vse.request ] {id=0, operation="GET /v1/A1/{URLPARAM0}/{URLPARAM1}/abcd/", arguments=URLPARAM0=12345678901234&URLPARAM1=4, attributes=, metaData=HTTP-Method=GET&HTTP-URI=/v1/A1/12345678901234/4/abcd&HTTP-Version=1.1&Host=localhost:31156&Connection=keep-alive&Cache-Control=no-cache&User-Agent=Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36&Postman-Token=3d793263-8b53-c2d4-157a-b978252eaefa&Accept=*/*&Accept-Encoding=gzip, deflate, sdch, br&Accept-Language=en-US,en;q=0.8&lisa.vse.request.client.id=0:0:0:0:0:0:0:1:53811, matchTolerance=Exact, binary=false, body=(non-binary)<null>]



  • 2.  Re: Extract data and value from REST

    Broadcom Employee
    Posted Apr 06, 2017 09:41 AM

    You could run your virtual service in "Dynamic" mode, and then put something like this in the Routing step

     

    requestMessage = lisa_vse_request.getBodyText();

    Postman_Token="3d793263-8b53-c2d4-157a-b978252eaefa";

    if requestMessage.contains(Postman_Token) return ExecutionMode.EFFICIENT;

    return ExecutionMode.LIVE;



  • 3.  Re: Extract data and value from REST

    Posted Apr 07, 2017 07:12 AM

    Thanks Rick. Did you mean in router step replace default entries import com.itko.lisa.vse.ExecutionMode;

    return ExecutionMode.EFFICIENT; to the one you mentioned above? Also please let me know if I need to import any class



  • 4.  Re: Extract data and value from REST

    Broadcom Employee
    Posted Apr 07, 2017 11:52 AM

    You will need to keep the following line & replace the rest with the code mentioned by Rick.Brown

    import com.itko.lisa.vse.ExecutionMode; 



  • 5.  Re: Extract data and value from REST

    Posted Apr 08, 2017 03:53 PM

    Prem_Bairoliya and Rick.Brown..version I am using is 7.5.2. when above code is run in "DYNAMIC" mode I am getting step error | Message: An error occurred resolving dynamic execution mode to a concrete mode, however have not seen this error when I run in "LIVE" mode.



  • 6.  Re: Extract data and value from REST

    Broadcom Employee
    Posted Apr 10, 2017 06:43 AM

    Please post the contents of your router step. My pseudo-code isn't syntax-checked, so I'm sure it needs modification.

    Doing a quick visual syntax check (I added brackets to the "if" line), I think your Router step should read like this:

     

    import com.itko.lisa.vse.ExecutionMode;

    requestMessage = lisa_vse_request.getBodyText();

    Postman_Token="3d793263-8b53-c2d4-157a-b978252eaefa";

    if(requestMessage.contains(Postman_Token)) return ExecutionMode.EFFICIENT;

    return ExecutionMode.LIVE;

     



  • 7.  Re: Extract data and value from REST

    Posted Apr 10, 2017 07:11 AM

    Hi Rick, below is the code..

     

    import com.itko.lisa.vse.ExecutionMode;
    requestMessage = lisa_vse_request.getBodyText();
    URLPARAM0="77741900528607";
    if (requestMessage.contains(URLPARAM0)) return ExecutionMode.EFFICIENT;
    return ExecutionMode.LIVE;

     

    ============================================================================
    | LISA Exception:
    ============================================================================
    | Step: Virtual Service Execution Router
    ----------------------------------------------------------------------------
    | Message: An error occurred resolving dynamic execution mode to a concrete mode.
    ----------------------------------------------------------------------------
    | Trapped Exception: Sourced file: inline evaluation of: ``// This script must return either an enum entry from ExecutionMode or // a stri . . . ''
    | Trapped Message: Sourced file: inline evaluation of: ``// This script must return either an enum entry from ExecutionMode or // a stri . . . '' : at Line: 8 : in file: inline evaluation of: ``// This script must return either an enum entry from ExecutionMode or // a stri . . . '' : requestMessage .contains ( URLPARAM0 )

    Target exception: java.lang.NullPointerException: Null Pointer in Method Invocation



  • 8.  Re: Extract data and value from REST

    Posted Apr 10, 2017 08:38 AM

    Add quote marks ( " ) around URLPARAM0 on line 8. You need to send a string to the .contains method.

    if (requestMessage.contains( "URLPARAM0" ) )



  • 9.  Re: Extract data and value from REST
    Best Answer

    Broadcom Employee
    Posted Apr 10, 2017 08:57 AM

    That will search the request for the string "URLPARAM0", rather than searching for the string "77741900528607".

     

    I see the same error only when I set my responseBody to null, which leads me to think that the body is blank, and the request message is just a GET.

     

    If this is the case, it's the whole request message we want, instead of lisa_vse_request.getBodyText().

     

    Change the line:

    requestMessage = lisa_vse_request.getBodyText();

    to:

    requestMessage = lisa_vse_request.toString();

    This brings back the entire request, including your URL arguments, rather than just the request body, and should allow the "contains" to work as expected.



  • 10.  Re: Extract data and value from REST

    Posted Apr 10, 2017 09:11 AM

    Yep, sorry 'bout that.  Brain spasm. I was thinking parameter list contains key not the string requestMessage.  



  • 11.  Re: Extract data and value from REST

    Broadcom Employee
    Posted Apr 10, 2017 09:24 AM

    A less of a sledgehammer way would be to use a getAttributes parameter list, so you're right, but it would mean some extra lines of scripting, and would need to be aware if the attribute had been moved to an argument (all too much work for me  )



  • 12.  Re: Extract data and value from REST

    Posted Apr 10, 2017 09:53 AM

    Thanks Rick.Brown J_NeSmith ..working now .