Service Virtualization

Expand all | Collapse all

Scripted assertion to control the flow based on status value

rajeswari.krishnan

rajeswari.krishnanSep 01, 2017 08:32 AM

  • 1.  Scripted assertion to control the flow based on status value

    Posted Aug 29, 2017 10:19 AM

     

    # monika mehta

     

    Response : {"deviceStatus":"InEligible","deviceStatusMessage":{"message":"Instore Pickup order has already been placed for this d

     

    Scripted assertion

    if(testExec.getStateValue("deviceStatus").equals("Eligible"))
    return true;
    else if(testExec.getStateValue("deviceStatus").equals("InEligible"))
    return false;

     

    I have used a json filter and the property variable is deviceStatus from the above response If its eligible i need to go to next step , else not

     

    But i am getting error as shown below. Pls help to get rid of this error.

     

    The assertion of type "Assert by Script Execution" had outcome:
    Script, // This script should return a boolean result indicating the assertion is true or false
    if(testExec.getStateValue("deviceStatus").equals("Eligible"))
    return true;
    else if(testExec.getStateValue("deviceStatus").equals("InEligible"))
    return false;
    execution error so we assume false: java.lang.IllegalArgumentException: key can not be empty

     



  • 2.  Re: Scripted assertion to control the flow based on status value

    Broadcom Employee
    Posted Aug 29, 2017 11:42 AM

     

    When I add a similar filter and scripted assertion, it seems to work fine for me. Can you please share the complete error log along with the step if possible to debug this.

     

    Thanks,

    Surya.



  • 3.  Re: Scripted assertion to control the flow based on status value

    Posted Aug 30, 2017 04:04 AM
      |   view attached

    PFA. Need this for two steps from the responses.

     

    WarrantyEligibility and deviceleigibility

    Attachment(s)

    zip
    E2E flow.zip   413 KB 1 version


  • 4.  Re: Scripted assertion to control the flow based on status value

    Posted Aug 29, 2017 11:59 AM

    Hi Rajeshwari,

     

    I tried same code in my machine, it worked. Can you try taking device Status property value in String variable and then compare? or  Add a do-nothing step and add scripted assertion mentioned above.

     

    Let me know if you still get same error.



  • 5.  Re: Scripted assertion to control the flow based on status value

    Posted Aug 30, 2017 04:02 AM

    I tried the below two types : PFA script . Pls see first rest setp -warrantyeleigibilty and second rest step Deviceeligibility

    String deviceStatus = new String("Eligible");

    String deviceStatus;

    Still getting same error 

    The assertion of type "Assert by Script Execution" had outcome:
    Script,

    String deviceStatus;
    if(testExec.getStateValue("deviceStatus").equals("Eligible"))
    return true;
    else if(testExec.getStateValue("deviceStatus").equals("InEligible"))
    return false;
    execution error so we assume false: java.lang.IllegalArgumentException: key can not be empty

     

    Pls see and help oon this issue

    Attachment(s)

    zip
    E2E flow.zip   413 KB 1 version
    zip
    E2E flow.zip   413 KB 1 version


  • 6.  Re: Scripted assertion to control the flow based on status value

    Posted Aug 30, 2017 10:06 AM

    Below is the script that I mentioned:

    String deviceStatusValue = testExec.getStateValue("deviceStatus");

    testExec.log(deviceStatusValue );

    if(deviceStatus.equals("Eligible"))
    return true;
    else if(deviceStatus.equals("InEligible"))
    return false;

     

    Try running this script and check if it is displaying correct value in Test Event logs.

     

    Thanks,

    Monika



  • 7.  Re: Scripted assertion to control the flow based on status value

    Posted Aug 31, 2017 06:23 AM
    Still getting the same error for above script as well in devtest 8.2
    Tet events logs
    By default always its giving false and aborting 
    The assertion of type "Assert by Script Execution" had outcome: 
     Script, String deviceStatusValue = testExec.getStateValue("deviceStatus");
    testExec.log(deviceStatusValue );
    if(deviceStatus.equals("Eligible"))
    return true;
    else if(deviceStatus.equals("InEligible"))
    return false; execution error so we assume false: java.lang.IllegalArgumentException: key can not be empty


  • 8.  Re: Scripted assertion to control the flow based on status value

    Posted Aug 31, 2017 09:40 AM

    Typically, the "execution error so we assume false" message means that there is a bug in your script that is causing an error. When this happens DevTest has to make one of two choices. 1) Since there is an error, does the script return true or 2) does the script return false. DevTest chooses the latter and returns false along with the message.

     

    The script above has an error.  

    The code sets the value of 'deviceStatus' to the variable deviceStatusValue, yet the 'IF' statement checks the variable 'deviceStatus' which is undefined anywhere in the script.

    Try adding some _logger statements to isolate where the code is erring.

    Then run a test in ITR Mode. The statements will display in the Events tab and in the Workstation.log file. If running in VSE, the statements will show up in the vse.log file.  Here's a snippet to help you isolate the issue:

     

    // log entry point into assertion
    _logger.info(" <<< Entering Assertion");
    String deviceStatusValue = testExec.getStateValue("deviceStatus");
    _logger.info(" <<< deviceStatusValue is: {}", deviceStatusValue);
    // you might consider dropping everything to lower case to remove potential upper/lower issues
    // if ( "eligible".equals( deviceStatusValue.toLowerCase() ) )
    // 
    if ("Eligible".equals( deviceStatusValue) ) {
       _logger.info(" <<< value is equal to Eligible...  returning true" );
       return true;
    } else if ( "InEligible".equals( deviceStatusValue) ) {
       _logger.info(" <<< value is equal to InEligible...  returning false" );
       return false;
    }
    // I added this logic since the original code used 'else if' and could fall through
    // the bottom of the assertion without setting a return value 
    _logger.info("<<< Value is neither Eligible or InEligible... returning false");
    return false;
    Here's an example of the above when sending "InEligible" as the Device Status:


  • 9.  Re: Scripted assertion to control the flow based on status value

    Posted Aug 31, 2017 10:44 AM

    deviceStatus is not undefined . Its a filtered property variable whose value is printed and confirmed , where as deviceStatusValue is not able to store the value of the filtered value. Sting is not needed only as as by default in json response the format is string  type 



  • 10.  Re: Scripted assertion to control the flow based on status value

    Posted Aug 31, 2017 01:15 PM
      |   view attached

    I agree with you that TestExec probably contains a property called deviceStatus. And, I agree that case sensitivity is likely unimportant as you suggested.

     

    Please understand that we can only support you based upon the script code you posted.   

    The posted script contained the following lines:

    String deviceStatusValue = testExec.getStateValue("deviceStatus");
    testExec.log(deviceStatusValue );

    if(deviceStatus.equals("Eligible"))

     

    On the basis of this code fragment, the IF statement reference to deviceStatus should be causing what is most likely a NULL pointer exception.  In the above code fragment, the variable 'deviceStatus' is local to the scripted assertion. It is not a reference to the value of the property key associated with the JSON's "deviceStatus". The testExec.getStateValue("deviceStatus") in the code is setting a local variable called deviceStatusValue which is never used in the IF / ELSE.  When the script encounters a NULL pointer exception, the assertion will always return false.

     

    Edit...

    Additionally, deviceStatusValue is also a local variable.  So, wrapping this local variable in {{ }} notation will not result in a printout of the value of the variable.

    You can use _logger.info, as I did in the script fragment I posted, to print the value out if you need to.

     

    As sursu06 suggested, if possible, please post the actual script with the exception you see, and we will try to help you get this resolved.

     

    Added:

    Added an upload of a Test Case that parses the JSON provided in this post. An assertion fires checking for Eligible, InEligible, and a status other than Eligible or InEligible.  Logging added so everyone can review the ITR Events Tab.  Assertion branches to Error Message Step when value is any value other than "Eligible".

    Attachment(s)



  • 11.  Re: Scripted assertion to control the flow based on status value

    Posted Aug 31, 2017 10:46 AM

    Joel

     

    {"MSISDN":"4704240188","deviceDetailList":{"device":[{"imei":"353753074724490","productName":"Motorola - V3 05 Dark Pearl Gray","productSKU":"XSAMG928TB32","manufacturer":"Motorola","model":"V3 05 Dark Pearl Gray","firstUseDate":"2017-08-26T22:49:49-07:00","lastUseDate":"2017-08-29T22:49:49-07:00","deviceType":"T-Mobile - New","deviceRepairLocation":"","warrantyExpirationDate":"2018-08-26T00:00:00-07:00","warrantyType":"OEM","PHPIndicator":"0","salesOffice":"","deviceEligiblity":{"deviceStatus":"Eligible","deviceStatusMessage":{},"openOrderDetail":{},"informationalMessageList":{"informationalMessages":[{"message":"","url":""}]},"managerOverrideRequired":false},"deviceFeeList":{"fee":[{"SKUType":"RestockingFee","SKU":"RSKFEE","SKUGroup":"RSKFE","fee":649.99},{"SKUType":"OOWFee","SKU":"OOWFEE","SKUGroup":"TLACC","fee":200.0}]},"IsAWREEligible":true}]},"PHPVendor":"OEM"}

     

    The above is the json response , it has nothing to do with lower or upper case . In script also checking for the same value. 



  • 12.  Re: Scripted assertion to control the flow based on status value

    Posted Aug 31, 2017 06:41 AM

    Other than this is there is anyother better assertion pls suggest that to check the value



  • 13.  Re: Scripted assertion to control the flow based on status value

    Posted Aug 31, 2017 10:40 AM

    {{deviceStatusValue}} - just tried printing this using a outputlog message . value is not getting printed



  • 14.  Re: Scripted assertion to control the flow based on status value

    Broadcom Employee
    Posted Aug 31, 2017 11:10 AM

    Rajeswari,

     

    Is it possible for you to share the ITR result for the test execution? This will help us look at the test flow and debug.

     

    Thanks,

    Surya.



  • 15.  Re: Scripted assertion to control the flow based on status value

    Posted Sep 01, 2017 08:42 AM
      |   view attached

    Thanks Surya 

     

    Have attached my script. PLs see and help on this . Need this urgently for deliverables 

    Attachment(s)

    zip
    Rest flows.zip   422 KB 1 version


  • 16.  Re: Scripted assertion to control the flow based on status value

    Posted Aug 30, 2017 06:18 AM

    Also i have given like if true to go to next step, but since its false because of execution error still its going to next step



  • 17.  Re: Scripted assertion to control the flow based on status value

    Posted Aug 31, 2017 11:03 AM

    heyjo01 I forgot to change variable in rest of the script, Thanks for pointing out.

    rajeswari.krishnan This script works fine, I have provided a log message statement to debug where your script is failing. and with log message statement you will be able to see below highlighted.

     

     

    You can always use Ensure Property Matches expression for the same.

     

    Thanks,

    Monika



  • 18.  Re: Scripted assertion to control the flow based on status value

    Posted Sep 01, 2017 08:26 AM

    I did change that . Still i m getting same error . 

     

    Some how in test events logmessage is not logging. I have used sripted assertion in first step . Is there a condition once used it wont be triggered again ? 

     

    I tried like below aslo

     

    String deviceStatusValue = testExec.getStateValue("dev_status");
    testExec.log(deviceStatusValue );
    if(deviceStatusValue.equals("Eligible"))
    return true;
    else
    return false; 

     

    Yes Ensure property matches expression. I have attahced script. Pls see whether you can open and debug and help

     

    I need it urgently



  • 19.  Re: Scripted assertion to control the flow based on status value

    Posted Sep 01, 2017 08:32 AM
      |   view attached

    # hi monika . PFA script for the flows 

     

    Attachment(s)

    zip
    Rest flows.zip   422 KB 1 version