Skip to main content

Flat file creator in Groovy for SoapUI

Quick code share for creating flat files in SoapUI via Groovy.

I used this to create a linearly increasing file size for an extract, transform and load tool to do some basic load testing.

SoapUI is great for this kind of thing, nice and simple IDE, with the ability to extend to use pretty much any jar you can think of. Enjoy!
 
/*This script will create a new flat file with number of records taken from a test case level property:
* Steps:
* 1.Create the basics for use throughout the file. Primarily dates and account numbers 
* 2.Create the relevant filename
* 3.Create the header
* 4.Create the body of records with amended account numbers
* 5.Add a footer
* 6.Output the lot to a separate file, its size configurable by test case property. 
*/

import java.util.*;
import org.apache.commons.lang.RandomStringUtils;

//Create the static bits to be used
def number_Records = context.expand( '${#testcase#Number_Records}' ).toInteger();
def outputDir = context.expand( '${#testcase#Output Dir}' ).toString();

//What we need to be build the filename
def date = new Date();
String month = date.format('yyyyMM');
String today = date.format('yyyyMMdd');
String headerDate = date.format('ddMMyyyy');
String randomString = (RandomStringUtils.random(7, true, false));

//Create the filename
String filename = month+randomString+today+".src"

//Declare a new file object
File file = new File(outputDir+filename);

//Create the header
String fileHeader = headerDate
file.write(fileHeader);
file.append '\n'

//Build the body of the file
for (int i = 0; i < number_Records; i++){
String newAccountNumber = (RandomStringUtils.randomNumeric(15));
String lineToReproduce = newAccountNumber;
file.append(lineToReproduce);
file.append '\n';
}

//Create the footer
String fileFooter = ""
switch (number_Records.toString().size()){
case '3':
fileFooter += "9999999999999999999900000"+number_Records+" ";
break;
case '4':
fileFooter += "999999999999999999990000"+number_Records+" ";
break;
case '5':
fileFooter += "99999999999999999999000"+number_Records+" ";

break;
case '6':
fileFooter += "9999999999999999999900"+number_Records+" ";
break;
case '7':
fileFooter += "999999999999999999990"+number_Records+" ";
break;
case '8':
fileFooter += "99999999999999999999"+number_Records+" ";
break;
}

file.append(fileFooter);

log.info("File "+outputDir+filename+" created with "+number_Records+" record(s)")

Comments

Popular posts from this blog

Testers Guide to Myths of Unit Testing

One area that testers might be able to enhance their contributions to software development teams is how we perceive and contribute to unit testing. I believe testers busting their own illusions about this aspect of building something good would bring us much closer to developers, and help us realise what other layers of testing can cover most effectively.

Also, I want to do a talk about it, so I figured I would test the premise, see if potential audiences were into it. I put this on Twitter:
Working on a talk about what testers might believe about unit #testing & how we interact with developers creating unit tests. Any challenges/additions for my list below? #development#agilepic.twitter.com/4oT5HE4qs3 — Ash Winter (@northern_tester) December 19, 201730 replies with ideas tends to indicate that people might be into it. 
The ListI thought, as my final blog of 2017, I would provide a super useful list of the myths and legends we as testers might believe about unit testing:
That developer…

Wheel of Testing Part 2 - Content

Thank you Reddit, while attempting to find pictures of the earths core, you surpass yourself.
Turns out Steve Buscemi is the centre of the world.

Anyway. Lets start with something I hold to be true. My testing career is mine to shape, it has many influences but only one driver. No one will do it for me. Organisations that offer a career (or even a vocation) are offering something that is not theirs to give. Too much of their own needs get in the way, plus morphing into a badass question-asker, assumption-challenger, claim-demolisher and illusion-breaker is a bit terrifying for most organisations. Therefore, I hope the wheel is a tool for possibilities not definitive answers, otherwise it would just be another tool trying to provide a path which is yours to define.


In part one, I discussed why I had thought about the wheel of testing in terms of my own motivations for creating it, plus applying the reasoning of a career in testing to it. As in, coming up with a sensible reflection of real…

Wheel of Testing Part 3 - Applications

I've only had to quit two jobs to finally find the time to finish this blog series. Winning at life. If you need reminders (like I did) check out Part 1 and Part 2 before reading on...

After the first two blogs regarding the Wheel of Testing, I was delighted to receive a few requests for the wheel itself, which got me thinking about applications of it, beyond what its original intent was, which I've explored in detail in part 1 of this series of intermittent blogs. Most models need a little air time to show their value, in software development we crank out models all the time, but I'm not sure how many get used. I am inspired by models such as the "Heuristic Test Strategy Model" by James Marcus Bach, as I have used it and seen the benefits it has brought for my clients, particularly the ability to ask questions. So, I wanted to create a model which has a number of use cases, both real and imagined:

Helping to unlocking a career in testing which may be stuck

It is no…