Forum Discussion

krispokkuluri's avatar
krispokkuluri
Occasional Contributor
3 years ago

How do we handle empty string dynamically in assertions in ReadyAPI

ERROR: An error occurred [empty String], see error log for details

Hi i have a need to validate two data sources using nested loop on multiple records in several tables Steps

  1. Build a Rest Request
  2. get the datasource
  3. getting the data from db as expected result
  4. write a groovy script inorder to get a value to be validated with db
  5. Asert the values are equal

Issue : I have multiple empty values in the data sources from the api(actual value) and expected value db Question : When i run my script i am getting this error ERROR: An error occurred [empty String], see error log for details for empty values asserted . Please help as i am on a deadline and am aunable to find a solution online .

  • ChrisAdams's avatar
    ChrisAdams
    3 years ago

    Hi,

     

    The below example and function show how you can test for an empty string.  Just create a brand new Groovy script step, paste in the contents and click Go in the Groovy window.  Ensure you can see the logging for the answers....

     

    def value = "1.12";
    log.info("value ${value} is empty?  ${ isEmpty(value)}");
    
    value = "1";
    log.info("value ${value} is empty?  ${ isEmpty(value)}");
    
    value = "";
    log.info("value ${value} is empty?  ${ isEmpty(value)}");
    
    value = null;
    log.info("value ${value} is empty?  ${ isEmpty(value)}");
    
    def isEmpty(stringvalue) {
    
        if(stringvalue == null) {
        	    return true;
        } else {
    		if((stringvalue.length() == 0) ||
    			(stringvalue == "") ) {
    			return true;
    		} else {
    			return false;
    		}
    	}
    }

     

    Also, Groovy Double does not have a tryParse function unlike other languages, but it does throw an exception which might help...  Next example...

    def myValue = "1.12";
    log.info("is ${myValue} parsable as double?  ${canBeDouble(myValue)}");
    
    myValue = "1";
    log.info("is ${myValue} parsable as double?  ${canBeDouble(myValue)}");
    
    myValue = "abc";
    log.info("is ${myValue} parsable as double?  ${canBeDouble(myValue)}");
    
    def canBeDouble(theValue) {
    	try {
    		  def a = Double.parseDouble(theValue);
    		  return true;
    	} catch (NumberFormatException e) {
    		  //the parseDouble failed and you need to handle it here
    		  return false;
    	}
    }

     

    You can then maybe use these examples, or modify them, to test suspect values from the db before trying the assert.

  • richie's avatar
    richie
    Community Hero
    Hey krispokkuluri,

    Im struggling to understand from your description

    Are you saying that you have a REST request that returns a response and you follow this up with a jdbc test step and you want to assert that a value in the REST response is the same as the value returned by the JDBC?

    If you can provide examples of the REST response payload along with the JDBC response as well as the groovy script youre using, someone will be able to help.

    Cheers,

    Rich
    • krispokkuluri's avatar
      krispokkuluri
      Occasional Contributor

      richie My apologies , I thought i was clear in my head , yes you are absolutely right . Please find the snippets in attachment .I hope it is clearer

       

      Reiterating where i am getting this issue  .

      In the UI i beleive you are able to see a few empty values so when i try to validate it with db empty values i am getting that error .

      DB : There's nothing i can do as it's coming from a stored proc .

      Groovy : This is where i am seeking help to modify my logic such that i could handle such empty strings . Kindly help

       

      jdbc output file for jdbc output .

       

      This is my beloow groovy scipt for logic and assertion .

      double commitmentqauterQ1 = commitmentQ1 ? commitmentQ1.toDouble() :1 ;
      double ContractDomainqauterQ1 = contractDomainQ1 ? contractDomainQ1.toDouble() : 1 ;
      double tierPriceqauterQ1 = tierPriceQ1 ? tierPriceQ1.toDouble() : 1 ;
      int prorateddaysQ1 = proratedQ1.toInteger();
      int QtyQ1 = planqtyQ1? planqtyQ1.toInteger() : 0 ;
      double PriceQ1 = tierPriceQ1 ? tierPriceqauterQ1 : intelPrice.toDouble()
      def planCostQ1 = (PriceQ1.toDouble() * QtyQ1.toInteger() * prorateddaysQ1.toInteger() * ContractDomainqauterQ1.toDouble() * commitmentqauterQ1.toDouble()).round(1)
      def expectedCostQ1 = queryplancostQ1.toDouble().round(1)
      //log.info("planCostQ1 $planCostQ1")
      //log.info("expectedCostQ1 $expectedCostQ1" )
      assert planCostQ1 == expectedCostQ1
      log.info("Assertion passed !! $planCostQ1 | $expectedCostQ1")

      • ChrisAdams's avatar
        ChrisAdams
        Champion Level 3

        Hi,

         

        The below example and function show how you can test for an empty string.  Just create a brand new Groovy script step, paste in the contents and click Go in the Groovy window.  Ensure you can see the logging for the answers....

         

        def value = "1.12";
        log.info("value ${value} is empty?  ${ isEmpty(value)}");
        
        value = "1";
        log.info("value ${value} is empty?  ${ isEmpty(value)}");
        
        value = "";
        log.info("value ${value} is empty?  ${ isEmpty(value)}");
        
        value = null;
        log.info("value ${value} is empty?  ${ isEmpty(value)}");
        
        def isEmpty(stringvalue) {
        
            if(stringvalue == null) {
            	    return true;
            } else {
        		if((stringvalue.length() == 0) ||
        			(stringvalue == "") ) {
        			return true;
        		} else {
        			return false;
        		}
        	}
        }

         

        Also, Groovy Double does not have a tryParse function unlike other languages, but it does throw an exception which might help...  Next example...

        def myValue = "1.12";
        log.info("is ${myValue} parsable as double?  ${canBeDouble(myValue)}");
        
        myValue = "1";
        log.info("is ${myValue} parsable as double?  ${canBeDouble(myValue)}");
        
        myValue = "abc";
        log.info("is ${myValue} parsable as double?  ${canBeDouble(myValue)}");
        
        def canBeDouble(theValue) {
        	try {
        		  def a = Double.parseDouble(theValue);
        		  return true;
        	} catch (NumberFormatException e) {
        		  //the parseDouble failed and you need to handle it here
        		  return false;
        	}
        }

         

        You can then maybe use these examples, or modify them, to test suspect values from the db before trying the assert.