Service Virtualization

Expand all | Collapse all

Help required on beanshell scripting

Jump to Best Answer
  • 1.  Help required on beanshell scripting

    Posted 07-11-2019 11:56 AM
      |   view attached
    Hi All,

    I tried to achieve the below new requirement with the beanshell script. But i'm facing script errors and logic is failing miserably. Please find the below requirement and help me to achieve it.

    • We'll receive the Orders details in the request message and they're dynamic.
      • So first, these details should be captured from the request and send them in response.
      • Secondly, based on the one particular item number value present in the order details, we need to send the additional tags and values in the response message. For other orders, these additional tags should not come.

    Currently, with help of executable script step- beanshell script, I can able to capture the order details from the request and send them in response. But, I'm failing to achieve the second step. 

    Please find attached the logic i used and part of the xml's screenshot.



    So, if the itemnumber value "845005" comes in the request, we should add the SalesOrderStatusRequestSerialNumbers details additionally and send in the response. For all other orders, we should not send the SalesOrderStatusRequestSerialNumbers details.

    Attachment(s)

    txt
    ExistingLogic.txt   2K 1 version


  • 2.  RE: Help required on beanshell scripting

    Posted 07-12-2019 02:07 AM
    In your requirements I don 't see any reason why you would have to use scripting to realize this? Can all be done by OOTB steps and filters.

    How have you created the basic version of your virtual service? Using request response pairs? In that case the virtual service image creation wizard should have recognized most of the data elements already as magic strings and should have replaced actual values in your response already with {{request_...}} tags.

    Let us know why the standard functionality and features cannot be used in your case?

    Cheers,
    Danny

    ------------------------------
    Cheers,
    Danny
    ------------------------------



  • 3.  RE: Help required on beanshell scripting

    Posted 07-12-2019 08:24 AM
    Hi Danny,

    Thanks for your reply. Yes, it recognized all the elements and i'm able to select the values.
    i.e. 
    request_<some elements>_ItemNumber_1= 845005
    request_<some elements>_ItemNumber_2= 847800
    request_<some elements>_ItemNumber_3= 847716
    request_<some elements>_ItemNumber_4= 847694

    But, to check for the specific condition, i'm getting the errors.

    For example:- 

    //noofitems has the count of item numbers
    for (int i = 1; i <=noofitems; i ++) {  
    if ("{{request_<someelements>_ItemNumber_" + (i) + "}}".eqauls("845005")){     --> I'm getting the error in this line
    //some logic
    }
    else{
    //some logic
    }
    }


  • 4.  RE: Help required on beanshell scripting
    Best Answer

    Posted 07-12-2019 08:34 AM

    The parsing of {{}} gets done before the execution of the script, so "i" won't have a value. If the name of a property you needs is composed dynamically then you need to use testExec.getStateValue(<aJavaStringExpression>).

    So try:

     

    if (testExec.getStateValue("request_<someelements>_ItemNumber_" + i).equals("845005")) {     

    Cheers,

    Danny

     

    ::DISCLAIMER::
    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    The contents of this e-mail and any attachment(s) are confidential and intended for the named recipient(s) only. E-mail transmission is not guaranteed to be secure or error-free as information could be intercepted, corrupted, lost, destroyed, arrive late or incomplete, or may contain viruses in transmission. The e mail and its contents (with or without referred errors) shall therefore not attach any liability on the originator or HCL or its affiliates. Views or opinions, if any, presented in this email are solely those of the author and may not necessarily reflect the views or opinions of HCL or its affiliates. Any form of reproduction, dissemination, copying, disclosure, modification, distribution and / or publication of this message without the prior written consent of authorized representative of HCL is strictly prohibited. If you have received this email in error please delete it and notify the sender immediately. Before opening any email and/or attachments, please check them for viruses and other defects.
    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------





  • 5.  RE: Help required on beanshell scripting

    Posted 07-16-2019 04:20 AM
    Thanks Danny. it's working now.