fix(接口测试): mock响应中后置脚本加载jar包时只加载访问的mock接口所属项目下的

mock响应中后置脚本加载jar包时只加载访问的mock接口所属项目下的
This commit is contained in:
song-tianyang 2022-05-19 19:38:05 +08:00 committed by TIanyang
parent 6424e71db6
commit 4530341ab0
6 changed files with 62 additions and 39 deletions

View File

@ -0,0 +1,12 @@
package io.metersphere.api.dto.mock;
import io.metersphere.base.domain.MockExpectConfigWithBLOBs;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class MockExpectConfigDTO{
private String projectId;
private MockExpectConfigWithBLOBs mockExpectConfig;
}

View File

@ -146,7 +146,7 @@ public class MockApiUtils {
return returnJson;
}
public static JSONObject parseJsonSchema(JSONObject bodyReturnObj,boolean useJMeterFunc) {
public static JSONObject parseJsonSchema(JSONObject bodyReturnObj, boolean useJMeterFunc) {
JSONObject returnObj = new JSONObject();
if (bodyReturnObj == null) {
return returnObj;
@ -157,7 +157,7 @@ public class MockApiUtils {
try {
JsonSchemaReturnObj obj = bodyReturnObj.getObject(key, JsonSchemaReturnObj.class);
if (StringUtils.equals("object", obj.getType())) {
JSONObject itemObj = parseJsonSchema(obj.getProperties(),useJMeterFunc);
JSONObject itemObj = parseJsonSchema(obj.getProperties(), useJMeterFunc);
if (!itemObj.isEmpty()) {
returnObj.put(key, itemObj);
}
@ -167,14 +167,14 @@ public class MockApiUtils {
if (itemObj.containsKey("type")) {
if (StringUtils.equals("object", itemObj.getString("type")) && itemObj.containsKey("properties")) {
JSONObject arrayObj = itemObj.getJSONObject("properties");
JSONObject parseObj = parseJsonSchema(arrayObj,useJMeterFunc);
JSONObject parseObj = parseJsonSchema(arrayObj, useJMeterFunc);
JSONArray array = new JSONArray();
array.add(parseObj);
returnObj.put(key, array);
} else if (StringUtils.equals("string", itemObj.getString("type")) && itemObj.containsKey("mock")) {
JsonSchemaReturnObj arrayObj = JSONObject.toJavaObject(itemObj, JsonSchemaReturnObj.class);
String value = arrayObj.getMockValue();
if(useJMeterFunc){
if (useJMeterFunc) {
value = getMockValues(arrayObj.getMockValue());
}
JSONArray array = new JSONArray();
@ -187,7 +187,7 @@ public class MockApiUtils {
String values = obj.getMockValue();
if (StringUtils.isEmpty(values)) {
values = "";
} else if(useJMeterFunc){
} else if (useJMeterFunc) {
try {
values = values.startsWith("@") ? ScriptEngineUtils.buildFunctionCallString(values) : values;
} catch (Exception e) {
@ -257,7 +257,7 @@ public class MockApiUtils {
if (bodyObj.containsKey("jsonSchema") && bodyObj.getJSONObject("jsonSchema").containsKey("properties")) {
String bodyRetunStr = bodyObj.getJSONObject("jsonSchema").getJSONObject("properties").toJSONString();
JSONObject bodyReturnObj = JSONObject.parseObject(bodyRetunStr);
JSONObject returnObj = MockApiUtils.parseJsonSchema(bodyReturnObj,false);
JSONObject returnObj = MockApiUtils.parseJsonSchema(bodyReturnObj, false);
returnStr = returnObj.toJSONString();
}
} else {
@ -336,7 +336,7 @@ public class MockApiUtils {
return responseDTO;
}
public String getResultByResponseResult(JSONObject bodyObj, String url, Map<String, String> headerMap, RequestMockParams requestMockParams, boolean useScript) {
public String getResultByResponseResult(String projectId, JSONObject bodyObj, String url, Map<String, String> headerMap, RequestMockParams requestMockParams, boolean useScript) {
MockScriptEngineUtils scriptEngineUtils = new MockScriptEngineUtils();
ScriptEngine scriptEngine = null;
String scriptLanguage = "beanshell";
@ -351,10 +351,10 @@ public class MockApiUtils {
LogUtil.error(e);
}
}
}
scriptEngine = scriptEngineUtils.getBaseScriptEngine(scriptLanguage, url, headerMap, requestMockParams);
if (StringUtils.isNotEmpty(script) && scriptEngine != null) {
scriptEngineUtils.runScript(scriptEngine, script);
scriptEngine = scriptEngineUtils.getBaseScriptEngine(projectId, scriptLanguage, url, headerMap, requestMockParams);
if (StringUtils.isNotEmpty(script) && scriptEngine != null) {
scriptEngineUtils.runScript(scriptEngine, script);
}
}
if (headerMap == null) {
@ -382,7 +382,7 @@ public class MockApiUtils {
if (bodyObj.containsKey("jsonSchema") && bodyObj.getJSONObject("jsonSchema").containsKey("properties")) {
String bodyRetunStr = bodyObj.getJSONObject("jsonSchema").getJSONObject("properties").toJSONString();
JSONObject bodyReturnObj = JSONObject.parseObject(bodyRetunStr);
JSONObject returnObj = MockApiUtils.parseJsonSchema(bodyReturnObj,false);
JSONObject returnObj = MockApiUtils.parseJsonSchema(bodyReturnObj, false);
returnStr = returnObj.toJSONString();
}
} else {
@ -415,7 +415,9 @@ public class MockApiUtils {
}
}
}
returnStr = scriptEngineUtils.parseReportString(scriptEngine, returnStr);
if (scriptEngine != null) {
returnStr = scriptEngineUtils.parseReportString(scriptEngine, returnStr);
}
return returnStr;
}
}
@ -773,12 +775,12 @@ public class MockApiUtils {
}
}
public static boolean isUrlInList(String url,List<String> urlList){
if(CollectionUtils.isEmpty(urlList)){
public static boolean isUrlInList(String url, List<String> urlList) {
if (CollectionUtils.isEmpty(urlList)) {
return false;
}
String urlSuffix = url;
if(urlSuffix.startsWith("/")){
if (urlSuffix.startsWith("/")) {
urlSuffix = urlSuffix.substring(1);
}
String[] urlParams = urlSuffix.split("/");

View File

@ -63,10 +63,10 @@ public class MockScriptEngineUtils {
/**
* 加载jar包
*/
private void loadJars() {
private void loadJars(String projectId) {
JarConfigService jarConfigService = CommonBeanFactory.getBean(JarConfigService.class);
if (jarConfigService != null) {
List<JarConfig> jars = jarConfigService.list();
List<JarConfig> jars = jarConfigService.listByProjectId(projectId);
jars.forEach(jarConfig -> {
try {
this.loadJar(jarConfig.getPath());
@ -78,7 +78,7 @@ public class MockScriptEngineUtils {
}
}
public ScriptEngine getBaseScriptEngine(String scriptLanguage, String url, Map<String, String> headerMap, RequestMockParams requestMockParams) {
public ScriptEngine getBaseScriptEngine(String projectId, String scriptLanguage, String url, Map<String, String> headerMap, RequestMockParams requestMockParams) {
ScriptEngine engine = null;
try {
if (StringUtils.isEmpty(scriptLanguage)) {
@ -94,7 +94,7 @@ public class MockScriptEngineUtils {
engine = scriptEngineFactory.getEngineByName(scriptLanguage);
preScript = this.genPythonPreScript(url, headerMap, requestMockParams);
}
this.loadJars();
this.loadJars(projectId);
engine.eval(preScript);
} catch (Exception e) {
LogUtil.error(e);
@ -250,7 +250,7 @@ public class MockScriptEngineUtils {
LogUtil.error(e);
}
}
if (StringUtils.isEmpty(value) || StringUtils.equals(value,"null")) {
if (StringUtils.isEmpty(value) || StringUtils.equals(value, "null")) {
value = paramKey;
}
return value;

View File

@ -9,10 +9,7 @@ import io.metersphere.api.dto.automation.EsbDataStruct;
import io.metersphere.api.dto.automation.TcpTreeTableDataStruct;
import io.metersphere.api.dto.automation.parse.TcpTreeTableDataParser;
import io.metersphere.api.dto.definition.parse.ApiDefinitionImport;
import io.metersphere.api.dto.mock.ApiDefinitionResponseDTO;
import io.metersphere.api.dto.mock.MockConfigRequestParams;
import io.metersphere.api.dto.mock.MockParamSuggestions;
import io.metersphere.api.dto.mock.RequestMockParams;
import io.metersphere.api.dto.mock.*;
import io.metersphere.api.dto.mockconfig.MockConfigImportDTO;
import io.metersphere.api.dto.mockconfig.MockConfigRequest;
import io.metersphere.api.dto.mockconfig.MockExpectConfigRequest;
@ -704,7 +701,7 @@ public class MockConfigService {
return returnModel;
}
public String updateHttpServletResponse(MockExpectConfigResponse finalExpectConfig, String url, Map<String, String> headerMap, RequestMockParams requestMockParams, HttpServletResponse response) {
public String updateHttpServletResponse(String projectId,MockExpectConfigResponse finalExpectConfig, String url, Map<String, String> headerMap, RequestMockParams requestMockParams, HttpServletResponse response) {
String returnStr = "";
try {
//设置响应头和响应码
@ -736,7 +733,7 @@ public class MockConfigService {
if (responseJsonObj.containsKey("usePostScript")) {
useScript = responseJsonObj.getBoolean("usePostScript");
}
returnStr = mockApiUtils.getResultByResponseResult(responseJsonObj.getJSONObject("body"), url, headerMap, requestMockParams, useScript);
returnStr = mockApiUtils.getResultByResponseResult(projectId,responseJsonObj.getJSONObject("body"), url, headerMap, requestMockParams, useScript);
}
if (responseJsonObj.containsKey("httpCode")) {
int httpCodeNum = 500;
@ -1002,7 +999,7 @@ public class MockConfigService {
MockConfigResponse mockConfigData = this.findByApiId(api.getId());
MockExpectConfigResponse finalExpectConfig = this.findExpectConfig(requestHeaderMap, mockConfigData.getMockExpectConfigList(), mockParams);
if (finalExpectConfig != null) {
returnStr = this.updateHttpServletResponse(finalExpectConfig, url, requestHeaderMap, mockParams, response);
returnStr = this.updateHttpServletResponse(project.getId(),finalExpectConfig, url, requestHeaderMap, mockParams, response);
}else {
returnStr = this.getApiDefinitionResponse(api, response);
}
@ -1047,7 +1044,7 @@ public class MockConfigService {
if (mockConfigData != null && mockConfigData.getMockExpectConfigList() != null) {
MockExpectConfigResponse finalExpectConfig = this.findExpectConfig(requestHeaderMap, mockConfigData.getMockExpectConfigList(), paramMap);
if (finalExpectConfig != null) {
returnStr = this.updateHttpServletResponse(finalExpectConfig, url, requestHeaderMap, paramMap, response);
returnStr = this.updateHttpServletResponse(project.getId(),finalExpectConfig, url, requestHeaderMap, paramMap, response);
}else {
returnStr = this.getApiDefinitionResponse(api, response);
}
@ -1131,7 +1128,7 @@ public class MockConfigService {
return returnList;
}
public MockExpectConfigWithBLOBs matchTcpMockExpect(String message, int port) {
public MockExpectConfigDTO matchTcpMockExpect(String message, int port) {
ProjectApplicationExample pae = new ProjectApplicationExample();
pae.createCriteria().andTypeEqualTo(ProjectApplicationType.MOCK_TCP_OPEN.name())
.andTypeValueEqualTo(String.valueOf(true));
@ -1149,8 +1146,8 @@ public class MockConfigService {
boolean isJsonMessage = this.checkMessageIsJson(message);
boolean isXMLMessage = this.checkMessageIsXml(message);
List<MockExpectConfigWithBLOBs> structResult = new ArrayList<>();
List<MockExpectConfigWithBLOBs> rawResult = new ArrayList<>();
List<MockExpectConfigDTO> structResult = new ArrayList<>();
List<MockExpectConfigDTO> rawResult = new ArrayList<>();
for (Project project : projectList) {
String projectId = project.getId();
@ -1204,9 +1201,15 @@ public class MockConfigService {
JSONObject responseObj = JSONObject.parseObject(responseStr);
if (responseObj.containsKey("body")) {
if (isRaw) {
rawResult.add(expectConfig);
MockExpectConfigDTO dto = new MockExpectConfigDTO();
dto.setMockExpectConfig(expectConfig);
dto.setProjectId(projectId);
rawResult.add(dto);
} else {
structResult.add(expectConfig);
MockExpectConfigDTO dto = new MockExpectConfigDTO();
dto.setMockExpectConfig(expectConfig);
dto.setProjectId(projectId);
structResult.add(dto);
}
}
}

View File

@ -1,9 +1,9 @@
package io.metersphere.api.tcp.server;
import com.alibaba.fastjson.JSONObject;
import io.metersphere.api.dto.mock.MockExpectConfigDTO;
import io.metersphere.api.mock.utils.MockApiUtils;
import io.metersphere.api.service.MockConfigService;
import io.metersphere.base.domain.MockExpectConfigWithBLOBs;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.LogUtil;
@ -43,10 +43,10 @@ public class TCPServicer {
private String getReturnMsg(String message) {
LogUtil.info("TCP-Mock start. port: " + this.port + "; Message:" + message);
MockConfigService mockConfigService = CommonBeanFactory.getBean(MockConfigService.class);
MockExpectConfigWithBLOBs matchdMockExpect = mockConfigService.matchTcpMockExpect(message, this.port);
MockExpectConfigDTO matchdMockExpectDTO = mockConfigService.matchTcpMockExpect(message, this.port);
String returnMsg = "";
if (matchdMockExpect != null) {
String response = matchdMockExpect.getResponse();
if (matchdMockExpectDTO != null && matchdMockExpectDTO.getMockExpectConfig() != null) {
String response = matchdMockExpectDTO.getMockExpectConfig().getResponse();
JSONObject responseObj = JSONObject.parseObject(response);
int delayed = 0;
try {
@ -64,7 +64,7 @@ public class TCPServicer {
if (respResultObj.containsKey("usePostScript")) {
useScript = respResultObj.getBoolean("usePostScript");
}
returnMsg = mockApiUtils.getResultByResponseResult(respResultObj.getJSONObject("body"), "", null, null, useScript);
returnMsg = mockApiUtils.getResultByResponseResult(matchdMockExpectDTO.getProjectId(), respResultObj.getJSONObject("body"), "", null, null, useScript);
}
try {
if (respResultObj.containsKey("delayed")) {

View File

@ -49,6 +49,12 @@ public class JarConfigService {
return jarConfigMapper.selectByExample(example);
}
public List<JarConfig> listByProjectId(String projectId) {
JarConfigExample example = new JarConfigExample();
example.createCriteria().andResourceTypeEqualTo("PROJECT").andResourceIdEqualTo(projectId);
return jarConfigMapper.selectByExample(example);
}
public Pager<List<JarConfig>> list(JarConfigRequest request, int pageNum, int pageSize) {
buildQueryRequest(request);
Page<Object> page = PageHelper.startPage(pageNum, pageSize, true);