tools.json.JsonAnalysis Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-autotest-tool Show documentation
Show all versions of java-autotest-tool Show documentation
This is an integration of autotest tools
package tools.json;
/**
* Created by zhengyu
*/
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.testng.Reporter;
import tools.poi.ExcelDataSource;
import tools.poi.ParamAnalysis;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
public class JsonAnalysis {
private String paramFilePath = "";
private String sheetName = "";
private ExcelDataSource edc = null;
private static int excelCurrRow = 0;
public JsonAnalysis(){
this.edc = new ExcelDataSource(2016);
this.edc.createSheet("Result");
this.edc.createRowCellValue(excelCurrRow,0,"接口自动化测试结果集");
this.edc.mergeCell(excelCurrRow,++excelCurrRow,0,5);
this.edc.setGreyStyleWithoutBorder();
}
public String getSpecificVal(String jsonStr, String queryStr){
Reporter.log("您输入的查询Json的语句为:"+queryStr,true);
String key = "";
String value = "";
int indexStart = 0, indexEnd = 0, count = 0;
String[] strGroup = queryStr.split("\\.");//.需要转义
int length = strGroup.length;
Reporter.log("length="+strGroup.length,true);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonGenerator.Feature.QUOTE_NON_NUMERIC_NUMBERS, false);
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
try {
JsonNode node = mapper.readTree(jsonStr);
for(int i=1; i actualMap, int rowNum){
boolean flag = true;
String expKey = "", expVal = "", actualVal;//此三个变量是为了判断结果对错。
ParamAnalysis pa = new ParamAnalysis(this.paramFilePath, this.sheetName);
String validationString = pa.getValidationString(rowNum-1);//此处要操纵Excel,所以上方要传入文件路径和sheet名,因此在此方法前先set这两个私有成员变量。
String caseName = pa.getCaseName(rowNum-1);
int currCol = 0;
excelCurrRow++;//每个用例结果之间空一行
this.edc.createRowCellValue(excelCurrRow,currCol,"用例名称:"+caseName);
this.edc.createRowCellValue(excelCurrRow+1, currCol, "期待结果");
this.edc.createRowCellValue(excelCurrRow+2, currCol, "实际结果");
//下方进行验证字符串的拆分和比对结果是否正确。
if(validationString!=null && !"".equals(validationString)){
if(validationString.contains(",")){
Reporter.log("Excel中的Validation字段有多对键值对。",true);
String[] expGroup = validationString.split(",");
for(String tempStr :expGroup){
currCol++;//一次循环就是一个变量,所以列数+1
if (tempStr.contains("=")) {
expKey = tempStr.split("=")[0].trim();
expVal = tempStr.split("=")[1].trim();
this.edc.createCellValue(excelCurrRow, currCol, expKey);//每一个变量
this.edc.createCellValue(excelCurrRow+1, currCol, tempStr);
if (actualMap.containsKey(expKey)){
actualVal = actualMap.get(expKey);
if (actualVal.contentEquals(expVal)){
Reporter.log(expKey+"的实际值与期待值相等,均为: "+actualVal,true);
this.edc.createCellValue(excelCurrRow+2, currCol, expKey+"="+actualVal);
}else{
Reporter.log(expKey+"的实际值与期待值 不相等, 期待值="+expVal+", 实际值="+actualVal,true);
this.edc.createCellValue(excelCurrRow+2, currCol, expKey+"="+actualVal);
this.edc.setFaultStyle();
flag = false;
}
}else{
Reporter.log("您收集的结果集中,不包括 "+expKey+" 这个变量",true);
this.edc.createCellValue(excelCurrRow+2, currCol, expKey+" 的实际值不存在");
flag = false;
}
}else if(tempStr.contains(">")) {
expKey = tempStr.split(">")[0].trim();
expVal = tempStr.split(">")[1].trim();
this.edc.createCellValue(excelCurrRow, currCol, expKey);//每一个变量
this.edc.createCellValue(excelCurrRow+1, currCol, tempStr);
if (actualMap.containsKey(expKey)){
actualVal = actualMap.get(expKey);
if (Double.parseDouble(actualVal)>Double.parseDouble(expVal)){
Reporter.log(expKey+"的实际值 "+actualVal+" > 期待值 "+expVal,true);
this.edc.createCellValue(excelCurrRow+2, currCol, expKey+"="+actualVal);
}else{
Reporter.log(expKey+"的实际值="+actualVal+" 并不大于 期待值="+expVal,true);
this.edc.createCellValue(excelCurrRow+2, currCol, expKey+"="+actualVal);
this.edc.setFaultStyle();
flag = false;
}
}else{
Reporter.log("您收集的结果集中,不包括 "+expKey+" 这个变量",true);
this.edc.createCellValue(excelCurrRow+2, currCol, expKey+" 的实际值不存在");
flag = false;
}
}else if(tempStr.contains("<")){
expKey = tempStr.split("<")[0].trim();
expVal = tempStr.split("<")[1].trim();
this.edc.createCellValue(excelCurrRow, currCol, expKey);//每一个变量
this.edc.createCellValue(excelCurrRow+1, currCol, tempStr);
if (actualMap.containsKey(expKey)){
actualVal = actualMap.get(expKey);
if (Double.parseDouble(actualVal)")) {
expKey = validationString.split(">")[0].trim();
expVal = validationString.split(">")[1].trim();
this.edc.createCellValue(excelCurrRow, currCol, expKey);//每一个变量
this.edc.createCellValue(excelCurrRow+1, currCol, validationString);
if (actualMap.containsKey(expKey)){
actualVal = actualMap.get(expKey);
if (Double.parseDouble(actualVal)>Double.parseDouble(expVal)){
Reporter.log(expKey+"的实际值 "+actualVal+" > 期待值 "+expVal,true);
this.edc.createCellValue(excelCurrRow+2, currCol, expKey+"="+actualVal);
}else{
Reporter.log(expKey+"的实际值="+actualVal+" 并不大于 期待值="+expVal,true);
this.edc.createCellValue(excelCurrRow+2, currCol, expKey+"="+actualVal);
this.edc.setFaultStyle();
flag = false;
}
}else{
Reporter.log("您收集的结果集中,不包括 "+expKey+" 这个变量",true);
this.edc.createCellValue(excelCurrRow+2, currCol, expKey+" 的实际值不存在");
flag = false;
}
}else if(validationString.contains("<")){
expKey = validationString.split("<")[0].trim();
expVal = validationString.split("<")[1].trim();
this.edc.createCellValue(excelCurrRow, currCol, expKey);//每一个变量
this.edc.createCellValue(excelCurrRow+1, currCol, validationString);
if (actualMap.containsKey(expKey)){
actualVal = actualMap.get(expKey);
if (Double.parseDouble(actualVal) actualList, List expectedList){
return this.listCompare(actualList,"<", expectedList);
}
public boolean lessEqualToExpected(List actualList, List expectedList){
return this.listCompare(actualList,"<=", expectedList);
}
public boolean equalToExpected(List actualList, List expectedList){
return this.listCompare(actualList, "==", expectedList);
}
public boolean greaterThanExpected(List actualList, List expectedList){
return this.listCompare(actualList, ">", expectedList);
}
public boolean greaterEqualToExpected(List actualList, List expectedList){
return this.listCompare(actualList, ">=", expectedList);
}
private boolean listCompare(List actualList, String manner, List expectedList){
boolean flag = true;
double actualVal = 0;
double expectedVal = 0;
int size = actualList.size();
if (size == expectedList.size()){
if ("==".contentEquals(manner)){
for (int i = 0; i < size; i++) {
if (actualList.get(i).contentEquals(expectedList.get(i))) {
//按String类型先比较
Reporter.log("[正确]:实际值=" + actualList.get(i) + " 等于 期待值=" + expectedList.get(i),true);
} else {
actualVal = Double.parseDouble(actualList.get(i));
expectedVal = Double.parseDouble(expectedList.get(i));
if (BigDecimal.valueOf(actualVal).equals(BigDecimal.valueOf(expectedVal))){
//按double类型比较
Reporter.log("[正确]:实际值=" + actualVal + " 等于 期待值=" + expectedVal,true);
} else {
Reporter.log("[错误]:实际值=" + actualList.get(i) + " 不等于 期待值=" + expectedList.get(i), true);
flag = false;
}
}
}
}else if (">".contentEquals(manner)) {
for (int i = 0; i < size; i++) {
actualVal = Double.parseDouble(actualList.get(i));
expectedVal = Double.parseDouble(expectedList.get(i));
if (actualVal < expectedVal || BigDecimal.valueOf(actualVal).equals(BigDecimal.valueOf(expectedVal))) {
Reporter.log("[错误]:实际值=" + actualVal + " 小于等于 期待值=" + expectedVal, true);
flag = false;
} else
Reporter.log("[正确]:实际值=" + actualVal + " 大于 期待值=" + expectedVal, true);
}
}else if (">=".contentEquals(manner)){
for (int i = 0; i < size; i++) {
actualVal = Double.parseDouble(actualList.get(i));
expectedVal = Double.parseDouble(expectedList.get(i));
if (actualVal < expectedVal) {
Reporter.log("[错误]:实际值=" + actualVal + " 小于 期待值=" + expectedVal, true);
flag = false;
} else
Reporter.log("[正确]:实际值=" + actualVal + " 大于等于 期待值=" + expectedVal, true);
}
}else if ("<".contentEquals(manner)){
for (int i=0; i expectedVal || BigDecimal.valueOf(actualVal).equals(BigDecimal.valueOf(expectedVal))){
Reporter.log("[错误]:实际值="+actualVal+" 大于等于 期待值="+expectedVal,true);
flag = false;
}else
Reporter.log("[正确]:实际值="+actualVal+" 小于 期待值="+expectedVal,true);
}
}else if ("<=".contentEquals(manner)){
for (int i=0; i expectedVal){
Reporter.log("[错误]:实际值="+actualVal+" 大于 期待值="+expectedVal,true);
flag = false;
}else
Reporter.log("[正确]:实际值="+actualVal+" 小于等于 期待值="+expectedVal,true);
}
}else {
Reporter.log("您输入的比较符号(manner变量)不合法。",true);
}
}else
Reporter.log("您给入的实际值Map和预期值Map,所含的键值对个数:不相等。");
return flag;
}
}