All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tools.json.JsonAnalysis Maven / Gradle / Ivy

There is a newer version: 0.2.2
Show newest version
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")) {
                        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;
    }
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy