Forum Discussion
Thank you for your response. As this could be an option, I've decided to do this differently. I wanted to be able to use the objects from ReadyAPI together with groovyscript. How I did it:
A first groovyscript: to lookup the files in a local 'resource' folder. A lot of those files have different extensions.
def basePath = context.expand('${#Global#filePath}')
def sourceDir = new File(basePath + "/resources")
def files = sourceDir.listFiles()
.findAll { it.isFile() }
def filePaths = files.collect { it.absolutePath }
testRunner.testCase.setPropertyValue(
"fileList",
filePaths.join("|")
)
testRunner.testCase.setPropertyValue( "fileIndex", "0")
log.info "FOUND ${filePaths.size()} FILES"2nd step is use of Data Source. I gave a name to the property 'currentFile' and use this groovy:
def fileList =
testRunner.testCase.getPropertyValue(
"fileList"
)
def index =
testRunner.testCase.getPropertyValue(
"fileIndex"
) as Integer
def files =
fileList.split("\\|")
if(index >= files.size()) {
log.info "NO MORE FILES"
return false
}
result["currentFile"] =
files[index]
log.info(
"CURRENT FILE = " +
files[index]
)The 3rd step is the REST request where I've attached the original file, saved on project level , as multipart/form-data
Followed by another groovyscript that will check which type of file we need to tackle, change the random vinnumber and saved it in a local folder to be fetched again in a next step
import groovy.xml.*
import java.util.Random
// PATHS
// =====================================================
def basePath = context.expand('${#Global#filePath}')
def currentFile = context.expand('${Data Source#currentFile}')
log.info("CURRENT FILE = " + currentFile)
def inputFile = new File(currentFile)
if (!inputFile.exists()) {
throw new Exception(
"FILE NOT FOUND : " + currentFile)
}
def originalFileName = inputFile.name
log.info("FILE NAME = " + originalFileName)
// NEW VIN
// =====================================================
def newVin = {
def chars = "ABCDEFGHJKLMNPRSTUVWXYZ0123456789"
(1..17).collect {chars[new Random().nextInt(chars.length())] }.join()
}
// DISPATCHER
// =====================================================
def generatedContent
if(originalFileName.toLowerCase().endsWith(".xml")) {
generatedContent = processXml(inputFile, newVin)
}
else if(originalFileName.toLowerCase().endsWith(".conv")) {
generatedContent = processConv(inputFile, newVin)
}
else {
generatedContent = processFlatFile(inputFile, newVin)
}
// XML
// =====================================================
def processXml(file,newVin) {
def xml = new XmlParser(false,false).parse(file)
boolean replaced = false
xml.depthFirst().each { node ->
if(replaced) return
def nodeName = node.name().toString().toLowerCase()
if(nodeName.contains("vin") ||
nodeName.contains("vehicleid")) {
log.info("FOUND XML NODE = ${nodeName}")
node.value = newVin
replaced = true
}
}
return XmlUtil.serialize(xml)
}
// FORD CONV
// =====================================================
def processConv(file,newVin) {
boolean replaced = false
def lines = file.readLines()
def updated = lines.collect { line ->
if(!replaced) {
def matcher = line =~ /(WF0[A-Z0-9]{14})/
if(matcher.find()) {
def oldVin = matcher.group(1)
//log.info("FOUND FORD VIN = ${oldVin}" )
line = line.replaceFirst( oldVin, newVin)
replaced = true
}
}
return line
}
updated.join("\n")
}
// DCUK
// =====================================================
def processFlatFile(file,newVin) {
boolean replaced = false
def lines = file.readLines()
def updated = lines.collect { line ->
if(!replaced) {
def matcher =line =~ /(W1K[A-Z0-9]{10,20})/
if(matcher.find()) {
def oldVin = matcher.group(1)
// log.info("FOUND DCUK VIN = ${oldVin}" )
line = line.replaceFirst(oldVin, newVin )
replaced = true
}
}
return line
}
updated.join("\n")
}
// SAVE FILE
// =====================================================
def generatedDir = new File(basePath + "/generated")
if(!generatedDir.exists()) {
generatedDir.mkdirs()
}
def generatedFileName = inputFile.name.replace( ".", "_" + System.currentTimeMillis() + "_generated.")
def fullPath =generatedDir.absolutePath + "/" +generatedFileName
new File(fullPath).write(
generatedContent,
"UTF-8"
)
//log.info "GENERATED FILE = ${fullPath}"
// generated file properties
testRunner.testCase.setPropertyValue( "generatedFile", fullPath)
testRunner.testCase.setPropertyValue("generatedFileName", inputFile.name)
//log.info("GENERATED FILE NAME = " +new File(fullPath).getName()
def currentIndex =
context.expand('${#TestCase#fileIndex}') as Integer
log.info "INDEX BEFORE = ${currentIndex}"
testRunner.testCase.setPropertyValue(
"fileIndex",
(currentIndex + 1).toString()
)
log.info "INDEX AFTER = ${currentIndex + 1}"
// SET PARTNERNAME
// =====================================================
def detectPartner(fileName) {
def knownPartners = [
"FORD",
"DCUK",
"JLR-RAIL",
"PDL",
"PRI",
"CarArrival",
"STELLANT"
]
for(p in knownPartners) {
if(fileName.toUpperCase().contains(p.toUpperCase())) {
return p
}
}
return "UNKNOWN"
}
def partner =detectPartner(originalFileName)
testRunner.testCase.setPropertyValue( "partner", partner)
//log.info("PARTNER = " + partner)5th step is the next REST request that fetches the newly create file.
and the last step is a cleanup from the previous step. You need to unattache the previous file so the newly created file can be added.
And it works as a charm!
Regards!