Forum Discussion

go2khurram's avatar
go2khurram
Contributor
9 years ago

Groovy If statement is not behaving as expected

I have written some code in which I am parsing the response xml of JDBC request and saving the rows in an ArrayList. "ar"

 

Later I am matching the return values with a watch list array called "requiredfield". I will be have some  more JDBC steps which I am calling using this script. The logic is if the matching value exist in ar array then I have to update in the table otherwise I have to insert a row.  In the below code I manage to record the values in my array "ar" but when I match these values with my watch list array "requiredField" , the if statement always return false and control goes to else part.  here is the code

Integer rr=0
ArrayList ar = new ArrayList()
jdbcXml = context.expand('${JDBCRequestForCheckingCommandPropertyID#ResponseAsXml}')
def root = new XmlSlurper().parseText(jdbcXml)
root.ResultSet.Row.each{row->
ar.add(row["COMMANDPROPERTYID"])
rr++

}

ArrayList requiredField = new ArrayList()
requiredField.add(61)
requiredField.add(276)
requiredField.add(605)

		
	for (j=0; j<requiredField.size();j++)
	{
		
		if (ar.contains(requiredField[j]))
		{	
			
			testRunner.gotoStepByName("Update"+requiredField[j])
		
		}
		else
		{
		
			testRunner.gotoStepByName("Add"+requiredField[j])
		}
	}

	

 However similar logic works fine if I created an array list and pass insert the values in "ar" array manually, here is my logic before reading the values from XML, In this if statement behaves as expected.

 

ArrayList ar = new ArrayList()
ar.add(605)
ar.add(61)
ar.add(276)

ArrayList requiredField = new ArrayList()
requiredField.add(61)
requiredField.add(276)
requiredField.add(605)

		
	for (j=0; j<requiredField.size();j++)
	{
		if (ar.contains(requiredField[j]))
		{
			
			testRunner.gotoStepByName("Update"+requiredField[j])
			
		}
		else
		{
		
			testRunner.gotoStepByName("Add"+requiredField[j])
		}
	}

 

Your help will highly appriciated.

 

regards,

 

  • go2khurram's avatar
    go2khurram
    9 years ago

    Hurray....

     

    I manage to fixed this by converting the array value into integer  Herer is the sample code 

     

    ArrayList ar = new ArrayList()
    jdbcXml = context.expand('${JDBCRequestForCheckingCommandPropertyID#ResponseAsXml}')
    def root = new XmlSlurper().parseText(jdbcXml)
    root.ResultSet.Row.each{row->
    ar.add(row["COMMANDPROPERTYID"].toInteger())
    }
    
    
  • nmrao's avatar
    nmrao
    Champion Level 3
    Would put the sample xml and what exactly you need out of it?
    • go2khurram's avatar
      go2khurram
      Contributor

      Here is the JDBC response XML I am parsing in my script 

       

      <Results>
          <ResultSet fetchSize="128">
              <Row rowNumber="1">
                  <COMMANDID>99762022</COMMANDID>
                  <COMMANDPROPERTYID>61</COMMANDPROPERTYID>
                  <COMMANDTYPEID>81</COMMANDTYPEID>
                  <PROPERTYVALUE>2013-11-22T00:00:00</PROPERTYVALUE>
                  <LASTUPDATED>2013-11-14 12:44:15.177</LASTUPDATED>
              </Row>
              <Row rowNumber="2">
                  <COMMANDID>99762022</COMMANDID>
                  <COMMANDPROPERTYID>276</COMMANDPROPERTYID>
                  <COMMANDTYPEID>81</COMMANDTYPEID>
                  <PROPERTYVALUE>2013-11-22T00:00:00</PROPERTYVALUE>
                  <LASTUPDATED>2013-11-14 12:44:15.16</LASTUPDATED>
              </Row>
             </ResultSet>
      </Results>

       

       

      In the scriprt mentioned in my initial post , I am reading the COMMANDPROPERTYID values and inserting into ar ArrayList. 

      Then using the  if (ar.contains(requiredField[j]) ) statemenmt If and validating that ar elements exist in another array list requiredField . if exist then call the update step else call insert step.

       

      While testing the logic I have trim my code to below where "ar.contains(requiredField[0]) " statement works or not and surprise to know that this starement returning false even both array have value e.g 61

       

      here is the short version of the code again

      ArrayList ar = new ArrayList()
      *jdbcXml = context.expand('${JDBCRequestForCheckingCommandPropertyID#ResponseAsXml}')
      def root = new XmlSlurper().parseText(jdbcXml)
      root.ResultSet.Row.each{row->
      ar.add(row["COMMANDPROPERTYID"])
      }
      
      
      ArrayList requiredField = new ArrayList()
      requiredField.add(61)
      requiredField.add(276)
      requiredField.add(605)
      
      
      log.info "First Element of ar array: "+ar[0]
      log.info "First Element of requiredFiled  array: "+requiredField[0]
      log.info "Is ar array contains element in requiredField[0]: "+ar.contains(requiredField[0]) 

      Result shows using the above XML as input: 

      Thu Feb 11 14:10:36 GMT 2016:INFO:First Element of ar array: 61
      Thu Feb 11 14:10:36 GMT 2016:INFO:First Element of requiredFiled array: 61
      Thu Feb 11 14:10:36 GMT 2016:INFO:Is ar array contains element in requiredField[0]: false

       

      However if I comment the code where I am  parsing XML and add element in ar Array List , result is as expected:

       

      Is thsi something to do with data type??

       

       

      • nmrao's avatar
        nmrao
        Champion Level 3
        How about this approach if I understand the question well?
        May be you can query directly for the same value using where commandpropertyid=x, then if you receive result set with 0, then insert.