fix(接口测试): 脚本添加多条环境变量时,只生效了一条

This commit is contained in:
AgAngle 2024-04-15 16:13:01 +08:00 committed by Craftsman
parent d145988a86
commit c79212f225
1 changed files with 27 additions and 3 deletions

View File

@ -111,6 +111,7 @@ public class ApiEnvironmentService {
JsonNode jsonObj = elements.next(); JsonNode jsonObj = elements.next();
if (jsonObj.has(NAME) && jsonObj.has(VALUE) if (jsonObj.has(NAME) && jsonObj.has(VALUE)
&& jsonObj.get(NAME).asText().equals(entry.getKey())) { && jsonObj.get(NAME).asText().equals(entry.getKey())) {
contains = true;
if (jsonObj.get(VALUE).asText().equals(entry.getValue())) { if (jsonObj.get(VALUE).asText().equals(entry.getValue())) {
break; break;
} }
@ -118,17 +119,31 @@ public class ApiEnvironmentService {
envNeedUpdate = true; envNeedUpdate = true;
break; break;
} else { } else {
contains = true;
envNeedUpdate = true; envNeedUpdate = true;
} }
} }
if (contains) { if (!contains) {
// 不包含则添加
ObjectNode itemObj = objectMapper.createObjectNode(); ObjectNode itemObj = objectMapper.createObjectNode();
itemObj.put(NAME, entry.getKey()); itemObj.put(NAME, entry.getKey());
itemObj.put(VALUE, entry.getValue()); itemObj.put(VALUE, entry.getValue());
itemObj.put(ENABLE, true); itemObj.put(ENABLE, true);
((ArrayNode) variables).set(length - 1, itemObj); ArrayNode arrayNode = ((ArrayNode) variables);
JsonNode lastNode = arrayNode.get(arrayNode.size() - 1);
boolean isBlankLine = isBlankLine(lastNode);
if (isBlankLine) {
// 如果最后一行是空行则替换最后一行
((ArrayNode) variables).set(length - 1, itemObj);
} else {
((ArrayNode) variables).add(itemObj);
}
// 新增一个空行
ObjectNode blankItem = objectMapper.createObjectNode();
blankItem.put(NAME, StringUtils.EMPTY);
blankItem.put(VALUE, StringUtils.EMPTY);
blankItem.put(ENABLE, true);
((ArrayNode) variables).add(blankItem);
((ObjectNode) configObj).set(VARIABLES, variables); ((ObjectNode) configObj).set(VARIABLES, variables);
} }
} }
@ -142,6 +157,15 @@ public class ApiEnvironmentService {
} }
} }
private boolean isBlankLine(JsonNode lastNode) {
boolean nameBlank = lastNode.get(NAME) == null ? true : StringUtils.isBlank(lastNode.get(NAME).asText());
boolean valueBlank = lastNode.get(VALUE) == null ? true : StringUtils.isBlank(lastNode.get(VALUE).asText());
if (nameBlank && valueBlank) {
return true;
}
return false;
}
private List<JsonNode> createArray(Map<String, String> varMap) { private List<JsonNode> createArray(Map<String, String> varMap) {
return varMap.entrySet().stream() return varMap.entrySet().stream()
.map(entry -> objectMapper.createObjectNode() .map(entry -> objectMapper.createObjectNode()