Thursday, 6 December 2012

File asserts and manipulations with SoapUI and Groovy

Very quick one, re adventures with SoapUI and Groovy.

Have recently been building tests which need to run batch jobs and then assert against their output within the file structure. Below is a simplified implementation of what I've been up to:

//Build the file name to look for


def date = new Date();
fileName = "OutputFile" + date.format('ddMMyyyyHHmmmss') + ".csv";
folderName = testRunner.testCase.testSuite.project.getPropertyValue("sftpPath");
filePath = folderName + fileName; 
log.info(filePath);

//Read in the data
def inputFile = new File (folderName + fileName);

//Create a new list for asserting against
assertList = [];

//Populate with Contents of Text File
addContentstoList = {assertList.add(it)};
inputFile.eachLine(addContentstoList);
log.info(assertList);

//Assert the number of records in the array, plus for the existence of a specific object
assert assertList.size() == 5;
assertCustID = assertList.any {3};
assert assertCustID;

Found this simple and effective. As an added bonus, this creates and deletes directories and builds CSV files from lists:


//Create New Directories
def inputDir = new File("C:/temp/in").mkdir()
def outputDir = new File("C:/temp/out").mkdir()

//Create an Array for Inputs
def inputFile = [
    [id:'1',description:'One'],
    [id:'2',description:'Two'],
    [id:'3',description:'Three'],
]

//Create a csv file
def BuildFile = new File("C:/temp/in/team.csv")
inputFile.each {
    def row = [it.id, it.description]
    BuildFile.append row.join(',')
    BuildFile.append '\n'
}

//Copy to output folder
def sourceFile = new File ("C:/temp/in/team.csv")
def destinationFile = new File ("C:/temp/out/team1.csv")

sourceFile.withInputStream { is -> 
  destinationFile << is 
}

//Finally delete to tidy up
def delInput = new File("C:/temp/in").deleteDir()
def delOutput = new File("C:/temp/out").deleteDir()




No comments:

Post a Comment