From 835beb3b49dce3e47b242b62716a42d9349095c0 Mon Sep 17 00:00:00 2001 From: AgAngle <1323481023@qq.com> Date: Sun, 5 Nov 2023 12:24:15 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E5=AE=9A=E4=B9=89=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E5=92=8C=E8=BD=AC=E6=8D=A2=E5=99=A8=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../spi/AbstractJmeterElementConverter.java | 72 ++++++++++++++++ .../plugin/api/spi/AbstractMsTestElement.java | 25 ++++++ .../plugin/api/spi/MsParameter.java | 8 ++ .../plugin/api/spi/MsTestElement.java | 17 ++++ .../sdk/dto/api/request/MsScenario.java | 10 +++ .../sdk/dto/api/request/ParameterConfig.java | 10 +++ .../dto/api/request/http/MsHTTPElement.java | 11 +++ .../api/dto/request/ParameterConfig.java | 10 +++ .../api/controller/PluginSubTypeTests.java | 31 +++++++ .../test/resources/file/jdbc-sampler-v3.x.jar | Bin 0 -> 5949 bytes .../system/service/PluginScriptService.java | 3 + .../system/base/BaseApiPluginTestService.java | 77 ++++++++++++++++++ .../controller/PluginControllerTests.java | 8 +- 13 files changed, 278 insertions(+), 4 deletions(-) create mode 100644 backend/framework/plugin/plugin-api-sdk/src/main/java/io/metersphere/plugin/api/spi/AbstractJmeterElementConverter.java create mode 100644 backend/framework/plugin/plugin-api-sdk/src/main/java/io/metersphere/plugin/api/spi/AbstractMsTestElement.java create mode 100644 backend/framework/plugin/plugin-api-sdk/src/main/java/io/metersphere/plugin/api/spi/MsParameter.java create mode 100644 backend/framework/plugin/plugin-api-sdk/src/main/java/io/metersphere/plugin/api/spi/MsTestElement.java create mode 100644 backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/request/MsScenario.java create mode 100644 backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/request/ParameterConfig.java create mode 100644 backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/request/http/MsHTTPElement.java create mode 100644 backend/services/api-test/src/main/java/io/metersphere/api/dto/request/ParameterConfig.java create mode 100644 backend/services/api-test/src/test/resources/file/jdbc-sampler-v3.x.jar create mode 100644 backend/services/system-setting/src/test/java/io/metersphere/system/base/BaseApiPluginTestService.java diff --git a/backend/framework/plugin/plugin-api-sdk/src/main/java/io/metersphere/plugin/api/spi/AbstractJmeterElementConverter.java b/backend/framework/plugin/plugin-api-sdk/src/main/java/io/metersphere/plugin/api/spi/AbstractJmeterElementConverter.java new file mode 100644 index 0000000000..fe6a1cdd44 --- /dev/null +++ b/backend/framework/plugin/plugin-api-sdk/src/main/java/io/metersphere/plugin/api/spi/AbstractJmeterElementConverter.java @@ -0,0 +1,72 @@ +package io.metersphere.plugin.api.spi; + + +import org.apache.jorphan.collections.HashTree; + +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.HashMap; +import java.util.Map; + +/** + * @author jianxing + * @createTime 2021-10-30 10:07 + * 将 MsTestElement 具体实现类转换为 HashTree + */ +public abstract class AbstractJmeterElementConverter { + + private Class msTestElementClass; + /** + * 解析器集合 + * key 为 MsTestElement 实现类的 Class + * value 为对应的转换器 + */ + private static final Map, AbstractJmeterElementConverter> parserMap = new HashMap(); + + public AbstractJmeterElementConverter() { + Type genericSuperclass = getClass().getGenericSuperclass(); + if (genericSuperclass instanceof ParameterizedType) { + ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass; + // 获取泛型的具体类型,即 MsTestElement 的具体实现类 + msTestElementClass = (Class) parameterizedType.getActualTypeArguments()[0]; + // 注册到解析器集合中 + parserMap.put(msTestElementClass, this); + } + } + + + /** + * 将 MsTestElement 具体实现类转换为 HashTree + * + * @param tree + * @param msTestElement + * @param config + */ + public abstract void toHashTree(HashTree tree, T msTestElement, MsParameter config); + + /** + * 解析 MsTestElement 的子节点 + * + * @param tree + * @param msTestElement + * @param config + */ + public void parseChild(HashTree tree, AbstractMsTestElement msTestElement, MsParameter config) { + if (msTestElement != null && msTestElement.getChildren() != null) { + for (MsTestElement testElement : msTestElement.getChildren()) { + AbstractJmeterElementConverter converter = getConverter(testElement); + converter.toHashTree(tree, msTestElement, config); + } + } + } + + /** + * 获取对应组件的转换器 + * + * @param msTestElement + * @return + */ + public static AbstractJmeterElementConverter getConverter(MsTestElement msTestElement) { + return parserMap.get(msTestElement.getClass()); + } +} diff --git a/backend/framework/plugin/plugin-api-sdk/src/main/java/io/metersphere/plugin/api/spi/AbstractMsTestElement.java b/backend/framework/plugin/plugin-api-sdk/src/main/java/io/metersphere/plugin/api/spi/AbstractMsTestElement.java new file mode 100644 index 0000000000..f0d8b061e4 --- /dev/null +++ b/backend/framework/plugin/plugin-api-sdk/src/main/java/io/metersphere/plugin/api/spi/AbstractMsTestElement.java @@ -0,0 +1,25 @@ +package io.metersphere.plugin.api.spi; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonTypeInfo; +import lombok.Data; + +import java.util.LinkedList; + +/** +*@Author: jianxing +*@CreateTime: 2023-10-30 15:08 +*/ +@Data +@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "polymorphicName") +@JsonIgnoreProperties(ignoreUnknown = true) +public abstract class AbstractMsTestElement implements MsTestElement { + + // 组件标签名称 + private String name; + // 是否启用 + private Boolean enable = true; + + // 子组件 + private LinkedList children; +} diff --git a/backend/framework/plugin/plugin-api-sdk/src/main/java/io/metersphere/plugin/api/spi/MsParameter.java b/backend/framework/plugin/plugin-api-sdk/src/main/java/io/metersphere/plugin/api/spi/MsParameter.java new file mode 100644 index 0000000000..d952b881a2 --- /dev/null +++ b/backend/framework/plugin/plugin-api-sdk/src/main/java/io/metersphere/plugin/api/spi/MsParameter.java @@ -0,0 +1,8 @@ +package io.metersphere.plugin.api.spi; + +/** + * @Author: jianxing + * @CreateTime: 2023-10-27 17:30 + */ +public class MsParameter { +} diff --git a/backend/framework/plugin/plugin-api-sdk/src/main/java/io/metersphere/plugin/api/spi/MsTestElement.java b/backend/framework/plugin/plugin-api-sdk/src/main/java/io/metersphere/plugin/api/spi/MsTestElement.java new file mode 100644 index 0000000000..f3fd7945ac --- /dev/null +++ b/backend/framework/plugin/plugin-api-sdk/src/main/java/io/metersphere/plugin/api/spi/MsTestElement.java @@ -0,0 +1,17 @@ +package io.metersphere.plugin.api.spi; + +import org.pf4j.ExtensionPoint; + +/** + * MS 中的测试组件 + * 即场景中的每个步骤组件 + * 或者接口定义对应的协议组件 + */ +public interface MsTestElement extends ExtensionPoint { + +} + + + + + diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/request/MsScenario.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/request/MsScenario.java new file mode 100644 index 0000000000..776848d8ed --- /dev/null +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/request/MsScenario.java @@ -0,0 +1,10 @@ +package io.metersphere.sdk.dto.api.request; + +import io.metersphere.plugin.api.spi.AbstractMsTestElement; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@Data +@EqualsAndHashCode(callSuper = true) +public class MsScenario extends AbstractMsTestElement { +} diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/request/ParameterConfig.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/request/ParameterConfig.java new file mode 100644 index 0000000000..f75394e534 --- /dev/null +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/request/ParameterConfig.java @@ -0,0 +1,10 @@ +package io.metersphere.sdk.dto.api.request; + +import io.metersphere.plugin.api.spi.MsParameter; + +/** + * @Author: jianxing + * @CreateTime: 2023-10-27 17:30 + */ +public class ParameterConfig extends MsParameter { +} diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/request/http/MsHTTPElement.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/request/http/MsHTTPElement.java new file mode 100644 index 0000000000..06b19ac863 --- /dev/null +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/api/request/http/MsHTTPElement.java @@ -0,0 +1,11 @@ +package io.metersphere.sdk.dto.api.request.http; + +import io.metersphere.plugin.api.spi.AbstractMsTestElement; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@Data +@EqualsAndHashCode(callSuper = true) +public class MsHTTPElement extends AbstractMsTestElement { + private String domain; +} \ No newline at end of file diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/ParameterConfig.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/ParameterConfig.java new file mode 100644 index 0000000000..09cbee661f --- /dev/null +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/request/ParameterConfig.java @@ -0,0 +1,10 @@ +package io.metersphere.api.dto.request; + +import io.metersphere.plugin.api.spi.MsParameter; + +/** + * @Author: jianxing + * @CreateTime: 2023-10-27 17:30 + */ +public class ParameterConfig extends MsParameter { +} diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/PluginSubTypeTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/PluginSubTypeTests.java index 2f0e35efae..c9d1dc3fbb 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/controller/PluginSubTypeTests.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/PluginSubTypeTests.java @@ -3,10 +3,15 @@ package io.metersphere.api.controller; import com.fasterxml.jackson.databind.jsontype.NamedType; import io.metersphere.api.util.ApiDataUtils; import io.metersphere.plugin.api.dto.TestElementDTO; +import io.metersphere.plugin.api.spi.AbstractMsTestElement; +import io.metersphere.plugin.api.spi.MsTestElement; import io.metersphere.sdk.dto.api.request.logic.controller.MsLoopController; import io.metersphere.sdk.dto.api.request.post.processors.MsPostJSR223Processor; import io.metersphere.sdk.dto.api.request.sampler.MsDebugSampler; +import io.metersphere.system.base.BaseApiPluginTestService; +import io.metersphere.system.service.PluginLoadService; import io.metersphere.system.uid.IDGenerator; +import jakarta.annotation.Resource; import org.junit.jupiter.api.*; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; @@ -20,6 +25,11 @@ import java.util.List; @AutoConfigureMockMvc public class PluginSubTypeTests { + @Resource + private BaseApiPluginTestService baseApiPluginTestService; + @Resource + private PluginLoadService pluginLoadService; + @Test @Order(0) @@ -79,4 +89,25 @@ public class PluginSubTypeTests { TestElementDTO testElementDTO = ApiDataUtils.parseObject(json, TestElementDTO.class); Assertions.assertNotNull(testElementDTO); } + + @Test + @Order(4) + public void jdbcPluginSubTypeTest() throws Exception { + // 上传 jdbc 插件 + baseApiPluginTestService.addJdbcPlugin(); + List> extensionClasses = + pluginLoadService.getMsPluginManager().getExtensionClasses(MsTestElement.class); + + // 注册序列化类 + extensionClasses.forEach(ApiDataUtils::setResolver); + + String jdbcJson = """ + { + "polymorphicName": "MsJDBCElement", + "test": "测试MsJDBCElement" + } + """; + AbstractMsTestElement testElementDTO = ApiDataUtils.parseObject(jdbcJson, AbstractMsTestElement.class); + Assertions.assertNotNull(testElementDTO); + } } diff --git a/backend/services/api-test/src/test/resources/file/jdbc-sampler-v3.x.jar b/backend/services/api-test/src/test/resources/file/jdbc-sampler-v3.x.jar new file mode 100644 index 0000000000000000000000000000000000000000..96819f90c61bd9e4fb7478e6a24830258f9d262b GIT binary patch literal 5949 zcmb7IcRbYpAHQrmE1Ss5D&x#cM#fp^>?nJk74F=b$=*UF6q4*w_ROfL_?}IYY>@~l zTgLCSl*ae>ec$8r`P?6`=kxu3zuxcn>-By;wV=3oB!K-!iL^5K{O9Y782g1m^yK(e zHIxLkzM2sMD6nQ8gne6;`;a350QHY%FgXoXC5Wz`089yT4HVNPL;>-JHU)(O)PDsb^Xrj;C04Jl0c@m3~!>+I3-_deNdo@LQ1zig)3mhe2w82t?<<*@J#O9&e!7TL@y8s`&VC@{U%CZM9^Jh3;S8(C2=LoIhK2A}WVACMd z)h$T9}b*a+_DL3Z^4g*xonk()h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RAv=T>HuPfGhQ1xele?Z^T4^-zDE~)L8-3 z|Bd)-Dn9Vwm;HJ;E+6poQU2n=Lr(Z<;QRasQCbU%Pq0r#iT#P=0{|beWB|bb0Ee6G A)Bpeg literal 0 HcmV?d00001 diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/PluginScriptService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/PluginScriptService.java index 4d6bad0e5b..4c636fb0e4 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/PluginScriptService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/PluginScriptService.java @@ -29,6 +29,9 @@ public class PluginScriptService { private ExtPluginScriptMapper extPluginScriptMapper; public void add(String pluginId, List frontendScript) { + if (CollectionUtils.isEmpty(frontendScript)) { + return; + } Set ids = new HashSet<>(); List pluginScripts = new ArrayList<>(frontendScript.size()); for (String script : frontendScript) { diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/base/BaseApiPluginTestService.java b/backend/services/system-setting/src/test/java/io/metersphere/system/base/BaseApiPluginTestService.java new file mode 100644 index 0000000000..0d202df614 --- /dev/null +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/base/BaseApiPluginTestService.java @@ -0,0 +1,77 @@ +package io.metersphere.system.base; + +import io.metersphere.system.domain.Plugin; +import io.metersphere.system.dto.request.PluginUpdateRequest; +import io.metersphere.system.mapper.PluginMapper; +import io.metersphere.system.service.PluginService; +import jakarta.annotation.Resource; +import org.springframework.mock.web.MockMultipartFile; +import org.springframework.stereotype.Service; + +import java.io.File; +import java.io.FileInputStream; + +import static io.metersphere.sdk.constants.InternalUserRole.ADMIN; + +/** + * @Author: jianxing + * @CreateTime: 2023-10-20 11:32 + */ +@Service +public class BaseApiPluginTestService { + + @Resource + private PluginService pluginService; + @Resource + private PluginMapper pluginMapper; + private static Plugin jdbcPlugin; + + + /** + * 添加插件,供测试使用 + * + * @return + * @throws Exception + */ + public Plugin addJdbcPlugin() throws Exception { + if (hasJdbcPlugin()) { + return jdbcPlugin; + } + PluginUpdateRequest request = new PluginUpdateRequest(); + File jarFile = new File( + this.getClass().getClassLoader().getResource("file/jdbc-sampler-v3.x.jar") + .getPath() + ); + FileInputStream inputStream = new FileInputStream(jarFile); + MockMultipartFile mockMultipartFile = new MockMultipartFile(jarFile.getName(), jarFile.getName(), "jar", inputStream); + request.setName("测试jdbc插件"); + request.setGlobal(true); + request.setEnable(true); + request.setCreateUser(ADMIN.name()); + jdbcPlugin = pluginService.add(request, mockMultipartFile); + return jdbcPlugin; + } + + + public Plugin getJdbcPlugin() throws Exception { + if (!hasJdbcPlugin()) { + return this.addJdbcPlugin(); + } + return jdbcPlugin; + } + + public boolean hasJdbcPlugin() { + if (jdbcPlugin != null) { + return true; + } + jdbcPlugin = pluginMapper.selectByPrimaryKey("jdbc"); + return jdbcPlugin != null; + } + + public void deleteJdbcPlugin() { + if (jdbcPlugin != null) { + pluginService.delete(jdbcPlugin.getId()); + jdbcPlugin = null; + } + } +} diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/PluginControllerTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/PluginControllerTests.java index ca50f86970..329b5ed95d 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/PluginControllerTests.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/PluginControllerTests.java @@ -17,10 +17,7 @@ import io.metersphere.system.mapper.PluginOrganizationMapper; import io.metersphere.system.mapper.PluginScriptMapper; import io.metersphere.system.dto.request.PlatformOptionRequest; import io.metersphere.system.dto.request.PluginUpdateRequest; -import io.metersphere.system.service.JdbcDriverPluginService; -import io.metersphere.system.service.OrganizationService; -import io.metersphere.system.service.PluginService; -import io.metersphere.system.service.UserLoginService; +import io.metersphere.system.service.*; import jakarta.annotation.Resource; import org.apache.commons.lang3.StringUtils; import org.junit.jupiter.api.*; @@ -68,6 +65,8 @@ public class PluginControllerTests extends BaseTest { private OrganizationService organizationService; @Resource private JdbcDriverPluginService jdbcDriverPluginService; + @Resource + private PluginScriptService pluginScriptService; private static Plugin addPlugin; private static Plugin anotherAddPlugin; @@ -131,6 +130,7 @@ public class PluginControllerTests extends BaseTest { // 增加覆盖率 this.requestGetWithOkAndReturn(DEFAULT_LIST); + pluginScriptService.add(null, null); // 校验 global 为 tru e时,organizationIds 为空 request.setGlobal(false);