Forum Discussion

richie's avatar
richie
Community Hero
7 years ago

Groovy Assistance - Change readlines from network to readlines from a property?

Hi,

 

I've been helped a lot by Rao, msiadak and others so far which got me to the point with the following groovyscript

 

def lines = new File('D:\\Donor Marketing\\SoapUI\\nongroovyscriptfile\\New Text Document.dat').readLines() 
lines.eachWithIndex { line, index ->     
if (index) {     	
	def data = line.tokenize('|'); 	
	log.info data[0];       
	context.testCase.testSteps["Properties"].setPropertyValue("CustRefID", data[0]);
	}	    	 
}

 

Now the above reads a file on my local drive, parses the file and logs the  CustRefID attribute value and passes the CustRefID value to a Properties test step.

 

Which is almost exactly what I need.  HOWEVER - because I'm already sourcing the testdata using a Datasource (Directory) type test step at the start, I need to change this script so that rather than the following line

 

def lines = new File('D:\\Donor Marketing\\SoapUI\\nongroovyscriptfile\\New Text Document.dat').readLines() 

I need to change the script so it parses the 'fileContents' property which is defined in my Datasource(directory) type.

 

I thought (Ive been doing some reading) that if I changed the line above to the following:

 

def lines = thisTestCase.getTestStepByName("Datasource - directory").getPropertyValue("fileContents").toString()

 where the Datasource step = 'Datasource - directory' and the property defined in the datasource is 'fileContents' this would work - i.e. so the whole script reads as follows:

 

def lines = thisTestCase.getTestStepByName("Datasource - directory").getPropertyValue("fileContents").toString()
lines.eachWithIndex { line, index ->     
if (index) {     	
	def data = line.tokenize('|'); 	
	log.info data[0];       
	context.testCase.testSteps["Properties"].setPropertyValue("CustRefID", data[0]);
	}	    	 
}

 

however - I'm getting 'no such property' response when I run the script.

 

Can anyone advise please?

 

Again - many thanks to all for any assistance you can provide!

 

richie

3 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3
    You have been providing lot these details, and observed there are many question around this. However, I some where feel that there is crucial information is missing for the external people to understand the problem clearly.

    Can you write about use case in a broader way?

    Becuase, you have a datasource (not sure on what basis you have this and looping) and reading a file and which is again data driven. ie., Nested data driven on the whole. So, in the essence, could not understand what are you trying to achieve at all.
    • richie's avatar
      richie
      Community Hero

      ok - I'm sorry for causing any confusion.

       

      I have a POST REST request that is submitted to the ESB which will either create/update a record in the remote database - the content of this POST request is via the ${Datasource#fileContents} property.

      I have a subsequent GET REST request that is submitted to the ESB to query the CRM's database layer to return all the attributes that were created/updated in the POST request - the query on the GET request uses the CustRefID value that is the unique id of the records held in testdata files.

       

      The reason why I'm using the Datasource (Directory) type is that I have 300 tests (300 test data files.  Each file has a header row (with column headers) a single line of data in each file).  The property on the Datasource object (fileContents) is used to provide the input to the REST POST request.  The datasource loop object (at the end of the test case) is there so the test case loops through each testdata file for each time the test executes.

       

      My current hierarchy is as follows:

       

      Datasource(Directory) -- fileContents property defined
      POST request (content of message is ${Datasource#fileContents}
      Groovy step (reads a file from the directory, parses the content for the CustRefID attribute value and sets the value in the following Properties step (${Properties#CustRefID}
      Properties step (holds the CustRefID attribute value)
      GET request (submits the CustRefID as the value of the GET query string)
      Datasource loop (loops through each file in the directory)

       

      Rao, msiadak and others have helped me tremendously so far.  The above script is not perfect.  There is one thing I need to change.  The ONLY problem I have now is that the line in the groovyscript that currently reads the file from the drive.  That is, the read file from directory is hardcoded in the groovyscript.  I need to change the script so that rather than reading the file from the directory (which is already being done by the Datasource(Directory) step, I need to change it so that it reads the ${Datasource#fileContents} property instead.

       

      Currently the groovyscript reads as follows:

       

      def lines = new File('D:\\Donor Marketing\\SoapUI\\nongroovyscriptfile\\New Text Document.dat').readLines() 
      lines.eachWithIndex { line, index ->     
      if (index) {     	
      	def data = line.tokenize('|'); 	
      	log.info data[0];       
      	context.testCase.testSteps["Properties"].setPropertyValue("CustRefID", data[0]);
      	}	    	 
      }

      As you can see - the 'def lines = new file(......).readLines()' is what I need to change (I think).  I want to change this script so rather than hardcoding the read from the file, the readLines() method is done via the ${Datasource#fileContents} property, so that it can pass the CustRefID attribute value to the subsequent GET REST Request.

       

      I understand that it's probably easier if I just extract the CustRefID value from the POST response to pass the CustRefID value onto the subsequent GET Request, however, I'vejust spoken to the Technical Architect at first he said extracting the value from the POST response was unacceptable - but he's now confirmed the POST response doesn't even include the CustRefID - how ridiculous! so I've got to get it from the testdata- there's no other way - otherwise I dump trying to parameterize and create 300 individual test cases!

       

      I hope I've been clear, if I haven't been clear, please come back so I can answer any questions.

       

      If I get this sorted - thats this round of testing sorted! :)

       

       

       

      I've attached the project file.  The relevant test case is entitled 'POST - Create/Update Donors'.  I've also attached 3 testdata files.

       

      As always - very, very grateful for your consideration.

       

      richie