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

     

     

15 Replies

  • Apoorva6's avatar
    Apoorva6
    Frequent Contributor

    I have removed this post and added updated one in my next message.

    • nmrao's avatar
      nmrao
      Champion Level 3
      First of all, the question is confusing.

      Why do you need values from a static file. Instead shouldn't you be using the values from the request and and send the response based on those? More details would probably help.
      • Apoorva6's avatar
        Apoorva6
        Frequent Contributor

        Hi, I have rephrased my requirement.

        My request below will write BillingAccountNumber to another application. Since we are not storing BillingAccountNumber anywhere, I need to store in another remote  XML file which I need to use later. The common fields in request and the remote XML file is BillAccountNodeId,BillAccountNodeType and BillingAccountNumber . That is these 3 attributes will be present in request and remote XML. Now my requirement is to compare BillAccountNodeId of Request
        with Remote XML And if matches, then write BillingAccountNumber from Request to Remote XML file. After writing to remote XML, I can use this for another transation. I have Request , mock Response and sample Remote XML below.
        Note: This request will come multiple times with different BillAccountNodeId and BillingAccountNumber and still it should write to Remote XML after comparing BillAccountNodeId

        Request:

        <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
           <soapenv:Header/>
           <soapenv:Body>
              <p109:setBillAccountMigrRequest xmlns:p882="http://cio.ttt.com/commonheader/v3" xmlns:p109="http://edb.ttt.com/dppco/billaccountinsertupdatemigr/v2">
                 <p882:WSHeader/>
                 <p109:tttContractNumber>NBI0147477</p109:tttContractNumber>
                 <p109:BillAccount>
                    <p109:BillAccountNodeId>119372</p109:BillAccountNodeId>
                    <p109:BillAccountNodeType>C</p109:BillAccountNodeType>
                    <p109:BillingAccountNumber>2562</p109:BillingAccountNumber>
                 </p109:BillAccount>
                 <p109:BillAccount>
                    <p109:BillAccountNodeId>119373</p109:BillAccountNodeId>
                    <p109:BillAccountNodeType>H</p109:BillAccountNodeType>
                    <p109:BillingAccountNumber>3928</p109:BillingAccountNumber>
                    <p109:Statustype>HierarchyCreation</p109:Statustype>
                    <p109:Status>Success</p109:Status>
                    <p109:StatusErrorCode/>
                    <p109:StatusErrorDescription/>
                 </p109:BillAccount>
                 <p109:BillAccount>
                    <p109:BillAccountNodeId>119374</p109:BillAccountNodeId>
                    <p109:BillAccountNodeType>BA</p109:BillAccountNodeType>
                    <p109:BillingAccountNumber>8310000221925</p109:BillingAccountNumber>
                 </p109:BillAccount>
                 <p109:BillAccount>
                    <p109:BillAccountNodeId>119375</p109:BillAccountNodeId>
                    <p109:BillAccountNodeType>I</p109:BillAccountNodeType>
                    <p109:BillingAccountNumber>8310000221927</p109:BillingAccountNumber>
                 </p109:BillAccount>
                 <p109:BillAccount>
                    <p109:BillAccountNodeAlias>000001</p109:BillAccountNodeAlias>
                    <p109:BillAccountNodeType>G</p109:BillAccountNodeType>
                    <p109:BillingAccountNumber>000001</p109:BillingAccountNumber>
                 </p109:BillAccount>
                 <p109:BillAccount>
                    <p109:BillAccountNodeId>119376</p109:BillAccountNodeId>
                    <p109:BillAccountNodeType>SA</p109:BillAccountNodeType>
                    <p109:BillingAccountNumber>8310000221930</p109:BillingAccountNumber>
                    <p109:GamsId>BEPLAGEN</p109:GamsId>
                    <p109:BillGroup>VAP</p109:BillGroup>
                 </p109:BillAccount>
                 <p109:BillAccount>
                    <p109:BillAccountNodeId>119377</p109:BillAccountNodeId>
                    <p109:BillAccountNodeType>SA</p109:BillAccountNodeType>
                    <p109:BillingAccountNumber>8310000221932</p109:BillingAccountNumber>
                    <p109:GamsId>BES00B</p109:GamsId>
                    <p109:BillGroup>VAQ</p109:BillGroup>
                 </p109:BillAccount>
              </p109:setBillAccountMigrRequest>
           </soapenv:Body>
        </soapenv:Envelope>

        Remote XML as below

         

        <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
           <env:Header/>
           <env:Body>
              <p35:getBillAccountPackageMigrResponse xmlns:p414662882="http://cio.ttt.com/commonheader/v3" xmlns:p35="http://edb.ttt.com/dppco/billaccountquerymigr/v2">
                 <p414662882:WSResponseHeader/>
                 <p35:Customer xmlns:p65="http://edb.ttt.com/dppco/bcicommon/v2">
                    <p65:CustName>Customer 312        Batch 9</p65:CustName>
                    <p65:Addr1>8433 QUIVIRA RD</p65:Addr1>
                    <p65:City>Lenexa</p65:City>
                    <p65:State>KS</p65:State>
                    <p65:PostalCode>66215</p65:PostalCode>
                    <p65:IsoCountryCode>US</p65:IsoCountryCode>
                    <p65:SubmittedByUserID>mp2012</p65:SubmittedByUserID>
                    <p65:MANumber>20110902-0178UA</p65:MANumber>
                    <p65:Contact>
                       <p65:ContactType>Sales</p65:ContactType>
                       <p65:tttuid>mp2012</p65:tttuid>
                    </p65:Contact>
                 </p35:Customer>
                 <p35:BillAccount xmlns:p65="http://edb.ttt.com/dppco/bcicommon/v2">
                    <p65:BillAccountNodeId>119372</p65:BillAccountNodeId>
                    <p65:BillAccountNodeType>C</p65:BillAccountNodeType>
                    <p65:BillingAccountNumber>2562</p65:BillingAccountNumber>
                    <p65:ContractedBusinessCustomerType>O</p65:ContractedBusinessCustomerType>
                 </p35:BillAccount>
                 <p35:BillAccount xmlns:p65="http://edb.ttt.com/dppco/bcicommon/v2">
                    <p65:ActivityType>Add</p65:ActivityType>
                    <p65:BillAccountNodeId>119373</p65:BillAccountNodeId>
                    <p65:BillAccountNodeType>H</p65:BillAccountNodeType>
                    <p65:BillingAccountNumber>11812</p65:BillingAccountNumber>
                    <p65:ParentBillAccountNodeID>143460</p65:ParentBillAccountNodeID>
                    <p65:CountryCode>US</p65:CountryCode>
                    <p65:CustomerLegalName>CLINICAL REFERENCE LABORATORY, INC.</p65:CustomerLegalName>
                    <p65:tttLegalEntityCode>US</p65:tttLegalEntityCode>
                    <p65:BillCycle>1</p65:BillCycle>
                 </p35:BillAccount>
                 <p35:BillAccount xmlns:p65="http://edb.ttt.com/dppco/bcicommon/v2">
                    <p65:BillAccountNodeId>119374</p65:BillAccountNodeId>
                    <p65:BillAccountNodeType>BA</p65:BillAccountNodeType>
                    <p65:BillingAccountNumber>8310000221925</p65:BillingAccountNumber>
                    <p65:ParentBillAccountNodeID>143461</p65:ParentBillAccountNodeID>
                 </p35:BillAccount>
                 <p35:BillAccount xmlns:p65="http://edb.ttt.com/dppco/bcicommon/v2">
                    <p65:BillAccountNodeId>119375</p65:BillAccountNodeId>
                    <p65:BillAccountNodeType>I</p65:BillAccountNodeType>
                    <p65:BillingAccountNumber>8310000037320</p65:BillingAccountNumber>
                    <p65:ParentBillAccountNodeID>143462</p65:ParentBillAccountNodeID>
                    <p65:SolutionsBilledCustomerInd>N</p65:SolutionsBilledCustomerInd>
                    <p65:BillingName>CLINICAL REFERENCE LABORATORY, INC.</p65:BillingName>
                    <p65:InvoiceCurrency>USD</p65:InvoiceCurrency>
                    <p65:Fan>VP0B30</p65:Fan>
                    <p65:AdvanceArrearsIndicator>1</p65:AdvanceArrearsIndicator>
                    <p65:BillingAddress>
                       <p65:TypeOfAddress>Billing</p65:TypeOfAddress>
                       <p65:Addr1>8433 QUIVIRA RD</p65:Addr1>
                       <p65:City>LENEXA</p65:City>
                       <p65:State>KS</p65:State>
                       <p65:PostalCode>66215</p65:PostalCode>
                       <p65:County>JOHNSON</p65:County>
                       <p65:IsoCountryCode>US</p65:IsoCountryCode>
                       <p65:GeoCode>0117091090000</p65:GeoCode>
                    </p65:BillingAddress>
                    <p65:BillingAddress>
                       <p65:TypeOfAddress>BillingTax</p65:TypeOfAddress>
                       <p65:Addr1>8433 QUIVIRA RD</p65:Addr1>
                       <p65:City>LENEXA</p65:City>
                       <p65:State>KS</p65:State>
                       <p65:PostalCode>66215</p65:PostalCode>
                       <p65:County>JOHNSON</p65:County>
                       <p65:IsoCountryCode>US</p65:IsoCountryCode>
                       <p65:GeoCode>0117091090000</p65:GeoCode>
                    </p65:BillingAddress>
                 </p35:BillAccount>
                 <p35:BillAccount xmlns:p65="http://edb.ttt.com/dppco/bcicommon/v2">
                    <p65:BillAccountNodeId>144525</p65:BillAccountNodeId>
                    <p65:BillAccountNodeType>G</p65:BillAccountNodeType>
                    <p65:BillingAccountNumber>000001</p65:BillingAccountNumber>
                    <p65:ParentBillAccountNodeID>143463</p65:ParentBillAccountNodeID>
                 </p35:BillAccount>
                 <p35:BillAccount xmlns:p65="http://edb.ttt.com/dppco/bcicommon/v2">
                    <p65:BillAccountNodeId>119376</p65:BillAccountNodeId>
                    <p65:BillAccountNodeType>SA</p65:BillAccountNodeType>
                    <p65:BillingAccountNumber>8310000044619</p65:BillingAccountNumber>
                    <p65:ParentBillAccountNodeID>144521</p65:ParentBillAccountNodeID>
                    <p65:Mcn>075603</p65:Mcn>
                    <p65:Soc>H6</p65:Soc>
                    <p65:BillGroup>VAE</p65:BillGroup>
                    <p65:TypeOfTriplet>S</p65:TypeOfTriplet>
                    <p65:GamsId>VP0B30</p65:GamsId>
                 </p35:BillAccount>
               <p35:BillAccount xmlns:p65="http://edb.ttt.com/dppco/bcicommon/v2">
                    <p65:ActivityType>Add</p65:ActivityType>
                    <p65:BillAccountNodeId>143461</p65:BillAccountNodeId>
                    <p65:BillAccountNodeType>H</p65:BillAccountNodeType>
                    <p65:BillingAccountNumber>11812</p65:BillingAccountNumber>
                    <p65:ParentBillAccountNodeID>143460</p65:ParentBillAccountNodeID>
                    <p65:CountryCode>US</p65:CountryCode>
                    <p65:CustomerLegalName>CLINICAL REFERENCE LABORATORY, INC.</p65:CustomerLegalName>
                    <p65:tttLegalEntityCode>US</p65:tttLegalEntityCode>
                    <p65:BillCycle>1</p65:BillCycle>
                 </p35:BillAccount>
                 <p35:BillAccount xmlns:p65="http://edb.ttt.com/dppco/bcicommon/v2">
                    <p65:BillAccountNodeId>143462</p65:BillAccountNodeId>
                    <p65:BillAccountNodeType>BA</p65:BillAccountNodeType>
                    <p65:BillingAccountNumber>BA_NUM</p65:BillingAccountNumber>
                    <p65:ParentBillAccountNodeID>143461</p65:ParentBillAccountNodeID>
                 </p35:BillAccount>
                 <p35:BillAccount xmlns:p65="http://edb.ttt.com/dppco/bcicommon/v2">
                    <p65:BillAccountNodeId>143463</p65:BillAccountNodeId>
                    <p65:BillAccountNodeType>I</p65:BillAccountNodeType>
                    <p65:BillingAccountNumber>8310000037320</p65:BillingAccountNumber>
                    <p65:ParentBillAccountNodeID>143462</p65:ParentBillAccountNodeID>
                    <p65:SolutionsBilledCustomerInd>N</p65:SolutionsBilledCustomerInd>
                    <p65:BillingName>CLINICAL REFERENCE LABORATORY, INC.</p65:BillingName>
                    <p65:InvoiceCurrency>USD</p65:InvoiceCurrency>
                    <p65:Fan>VP0B30</p65:Fan>
                    <p65:AdvanceArrearsIndicator>1</p65:AdvanceArrearsIndicator>
                    <p65:BillingAddress>
                       <p65:TypeOfAddress>Billing</p65:TypeOfAddress>
                       <p65:Addr1>8433 QUIVIRA RD</p65:Addr1>
                       <p65:City>LENEXA</p65:City>
                       <p65:State>KS</p65:State>
                       <p65:PostalCode>66215</p65:PostalCode>
                       <p65:County>JOHNSON</p65:County>
                       <p65:IsoCountryCode>US</p65:IsoCountryCode>
                       <p65:GeoCode>0117091090000</p65:GeoCode>
                    </p65:BillingAddress>
                    <p65:BillingAddress>
                       <p65:TypeOfAddress>BillingTax</p65:TypeOfAddress>
                       <p65:Addr1>8433 QUIVIRA RD</p65:Addr1>
                       <p65:City>LENEXA</p65:City>
                       <p65:State>KS</p65:State>
                       <p65:PostalCode>66215</p65:PostalCode>
                       <p65:County>JOHNSON</p65:County>
                       <p65:IsoCountryCode>US</p65:IsoCountryCode>
                       <p65:GeoCode>0117091090000</p65:GeoCode>
                    </p65:BillingAddress>
                 </p35:BillAccount>
                 <p35:BillAccount xmlns:p65="http://edb.ttt.com/dppco/bcicommon/v2">
                    <p65:BillAccountNodeId>144525</p65:BillAccountNodeId>
                    <p65:BillAccountNodeType>G</p65:BillAccountNodeType>
                    <p65:BillingAccountNumber>000001</p65:BillingAccountNumber>
                    <p65:ParentBillAccountNodeID>143463</p65:ParentBillAccountNodeID>
                 </p35:BillAccount>
                 <p35:BillAccount xmlns:p65="http://edb.ttt.com/dppco/bcicommon/v2">
                    <p65:BillAccountNodeId>143464</p65:BillAccountNodeId>
                    <p65:BillAccountNodeType>SA</p65:BillAccountNodeType>
                    <p65:BillingAccountNumber>8310000044619</p65:BillingAccountNumber>
                    <p65:ParentBillAccountNodeID>144521</p65:ParentBillAccountNodeID>
                    <p65:Mcn>075603</p65:Mcn>
                    <p65:Soc>H6</p65:Soc>
                    <p65:BillGroup>VAE</p65:BillGroup>
                    <p65:TypeOfTriplet>S</p65:TypeOfTriplet>
                    <p65:GamsId>VP0B30</p65:GamsId>
                 </p35:BillAccount>
              </p35:getBillAccountPackageMigrResponse>
           </env:Body>
        </env:Envelope>

        My Mock Response as below

         

        <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
           <env:Header/>
           <env:Body>
              <p109:setBillAccountMigrResponse xmlns:p882="http://cio.ttt.com/commonheader/v3" xmlns:p109="http://edb.ttt.com/dppco/billaccountinsertupdatemigr/v2">
                 <p882:WSResponseHeader/>
                 <p109:BillAccount>
                    <p109:BillAccountNodeId>119372</p109:BillAccountNodeId>
                    <p109:BillAccountNodeType>C</p109:BillAccountNodeType>
                 </p109:BillAccount>
                 <p109:BillAccount>
                    <p109:BillAccountNodeId>119373</p109:BillAccountNodeId>
                    <p109:BillAccountNodeType>H</p109:BillAccountNodeType>
                 </p109:BillAccount>
                 <p109:BillAccount>
                    <p109:BillAccountNodeId>119374</p109:BillAccountNodeId>
                    <p109:BillAccountNodeType>BA</p109:BillAccountNodeType>
                 </p109:BillAccount>
                 <p109:BillAccount>
                    <p109:BillAccountNodeId>119375</p109:BillAccountNodeId>
                    <p109:BillAccountNodeType>I</p109:BillAccountNodeType>
                 </p109:BillAccount>
                 <p109:BillAccount>
                    <p109:BillAccountNodeId>173806</p109:BillAccountNodeId>
                    <p109:BillAccountNodeType>G</p109:BillAccountNodeType>
                 </p109:BillAccount>
                 <p109:BillAccount>
                    <p109:BillAccountNodeId>119376</p109:BillAccountNodeId>
                    <p109:BillAccountNodeType>SA</p109:BillAccountNodeType>
                 </p109:BillAccount>
              <p109:BillAccount>
                    <p109:BillAccountNodeId>119377</p109:BillAccountNodeId>
                    <p109:BillAccountNodeType>SA</p109:BillAccountNodeType>
                 </p109:BillAccount>
              </p109:setBillAccountMigrResponse>
           </env:Body>
        </env:Envelope>
  • Apoorva6's avatar
    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?
    • nmrao's avatar
      nmrao
      Champion Level 3
      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
    • nmrao's avatar
      nmrao
      Champion Level 3
      Just did another update in the script, please take it freshly before trying.
      • Apoorva6's avatar
        Apoorva6
        Frequent Contributor

        Hi Rao,

         

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