import org.custommonkey.xmlunit.* import org.custommonkey.xmlunit.examples.* import javax.xml.xpath.* import javax.xml.parsers.* import static java.lang.Math.* import com.eviware.soapui.support.GroovyUtils // The parameter allowableDeviation means the allowable deviation can be ? percent, e.g. allowableDeviation = 0.03 , the allowable deviation is 3% def allowableDeviation = 0.03 def maxRecordFail = 20 ArrayList failMessageList = new ArrayList() String UIDataName, ticker, XPathOfDiffDataName, failMessage def currentStepIndex = context.currentStepIndex String currentStepName = testRunner.testCase.getTestStepAt(currentStepIndex).name String previousStepName = testRunner.testCase.getTestStepAt(currentStepIndex-1).name String prePreStepName = testRunner.testCase.getTestStepAt(currentStepIndex-2).name String dataIdMappingFile = testRunner.testCase.testSuite.project.getPropertyValue( "dataIdMappingFile" ) String testResultPath = testRunner.testCase.testSuite.project.getPropertyValue( "testResultPath" ) def xmlLive=context.expand(‘${‘+prePreStepName+‘#Response}‘ ) def xmlTP=context.expand( ‘${‘+previousStepName+‘#Response}‘ ) def groovyUtils = new GroovyUtils( context ) def xmlHolderLive = groovyUtils.getXmlHolder(xmlLive) Diff diffTotal= new Diff(xmlLive, xmlTP) DetailedDiff xmlDetailedDiff = new DetailedDiff(diffTotal) def diffList = xmlDetailedDiff.getAllDifferences() int diffListSize=diffList.size() log.info "TP vs Live , different number in all: "+diffListSize if(maxRecordFail>diffListSize||maxRecordFail<=0){ maxRecordFail=diffListSize } if(diffListSize>0){ for (i = 0; i < diffListSize; i++) { String diff=diffList.get(i) // log.info " Difference : "+diff if(diff.contains("holding")){ failMessage = " Holding is different , TP = "+diff.split("holding‘>")[1].split("<")[0]+" , "+ "Live = "+diff.split("holding‘>")[2].split("<")[0] failMessageList.add(failMessage) } if(diff.contains("PA001")){ failMessage = " Portfolio Id is different , TP = "+diff.split("‘")[1]+" , "+ "Live = "+diff.split("‘")[3] failMessageList.add(failMessage) } String diffDataID = diff.split("@")[2].trim() String TPDataValue = diff.split("‘")[1] String LiveDataValue =diff.split("‘")[3] if(((TPDataValue=="")&&(LiveDataValue!=""))||((TPDataValue!="")&&(LiveDataValue==""))){ addFailMessageAboutDataValueNull(failMessageList, TPDataValue,LiveDataValue, diff, diffDataID, xmlHolderLive, dataIdMappingFile) } if(TPDataValue.isFloat()&&LiveDataValue.isFloat()){ addFailMessageAboutDataValueDiff(failMessageList, TPDataValue,LiveDataValue, diff, diffDataID, xmlHolderLive, dataIdMappingFile, allowableDeviation) } if(failMessageList.size()==maxRecordFail){ break } if((i == (diffListSize-1))&&(failMessageList.size()<maxRecordFail)){ maxRecordFail = failMessageList.size() } } } if(maxRecordFail>0){ def testResultFile = new File(testResultPath+ currentStepName+".txt") if (testResultFile.exists()) { testResultFile.delete() } for(j=0; j<maxRecordFail; j++){ String currentFailMessage = failMessageList.get(j) log.error currentFailMessage testResultFile.append(currentFailMessage+"\n" ) } assert false,failMessageList.get(0) } def getDataNameInMapping(String diffDataID,String dataIdMappingFile){ def xmlDataIdMapping= new XmlParser().parse(dataIdMappingFile) for(it in xmlDataIdMapping.f){ String mapDataID = "${it.attribute("i")}" // log.info "mapDataID ="+mapDataID if(mapDataID == diffDataID){ UIDataName = "${it.attribute("udlbl")}" return UIDataName } } } def getTickerByXPath(String XPathOfDiffDataName,Object xmlHolderLive){ String ticker = xmlHolderLive.getNodeValue(XPathOfDiffDataName) return ticker } def getXPathOfDataName(String diff){ String diffDataValueXPath = diff.split("at ")[2] String diffDataNameXPath = diffDataValueXPath.split("@")[0]+"@OS385" return diffDataNameXPath } def addFailMessageAboutDataValueDiff(ArrayList failMessageList, String TPDataValue, String LiveDataValue, String diff, String diffDataID, Object xmlHolderLive, String dataIdMappingFile, Float allowableDeviation){ Float TPDataValueFloat = TPDataValue.toFloat() Float LiveDataValueFloat = LiveDataValue.toFloat() Float benchmark = LiveDataValueFloat if (LiveDataValueFloat ==0){ benchmark = TPDataValueFloat } if(Math.abs((LiveDataValueFloat-TPDataValueFloat )/benchmark)>allowableDeviation){ // log.info "diffDataID =" + diffDataID UIDataName = getDataNameInMapping(diffDataID,dataIdMappingFile) XPathOfDiffDataName = getXPathOfDataName(diff) ticker =getTickerByXPath(XPathOfDiffDataName,xmlHolderLive) failMessage = " Data Value is different , TP = "+TPDataValue+" , Live = "+LiveDataValue+" , Ticker = "+ticker+" , Data Point = "+UIDataName+" , Data ID = "+diffDataID failMessageList.add(failMessage) } } def addFailMessageAboutDataValueNull(ArrayList failMessageList, String TPDataValue, String LiveDataValue, String diff, String diffDataID, Object xmlHolderLive, String dataIdMappingFile){ UIDataName = getDataNameInMapping(diffDataID,dataIdMappingFile) XPathOfDiffDataName = getXPathOfDataName(diff) ticker =getTickerByXPath(XPathOfDiffDataName,xmlHolderLive) failMessage = " Data Value is different , TP = "+TPDataValue+" , Live = "+LiveDataValue+" , Ticker = "+ticker+" , Data Point = "+UIDataName+" , Data ID = "+diffDataID failMessageList.add(failMessage) }
时间: 2024-11-02 18:04:16