Need help in writing content of request (selected attributes) to external XML file.
SOLVED- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-12-2017
10:46 AM
03-12-2017
10:46 AM
Need help in writing content of request (selected attributes) to external XML file.
I have removed this post and added modified one in my next message.
Solved! Go to Solution.
15 REPLIES 15
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2017
09:33 AM
03-27-2017
09:33 AM
Below is the Script for Mock Script Dispatcher.
Hope you aware that you need to set response to ${content}
NOTE: the script is not tried before providing, so please try and post if there are any errors.
Response content is dynamically generated and set the response to content variable.
//Provided the directory name where external file exists def extFileDirectory = '/path/to/ext/file/directory' //Provide the absolute file path of soapfault file def soapFaultPath = '/path/to/soapFault.xml' //NOTE: Donot edit beyond this point def parsedXml = { fileName -> if (new File(fileName).exists()) { return new XmlSlurper().parse(fileName) } else { log.error "${fileName} does not exists" context.content = new File(soapFaultPath).text return null } } def requestXml = new XmlSlurper().parseText(mockRequest.requestContent) //Get the file name of external file def extFileName = requestXml.'**'.find { it.name() == 'tttContractNumber'} as String def extFileAbsPath = "${extFileDirectory}/${extFileName}.xml" //Provide the file path of external file def externalXml = parsedXml(extFileAbsPath) if(externalXml) { //Closure to get the BillAccounts def billAccounts = { data -> data.'**'.findAll { it.name() == 'BillAccount' } } //Closure to get the map of matching keys def billDetailsMap = { data, key, value -> billAccounts(data).inject([:]){map, account -> map[account."$key".text()] = account."$value".text(); map} } //Get the bill details map from request def requestBillDetailsMap = billDetailsMap(requestXml, 'BillAccountNodeId', 'BillingAccountNumber') //check if the map has some details i.e., there are BillAccountNodeId's in the request if (0 < requestBillDetailsMap.size()) { def requestBillDetailsMapForResponse = billDetailsMap(requestXml, 'BillAccountNodeId', 'BillAccountNodeType') def exterBillAccounts = billAccounts(externalXml) def isMatchingNodeIdFound = false requestBillDetailsMap.each { key, value -> exterBillAccounts.each { account -> if (account?.BillAccountNodeId.text() == key) { isMatchingNodeIdFound = true account.BillingAccountNumber = value } } } if(isMatchingNodeIdFound) { log.info 'There are matching BillAccountNodeId between the request and external file, updating external file' log.info "Content of the external file is going to be : ${groovy.xml.XmlUtil.serialize(externalXml)}" new File(extFileAbsPath).write(groovy.xml.XmlUtil.serialize(externalXml)) log.info 'Building the response' def builder = new groovy.xml.StreamingMarkupBuilder() builder.encoding = 'UTF-8' def xml = builder.bind { mkp.xmlDeclaration() mkp.declareNamespace(env:'http://schemas.xmlsoap.org/soap/envelope/', ch : 'http://cio.ttt.com/commonheader/v3', ns : 'http://edb.ttt.com/dppco/billaccountinsertupdatemigr/v2') 'env:Envelope' { 'env:Header' { } 'env:Body' { 'ns:setBillAccountMigrResponse' { 'ch:WSResponseHeader' { } requestBillDetailsMapForResponse.each { key, value -> 'ns:BillAccount' { 'ns:BillAccountNodeId' (key) 'ns:BillAccountNodeType' (value) } } } } } } log.info "Response that is going to send: ${groovy.xml.XmlUtil.serialize(xml)}" context.content = groovy.xml.XmlUtil.serialize(xml) } else { log.info 'There are no matching BillAccountNodeId between the request and external file' context.content = new File(soapFaultPath).text } } else { log.error 'No BillAccountNodeId found in the request' context.content = new File(soapFaultPath).text } }
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2017
10:10 AM
03-27-2017
10:10 AM
Hi Rao
Thank you very much. Will try this. However you included external.xml directly. Per my previous comment, there can be multiple external.xml in the name of tttContractNumber value from request. Dont we need to get xpath of that and check for its value .xml?
Thank you very much. Will try this. However you included external.xml directly. Per my previous comment, there can be multiple external.xml in the name of tttContractNumber value from request. Dont we need to get xpath of that and check for its value .xml?
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2017
10:20 AM
03-27-2017
10:20 AM
No, it is dealt automatically based on the request xml value.
Note that answer is updated to handle that.
All you need to provide / edit in the top few 2
1. path of all xml files
2. absolute path of soapfault xml
Regards,
Rao.
Note that answer is updated to handle that.
All you need to provide / edit in the top few 2
1. path of all xml files
2. absolute path of soapfault xml
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2017
10:52 AM
03-27-2017
10:52 AM
Just did another update in the script, please take it freshly before trying.
Regards,
Rao.
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2017
11:33 AM
03-27-2017
11:33 AM
Hi Rao,
Its working absolutely great and as expected. Thank you very much.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2017
02:53 PM
03-27-2017
02:53 PM
@Apoorva6,
I am glad to know that it is helpful.
There is a learning here:
a. Great patience
b. Being persistent in following up for the answer
c. Improvement in the presentation of information to viewers, so that lengthy question making interesting. Still can be improved to do in the first attempt.
d. sometimes waiting also make worth, of course, not always happens.
Regards,
Rao.
I am glad to know that it is helpful.
There is a learning here:
a. Great patience
b. Being persistent in following up for the answer
c. Improvement in the presentation of information to viewers, so that lengthy question making interesting. Still can be improved to do in the first attempt.
d. sometimes waiting also make worth, of course, not always happens.
Regards,
Rao.

- « Previous
-
- 1
- 2
- Next »
- « Previous
-
- 1
- 2
- Next »