Ask a Question

Need help in writing content of request (selected attributes) to external XML file.

SOLVED
Apoorva6
Frequent Contributor

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.

15 REPLIES 15

@Apoorva6,

 

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.
Apoorva6
Frequent Contributor

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?

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.

Just did another update in the script, please take it freshly before trying.


Regards,
Rao.
Apoorva6
Frequent Contributor

Hi Rao,

 

Its working absolutely great and as expected. Thank you very much.

@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.
cancel
Showing results for 
Search instead for 
Did you mean: