Service Virtualization

  • 1.  replace the value in response in live invocation scenario

    Posted Jan 13, 2017 10:32 AM

    Hi,

     I need one quick help in replacing some of the xml elements values from response.

    Scenario: I need to invoke live service in my model, capture the response, based on the value of xml element “A”, I need to replace the value of element “B”, and then send the response.

    What I did: I captured the live response, since response comes in a responsee list, I got the response body as text by-       response = response.getBodyText()

    Now I can access the response body as text. I am able to send the response back as transient response.

    Issue: before sending response, based on the value of xml element “A”, I need to replace the value of element “B”, and then send the response. I am trying to use xpath and not getting the replacement. Not sure if regular expression will help me.

    Ex: in following response body

    </PRegistration>

                  <Registration>

                 <Status />

                   <Type>

                      <code>500000</code>

                      <description>my no</description>

                    </Type>

                    <Number>900</Number>

                    <Identification>

                      <Name>Obj</Name>

    ………

     

    If value of <code> is 500000 then replace the value of <Number> with “abc”

     

    Please guide me how to achieve this.

     

    Thanks,



  • 2.  Re: replace the value in response in live invocation scenario

    Broadcom Employee
    Posted Jan 13, 2017 04:34 PM

    You can add an "Execute script" step after the LIVE invocation step and write some code in it for the functionality you are looking. 

    . First get the body of the VSE or LIVE transient response.

    . Then update the response with the functionality what you are looking. 

    . Change the updated response back to transient response.

    . Set updated transient response to lisa.vse.live.response. 

    . Then set the JS"Execute script" step to go to  Responder. Now you should get the updated response. 

     

    Thanks,

    Prema



  • 3.  Re: replace the value in response in live invocation scenario

    Posted Jan 17, 2017 12:47 PM

    Thanks for your response Prema. I have already done this but I am stuck at beanshell scripting part in jsr script where I need to modify response before send it. In my example, response body looks like-

     

    </PRegistration>

     

                  <Registration>

     

                 <Status />

     

                   <Type>

     

                      <code>500000</code>

     

                      <description>my no</description>

     

                    </Type>

     

                    <Number>900</Number>

     

                    <Identification>

     

                      <Name>Obj</Name>

     

    ………

     

     

     

    and the element <code> comes many no of time. What I need is the sample script to modify the response. if the value of <code> is 500000 then replace the value of corresponding <Number> element with “abc”. The value of <Number> is dynamic and will change every time.

     

     

     

    When the response body is modified, I know the way how can I send the response back to the responder step.

     

     

     

    Appreciate your quick help.



  • 4.  Re: replace the value in response in live invocation scenario

    Broadcom Employee
    Posted Jan 18, 2017 01:37 PM

    Hi,

     

    I am sorry don't have a sample code to update the response. CA Services might help you with the code.

     

    Thanks,

    Prema



  • 5.  Re: replace the value in response in live invocation scenario

    Posted Jan 26, 2017 09:10 AM

    As gadpr08 indicated, there is no OOTB asset for this type of requirement, and I doubt that CA services personnel have shareable examples that accomplish exactly what you are trying to do.  The most efficient implementation would be to use Groovy or Java to construct a DOM object, iterate over the element nodes, and make the replacements you need.  Stackoverflow can provide some code samples to get you started.



  • 6.  Re: replace the value in response in live invocation scenario
    Best Answer

    Posted Jan 26, 2017 11:39 AM

    Thanks Prema and Joel.

     

    My solution required to modify the values in response and not in the request and the solution in the post Modify argument values in request to send in LIVE SYSTEM/Image Validation mode? 

    talk about the "modification in request arguments"

     

    Response arguments are not available explicitly, that's the reason it can't be modify straight away. I tried to write Beanshell script and use regex to find the value, match and replace the same.

     

    I am done with the solution and it is working as expected.

     

    Thanks for all the help.

     

    Regards,

    Ritu



  • 7.  Re: replace the value in response in live invocation scenario

    Broadcom Employee
    Posted Jan 25, 2017 12:51 PM

    Look at the community post Modify argument values in request to send in LIVE SYSTEM/Image Validation mode? 

     

    In that post Surya provided some sample code to change request argument, you can try using the code to update your response.