码迷,mamicode.com
首页 > Windows程序 > 详细

[SoapUI] 比较TP和Live环境中API的XML response,打印多条不同的地方,比较数据时允许偏差值,同时错误信息中数据点ID通过映射表替换成UI上数据点的名字

时间:2015-07-05 00:53:05      阅读:300      评论:0      收藏:0      [点我收藏+]

标签:

import org.custommonkey.xmlunit.*
import javax.xml.xpath.*
import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.parsers.*
import static  java.lang.Math.*  

String xmlTP = "D:\\Study\\MA\\xml_TP.xml"
String xmlLive = "D:\\Study\\MA\\xml_Live.xml"
String xmlMapping = "D:\\Study\\MA\\DataIdMapping\\DataIdMappingPA.xml"

def allowableDeviation = 0.01
def maxRecordFail = 50
String UIDataName = ""
String holding = ""
String XPathOfDiffDataName = ""

def responseTP=  new FileReader( new File(xmlTP))
def responseLive= new FileReader(new File(xmlLive))

def xmlAllDiff = new DetailedDiff( new Diff(responseLive,responseTP))
def diffList = xmlAllDiff.getAllDifferences() 

int diffListSize=diffList.size()
log.info "TP vs Live , different number : "+diffListSize
if(maxRecordFail>diffListSize||maxRecordFail<=0){
	maxRecordFail=diffListSize
}

for (i = 0; i < maxRecordFail; i++) {
	String diff=diffList.get(i)
//	log.info " Difference  : "+diff

	if(diff.contains("holding")){
		log.info " Fail : Holding is different , TP = "+diff.split("holding‘>")[1].split("<")[0]+"  , "+ "Live = "+diff.split("holding‘>")[2].split("<")[0]
	}

	if(diff.contains("PA001")){
		log.info " Fail : Portfolio Id is different , TP = "+diff.split("‘")[1]+"  , "+ "Live = "+diff.split("‘")[3]
	}
	
	String diffDataID = diff.split("@")[2].trim()
	String TPDataValue = diff.split("‘")[1]
	String LiveDataValue =diff.split("‘")[3]

	if(((TPDataValue=="")&&(LiveDataValue!=""))||((TPDataValue!="")&&(LiveDataValue==""))){
		UIDataName = getDataNameInMapping(diffDataID,xmlMapping)
		
		XPathOfDiffDataName = getXPathOfDataName(diff)
		holding = getHoldingByXPath(XPathOfDiffDataName,xmlLive)
		
		log.info  " Fail : Data Value is different , TP = "+TPDataValue+" , Live = "+LiveDataValue+" , Holding = "+holding+" , Data Point = "+UIDataName+" , Data ID = "+diffDataID
	}

	if(TPDataValue.isFloat()&&LiveDataValue.isFloat()){
		Float TPDataValueFloat = TPDataValue.toFloat()
		Float LiveDataValueFloat = LiveDataValue.toFloat()
	
		if(Math.abs(TPDataValueFloat-LiveDataValueFloat )>allowableDeviation){
	//		log.info "diffDataID =" + diffDataID
			UIDataName = getDataNameInMapping(diffDataID,xmlMapping)
			
			XPathOfDiffDataName = getXPathOfDataName(diff)
			holding = getHoldingByXPath(XPathOfDiffDataName,xmlLive)
			
			String failMessage = " Fail : Data Value is different , TP = "+TPDataValue+" , Live = "+LiveDataValue+" , Holding = "+holding+" , Data Point = "+UIDataName+" , Data ID = "+diffDataID
			log.info failMessage
		}
	}
}

def getDataNameInMapping(String diffDataID,String xmlMapping){
 	def xmlDataIdMapping= new XmlParser().parse(xmlMapping)
	for(it in xmlDataIdMapping.f){
		String mapDataID =  "${it.attribute("i")}"
//	     log.info "mapDataID ="+mapDataID
	      if(mapDataID == diffDataID){
	      	 UIDataName =  "${it.attribute("udlbl")}"
	      	 return UIDataName
	      }
	} 
}

def getHoldingByXPath(String diffDataNameXPath,String xmlLive){
 	DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	dbf.setValidating(false);
	DocumentBuilder db = dbf.newDocumentBuilder();
	doc = db.parse(new FileInputStream(new File(xmlLive)));
	XPathFactory factory = XPathFactory.newInstance();
	xpath = factory.newXPath();
	String OS385 =  xpath.evaluate(diffDataNameXPath, doc, XPathConstants.NODE).getValue();
	String holding = OS385.split(">")[2].split("<")[0]
	return holding
}

def getXPathOfDataName(String diff){
	String diffDataValueXPath = diff.split("at ")[2]
	//log.info "The XPath of the data value is : "+diffDataValueXPath
	String diffDataNameXPath = diffDataValueXPath.split("@")[0]+"@OS385"
	//log.info "The XPath of the data name is : "+diffDataNameXPath
	return diffDataNameXPath
}

最后的错误结果显示为:
技术分享

[SoapUI] 比较TP和Live环境中API的XML response,打印多条不同的地方,比较数据时允许偏差值,同时错误信息中数据点ID通过映射表替换成UI上数据点的名字

标签:

原文地址:http://www.cnblogs.com/MasterMonkInTemple/p/4621477.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!