fix(接口测试): 上传多个接口插件,只有最后上传的插件接口能保存成功

This commit is contained in:
AgAngle 2024-02-18 16:29:50 +08:00 committed by Craftsman
parent e840de394c
commit 34bf2e035d
10 changed files with 28 additions and 36 deletions

View File

@ -14,6 +14,9 @@ import io.metersphere.system.service.PluginLoadService;
import org.pf4j.Plugin;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: jianxing
* @CreateTime: 2024-02-06 20:55
@ -26,9 +29,11 @@ public class ApiPluginChangeService implements PluginChangeService {
Plugin plugin = msPluginManager.getPlugin(pluginId).getPlugin();
try {
if (plugin instanceof AbstractApiPlugin) {
// 注册序列化类
msPluginManager.getExtensionClasses(MsTestElement.class, pluginId)
.forEach(ApiDataUtils::setResolver);
// 注册序列化类获取最新的实现类重新注册
List<Class<? extends MsTestElement>> extensionClasses = msPluginManager.getExtensionClasses(MsTestElement.class);
List<Class<?>> clazzList = new ArrayList<>(extensionClasses.size());
extensionClasses.forEach(clazzList::add);
ApiDataUtils.setResolvers(clazzList);
// 注册插件元素解析器
msPluginManager.getExtensionClasses(JmeterElementConverter.class, pluginId)

View File

@ -187,7 +187,7 @@ public class ApiDebugService {
}
private ApiDebug checkResourceExist(String id) {
return ServiceUtils.checkResourceExist(apiDebugMapper.selectByPrimaryKey(id), "permission.system_api_debug.name");
return ServiceUtils.checkResourceExist(apiDebugMapper.selectByPrimaryKey(id), "permission.api_debug.name");
}
public String uploadTempFile(MultipartFile file) {

View File

@ -119,4 +119,13 @@ public class ApiDataUtils {
// 加入新的动态组件
objectMapper.registerSubtypes(clazz);
}
/**
* 设置动态加载的jar的Resolver
*/
public static void setResolvers(List<Class<?>> clazzList) {
setResolver(new LinkedList<>());
// 加入新的动态组件
clazzList.forEach(objectMapper::registerSubtypes);
}
}

View File

@ -268,7 +268,6 @@ public class ApiDebugControllerTests extends BaseTest {
ApiDebug copyApiDebug = BeanUtils.copyBean(new ApiDebug(), apiDebug);
copyApiDebug = BeanUtils.copyBean(copyApiDebug, request);
Assertions.assertEquals(apiDebug, copyApiDebug);
ApiDataUtils.setResolver(MsHTTPElement.class);
Assertions.assertEquals(msHttpElement, ApiDataUtils.parseObject(new String(apiDebugBlob.getRequest()), AbstractMsTestElement.class));
return apiDebug;
}
@ -343,7 +342,6 @@ public class ApiDebugControllerTests extends BaseTest {
// @@请求成功
MvcResult mvcResult = this.requestGetWithOk(DEFAULT_GET, addApiDebug.getId())
.andReturn();
ApiDataUtils.setResolver(MsHTTPElement.class);
ApiDebugDTO apiDebugDTO = ApiDataUtils.parseObject(JSON.toJSONString(parseResponse(mvcResult).get("data")), ApiDebugDTO.class);
// 校验数据是否正确
ApiDebugDTO copyApiDebugDTO = BeanUtils.copyBean(new ApiDebugDTO(), apiDebugMapper.selectByPrimaryKey(addApiDebug.getId()));

View File

@ -282,7 +282,6 @@ public class ApiDefinitionControllerTests extends BaseTest {
ApiDefinition copyApiDefinition = BeanUtils.copyBean(new ApiDefinition(), apiDefinition);
BeanUtils.copyBean(copyApiDefinition, request);
Assertions.assertEquals(apiDefinition, copyApiDefinition);
ApiDataUtils.setResolver(MsHTTPElement.class);
if (apiDefinitionBlob != null) {
Assertions.assertEquals(msHttpElement, ApiDataUtils.parseObject(new String(apiDefinitionBlob.getRequest()), AbstractMsTestElement.class));
}
@ -298,7 +297,6 @@ public class ApiDefinitionControllerTests extends BaseTest {
}
// @@请求成功
MvcResult mvcResult = this.requestGetWithOkAndReturn(GET + apiDefinition.getId());
ApiDataUtils.setResolver(MsHTTPElement.class);
ApiDefinitionDTO apiDefinitionDTO = ApiDataUtils.parseObject(JSON.toJSONString(parseResponse(mvcResult).get("data")), ApiDefinitionDTO.class);
// 校验数据是否正确
ApiDefinitionDTO copyApiDefinitionDTO = BeanUtils.copyBean(new ApiDefinitionDTO(), apiDefinition);
@ -750,7 +748,6 @@ public class ApiDefinitionControllerTests extends BaseTest {
ApiDefinition apiDefinition = apiDefinitionMapper.selectByPrimaryKey("1001");
// @@请求成功
MvcResult mvcResult = this.requestGetWithOk(VERSION + apiDefinition.getId()).andReturn();
ApiDataUtils.setResolver(MsHTTPElement.class);
List<ApiDefinitionVersionDTO> apiDefinitionVersionDTO = getResultDataArray(mvcResult, ApiDefinitionVersionDTO.class);
// 校验数据是否正确
List<ApiDefinitionVersionDTO> copyApiDefinitionVersionDTO = extApiDefinitionMapper.getApiDefinitionByRefId(apiDefinition.getRefId());
@ -885,7 +882,6 @@ public class ApiDefinitionControllerTests extends BaseTest {
request.setType(ApiDefinitionDocType.API.name());
// @@请求成功
MvcResult mvcResult = this.requestPostWithOkAndReturn(DOC, request);
ApiDataUtils.setResolver(MsHTTPElement.class);
ApiDefinitionDocDTO apiDefinitionDocDTO = ApiDataUtils.parseObject(JSON.toJSONString(parseResponse(mvcResult).get("data")), ApiDefinitionDocDTO.class);
// 校验数据是否正确
ApiDefinitionDocDTO copyApiDefinitionDocDTO = new ApiDefinitionDocDTO();
@ -911,7 +907,6 @@ public class ApiDefinitionControllerTests extends BaseTest {
request.setType(ApiDefinitionDocType.MODULE.name());
request.setModuleIds(List.of("10001"));
MvcResult mvcResultModule = this.requestPostWithOkAndReturn(DOC, request);
ApiDataUtils.setResolver(MsHTTPElement.class);
ApiDefinitionDocDTO moduleApiDefinitionDocDTO = ApiDataUtils.parseObject(JSON.toJSONString(parseResponse(mvcResultModule).get("data")), ApiDefinitionDocDTO.class);
// 校验数据是否正确
ApiDefinitionDocDTO copyModuleApiDefinitionDocDTO = new ApiDefinitionDocDTO();
@ -943,7 +938,6 @@ public class ApiDefinitionControllerTests extends BaseTest {
request.setProjectId(DEFAULT_PROJECT_ID);
request.setType(ApiDefinitionDocType.ALL.name());
MvcResult mvcResultAll = this.requestPostWithOkAndReturn(DOC, request);
ApiDataUtils.setResolver(MsHTTPElement.class);
ApiDefinitionDocDTO allApiDefinitionDocDTO = ApiDataUtils.parseObject(JSON.toJSONString(parseResponse(mvcResultAll).get("data")), ApiDefinitionDocDTO.class);
// 校验数据是否正确
ApiDefinitionDocDTO copyAllApiDefinitionDocDTO = new ApiDefinitionDocDTO();
@ -980,7 +974,6 @@ public class ApiDefinitionControllerTests extends BaseTest {
request.setType(ApiDefinitionDocType.MODULE.name());
request.setModuleIds(List.of("1001001"));
MvcResult mvcResultModule = this.requestPostWithOkAndReturn(DOC, request);
ApiDataUtils.setResolver(MsHTTPElement.class);
ApiDefinitionDocDTO moduleApiDefinitionDocDTO = ApiDataUtils.parseObject(JSON.toJSONString(parseResponse(mvcResultModule).get("data")), ApiDefinitionDocDTO.class);
// 校验数据是否正确
ApiDefinitionDocDTO copyModuleApiDefinitionDocDTO = new ApiDefinitionDocDTO();
@ -1131,7 +1124,6 @@ public class ApiDefinitionControllerTests extends BaseTest {
apiDefinitionDeleteRequest.setId("1004");
ApiDefinition delApiDefinition = apiDefinitionMapper.selectByPrimaryKey(apiDefinitionDeleteRequest.getId());
MvcResult mvcResult = this.requestGetWithOk(VERSION + apiDefinitionDeleteRequest.getId()).andReturn();
ApiDataUtils.setResolver(MsHTTPElement.class);
List<ApiDefinitionVersionDTO> apiDefinitionVersionDTO = getResultDataArray(mvcResult, ApiDefinitionVersionDTO.class);
if (!apiDefinitionVersionDTO.isEmpty()) {
this.requestPostWithOkAndReturn(DELETE, apiDefinitionDeleteRequest);

View File

@ -241,7 +241,6 @@ public class ApiDefinitionMockControllerTests extends BaseTest {
ApiDefinitionMock copyApiDefinitionMock = BeanUtils.copyBean(new ApiDefinitionMock(), apiDefinitionMock);
BeanUtils.copyBean(copyApiDefinitionMock, request);
Assertions.assertEquals(apiDefinitionMock, copyApiDefinitionMock);
ApiDataUtils.setResolver(MsHTTPElement.class);
if(apiDefinitionMockConfig != null){
Assertions.assertEquals(msHttpElement, ApiDataUtils.parseObject(new String(apiDefinitionMockConfig.getMatching()), AbstractMsTestElement.class));
}
@ -257,7 +256,6 @@ public class ApiDefinitionMockControllerTests extends BaseTest {
apiDefinitionMockRequest.setApiDefinitionId(apiDefinitionMock.getApiDefinitionId());
// @@请求成功
MvcResult mvcResult = this.requestPostWithOkAndReturn(DETAIL, apiDefinitionMockRequest);
ApiDataUtils.setResolver(MsHTTPElement.class);
ApiDefinitionMockDTO apiDefinitionMockDTO = ApiDataUtils.parseObject(JSON.toJSONString(parseResponse(mvcResult).get("data")), ApiDefinitionMockDTO.class);
// 校验数据是否正确
ApiDefinitionMockDTO copyApiDefinitionMockDTO = BeanUtils.copyBean(new ApiDefinitionMockDTO(), apiDefinitionMock);

View File

@ -9,7 +9,6 @@ import io.metersphere.api.dto.definition.ApiDefinitionDTO;
import io.metersphere.api.dto.definition.ApiDefinitionDocDTO;
import io.metersphere.api.dto.definition.ApiDefinitionDocRequest;
import io.metersphere.api.dto.definition.HttpResponse;
import io.metersphere.api.dto.request.http.MsHTTPElement;
import io.metersphere.api.dto.share.ShareInfoDTO;
import io.metersphere.api.mapper.ApiDefinitionBlobMapper;
import io.metersphere.api.mapper.ApiDefinitionMapper;
@ -75,7 +74,6 @@ public class ApiShareControllerTests extends BaseTest {
// @@请求成功
this.requestPostWithOkAndReturn(SHARE_DOC, request);
MvcResult mvcResult = this.requestPostWithOkAndReturn(SHARE_DOC, request);
ApiDataUtils.setResolver(MsHTTPElement.class);
ShareInfoDTO shareInfoDTO = ApiDataUtils.parseObject(JSON.toJSONString(parseResponse(mvcResult).get("data")), ShareInfoDTO.class);
// 校验数据是否正确
List<ShareInfo> shareInfos = extShareInfoMapper.selectByShareTypeAndShareApiIdWithBLOBs(ShareInfoType.SINGLE.name(), JSON.toJSONString(request).getBytes(), "zh_CN");
@ -93,7 +91,6 @@ public class ApiShareControllerTests extends BaseTest {
request.setType(ApiDefinitionDocType.MODULE.name());
request.setModuleIds(List.of("10001"));
MvcResult mvcResultModule = this.requestPostWithOkAndReturn(SHARE_DOC, request);
ApiDataUtils.setResolver(MsHTTPElement.class);
ShareInfoDTO shareInfoDTOModule = ApiDataUtils.parseObject(JSON.toJSONString(parseResponse(mvcResultModule).get("data")), ShareInfoDTO.class);
// 校验数据是否正确
List<ShareInfo> shareInfosModule = extShareInfoMapper.selectByShareTypeAndShareApiIdWithBLOBs(ShareInfoType.BATCH.name(), JSON.toJSONString(request).getBytes(), "zh_CN");
@ -108,7 +105,6 @@ public class ApiShareControllerTests extends BaseTest {
request.setProjectId(DEFAULT_PROJECT_ID);
request.setType(ApiDefinitionDocType.ALL.name());
MvcResult mvcResultAll = this.requestPostWithOkAndReturn(SHARE_DOC, request);
ApiDataUtils.setResolver(MsHTTPElement.class);
ShareInfoDTO allShareInfoDTO = ApiDataUtils.parseObject(JSON.toJSONString(parseResponse(mvcResultAll).get("data")), ShareInfoDTO.class);
// 校验数据是否正确
@ -129,7 +125,6 @@ public class ApiShareControllerTests extends BaseTest {
ShareInfo shareInfo = shareInfoMapper.selectByPrimaryKey(shareId);
ApiDefinitionDocRequest apiDefinitionDocRequest = ApiDataUtils.parseObject(new String(shareInfo.getCustomData()), ApiDefinitionDocRequest.class);
MvcResult mvcResultAll = this.requestGetWithOkAndReturn(SHARE_VIEW + shareId);
ApiDataUtils.setResolver(MsHTTPElement.class);
ApiDefinitionDocDTO allApiDefinitionDocDTO = ApiDataUtils.parseObject(JSON.toJSONString(parseResponse(mvcResultAll).get("data")), ApiDefinitionDocDTO.class);
// 校验数据是否正确
ApiDefinitionDocDTO copyAllApiDefinitionDocDTO = new ApiDefinitionDocDTO();

View File

@ -296,7 +296,6 @@ public class ApiTestCaseControllerTests extends BaseTest {
ApiTestCase copyApiDebug = BeanUtils.copyBean(new ApiTestCase(), apiCase);
BeanUtils.copyBean(copyApiDebug, request);
Assertions.assertEquals(apiCase, copyApiDebug);
ApiDataUtils.setResolver(MsHTTPElement.class);
Assertions.assertEquals(msHttpElement, ApiDataUtils.parseObject(new String(apiTestCaseBlob.getRequest()), AbstractMsTestElement.class));
return apiCase;
}
@ -384,7 +383,6 @@ public class ApiTestCaseControllerTests extends BaseTest {
// @@请求成功
MvcResult mvcResult = this.requestGetWithOk(GET + apiTestCase.getId())
.andReturn();
ApiDataUtils.setResolver(MsHTTPElement.class);
ApiTestCaseDTO apiDebugDTO = ApiDataUtils.parseObject(JSON.toJSONString(parseResponse(mvcResult).get("data")), ApiTestCaseDTO.class);
// 校验数据是否正确
ApiTestCase testCase = apiTestCaseMapper.selectByPrimaryKey(apiTestCase.getId());

View File

@ -44,9 +44,6 @@ import java.util.List;
* @CreateTime: 2023-11-07 11:17
*/
public class MsHTTPElementTest {
public MsHTTPElementTest() {
ApiDataUtils.setResolver(MsHTTPElement.class);
}
@Test
public void bodyTest() {

View File

@ -2,7 +2,6 @@ package io.metersphere.api.controller;
import com.fasterxml.jackson.databind.jsontype.NamedType;
import io.metersphere.api.dto.request.controller.MsLoopController;
import io.metersphere.api.dto.request.http.MsHTTPElement;
import io.metersphere.api.utils.ApiDataUtils;
import io.metersphere.plugin.api.dto.TestElementDTO;
import io.metersphere.plugin.api.spi.AbstractMsTestElement;
@ -15,6 +14,7 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMock
import org.springframework.boot.test.context.SpringBootTest;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Consumer;
@ -30,7 +30,6 @@ public class PluginSubTypeTests {
@Resource
private PluginLoadService pluginLoadService;
@Test
@Order(0)
public void resolverTest() throws Exception {
@ -56,18 +55,21 @@ public class PluginSubTypeTests {
public void jdbcPluginSubTypeTest() throws Exception {
// 上传 jdbc 插件
baseApiPluginTestService.addJdbcPlugin();
List<Class<? extends MsTestElement>> extensionClasses =
pluginLoadService.getMsPluginManager().getExtensionClasses(MsTestElement.class);
// 注册序列化类
extensionClasses.forEach(ApiDataUtils::setResolver);
String jdbcJson = """
{
"polymorphicName": "MsJDBCElement",
"test": "测试MsJDBCElement"
}
""";
List<Class<? extends MsTestElement>> extensionClasses =
pluginLoadService.getMsPluginManager().getExtensionClasses(MsTestElement.class);
// 注册序列化类
List<Class<?>> clazzList = new ArrayList<>(extensionClasses.size());
extensionClasses.forEach(clazzList::add);
ApiDataUtils.setResolvers(clazzList);
AbstractMsTestElement testElementDTO = ApiDataUtils.parseObject(jdbcJson, AbstractMsTestElement.class);
Assertions.assertNotNull(testElementDTO);
}
@ -82,8 +84,6 @@ public class PluginSubTypeTests {
}));
Assertions.assertTrue(isFuncSuccess((v) -> ApiDataUtils.parseObject("{")));
Assertions.assertTrue(isFuncSuccess((v) -> ApiDataUtils.parseArray(null, AbstractMsTestElement.class)));
ApiDataUtils.setResolver(MsHTTPElement.class);
// 检验 parseArray
String msHttpJson = """
[{