Need help in groovy script to fetch proper response.
I need help in below issue.
I have 2 attributes in request orderComponentId and orderDataType.
orderDataType can take 2 values (TTTN and PORT) and orderComponentId can take any value.
I have 2 path where response is stored in XML. This response name will be same as orderComponentId value
path1= groovyUtils.projectPath+"/MOCK/CSI/IETN/
path2= groovyUtils.projectPath+"/MOCK/CSI/IETN/PORTEDTN/
Now based on orderDataType value i.e TTTN or PORT, I need to fetch response from path1 or path2 and if not present throw a fault message as No data found
If orderDataType is TTTN, then fetch response from path1 and if orderDataType is PORT, fetch response from path2
I am trying with below script and always i get fault message
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def holder = groovyUtils.getXmlHolder(mockRequest.requestContent)
def soNumber = holder.getNodeValue("//*:InquireEnterpriseTNOrderDataRequest/*:orderComponentId")
def orderType = holder.getNodeValue("//*:InquireEnterpriseTNOrderDataRequest/*:orderDataType")
def file = new File (groovyUtils.projectPath+"/MOCK/CSI/IETN/${soNumber}.xml")
def file1 = new File (groovyUtils.projectPath+"/MOCK/CSI/IETN/PORTEDTN/${soNumber}.xml")
def fileToLoad = 'soapFault'
if (orderType == "TTTN" and file.exists ) {
fileToLoad = soNumber
context.content = groovy.xml.XmlUtil.serialize(new XmlParser().parse(groovyUtils.projectPath+"/MOCK/CSI/IETN/${fileToLoad}.xml"))
}
else if (orderType == "PORT" and file1.exists ) {
fileToLoad = soNumber
context.content = groovy.xml.XmlUtil.serialize(new XmlParser().parse(groovyUtils.projectPath+"/MOCK/CSI/IETN/PORTEDTN/${fileToLoad}.xml"))
}
Can you try below script ? Note that I could not test before providing as I do not have artifacts.
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context) def holder = groovyUtils.getXmlHolder(mockRequest.requestContent) def soNumber = holder.getNodeValue("//*:InquireEnterpriseTNOrderDataRequest/*:orderComponentId") def orderType = holder.getNodeValue("//*:InquireEnterpriseTNOrderDataRequest/*:orderDataType") def prefix = "${groovyUtils.projectPath}/MOCK/CSI/IETN/" def getPrefix = { orderType == 'PORT' ? ("${prefix}PORTEDTN/" as String) : prefix } def getFileName = { def fileName = "${getPrefix()}${soNumber}.xml" as String def file = new File(fileName) file.exists() ? fileName : ("${getPrefix()}soapFault.xml" as String) } def getResponseContent = { new File(getFileName()).text } context.content = getResponseContent() log.info "Response content is set to : ${context.content}"