How to add sum of two number in virtual service response?

book

Article ID: 110955

calendar_today

Updated On:

Products

CA Application Test Service Virtualization CA Continuous Application Insight (PathFinder) CA Service Virtualization (DevTest / LISA / VSE / Application Test)

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.

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% 

import com.itko.util.ParameterList; 
import com.itko.util.Parameter; 

// Arguments, Attributes, and Metadata are all ParameterList 
ParameterList args = lisa_vse_request.getArguments(); 

String Num0 = args.get("arg0"); 
String Num1 = args.get("arg1"); 

int outgoingReturn = Integer.parseInt(Num0)+Integer.parseInt(Num1); 
testExec.setStateValue("returnValue",outgoingReturn); 

Then use the "returnValue" in the VSI response.
 

Additional Information

Sample Request and Responses with default response and modified response (Option 1).

Request: 
--------------- 
<?xml version="1.0" encoding="utf-8"?> 
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
<soapenv:Body> 
<add xmlns="http://c.b.a"> 
<!--n1 is optional--> 
<n1>1</n1> 
<!--n2 is optional--> 
<n2>1</n2> 
</add> 
</soapenv:Body> 
</soapenv:Envelope> 

Default Response: 
------------------------- 
<?xml version="1.0" encoding="utf-8"?> 
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
<soapenv:Body> 
<addResponse xmlns="http://c.b.a"> 
<!--return is optional--> 
<return>{{=[:return:1]}}</return> 
</addResponse> 
</soapenv:Body> 
</soapenv:Envelope> 

* 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