Forum Discussion

skelkar's avatar
skelkar
Contributor
11 years ago

How to assert data type of a particular node value and desired no of decimal points ?

Hi,

 

I have nodes which are of different data types such as String, Int, double I need to assert that a node belongs to a specific data type.

For e..g if node is 'age' then it shall be integer always.

 

Also, I want to assert if node value is float/doiuble the max allowed decimal points is two. FOR THIS I used . nodevalue.scale=2 by importing java math. it worked but is there any toher way.

 

 

  • nmrao's avatar
    nmrao
    Icon for Champion Level 2 rankChampion Level 2
    How are you reading node value? Are you using XmlSlurper() or XmlHoder and getNodeValue()?
    • skelkar's avatar
      skelkar
      Contributor

      I am using both.

       

      For some request where I have json transalted to xml I use xm,l holder with getnodevalue.

      For other requests where soapui fails to translate json into xml I use jsnslurper.

      • nmrao's avatar
        nmrao
        Icon for Champion Level 2 rankChampion Level 2

        Here is the simple script to Test the data type. And result shows that all the element values are being treated as String only. So in your case you have to parse to the required data type as you know it from the schema. 

         

        def xml='''
        <xml>
           <node>
              <val1>1</val1>
              <val2>12.00</val2>
              <val3>true</val3>
              <val4>testvalue</val4>
           </node>
         </xml>'''
         def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
         def reader = groovyUtils.getXmlHolder(xml )
         use (groovy.xml.dom.DOMCategory) {
            for( node in reader.getDomNodes( "//node" )) {
                node.children().each { child ->
                    if (child.text() instanceof Integer) {
                    	log.info  child.text() +" is integer"            	
                    } else if (child.text() instanceof Double) {
                    	log.info  child.text()+" is double"            	
                    } else if (child.text() instanceof Boolean) {
                    	log.info  child.text()+" is boolean"            	
                    } else if (child.text() instanceof String) {
                    	log.info  child.text()+" is string"            	
                    }
                }
            }
        }

         Output:

        Fri Feb 13 07:46:03 IST 2015:INFO:1 is string
        Fri Feb 13 07:46:03 IST 2015:INFO:12.00 is string
        Fri Feb 13 07:46:03 IST 2015:INFO:true is string
        Fri Feb 13 07:46:03 IST 2015:INFO:testvalue is string