fix(接口测试): getJSONObject获取不包含的属性报错问题修复

This commit is contained in:
fit2-zhao 2022-10-12 14:55:21 +08:00 committed by f2c-ci-robot[bot]
parent ac8234a8ab
commit 138cd4b3ae
21 changed files with 81 additions and 80 deletions

View File

@ -379,7 +379,7 @@ public class ElementUtil {
*/
public static void relationships(JSONArray hashTree, List<String> referenceRelationships) {
for (int i = 0; i < hashTree.length(); i++) {
JSONObject element = hashTree.getJSONObject(i);
JSONObject element = hashTree.optJSONObject(i);
if (element != null && StringUtils.equals(element.get(PropertyConstant.TYPE).toString(), ElementConstants.SCENARIO) && StringUtils.equals(element.get("referenced").toString(), "REF")) {
if (!referenceRelationships.contains(element.get("id").toString())) {
referenceRelationships.add(element.get("id").toString());
@ -395,7 +395,7 @@ public class ElementUtil {
public static void dataFormatting(JSONArray hashTree) {
for (int i = 0; i < hashTree.length(); i++) {
JSONObject element = hashTree.getJSONObject(i);
JSONObject element = hashTree.optJSONObject(i);
if (element == null) {
continue;
}
@ -426,7 +426,7 @@ public class ElementUtil {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
for (int i = 0; i < hashTree.length(); i++) {
JSONObject element = hashTree.getJSONObject(i);
JSONObject element = hashTree.optJSONObject(i);
boolean isScenarioEnv = false;
ParameterConfig config = new ParameterConfig();
if (element != null && element.get(PropertyConstant.TYPE).toString().equals(ElementConstants.SCENARIO)) {
@ -746,7 +746,7 @@ public class ElementUtil {
Map<String, List<JSONObject>> groupMap = new LinkedHashMap<>();
if (elements != null) {
for (int i = 0; i < elements.length(); i++) {
JSONObject item = elements.getJSONObject(i);
JSONObject item = elements.optJSONObject(i);
if (ElementConstants.ASSERTIONS.equals(item.optString(PropertyConstant.TYPE))) {
if (groupMap.containsKey(ASSERTIONS)) {
groupMap.get(ASSERTIONS).add(item);

View File

@ -3,27 +3,23 @@ package io.metersphere.api.dto.definition.request.sampler;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.metersphere.api.dto.mock.MockApiHeaders;
import io.metersphere.api.parse.api.JMeterScriptUtil;
import io.metersphere.api.dto.definition.request.ElementUtil;
import io.metersphere.api.dto.definition.request.ParameterConfig;
import io.metersphere.api.dto.definition.request.assertions.MsAssertions;
import io.metersphere.api.dto.definition.request.auth.MsAuthManager;
import io.metersphere.api.dto.definition.request.dns.MsDNSCacheManager;
import io.metersphere.api.dto.mock.MockApiHeaders;
import io.metersphere.api.dto.scenario.Body;
import io.metersphere.api.dto.scenario.HttpConfig;
import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.api.dto.scenario.environment.CommonConfig;
import io.metersphere.api.dto.scenario.environment.EnvironmentConfig;
import io.metersphere.service.definition.ApiTestCaseService;
import io.metersphere.api.parse.api.JMeterScriptUtil;
import io.metersphere.base.domain.ApiTestCaseWithBLOBs;
import io.metersphere.commons.constants.ElementConstants;
import io.metersphere.commons.constants.MsTestElementConstants;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.FileUtils;
import io.metersphere.commons.utils.JSON;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.*;
import io.metersphere.environment.service.CommandService;
import io.metersphere.environment.ssl.KeyStoreConfig;
import io.metersphere.environment.ssl.KeyStoreFile;
@ -31,8 +27,7 @@ import io.metersphere.environment.ssl.MsKeyStore;
import io.metersphere.jmeter.utils.ScriptEngineUtils;
import io.metersphere.plugin.core.MsParameter;
import io.metersphere.plugin.core.MsTestElement;
import io.metersphere.commons.utils.HashTreeUtil;
import io.metersphere.commons.utils.JSONUtil;
import io.metersphere.service.definition.ApiTestCaseService;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.collections.CollectionUtils;
@ -47,6 +42,7 @@ import org.apache.jmeter.protocol.http.util.HTTPConstants;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.collections.HashTree;
import org.codehaus.jackson.JsonNode;
import org.json.JSONObject;
import java.net.URL;
@ -312,9 +308,9 @@ public class MsHTTPSamplerProxy extends MsTestElement {
String useEvnId = environmentConfig.getApiEnvironmentid();
if (this.authManager == null && environmentConfig.getAuthManager() != null && environmentConfig.getAuthManager().has("authManager")) {
try {
JSONObject authObject = environmentConfig.getAuthManager().getJSONObject("authManager");
JsonNode authObject = environmentConfig.getAuthManager().get("authManager");
if (authObject != null) {
if (authObject.has("verification") && !StringUtils.equalsIgnoreCase(authObject.optString("verification"), "No Auth")) {
if (authObject.has("verification") && !StringUtils.equalsIgnoreCase(authObject.get("verification").asText(), "No Auth")) {
this.authManager = JSON.parseObject(authObject.toString(), MsAuthManager.class);
}
}

View File

@ -9,7 +9,7 @@ import io.metersphere.api.dto.scenario.HttpConfig;
import io.metersphere.api.dto.scenario.TCPConfig;
import io.metersphere.environment.ssl.KeyStoreConfig;
import lombok.Data;
import org.json.JSONObject;
import org.codehaus.jackson.node.ObjectNode;
import java.util.ArrayList;
import java.util.List;
@ -31,7 +31,7 @@ public class EnvironmentConfig {
private MsJSR223Processor postStepProcessor;
//全局前后置脚本都配置
private GlobalScriptConfig globalScriptConfig;
private JSONObject authManager;
private ObjectNode authManager;
private List<MsAssertions> assertions;
private boolean useErrorCode;
private boolean higherThanSuccess;

View File

@ -158,8 +158,8 @@ public abstract class AbstractEngine implements Engine {
if (jsonArray.get(i) instanceof List) {
JSONArray o = jsonArray.getJSONArray(i);
for (int j = 0; j < o.length(); j++) {
if (StringUtils.equals(o.getJSONObject(j).optString("key"), "TargetLevel")) {
s += o.getJSONObject(j).optInt("value");
if (StringUtils.equals(o.optJSONObject(j).optString("key"), "TargetLevel")) {
s += o.optJSONObject(j).optInt("value");
break;
}
}

View File

@ -41,7 +41,7 @@ public class ApiEnvironmentRunningParamService {
try {
JSONObject configObj = JSONUtil.parseObject(environment.getConfig());
if (configObj.has("commonConfig")) {
JSONObject commonConfig = configObj.getJSONObject("commonConfig");
JSONObject commonConfig = configObj.optJSONObject("commonConfig");
if (commonConfig.has("variables")) {
JSONArray variables = commonConfig.getJSONArray("variables");
List<JSONObject> variableList = new LinkedList<>();
@ -51,7 +51,7 @@ public class ApiEnvironmentRunningParamService {
boolean contains = false;
for (int i = 0; i < variables.length(); i++) {
JSONObject jsonObj = variables.getJSONObject(i);
JSONObject jsonObj = variables.optJSONObject(i);
if (jsonObj.has("name") && StringUtils.equals(jsonObj.optString("name"), key)) {
contains = true;
if (jsonObj.has("value") && StringUtils.equals(jsonObj.optString("value"), value)) {

View File

@ -221,9 +221,9 @@ public abstract class HarScenarioAbstractParser<T> extends ApiImportAbstractPars
private void parseRawBody(Body body, JSONObject postmanBody, String bodyMode) {
body.setRaw(postmanBody.optString(bodyMode));
body.setType(MsRequestBodyType.RAW.value());
JSONObject options = postmanBody.getJSONObject("options");
JSONObject options = postmanBody.optJSONObject("options");
if (options != null) {
JSONObject raw = options.getJSONObject(PostmanRequestBodyMode.RAW.value());
JSONObject raw = options.optJSONObject(PostmanRequestBodyMode.RAW.value());
if (raw != null) {
String bodyType = "";
switch (raw.optString("language")) {

View File

@ -17,10 +17,10 @@ import java.util.*;
public abstract class MsAbstractParser<T> extends ApiImportAbstractParser<T> {
protected List<MsHTTPSamplerProxy> parseMsHTTPSamplerProxy(JSONObject testObject, String tag, boolean isSetUrl) {
JSONObject requests = testObject.getJSONObject(tag);
JSONObject requests = testObject.optJSONObject(tag);
List<MsHTTPSamplerProxy> msHTTPSamplerProxies = new ArrayList<>();
requests.keySet().forEach(requestName -> {
JSONObject requestObject = requests.getJSONObject(requestName);
JSONObject requestObject = requests.optJSONObject(requestName);
String path = requestObject.optString("url");
String method = requestObject.optString("method");
MsHTTPSamplerProxy request = buildRequest(requestName, path, method);
@ -67,7 +67,7 @@ public abstract class MsAbstractParser<T> extends ApiImportAbstractParser<T> {
JSONArray headers = requestObject.getJSONArray("headers");
if (headers != null) {
for (int i = 0; i < headers.length(); i++) {
JSONObject header = headers.getJSONObject(i);
JSONObject header = headers.optJSONObject(i);
msHeaders.add(new KeyValue(header.optString("name"), header.optString("value")));
}
}
@ -88,7 +88,7 @@ public abstract class MsAbstractParser<T> extends ApiImportAbstractParser<T> {
msBody.setRaw(bodyStr.toString());
}
} else if (body instanceof JSONObject) {
JSONObject bodyObj = requestObject.getJSONObject("body");
JSONObject bodyObj = requestObject.optJSONObject("body");
if (bodyObj != null) {
ArrayList<KeyValue> kvs = new ArrayList<>();
bodyObj.keySet().forEach(key -> {

View File

@ -159,9 +159,9 @@ public abstract class PostmanAbstractParserParser<T> extends ApiImportAbstractPa
private void parseRawBody(Body body, JSONObject postmanBody, String bodyMode) {
body.setRaw(parseVariable(postmanBody.optString(bodyMode)));
body.setType(MsRequestBodyType.RAW.value());
JSONObject options = postmanBody.getJSONObject("options");
JSONObject options = postmanBody.optJSONObject("options");
if (options != null) {
JSONObject raw = options.getJSONObject(PostmanRequestBodyMode.RAW.value());
JSONObject raw = options.optJSONObject(PostmanRequestBodyMode.RAW.value());
if (raw != null) {
String bodyType = "";
switch (raw.optString("language")) {

View File

@ -63,7 +63,7 @@ public class MsScenarioParser extends MsAbstractParser<ScenarioImport> {
if (StringUtils.isNotBlank(scenarioDefinitionStr)) {
JSONObject scenarioDefinition = JSONUtil.parseObject(scenarioDefinitionStr);
if (scenarioDefinition != null) {
JSONObject environmentMap = scenarioDefinition.getJSONObject("environmentMap");
JSONObject environmentMap = scenarioDefinition.optJSONObject("environmentMap");
if (environmentMap != null) {
scenarioDefinition.put("environmentMap", new HashMap<>());
}

View File

@ -58,14 +58,14 @@ public class TCPService {
LogUtil.error(e);
}
if (responseObj.has("responseResult")) {
JSONObject respResultObj = responseObj.getJSONObject("responseResult");
JSONObject respResultObj = responseObj.optJSONObject("responseResult");
if (respResultObj.has("body")) {
MockApiUtils mockApiUtils = new MockApiUtils();
boolean useScript = false;
if (respResultObj.has("usePostScript")) {
useScript = respResultObj.getBoolean("usePostScript");
}
returnMsg = mockApiUtils.getResultByResponseResult(matchdMockExpectDTO.getProjectId(), respResultObj.getJSONObject("body"), "", null, null, useScript);
returnMsg = mockApiUtils.getResultByResponseResult(matchdMockExpectDTO.getProjectId(), respResultObj.optJSONObject("body"), "", null, null, useScript);
}
try {
if (respResultObj.has("delayed")) {

View File

@ -49,12 +49,12 @@ public class HashTreeUtil {
try {
JSONObject configJson = JSONUtil.parseObject(environment.getConfig());
if (configJson.has(COMMON_CONFIG)) {
JSONObject commonConfig = configJson.getJSONObject(COMMON_CONFIG);
JSONObject commonConfig = configJson.optJSONObject(COMMON_CONFIG);
if (commonConfig.has(VARIABLES)) {
Map<String, String> envHeadMap = new HashMap<>();
JSONArray variablesArr = commonConfig.getJSONArray(VARIABLES);
for (int i = 0; i < variablesArr.length(); i++) {
JSONObject object = variablesArr.getJSONObject(i);
JSONObject object = variablesArr.optJSONObject(i);
if (object.has(NAME) && object.has(VALUE)) {
boolean isEnable = true;
if (object.has(ENABLE)) {

View File

@ -122,7 +122,7 @@ public class JsonStructUtils {
try {
Set<String> matchKeys = matchObj.keySet();
for (int sourceIndex = 0; sourceIndex < sourceArray.length(); sourceIndex++) {
JSONObject sourceObj = sourceArray.getJSONObject(sourceIndex);
JSONObject sourceObj = sourceArray.optJSONObject(sourceIndex);
for (String key : matchKeys) {
if (sourceObj.has(key)) {
Object sourceObjItem = sourceObj.get(key);

View File

@ -43,7 +43,7 @@ public class XMLUtil {
if (en == null || en.getValue() == null) continue;
if (en.getValue() instanceof JSONObject) {
buffer.append(tab).append("<").append(en.getKey()).append(">\n");
JSONObject jo = jObj.getJSONObject(en.getKey());
JSONObject jo = jObj.optJSONObject(en.getKey());
jsonToXmlStr(jo, buffer, nowTab.append("\t"));
buffer.append(tab).append("</").append(en.getKey()).append(">\n");
} else if (en.getValue() instanceof JSONArray) {
@ -51,7 +51,7 @@ public class XMLUtil {
for (int i = 0; i < array.length(); i++) {
buffer.append(tab).append("<").append(en.getKey()).append(">\n");
if (StringUtils.isNotBlank(array.optString(i))) {
JSONObject jsonobject = array.getJSONObject(i);
JSONObject jsonobject = array.optJSONObject(i);
jsonToXmlStr(jsonobject, buffer, nowTab.append("\t"));
buffer.append(tab).append("</").append(en.getKey()).append(">\n");
}

View File

@ -46,7 +46,7 @@ public class MockApiUtils {
public static boolean matchRequestHeader(JSONArray mockExpectHeaderArray, Map<String, String> requestHeaderMap) {
Map<String, String> mockExpectHeaders = new HashMap<>();
for (int i = 0; i < mockExpectHeaderArray.length(); i++) {
JSONObject obj = mockExpectHeaderArray.getJSONObject(i);
JSONObject obj = mockExpectHeaderArray.optJSONObject(i);
if (obj.has("name") && obj.has("value")) {
mockExpectHeaders.put(obj.optString("name"), obj.optString("value"));
}
@ -87,7 +87,7 @@ public class MockApiUtils {
String jsonString = "";
if (isJsonSchema) {
if (bodyObj.has("jsonSchema")) {
String bodyRetunStr = bodyObj.getJSONObject("jsonSchema").toString();
String bodyRetunStr = bodyObj.optJSONObject("jsonSchema").toString();
jsonString = JSONSchemaGenerator.getJson(bodyRetunStr);
}
} else {
@ -122,7 +122,7 @@ public class MockApiUtils {
JSONObject bodyParamArr = new JSONObject();
JSONArray kvsArr = bodyObj.getJSONArray("kvs");
for (int i = 0; i < kvsArr.length(); i++) {
JSONObject kv = kvsArr.getJSONObject(i);
JSONObject kv = kvsArr.optJSONObject(i);
if (kv.has("name")) {
String values = kv.optString("value");
if (StringUtils.isEmpty(values)) {
@ -150,7 +150,7 @@ public class MockApiUtils {
public static List<MockConfigRequestParams> getParamsByJSONArray(JSONArray array) {
List<MockConfigRequestParams> requestParamsList = new ArrayList<>();
for (int i = 0; i < array.length(); i++) {
JSONObject obj = array.getJSONObject(i);
JSONObject obj = array.optJSONObject(i);
if (obj.has("name") && obj.has("value")) {
String condition = null;
if (obj.has("rangeType")) {
@ -174,7 +174,7 @@ public class MockApiUtils {
if (respObj != null) {
if (respObj.has("body")) {
String returnStr = "";
JSONObject bodyObj = respObj.getJSONObject("body");
JSONObject bodyObj = respObj.optJSONObject("body");
if (bodyObj.has(PropertyConstant.TYPE)) {
String type = bodyObj.optString(PropertyConstant.TYPE);
if (StringUtils.equals(type, "JSON")) {
@ -187,7 +187,7 @@ public class MockApiUtils {
}
}
if (isJsonSchema && bodyObj.has("jsonSchema")) {
String json = JSONSchemaGenerator.getJson(bodyObj.getJSONObject("jsonSchema").toString());
String json = JSONSchemaGenerator.getJson(bodyObj.optJSONObject("jsonSchema").toString());
if (StringUtils.isNotEmpty(json)) {
returnStr = json;
}
@ -206,7 +206,7 @@ public class MockApiUtils {
if (bodyObj.has("kvs")) {
JSONArray kvsArr = bodyObj.getJSONArray("kvs");
for (int i = 0; i < kvsArr.length(); i++) {
JSONObject kv = kvsArr.getJSONObject(i);
JSONObject kv = kvsArr.optJSONObject(i);
if (kv.has("name")) {
String values = kv.optString("value");
if (StringUtils.isEmpty(values)) {
@ -231,7 +231,7 @@ public class MockApiUtils {
int code = 200;
if (statusCodeArray != null) {
for (int i = 0; i < statusCodeArray.length(); i++) {
JSONObject object = statusCodeArray.getJSONObject(i);
JSONObject object = statusCodeArray.optJSONObject(i);
if (object.has("name") && StringUtils.isNotEmpty(object.optString("name"))) {
try {
code = Integer.parseInt(object.optString("name"));
@ -248,7 +248,7 @@ public class MockApiUtils {
JSONArray jsonArray = respObj.getJSONArray("headers");
Map<String, String> headMap = new HashMap<>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject headObj = jsonArray.getJSONObject(i);
JSONObject headObj = jsonArray.optJSONObject(i);
if (headObj.has("name") && headObj.has("value") && headObj.has("enable")) {
boolean enable = headObj.getBoolean("enable");
if (enable) {
@ -274,7 +274,7 @@ public class MockApiUtils {
if (useScript) {
if (bodyObj.has("scriptObject")) {
try {
JSONObject scriptObj = bodyObj.getJSONObject("scriptObject");
JSONObject scriptObj = bodyObj.optJSONObject("scriptObject");
scriptLanguage = scriptObj.optString("scriptLanguage");
script = scriptObj.optString("script");
} catch (Exception e) {
@ -310,7 +310,7 @@ public class MockApiUtils {
}
}
if (isJsonSchema && bodyObj.has("jsonSchema")) {
String json = JSONSchemaGenerator.getJson(bodyObj.getJSONObject("jsonSchema").toString());
String json = JSONSchemaGenerator.getJson(bodyObj.optJSONObject("jsonSchema").toString());
if (StringUtils.isNotEmpty(json)) {
returnStr = json;
}
@ -645,7 +645,7 @@ public class MockApiUtils {
public static boolean checkParamsCompliance(JSONArray jsonArray, List<MockConfigRequestParams> mockConfigRequestParamList, boolean isAllMatch) {
if (jsonArray != null) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
JSONObject obj = jsonArray.optJSONObject(i);
boolean isMatch = checkParamsCompliance(obj, mockConfigRequestParamList, isAllMatch);
if (isMatch) {
return true;

View File

@ -122,7 +122,7 @@ public class MockScriptEngineUtils {
if (requestMockParams.getBodyParams() != null) {
if (requestMockParams.getBodyParams().length() == 1) {
//参数是jsonObject
JSONObject bodyParamObj = requestMockParams.getBodyParams().getJSONObject(0);
JSONObject bodyParamObj = requestMockParams.getBodyParams().optJSONObject(0);
for (String key : bodyParamObj.keySet()) {
String value = String.valueOf(bodyParamObj.get(key));
value = StringUtils.replace(value, "\\", "\\\\");
@ -177,7 +177,7 @@ public class MockScriptEngineUtils {
if (requestMockParams.getBodyParams() != null) {
if (requestMockParams.getBodyParams().length() == 1) {
//参数是jsonObject
JSONObject bodyParamObj = requestMockParams.getBodyParams().getJSONObject(0);
JSONObject bodyParamObj = requestMockParams.getBodyParams().optJSONObject(0);
for (String key : bodyParamObj.keySet()) {
String value = String.valueOf(bodyParamObj.get(key));
value = StringUtils.replace(value, "\\", "\\\\");

View File

@ -332,12 +332,12 @@ public class MockConfigService {
}
private boolean isRequestMockExpectMatchingByParams(Map<String, String> requestHeaderMap, JSONObject mockExpectRequestObj, RequestMockParams requestMockParams) {
JSONObject expectParamsObj = mockExpectRequestObj.getJSONObject("params");
JSONObject expectParamsObj = mockExpectRequestObj.optJSONObject("params");
if (expectParamsObj.has("headers")) {
//检测headers
JSONArray headerArr = expectParamsObj.getJSONArray("headers");
for (int i = 0; i < headerArr.length(); i++) {
JSONObject jsonObject = headerArr.getJSONObject(i);
JSONObject jsonObject = headerArr.optJSONObject(i);
if (jsonObject.has("name") && jsonObject.has("value")) {
String headerName = jsonObject.optString("name");
String headerValue = jsonObject.optString("value");
@ -351,7 +351,7 @@ public class MockConfigService {
}
if (expectParamsObj.has("body")) {
JSONObject expectBodyObject = expectParamsObj.getJSONObject("body");
JSONObject expectBodyObject = expectParamsObj.optJSONObject("body");
JSONArray jsonArray = requestMockParams.getBodyParams();
String type = expectBodyObject.optString(PropertyConstant.TYPE);
String paramsFilterType = "And";
@ -416,7 +416,7 @@ public class MockConfigService {
if (StringUtils.equalsIgnoreCase("Array", jsonValidator.getType().name())) {
JSONArray mockExpectArr = JSONUtil.parseArray(jsonParams);
for (int expectIndex = 0; expectIndex < mockExpectArr.length(); expectIndex++) {
JSONObject itemObj = mockExpectArr.getJSONObject(expectIndex);
JSONObject itemObj = mockExpectArr.optJSONObject(expectIndex);
mockExpectJson = itemObj;
}
} else if (StringUtils.equalsIgnoreCase("Object", jsonValidator.getType().name())) {
@ -426,7 +426,7 @@ public class MockConfigService {
} else {
JSONArray jsonArray = mockExpectRequestObj.getJSONArray("variables");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
JSONObject object = jsonArray.optJSONObject(i);
String name = "";
String value = "";
if (object.has("name")) {
@ -453,7 +453,7 @@ public class MockConfigService {
}
if (requestMockParams.getBodyParams() != null) {
for (int i = 0; i < requestMockParams.getBodyParams().length(); i++) {
JSONObject reqJsonObj = requestMockParams.getBodyParams().getJSONObject(i);
JSONObject reqJsonObj = requestMockParams.getBodyParams().optJSONObject(i);
matchBody = JsonStructUtils.checkJsonObjCompliance(reqJsonObj, mockExpectJson);
if (matchBody) {
break;
@ -468,7 +468,7 @@ public class MockConfigService {
private MockExpectConfigResponse getEmptyRequestMockExpectByParams(Map<String, String> requestHeaderMap, MockExpectConfigResponse model) {
JSONObject requestObj = JSONUtil.parseObject(JSON.toJSONString(model.getRequest()));
if (requestObj.has("params")) {
JSONObject paramsObj = requestObj.getJSONObject("params");
JSONObject paramsObj = requestObj.optJSONObject("params");
if (paramsObj.has("headers")) {
JSONArray headArray = paramsObj.getJSONArray("headers");
boolean isHeadMatch = MockApiUtils.matchRequestHeader(headArray, requestHeaderMap);
@ -479,7 +479,7 @@ public class MockConfigService {
if (paramsObj.has("rest")) {
JSONArray restArray = paramsObj.getJSONArray("rest");
for (int i = 0; i < restArray.length(); i++) {
JSONObject restObj = restArray.getJSONObject(i);
JSONObject restObj = restArray.optJSONObject(i);
if (restObj.has("name") && restObj.has("value")) {
return null;
}
@ -489,7 +489,7 @@ public class MockConfigService {
if (paramsObj.has("arguments")) {
JSONArray argumentsArray = paramsObj.getJSONArray("arguments");
for (int i = 0; i < argumentsArray.length(); i++) {
JSONObject argumentsObj = argumentsArray.getJSONObject(i);
JSONObject argumentsObj = argumentsArray.optJSONObject(i);
if (argumentsObj.has("name") && argumentsObj.has("value")) {
return null;
}
@ -497,7 +497,7 @@ public class MockConfigService {
}
//判断请求体为空
if (paramsObj.has("body")) {
JSONObject bodyObj = paramsObj.getJSONObject("body");
JSONObject bodyObj = paramsObj.optJSONObject("body");
if (bodyObj.has(PropertyConstant.TYPE)) {
String type = bodyObj.optString(PropertyConstant.TYPE);
if (StringUtils.equalsIgnoreCase(type, "json")) {
@ -515,7 +515,7 @@ public class MockConfigService {
if (bodyObj.has("kvs")) {
JSONArray kvsArray = bodyObj.getJSONArray("kvs");
for (int i = 0; i < kvsArray.length(); i++) {
JSONObject kvsObj = kvsArray.getJSONObject(i);
JSONObject kvsObj = kvsArray.optJSONObject(i);
if (kvsObj.has("name") && kvsObj.has("value")) {
return null;
}
@ -549,7 +549,7 @@ public class MockConfigService {
JSONObject mockExpectJson = new JSONObject();
JSONArray jsonArray = requestObj.getJSONArray("variables");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
JSONObject object = jsonArray.optJSONObject(i);
String name = "";
String value = "";
if (object.has("name")) {
@ -581,11 +581,11 @@ public class MockConfigService {
response.setStatus(Integer.parseInt(httpCode));
long sleepTime = 0;
if (responseObj.has("responseResult")) {
JSONObject responseJsonObj = responseObj.getJSONObject("responseResult");
JSONObject responseJsonObj = responseObj.optJSONObject("responseResult");
if (responseJsonObj.has("headers")) {
JSONArray jsonArray = responseJsonObj.getJSONArray("headers");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
JSONObject object = jsonArray.optJSONObject(i);
if (object.has("name") && object.has("value")) {
String name = String.valueOf(object.get("name")).trim();
String value = String.valueOf(object.get("value")).trim();
@ -601,7 +601,7 @@ public class MockConfigService {
if (responseJsonObj.has("usePostScript")) {
useScript = responseJsonObj.getBoolean("usePostScript");
}
returnStr = mockApiUtils.getResultByResponseResult(projectId, responseJsonObj.getJSONObject("body"), url, headerMap, requestMockParams, useScript);
returnStr = mockApiUtils.getResultByResponseResult(projectId, responseJsonObj.optJSONObject("body"), url, headerMap, requestMockParams, useScript);
}
if (responseJsonObj.has("httpCode")) {
int httpCodeNum = 500;
@ -620,7 +620,7 @@ public class MockConfigService {
} else {
JSONArray jsonArray = responseObj.getJSONArray("httpHeads");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
JSONObject object = jsonArray.optJSONObject(i);
String name = null;
String value = "";
if (object.has("name")) {
@ -749,7 +749,7 @@ public class MockConfigService {
JSONArray headArr = requestObj.getJSONArray("arguments");
for (int index = 0; index < headArr.length(); index++) {
JSONObject headObj = headArr.getJSONObject(index);
JSONObject headObj = headArr.optJSONObject(index);
if (headObj.has("name") && !queryParamList.contains(headObj.has("name"))) {
queryParamList.add(String.valueOf(headObj.get("name")));
}
@ -761,7 +761,7 @@ public class MockConfigService {
try {
JSONArray headArr = requestObj.getJSONArray("rest");
for (int index = 0; index < headArr.length(); index++) {
JSONObject headObj = headArr.getJSONObject(index);
JSONObject headObj = headArr.optJSONObject(index);
if (headObj.has("name") && !restParamList.contains(headObj.has("name"))) {
restParamList.add(String.valueOf(headObj.get("name")));
}
@ -772,7 +772,7 @@ public class MockConfigService {
//请求体参数类型
if (requestObj.has("body")) {
try {
JSONObject bodyObj = requestObj.getJSONObject("body");
JSONObject bodyObj = requestObj.optJSONObject("body");
if (bodyObj.has(PropertyConstant.TYPE)) {
String type = bodyObj.optString(PropertyConstant.TYPE);
@ -780,7 +780,7 @@ public class MockConfigService {
if (bodyObj.has("kvs")) {
JSONArray kvsArr = bodyObj.getJSONArray("kvs");
for (int i = 0; i < kvsArr.length(); i++) {
JSONObject kv = kvsArr.getJSONObject(i);
JSONObject kv = kvsArr.optJSONObject(i);
if (kv.has("name") && !formDataList.contains(kv.has("name"))) {
formDataList.add(String.valueOf(kv.get("name")));
}
@ -1242,9 +1242,9 @@ public class MockConfigService {
try {
JSONObject responseObj = JSONUtil.parseObject(model.getResponse());
if (responseObj.has("responseResult")) {
JSONObject responseResultObject = responseObj.getJSONObject("responseResult");
JSONObject responseResultObject = responseObj.optJSONObject("responseResult");
if (responseResultObject.has("body")) {
responseResultObject.getJSONObject("body").put("apiRspRaw", responseDTO.getReturnData());
responseResultObject.optJSONObject("body").put("apiRspRaw", responseDTO.getReturnData());
model.setResponse(responseObj.toString());
mockExpectConfigMapper.updateByPrimaryKeySelective(model);

View File

@ -214,7 +214,7 @@ public class MsHashTreeService {
public void dataFormatting(JSONArray hashTree) {
for (int i = 0; i < hashTree.length(); i++) {
JSONObject element = hashTree.getJSONObject(i);
JSONObject element = hashTree.optJSONObject(i);
if (element != null && StringUtils.equalsIgnoreCase(element.optString(TYPE), SCENARIO)) {
element = this.setRefScenario(element);
hashTree.put(i, element);

View File

@ -113,7 +113,7 @@ public class ApiScenarioReferenceIdService {
}
JSONArray hashTree = jsonObject.getJSONArray(MsHashTreeService.HASH_TREE);
for (int index = 0; index < hashTree.length(); index++) {
JSONObject item = hashTree.getJSONObject(index);
JSONObject item = hashTree.optJSONObject(index);
if (item == null) {
continue;
}
@ -166,7 +166,7 @@ public class ApiScenarioReferenceIdService {
List<ApiScenarioReferenceId> deepRelations = new LinkedList<>();
if (hashTree != null) {
for (int index = 0; index < hashTree.length(); index++) {
JSONObject item = hashTree.getJSONObject(index);
JSONObject item = hashTree.optJSONObject(index);
if (item.has(MsHashTreeService.ID) && item.has(MsHashTreeService.REFERENCED)) {
String method = null;
String url = null;

View File

@ -197,7 +197,7 @@ public class ApiScenarioReportStructureService {
public static void dataFormatting(JSONArray hashTree, StepTreeDTO dto, String id, String reportType) {
for (int i = 0; i < hashTree.length(); i++) {
JSONObject element = hashTree.getJSONObject(i);
JSONObject element = hashTree.optJSONObject(i);
if (element != null && element.getBoolean(ENABLE)) {
String resourceId = combinationResourceId(element, reportType, id);
StepTreeDTO children = new StepTreeDTO(element.optString(NAME), resourceId, element.optString(TYPE), resourceId, element.optInt("index"));

View File

@ -735,7 +735,7 @@ public class ApiScenarioService {
} else {
if (value instanceof JSONObject) {
JSONObject valueJson = (JSONObject) value;
JSONObject targetValue = jsonMerge(valueJson, target.getJSONObject(key));
JSONObject targetValue = jsonMerge(valueJson, target.optJSONObject(key));
target.put(key, targetValue);
} else if (value instanceof JSONArray) {
JSONArray valueArray = (JSONArray) value;
@ -2104,7 +2104,7 @@ public class ApiScenarioService {
}
JSONObject environmentMap = null;
if (object.has("environmentMap")) {
environmentMap = object.getJSONObject("environmentMap");
environmentMap = object.optJSONObject("environmentMap");
}
if (environmentMap != null) {
object.put("environmentMap", new HashMap<>());

View File

@ -5,7 +5,7 @@
<fieldset :disabled="disabled" class="ms-fieldset">
<el-collapse-transition>
<el-tabs v-model="activeName">
<el-tabs v-model="activeName" @tab-click="reloadTable">
<el-tab-pane :label="$t('api_test.scenario.variables')" name="variable">
<div>
<el-row style="margin-bottom: 10px">
@ -255,6 +255,11 @@ export default {
};
},
methods: {
reloadTable() {
if (this.$refs.variableTable) {
this.$refs.variableTable.reloadTable();
}
},
importVariable() {
this.$refs.variableImport.open();
},