How to add sum of two number in virtual service response?
book
Article ID: 110955
calendar_today
Updated On:
Products
CA Application TestCA Continuous Application Insight (PathFinder)Service Virtualization
Issue/Introduction
When request has 2 arguments and response is the "sum" of the 2 arguments, how can we create the response with "sum" value.
How to add sum of two number in virtual service response dynamically?
Environment
All supported DevTest environments.
Cause
None, Customization.
Resolution
There are multiple ways you can achieve it. For example, if you have the request arguments as n1 and n2.
* Option 1 - Updating VSI Response: -------------------------------------------------- Adding something like below in the VSI response will give the sum of arguments n1 and n2. {{=(request_n1+request_n2)}}
* Option 2 - Match Script in the VSI: --------------------------------------------------- String incomingValue0 = incomingRequest.getArguments().get("arg0"); String incomingValue1 = incomingRequest.getArguments().get("arg1");
int outgoingReturn = Integer.parseInt(incomingValue0)+Integer.parseInt(incomingValue1); testExec.setStateValue("returnValue",outgoingReturn);
Then use the "returnValue" in the VSI response.
* Option 3 - Scriptable DPH in the VSM-->Listen step: ----------------------------------------------------------------------- %beanshell%
* Selected "Is Numeric" option for n1 and n2 arguments in VSI. * Modified the <return> element in the above response as below and redeployed the VS. <return>{{=[:return:1]}}</return> ==> <return>Sum is {{=(request_n1+request_n2)}}</return>
Documentation of the classes and methods that can be used in Match script or Scriptable DPH are available in: ---------------------------------------------------------------------------------------------------------------------------------------------------- . Product Installation\doc\SDKJavaDoc . Product Installation\doc\AgentJavaDoc