Forum Discussion

Mis3's avatar
Mis3
Frequent Contributor
4 years ago

If-else statement

I have a Groovy script to read an external CSV file in my C drive and execute the teststep one-by-one.  Example here:

302331000006102,13313140097, odb220

302331000006103,13313140099, 123

I then added a conditional statement and when I used "if (data[2]="odb220")", SoapUI would throw this error:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script1.groovy: 19: expecting ')', found '=' @ line 19,

I was desperate so I tried "if (data[2]=="odb220")".  To my surprise and some luck, it worked.  

I goggled this operator "==" and found out that it has something to do with String literal and String object.   I do not understand this since I am not a Java developer.

 

The conditional statement is to compare 2 strings; data[2] with the constant string "odb220". The entire script is below. It is working well but I am concerned about the "==".    Is it correct that I use the operator "=="?   Is there a better way to code this?

 

Also, the script works when the format of the input file is the one above.  If the input file is:

302331000006102,13313140097, odb220

302331000006103,13313140099

It would throw an error like Arra-out-of-bound.  I understand why because there is no data[2] of the 2nd record.  How should I code this so it works with no data for data[2]?

Thanks.

-----------------------------------------------------------------------------------------------------------------------------------------------------------

import java.text.SimpleDateFormat
def date = new Date()
def file_date = new SimpleDateFormat("dd_MM_yyyy_HH_mm_ss")

def csvFilePath = "C:\\G-Groovy\\SDM-IMSI-MSISDN-220.csv"
def reportFile = "C:\\G-Groovy\\Reports\\SDM-Activate_"+file_date.format(date)+".log"
context.fileReader = new BufferedReader(new FileReader(csvFilePath))
File report = new File(reportFile)
def rowsData = context.fileReader.readLines()
report.text = "SDM-Activate Start: ${file_date.format(date)}\n"
report << '\n'

for(int i = 0; i < rowsData.size(); i++)
{
def data = rowsData[i].split(",")
testRunner.testCase.setPropertyValue( "TARGET-1", data[0] )
testRunner.testCase.setPropertyValue( "TARGET-2", data[1] )

if (data[2]=="odb220")
{
testRunner.testCase.setPropertyValue( "TARGET-3", "<odbProfileId>220</odbProfileId>" )
testRunner.testCase.setPropertyValue( "TARGET-4", "720" )
}
else
{
testRunner.testCase.setPropertyValue( "TARGET-3", "" )
testRunner.testCase.setPropertyValue( "TARGET-4", "721" )
}
tStep = testRunner.testCase.getTestStepByName("PT-SDM-Activate-A1")
testRunner.runTestStepByName( "PT-SDM-Activate-A1")

def xmlResponse = tStep.getPropertyValue("Response")
def xml = new XmlSlurper().parseText(xmlResponse)
def line = ["Seq=${i}", data[0], data[1],data[2], "Status=${xml.Body.changeSdmDataResponse.@resultCode}" ]

line << xml.'**'.findAll {it.name() == 'sdmDataResponse' }.collectMany{ ["FlowId=${it.@flowId}", "Status=${it.children()[0].@resultCode}" ]}
log.info line.flatten().join(' ')
report << line.flatten().join(' ')
report << '\n'

sleep(300)
}

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

  • "=" is assignment operator, "==" is to compare.
  • Bring the statement causing error under if condition that was suggested in previous reply.

    In the else block, copy the same statement without "data[2]"
  • nmrao's avatar
    nmrao
    Champion Level 3
    "=" is assignment operator, "==" is to compare.
    • nmrao's avatar
      nmrao
      Champion Level 3
      if (a.equals(b)) {
      //true block
      }
    • Mis3's avatar
      Mis3
      Frequent Contributor

      Thanks, Rao.

      The script works when the format of the input file is the one below: 

      302331000006102,13313140097, odb220hismfail

      302331000006103,13313140099, 123

       

      If there is no data for the data[2] like:

      302331000006102,13313140097, odb220

      302331000006103,13313140099

      It would throw an error …Array-out-of-bound.  I understand why because there is no data[2] of the 2nd record. 
      I tried 302331000006103!13313140099,,  and this fails as well.  
      How should I code this so it works with no data for data[2]?

      • nmrao's avatar
        nmrao
        Champion Level 3

        Change from
        if (data[2]=="odb220") {

        to
        if ( data.size() >=3 && data[2]=="odb220") {