Service Virtualization

 View Only
  • 1.  Best practice to handle dynamic responses?

    Posted Feb 16, 2016 07:47 AM

    Hello

     

    We have a particular case, where we need to process the request in order to determine and construct the response.

     

    In the request we have 1 to n different keys, that are valid (existing) or not.

    Out of the valid keys we need to identify the distinct ones...

    The response should then consist of all the distinct keys, each paired with some dummy-data.

     

     

    Request:

    <tns:imageParams>

        <img:imageParam>

            <img:id>123456</img:id> <!-- does exist -->

        </img:imageParam>

        <img:imageParam>

            <img:id>999999</img:id> <!-- does NOT exist -->

        </img:imageParam>

        <img:imageParam>

            <img:id>123456</img:id>

        </img:imageParam>

    </tns:imageParams>

     

    Response:

    <v1:getListResponse xmlns:v1="http://...">

        <v1:images>

            <v11:image  xmlns:v11="http://...">

                <v11:id>123456</v11:id>

                <v11:content>bytes[]</v11:content>

            </v11:image>

        </v1:images>

    </v1:getListResponse>

     

     

    What's the best practice to handle such dynamic cases? How and where would you construct the response?

    Thanks in advance!!

     

    Carine



  • 2.  Re: Best practice to handle dynamic responses?
    Best Answer

    Posted Feb 16, 2016 10:44 PM

    This is definitely a challenging case and probably not extremely straight forward to implement as it will require embedding some "business logic" into the VSM.  If at all possible, we typically recommend to stay away from integrating application specific logic into your services and stick to virtualizing based on a set of well defined test cases with known responses.

     

    Of course, sometimes you just have to add some level of customization to make a usable solution.  In a case like above what I would do is add a custom JSR-223 (Scripting) step into your model after the Listen step.  You can pull out the sections of XML you're interested in working with via Xpath.

     

    The question I would have is how do you identify which keys are valid and which aren't?  It should be pretty easy to pull out all the id's using XPath, then use a script to filter out the ones you don't want.  Then build the response XML inserting the id's you pulled out and filtered.  You can then store that response XML in a property using something like:

     

    testExec.setStateObject("fl_getListResponse", getListResponse); // getListResponse will be your constructed XML string

     

    Then in your service image in the response you can reference {{fl_getListResponse}} where you want that snippet of XML to go.

     

    Hope that helps get you thinking in the right direction.



  • 3.  Re: Best practice to handle dynamic responses?

    Posted Feb 17, 2016 10:23 AM

    Thank you for the quick answer!! That helps indeed. :-)

     

    In fact we first thought about constructing the response in the match-script and defining it as a property. But unfortunately the property is then not known in the response.

    Your approach sounds good and works… Thanks a lot!!

     

    I will define valid keys to be all keys starting with xy. That should be easy.