Service Virtualization

 View Only
Expand all | Collapse all

Generate Random Response for REST VS in CA DevTest 10.2

  • 1.  Generate Random Response for REST VS in CA DevTest 10.2

    Posted Jul 01, 2019 10:23 AM
    Hello Community,

    i'm creating a REST virtual service with the requirement that i have to return a random JSON response : 

    A. 90 % of the response to the VS must be like that : 
    {
              "Status" = "X"
            }


    B.10% of the response :
          {
              "Status" = "Y"
            }

    Any idea how to implement this logic ?

    Thanks in advance for your help.

    Regards,
    Hamza


  • 2.  RE: Generate Random Response for REST VS in CA DevTest 10.2

    Posted Jul 01, 2019 11:09 AM
    Does it have to be random? If it has to be randon than it comes with the consequence that you cannot make it exactly 90% of "X" and 10% of "Y" but only roughly and the more requests you get the closer it will get to 90% and 10%. If that's ok then you can use this:
     {
         "Status" = {{=(((int)(java.lang.Math.random()*100 + 1) > 10)?"X":"Y");}}
     }



    ------------------------------
    Cheers,
    Danny
    ------------------------------



  • 3.  RE: Generate Random Response for REST VS in CA DevTest 10.2
    Best Answer

    Posted Jul 01, 2019 11:17 AM
    Sorry, mistake. I didn't thest that statement actually inside a virtual service, it will output "Status" = X or "Status" = Y whereby X and Y and not in double quotes.
    So make it like:
    {
         "Status" = "{{=(((int)(java.lang.Math.random()*100 + 1) > 10)?"X":"Y");}}"
     }


    ------------------------------
    Cheers,
    Danny
    ------------------------------



  • 4.  RE: Generate Random Response for REST VS in CA DevTest 10.2

    Posted Jul 02, 2019 04:14 AM
    Hello Danny,

    Thanks for your help. it works perfectly.

    Regards,
    Hamza


  • 5.  RE: Generate Random Response for REST VS in CA DevTest 10.2

    Posted Jul 02, 2019 04:14 AM
    Hello Danny,

    Thanks for your help. it works perfectly.

    Regards,
    Hamza


  • 6.  RE: Generate Random Response for REST VS in CA DevTest 10.2

    Broadcom Employee
    Posted Jul 09, 2019 03:07 PM
    Just to let you know the EOL for 10.2 is Feb, 2020. So I would strongly recommend to start planning to upgrade to the latest 10.5.


  • 7.  RE: Generate Random Response for REST VS in CA DevTest 10.2

    Posted Sep 02, 2019 04:46 AM
    Hello Danny,

    Do you have any idea how can i implement the same logic but in static way, without using this Random in JAVA. That dynamic implementation causes a problem issue for the performance of the this virtual service.

    Can i have 1/2 for each response :

    A. 50% of the response to the VS must be like that : 
    {
              "Status" = "X"
            }


    B.50% of the response :
          {
              "Status" = "Y"
            }

    Thanks in advance for your help.

    Regards,
    Hamza


  • 8.  RE: Generate Random Response for REST VS in CA DevTest 10.2

    Posted Sep 02, 2019 04:56 AM

    Hi,

     

    If you do not need the randomly generated answers then there is no need for any scripting. Example; you could reply with response Status X when article number is even and reply with response Status Y when it is odd?

     

    To implement: I understand you have already 5 specific responses in your VSI. Add 3 more responses, put them as last of the specific responses,

    put as comparison operator: regex

    For Status X put as value: .*[02468]

    For Status Y put as value: .*[13579]

     

    You could expand this scenario for more responses, just divide your article numbers amongst the responses as needed

     

    Hope this helps.

     

    Cheers,

    Danny

     

    ::DISCLAIMER::
    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    The contents of this e-mail and any attachment(s) are confidential and intended for the named recipient(s) only. E-mail transmission is not guaranteed to be secure or error-free as information could be intercepted, corrupted, lost, destroyed, arrive late or incomplete, or may contain viruses in transmission. The e mail and its contents (with or without referred errors) shall therefore not attach any liability on the originator or HCL or its affiliates. Views or opinions, if any, presented in this email are solely those of the author and may not necessarily reflect the views or opinions of HCL or its affiliates. Any form of reproduction, dissemination, copying, disclosure, modification, distribution and / or publication of this message without the prior written consent of authorized representative of HCL is strictly prohibited. If you have received this email in error please delete it and notify the sender immediately. Before opening any email and/or attachments, please check them for viruses and other defects.
    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------





  • 9.  RE: Generate Random Response for REST VS in CA DevTest 10.2

    Posted Sep 02, 2019 05:40 AM
    Hi Danny,

    Thanks for your feedback, but the proposition still not clear for me...i have just two responses in VSI to return, one with X and the Other with Y. how to use the operator regex ?

    Regards,
    Hamza


  • 10.  RE: Generate Random Response for REST VS in CA DevTest 10.2

    Posted Sep 02, 2019 08:44 AM

    Hi,

     

    Apologies, due to license issues my DevTest is unavailable and I cannot create any printscreens to show what I mean. So the below printscreen comes from the documentation:

     

    In the Service Image editor, select one of the 2 transactions (not the DEMO) and for your article argument, select "regular expression" as "Comparison Operator" and as value set .*[02468]

    For the other  response do the same and set as value .*[13579]

     

     

    Cheers,

    Danny

     

    ::DISCLAIMER::
    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    The contents of this e-mail and any attachment(s) are confidential and intended for the named recipient(s) only. E-mail transmission is not guaranteed to be secure or error-free as information could be intercepted, corrupted, lost, destroyed, arrive late or incomplete, or may contain viruses in transmission. The e mail and its contents (with or without referred errors) shall therefore not attach any liability on the originator or HCL or its affiliates. Views or opinions, if any, presented in this email are solely those of the author and may not necessarily reflect the views or opinions of HCL or its affiliates. Any form of reproduction, dissemination, copying, disclosure, modification, distribution and / or publication of this message without the prior written consent of authorized representative of HCL is strictly prohibited. If you have received this email in error please delete it and notify the sender immediately. Before opening any email and/or attachments, please check them for viruses and other defects.
    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------





  • 11.  RE: Generate Random Response for REST VS in CA DevTest 10.2

    Posted Sep 02, 2019 08:55 AM
    Hello Danny,

    I understand your solution now, but i have not to modify arguments of the service, i have one argument "arg_VS" and this is what i receive from an other system, then i have to return one time X, one time Y.

    Regards,
    Hamza


  • 12.  RE: Generate Random Response for REST VS in CA DevTest 10.2

    Posted Sep 02, 2019 09:11 AM
    Hi,

    sorry that tI was always talking about "Article Number", I got mixed up with another question which I answered last week.

    Going back to your initial question this morning, I am surprised that the execution of 
    {
         "Status" = "{{=(((int)(java.lang.Math.random()*100 + 1) > 10)?"X":"Y");}}"
     }
    would give a performance issue? Is this for a performance test virtual service? How did your analysis ultimately point to this statement as the root casue?
    Did you put the "think times" of your responses to 0? (I don't know if you edited your VSM, if so also double-check that all the think-times of your VSM steps are at 0.

    What are the possible values of "arg_VS"? Is it a number? If so, you can emploi the same scheme as I described for "article" above.


    ------------------------------
    Cheers,
    Danny
    ------------------------------