目录
概要:提供java工具类,为json数据附上随机value
一、应用场景
1.快速伪造JSON数据用于简单的接口测试
2.压力测试环境,造一批不同的数据以发起请求(可以结合kafka+gor可以实现自定义压力测试,比jemter功能更开放)
二、用法描述
1.自定义关键字
概述:以{{}}包括表示它是一个将会变成随机值的字段,{{nls,10}}表示将会随机产生数字+字母的长度为10位的字符串
数字:{{num,10}}
字母:{{let,10}}
数字+字母(小写):{{nls,20}}
数字+字母(大写):{{nll,20}}
数字+字母(大小写):${{nlm,10}}
ipv4:{{ipv4}}
ipv6:{{ipv6}}
随机字符串:{{random,10}}
(可以自己做补充,随机字符串这里没写实现)
2.示例
请求数据:
{
"Req_URL":"/v8/video/test?uid={{nls,32}}&type=1={{nls,40}}&did={{nls,20}}&mac={{nls,40}}&pageSize=8&plId=&seqId={{nls,32}}",
"Req_Type":"1",
"Req_ID":"{{nls,40}}",
"Req_Ts":"1604308586674008370",
"Req_Method":"GET"
}
生成数据:
{
"Req_URL": "/v8/video/test?uid=ewh5d17ey82raq6093deir5h92hvc532&type=1=hd4f149g340hz6l29w7o405i8z19fcgoqjgb711g&did=236211ov51ilv967c7r7&mac=p24n1s12xfq68b81vv7o448o26f43858ruqo1a3z&pageSize=8&plId=&seqId=bkci1523j496782xoj49f765zq818j49",
"Req_Type": "1",
"Req_ID": "ltz878d67yi6915x810tz56bm5t26a275w69x4j1",
"Req_Ts": "1604308586674008370",
"Req_Method": "GET"
}
三、代码
import net.sf.json.JSONObject;
import java.util.Iterator;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Title:
* Description:
* author :xbl
* Date:2020/11/4
* Time:14:58
*/
public class JsonGeneratorUtil {
/**
* 正则,取出 {{value}} 格式的数据
*/
public static final String pattern = "\\{\\{(.* ?)}}";
/** 指定random字段格式key 例如:{{num,10}} 表示生成10位随机数字*/
/**
* 数字
*/
public static final String NUM_RANDOM_KEY = "num";
/**
* 字母(小写)
*/
public static final String LETS_RANDOM_KEY = "lets";
/**
* 字母(大写)
*/
public static final String LETL_RANDOM_KEY = "letl";
/**
* 字母(大小写)
*/
public static final String LETM_RANDOM_KEY = "letm";
/**
* 数字+字母(小写)
*/
public static final String NLS_RANDOM_KEY = "nls";
/**
* 数字+字母(大写)
*/
public static final String NLL_RANDOM_KEY = "nll";
/**
* 数字+字母(大小写)
*/
public static final String NLM_RANDOM_KEY = "nlm";
/**
* ipv4
*/
public static final String IPV4_RANDOM_KEY = "ipv4";
/**
* ipv6
*/
public static final String IPV6_RANDOM_KEY = "ipv6";
/**
* 随机字符串
*/
public static final String STR_RANDOM_KEY = "random";
private static final String SPLID_RANDOM_KEY = "}}";
private static final String SPLID = ",";
private static final String LET_SMALL = "abcdefghijklmnopqrstuvwxyz";
private static final String LET_LARGE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static final String LET_MIDDLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
/**
* 处理格式生成JSON数据(下标替换字符串)
*
* @param reqJson
* @return
*/
public static JSONObject buildJson(JSONObject reqJson) {
Iterator iterator = reqJson.keys();
Pattern p = Pattern.compile(pattern);
while (iterator.hasNext()) {
String key = (String) iterator.next();
String value = reqJson.getString(key);
String[] values = value.split(SPLID_RANDOM_KEY);
StringBuilder sb = new StringBuilder(value);
int lens = 0;
int totalSize = 0;
if (values.length > 0) {
for (String str : values) {
String v = str + SPLID_RANDOM_KEY;
Matcher matcher = p.matcher(v);
if (matcher.find() && matcher.groupCount() >= 1) {
String tt = replaceRandom(matcher.group(1).split(SPLID)[0], Integer.parseInt(matcher.group(1).split(SPLID)[1]));
int size = (matcher.group(1).length() + 4);
int index = (lens + v.length() - size);
sb.delete(index + totalSize, index + totalSize + size);
sb.insert(index + totalSize, tt);
totalSize += tt.length() - size;
}
lens += v.length();
}
reqJson.put(key, sb.toString());
}
}
return reqJson;
}
/**
* 如:将 {{num,10}} 生成 10位随机数字
*
* @return
*/
private static String replaceRandom(String mat, int count) {
String result = "";
Random random = new Random();
switch (mat) {
//随机数字生成
case NUM_RANDOM_KEY:
for (int i = 0; i < count; i++) {
result += (int) (10 * (Math.random()));
}
break;
//字母(小)
case LETS_RANDOM_KEY:
char[] c1 = LET_SMALL.toCharArray();
for (int i = 0; i < count; i++) {
result += c1[random.nextInt(c1.length)];
}
break;
//字母(大)
case LETL_RANDOM_KEY:
char[] c2 = LET_LARGE.toCharArray();
for (int i = 0; i < count; i++) {
result += c2[random.nextInt(c2.length)];
}
break;
//字母(大小)
case LETM_RANDOM_KEY:
char[] c3 = LET_MIDDLE.toCharArray();
for (int i = 0; i < count; i++) {
result += c3[random.nextInt(c3.length)];
}
break;
//数字+字母(小写)
case NLS_RANDOM_KEY:
result = getCharAndNum(count, true);
break;
//数字+字母(大写)
case NLL_RANDOM_KEY:
result = getCharAndNum(count, false);
break;
//数字+字母(大小写)
case NLM_RANDOM_KEY:
result = getCharAndNum(count, null);
break;
//ipv4
case IPV4_RANDOM_KEY:
result = getRandomIp();
break;
//ipv6 to-do
case IPV6_RANDOM_KEY:
break;
//随机字符串 to-do 同时 encode
case STR_RANDOM_KEY:
break;
default:
break;
}
return result;
}
/**
* 随机生成数字+字符串
*
* @param length 生成字符串长度
* @param isSmall null:随机大小写 true:小写 false:大写
* @return
*/
public static String getCharAndNum(int length, Boolean isSmall) {
String val = "";
Random random = new Random();
for (int i = 0; i < length; i++) {
// 输出字母还是数字
String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
// 字符串
if ("char".equalsIgnoreCase(charOrNum)) {
// 取得大写字母还是小写字母
if (isSmall == null) {
int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
val += (char) (choice + random.nextInt(26));
} else if (isSmall) {
val += (char) (97 + random.nextInt(26));
} else {
val += (char) (65 + random.nextInt(26));
}
} else if ("num".equalsIgnoreCase(charOrNum)) { // 数字
val += String.valueOf(random.nextInt(10));
}
}
return val;
}
/**
* 生成国内ipv4
*
* @return
*/
private static String getRandomIp() {
// ip范围
int[][] range = {{607649792, 608174079}, // 36.56.0.0-36.63.255.255
{1038614528, 1039007743}, // 61.232.0.0-61.237.255.255
{1783627776, 1784676351}, // 106.80.0.0-106.95.255.255
{2035023872, 2035154943}, // 121.76.0.0-121.77.255.255
{2078801920, 2079064063}, // 123.232.0.0-123.235.255.255
{-1950089216, -1948778497}, // 139.196.0.0-139.215.255.255
{-1425539072, -1425014785}, // 171.8.0.0-171.15.255.255
{-1236271104, -1235419137}, // 182.80.0.0-182.92.255.255
{-770113536, -768606209}, // 210.25.0.0-210.47.255.255
{-569376768, -564133889}, // 222.16.0.0-222.95.255.255
};
Random rdint = new Random();
int index = rdint.nextInt(10);
String ip = num2ip(range[index][0] + new Random().nextInt(range[index][1] - range[index][0]));
return ip;
}
private static String num2ip(int ip) {
int[] b = new int[4];
String x = "";
b[0] = (int) ((ip >> 24) & 0xff);
b[1] = (int) ((ip >> 16) & 0xff);
b[2] = (int) ((ip >> 8) & 0xff);
b[3] = (int) (ip & 0xff);
x = Integer.toString(b[0]) + "." + Integer.toString(b[1]) + "." + Integer.toString(b[2]) + "."
+ Integer.toString(b[3]);
return x;
}
}
代码可以直接拿着用,核心代码就是方法 buildJson()。
这个代码不止于此,还能用到很多地方,它的真实作用:指定替换字符串中某一段具体字符串,不像replace方法替换所有符合要求的字符串。