Forum Discussion

Apoorva6's avatar
Apoorva6
Frequent Contributor
8 years ago
Solved

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.
  • nmrao's avatar
    nmrao
    8 years ago

    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
    	}
    }