Groovy: Как заменить тег Body для существующего файла xml в soapui - PullRequest
0 голосов
/ 26 февраля 2020

Я пытаюсь заменить тело XML из ложного запроса на существующую схему XML. Не могли бы вы помочь мне в достижении этого

Тела XML (которое взято из txt файла), которое необходимо заменить в файл Base XML:

<tns:Body xmlns:tns="http://schemas.xmlsoap.org/soap/envelope/">
      <polsearch1:searchForPoliciesResponse xmlns:polsearch1="http://service.axa.de/komposit/policyretrieve/searchforpoliciesmodel/v1">
         <polsearch1:Entry Address="Colonia-Allee" AddressLine1="Colonia-Allee" City="Köln" EffectiveDate="2020-02-06T00:00:00+01:00" ExpirationDate="2021-02-06T00:00:00+01:00" InsuredName="Herr Dr. Axafiname Ovuir Suf" IsArchived="false" PolicyNumber="56000068476" PolicyType="VHV19Household" PostalCode="51067" ProducerCode="8834002000" Status="inforce" HouseNumber_De="10" DateOfBirth_De="1990-08-26T00:00:00+02:00"/>
      </polsearch1:searchForPoliciesResponse>
   </tns:Body>

My base XML file:

<tns:Envelope xmlns:tns="http://schemas.xmlsoap.org/soap/envelope/">
   <tns:Header>
      some info;
   </tns:Header>
   <tns:Body>
      <polsearch1:searchForPoliciesResponse xmlns:polsearch1="http://service.axa.de/komposit/policyretrieve/searchforpoliciesmodel/v1">
         <polsearch1:Entry Address="Colonia-Allee" AddressLine1="Colonia-Allee" City="Köln" EffectiveDate="2019-02-27T00:01:00+01:00" ExpirationDate="2020-02-27T00:01:00+01:00" InsuredName="Mr. Testklasklsk Testqerqwerwqe" IsArchived="false" PolicyNumber="${#MockResponse#Request#declare namespace v1='http://service.axa.de/komposit/policyretrieve/searchforpoliciesmodel/v1'; //v1:searchForPolicies[1]/v1:criteria[1]/@PolicyNumber}" PolicyType="HR19LiabilityRetail" PostalCode="51067" ProducerCode="8834002000" Status="inforce" HouseNumber_De="88" DateOfBirth_De="1987-12-12T00:00:00+01:00"/>
      </polsearch1:searchForPoliciesResponse>
   </tns:Body>
</tns:Envelope>

А мой Groovy скрипт выглядит так:

jsonObjectForPartner = jsonSlurper.parseText(line) //line contains Body of xml from txt file 


String tempContent = new File(groovyUtils.projectPath+"/sp_baseFile.xml").getText('UTF-8') // reading Base file xml
    def Basefileholder = groovyUtils.getXmlHolder(tempContent) //taking into XMLholder class

//replacing the body content
Basefileholder.setNodeValue("//*:Body",jsonObjectForPolicy.line)

с помощью setNodevalue Я мог бы разместить строку, но существующее содержимое не удаляется. Может ли кто-нибудь помочь в этом?

1 Ответ

0 голосов
/ 28 февраля 2020

Ответ на этот вопрос:

SearchPolicies.txt - содержит фактический вывод (тело xml в формате строки)

Sp_base. xml - содержит пример схемы

Задача: заменить xml содержимое тела из Txt-файла в базу xml файл

def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
//Get Mockrequest
def requestholder = groovyUtils.getXmlHolder(mockRequest.requestContent)
//log.info(requestholder)
//get project root folder path
def projRootLocation = groovyUtils.projectPath
//log.info(projRootLocation)
//parent folder path
def requestSampleFileFolder = projRootLocation  
//Get the info of the Policy to be searched from request
def policynumber = requestholder.getNodeValue("//*:Body/*:searchForPolicies/*:criteria/@PolicyNumber")
//log.info(policynumber)

 //Check for the POLICY whethere existed in already created data
def allRowsDataForCreatedPolicies = new File(requestSampleFileFolder + "/SearchPolicies.txt").readLines()
def jsonObjectForPolicy 
def policyFound = false
import groovy.json.JsonSlurper
def jsonSlurper = new JsonSlurper()
allRowsDataForCreatedPolicies.find { String line ->  
  if(line.contains(policynumber))
  {
  log.info(line)      
  policyFound = true  
  jsonObjectForPolicy = jsonSlurper.parseText(line)
  true
  }
}
//Get base file
String tempContent = new File(groovyUtils.projectPath+"/sp_baseFile.xml").getText('UTF-8')
def Basefileholder = groovyUtils.getXmlHolder(tempContent)
def finalResponse
if(policyFound)
{
    //Removing the searchForPoliciesResponse node to replace with actual output 
      Basefileholder.removeDomNodes("//*:Body/*:searchForPoliciesResponse") 
      //Replacing the required body content to the base schema file
    Basefileholder.setNodeValue("//*:Body",jsonObjectForPolicy.XMLBodyResponse)
    //Removing unwanted charecters from xml
    finalResponse = Basefileholder.xml.replace("&lt;", "<")
    //Dsipatching the final response
    mockResponse.responseContent = finalResponse
}
else {mockResponse.responseContent = "Policy Number not found"}
...