Service Virtualization

  • 1.  Store value from response into a property

    Posted Jun 16, 2017 08:03 AM

    Hello,

     

    I have build a virtual service (transport:http - data: soap xml) so that when it responds, it also sends a feedback request asynchronous (by playing with think time). This feedback request contains an ID that needs to be the same as the ID coming from the response.

     

    So I'm looking for a way to extract the ID value from the response file and save it into a property.

    The property then needs to be reused in the feedback request.

     

    Could someone help me on this? Or maybe there is another way available to achieve this?

     

    Thanks in advance,

    Dave



  • 2.  Re: Store value from response into a property
    Best Answer

    Broadcom Employee
    Posted Jun 16, 2017 03:23 PM

    Dave,  please look at this communities post:

    https://communities.ca.com/message/241865342?commentID=241865342#comment-241865342 

     

    let me know if this helps ? 



  • 3.  Re: Store value from response into a property

    Posted Jun 16, 2017 04:52 PM

    +1 abrsh01

    Also, DevTest uses a technique we call Magic Strings.

    Refer to this link for some guidance:  Magic Strings - DevTest Solutions - 10.1 - CA Technologies Documentation  

     

    If the ID is an Element in the XML request (for example,  <ID>10137658</ID> ), DevTest should be creating an argument when the SOAP or XML DPH executes -- unless you have overridden and removed the argument. Look at the VSI and validate if the ID argument is present in the request side arguments.  If it is there, it can be marshalled into the response using magic string notation.  

    For example, if the ID were in the XML as (and subsequently seen in the argument list in the VSI):

    <body>

        <fieldA>

          <ID>10137658</ID>

    one could use  {{=request_fieldA_ID;/*defaultValue*/}} in the Magic String notation.

     

    Notes:

    - The default minimum length of an XML argument value considered for Magic String is set to 3 digits.  (see property:  lisa.magic.string.length=3 in the lisa.properties file)  If the ID is less than this, you can still use Magic Strings; however, the recorder will ignore the field when trying to automatically determine Magic Strings. This approach (ignoring short element values) prevents a lot of extra overhead and the creation of potentially faulty magic strings in responses.  

     

    - If the ID is described using an XML Attribute (i.e., <fieldA ID="10137658" >field a value</ID> ), you may need to use a DPH and XPath the ID (or via script extract the ID into a property) to use as a Magic String. 



  • 4.  Re: Store value from response into a property

    Posted Jun 19, 2017 02:00 AM

    Thanks Shiney and Joel for your input,

     

    @Shiney, i will take a look at your link, looks like it is what i need.

     

    @Joel, I actually need the reverse. Not starting from request property and use it with magic string, but take the ID from the response, save it as a property and use that in the feedback request (an additional raw soap request step in the VSM after the responder step).

     

    I will try to give feedback today or tomorrow, if i was able to work it out based on the link from Shiney.



  • 5.  Re: Store value from response into a property

    Posted Jun 21, 2017 08:41 AM

    I was able to solve my issue based on the link from Shiney.

    For anyone that would find it usefull, here is how i did it:

     

    import java.util.ArrayList;
    import com.itko.lisa.vse.stateful.model.TransientResponse;
    import javax.xml.parsers.*;
    import javax.xml.xpath.*;
    import org.w3c.dom.*;
    import org.xml.sax.InputSource;

    //If the operation is not a useLoginRequest, exit the script
    if (! lisa_vse_request.getOperation().equals("useLoginRequest")){
        System.out.println("No useLoginRequest detected, exiting script");
        return;
    }

    // Retrieve the responses-list from the environment
    // and get the first response (only response for synchronous HTTP)
    ArrayList alResponse = testExec.getStateObject("lisa.vse.response");
    TransientResponse response = alResponse.get(0); // No looping over list of responses needed as there can only be one
    responseBody = response.getBodyText(); //get hold of response body

    // Get the value of the requestID argument from the response body using xPath
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document dDoc = builder.parse(new InputSource(new StringReader(responseBody)));

    XPath xPath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xPath.compile("//requestId");
    String requestID = expr.evaluate(dDoc);

    //Set the requestID value into a property prpRequestID
    testExec.setStateValue("prpRequestID", requestID);