From 35c697674cb9f391588418edc04601badee454f9 Mon Sep 17 00:00:00 2001 From: jianxing Date: Tue, 8 Aug 2023 13:46:08 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E7=B3=BB=E7=BB=9F=E8=AE=BE=E7=BD=AE):=20?= =?UTF-8?q?=E6=8F=92=E4=BB=B6=E6=9B=B4=E6=96=B0=E6=8E=A5=E5=8F=A3=E4=B8=8D?= =?UTF-8?q?=E4=BC=A0name=E5=AD=97=E6=AE=B5=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/request/PluginUpdateRequest.java | 4 +- .../system/service/PluginService.java | 10 ++++- .../controller/PluginControllerTests.java | 45 +++++++++++-------- 3 files changed, 38 insertions(+), 21 deletions(-) diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/request/PluginUpdateRequest.java b/backend/services/system-setting/src/main/java/io/metersphere/system/request/PluginUpdateRequest.java index e1442113bf..5601fb29f8 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/request/PluginUpdateRequest.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/request/PluginUpdateRequest.java @@ -22,10 +22,10 @@ public class PluginUpdateRequest { private String name; @Schema(title = "是否启用插件, 默认启用") - private Boolean enable = true; + private Boolean enable; @Schema(title = "是否是全局插件, 默认全局") - private Boolean global = true; + private Boolean global; @Schema(title = "插件描述") @Size(min = 1, max = 500, message = "{plugin.scenario.length_range}", groups = {Created.class, Updated.class}) diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/PluginService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/PluginService.java index 1b8a3b8902..d2aa37c46c 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/PluginService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/PluginService.java @@ -16,6 +16,8 @@ import io.metersphere.system.mapper.PluginMapper; import io.metersphere.system.request.PluginUpdateRequest; import jakarta.annotation.Resource; import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.BooleanUtils; +import org.apache.commons.lang3.StringUtils; import org.springframework.kafka.core.KafkaTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -74,6 +76,9 @@ public class PluginService { plugin.setFileName(file.getOriginalFilename()); plugin.setCreateTime(System.currentTimeMillis()); plugin.setUpdateTime(System.currentTimeMillis()); + // 如果没有,默认设置为 true + request.setEnable(!BooleanUtils.isFalse(request.getEnable())); + request.setEnable(!BooleanUtils.isFalse(request.getEnable())); // 校验重名 checkPluginAddExist(plugin); @@ -161,7 +166,7 @@ public class PluginService { // 校验重名 checkPluginUpdateExist(plugin); pluginMapper.updateByPrimaryKeySelective(plugin); - if (request.getGlobal()) { + if (BooleanUtils.isTrue(request.getGlobal())) { // 全局插件,删除和组织的关联关系 request.setOrganizationIds(new ArrayList<>(0)); } @@ -170,6 +175,9 @@ public class PluginService { } private void checkPluginUpdateExist(Plugin plugin) { + if (StringUtils.isBlank(plugin.getName())) { + return; + } PluginExample example = new PluginExample(); example.createCriteria() .andIdNotEqualTo(plugin.getId()) 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 ed83729c15..8cb302d7e5 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 @@ -78,7 +78,7 @@ public class PluginControllerTests extends BaseTest { request.setName("test"); request.setDescription("test desc"); - request.setGlobal(false); + request.setGlobal(true); request.setEnable(false); request.setOrganizationIds(Arrays.asList(org.getId())); MultiValueMap multiValueMap = getDefaultMultiPartParam(request, jarFile); @@ -94,10 +94,27 @@ public class PluginControllerTests extends BaseTest { Assertions.assertEquals(plugin.getXpack(), false); Assertions.assertEquals(plugin.getFileName(), jarFile.getName()); Assertions.assertEquals(plugin.getScenario(), PluginScenarioType.API.name()); - Assertions.assertEquals(Arrays.asList(org.getId()), getOrgIdsByPlugId(plugin.getId())); + Assertions.assertEquals(new ArrayList<>(0), getOrgIdsByPlugId(plugin.getId())); Assertions.assertEquals(Arrays.asList("connect", "disconnect", "pub", "sub"), getScriptIdsByPlugId(plugin.getId())); addPlugin = plugin; + // 增加覆盖率 + this.requestGetWithOkAndReturn(DEFAULT_LIST); + + // 校验 global 为 tru e时,organizationIds 为空 + request.setGlobal(false); + request.setEnable(true); + request.setName("test2"); + request.setOrganizationIds(Arrays.asList(org.getId())); + MvcResult antoherMvcResult = this.requestMultipartWithOkAndReturn(DEFAULT_ADD, + getDefaultMultiPartParam(request, anotherJarFile)); + Plugin antoherPlugin = pluginMapper.selectByPrimaryKey(getResultData(antoherMvcResult, Plugin.class).getId()); + Assertions.assertEquals(antoherPlugin.getEnable(), request.getEnable()); + Assertions.assertEquals(antoherPlugin.getGlobal(), request.getGlobal()); + Assertions.assertEquals(Arrays.asList(org.getId()), getOrgIdsByPlugId(antoherPlugin.getId())); + anotherAddPlugin = antoherPlugin; + addPlugin = plugin; + // @@重名校验异常 // 校验插件名称重名 assertErrorCode(this.requestMultipart(DEFAULT_ADD, @@ -132,20 +149,6 @@ public class PluginControllerTests extends BaseTest { assertErrorCode(this.requestMultipart(DEFAULT_ADD, getDefaultMultiPartParam(request, scriptIdRepeatFile)), PLUGIN_SCRIPT_EXIST); - request.setGlobal(true); - request.setEnable(true); - request.setName("test2"); - MvcResult antoherMvcResult = this.requestMultipartWithOkAndReturn(DEFAULT_ADD, - getDefaultMultiPartParam(request, anotherJarFile)); - // 校验 global 为 tru e时,organizationIds 为空 - Plugin antoherPlugin = pluginMapper.selectByPrimaryKey(getResultData(antoherMvcResult, Plugin.class).getId()); - Assertions.assertEquals(antoherPlugin.getEnable(), request.getEnable()); - Assertions.assertEquals(antoherPlugin.getGlobal(), request.getGlobal()); - Assertions.assertEquals(new ArrayList<>(0), getOrgIdsByPlugId(antoherPlugin.getId())); - anotherAddPlugin = antoherPlugin; - - this.addPlugin = plugin; - // @@校验日志 checkLog(this.addPlugin.getId(), OperationLogType.ADD); // @@校验权限 @@ -185,12 +188,18 @@ public class PluginControllerTests extends BaseTest { // 校验 global 为 false 时,organizationIds 数据 request.setGlobal(false); this.requestPostWithOk(DEFAULT_UPDATE, request); - Assertions.assertEquals(Arrays.asList(org.getId()), getOrgIdsByPlugId(plugin.getId())); + Assertions.assertEquals(Arrays.asList(org.getId()), getOrgIdsByPlugId(request.getId())); + + // 只修改启用禁用 + PluginUpdateRequest activeRequest = new PluginUpdateRequest(); + activeRequest.setId(request.getId()); + activeRequest.setEnable(true); + this.requestPostWithOk(DEFAULT_UPDATE, activeRequest); // 校验组织为null,不修改关联关系 request.setOrganizationIds(null); this.requestPostWithOk(DEFAULT_UPDATE, request); - Assertions.assertEquals(Arrays.asList(org.getId()), getOrgIdsByPlugId(plugin.getId())); + Assertions.assertEquals(Arrays.asList(org.getId()), getOrgIdsByPlugId(request.getId())); // @@重名校验异常 request.setName(anotherAddPlugin.getName());