java发送请求-接口数据存储到数据库沉淀之PatternUtil工具类优化
代码
public class PatternUtil {
private static Map map = new HashMap();
private static String DepKeyValueRegex = "([\w/]+):([\$\.\w]+)";
private static String reqDataRegex = "([\w/]+):([\$\.\w]+)";
private static String compareResultRegex = "(\$[\.\w]+)=([\u4e00-\u9fa5\w]+)";
private static Logger logger = Logger.getLogger(PatternUtil.class);
private PatternUtil() {}
public static void compareResultToReportng(String expResult,String actResult) {
//建立正则表达式
Pattern p = Pattern.compile(compareResultRegex);
logger.info("建立" + compareResultRegex + "正则表达式");
//匹配结果
Matcher m = p.matcher(expResult);
while(m.find()) {
//excel内的预期值
String jsonPath = m.group(1);
String expValue = m.group(2);
logger.info("预期值:" + expValue);
//服务器返回的实际值
String actValue = JSONPath.read(actResult, jsonPath).toString();
logger.info("实际值:" + actValue);
Assert.assertEquals(actValue, expValue);
logger.info("Assert比对成功");
}
}
public static int compareResultToDb(String expResult,String actResult) {
//初始化
List list = new ArrayList();
//建立正则表达式
Pattern p = Pattern.compile(compareResultRegex);
logger.info("建立" + compareResultRegex + "正则表达式");
//匹配结果
Matcher m = p.matcher(expResult);
while(m.find()) {
//excel内的预期值
String jsonPath = m.group(1);
String expValue = m.group(2);
logger.info("预期值:" + expValue);
//服务器返回的实际值
String actValue = JSONPath.read(actResult, jsonPath).toString();
logger.info("实际值:" + actValue);
//收集比对结果
int status = actValue.equals(expValue)?1:0;
list.add(status);
}
return list.contains(0)?0:1;
}
public static void storeDepKeyValue(String depKey,String actResult) {
Pattern p = Pattern.compile(DepKeyValueRegex);
logger.info("建立" + DepKeyValueRegex + "正则表达式");
Matcher m = p.matcher(depKey);
while(m.find()) {
String key = m.group();
logger.info("key="+ key);
String value = JSONPath.read(actResult, m.group(2)).toString();
logger.info("value="+ value);
map.put(key, value);
logger.info("map: " + map);
}
}
public static String handlerReqDataOfDep(String reqData) {
Pattern p = Pattern.compile(reqDataRegex);
logger.info("建立" + reqDataRegex + "正则表达式");
Matcher m = p.matcher(reqData);
while(m.find()) {
//2-1、需要得到map中的key
String key = m.group();
logger.info("map中的key="+ key);
//2-2、基于key从map中取值
String value = map.get(key);
logger.info("基于key从map中取的value值="+ value);
reqData = StringUtil.replaceStr(reqData, key, value);
}
logger.info("接口的请求参数:" + reqData);
return reqData;
}
}