refactor(接口测试): 优化json-schema字符串自动生成
--bug=1044188 --user=陈建星 【接口测试】场景详情-自定义请求-请求体json-schema格式-参数值为空-未配置高级设置-json自动生成参数值非8位 https://www.tapd.cn/55049933/s/1550605
This commit is contained in:
parent
7e9a56f09e
commit
4aa3ea1566
|
@ -135,41 +135,7 @@ public class JsonSchemaBuilder {
|
||||||
String type = getPropertyTextValue(propertyNode, PropertyConstant.TYPE);
|
String type = getPropertyTextValue(propertyNode, PropertyConstant.TYPE);
|
||||||
String value = getPropertyTextValue(propertyNode, PropertyConstant.EXAMPLE);
|
String value = getPropertyTextValue(propertyNode, PropertyConstant.EXAMPLE);
|
||||||
return switch (type) {
|
return switch (type) {
|
||||||
case PropertyConstant.STRING -> {
|
case PropertyConstant.STRING -> new TextNode(generateStrValue(propertyNode, value));
|
||||||
if (StringUtils.isBlank(value)) {
|
|
||||||
JsonNode enumValues = propertyNode.get(PropertyConstant.ENUM_VALUES);
|
|
||||||
JsonNode defaultValue = propertyNode.get(PropertyConstant.DEFAULT_VALUE);
|
|
||||||
JsonNode pattern = propertyNode.get(PropertyConstant.PATTERN);
|
|
||||||
JsonNode maxLength = propertyNode.get(PropertyConstant.MAX_LENGTH);
|
|
||||||
JsonNode minLength = propertyNode.get(PropertyConstant.MIN_LENGTH);
|
|
||||||
int max = isTextNotBlank(maxLength) ? maxLength.asInt() : 20;
|
|
||||||
int min = isTextNotBlank(minLength) ? minLength.asInt() : 1;
|
|
||||||
if (enumValues != null && enumValues instanceof ArrayNode) {
|
|
||||||
value = enumValues.get(new SecureRandom().nextInt(enumValues.size())).asText();
|
|
||||||
if (value.length() > max) {
|
|
||||||
value = value.substring(0, max);
|
|
||||||
}
|
|
||||||
} else if (isTextNotBlank(pattern)) {
|
|
||||||
try {
|
|
||||||
Node node = new OrdinaryNode(pattern.asText());
|
|
||||||
value = node.random();
|
|
||||||
} catch (Exception e) {
|
|
||||||
value = pattern.asText();
|
|
||||||
}
|
|
||||||
} else if (isTextNotBlank(defaultValue)) {
|
|
||||||
value = defaultValue.asText();
|
|
||||||
if (value.length() > max) {
|
|
||||||
value = value.substring(0, max);
|
|
||||||
}
|
|
||||||
if (value.length() < min) {
|
|
||||||
value = value + generateStr(min - value.length());
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
value = generateStr(new SecureRandom().nextInt(max - min + 1) + min);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
yield new TextNode(value);
|
|
||||||
}
|
|
||||||
case PropertyConstant.INTEGER -> {
|
case PropertyConstant.INTEGER -> {
|
||||||
if (StringUtils.isBlank(value)) {
|
if (StringUtils.isBlank(value)) {
|
||||||
JsonNode enumValues = propertyNode.get(PropertyConstant.ENUM_VALUES);
|
JsonNode enumValues = propertyNode.get(PropertyConstant.ENUM_VALUES);
|
||||||
|
@ -238,6 +204,58 @@ public class JsonSchemaBuilder {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String generateStrValue(JsonNode propertyNode, String value) {
|
||||||
|
if (StringUtils.isBlank(value)) {
|
||||||
|
JsonNode enumValues = propertyNode.get(PropertyConstant.ENUM_VALUES);
|
||||||
|
JsonNode defaultValue = propertyNode.get(PropertyConstant.DEFAULT_VALUE);
|
||||||
|
JsonNode pattern = propertyNode.get(PropertyConstant.PATTERN);
|
||||||
|
JsonNode maxLength = propertyNode.get(PropertyConstant.MAX_LENGTH);
|
||||||
|
JsonNode minLength = propertyNode.get(PropertyConstant.MIN_LENGTH);
|
||||||
|
int max;
|
||||||
|
int min;
|
||||||
|
if (isTextNotBlank(maxLength) && isTextNotBlank(minLength)) {
|
||||||
|
max = maxLength.asInt();
|
||||||
|
min = minLength.asInt();
|
||||||
|
} else if (isTextNotBlank(maxLength)) {
|
||||||
|
// 只填了最大值,最小值默为 1
|
||||||
|
max = maxLength.asInt();
|
||||||
|
min = max > 0 ? 1 : 0;
|
||||||
|
} else if (isTextNotBlank(minLength)) {
|
||||||
|
// 只填了最小值,最大值默为最小值 + 10
|
||||||
|
min = minLength.asInt();
|
||||||
|
max = min + 10;
|
||||||
|
} else {
|
||||||
|
// 都没填,默认生成8位
|
||||||
|
max = 8;
|
||||||
|
min = 8;
|
||||||
|
}
|
||||||
|
if (enumValues != null && enumValues instanceof ArrayNode) {
|
||||||
|
value = enumValues.get(new SecureRandom().nextInt(enumValues.size())).asText();
|
||||||
|
if (isTextNotBlank(maxLength) && value.length() > max) {
|
||||||
|
value = value.substring(0, max);
|
||||||
|
}
|
||||||
|
} else if (isTextNotBlank(pattern)) {
|
||||||
|
try {
|
||||||
|
Node node = new OrdinaryNode(pattern.asText());
|
||||||
|
value = node.random();
|
||||||
|
} catch (Exception e) {
|
||||||
|
value = pattern.asText();
|
||||||
|
}
|
||||||
|
} else if (isTextNotBlank(defaultValue)) {
|
||||||
|
value = defaultValue.asText();
|
||||||
|
if (isTextNotBlank(maxLength) && value.length() > max) {
|
||||||
|
value = value.substring(0, max);
|
||||||
|
}
|
||||||
|
if (value.length() < min) {
|
||||||
|
value = value + generateStr(min - value.length());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
value = generateStr(new SecureRandom().nextInt(max - min + 1) + min);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
private static String generateStr(int length) {
|
private static String generateStr(int length) {
|
||||||
return RandomStringGenerator.builder().withinRange('0', 'z').build().generate(length);
|
return RandomStringGenerator.builder().withinRange('0', 'z').build().generate(length);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1807,6 +1807,12 @@ public class ApiDefinitionControllerTests extends BaseTest {
|
||||||
{"type":"object","enable":true,"properties":{"arraywww":{"type":"array","enable":true,"items":[{"type":"string","example":"1","description":"","additionalProperties":null,"defaultValue":"","pattern":null,"maxLength":null,"minLength":null,"minimum":null,"maximum":null,"maxItems":null,"minItems":null,"format":null,"enable":true},{"type":"number","example":"2","description":"","additionalProperties":null,"defaultValue":"","pattern":null,"maxLength":null,"minLength":null,"minimum":null,"maximum":null,"maxItems":null,"minItems":null,"format":null,"enable":true}],"minItems":null,"maxItems":null},"string":{"type":"string","example":"@natural(1,100)","description":"","additionalProperties":null,"defaultValue":"","pattern":null,"maxLength":null,"minLength":null,"minimum":null,"maximum":null,"maxItems":null,"minItems":null,"format":null,"enable":true},"int":{"type":"integer","example":"intValue","description":"","additionalProperties":null,"defaultValue":"","pattern":null,"maxLength":null,"minLength":null,"minimum":null,"maximum":null,"maxItems":null,"minItems":null,"format":null,"enable":true},"num":{"type":"number","example":"","description":"","additionalProperties":null,"defaultValue":"","pattern":null,"maxLength":null,"minLength":null,"minimum":null,"maximum":null,"maxItems":null,"minItems":null,"format":null,"enable":true},"boolean":{"type":"boolean","example":"booleanValue","description":"","additionalProperties":null,"defaultValue":"","pattern":null,"maxLength":null,"minLength":null,"minimum":null,"maximum":null,"maxItems":null,"minItems":null,"format":null,"enable":true},"null":{"type":"null","enable":true},"默认值大于最大长度":{"type":"string","example":"","description":"","enable":true,"defaultValue":"sdfdsd","maxLength":1,"minLength":0},"默认值小于最小长度":{"type":"string","example":"","description":"","enable":true,"defaultValue":"1","maxLength":3,"minLength":2},"数组项小于最小项":{"type":"array","enable":true,"items":[],"minItems":2,"maxItems":3},"数组项大于最大项":{"type":"array","enable":true,"items":[null,null],"minItems":0,"maxItems":1},"正则大于最大长度":{"type":"string","example":"","description":"","enable":true,"defaultValue":"","maxLength":2,"minLength":0,"pattern":"[A-Z]{4}"}}}
|
{"type":"object","enable":true,"properties":{"arraywww":{"type":"array","enable":true,"items":[{"type":"string","example":"1","description":"","additionalProperties":null,"defaultValue":"","pattern":null,"maxLength":null,"minLength":null,"minimum":null,"maximum":null,"maxItems":null,"minItems":null,"format":null,"enable":true},{"type":"number","example":"2","description":"","additionalProperties":null,"defaultValue":"","pattern":null,"maxLength":null,"minLength":null,"minimum":null,"maximum":null,"maxItems":null,"minItems":null,"format":null,"enable":true}],"minItems":null,"maxItems":null},"string":{"type":"string","example":"@natural(1,100)","description":"","additionalProperties":null,"defaultValue":"","pattern":null,"maxLength":null,"minLength":null,"minimum":null,"maximum":null,"maxItems":null,"minItems":null,"format":null,"enable":true},"int":{"type":"integer","example":"intValue","description":"","additionalProperties":null,"defaultValue":"","pattern":null,"maxLength":null,"minLength":null,"minimum":null,"maximum":null,"maxItems":null,"minItems":null,"format":null,"enable":true},"num":{"type":"number","example":"","description":"","additionalProperties":null,"defaultValue":"","pattern":null,"maxLength":null,"minLength":null,"minimum":null,"maximum":null,"maxItems":null,"minItems":null,"format":null,"enable":true},"boolean":{"type":"boolean","example":"booleanValue","description":"","additionalProperties":null,"defaultValue":"","pattern":null,"maxLength":null,"minLength":null,"minimum":null,"maximum":null,"maxItems":null,"minItems":null,"format":null,"enable":true},"null":{"type":"null","enable":true},"默认值大于最大长度":{"type":"string","example":"","description":"","enable":true,"defaultValue":"sdfdsd","maxLength":1,"minLength":0},"默认值小于最小长度":{"type":"string","example":"","description":"","enable":true,"defaultValue":"1","maxLength":3,"minLength":2},"数组项小于最小项":{"type":"array","enable":true,"items":[],"minItems":2,"maxItems":3},"数组项大于最大项":{"type":"array","enable":true,"items":[null,null],"minItems":0,"maxItems":1},"正则大于最大长度":{"type":"string","example":"","description":"","enable":true,"defaultValue":"","maxLength":2,"minLength":0,"pattern":"[A-Z]{4}"}}}
|
||||||
""";
|
""";
|
||||||
requestPostWithOk(JSON_SCHEMA_AUTO_GENERATE, JSON.parseObject(jsonString, JsonSchemaItem.class));
|
requestPostWithOk(JSON_SCHEMA_AUTO_GENERATE, JSON.parseObject(jsonString, JsonSchemaItem.class));
|
||||||
|
|
||||||
|
// 字符串自动生成
|
||||||
|
jsonString = """
|
||||||
|
{"type":"object","enable":true,"properties":{"默认8位":{"type":"string","example":"","description":"","enable":true,"defaultValue":"","format":"date-time"},"最小2位":{"type":"string","example":"","description":"","enable":true,"defaultValue":"","minLength":2},"最大4":{"type":"string","example":"","description":"","enable":true,"defaultValue":"","maxLength":4},"最小2位,最大4":{"type":"string","example":"","description":"","enable":true,"defaultValue":"","maxLength":4,"minLength":2}}}
|
||||||
|
""";
|
||||||
|
requestPostWithOk(JSON_SCHEMA_AUTO_GENERATE, JSON.parseObject(jsonString, JsonSchemaItem.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue