Service Virtualization

Expand all | Collapse all

How to Change the Response in VSE with ScritableDatProtocol

  • 1.  How to Change the Response in VSE with ScritableDatProtocol

    Posted Nov 25, 2015 05:27 PM
      |   view attached

    Hi ,

     

         I am using LISA 7.5 version and I am trying to get the reponse element and create the filter by below code. I am not sure why the error message coming at below red string

     

    CODE;

             ParameterList allParams = lisa_vse_response.getArguments();

            for (int paramLoop=0;paramLoop<allParams.size();paramLoop++){

            if(allParams.get(paramLoop).getName().contains("WFContext_")){

            testExec.setStateValue("WFFilter_response_" + allParams.get(paramLoop).getName(),allParams.getParameterValue(allParams.get(paramLoop).getValue()));

            }

            }

     

    Error:

     

    java.lang.RuntimeException: Exception evaluating script: Sourced file: inline evaluation of: ``  import com.itko.util.ParameterList;      testExec.setStateValue("WFFilter_resp . . . '' : Typed variable declaration : Error in method invocation: Method getArguments() not found in class'com.itko.lisa.vse.stateful.model.Response' : at Line: 2 : in file: inline evaluation of: ``  import com.itko.util.ParameterList;      testExec.setStateValue("WFFilter_resp . . . '' : lisa_vse_response .getArguments ( )

     

     

    java.lang.RuntimeException: Exception evaluating script: Sourced file: inline evaluation of: ``import com.itko.util.ParameterList;  ParameterList allParams = lisa_vse_response . . . '' : Typed variable declaration : Error in method invocation: Method getArguments() not found in class'com.itko.lisa.vse.stateful.model.Response' : at Line: 2 : in file: inline evaluation of: ``import com.itko.util.ParameterList;  ParameterList allParams = lisa_vse_response . . . '' : lisa_vse_response .getArguments ( )

     

     

      at com.itko.lisa.vse.stateful.protocol.scriptable.ScriptableDataProtocolHandler.updateResponse(ScriptableDataProtocolHandler.java:75)

      at com.itko.lisa.vse.stateful.protocol.DataProtocol.updateResponse(DataProtocol.java:140)

      at com.itko.lisa.vse.stateful.protocol.Marker.getTransaction(Marker.java:152)

      at com.itko.lisa.vse.stateful.protocol.TransactionLine$Line.next(TransactionLine.java:234)

      at com.itko.lisa.vse.stateful.protocol.TransactionLine$Line.next(TransactionLine.java:190)

      at com.itko.lisa.vse.stateful.recorder.TrafficMerger.getNextTransaction(TrafficMerger.java:477)

      at com.itko.lisa.vse.stateful.recorder.TrafficMerger.loadStatelessTransactions(TrafficMerger.java:431)

      at com.itko.lisa.vse.stateful.recorder.TrafficMerger.populateTraffic(TrafficMerger.java:121)

      at com.itko.lisa.vse.stateful.recorder.TrafficMerger.populateTraffic(TrafficMerger.java:97)

      at com.itko.lisa.vse.stateful.recorder.RecordingSession.storeRecordedTransactions(RecordingSession.java:859)

      at com.itko.lisa.vse.stateful.recorder.BuildProgressPanel$Worker.run(BuildProgressPanel.java:414)

      at java.lang.Thread.run(Unknown Source)



  • 2.  Re: How to Change the Response in VSE with ScritableDatProtocol

    Broadcom Employee
    Posted Nov 25, 2015 05:56 PM

    The error is telling you that LISA doesn't have arguments in the response. You need to lisa_vse_response.getBodyText() or lisa_vse_response.getBodyBytes() and perform your actions against that.

     

    I think the DevTest Scripting Guide has examples of scriptable DPHs. Although it was written for DevTest v8.x, all the facilities should still work in LISA v7.x (except for the %beanshell% that you need in v8 but not in v7, because v8 supports JSR-223 plug-in scripting languages and v7 doesn't)



  • 3.  Re: How to Change the Response in VSE with ScritableDatProtocol

    Posted Sep 15, 2016 10:05 AM

    Hi I am also facing the same error :

     

     

    ============================================================================
    | Error in Script
    ============================================================================
    | Step: Evaluate Transient response
    ----------------------------------------------------------------------------
    | Message: bsh.TargetError: Sourced file: inline evaluation of: ``import com.itko.lisa.vse.stateful.model.TransientResponse; import java.util.Lis . . . '' : at Line: 6 : in file: inline evaluation of: ``import com.itko.lisa.vse.stateful.model.TransientResponse; import java.util.Lis . . . '' : list .size ( )

    Target exception: java.lang.NullPointerException: Null Pointer in Method Invocation
    in inline evaluation of: ``import com.itko.lisa.vse.stateful.model.TransientResponse; import java.util.Lis . . . '' at line number 6
    ----------------------------------------------------------------------------
    | Trapped Exception: bsh.TargetError: Sourced file: inline evaluation of: ``import com.itko.lisa.vse.stateful.model.TransientResponse; import java.util.Lis . . . '' : at Line: 6 : in file: inline evaluation of: ``import com.itko.lisa.vse.stateful.model.TransientResponse; import java.util.Lis . . . '' : list .size ( )

    Target exception: java.lang.NullPointerException: Null Pointer in Method Invocation
    in inline evaluation of: ``import com.itko.lisa.vse.stateful.model.TransientResponse; import java.util.Lis . . . '' at line number 6
    | Trapped Message: javax.script.ScriptException: bsh.TargetError: Sourced file: inline evaluation of: ``import com.itko.lisa.vse.stateful.model.TransientResponse; import java.util.Lis . . . '' : at Line: 6 : in file: inline evaluation of: ``import com.itko.lisa.vse.stateful.model.TransientResponse; import java.util.Lis . . . '' : list .size ( )

    Target exception: java.lang.NullPointerException: Null Pointer in Method Invocation
    in inline evaluation of: ``import com.itko.lisa.vse.stateful.model.TransientResponse; import java.util.Lis . . . '' at line number 6
    ----------------------------------------------------------------------------
    STACK TRACE
    javax.script.ScriptException: bsh.TargetError: Sourced file: inline evaluation of: ``import com.itko.lisa.vse.stateful.model.TransientResponse; import java.util.Lis . . . '' : at Line: 6 : in file: inline evaluation of: ``import com.itko.lisa.vse.stateful.model.TransientResponse; import java.util.Lis . . . '' : list .size ( )

    Target exception: java.lang.NullPointerException: Null Pointer in Method Invocation
    in inline evaluation of: ``import com.itko.lisa.vse.stateful.model.TransientResponse; import java.util.Lis . . . '' at line number 6
    at bsh.BshScriptEngine.evalSource(BshScriptEngine.java:97)
    at bsh.BshScriptEngine.eval(BshScriptEngine.java:61)
    at javax.script.AbstractScriptEngine.eval(Unknown Source)
    at com.itko.lisa.test.ScriptExecHandler.executeScript(ScriptExecHandler.java:670)
    at com.itko.lisa.test.ScriptExecHandler.executeScript(ScriptExecHandler.java:427)
    at com.itko.lisa.test.UserScriptNode._execute(UserScriptNode.java:210)
    at com.itko.lisa.editor.UserScriptNodeEditor.execute(UserScriptNodeEditor.java:214)
    at com.itko.lisa.editor.UserScriptNodeEditor.access$000(UserScriptNodeEditor.java:49)
    at com.itko.lisa.editor.UserScriptNodeEditor$1.actionPerformed(UserScriptNodeEditor.java:73)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
    Caused by: java.lang.NullPointerException: Null Pointer in Method Invocation
    at bsh.Name.invokeMethod(Name.java:844)
    at bsh.BSHMethodInvocation.eval(BSHMethodInvocation.java:75)
    at bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:102)
    at bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:47)
    at bsh.BSHBinaryExpression.eval(BSHBinaryExpression.java:52)
    at bsh.BSHIfStatement.evaluateCondition(BSHIfStatement.java:63)
    at bsh.BSHIfStatement.eval(BSHIfStatement.java:46)
    at bsh.Interpreter.eval(Interpreter.java:664)
    at bsh.Interpreter.eval(Interpreter.java:758)
    at bsh.Interpreter.eval(Interpreter.java:747)
    at bsh.BshScriptEngine.evalSource(BshScriptEngine.java:89)
    ... 44 more
    ============================================================================

     

     

     

    I tried rick brown's solution too. But same error is coming every time.

    My VSI contains 1 request and 2 different responses. i want to change the content of 2nd response by mapping few elements of 2nd response with request xml.

    Can anyone please help on dis.



  • 4.  Re: How to Change the Response in VSE with ScritableDatProtocol

    Posted Sep 15, 2016 11:12 AM

    Hi ,

     

     For Response we have to go with  getBodyAsString();  , After that manipulate as XMl Content using org.w3c.dom.*; functions . After you update the response set the response as below 

    Sample Code 

     

    String theBody = lisa_vse_response.getBodyAsString();

    org.w3c.dom.Document doc = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new org.xml.sax.InputSource(new java.io.StringReader(theBody)) );
    org.w3c.dom.Element element = doc.getDocumentElement();

    /** Add you code to change resposne values **//

    java.io.StringWriter outWriter = new java.io.StringWriter();
    javax.xml.transform.stream.StreamResult result = new javax.xml.transform.stream.StreamResult( outWriter );
    javax.xml.transform.Transformer xformer = javax.xml.transform.TransformerFactory.newInstance().newTransformer();
    xformer.transform(new javax.xml.transform.dom.DOMSource(doc), result);
    StringBuffer sb = outWriter.getBuffer();
    lisa_vse_response.setBody(sb.toString());



  • 5.  Re: How to Change the Response in VSE with ScritableDatProtocol

    Posted Sep 15, 2016 08:32 PM

    Thanks for the reply. I will definately try your code, but the issue is that what is syntax of getting the request element name and then mapping the same with a particular response element as magic string implementation is not working with 2nd response after I ran my previous script. can you please help



  • 6.  Re: How to Change the Response in VSE with ScritableDatProtocol

    Posted Sep 15, 2016 12:15 PM

    try -- 

    String theBody = lisa_vse_response.get(1).getBodyAsString();

    Responses are contained in a list and you need to get at the 2nd entry in the list.  Your code is responsible for checking the size of the list to ensure that you don't get out of bounds.  



  • 7.  Re: How to Change the Response in VSE with ScritableDatProtocol

    Posted Sep 15, 2016 08:33 PM

    Thanks for the reply. I will definately try your code, but the issue is that what is syntax of getting the request element name and then mapping the same with a particular response element as magic string implementation is not working with 2nd response after I ran my previous script. can you please help



  • 8.  Re: How to Change the Response in VSE with ScritableDatProtocol

    Posted Sep 16, 2016 09:11 AM

    Sorry, I am not sure I understand completely...  Accessing the request element name and placing it in a response is a feature we call "magic string".  For example: In the VSIs 2nd Response, magic strings can move data from the incoming request arg list to the response.  Example notation:

    <someResponse2Element>{{=request_myInputField1;/*defaultValue*/}}<someResponse2Element> 

     

    It looks like you want to roll your own code.  You can do this; however, I am not sure I understand how the code fragment in the original post intends to place values into the response body.  Are you going to use the new property values you are setting in your code as magic strings in the response?  If you are not going to do this, I don't see how your example code is going to affect any data in the response.  

     

    I quickly added some code below -- pardon typos.  I added some logging so you can see some displays in the log file -- and just in case I accidentally injected some typos. Take a look and see if this will accomplish what you are trying to do?

     

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

    // Get the Incoming Request Arg List
    ParameterList args = lisa_vse_request.getArguments();
    String thisName;
    String thisValue;

     

    // Added logging so you can see what the routine is doing at runtime remove

    // or change to "_logger.debug" else these lines print every time 
    _logger.info(" <<<< Starting to loop over the incoming request arg list");

    // Loop over the incoming request args
    for (int i=0; i < args.size(); i++) {
        // get the argument as a Parameter then get its name and value
        Parameter thisParm = args.get( i );
        thisName = thisParm.getName();
        thisVal = thisParm.getValue();


        _logger.info(" <<<< Arg Name is: {}\r\n Arg Val is: {}", thisName, thisVal);

     

        // unclear if property name 'contains' or 'startsWith' appears to startWith 
        if( thisName.contains( "WFContext_" ) ) {
            _logger.info(" <<<< Name contains WFContext");
            testExec.setStateValue("WFFilter_response_" + thisName, thisVal ) );
            _logger.info(" <<<< Set tag name to: {} with value of: {}",

                          testExec.getStateValue( "WFFilter_response_" + thisName ),

                          thisVal );
        } // end if
    } // end for

     



  • 9.  Re: How to Change the Response in VSE with ScritableDatProtocol

    Posted Sep 16, 2016 09:32 AM

    Hi Joel

     

    Thanks for your prompt response. What I am trying to do :

     

    I have created a virtual service using RR pair having 1 request and 2 response. My interface call is not Synchronous to synchronous, instead the client will invoke my Virtual service and will get back the Response1 immediately(same port), but I have to send the 2nd response to different URL not the one which invoked my Virtual service. For that I need my 2nd response to contain some data which should be matched with Request. so Magic String is working very much fine in my 1st response which is going immediately. My next step is to send my 2nd response to different URL for that I wrote a script to take some Request data and put the same data in my 2nd response . is there any way to save some request argument/element value in property file and then use the same while invoking the 2nd response.

     

    I hope you understood my scenario.

     

    Thanks



  • 10.  Re: How to Change the Response in VSE with ScritableDatProtocol

    Posted Sep 16, 2016 09:43 AM

    Thanks for the reply.  In your case, saving off the incoming request arg data into a WFFilter_response_<restOfTagNameHere>  should be OK so long as your second response embeds a magic string {{...}} reference to the saved off property.      

     

    I would recommend that you implement your code prior to the execution of the Responder Step to ensure that the incoming request object is referenceable.



  • 11.  Re: How to Change the Response in VSE with ScritableDatProtocol

    Posted Sep 16, 2016 09:59 AM

    Hi Joel,

     

    As per your code I hope it will match all the request element and map it to the response. But my question is that I want to save some specific argument's value in property file and then when I will invoke 2nd response. I will put that value in place of required response element but not all element.

     

    Also If I implement WFFilter_response_<restOfTagNameHere> in your previous code then where will be the value get saved means in which property file as I need this to refer it in reesponse.

     

    Thanks

     



  • 12.  Re: How to Change the Response in VSE with ScritableDatProtocol

    Posted Sep 16, 2016 10:29 AM

    Both of the code fragments are simply iterating over the incoming args and creating key/value pair properties where the key contains the name of "WFFilter_response_WFContext_<restOfIncomingReqArgName>" and a value equals to the corresponding value that is contained in this element's incoming request.

     

    For example.  Assume that the incoming request argument's key/value pair is:  WFContext_myField=ABCD

     

    Your code fragment and my code fragment are simply creating a key/value pair and storing it in testExec that looks like this: WFFilter_response_WFContext_myField=ABCD.

     

    Magic strings place the value side of the key/value pair into the response.  They do not operate on the key side except to perform the lookup of the value.  So assume your response contains XML and you magic string the above.

     

    <{{=WFFilter_response_WFContext_myField}}>1234</{{=WFFilter_response_WFContext_myField}}>

    creates a response XML containing:

    <ABCD>1234</ABCD>

     

    Whereas:

    <responseElement>{{=WFFilter_response_WFContext_myField}}</responseElement>

    creates a response XML containing:

    <responseElement>ABCD</responseElement>

     

    If you are trying to dynamically create XML where the resulting XML contains the key of:

    <WFFilter_response_WFContext_myField> ...  <WFFilter_response_WFContext_myField> 

    and the XML value is ABCD 

    <WFFilter_response_WFContext_myField>ABCD<WFFilter_response_WFContext_myField>

     

    then you have a lot more coding to do because you will need to dynamically build the response XML not just magic string data from the request.



  • 13.  Re: How to Change the Response in VSE with ScritableDatProtocol

    Posted Sep 16, 2016 10:56 AM

    Hi Joel,

     

    I do not want to create dynamic response xml. I just simply wants to pick the values from property file and put in response xml where it is required only. Rest of the elements will remain same.

     

    Want to add one more query, suppose my 2nd response xml looks like below :

     

    <envelope>

    <header/>

    <body>

    <OperationResponse>

    <responseElement>{{=WFFilter_response_WFContext_myField}}</responseElement>

    <Name>achcada</Name>

    </OperationResponse>

    </body>

    </envelope>

     

    Can I put this response inside Web service Execution XML  step inside Raw xml tab in VSM.

    Does the value of <responseElement> will be picked from property?

     

    Thanks

     



  • 14.  Re: How to Change the Response in VSE with ScritableDatProtocol

    Posted Sep 16, 2016 11:11 AM

    Yes, you should be able to place the response in a Webservice XML step.  DevTest should perform the replacement on the  {{=WFFilter_response_WFContext_myField}} with the value of this property when the message is put on the wire.  DevTest waits as long as possible to perform the substitution for performance reasons.  



  • 15.  Re: How to Change the Response in VSE with ScritableDatProtocol

    Posted Sep 16, 2016 11:15 AM

    Hi Joel,

     

    Thanks for you help.

    I will implement the same and will post the update. These key/value pair got saved in Conf/project file ?

     

    Thanks



  • 16.  Re: How to Change the Response in VSE with ScritableDatProtocol

    Posted Sep 16, 2016 01:43 PM

    No, actually the key/value pairs are placed in the instance of the testExec object that is controlling the incoming request.  The config file is used to store and provide shareable information to the assets that are associated with the project as a whole.  I would not recommend using the config as a place to store these types of properties.



  • 17.  Re: How to Change the Response in VSE with ScritableDatProtocol

    Posted Sep 16, 2016 03:41 PM

    Hi Joel,

     

    I am now bit confused. you said earlier that <responseElement>{{=WFFilter_response_WFContext_myField}}</responseElement> will actually do <responseElement>ABCD</responseElement> but I want to store the request value in key/value pair and then I want to use that request value in my response xml.

    What i think it should be <responseElement>{{=WFFilter_request_WFContext_myField}}</responseElement>

    Please clarify me or can you please put all above code together to one code as I am getting confused, that what to use where. It will be grateful to you.

     

    Thanks

     



  • 18.  Re: How to Change the Response in VSE with ScritableDatProtocol

    Posted Sep 16, 2016 05:07 PM

    OK.  Let's do this... 

    1) Mock up a simple, fake request that contains one element you want copied into a response.  

    2) Identify the request element you want copied into the response.

    3) Mock up an XML response and identify the element you want the request value to be placed inside

     

    Don't worry about including a bunch of elements such as the SOAP header, and everything else.  Let's try to keep it simple.  Also, remove any customer-specific or identifying data so we can keep it generic.

     

    If possible, use the advanced feature and upload as an attachment.  I will try to look at it and give you an option or 2.



  • 19.  Re: How to Change the Response in VSE with ScritableDatProtocol

    Posted Sep 16, 2016 06:04 PM

    I will create a virtual service using 1 simple request and 2 simple response using RR pair methodology. After that which code do I need to place in VSM? Or Can I directly put the code in Web Service Execution XML step?

    You provided many code snippets, so I am confused where to put those in VSM? Kindly provide your email id or let me know how can I send you the .mar file.

     

    Thanks