Forum Discussion
You may or may not feel this over complicates things but I would use the Apache Commons CSV java library, this will deal with the parsing of your data.
Just download the jar file from the link above and pop it in your bin/ext directory and then the code below is an example of how to parse your data.
import org.apache.commons.csv.CSVFormat
import org.apache.commons.csv.CSVParser
def pipeDelimText = '''CustRefID|title|firstname|lastname|addressline1|addressline2|addressline3|postcode
ID0000001|MR|RICH|JONES|5 WHATEVER WAY|HODGE HILL|BIRMINGHAM|B36 9LB
ID0000002|MRS|ALISON|WHITEFORD|6 READY ROAD|WHITTAKER|FOUR OAKS|B74 4TH'''
def pipeDelimTextStringReader = new StringReader(pipeDelimText)
CSVFormat pipeDelimFormat = CSVFormat.newFormat('|'.toCharacter()).withRecordSeparator('\r\n').withHeader()
pipeDataParser = new CSVParser(pipeDelimTextStringReader, pipeDelimFormat);
pipeDataParser.getRecords().each {record ->
log.info('Record ' + record.getRecordNumber().toString() + ': Cust Ref ' + record.get('CustRefID') + ' is ' + record.get('title') + ' ' + record.get('lastname'))
}
If you need to loop through your pipe delimited data then this could be put in a Groovy Data Source and paired with a Data source loop.
- Radford9 years agoSuper Contributor
This issue has interested me and because I was looking at the Groovy DataSource for another issue I thought I'd turn this into a full example, it may possibly help you or perhaps someone else. I've attached a full stand-alone sample project, where you will just need to go to the DataSource step and update the path to your file (assuming you have already added the Apache commons CSV library jar file to you bin/ext directory).
The things I like about this solution is:
- Let the CSV library deal with parsing out your data (why reinvent the wheel?)
- It minimises the Groovy, which is all in one place.
- You can now access your pipe delimitted data in the standard Ready API way as it is now just properties of the DataSource test step within a DataSource Loop.
Below is a screen shot from the attached project of the Groovy DataSource and you can see that your data has been parsed at the bottom:
- nmrao9 years agoCommunity Hero
Well, here are my comments :
1. The code presented by me is much simpler, readable and condensed than other samples presented above.
2. No custom library is used.
3. No reinvention here, just groovy, I believe (as only separator changed and it is applicable even if other libraries are used) at least for this case requested by original poster.
If at all custom library needs to be used, below is very simplified and groovified:
https://github.com/xlson/groovycsv - groovyguy9 years agoCommunity Hero
I really like what you came up with, Radford. That's an elegant solution! It does inherit some potential cost, though. Importing the CSV stuff into the bin/ext folder means that if anyone else is going to need to use the project / tests being created, they will need to take the same steps.
I only bring this up as this has bitten me once or twice before.
- richie9 years agoCommunity Hero
WOW!
you guys (msiadak, nmrao and Radford) are the best!
really appreciate all your help - I'm desperately trawling through SoapUI/groovyscript courses so hopefully I will stop asking for so much help soon!
Again - thanks so much - you guys are really saving my life here,
richie