Service Virtualization

Expand all | Collapse all

I have a Request which contains an attribute with negative value. I want to dynamically map this value in response BUT instead of negative the response should have only positive value with it. I am using DevTest 9.5.1.6

  • 1.  I have a Request which contains an attribute with negative value. I want to dynamically map this value in response BUT instead of negative the response should have only positive value with it. I am using DevTest 9.5.1.6

    Posted Mar 28, 2017 01:30 AM

    example for this request i should have given response : - 

    Request: 

    <Bill>

    <Amount> -17.2 </Amount>

    </Bill>

    Response:

    <BillGenerated>

    <RequestAmount> -17.2 </RequestedAmount>

    <AmountDeducted> 17.2</AmountDeducted>

    <BillGenerated>



  • 2.  Re: I have a Request which contains an attribute with negative value. I want to dynamically map this value in response BUT instead of negative the response should have only positive value with it. I am using DevTest 9.5.1.6

    Posted Mar 28, 2017 01:32 AM

    Its the first time i am posting anything here. Please tag me if there is such option here while replying message me. 
    Thanks Guys



  • 3.  Re: I have a Request which contains an attribute with negative value. I want to dynamically map this value in response BUT instead of negative the response should have only positive value with it. I am using DevTest 9.5.1.6

    Posted Mar 28, 2017 10:39 AM

    VineetTevetiya you can use the scriptable data protocol to retrieve the request argument, make it positive value, store it in a variable and the use the variable in the response. 

     

    import com.itko.util.ParameterList;
    ParameterList args = lisa_vse_request.getArguments();
    String amount= args.get("************Request Arg Name*********");
    testExec.setStateValue("amount_positive", Math.abs(Integer.parseInt(amount)));

     

    in the response refer it as {{amount_positive}}

     

    Not sure if there is any simpler option available !!



  • 4.  Re: I have a Request which contains an attribute with negative value. I want to dynamically map this value in response BUT instead of negative the response should have only positive value with it. I am using DevTest 9.5.1.6

    Posted Apr 03, 2017 01:18 AM

    Thanks AnnaRamadoss. I am quite new to LISA. Can you please tell me where do i do this scripting ? And what is scriptable data protocol ?



  • 5.  Re: I have a Request which contains an attribute with negative value. I want to dynamically map this value in response BUT instead of negative the response should have only positive value with it. I am using DevTest 9.5.1.6
    Best Answer

    Posted Mar 28, 2017 11:22 AM

    UPDATE: If the value does not begin with a "-" sign, the below logic has a flaw and does not work.  Updated example below. 

     

    Using your payload as the example, 

    1) When the request comes into the VSM, DevTest attempts to parse out the XML and convert the elements into key/value pairs.

    2) These converted elements are used in the VSI to evaluate the request.  They can also become candidates for Magic String processing which marshals data from the input request into the response.

    3) DevTest creates properties by iterating over the XML document. The resulting property names end up following a pattern where the key side of the K/V pair is a concatenation of its respective parent element names.  Further, DevTest places its own internal "request_" value in front of the property name to provide some ease of access.  Using the below XML request data as an example, coupled with a REST DPH and an XML Data Protocol DPH:

    POST /user/sample/amounts HTTP/1.1

     

    <payload>
      <Bill>
        <Amount>-17.2</Amount>
      </Bill>
    </payload>

    The resulting VSI contains a transaction that looks like this:

    Notice that the 'Bill_Amount' key represents this element's position in the DOM.  Instances where amount is part of list will result in DevTest post fixing a value of 1 to n so that each K/V pair remains unique. 

    4) DevTest utilizes the Magic String concept to move the Bill_Amount from the incoming request into the response to make the transaction as 'real' as possible.  This is implemented using {{ }} notation having a reference to the incoming element name.  For example, to magic string the value as-is including the '-' sign:

    5) Your scenario has to go a step further in that your requirement is to remove the '-' sign from the amount.  To do this, we can use some BeanShell scripting and add a small amount of code to implement the behavior to remove the '-' sign.

    The code that is added to the response is as follows:

    <payload>
      <BillGenerated>
        <RequestAmount>{{=request_Bill_Amount;/*-17.2*/}}</RequestAmount>
        <AmountDeducted>{{=if ("-".equals( testExec.getStateValue("request_Bill_Amount").substring(0, 1) ) ) return testExec.getStateValue("request_Bill_Amount").substring(1); else return testExec.getStateValue("request_Bill_Amount")}}</AmountDeducted>
      </BillGenerated>
    </payload>

    So, using the R/R pair with a REST call to the service yields the following:

     

    In this example, the META transaction is configured to handle the response using the Magic String logic, so when the incoming bill amount changes, so does the response output:

    Using updated script to cater for no "-" sign on input request:



  • 6.  Re: I have a Request which contains an attribute with negative value. I want to dynamically map this value in response BUT instead of negative the response should have only positive value with it. I am using DevTest 9.5.1.6

    Broadcom Employee
    Posted Mar 28, 2017 12:02 PM

     <AmountDeducted>{{=java.lang.Math.abs(Float.parseFloat(testExec.getStateValue("request_Bill_Amount")));}} </AmountDeducted>

    will work, so long as the original amount is a number (it'll return the literal string above if it isn't).



  • 7.  Re: I have a Request which contains an attribute with negative value. I want to dynamically map this value in response BUT instead of negative the response should have only positive value with it. I am using DevTest 9.5.1.6

    Posted Mar 28, 2017 10:51 PM

    Thanks @Rick Brown, it was really helpful



  • 8.  Re: I have a Request which contains an attribute with negative value. I want to dynamically map this value in response BUT instead of negative the response should have only positive value with it. I am using DevTest 9.5.1.6

    Posted Mar 28, 2017 10:50 PM

    Thanks Joel Nesmith, it was really helpful



  • 9.  Re: I have a Request which contains an attribute with negative value. I want to dynamically map this value in response BUT instead of negative the response should have only positive value with it. I am using DevTest 9.5.1.6

    Posted Apr 03, 2017 10:09 AM

    Hi VineetTevetiya, you need to add the Scriptable Data protocol on the request side during virtual service creation as below

     

     

    when you click next, you will get the script screen as below where you code.

     



  • 10.  Re: I have a Request which contains an attribute with negative value. I want to dynamically map this value in response BUT instead of negative the response should have only positive value with it. I am using DevTest 9.5.1.6

    Posted Apr 03, 2017 10:11 AM