Service Virtualization

Expand all | Collapse all

How to switch between live and mock data?

  • 1.  How to switch between live and mock data?

    Posted Dec 24, 2018 08:49 AM

    How can I implement SV which demands that for one or two scenario with this BAN,give back a virtualized response and for others,go live and then fetch response and give it back to user?



  • 2.  Re: How to switch between live and mock data?

    Broadcom Employee
    Posted Dec 26, 2018 10:08 AM

    Hi Abishek,

     

    Basically the "stand in" execution mode already provides this functionality. (If you are not familiar with this then please have a look in the documentation for the functionality of this execution mode)

     

    But I believe for your specific "stand-in" functionality a small adaption to the virtual service model will be needed.

    Because the default stand in execution mode will return the META response if the signature of the incoming request matches. And I suspect if it is just one request argument (BAN) that you want to depend on for either going playback or for going live then you will always get the META response instead of the Live response.

     

    You can do the following:

    • Replace the content of your META response with the string "META"
    • Add a scripted assertion as the last assertion of your "VS Image Response Selection" step
    • Define scripted assertion as follows.
      • Name: If META response
      • If FALSE then go to LIVE INVOCATION
      • Script:

     

                   return lisa_vse_response.get(0).getBodyText().equals("META");

     

    The above script line is from the top of my head, not tested

     

     

    Cheers,

    Danny



  • 3.  Re: How to switch between live and mock data?

    Broadcom Employee
    Posted Dec 26, 2018 10:09 AM

    Apologies, this should say:

     

    • Define scripted assertion as follows.
      • Name: If META response
      • If TRUE then go to LIVE INVOCATION
      • Script:

     

                   return lisa_vse_response.get(0).getBodyText().equals("META");



  • 4.  Re: How to switch between live and mock data?

    Posted Jan 22, 2019 01:34 AM

    Thanks for reply . I added a scripted assertion in VSM at VSI Image response selection step .

     

    import com.itko.lisa.vse.stateful.model.TransientResponse;
    import com.itko.lisa.vse.stateful.model.Response;
    import java.util.List;

    List responseList = testExec.getStateObject("lisa.vse.response");
    TransientResponse response = responseList.get(0);
    String respText = testExec.parseInState(response.getBodyText());
    if(respText=404)
    return true;
    else
    //testExec.setStateValue("responseBodyText", respText);
    return false;

     

    I have kept meta value to 404

     

    Added logic: If true -> goto Virtual HTTPS Live invocation

    I have created two virtual service on two different port numbers and trying out this logic.

    Can you please help



  • 5.  Re: How to switch between live and mock data?

    Broadcom Employee
    Posted Jan 22, 2019 02:47 AM

    Hi Abishek,

     

    You mention 'I have kept meta value to 404', what exactly did you set to the value "404"? Was it the response body payload content? Or was it the http-response-code in the response metadata?

     

    Your script examines the response body payload content, it does NOT check the value of the Http-response-code on the response metadata tab.

     

    So, if you have replaced the XML or the JSON response payload with the value "404" (one single line, no spaces before or after this number) then fix the following issue in your script:

     

    if (respText.equals("404")

     

    Just FYI,

    • you used "=" which is the assignment operator, you were not comparing, you assigned "404" as a value to the string respText.
    • Secondly, even if you would have used the equals-operator "==" it would NOT have worked because this checks if the two variables are pointing to the same object.
    • If you want to check if two string objects have the same value then you need to use the .equals() method

     

    Cheers,

    Danny



  • 6.  Re: How to switch between live and mock data?

    Posted Jan 22, 2019 03:47 AM

    Thats helped Danny.Thx very much .

     

    I had put 404 in response payload. Now,I want to route to live service when certain input parameter has some value .For ex:

    {
    "filterByFields": [
    {
    "fieldName": "category",
    "fieldValues": [
    "SIM_STARTER_KIT"
    ]
    },
    {
    "fieldName": "billingType",
    "fieldValues": [
    "POSTPAID"
    ]
    },
    {
    "fieldName": "skuNumber",
    "fieldValues": [
    "TM6760V3"
    ]
    }
    ],
    "sortByFields" : [ {
    "fieldName" : "featured",
    "sortOrder" : "ASC"
    } ],
    "crpList": ["1"]
    }

     

    If filterByFields_2_fieldValues_0 =TM6760V3 ,then send virtualized response,else go to live endpoint.

     

    I tried to change the same code likewise for request but it picked meta response anyways.

     


    import com.itko.lisa.vse.stateful.model.Request;
    import java.util.List;

    List requestList = testExec.getStateObject("lisa.vse.request");
    Request request = requestList.get(0);
    String reqText = testExec.parseInState(request.filterByFields_2_fieldValues_0.getBodyText());
    if(reqText.equals("TM6760V3"))
    return false;
    else

     

    return true;

     

    Is there a way that we can save "filterByFields_2_fieldValues_0" as property as then add assertion accordingly .

     

    Thanks in advance,

    Abhishek



  • 7.  Re: How to switch between live and mock data?

    Broadcom Employee
    Posted Jan 22, 2019 04:28 AM

    Ok,

     

    The easiest way for you to get the value of argument filterByFields_2_fieldValues_0 into a property would be to check that argument as a magic string inside your VSI. Once it is marked as a magic string then after the "VS Image Response Selection" step the argument is available as a property under the name "request_filterByFields_2_fieldValues_0"

     

    The test becomes:

     

    if (request_filterByFields_2_fieldValues_0.equals("TM6760V3"))

      return false;

    else

       return true;



  • 8.  Re: How to switch between live and mock data?

    Posted Jan 22, 2019 05:06 AM

    Thanks Danny.That helped again. Now,I want to add one more check point like if my operation name is abc, then look for this parameter and give this output else go live.

     

    import com.itko.lisa.vse.ExecutionMode;
    String opname=testExec.getStateObject("OPERATION");
    log.info(opname);
    if (opname.equals("POST /v9/catalog/products"))
    {
    if (request_filterByFields_2_fieldValues_0.equals("TM6760V3"))
    return false;

    }
    else
    return true;

     

    Can you plz suggest what is wrong here?



  • 9.  Re: How to switch between live and mock data?

    Broadcom Employee
    Posted Jan 22, 2019 05:50 AM

    Hi,

     

    That if statement can be like:

     

    if (lisa_vse_request.getOperation().equals("POST /v9/catalog/products"))

     

    Cheers,

    Danny



  • 10.  Re: How to switch between live and mock data?

    Posted Jan 22, 2019 07:49 AM

    Thanks very much Danny. 

     

    Regards,

    Abhishek



  • 11.  Re: How to switch between live and mock data?

    Posted Jan 24, 2019 07:25 AM

    Hi Danny/Team,

     

    One more query now.

     

    I need to pass 8-9 headers for a get /post or put request and then switch between real and live based on a header value. I used a request response pair but its not reading it as header,its taking the headers in recorded raw request.

    Any idea what could be wrong

     

    My request was :

     

    GET /v3/cart/{cartId} HTTP/1.1

    Content-Type:application/json
    correlationId:653221387
    applicationId:MyTMO
    usn:c0828877c8cf7af1
    phoneNumber:4049668747
    channelId:WEB
    clientId:ESERVICE
    transactionBusinessKey:BAN
    transactionBusinesskeyType:958373853
    transactionId:25849a24-9223-49fd-9d77-c3cca32e77ee
    transactionType:AAL
    Authorization:eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IlJFRlVNakF4T0E9PSJ9

     

    Thanks,

    Abhishek



  • 12.  Re: How to switch between live and mock data?

    Broadcom Employee
    Posted Jan 25, 2019 05:25 AM

    These HTTP headers are found under the Request Meta Data tab.

    Add a Request Data Copier DPH to your Listen step and copy the meta data parameters to properties.

    In your script you then have access to these properties and can test their values and make your decision to go live or virtual.

     

    Cheers,

    Danny



  • 13.  Re: How to switch between live and mock data?

    Posted Feb 18, 2019 06:42 AM

    Hi Danny, 

     

    Thanks for sharing.Do have some documentation on it?I did add request data copier step but do I have to add one parameter at a time?Also,I have around 10 flows where I want the same set of headers. And on top of that,I want to write my logic which would depend on a property value.

     

     

    Script would be similar to :

    Also,is there a script that would copy all the headers from one flow to another flow?

     

    // This script should return a boolean result indicating the assertion is true or false
    import com.itko.lisa.vse.stateful.model.TransientResponse;
    import com.itko.lisa.vse.stateful.model.Response;
    import java.util.List;

    if (lisa_vse_request.getOperation().equals("operation name"))
    {
    if (header.paramname.equals("value"))
    return false;
    else
    return true;
    }



  • 14.  Re: How to switch between live and mock data?

    Broadcom Employee
    Posted Feb 20, 2019 04:50 AM

    Re. Request Data Copier:

     

    Only one Request Data Copier DPH is needed. It is an all-or nothing copy operation depending on which types of attributes you tick. If you tick metadata then all the metadata values will be copied to properties. What you enter is either a prefix to the existing name, or a suffix (or both). So, in your printscreen prefix "request_" makes sense but suffix "phoneNumber" does not.

    Example, if in metadata there is an argument called "abc" and "xyz" , then by adding prefix "request_" in RDC DPH you will have available 2 properties request_abc and request_xyz.

     

    Re. Also,is there a script that would copy all the headers from one flow to another flow?

     

    I don't understand. What do you mean by "flow", do you mean you have around 10 VSMs?

     

    Re. script

     

    I guess there are 2 ways. If it is about testing the value of a metadata argument you can use (without using a RDC DPH):

     

    lisa_vse_request.getMetaData().get("xyz").equals("value")

     

    If you use a RDC DPH configured as discussed above then use:

     

    request_xyz.equals("value")

     

    Cheers,

    Danny



  • 15.  Re: How to switch between live and mock data?

    Posted Feb 20, 2019 04:56 AM

    Thanks very much Danny.will try that out.Meanwhile,can you check why this code is not redirecting me to live endpoint rather than giving me meta response.

     

    import com.itko.lisa.vse.stateful.model.Request;
    import com.itko.lisa.vse.ExecutionMode;
    String opname=testExec.getStateObject("OPERATION");
    log.info(opname);

    if (opname.equals("POST /v9/catalog/products"))
    {
    if (request_filterByFields_2_fieldValues_0.equals("TM6760V3"))
    return false;

    else
    return true;
    }

     

    my request is 

     

    {
    "filterByFields":[
    {
    "fieldName":"category",
    "fieldValues":[
    "SIM_STARTER_KIT"
    ]
    },
    {
    "fieldName":"billingType",
    "fieldValues":[
    "POSTPAID"
    ]
    },
    {
    "fieldName":"skuNumber",
    "fieldValues":[
    "TM6760V3"
    ]
    }
    ],
    "sortByFields":[
    {
    "fieldName":"featured",
    "sortOrder":"ASC"
    }
    ],
    "crpList":[
    "1"
    ]
    }

     

    And I have mentioned live endpoint in config too.



  • 16.  Re: How to switch between live and mock data?

    Posted Feb 20, 2019 05:25 AM

    sorry to bother you.this is working fine.will work on dph part and let you know.



  • 17.  Re: How to switch between live and mock data?

    Broadcom Employee
    Posted Feb 20, 2019 05:26 AM

    There are 2 things you need to check to get this code to work:

    1. Is the name correct? I assume that you took the name from the .vsi, so it should be correct. Anyways it appers to be correct.
    2. Does this request_filterByFields... object exist? There are 2 most used ways to create a proper seaprate object originating from an argument in the incoming request object:
      1. The argument is marked as Magic String in the .vsi
      2. A Request Data Copier DPH is added which copies all arguments to properties with suffix "request_"

    Which of these 2 options have you used?



  • 18.  Re: How to switch between live and mock data?

    Posted Feb 25, 2019 06:06 AM

    Hi ,Somehow,I am failing to hit service with dynamic parameter in LISA.

    Request:

     

    GET /v3/cart/{cartId}
    Content-Type:application/json
    correlationId:5454
    applicationId:MyTMO
    usn:c0828877c8cf7af1
    phoneNumber:781263
    channelId:WEB
    clientId:fdfe
    transactionBusinessKey:BAN
    transactionBusinesskeyType:4344
    transactionId:25849
    ransactionType:AAL
    Authorization:abc

     

    Response 

    any json

     

    After creating the service,cart id is not coming in arguments.and when I hit it,I am getting error.



  • 19.  Re: How to switch between live and mock data?

    Broadcom Employee
    Posted Feb 25, 2019 07:30 AM

    This might be because the REST DPH in your VSM is not (anymore) in sync with the signature in your .vsi file.

    Have you checked if you still have the folowing rule defined in the DPH in your VSM?

     

    GET /v3/cart/{cartId}

     

    Cheers,

    Danny



  • 20.  Re: How to switch between live and mock data?

    Posted Feb 25, 2019 07:41 AM

    yeah Danny.They look good.Actually,my VSM would have many signatures having POST,PUT and other calls.So,my base path in vsm is / . I have added Argument cartId just for making it work.Still,no luck

     

     

    My Attributes

     



  • 21.  Re: How to switch between live and mock data?

    Broadcom Employee
    Posted Feb 25, 2019 08:41 AM

    These are printscreens from your virtual service image.

     

    Can you show a printscreen from your REST DPH configuration from your virtual service model?



  • 22.  Re: How to switch between live and mock data?

    Posted Feb 25, 2019 08:45 AM

    Hi danny,

    No DPH configured .

     



  • 23.  Re: How to switch between live and mock data?

    Broadcom Employee
    Posted Feb 25, 2019 08:58 AM

    Ok, then we have the root-cause of your problem. Agreed?

     

    Your incoming request operation is example: GET /v4/cart/12345678

    If you don't have a REST DPH containing the rule GET /v4/cart/{cartId} then the operation of your incoming request object stays GET /v4/cart/12345678, and that's how you reach your .vsi, and in your .vsi you have no transaction with the operation GET /v4/cart/12345678, hence no match.

    If you have a REST DPH containing the rule GET /v4/cart/{cartId} then the DPH will replace the operation of your incoming request object with GET /v4/cart/{cartId} and it will parse out the carId from that incoming operation and add an argument cartId=12345678 to your request object. Now if you reach your .vsi with that updated request object you will have a match.

     

    Cheers,

    Danny